summaryrefslogtreecommitdiffstats
path: root/src/object/sp-star.cpp
blob: 11835dc8298298c89914db3e0a83a9858278c1ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * <sodipodi:star> implementation
 *
 * Authors:
 *   Mitsuru Oka <oka326@parkcity.ne.jp>
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   bulia byak <buliabyak@users.sf.net>
 *   Abhishek Sharma
 *
 * Copyright (C) 1999-2002 Lauris Kaplinski
 * Copyright (C) 2000-2001 Ximian, Inc.
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <cstring>
#include <string>
#include <glib.h>
#include "live_effects/effect.h"

#include "svg/svg.h"
#include "attributes.h"
#include "display/curve.h"
#include "xml/repr.h"
#include "document.h"

#include "sp-star.h"
#include <glibmm/i18n.h>

SPStar::SPStar() : SPPolygon() ,
	sides(5),
	center(0, 0),
	flatsided(false),
	rounded(0.0),
	randomized(0.0)
{
	this->r[0] = 1.0;
	this->r[1] = 0.001;
	this->arg[0] = this->arg[1] = 0.0;
}

SPStar::~SPStar() = default;

void SPStar::build(SPDocument * document, Inkscape::XML::Node * repr) {
	// CPPIFY: see header file
    SPShape::build(document, repr);

    this->readAttr( "sodipodi:cx" );
    this->readAttr( "sodipodi:cy" );
    this->readAttr( "sodipodi:sides" );
    this->readAttr( "sodipodi:r1" );
    this->readAttr( "sodipodi:r2" );
    this->readAttr( "sodipodi:arg1" );
    this->readAttr( "sodipodi:arg2" );
    this->readAttr( "inkscape:flatsided" );
    this->readAttr( "inkscape:rounded" );
    this->readAttr( "inkscape:randomized" );
}

Inkscape::XML::Node* SPStar::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) {
    if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
        repr = xml_doc->createElement("svg:path");
    }

    if (flags & SP_OBJECT_WRITE_EXT) {
        repr->setAttribute("sodipodi:type", "star");
        sp_repr_set_int (repr, "sodipodi:sides", this->sides);
        sp_repr_set_svg_double(repr, "sodipodi:cx", this->center[Geom::X]);
        sp_repr_set_svg_double(repr, "sodipodi:cy", this->center[Geom::Y]);
        sp_repr_set_svg_double(repr, "sodipodi:r1", this->r[0]);
        sp_repr_set_svg_double(repr, "sodipodi:r2", this->r[1]);
        sp_repr_set_svg_double(repr, "sodipodi:arg1", this->arg[0]);
        sp_repr_set_svg_double(repr, "sodipodi:arg2", this->arg[1]);
        sp_repr_set_boolean (repr, "inkscape:flatsided", this->flatsided);
        sp_repr_set_svg_double(repr, "inkscape:rounded", this->rounded);
        sp_repr_set_svg_double(repr, "inkscape:randomized", this->randomized);
    }

    this->set_shape();
    if (this->_curve) {
        char *d = sp_svg_write_path (this->_curve->get_pathvector());
        repr->setAttribute("d", d);
        g_free(d);
    } else {
        repr->removeAttribute("d");
    }
    // CPPIFY: see header file
    SPShape::write(xml_doc, repr, flags);

    return repr;
}

