Code

PNG output for Cairo renderer
[inkscape.git] / src / sp-star.cpp
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"
26 #include "sp-star.h"
28 static void sp_star_class_init (SPStarClass *klass);
29 static void sp_star_init (SPStar *star);
31 static void sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
32 static Inkscape::XML::Node *sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
33 static void sp_star_set (SPObject *object, unsigned int key, const gchar *value);
34 static void sp_star_update (SPObject *object, SPCtx *ctx, guint flags);
36 static gchar * sp_star_description (SPItem * item);
37 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p);
39 static void sp_star_set_shape (SPShape *shape);
41 static SPShapeClass *parent_class;
43 GType
44 sp_star_get_type (void)
45 {
46         static GType type = 0;
48         if (!type) {
49                 GTypeInfo info = {
50                         sizeof (SPStarClass),
51                         NULL, NULL,
52                         (GClassInitFunc) sp_star_class_init,
53                         NULL, NULL,
54                         sizeof (SPStar),
55                         16,
56                         (GInstanceInitFunc) sp_star_init,
57                         NULL,   /* value_table */
58                 };
59                 type = g_type_register_static (SP_TYPE_SHAPE, "SPStar", &info, (GTypeFlags)0);
60         }
61         return type;
62 }
64 static void
65 sp_star_class_init (SPStarClass *klass)
66 {
67         GObjectClass * gobject_class;
68         SPObjectClass * sp_object_class;
69         SPItemClass * item_class;
70         SPPathClass * path_class;
71         SPShapeClass * shape_class;
73         gobject_class = (GObjectClass *) klass;
74         sp_object_class = (SPObjectClass *) klass;
75         item_class = (SPItemClass *) klass;
76         path_class = (SPPathClass *) klass;
77         shape_class = (SPShapeClass *) klass;
79         parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
81         sp_object_class->build = sp_star_build;
82         sp_object_class->write = sp_star_write;
83         sp_object_class->set = sp_star_set;
84         sp_object_class->update = sp_star_update;
86         item_class->description = sp_star_description;
87         item_class->snappoints = sp_star_snappoints;
89         shape_class->set_shape = sp_star_set_shape;
90 }
92 static void
93 sp_star_init (SPStar * star)
94 {
95         star->sides = 5;
96         star->center = NR::Point(0, 0);
97         star->r[0] = 1.0;
98         star->r[1] = 0.001;
99         star->arg[0] = star->arg[1] = 0.0;
100         star->flatsided = 0;
101         star->rounded = 0.0;
102         star->randomized = 0.0;
105 static void
106 sp_star_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
108         if (((SPObjectClass *) parent_class)->build)
109                 ((SPObjectClass *) parent_class)->build (object, document, repr);
111         sp_object_read_attr (object, "sodipodi:cx");
112         sp_object_read_attr (object, "sodipodi:cy");
113         sp_object_read_attr (object, "sodipodi:sides");
114         sp_object_read_attr (object, "sodipodi:r1");
115         sp_object_read_attr (object, "sodipodi:r2");
116         sp_object_read_attr (object, "sodipodi:arg1");
117         sp_object_read_attr (object, "sodipodi:arg2");
118         sp_object_read_attr (object, "inkscape:flatsided");
119         sp_object_read_attr (object, "inkscape:rounded");
120         sp_object_read_attr (object, "inkscape:randomized");
123 static Inkscape::XML::Node *
124 sp_star_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
126         SPStar *star = SP_STAR (object);
128         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
129                 repr = sp_repr_new ("svg:path");
130         }
132         if (flags & SP_OBJECT_WRITE_EXT) {
133                 repr->setAttribute("sodipodi:type", "star");
134                 sp_repr_set_int (repr, "sodipodi:sides", star->sides);
135                 sp_repr_set_svg_double(repr, "sodipodi:cx", star->center[NR::X]);
136                 sp_repr_set_svg_double(repr, "sodipodi:cy", star->center[NR::Y]);
137                 sp_repr_set_svg_double(repr, "sodipodi:r1", star->r[0]);
138                 sp_repr_set_svg_double(repr, "sodipodi:r2", star->r[1]);
139                 sp_repr_set_svg_double(repr, "sodipodi:arg1", star->arg[0]);
140                 sp_repr_set_svg_double(repr, "sodipodi:arg2", star->arg[1]);
141                 sp_repr_set_boolean (repr, "inkscape:flatsided", star->flatsided);
142                 sp_repr_set_svg_double(repr, "inkscape:rounded", star->rounded);
143                 sp_repr_set_svg_double(repr, "inkscape:randomized", star->randomized);
144         }
146         sp_star_set_shape ((SPShape *) star);
147         char *d = sp_svg_write_path (SP_CURVE_BPATH(((SPShape *) star)->curve));
148         repr->setAttribute("d", d);
149         g_free (d);
151         if (((SPObjectClass *) (parent_class))->write)
152                 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
154         return repr;
157 static void
158 sp_star_set (SPObject *object, unsigned int key, const gchar *value)
160         SVGLength::Unit unit;
162         SPStar *star = SP_STAR (object);
164         /* fixme: we should really collect updates */
165         switch (key) {
166         case SP_ATTR_SODIPODI_SIDES:
167                 if (value) {
168                         star->sides = atoi (value);
169                         star->sides = CLAMP (star->sides, 3, 1024);
170                 } else {
171                         star->sides = 5;
172                 }
173                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
174                 break;
175         case SP_ATTR_SODIPODI_CX:
176                 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::X]) ||
177                     (unit == SVGLength::EM) ||
178                     (unit == SVGLength::EX) ||
179                     (unit == SVGLength::PERCENT)) {
180                         star->center[NR::X] = 0.0;
181                 }
182                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
183                 break;
184         case SP_ATTR_SODIPODI_CY:
185                 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->center[NR::Y]) ||
186                     (unit == SVGLength::EM) ||
187                     (unit == SVGLength::EX) ||
188                     (unit == SVGLength::PERCENT)) {
189                         star->center[NR::Y] = 0.0;
190                 }
191                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
192                 break;
193         case SP_ATTR_SODIPODI_R1:
194                 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[0]) ||
195                     (unit == SVGLength::EM) ||
196                     (unit == SVGLength::EX) ||
197                     (unit == SVGLength::PERCENT)) {
198                         star->r[0] = 1.0;
199                 }
200                 /* fixme: Need CLAMP (Lauris) */
201                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202                 break;
203         case SP_ATTR_SODIPODI_R2:
204                 if (!sp_svg_length_read_ldd (value, &unit, NULL, &star->r[1]) ||
205                     (unit == SVGLength::EM) ||
206                     (unit == SVGLength::EX) ||
207                     (unit == SVGLength::PERCENT)) {
208                         star->r[1] = 0.0;
209                 }
210                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
211                 return;
212         case SP_ATTR_SODIPODI_ARG1:
213                 if (value) {
214                         star->arg[0] = g_ascii_strtod (value, NULL);
215                 } else {
216                         star->arg[0] = 0.0;
217                 }
218                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
219                 break;
220         case SP_ATTR_SODIPODI_ARG2:
221                 if (value) {
222                         star->arg[1] = g_ascii_strtod (value, NULL);
223                 } else {
224                         star->arg[1] = 0.0;
225                 }
226                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
227                 break;
228         case SP_ATTR_INKSCAPE_FLATSIDED:
229                 if (value && !strcmp (value, "true"))
230                         star->flatsided = true;
231                 else star->flatsided = false;
232                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
233                 break;
234         case SP_ATTR_INKSCAPE_ROUNDED:
235                 if (value) {
236                         star->rounded = g_ascii_strtod (value, NULL);
237                 } else {
238                         star->rounded = 0.0;
239                 }
240                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
241                 break;
242         case SP_ATTR_INKSCAPE_RANDOMIZED:
243                 if (value) {
244                         star->randomized = g_ascii_strtod (value, NULL);
245                 } else {
246                         star->randomized = 0.0;
247                 }
248                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
249                 break;
250         default:
251                 if (((SPObjectClass *) parent_class)->set)
252                         ((SPObjectClass *) parent_class)->set (object, key, value);
253                 break;
254         }
257 static void
258 sp_star_update (SPObject *object, SPCtx *ctx, guint flags)
260         if (flags & (SP_OBJECT_MODIFIED_FLAG |
261                      SP_OBJECT_STYLE_MODIFIED_FLAG |
262                      SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
263                 sp_shape_set_shape ((SPShape *) object);
264         }
266         if (((SPObjectClass *) parent_class)->update)
267                 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
270 static gchar *
271 sp_star_description (SPItem *item)
273     SPStar *star = SP_STAR (item);
275     // while there will never be less than 3 vertices, we still need to
276     // make calls to ngettext because the pluralization may be different
277     // for various numbers >=3.  The singular form is used as the index.
278     if (star->flatsided == false )
279         return g_strdup_printf (ngettext("<b>Star</b> with %d vertex",
280                                          "<b>Star</b> with %d vertices",
281                                          star->sides), star->sides);
282     else
283         return g_strdup_printf (ngettext("<b>Polygon</b> with %d vertex",
284                                          "<b>Polygon</b> with %d vertices",
285                                          star->sides), star->sides);
288 /**
289 Returns a unit-length vector at 90 degrees to the direction from o to n
290  */
291 static NR::Point
292 rot90_rel (NR::Point o, NR::Point n)
294         return ((1/NR::L2(n - o)) * NR::Point ((n - o)[NR::Y],  (o - n)[NR::X]));
297 /**
298 Returns a unique 32 bit int for a given point.
299 Obvious (but acceptable for my purposes) limits to uniqueness:
300 - returned value for x,y repeats for x+n*1024,y+n*1024
301 - returned value is unchanged when the point is moved by less than 1/1024 of px
302 */
303 static guint32
304 point_unique_int (NR::Point o)
306         return ((guint32)
307         65536 *
308                 (((int) floor (o[NR::X] * 64)) % 1024 + ((int) floor (o[NR::X] * 1024)) % 64)
309         +
310                 (((int) floor (o[NR::Y] * 64)) % 1024 + ((int) floor (o[NR::Y] * 1024)) % 64)
311         );
314 /**
315 Returns the next pseudorandom value using the Linear Congruential Generator algorithm (LCG)
316 with the parameters (m = 2^32, a = 69069, b = 1). These parameters give a full-period generator,
317 i.e. it is guaranteed to go through all integers < 2^32 (see http://random.mat.sbg.ac.at/~charly/server/server.html)
318 */
319 static inline guint32
320 lcg_next(guint32 const prev)
322         return (guint32) ( 69069 * prev + 1 );
325 /**
326 Returns a random number in the range [-0.5, 0.5) from the given seed, stepping the given number of steps from the seed.
327 */
328 static double
329 rnd (guint32 const seed, unsigned steps) {
330         guint32 lcg = seed;
331         for (; steps > 0; steps --)
332                 lcg = lcg_next (lcg);
334         return ( lcg / 4294967296. ) - 0.5;
337 static NR::Point
338 sp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
340         // the point whose neighboring curve handle we're calculating
341         NR::Point o = sp_star_get_xy (star, point, index);
343         // indices of previous and next points
344         gint pi = (index > 0)? (index - 1) : (star->sides - 1);
345         gint ni = (index < star->sides - 1)? (index + 1) : 0;
347         // the other point type
348         SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);
350         // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
351         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));
352         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));
354         // prev-next midpoint
355         NR::Point mid =  0.5 * (prev + next);
357         // point to which we direct the bissector of the curve handles;
358         // it's far enough outside the star on the perpendicular to prev-next through mid
359         NR::Point biss =  mid + 100000 * rot90_rel (mid, next);
361         // lengths of vectors to prev and next
362         gdouble prev_len = NR::L2 (prev - o);
363         gdouble next_len = NR::L2 (next - o);
365         // unit-length vector perpendicular to o-biss
366         NR::Point rot = rot90_rel (o, biss);
368         // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
369         NR::Point ret;
370         if (previ) {
371                 ret = (star->rounded * prev_len) * rot;
372         } else {
373                 ret = (star->rounded * next_len * -1) * rot;
374         }
376         if (star->randomized == 0) {
377                 // add the vector to o to get the final curvepoint
378                 return o + ret;
379         } else {
380                 // the seed corresponding to the exact point
381                 guint32 seed = point_unique_int (o);
383                 // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
384                 ret = ret * NR::Matrix (NR::rotate (star->randomized * M_PI * rnd (seed, 3)));
385                 ret *= ( 1 + star->randomized * rnd (seed, 4));
387                 // the randomized corner point
388                 NR::Point o_randomized = sp_star_get_xy (star, point, index, true);
390                 return o_randomized + ret;
391         }
395 #define NEXT false
396 #define PREV true
398 static void
399 sp_star_set_shape (SPShape *shape)
401         SPStar *star = SP_STAR (shape);
403         SPCurve *c = sp_curve_new ();
404         
405         gint sides = star->sides;
406         bool not_rounded = (fabs (star->rounded) < 1e-4);
408         // note that we pass randomized=true to sp_star_get_xy, because the curve must be randomized;
409         // other places that call that function (e.g. the knotholder) need the exact point
411         // draw 1st segment
412         sp_curve_moveto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
413         if (star->flatsided == false) {
414                 if (not_rounded) {
415                         sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
416                 } else {
417                         sp_curve_curveto (c,
418                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, NEXT),
419                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, 0, PREV),
420                                 sp_star_get_xy (star, SP_STAR_POINT_KNOT2, 0, true));
421                 }
422         }
424         // draw all middle segments
425         for (gint i = 1; i < sides; i++) {
426                 if (not_rounded) {
427                         sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
428                 } else {
429                         if (star->flatsided == false) {
430                                 sp_curve_curveto (c,
431                                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i - 1, NEXT),
432                                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
433                                                 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
434                         } else {
435                                 sp_curve_curveto (c,
436                                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i - 1, NEXT),
437                                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, PREV),
438                                                 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, i, true));
439                         }
440                 }
441                 if (star->flatsided == false) {
443                         if (not_rounded) {
444                        sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
445                         } else {
446                                 sp_curve_curveto (c,
447                                         sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, i, NEXT),
448                                         sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, i, PREV),
449                                         sp_star_get_xy (star, SP_STAR_POINT_KNOT2, i, true));
450                         }
451                 }
452         }
453         
454         // draw last segment
455                 if (not_rounded) {
456                         sp_curve_lineto (c, sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
457                 } else {
458                         if (star->flatsided == false) {
459                         sp_curve_curveto (c,
460                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
461                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
462                                 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
463                         } else {
464                         sp_curve_curveto (c,
465                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
466                                 sp_star_get_curvepoint (star, SP_STAR_POINT_KNOT1, 0, PREV),
467                                 sp_star_get_xy (star, SP_STAR_POINT_KNOT1, 0, true));
468                         }
469                 }
471         sp_curve_closepath (c);
472         sp_shape_set_curve_insync (SP_SHAPE (star), c, TRUE);
473         sp_curve_unref (c);
476 void
477 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)
479         g_return_if_fail (star != NULL);
480         g_return_if_fail (SP_IS_STAR (star));
481         
482         star->sides = CLAMP (sides, 3, 1024);
483         star->center = center;
484         star->r[0] = MAX (r1, 0.001);
485         if (isflat == false) {
486                 star->r[1] = CLAMP (r2, 0.0, star->r[0]);
487         } else {
488                 star->r[1] = CLAMP ( r1*cos(M_PI/sides) ,0.0, star->r[0] );
489         }
490         star->arg[0] = arg1;
491         star->arg[1] = arg2;
492         star->flatsided = isflat;
493         star->rounded = rounded;
494         star->randomized = randomized;
495         SP_OBJECT(star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
498 /* fixme: We should use all corners of star (Lauris) */
500 static void sp_star_snappoints(SPItem const *item, SnapPointsIter p)
502         if (((SPItemClass *) parent_class)->snappoints) {
503                 ((SPItemClass *) parent_class)->snappoints (item, p);
504         }
507 /**
508  * sp_star_get_xy: Get X-Y value as item coordinate system
509  * @star: star item
510  * @point: point type to obtain X-Y value
511  * @index: index of vertex
512  * @p: pointer to store X-Y value
513  * @randomized: false (default) if you want to get exact, not randomized point
514  *
515  * Initial item coordinate system is same as document coordinate system.
516  */
518 NR::Point
519 sp_star_get_xy (SPStar *star, SPStarPoint point, gint index, bool randomized)
521         gdouble darg = 2.0 * M_PI / (double) star->sides;
523         double arg = star->arg[point];
524         arg += index * darg;
526         NR::Point xy = star->r[point] * NR::Point(cos(arg), sin(arg)) + star->center;
528         if (!randomized || star->randomized == 0) {
529                 // return the exact point
530                 return xy;
531         } else { // randomize the point
532                 // find out the seed, unique for this point so that randomization is the same so long as the original point is stationary
533                 guint32 seed = point_unique_int (xy);
534                 // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
535                 double range = 2 * MAX (star->r[0], star->r[1]);
536                 // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
537                 NR::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
538                 // add the shift to the exact point
539                 return xy + shift;
540         }