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 (SPShape *shape, 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 SPPathClass * path_class;
77 SPShapeClass * shape_class;
79 gobject_class = (GObjectClass *) klass;
80 sp_object_class = (SPObjectClass *) klass;
81 item_class = (SPItemClass *) klass;
82 path_class = (SPPathClass *) 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 shape_class->set_shape = sp_star_set_shape;
96 shape_class->update_patheffect = sp_star_update_patheffect;
97 }
99 static void
100 sp_star_init (SPStar * star)
101 {
102 star->sides = 5;
103 star->center = NR::Point(0, 0);
104 star->r[0] = 1.0;
105 star->r[1] = 0.001;
106 star->arg[0] = star->arg[1] = 0.0;
107 star->flatsided = 0;
108 star->rounded = 0.0;
109 star->randomized = 0.0;
110 }
112 static void
113 sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
114 {
115 if (((SPObjectClass *) parent_class)->build)
116 ((SPObjectClass *) parent_class)->build (object, document, repr);
118 sp_object_read_attr (object, "sodipodi:cx");
119 sp_object_read_attr (object, "sodipodi:cy");
120 sp_object_read_attr (object, "sodipodi:sides");
121 sp_object_read_attr (object, "sodipodi:r1");
122 sp_object_read_attr (object, "sodipodi:r2");
123 sp_object_read_attr (object, "sodipodi:arg1");
124 sp_object_read_attr (object, "sodipodi:arg2");
125 sp_object_read_attr (object, "inkscape:flatsided");
126 sp_object_read_attr (object, "inkscape:rounded");
127 sp_object_read_attr (object, "inkscape:randomized");
128 }
130 static Inkscape::XML::Node *
131 sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
132 {
133 SPStar *star = SP_STAR (object);
135 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
136 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
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[NR::X]);
144 sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[NR::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 (SP_CURVE_BPATH(((SPShape *) star)->curve));
156 repr->setAttribute("d", d);
157 g_free (d);
159 if (((SPObjectClass *) (parent_class))->write)
160 ((SPObjectClass *) (parent_class))->write (object, 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[NR::X]) ||
185 (unit == SVGLength::EM) ||
186 (unit == SVGLength::EX) ||
187 (unit == SVGLength::PERCENT)) {
188 star->center[NR::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[NR::Y]) ||
194 (unit == SVGLength::EM) ||
195 (unit == SVGLength::EX) ||
196 (unit == SVGLength::PERCENT)) {
197 star->center[NR::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(SPShape *shape, bool write)
280 {
281 sp_star_set_shape(shape);
283 if (write) {
284 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
285 if ( shape->curve != NULL ) {
286 NArtBpath *abp = sp_curve_first_bpath(shape->curve);
287 if (abp) {
288 gchar *str = sp_svg_write_path(abp);
289 repr->setAttribute("d", str);
290 g_free(str);
291 } else {
292 repr->setAttribute("d", "");
293 }
294 } else {
295 repr->setAttribute("d", NULL);
296 }
297 }
299 ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
300 }
302 static gchar *
303 sp_star_description (SPItem *item)
304 {
305 SPStar *star = SP_STAR (item);
307 // while there will never be less than 3 vertices, we still need to
308 // make calls to ngettext because the pluralization may be different
309 // for various numbers >=3. The singular form is used as the index.
310 if (star->flatsided == false )
311 return g_strdup_printf (ngettext("<b>Star</b> with %d vertex",
312 "<b>Star</b> with %d vertices",
313 star->sides), star->sides);
314 else
315 return g_strdup_printf (ngettext("<b>Polygon</b> with %d vertex",
316 "<b>Polygon</b> with %d vertices",
317 star->sides), star->sides);
318 }
320 /**
321 Returns a unit-length vector at 90 degrees to the direction from o to n
322 */
323 static NR::Point
324 rot90_rel (NR::Point o, NR::Point n)
325 {
326 return ((1/NR::L2(n - o)) * NR::Point ((n - o)[NR::Y], (o - n)[NR::X]));
327 }
329 /**
330 Returns a unique 32 bit int for a given point.
331 Obvious (but acceptable for my purposes) limits to uniqueness:
332 - returned value for x,y repeats for x+n*1024,y+n*1024
333 - returned value is unchanged when the point is moved by less than 1/1024 of px
334 */
335 static guint32
336 point_unique_int (NR::Point o)
337 {
338 return ((guint32)
339 65536 *
340 (((int) floor (o[NR::X] * 64)) % 1024 + ((int) floor (o[NR::X] * 1024)) % 64)
341 +
342 (((int) floor (o[NR::Y] * 64)) % 1024 + ((int) floor (o[NR::Y] * 1024)) % 64)
343 );
344 }
346 /**
347 Returns the next pseudorandom value using the Linear Congruential Generator algorithm (LCG)
348 with the parameters (m = 2^32, a = 69069, b = 1). These parameters give a full-period generator,
349 i.e. it is guaranteed to go through all integers < 2^32 (see http://random.mat.sbg.ac.at/~charly/server/server.html)
350 */
351 static inline guint32
352 lcg_next(guint32 const prev)
353 {
354 return (guint32) ( 69069 * prev + 1 );
355 }
357 /**
358 Returns a random number in the range [-0.5, 0.5) from the given seed, stepping the given number of steps from the seed.
359 */
360 static double
361 rnd (guint32 const seed, unsigned steps) {
362 guint32 lcg = seed;
363 for (; steps > 0; steps --)
364 lcg = lcg_next (lcg);
366 return ( lcg / 4294967296. ) - 0.5;
367 }
369 static NR::Point
370 sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
371 {
372 // the point whose neighboring curve handle we're calculating
373 NR::Point o = sp_star_get_xy (star, point, index);
375 // indices of previous and next points
376 gint pi = (index > 0)? (index - 1) : (star->sides - 1);
377 gint ni = (index < star->sides - 1)? (index + 1) : 0;
379 // the other point type
380 SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);
382 // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
383 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));
384 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));
386 // prev-next midpoint
387 NR::Point mid = 0.5 * (prev + next);
389 // point to which we direct the bissector of the curve handles;
390 // it's far enough outside the star on the perpendicular to prev-next through mid
391 NR::Point biss = mid + 100000 * rot90_rel (mid, next);
393 // lengths of vectors to prev and next
394 gdouble prev_len = NR::L2 (prev - o);
395 gdouble next_len = NR::L2 (next - o);
397 // unit-length vector perpendicular to o-biss
398 NR::Point rot = rot90_rel (o, biss);
400 // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
401 NR::Point ret;
402 if (previ) {
403 ret = (star->rounded * prev_len) * rot;
404 } else {
405 ret = (star->rounded * next_len * -1) * rot;
406 }
408 if (star->randomized == 0) {
409 // add the vector to o to get the final curvepoint
410 return o + ret;
411 } else {
412 // the seed corresponding to the exact point
413 guint32 seed = point_unique_int (o);
415 // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
416 ret = ret * NR::Matrix (NR::rotate (star->randomized * M_PI * rnd (seed, 3)));
417 ret *= ( 1 + star->randomized * rnd (seed, 4));
419 // the randomized corner point
420 NR::Point o_randomized = sp_star_get_xy (star, point, index, true);
422 return o_randomized + ret;
423 }
424 }
427 #define NEXT false
428 #define PREV true
430 static void
431 sp_star_set_shape (SPShape *shape)
432 {
433 SPStar *star = SP_STAR (shape);
435 SPCurve *c = sp_curve_new ();
437 gint sides = star->sides;
438 bool not_rounded = (fabs (star->rounded) < 1e-4);
440 // note that we pass randomized=true to sp_star_get_xy, because the curve must be randomized;
441 // other places that call that function (e.g. the knotholder) need the exact point
443 // draw 1st segment
444 sp_curve_moveto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
445 if (star->flatsided == false) {
446 if (not_rounded) {
447 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
448 } else {
449 sp_curve_curveto (c,
450 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
451 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, 0, PREV),
452 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
453 }
454 }
456 // draw all middle segments
457 for (gint i = 1; i < sides; i++) {
458 if (not_rounded) {
459 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
460 } else {
461 if (star->flatsided == false) {
462 sp_curve_curveto (c,
463 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 sp_curve_curveto (c,
468 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
469 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
470 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
471 }
472 }
473 if (star->flatsided == false) {
475 if (not_rounded) {
476 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
477 } else {
478 sp_curve_curveto (c,
479 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
480 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i, PREV),
481 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
482 }
483 }
484 }
486 // draw last segment
487 if (not_rounded) {
488 sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
489 } else {
490 if (star->flatsided == false) {
491 sp_curve_curveto (c,
492 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
493 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
494 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
495 } else {
496 sp_curve_curveto (c,
497 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
498 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
499 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
500 }
501 }
503 sp_curve_closepath (c);
504 sp_shape_perform_path_effect(c, SP_SHAPE (star));
505 sp_shape_set_curve_insync (SP_SHAPE (star), c, TRUE);
506 sp_curve_unref (c);
507 }
509 void
510 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)
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 /* fixme: We should use all corners of star (Lauris) */
533 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p)
534 {
535 if (((SPItemClass *) parent_class)->snappoints) {
536 ((SPItemClass *) parent_class)->snappoints (item, p);
537 }
538 }
540 /**
541 * sp_star_get_xy: Get X-Y value as item coordinate system
542 * @star: star item
543 * @point: point type to obtain X-Y value
544 * @index: index of vertex
545 * @p: pointer to store X-Y value
546 * @randomized: false (default) if you want to get exact, not randomized point
547 *
548 * Initial item coordinate system is same as document coordinate system.
549 */
551 NR::Point
552 sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
553 {
554 gdouble darg = 2.0 * M_PI / (double) star->sides;
556 double arg = star->arg[point];
557 arg += index * darg;
559 NR::Point xy = star->r[point] * NR::Point(cos(arg), sin(arg)) + star->center;
561 if (!randomized || star->randomized == 0) {
562 // return the exact point
563 return xy;
564 } else { // randomize the point
565 // find out the seed, unique for this point so that randomization is the same so long as the original point is stationary
566 guint32 seed = point_unique_int (xy);
567 // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
568 double range = 2 * MAX (star->r[0], star->r[1]);
569 // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
570 NR::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
571 // add the shift to the exact point
572 return xy + shift;
573 }
574 }