void SPStar::set(SPAttributeEnum key, const gchar* value) {
    SVGLength::Unit unit;

    /* fixme: we should really collect updates */
    switch (key) {
    case SP_ATTR_SODIPODI_SIDES:
        if (value) {
            this->sides = atoi (value);
            this->sides = CLAMP(this->sides, 3, 1024);
        } else {
            this->sides = 5;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_SODIPODI_CX:
        if (!sp_svg_length_read_ldd (value, &unit, nullptr, &this->center[Geom::X]) ||
            (unit == SVGLength::EM) ||
            (unit == SVGLength::EX) ||
            (unit == SVGLength::PERCENT)) {
            this->center[Geom::X] = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_SODIPODI_CY:
        if (!sp_svg_length_read_ldd (value, &unit, nullptr, &this->center[Geom::Y]) ||
            (unit == SVGLength::EM) ||
            (unit == SVGLength::EX) ||
            (unit == SVGLength::PERCENT)) {
            this->center[Geom::Y] = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_SODIPODI_R1:
        if (!sp_svg_length_read_ldd (value, &unit, nullptr, &this->r[0]) ||
            (unit == SVGLength::EM) ||
            (unit == SVGLength::EX) ||
            (unit == SVGLength::PERCENT)) {
            this->r[0] = 1.0;
        }

        /* fixme: Need CLAMP (Lauris) */
        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_SODIPODI_R2:
        if (!sp_svg_length_read_ldd (value, &unit, nullptr, &this->r[1]) ||
            (unit == SVGLength::EM) ||
            (unit == SVGLength::EX) ||
            (unit == SVGLength::PERCENT)) {
            this->r[1] = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        return;

    case SP_ATTR_SODIPODI_ARG1:
        if (value) {
            this->arg[0] = g_ascii_strtod (value, nullptr);
        } else {
            this->arg[0] = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_SODIPODI_ARG2:
        if (value) {
            this->arg[1] = g_ascii_strtod (value, nullptr);
        } else {
            this->arg[1] = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_INKSCAPE_FLATSIDED:
        if (value && !strcmp(value, "true")) {
            this->flatsided = true;
        } else {
        	this->flatsided = false;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_INKSCAPE_ROUNDED:
        if (value) {
            this->rounded = g_ascii_strtod (value, nullptr);
        } else {
            this->rounded = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    case SP_ATTR_INKSCAPE_RANDOMIZED:
        if (value) {
            this->randomized = g_ascii_strtod (value, nullptr);
        } else {
            this->randomized = 0.0;
        }

        this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
        break;

    default:
    	// CPPIFY: see header file
        SPShape::set(key, value);
        break;
    }
}

void SPStar::update(SPCtx *ctx, guint flags) {
    if (flags & (SP_OBJECT_MODIFIED_FLAG |
             SP_OBJECT_STYLE_MODIFIED_FLAG |
             SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {

        this->set_shape();
    }

    // CPPIFY: see header file
    SPShape::update(ctx, flags);
}


const char* SPStar::displayName() const {
    if (this->flatsided == false)
        return _("Star");
    return _("Polygon");
}

gchar* SPStar::description() const {
    // while there will never be less than 3 vertices, we still need to
    // make calls to ngettext because the pluralization may be different
    // for various numbers >=3.  The singular form is used as the index.
    return g_strdup_printf (ngettext(_("with %d vertex"), _("with %d vertices"),
                this->sides), this->sides);
}

/**
Returns a unit-length vector at 90 degrees to the direction from o to n
 */
static Geom::Point
rot90_rel (Geom::Point o, Geom::Point n)
{
    return ((1/Geom::L2(n - o)) * Geom::Point ((n - o)[Geom::Y],  (o - n)[Geom::X]));
}

/**
Returns a unique 32 bit int for a given point.
Obvious (but acceptable for my purposes) limits to uniqueness:
- returned value for x,y repeats for x+n*1024,y+n*1024
- returned value is unchanged when the point is moved by less than 1/1024 of px
*/
static guint32
point_unique_int (Geom::Point o)
{
    return ((guint32)
    65536 *
        (((int) floor (o[Geom::X] * 64)) % 1024 + ((int) floor (o[Geom::X] * 1024)) % 64)
    +
             (((int) floor (o[Geom::Y] * 64)) % 1024 + ((int) floor (o[Geom::Y] * 1024)) % 64)
    );
}

/**
Returns the next pseudorandom value using the Linear Congruential Generator algorithm (LCG)
with the parameters (m = 2^32, a = 69069, b = 1). These parameters give a full-period generator,
i.e. it is guaranteed to go through all integers < 2^32 (see http://random.mat.sbg.ac.at/~charly/server/server.html)
*/
static inline guint32
lcg_next(guint32 const prev)
{
    return (guint32) ( 69069 * prev + 1 );
}

/**
Returns a random number in the range [-0.5, 0.5) from the given seed, stepping the given number of steps from the seed.
*/
static double
rnd (guint32 const seed, unsigned steps) {
    guint32 lcg = seed;
    for (; steps > 0; steps --)
        lcg = lcg_next (lcg);

    return ( lcg / 4294967296. ) - 0.5;
}

static Geom::Point
sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
{
    // the point whose neighboring curve handle we're calculating
    Geom::Point o = sp_star_get_xy (star, point, index);

    // indices of previous and next points
    gint pi = (index > 0)? (index - 1) : (star->sides - 1);
    gint ni = (index < star->sides - 1)? (index + 1) : 0;

    // the other point type
    SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);

    // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
    Geom::Point prev = (star->flatsided? sp_star_get_xy (star, point, pi) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT2? index : pi));
    Geom::Point next = (star->flatsided? sp_star_get_xy (star, point, ni) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT1? index : ni));

    // prev-next midpoint
    Geom::Point mid =  0.5 * (prev + next);

    // point to which we direct the bissector of the curve handles;
    // it's far enough outside the star on the perpendicular to prev-next through mid
    Geom::Point biss =  mid + 100000 * rot90_rel (mid, next);

    // lengths of vectors to prev and next
    gdouble prev_len = Geom::L2 (prev - o);
    gdouble next_len = Geom::L2 (next - o);

    // unit-length vector perpendicular to o-biss
    Geom::Point rot = rot90_rel (o, biss);

    // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
    Geom::Point ret;
    if (previ) {
        ret = (star->rounded * prev_len) * rot;
    } else {
        ret = (star->rounded * next_len * -1) * rot;
    }

    if (star->randomized == 0) {
        // add the vector to o to get the final curvepoint
        return o + ret;
    } else {
        // the seed corresponding to the exact point
        guint32 seed = point_unique_int (o);

        // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
        ret = ret * Geom::Affine (Geom::Rotate (star->randomized * M_PI * rnd (seed, 3)));
        ret *= ( 1 + star->randomized * rnd (seed, 4));

        // the randomized corner point
        Geom::Point o_randomized = sp_star_get_xy (star, point, index, true);

        return o_randomized + ret;
    }
}

#define NEXT false
#define PREV true

void SPStar::set_shape() {
    // perhaps we should convert all our shapes into LPEs without source path
    // and with knotholders for parameters, then this situation will be handled automatically
    // by disabling the entire stack (including the shape LPE)
    if (hasBrokenPathEffect()) {
        g_warning ("The star shape has unknown LPE on it! Convert to path to make it editable preserving the appearance; editing it as star will remove the bad LPE");

        if (this->getRepr()->attribute("d")) {
            // unconditionally read the curve from d, if any, to preserve appearance
            Geom::PathVector pv = sp_svg_read_pathv(this->getRepr()->attribute("d"));
            SPCurve *cold = new SPCurve(pv);
            this->setCurveInsync(cold);
            this->setCurveBeforeLPE(cold);
            cold->unref();
        }

        return;
    }

    SPCurve *c = new SPCurve ();

    bool not_rounded = (fabs (this->rounded) < 1e-4);

    // note that we pass randomized=true to sp_star_get_xy, because the curve must be randomized;
    // other places that call that function (e.g. the knotholder) need the exact point

    // draw 1st segment
    c->moveto(sp_star_get_xy (this, SP_STAR_POINT_KNOT1, 0, true));

    if (this->flatsided == false) {
        if (not_rounded) {
            c->lineto(sp_star_get_xy (this, SP_STAR_POINT_KNOT2, 0, true));
        } else {
            c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, 0, NEXT),
                sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, 0, PREV),
                sp_star_get_xy (this, SP_STAR_POINT_KNOT2, 0, true));
        }
    }

    // draw all middle segments
    for (gint i = 1; i < sides; i++) {
        if (not_rounded) {
            c->lineto(sp_star_get_xy (this, SP_STAR_POINT_KNOT1, i, true));
        } else {
            if (this->flatsided == false) {
                c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, i - 1, NEXT),
                        sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i, PREV),
                        sp_star_get_xy (this, SP_STAR_POINT_KNOT1, i, true));
            } else {
                c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i - 1, NEXT),
                        sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i, PREV),
                        sp_star_get_xy (this, SP_STAR_POINT_KNOT1, i, true));
            }
        }

        if (this->flatsided == false) {
            if (not_rounded) {
                       c->lineto(sp_star_get_xy (this, SP_STAR_POINT_KNOT2, i, true));
            } else {
                c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i, NEXT),
                    sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, i, PREV),
                    sp_star_get_xy (this, SP_STAR_POINT_KNOT2, i, true));
            }
        }
    }

    // draw last segment
	if (!not_rounded) {
		if (this->flatsided == false) {
			c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
				sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, 0, PREV),
				sp_star_get_xy (this, SP_STAR_POINT_KNOT1, 0, true));
		} else {
			c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
				sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, 0, PREV),
				sp_star_get_xy (this, SP_STAR_POINT_KNOT1, 0, true));
		}
	}

    c->closepath();

    /* Reset the shape's curve to the "original_curve"
     * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
    SPCurve * before = this->getCurveBeforeLPE();
    bool haslpe = this->hasPathEffectOnClipOrMaskRecursive(this);
    if (before || haslpe) {
        if (c && before && before->get_pathvector() != c->get_pathvector()){
            this->setCurveBeforeLPE(c);
            sp_lpe_item_update_patheffect(this, true, false);
        } else if(haslpe) {
            this->setCurveBeforeLPE(c);
        } else {
            //This happends on undo, fix bug:#1791784
            this->setCurveInsync(c);
        }
    } else {
        this->setCurveInsync(c);
    }
    if (before) {
        before->unref();
    }
    c->unref();
}

void
sp_star_position_set (SPStar *star, gint sides, Geom::Point center, gdouble r1, gdouble r2, gdouble arg1, gdouble arg2, bool isflat, double rounded, double randomized)
{
    g_return_if_fail (star != nullptr);
    g_return_if_fail (SP_IS_STAR (star));

    star->sides = CLAMP(sides, 3, 1024);
    star->center = center;
    star->r[0] = MAX (r1, 0.001);

    if (isflat == false) {
        star->r[1] = CLAMP(r2, 0.0, star->r[0]);
    } else {
        star->r[1] = CLAMP( r1*cos(M_PI/sides) ,0.0, star->r[0] );
    }

    star->arg[0] = arg1;
    star->arg[1] = arg2;
    star->flatsided = isflat;
    star->rounded = rounded;
    star->randomized = randomized;
    star->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
}

void SPStar::snappoints(std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs) const {
    // We will determine the star's midpoint ourselves, instead of trusting on the base class
    // Therefore snapping to object midpoints is temporarily disabled
    Inkscape::SnapPreferences local_snapprefs = *snapprefs;
    local_snapprefs.setTargetSnappable(Inkscape::SNAPTARGET_OBJECT_MIDPOINT, false);

    // CPPIFY: see header file
    SPShape::snappoints(p, &local_snapprefs);

    if (snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_OBJECT_MIDPOINT)) {
        Geom::Affine const i2dt (this->i2dt_affine ());
        p.emplace_back(this->center * i2dt,Inkscape::SNAPSOURCE_OBJECT_MIDPOINT, Inkscape::SNAPTARGET_OBJECT_MIDPOINT);
    }
}

Geom::Affine SPStar::set_transform(Geom::Affine const &xform)
{
    bool opt_trans = (randomized == 0);
    if (pathEffectsEnabled() && !optimizeTransforms()) {
        return xform;
    }
    // Only set transform with proportional scaling
    if (!xform.withoutTranslation().isUniformScale()) {
        return xform;
    }
    notifyTransform(xform);


    /* Calculate star start in parent coords. */
    Geom::Point pos( this->center * xform );

    /* This function takes care of translation and scaling, we return whatever parts we can't
       handle. */
    Geom::Affine ret(opt_trans ? xform.withoutTranslation() : xform);
    gdouble const s = hypot(ret[0], ret[1]);
    if (s > 1e-9) {
        ret[0] /= s;
        ret[1] /= s;
        ret[2] /= s;
        ret[3] /= s;
    } else {
        ret[0] = 1.0;
        ret[1] = 0.0;
        ret[2] = 0.0;
        ret[3] = 1.0;
    }

    this->r[0] *= s;
    this->r[1] *= s;

    /* Find start in item coords */
    pos = pos * ret.inverse();
    this->center = pos;

    this->set_shape();

    // Adjust stroke width
    this->adjust_stroke(s);

    // Adjust pattern fill
    this->adjust_pattern(xform * ret.inverse());

    // Adjust gradient fill
    this->adjust_gradient(xform * ret.inverse());

    return ret;
}

void SPStar::update_patheffect(bool write) {
    SPShape::update_patheffect(write);
}

/**
 * sp_star_get_xy: Get X-Y value as item coordinate system
 * @star: star item
 * @point: point type to obtain X-Y value
 * @index: index of vertex
 * @p: pointer to store X-Y value
 * @randomized: false (default) if you want to get exact, not randomized point
 *
 * Initial item coordinate system is same as document coordinate system.
 */
Geom::Point
sp_star_get_xy (SPStar const *star, SPStarPoint point, gint index, bool randomized)
{
    gdouble darg = 2.0 * M_PI / (double) star->sides;

    double arg = star->arg[point];
    arg += index * darg;

    Geom::Point xy = star->r[point] * Geom::Point(cos(arg), sin(arg)) + star->center;

    if (!randomized || star->randomized == 0) {
        // return the exact point
        return xy;
    } else { // randomize the point
        // find out the seed, unique for this point so that randomization is the same so long as the original point is stationary
        guint32 seed = point_unique_int (xy);
        // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
        double range = 2 * MAX (star->r[0], star->r[1]);
        // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
        Geom::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
        // add the shift to the exact point
        return xy + shift;
    }
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :