Code

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