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 #include "config.h"
19 #include <glibmm/i18n.h>
21 #include "svg/svg.h"
22 #include "attributes.h"
23 #include "display/curve.h"
24 #include "xml/repr.h"
25 #include "document.h"
27 #include "sp-star.h"
29 static void sp_star_class_init (SPStarClass *klass);
30 static void sp_star_init (SPStar *star);
32 static void sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
33 static Inkscape::XML::Node *sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
34 static void sp_star_set (SPObject *object, unsigned int key, const gchar *value);
35 static void sp_star_update (SPObject *object, SPCtx *ctx, guint flags);
37 static gchar * sp_star_description (SPItem * item);
38 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p);
40 static void sp_star_set_shape (SPShape *shape);
41 static void sp_star_update_patheffect (SPShape *shape, bool write);
43 static SPShapeClass *parent_class;
45 GType
46 sp_star_get_type (void)
47 {
48 static GType type = 0;
50 if (!type) {
51 GTypeInfo info = {
52 sizeof (SPStarClass),
53 NULL, NULL,
54 (GClassInitFunc) sp_star_class_init,
55 NULL, NULL,
56 sizeof (SPStar),
57 16,
58 (GInstanceInitFunc) sp_star_init,
59 NULL, /* value_table */
60 };
61 type = g_type_register_static (SP_TYPE_SHAPE, "SPStar", &info, (GTypeFlags)0);
62 }
63 return type;
64 }
66 static void
67 sp_star_class_init (SPStarClass *klass)
68 {
69 GObjectClass * gobject_class;
70 SPObjectClass * sp_object_class;
71 SPItemClass * item_class;
72 SPPathClass * path_class;
73 SPShapeClass * shape_class;
75 gobject_class = (GObjectClass *) klass;
76 sp_object_class = (SPObjectClass *) klass;
77 item_class = (SPItemClass *) klass;
78 path_class = (SPPathClass *) klass;
79 shape_class = (SPShapeClass *) klass;
81 parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
83 sp_object_class->build = sp_star_build;
84 sp_object_class->write = sp_star_write;
85 sp_object_class->set = sp_star_set;
86 sp_object_class->update = sp_star_update;
88 item_class->description = sp_star_description;
89 item_class->snappoints = sp_star_snappoints;
91 shape_class->set_shape = sp_star_set_shape;
92 shape_class->update_patheffect = sp_star_update_patheffect;
93 }
95 static void
96 sp_star_init (SPStar * star)
97 {
98 star->sides = 5;
99 star->center = NR::Point(0, 0);
100 star->r[0] = 1.0;
101 star->r[1] = 0.001;
102 star->arg[0] = star->arg[1] = 0.0;
103 star->flatsided = 0;
104 star->rounded = 0.0;
105 star->randomized = 0.0;
106 }
108 static void
109 sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
110 {
111 if (((SPObjectClass *) parent_class)->build)
112 ((SPObjectClass *) parent_class)->build (object, document, repr);
114 sp_object_read_attr (object, "sodipodi:cx");
115 sp_object_read_attr (object, "sodipodi:cy");
116 sp_object_read_attr (object, "sodipodi:sides");
117 sp_object_read_attr (object, "sodipodi:r1");
118 sp_object_read_attr (object, "sodipodi:r2");
119 sp_object_read_attr (object, "sodipodi:arg1");
120 sp_object_read_attr (object, "sodipodi:arg2");
121 sp_object_read_attr (object, "inkscape:flatsided");
122 sp_object_read_attr (object, "inkscape:rounded");
123 sp_object_read_attr (object, "inkscape:randomized");
124 }
126 static Inkscape::XML::Node *
127 sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
128 {
129 SPStar *star = SP_STAR (object);
131 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
132 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
133 repr = xml_doc->createElement("svg:path");
134 }
136 if (flags & SP_OBJECT_WRITE_EXT) {
137 repr->setAttribute("sodipodi:type", "star");
138 sp_repr_set_int (repr, "sodipodi:sides", star->sides);
139 sp_repr_set_svg_double(repr, "sodipodi:cx", star->center[NR::X]);
140 sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[NR::Y]);
141 sp_repr_set_svg_double(repr, "sodipodi:r1", star->r[0]);
142 sp_repr_set_svg_double(repr, "sodipodi:r2", star->r[1]);
143 sp_repr_set_svg_double(repr, "sodipodi:arg1", star->arg[0]);
144 sp_repr_set_svg_double(repr, "sodipodi:arg2", star->arg[1]);
145 sp_repr_set_boolean (repr, "inkscape:flatsided", star->flatsided);
146 sp_repr_set_svg_double(repr, "inkscape:rounded", star->rounded);
147 sp_repr_set_svg_double(repr, "inkscape:randomized", star->randomized);
148 }
150 sp_star_set_shape ((SPShape *) star);
151 char *d = sp_svg_write_path (SP_CURVE_BPATH(((SPShape *) star)->curve));
152 repr->setAttribute("d", d);
153 g_free (d);
155 if (((SPObjectClass *) (parent_class))->write)
156 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
158 return repr;
159 }
161 static void
162 sp_star_set (SPObject *object, unsigned int key, const gchar *value)
163 {
164 SVGLength::Unit unit;
166 SPStar *star = SP_STAR (object);
168 /* fixme: we should really collect updates */
169 switch (key) {
170 case SP_ATTR_SODIPODI_SIDES:
171 if (value) {
172 star->sides = atoi (value);
173 star->sides = CLAMP (star->sides, 3, 1024);
174 } else {
175 star->sides = 5;
176 }
177 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
178 break;
179 case SP_ATTR_SODIPODI_CX:
180 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::X]) ||
181 (unit == SVGLength::EM) ||
182 (unit == SVGLength::EX) ||
183 (unit == SVGLength::PERCENT)) {
184 star->center[NR::X] = 0.0;
185 }
186 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
187 break;
188 case SP_ATTR_SODIPODI_CY:
189 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::Y]) ||
190 (unit == SVGLength::EM) ||
191 (unit == SVGLength::EX) ||
192 (unit == SVGLength::PERCENT)) {
193 star->center[NR::Y] = 0.0;
194 }
195 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
196 break;
197 case SP_ATTR_SODIPODI_R1:
198 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[0]) ||
199 (unit == SVGLength::EM) ||
200 (unit == SVGLength::EX) ||
201 (unit == SVGLength::PERCENT)) {
202 star->r[0] = 1.0;
203 }
204 /* fixme: Need CLAMP (Lauris) */
205 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
206 break;
207 case SP_ATTR_SODIPODI_R2:
208 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[1]) ||
209 (unit == SVGLength::EM) ||
210 (unit == SVGLength::EX) ||
211 (unit == SVGLength::PERCENT)) {
212 star->r[1] = 0.0;
213 }
214 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
215 return;
216 case SP_ATTR_SODIPODI_ARG1:
217 if (value) {
218 star->arg[0] = g_ascii_strtod (value, NULL);
219 } else {
220 star->arg[0] = 0.0;
221 }
222 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
223 break;
224 case SP_ATTR_SODIPODI_ARG2:
225 if (value) {
226 star->arg[1] = g_ascii_strtod (value, NULL);
227 } else {
228 star->arg[1] = 0.0;
229 }
230 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
231 break;
232 case SP_ATTR_INKSCAPE_FLATSIDED:
233 if (value && !strcmp (value, "true"))
234 star->flatsided = true;
235 else star->flatsided = false;
236 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
237 break;
238 case SP_ATTR_INKSCAPE_ROUNDED:
239 if (value) {
240 star->rounded = g_ascii_strtod (value, NULL);
241 } else {
242 star->rounded = 0.0;
243 }
244 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
245 break;
246 case SP_ATTR_INKSCAPE_RANDOMIZED:
247 if (value) {
248 star->randomized = g_ascii_strtod (value, NULL);
249 } else {
250 star->randomized = 0.0;
251 }
252 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
253 break;
254 default:
255 if (((SPObjectClass *) parent_class)->set)
256 ((SPObjectClass *) parent_class)->set (object, key, value);
257 break;
258 }
259 }
261 static void
262 sp_star_update (SPObject *object, SPCtx *ctx, guint flags)
263 {
264 if (flags & (SP_OBJECT_MODIFIED_FLAG |
265 SP_OBJECT_STYLE_MODIFIED_FLAG |
266 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
267 sp_shape_set_shape ((SPShape *) object);
268 }
270 if (((SPObjectClass *) parent_class)->update)
271 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
272 }
274 static void
275 sp_star_update_patheffect(SPShape *shape, bool write)
276 {
277 sp_star_set_shape(shape);
279 if (write) {
280 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
281 if ( shape->curve != NULL ) {
282 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
283 if (abp) {
284 gchar *str = sp_svg_write_path(abp);
285 repr->setAttribute("d", str);
286 g_free(str);
287 } else {
288 repr->setAttribute("d", "");
289 }
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 NR::Point
320 rot90_rel (NR::Point o, NR::Point n)
321 {
322 return ((1/NR::L2(n - o)) * NR::Point ((n - o)[NR::Y], (o - n)[NR::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 (NR::Point o)
333 {
334 return ((guint32)
335 65536 *
336 (((int) floor (o[NR::X] * 64)) % 1024 + ((int) floor (o[NR::X] * 1024)) % 64)
337 +
338 (((int) floor (o[NR::Y] * 64)) % 1024 + ((int) floor (o[NR::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 NR::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 NR::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 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));
380 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));
382 // prev-next midpoint
383 NR::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 NR::Point biss = mid + 100000 * rot90_rel (mid, next);
389 // lengths of vectors to prev and next
390 gdouble prev_len = NR::L2 (prev - o);
391 gdouble next_len = NR::L2 (next - o);
393 // unit-length vector perpendicular to o-biss
394 NR::Point rot = rot90_rel (o, biss);
396 // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
397 NR::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 * NR::Matrix (NR::rotate (star->randomized * M_PI * rnd (seed, 3)));
413 ret *= ( 1 + star->randomized * rnd (seed, 4));
415 // the randomized corner point
416 NR::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 = sp_curve_new ();
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 sp_curve_moveto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
441 if (star->flatsided == false) {
442 if (not_rounded) {
443 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
444 } else {
445 sp_curve_curveto (c,
446 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
447 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, 0, PREV),
448 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
449 }
450 }
452 // draw all middle segments
453 for (gint i = 1; i < sides; i++) {
454 if (not_rounded) {
455 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
456 } else {
457 if (star->flatsided == false) {
458 sp_curve_curveto (c,
459 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i - 1, NEXT),
460 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
461 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
462 } else {
463 sp_curve_curveto (c,
464 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
465 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
466 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
467 }
468 }
469 if (star->flatsided == false) {
471 if (not_rounded) {
472 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
473 } else {
474 sp_curve_curveto (c,
475 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
476 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i, PREV),
477 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
478 }
479 }
480 }
482 // draw last segment
483 if (not_rounded) {
484 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
485 } else {
486 if (star->flatsided == false) {
487 sp_curve_curveto (c,
488 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
489 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
490 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
491 } else {
492 sp_curve_curveto (c,
493 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 sp_curve_closepath (c);
500 sp_shape_perform_path_effect(c, SP_SHAPE (star));
501 sp_shape_set_curve_insync (SP_SHAPE (star), c, TRUE);
502 sp_curve_unref (c);
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 }