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::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);
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 = NR::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::Node *repr, guint flags)
133 {
134 SPStar *star = SP_STAR (object);
136 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
137 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
138 repr = xml_doc->createElement("svg:path");
139 }
141 if (flags & SP_OBJECT_WRITE_EXT) {
142 repr->setAttribute("sodipodi:type", "star");
143 sp_repr_set_int (repr, "sodipodi:sides", star->sides);
144 sp_repr_set_svg_double(repr, "sodipodi:cx", star->center[NR::X]);
145 sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[NR::Y]);
146 sp_repr_set_svg_double(repr, "sodipodi:r1", star->r[0]);
147 sp_repr_set_svg_double(repr, "sodipodi:r2", star->r[1]);
148 sp_repr_set_svg_double(repr, "sodipodi:arg1", star->arg[0]);
149 sp_repr_set_svg_double(repr, "sodipodi:arg2", star->arg[1]);
150 sp_repr_set_boolean (repr, "inkscape:flatsided", star->flatsided);
151 sp_repr_set_svg_double(repr, "inkscape:rounded", star->rounded);
152 sp_repr_set_svg_double(repr, "inkscape:randomized", star->randomized);
153 }
155 sp_star_set_shape ((SPShape *) star);
156 char *d = sp_svg_write_path (SP_CURVE_BPATH(((SPShape *) star)->curve));
157 repr->setAttribute("d", d);
158 g_free (d);
160 if (((SPObjectClass *) (parent_class))->write)
161 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
163 return repr;
164 }
166 static void
167 sp_star_set (SPObject *object, unsigned int key, const gchar *value)
168 {
169 SVGLength::Unit unit;
171 SPStar *star = SP_STAR (object);
173 /* fixme: we should really collect updates */
174 switch (key) {
175 case SP_ATTR_SODIPODI_SIDES:
176 if (value) {
177 star->sides = atoi (value);
178 star->sides = CLAMP (star->sides, 3, 1024);
179 } else {
180 star->sides = 5;
181 }
182 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
183 break;
184 case SP_ATTR_SODIPODI_CX:
185 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::X]) ||
186 (unit == SVGLength::EM) ||
187 (unit == SVGLength::EX) ||
188 (unit == SVGLength::PERCENT)) {
189 star->center[NR::X] = 0.0;
190 }
191 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
192 break;
193 case SP_ATTR_SODIPODI_CY:
194 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::Y]) ||
195 (unit == SVGLength::EM) ||
196 (unit == SVGLength::EX) ||
197 (unit == SVGLength::PERCENT)) {
198 star->center[NR::Y] = 0.0;
199 }
200 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
201 break;
202 case SP_ATTR_SODIPODI_R1:
203 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[0]) ||
204 (unit == SVGLength::EM) ||
205 (unit == SVGLength::EX) ||
206 (unit == SVGLength::PERCENT)) {
207 star->r[0] = 1.0;
208 }
209 /* fixme: Need CLAMP (Lauris) */
210 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
211 break;
212 case SP_ATTR_SODIPODI_R2:
213 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[1]) ||
214 (unit == SVGLength::EM) ||
215 (unit == SVGLength::EX) ||
216 (unit == SVGLength::PERCENT)) {
217 star->r[1] = 0.0;
218 }
219 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
220 return;
221 case SP_ATTR_SODIPODI_ARG1:
222 if (value) {
223 star->arg[0] = g_ascii_strtod (value, NULL);
224 } else {
225 star->arg[0] = 0.0;
226 }
227 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
228 break;
229 case SP_ATTR_SODIPODI_ARG2:
230 if (value) {
231 star->arg[1] = g_ascii_strtod (value, NULL);
232 } else {
233 star->arg[1] = 0.0;
234 }
235 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
236 break;
237 case SP_ATTR_INKSCAPE_FLATSIDED:
238 if (value && !strcmp (value, "true"))
239 star->flatsided = true;
240 else star->flatsided = false;
241 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
242 break;
243 case SP_ATTR_INKSCAPE_ROUNDED:
244 if (value) {
245 star->rounded = g_ascii_strtod (value, NULL);
246 } else {
247 star->rounded = 0.0;
248 }
249 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
250 break;
251 case SP_ATTR_INKSCAPE_RANDOMIZED:
252 if (value) {
253 star->randomized = g_ascii_strtod (value, NULL);
254 } else {
255 star->randomized = 0.0;
256 }
257 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
258 break;
259 default:
260 if (((SPObjectClass *) parent_class)->set)
261 ((SPObjectClass *) parent_class)->set (object, key, value);
262 break;
263 }
264 }
266 static void
267 sp_star_update (SPObject *object, SPCtx *ctx, guint flags)
268 {
269 if (flags & (SP_OBJECT_MODIFIED_FLAG |
270 SP_OBJECT_STYLE_MODIFIED_FLAG |
271 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
272 sp_shape_set_shape ((SPShape *) object);
273 }
275 if (((SPObjectClass *) parent_class)->update)
276 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
277 }
279 static void
280 sp_star_update_patheffect(SPLPEItem *lpeitem, bool write)
281 {
282 SPShape *shape = (SPShape *) lpeitem;
283 sp_star_set_shape(shape);
285 if (write) {
286 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
287 if ( shape->curve != NULL ) {
288 NArtBpath *abp = shape->curve->first_bpath();
289 if (abp) {
290 gchar *str = sp_svg_write_path(abp);
291 repr->setAttribute("d", str);
292 g_free(str);
293 } else {
294 repr->setAttribute("d", "");
295 }
296 } else {
297 repr->setAttribute("d", NULL);
298 }
299 }
301 ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
302 }
304 static gchar *
305 sp_star_description (SPItem *item)
306 {
307 SPStar *star = SP_STAR (item);
309 // while there will never be less than 3 vertices, we still need to
310 // make calls to ngettext because the pluralization may be different
311 // for various numbers >=3. The singular form is used as the index.
312 if (star->flatsided == false )
313 return g_strdup_printf (ngettext("<b>Star</b> with %d vertex",
314 "<b>Star</b> with %d vertices",
315 star->sides), star->sides);
316 else
317 return g_strdup_printf (ngettext("<b>Polygon</b> with %d vertex",
318 "<b>Polygon</b> with %d vertices",
319 star->sides), star->sides);
320 }
322 /**
323 Returns a unit-length vector at 90 degrees to the direction from o to n
324 */
325 static NR::Point
326 rot90_rel (NR::Point o, NR::Point n)
327 {
328 return ((1/NR::L2(n - o)) * NR::Point ((n - o)[NR::Y], (o - n)[NR::X]));
329 }
331 /**
332 Returns a unique 32 bit int for a given point.
333 Obvious (but acceptable for my purposes) limits to uniqueness:
334 - returned value for x,y repeats for x+n*1024,y+n*1024
335 - returned value is unchanged when the point is moved by less than 1/1024 of px
336 */
337 static guint32
338 point_unique_int (NR::Point o)
339 {
340 return ((guint32)
341 65536 *
342 (((int) floor (o[NR::X] * 64)) % 1024 + ((int) floor (o[NR::X] * 1024)) % 64)
343 +
344 (((int) floor (o[NR::Y] * 64)) % 1024 + ((int) floor (o[NR::Y] * 1024)) % 64)
345 );
346 }
348 /**
349 Returns the next pseudorandom value using the Linear Congruential Generator algorithm (LCG)
350 with the parameters (m = 2^32, a = 69069, b = 1). These parameters give a full-period generator,
351 i.e. it is guaranteed to go through all integers < 2^32 (see http://random.mat.sbg.ac.at/~charly/server/server.html)
352 */
353 static inline guint32
354 lcg_next(guint32 const prev)
355 {
356 return (guint32) ( 69069 * prev + 1 );
357 }
359 /**
360 Returns a random number in the range [-0.5, 0.5) from the given seed, stepping the given number of steps from the seed.
361 */
362 static double
363 rnd (guint32 const seed, unsigned steps) {
364 guint32 lcg = seed;
365 for (; steps > 0; steps --)
366 lcg = lcg_next (lcg);
368 return ( lcg / 4294967296. ) - 0.5;
369 }
371 static NR::Point
372 sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
373 {
374 // the point whose neighboring curve handle we're calculating
375 NR::Point o = sp_star_get_xy (star, point, index);
377 // indices of previous and next points
378 gint pi = (index > 0)? (index - 1) : (star->sides - 1);
379 gint ni = (index < star->sides - 1)? (index + 1) : 0;
381 // the other point type
382 SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);
384 // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
385 NR::Point prev = (star->flatsided? sp_star_get_xy (star, point, pi) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT2? index : pi));
386 NR::Point next = (star->flatsided? sp_star_get_xy (star, point, ni) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT1? index : ni));
388 // prev-next midpoint
389 NR::Point mid = 0.5 * (prev + next);
391 // point to which we direct the bissector of the curve handles;
392 // it's far enough outside the star on the perpendicular to prev-next through mid
393 NR::Point biss = mid + 100000 * rot90_rel (mid, next);
395 // lengths of vectors to prev and next
396 gdouble prev_len = NR::L2 (prev - o);
397 gdouble next_len = NR::L2 (next - o);
399 // unit-length vector perpendicular to o-biss
400 NR::Point rot = rot90_rel (o, biss);
402 // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
403 NR::Point ret;
404 if (previ) {
405 ret = (star->rounded * prev_len) * rot;
406 } else {
407 ret = (star->rounded * next_len * -1) * rot;
408 }
410 if (star->randomized == 0) {
411 // add the vector to o to get the final curvepoint
412 return o + ret;
413 } else {
414 // the seed corresponding to the exact point
415 guint32 seed = point_unique_int (o);
417 // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
418 ret = ret * NR::Matrix (NR::rotate (star->randomized * M_PI * rnd (seed, 3)));
419 ret *= ( 1 + star->randomized * rnd (seed, 4));
421 // the randomized corner point
422 NR::Point o_randomized = sp_star_get_xy (star, point, index, true);
424 return o_randomized + ret;
425 }
426 }
429 #define NEXT false
430 #define PREV true
432 static void
433 sp_star_set_shape (SPShape *shape)
434 {
435 SPStar *star = SP_STAR (shape);
437 SPCurve *c = new SPCurve ();
439 gint sides = star->sides;
440 bool not_rounded = (fabs (star->rounded) < 1e-4);
442 // note that we pass randomized=true to sp_star_get_xy, because the curve must be randomized;
443 // other places that call that function (e.g. the knotholder) need the exact point
445 // draw 1st segment
446 c->moveto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
447 if (star->flatsided == false) {
448 if (not_rounded) {
449 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
450 } else {
451 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
452 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, 0, PREV),
453 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
454 }
455 }
457 // draw all middle segments
458 for (gint i = 1; i < sides; i++) {
459 if (not_rounded) {
460 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
461 } else {
462 if (star->flatsided == false) {
463 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i - 1, NEXT),
464 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
465 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
466 } else {
467 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
468 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
469 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
470 }
471 }
472 if (star->flatsided == false) {
474 if (not_rounded) {
475 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
476 } else {
477 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
478 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i, PREV),
479 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
480 }
481 }
482 }
484 // draw last segment
485 if (not_rounded) {
486 c->lineto(sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
487 } else {
488 if (star->flatsided == false) {
489 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
490 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
491 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
492 } else {
493 c->curveto(sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
494 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
495 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
496 }
497 }
499 c->closepath();
500 sp_lpe_item_perform_path_effect(SP_LPE_ITEM (star), c);
501 sp_shape_set_curve_insync (SP_SHAPE (star), c, TRUE);
502 c->unref();
503 }
505 void
506 sp_star_position_set (SPStar *star, gint sides, NR::Point center, gdouble r1, gdouble r2, gdouble arg1, gdouble arg2, bool isflat, double rounded, double randomized)
507 {
508 g_return_if_fail (star != NULL);
509 g_return_if_fail (SP_IS_STAR (star));
511 star->sides = CLAMP (sides, 3, 1024);
512 star->center = center;
513 star->r[0] = MAX (r1, 0.001);
514 if (isflat == false) {
515 star->r[1] = CLAMP (r2, 0.0, star->r[0]);
516 } else {
517 star->r[1] = CLAMP ( r1*cos(M_PI/sides) ,0.0, star->r[0] );
518 }
519 star->arg[0] = arg1;
520 star->arg[1] = arg2;
521 star->flatsided = isflat;
522 star->rounded = rounded;
523 star->randomized = randomized;
524 SP_OBJECT(star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
525 }
527 /* fixme: We should use all corners of star (Lauris) */
529 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p)
530 {
531 if (((SPItemClass *) parent_class)->snappoints) {
532 ((SPItemClass *) parent_class)->snappoints (item, p);
533 }
534 }
536 /**
537 * sp_star_get_xy: Get X-Y value as item coordinate system
538 * @star: star item
539 * @point: point type to obtain X-Y value
540 * @index: index of vertex
541 * @p: pointer to store X-Y value
542 * @randomized: false (default) if you want to get exact, not randomized point
543 *
544 * Initial item coordinate system is same as document coordinate system.
545 */
547 NR::Point
548 sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
549 {
550 gdouble darg = 2.0 * M_PI / (double) star->sides;
552 double arg = star->arg[point];
553 arg += index * darg;
555 NR::Point xy = star->r[point] * NR::Point(cos(arg), sin(arg)) + star->center;
557 if (!randomized || star->randomized == 0) {
558 // return the exact point
559 return xy;
560 } else { // randomize the point
561 // find out the seed, unique for this point so that randomization is the same so long as the original point is stationary
562 guint32 seed = point_unique_int (xy);
563 // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
564 double range = 2 * MAX (star->r[0], star->r[1]);
565 // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
566 NR::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
567 // add the shift to the exact point
568 return xy + shift;
569 }
570 }