1 #define __SP_STAR_C__
3 /*
4 * <sodipodi:star> implementation
5 *
6 * Authors:
7 * Mitsuru Oka <oka326@parkcity.ne.jp>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 1999-2002 Lauris Kaplinski
12 * Copyright (C) 2000-2001 Ximian, Inc.
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <cstring>
22 #include <string>
23 #include <glibmm/i18n.h>
25 #include "svg/svg.h"
26 #include "attributes.h"
27 #include "display/curve.h"
28 #include "xml/repr.h"
29 #include "document.h"
31 #include "sp-star.h"
33 static void sp_star_class_init (SPStarClass *klass);
34 static void sp_star_init (SPStar *star);
36 static void sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
37 static Inkscape::XML::Node *sp_star_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
38 static void sp_star_set (SPObject *object, unsigned int key, const gchar *value);
39 static void sp_star_update (SPObject *object, SPCtx *ctx, guint flags);
41 static gchar * sp_star_description (SPItem * item);
42 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
44 static void sp_star_set_shape (SPShape *shape);
45 static void sp_star_update_patheffect (SPLPEItem *lpeitem, bool write);
47 static SPShapeClass *parent_class;
49 GType
50 sp_star_get_type (void)
51 {
52 static GType type = 0;
54 if (!type) {
55 GTypeInfo info = {
56 sizeof (SPStarClass),
57 NULL, NULL,
58 (GClassInitFunc) sp_star_class_init,
59 NULL, NULL,
60 sizeof (SPStar),
61 16,
62 (GInstanceInitFunc) sp_star_init,
63 NULL, /* value_table */
64 };
65 type = g_type_register_static (SP_TYPE_SHAPE, "SPStar", &info, (GTypeFlags)0);
66 }
67 return type;
68 }
70 static void
71 sp_star_class_init (SPStarClass *klass)
72 {
73 GObjectClass * gobject_class;
74 SPObjectClass * sp_object_class;
75 SPItemClass * item_class;
76 SPLPEItemClass * lpe_item_class;
77 SPShapeClass * shape_class;
79 gobject_class = (GObjectClass *) klass;
80 sp_object_class = (SPObjectClass *) klass;
81 item_class = (SPItemClass *) klass;
82 lpe_item_class = (SPLPEItemClass *) klass;
83 shape_class = (SPShapeClass *) klass;
85 parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
87 sp_object_class->build = sp_star_build;
88 sp_object_class->write = sp_star_write;
89 sp_object_class->set = sp_star_set;
90 sp_object_class->update = sp_star_update;
92 item_class->description = sp_star_description;
93 item_class->snappoints = sp_star_snappoints;
95 lpe_item_class->update_patheffect = sp_star_update_patheffect;
97 shape_class->set_shape = sp_star_set_shape;
98 }
100 static void
101 sp_star_init (SPStar * star)
102 {
103 star->sides = 5;
104 star->center = Geom::Point(0, 0);
105 star->r[0] = 1.0;
106 star->r[1] = 0.001;
107 star->arg[0] = star->arg[1] = 0.0;
108 star->flatsided = 0;
109 star->rounded = 0.0;
110 star->randomized = 0.0;
111 }
113 static void
114 sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
115 {
116 if (((SPObjectClass *) parent_class)->build)
117 ((SPObjectClass *) parent_class)->build (object, document, repr);
119 sp_object_read_attr (object, "sodipodi:cx");
120 sp_object_read_attr (object, "sodipodi:cy");
121 sp_object_read_attr (object, "sodipodi:sides");
122 sp_object_read_attr (object, "sodipodi:r1");
123 sp_object_read_attr (object, "sodipodi:r2");
124 sp_object_read_attr (object, "sodipodi:arg1");
125 sp_object_read_attr (object, "sodipodi:arg2");
126 sp_object_read_attr (object, "inkscape:flatsided");
127 sp_object_read_attr (object, "inkscape:rounded");
128 sp_object_read_attr (object, "inkscape:randomized");
129 }
131 static Inkscape::XML::Node *
132 sp_star_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
133 {
134 SPStar *star = SP_STAR (object);
136 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
137 repr = xml_doc->createElement("svg:path");
138 }
140 if (flags & SP_OBJECT_WRITE_EXT) {
141 repr->setAttribute("sodipodi:type", "star");
142 sp_repr_set_int (repr, "sodipodi:sides", star->sides);
143 sp_repr_set_svg_double(repr, "sodipodi:cx", star->center[Geom::X]);
144 sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[Geom::Y]);
145 sp_repr_set_svg_double(repr, "sodipodi:r1", star->r[0]);
146 sp_repr_set_svg_double(repr, "sodipodi:r2", star->r[1]);
147 sp_repr_set_svg_double(repr, "sodipodi:arg1", star->arg[0]);
148 sp_repr_set_svg_double(repr, "sodipodi:arg2", star->arg[1]);
149 sp_repr_set_boolean (repr, "inkscape:flatsided", star->flatsided);
150 sp_repr_set_svg_double(repr, "inkscape:rounded", star->rounded);
151 sp_repr_set_svg_double(repr, "inkscape:randomized", star->randomized);
152 }
154 sp_star_set_shape ((SPShape *) star);
155 char *d = sp_svg_write_path (((SPShape *) star)->curve->get_pathvector());
156 repr->setAttribute("d", d);
157 g_free (d);
159 if (((SPObjectClass *) (parent_class))->write)
160 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
162 return repr;
163 }
165 static void
166 sp_star_set (SPObject *object, unsigned int key, const gchar *value)
167 {
168 SVGLength::Unit unit;
170 SPStar *star = SP_STAR (object);
172 /* fixme: we should really collect updates */
173 switch (key) {
174 case SP_ATTR_SODIPODI_SIDES:
175 if (value) {
176 star->sides = atoi (value);
177 star->sides = CLAMP (star->sides, 3, 1024);
178 } else {
179 star->sides = 5;
180 }
181 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
182 break;
183 case SP_ATTR_SODIPODI_CX:
184 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[Geom::X]) ||
185 (unit == SVGLength::EM) ||
186 (unit == SVGLength::EX) ||
187 (unit == SVGLength::PERCENT)) {
188 star->center[Geom::X] = 0.0;
189 }
190 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
191 break;
192 case SP_ATTR_SODIPODI_CY:
193 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[Geom::Y]) ||
194 (unit == SVGLength::EM) ||
195 (unit == SVGLength::EX) ||
196 (unit == SVGLength::PERCENT)) {
197 star->center[Geom::Y] = 0.0;
198 }
199 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
200 break;
201 case SP_ATTR_SODIPODI_R1:
202 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[0]) ||
203 (unit == SVGLength::EM) ||
204 (unit == SVGLength::EX) ||
205 (unit == SVGLength::PERCENT)) {
206 star->r[0] = 1.0;
207 }
208 /* fixme: Need CLAMP (Lauris) */
209 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
210 break;
211 case SP_ATTR_SODIPODI_R2:
212 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[1]) ||
213 (unit == SVGLength::EM) ||
214 (unit == SVGLength::EX) ||
215 (unit == SVGLength::PERCENT)) {
216 star->r[1] = 0.0;
217 }
218 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
219 return;
220 case SP_ATTR_SODIPODI_ARG1:
221 if (value) {
222 star->arg[0] = g_ascii_strtod (value, NULL);
223 } else {
224 star->arg[0] = 0.0;
225 }
226 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
227 break;
228 case SP_ATTR_SODIPODI_ARG2:
229 if (value) {
230 star->arg[1] = g_ascii_strtod (value, NULL);
231 } else {
232 star->arg[1] = 0.0;
233 }
234 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
235 break;
236 case SP_ATTR_INKSCAPE_FLATSIDED:
237 if (value && !strcmp (value, "true"))
238 star->flatsided = true;
239 else star->flatsided = false;
240 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
241 break;
242 case SP_ATTR_INKSCAPE_ROUNDED:
243 if (value) {
244 star->rounded = g_ascii_strtod (value, NULL);
245 } else {
246 star->rounded = 0.0;
247 }
248 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
249 break;
250 case SP_ATTR_INKSCAPE_RANDOMIZED:
251 if (value) {
252 star->randomized = g_ascii_strtod (value, NULL);
253 } else {
254 star->randomized = 0.0;
255 }
256 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
257 break;
258 default:
259 if (((SPObjectClass *) parent_class)->set)
260 ((SPObjectClass *) parent_class)->set (object, key, value);
261 break;
262 }
263 }
265 static void
266 sp_star_update (SPObject *object, SPCtx *ctx, guint flags)
267 {
268 if (flags & (SP_OBJECT_MODIFIED_FLAG |
269 SP_OBJECT_STYLE_MODIFIED_FLAG |
270 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
271 sp_shape_set_shape ((SPShape *) object);
272 }
274 if (((SPObjectClass *) parent_class)->update)
275 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
276 }
278 static void
279 sp_star_update_patheffect(SPLPEItem *lpeitem, bool write)
280 {
281 SPShape *shape = (SPShape *) lpeitem;
282 sp_star_set_shape(shape);
284 if (write) {
285 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
286 if ( shape->curve != NULL ) {
287 gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
288 repr->setAttribute("d", str);
289 g_free(str);
290 } else {
291 repr->setAttribute("d", NULL);
292 }
293 }
295 ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
296 }
298 static gchar *
299 sp_star_description (SPItem *item)
300 {
301 SPStar *star = SP_STAR (item);
303 // while there will never be less than 3 vertices, we still need to
304 // make calls to ngettext because the pluralization may be different
305 // for various numbers >=3. The singular form is used as the index.
306 if (star->flatsided == false )
307 return g_strdup_printf (ngettext("<b>Star</b> with %d vertex",
308 "<b>Star</b> with %d vertices",
309 star->sides), star->sides);
310 else
311 return g_strdup_printf (ngettext("<b>Polygon</b> with %d vertex",
312 "<b>Polygon</b> with %d vertices",
313 star->sides), star->sides);
314 }
316 /**
317 Returns a unit-length vector at 90 degrees to the direction from o to n
318 */
319 static Geom::Point
320 rot90_rel (Geom::Point o, Geom::Point n)
321 {
322 return ((1/Geom::L2(n - o)) * Geom::Point ((n - o)[Geom::Y], (o - n)[Geom::X]));
323 }
325 /**
326 Returns a unique 32 bit int for a given point.
327 Obvious (but acceptable for my purposes) limits to uniqueness:
328 - returned value for x,y repeats for x+n*1024,y+n*1024
329 - returned value is unchanged when the point is moved by less than 1/1024 of px
330 */
331 static guint32
332 point_unique_int (Geom::Point o)
333 {
334 return ((guint32)
335 65536 *
336 (((int) floor (o[Geom::X] * 64)) % 1024 + ((int) floor (o[Geom::X] * 1024)) % 64)
337 +
338 (((int) floor (o[Geom::Y] * 64)) % 1024 + ((int) floor (o[Geom::Y] * 1024)) % 64)
339 );
340 }
342 /**
343 Returns the next pseudorandom value using the Linear Congruential Generator algorithm (LCG)
344 with the parameters (m = 2^32, a = 69069, b = 1). These parameters give a full-period generator,
345 i.e. it is guaranteed to go through all integers < 2^32 (see http://random.mat.sbg.ac.at/~charly/server/server.html)
346 */
347 static inline guint32
348 lcg_next(guint32 const prev)
349 {
350 return (guint32) ( 69069 * prev + 1 );
351 }
353 /**
354 Returns a random number in the range [-0.5, 0.5) from the given seed, stepping the given number of steps from the seed.
355 */
356 static double
357 rnd (guint32 const seed, unsigned steps) {
358 guint32 lcg = seed;
359 for (; steps > 0; steps --)
360 lcg = lcg_next (lcg);
362 return ( lcg / 4294967296. ) - 0.5;
363 }
365 static Geom::Point
366 sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
367 {
368 // the point whose neighboring curve handle we're calculating
369 Geom::Point o = sp_star_get_xy (star, point, index);
371 // indices of previous and next points
372 gint pi = (index > 0)? (index - 1) : (star->sides - 1);
373 gint ni = (index < star->sides - 1)? (index + 1) : 0;
375 // the other point type
376 SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);
378 // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
379 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));
380 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));
382 // prev-next midpoint
383 Geom::Point mid = 0.5 * (prev + next);
385 // point to which we direct the bissector of the curve handles;
386 // it's far enough outside the star on the perpendicular to prev-next through mid
387 Geom::Point biss = mid + 100000 * rot90_rel (mid, next);
389 // lengths of vectors to prev and next
390 gdouble prev_len = Geom::L2 (prev - o);
391 gdouble next_len = Geom::L2 (next - o);
393 // unit-length vector perpendicular to o-biss
394 Geom::Point rot = rot90_rel (o, biss);
396 // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
397 Geom::Point ret;
398 if (previ) {
399 ret = (star->rounded * prev_len) * rot;
400 } else {
401 ret = (star->rounded * next_len * -1) * rot;
402 }
404 if (star->randomized == 0) {
405 // add the vector to o to get the final curvepoint
406 return o + ret;
407 } else {
408 // the seed corresponding to the exact point
409 guint32 seed = point_unique_int (o);
411 // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
412 ret = ret * Geom::Matrix (Geom::Rotate (star->randomized * M_PI * rnd (seed, 3)));
413 ret *= ( 1 + star->randomized * rnd (seed, 4));
415 // the randomized corner point
416 Geom::Point o_randomized = sp_star_get_xy (star, point, index, true);
418 return o_randomized + ret;
419 }
420 }
423 #define NEXT false
424 #define PREV true
426 static void
427 sp_star_set_shape (SPShape *shape)
428 {
429 SPStar *star = SP_STAR (shape);
431 SPCurve *c = new SPCurve ();
433 gint sides = star->sides;
434 bool not_rounded = (fabs (star->rounded) < 1e-4);
436 // note that we pass randomized=true to sp_star_get_xy, because the curve must be randomized;
437 // other places that call that function (e.g. the knotholder) need the exact point
439 // draw 1st segment
440 c->moveto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
441 if (star->flatsided == false) {
442 if (not_rounded) {
443 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
444 } else {
445 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
446 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, 0, PREV),
447 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
448 }
449 }
451 // draw all middle segments
452 for (gint i = 1; i < sides; i++) {
453 if (not_rounded) {
454 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
455 } else {
456 if (star->flatsided == false) {
457 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i - 1, NEXT),
458 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
459 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
460 } else {
461 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
462 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
463 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
464 }
465 }
466 if (star->flatsided == false) {
468 if (not_rounded) {
469 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
470 } else {
471 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
472 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i, PREV),
473 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
474 }
475 }
476 }
478 // draw last segment
479 if (not_rounded) {
480 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
481 } else {
482 if (star->flatsided == false) {
483 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
484 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
485 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
486 } else {
487 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
488 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
489 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
490 }
491 }
493 c->closepath();
495 /* Reset the shape'scurve to the "original_curve"
496 * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
497 sp_shape_set_curve_insync (shape, c, TRUE);
498 if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
499 SPCurve *c_lpe = c->copy();
500 bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
501 if (success) {
502 sp_shape_set_curve_insync (shape, c_lpe, TRUE);
503 }
504 c_lpe->unref();
505 }
506 c->unref();
507 }
509 void
510 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)
511 {
512 g_return_if_fail (star != NULL);
513 g_return_if_fail (SP_IS_STAR (star));
515 star->sides = CLAMP (sides, 3, 1024);
516 star->center = center;
517 star->r[0] = MAX (r1, 0.001);
518 if (isflat == false) {
519 star->r[1] = CLAMP (r2, 0.0, star->r[0]);
520 } else {
521 star->r[1] = CLAMP ( r1*cos(M_PI/sides) ,0.0, star->r[0] );
522 }
523 star->arg[0] = arg1;
524 star->arg[1] = arg2;
525 star->flatsided = isflat;
526 star->rounded = rounded;
527 star->randomized = randomized;
528 SP_OBJECT(star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
529 }
531 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
532 {
533 // We will determine the star's midpoint ourselves, instead of trusting on the base class
534 // Therefore setSnapObjectMidpoints() is set to false temporarily
535 Inkscape::SnapPreferences local_snapprefs = *snapprefs;
536 local_snapprefs.setSnapObjectMidpoints(false);
538 if (((SPItemClass *) parent_class)->snappoints) {
539 ((SPItemClass *) parent_class)->snappoints (item, p, &local_snapprefs);
540 }
542 // Help enforcing strict snapping, i.e. only return nodes when we're snapping nodes to nodes or a guide to nodes
543 if (!(snapprefs->getSnapModeNode() || snapprefs->getSnapModeGuide())) {
544 return;
545 }
547 if (snapprefs->getSnapObjectMidpoints()) {
548 Geom::Matrix const i2d (sp_item_i2d_affine (item));
549 *p = SP_STAR(item)->center * i2d;
550 }
551 }
553 /**
554 * sp_star_get_xy: Get X-Y value as item coordinate system
555 * @star: star item
556 * @point: point type to obtain X-Y value
557 * @index: index of vertex
558 * @p: pointer to store X-Y value
559 * @randomized: false (default) if you want to get exact, not randomized point
560 *
561 * Initial item coordinate system is same as document coordinate system.
562 */
564 Geom::Point
565 sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
566 {
567 gdouble darg = 2.0 * M_PI / (double) star->sides;
569 double arg = star->arg[point];
570 arg += index * darg;
572 Geom::Point xy = star->r[point] * Geom::Point(cos(arg), sin(arg)) + star->center;
574 if (!randomized || star->randomized == 0) {
575 // return the exact point
576 return xy;
577 } else { // randomize the point
578 // find out the seed, unique for this point so that randomization is the same so long as the original point is stationary
579 guint32 seed = point_unique_int (xy);
580 // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
581 double range = 2 * MAX (star->r[0], star->r[1]);
582 // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
583 Geom::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
584 // add the shift to the exact point
585 return xy + shift;
586 }
587 }