Code

Some consistency cleanup for descriptions.
[inkscape.git] / src / sp-ellipse.cpp
1 #define __SP_ELLIPSE_C__
3 /*
4  * SVG <ellipse> and related implementations
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   Mitsuru Oka
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
22 #include "libnr/nr-matrix-fns.h"
23 #include "svg/svg.h"
24 #include "svg/path-string.h"
25 #include "xml/repr.h"
26 #include "attributes.h"
27 #include "style.h"
28 #include "display/curve.h"
29 #include <glibmm/i18n.h>
30 #include <2geom/transforms.h>
32 #include "document.h"
33 #include "sp-ellipse.h"
35 #include "preferences.h"
37 /* Common parent class */
39 #define noELLIPSE_VERBOSE
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846
43 #endif
45 #define SP_2PI (2 * M_PI)
47 #if 1
48 /* Hmmm... shouldn't this also qualify */
49 /* Whether it is faster or not, well, nobody knows */
50 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
51 #else
52 /* we do not use C99 round(3) function yet */
53 static double sp_round(double x, double y)
54 {
55     double remain;
57     g_assert(y > 0.0);
59     /* return round(x/y) * y; */
61     remain = fmod(x, y);
63     if (remain >= 0.5*y)
64         return x - remain + y;
65     else
66         return x - remain;
67 }
68 #endif
70 static void sp_genericellipse_class_init(SPGenericEllipseClass *klass);
71 static void sp_genericellipse_init(SPGenericEllipse *ellipse);
73 static void sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags);
75 static void sp_genericellipse_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
77 static void sp_genericellipse_set_shape(SPShape *shape);
78 static void sp_genericellipse_update_patheffect (SPLPEItem *lpeitem, bool write);
80 static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr,
81                                                     guint flags);
83 static gboolean sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr);
85 static SPShapeClass *ge_parent_class;
87 GType
88 sp_genericellipse_get_type(void)
89 {
90     static GType type = 0;
91     if (!type) {
92         GTypeInfo info = {
93             sizeof(SPGenericEllipseClass),
94             NULL,   /* base_init */
95             NULL,   /* base_finalize */
96             (GClassInitFunc) sp_genericellipse_class_init,
97             NULL,   /* class_finalize */
98             NULL,   /* class_data */
99             sizeof(SPGenericEllipse),
100             16,   /* n_preallocs */
101             (GInstanceInitFunc) sp_genericellipse_init,
102             NULL,   /* value_table */
103         };
104         type = g_type_register_static(SP_TYPE_SHAPE, "SPGenericEllipse", &info, (GTypeFlags)0);
105     }
106     return type;
109 static void sp_genericellipse_class_init(SPGenericEllipseClass *klass)
111     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
112     SPItemClass *item_class = (SPItemClass *) klass;
113     SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass;
114     SPShapeClass *shape_class = (SPShapeClass *) klass;
116     ge_parent_class = (SPShapeClass*) g_type_class_ref(SP_TYPE_SHAPE);
118     sp_object_class->update = sp_genericellipse_update;
119     sp_object_class->write = sp_genericellipse_write;
121     item_class->snappoints = sp_genericellipse_snappoints;
123     shape_class->set_shape = sp_genericellipse_set_shape;
124     lpe_item_class->update_patheffect = sp_genericellipse_update_patheffect;
127 static void
128 sp_genericellipse_init(SPGenericEllipse *ellipse)
130     ellipse->cx.unset();
131     ellipse->cy.unset();
132     ellipse->rx.unset();
133     ellipse->ry.unset();
135     ellipse->start = 0.0;
136     ellipse->end = SP_2PI;
137     ellipse->closed = TRUE;
140 static void
141 sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags)
143     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
144         SPGenericEllipse *ellipse = (SPGenericEllipse *) object;
145         SPStyle const *style = object->style;
146         Geom::OptRect viewbox = ((SPItemCtx const *) ctx)->vp;
147         double const dx = viewbox->width();
148         double const dy = viewbox->height();
149         double const dr = sqrt(dx*dx + dy*dy)/sqrt(2);
150         double const em = style->font_size.computed;
151         double const ex = em * 0.5; // fixme: get from pango or libnrtype
152         ellipse->cx.update(em, ex, dx);
153         ellipse->cy.update(em, ex, dy);
154         ellipse->rx.update(em, ex, dr);
155         ellipse->ry.update(em, ex, dr);
156         sp_shape_set_shape((SPShape *) object);
157     }
159     if (((SPObjectClass *) ge_parent_class)->update)
160         ((SPObjectClass *) ge_parent_class)->update(object, ctx, flags);
163 static void
164 sp_genericellipse_update_patheffect(SPLPEItem *lpeitem, bool write)
166     SPShape *shape = (SPShape *) lpeitem;
167     sp_genericellipse_set_shape(shape);
169     if (write) {
170         Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
171         if ( shape->curve != NULL ) {
172             gchar *str = sp_svg_write_path(shape->curve->get_pathvector());
173             repr->setAttribute("d", str);
174             g_free(str);
175         } else {
176             repr->setAttribute("d", NULL);
177         }
178     }
180     ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
184 #define C1 0.552
186 /* fixme: Think (Lauris) */
187 /* Can't we use arcto in this method? */
188 static void sp_genericellipse_set_shape(SPShape *shape)
190     double rx, ry, s, e;
191     double x0, y0, x1, y1, x2, y2, x3, y3;
192     double len;
193     gint slice = FALSE;
194  //   gint i;
196     SPGenericEllipse *ellipse = (SPGenericEllipse *) shape;
198     if ((ellipse->rx.computed < 1e-18) || (ellipse->ry.computed < 1e-18)) return;
199     if (fabs(ellipse->end - ellipse->start) < 1e-9) return;
201     sp_genericellipse_normalize(ellipse);
203     rx = ellipse->rx.computed;
204     ry = ellipse->ry.computed;
206     // figure out if we have a slice, guarding against rounding errors
207     len = fmod(ellipse->end - ellipse->start, SP_2PI);
208     if (len < 0.0) len += SP_2PI;
209     if (fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8) {
210         slice = FALSE;
211         ellipse->end = ellipse->start + SP_2PI;
212     } else {
213         slice = TRUE;
214     }
216     SPCurve * curve = new SPCurve();
217     curve->moveto(cos(ellipse->start), sin(ellipse->start));
219     for (s = ellipse->start; s < ellipse->end; s += M_PI_2) {
220         e = s + M_PI_2;
221         if (e > ellipse->end)
222             e = ellipse->end;
223         len = C1 * (e - s) / M_PI_2;
224         x0 = cos(s);
225         y0 = sin(s);
226         x1 = x0 + len * cos(s + M_PI_2);
227         y1 = y0 + len * sin(s + M_PI_2);
228         x3 = cos(e);
229         y3 = sin(e);
230         x2 = x3 + len * cos(e - M_PI_2);
231         y2 = y3 + len * sin(e - M_PI_2);
232 #ifdef ELLIPSE_VERBOSE
233         g_print("step %d s %f e %f coords %f %f %f %f %f %f\n",
234                 i, s, e, x1, y1, x2, y2, x3, y3);
235 #endif
236         curve->curveto(x1,y1, x2,y2, x3,y3);
237     }
239     if (slice && ellipse->closed) {  // TODO: is this check for "ellipse->closed" necessary?
240         curve->lineto(0., 0.);
241     }
242     if (ellipse->closed) {
243         curve->closepath();
244     }
246     Geom::Matrix aff = Geom::Scale(rx, ry) * Geom::Translate(ellipse->cx.computed, ellipse->cy.computed);
247     curve->transform(aff);
249     /* Reset the shape'scurve to the "original_curve"
250      * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
251     sp_shape_set_curve_insync (shape, curve, TRUE);
252     if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
253         SPCurve *c_lpe = curve->copy();
254         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
255         if (success) {
256             sp_shape_set_curve_insync (shape, c_lpe, TRUE);
257         }
258         c_lpe->unref();
259     }
260     curve->unref();
263 static void sp_genericellipse_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
265     g_assert(item != NULL);
266     g_assert(SP_IS_GENERICELLIPSE(item));
268     // Help enforcing strict snapping, i.e. only return nodes when we're snapping nodes to nodes or a guide to nodes
269         if (!(snapprefs->getSnapModeNode() || snapprefs->getSnapModeGuide())) {
270                 return;
271         }
273     SPGenericEllipse *ellipse = SP_GENERICELLIPSE(item);
274     sp_genericellipse_normalize(ellipse);
275     Geom::Matrix const i2d = sp_item_i2d_affine(item);
277     // figure out if we have a slice, while guarding against rounding errors
278     bool slice = false;
279     double len = fmod(ellipse->end - ellipse->start, SP_2PI);
280     if (len < 0.0) len += SP_2PI;
281     if (fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8) {
282         slice = false;
283         ellipse->end = ellipse->start + SP_2PI;
284     } else {
285         slice = true;
286     }
288     double rx = ellipse->rx.computed;
289     double ry = ellipse->ry.computed;
290     double cx = ellipse->cx.computed;
291     double cy = ellipse->cy.computed;
293     // Snap to the 4 quadrant points of the ellipse, but only if the arc
294     // spans far enough to include them
295     if (snapprefs->getSnapToItemNode()) { //TODO: Make a separate snap option toggle for this?
296                 double angle = 0;
297                 for (angle = 0; angle < SP_2PI; angle += M_PI_2) {
298                         if (angle >= ellipse->start && angle <= ellipse->end) {
299                                 *p = Geom::Point(cx + cos(angle)*rx, cy + sin(angle)*ry) * i2d;
300                         }
301                 }
302     }
304     // Add the centre, if we have a closed slice or when explicitly asked for
305     if ((snapprefs->getSnapToItemNode() && slice && ellipse->closed) || snapprefs->getSnapObjectMidpoints()) {
306         *p = Geom::Point(cx, cy) * i2d;
307     }
309     // And if we have a slice, also snap to the endpoints
310     if (snapprefs->getSnapToItemNode() && slice) {
311         // Add the start point, if it's not coincident with a quadrant point
312         if (fmod(ellipse->start, M_PI_2) != 0.0 ) {
313             *p = Geom::Point(cx + cos(ellipse->start)*rx, cy + sin(ellipse->start)*ry) * i2d;
314         }
315         // Add the end point, if it's not coincident with a quadrant point
316         if (fmod(ellipse->end, M_PI_2) != 0.0 ) {
317             *p = Geom::Point(cx + cos(ellipse->end)*rx, cy + sin(ellipse->end)*ry) * i2d;
318         }
319     }
322 void
323 sp_genericellipse_normalize(SPGenericEllipse *ellipse)
325     ellipse->start = fmod(ellipse->start, SP_2PI);
326     ellipse->end = fmod(ellipse->end, SP_2PI);
328     if (ellipse->start < 0.0)
329         ellipse->start += SP_2PI;
330     double diff = ellipse->start - ellipse->end;
331     if (diff >= 0.0)
332         ellipse->end += diff - fmod(diff, SP_2PI) + SP_2PI;
334     /* Now we keep: 0 <= start < end <= 2*PI */
337 static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
339     SPGenericEllipse *ellipse = SP_GENERICELLIPSE(object);
341     if (flags & SP_OBJECT_WRITE_EXT) {
342         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
343             repr = xml_doc->createElement("svg:path");
344         }
346         sp_repr_set_svg_double(repr, "sodipodi:cx", ellipse->cx.computed);
347         sp_repr_set_svg_double(repr, "sodipodi:cy", ellipse->cy.computed);
348         sp_repr_set_svg_double(repr, "sodipodi:rx", ellipse->rx.computed);
349         sp_repr_set_svg_double(repr, "sodipodi:ry", ellipse->ry.computed);
351         if (SP_IS_ARC(ellipse))
352             sp_arc_set_elliptical_path_attribute(SP_ARC(object), SP_OBJECT_REPR(object));
353     }
355     if (((SPObjectClass *) ge_parent_class)->write)
356         ((SPObjectClass *) ge_parent_class)->write(object, xml_doc, repr, flags);
358     return repr;
361 /* SVG <ellipse> element */
363 static void sp_ellipse_class_init(SPEllipseClass *klass);
364 static void sp_ellipse_init(SPEllipse *ellipse);
366 static void sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
367 static Inkscape::XML::Node *sp_ellipse_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
368 static void sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value);
369 static gchar *sp_ellipse_description(SPItem *item);
371 static SPGenericEllipseClass *ellipse_parent_class;
373 GType
374 sp_ellipse_get_type(void)
376     static GType type = 0;
377     if (!type) {
378         GTypeInfo info = {
379             sizeof(SPEllipseClass),
380             NULL,   /* base_init */
381             NULL,   /* base_finalize */
382             (GClassInitFunc) sp_ellipse_class_init,
383             NULL,   /* class_finalize */
384             NULL,   /* class_data */
385             sizeof(SPEllipse),
386             16,   /* n_preallocs */
387             (GInstanceInitFunc) sp_ellipse_init,
388             NULL,   /* value_table */
389         };
390         type = g_type_register_static(SP_TYPE_GENERICELLIPSE, "SPEllipse", &info, (GTypeFlags)0);
391     }
392     return type;
395 static void sp_ellipse_class_init(SPEllipseClass *klass)
397     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
398     SPItemClass *item_class = (SPItemClass *) klass;
400     ellipse_parent_class = (SPGenericEllipseClass*) g_type_class_ref(SP_TYPE_GENERICELLIPSE);
402     sp_object_class->build = sp_ellipse_build;
403     sp_object_class->write = sp_ellipse_write;
404     sp_object_class->set = sp_ellipse_set;
406     item_class->description = sp_ellipse_description;
409 static void
410 sp_ellipse_init(SPEllipse */*ellipse*/)
412     /* Nothing special */
415 static void
416 sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
418     if (((SPObjectClass *) ellipse_parent_class)->build)
419         (* ((SPObjectClass *) ellipse_parent_class)->build) (object, document, repr);
421     sp_object_read_attr(object, "cx");
422     sp_object_read_attr(object, "cy");
423     sp_object_read_attr(object, "rx");
424     sp_object_read_attr(object, "ry");
427 static Inkscape::XML::Node *
428 sp_ellipse_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
430     SPGenericEllipse *ellipse;
432     ellipse = SP_GENERICELLIPSE(object);
434     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
435         repr = xml_doc->createElement("svg:ellipse");
436     }
438     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
439     sp_repr_set_svg_double(repr, "cy", ellipse->cy.computed);
440     sp_repr_set_svg_double(repr, "rx", ellipse->rx.computed);
441     sp_repr_set_svg_double(repr, "ry", ellipse->ry.computed);
443     if (((SPObjectClass *) ellipse_parent_class)->write)
444         (* ((SPObjectClass *) ellipse_parent_class)->write) (object, xml_doc, repr, flags);
446     return repr;
449 static void
450 sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value)
452     SPGenericEllipse *ellipse;
454     ellipse = SP_GENERICELLIPSE(object);
456     switch (key) {
457         case SP_ATTR_CX:
458             ellipse->cx.readOrUnset(value);
459             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
460             break;
461         case SP_ATTR_CY:
462             ellipse->cy.readOrUnset(value);
463             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
464             break;
465         case SP_ATTR_RX:
466             if (!ellipse->rx.read(value) || (ellipse->rx.value <= 0.0)) {
467                 ellipse->rx.unset();
468             }
469             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
470             break;
471         case SP_ATTR_RY:
472             if (!ellipse->ry.read(value) || (ellipse->ry.value <= 0.0)) {
473                 ellipse->ry.unset();
474             }
475             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
476             break;
477         default:
478             if (((SPObjectClass *) ellipse_parent_class)->set)
479                 ((SPObjectClass *) ellipse_parent_class)->set(object, key, value);
480             break;
481     }
484 static gchar *sp_ellipse_description(SPItem */*item*/)
486     return g_strdup(_("<b>Ellipse</b>"));
490 void
491 sp_ellipse_position_set(SPEllipse *ellipse, gdouble x, gdouble y, gdouble rx, gdouble ry)
493     SPGenericEllipse *ge;
495     g_return_if_fail(ellipse != NULL);
496     g_return_if_fail(SP_IS_ELLIPSE(ellipse));
498     ge = SP_GENERICELLIPSE(ellipse);
500     ge->cx.computed = x;
501     ge->cy.computed = y;
502     ge->rx.computed = rx;
503     ge->ry.computed = ry;
505     ((SPObject *)ge)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
508 /* SVG <circle> element */
510 static void sp_circle_class_init(SPCircleClass *klass);
511 static void sp_circle_init(SPCircle *circle);
513 static void sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
514 static Inkscape::XML::Node *sp_circle_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
515 static void sp_circle_set(SPObject *object, unsigned int key, gchar const *value);
516 static gchar *sp_circle_description(SPItem *item);
518 static SPGenericEllipseClass *circle_parent_class;
520 GType
521 sp_circle_get_type(void)
523     static GType type = 0;
524     if (!type) {
525         GTypeInfo info = {
526             sizeof(SPCircleClass),
527             NULL,   /* base_init */
528             NULL,   /* base_finalize */
529             (GClassInitFunc) sp_circle_class_init,
530             NULL,   /* class_finalize */
531             NULL,   /* class_data */
532             sizeof(SPCircle),
533             16,   /* n_preallocs */
534             (GInstanceInitFunc) sp_circle_init,
535             NULL,   /* value_table */
536         };
537         type = g_type_register_static(SP_TYPE_GENERICELLIPSE, "SPCircle", &info, (GTypeFlags)0);
538     }
539     return type;
542 static void
543 sp_circle_class_init(SPCircleClass *klass)
545     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
546     SPItemClass *item_class = (SPItemClass *) klass;
548     circle_parent_class = (SPGenericEllipseClass*) g_type_class_ref(SP_TYPE_GENERICELLIPSE);
550     sp_object_class->build = sp_circle_build;
551     sp_object_class->write = sp_circle_write;
552     sp_object_class->set = sp_circle_set;
554     item_class->description = sp_circle_description;
557 static void
558 sp_circle_init(SPCircle */*circle*/)
560     /* Nothing special */
563 static void
564 sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
566     if (((SPObjectClass *) circle_parent_class)->build)
567         (* ((SPObjectClass *) circle_parent_class)->build)(object, document, repr);
569     sp_object_read_attr(object, "cx");
570     sp_object_read_attr(object, "cy");
571     sp_object_read_attr(object, "r");
574 static Inkscape::XML::Node *
575 sp_circle_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
577     SPGenericEllipse *ellipse;
579     ellipse = SP_GENERICELLIPSE(object);
581     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
582         repr = xml_doc->createElement("svg:circle");
583     }
585     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
586     sp_repr_set_svg_double(repr, "cy", ellipse->cy.computed);
587     sp_repr_set_svg_double(repr, "r", ellipse->rx.computed);
589     if (((SPObjectClass *) circle_parent_class)->write)
590         ((SPObjectClass *) circle_parent_class)->write(object, xml_doc, repr, flags);
592     return repr;
595 static void
596 sp_circle_set(SPObject *object, unsigned int key, gchar const *value)
598     SPGenericEllipse *ge;
600     ge = SP_GENERICELLIPSE(object);
602     switch (key) {
603         case SP_ATTR_CX:
604             ge->cx.readOrUnset(value);
605             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
606             break;
607         case SP_ATTR_CY:
608             ge->cy.readOrUnset(value);
609             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
610             break;
611         case SP_ATTR_R:
612             if (!ge->rx.read(value) || ge->rx.value <= 0.0) {
613                 ge->rx.unset();
614             }
615             ge->ry = ge->rx;
616             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
617             break;
618         default:
619             if (((SPObjectClass *) circle_parent_class)->set)
620                 ((SPObjectClass *) circle_parent_class)->set(object, key, value);
621             break;
622     }
625 static gchar *sp_circle_description(SPItem */*item*/)
627     return g_strdup(_("<b>Circle</b>"));
630 /* <path sodipodi:type="arc"> element */
632 static void sp_arc_class_init(SPArcClass *klass);
633 static void sp_arc_init(SPArc *arc);
635 static void sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
636 static Inkscape::XML::Node *sp_arc_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
637 static void sp_arc_set(SPObject *object, unsigned int key, gchar const *value);
638 static void sp_arc_modified(SPObject *object, guint flags);
640 static gchar *sp_arc_description(SPItem *item);
642 static SPGenericEllipseClass *arc_parent_class;
644 GType
645 sp_arc_get_type(void)
647     static GType type = 0;
648     if (!type) {
649         GTypeInfo info = {
650             sizeof(SPArcClass),
651             NULL,   /* base_init */
652             NULL,   /* base_finalize */
653             (GClassInitFunc) sp_arc_class_init,
654             NULL,   /* class_finalize */
655             NULL,   /* class_data */
656             sizeof(SPArc),
657             16,   /* n_preallocs */
658             (GInstanceInitFunc) sp_arc_init,
659             NULL,   /* value_table */
660         };
661         type = g_type_register_static(SP_TYPE_GENERICELLIPSE, "SPArc", &info, (GTypeFlags)0);
662     }
663     return type;
666 static void
667 sp_arc_class_init(SPArcClass *klass)
669     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
670     SPItemClass *item_class = (SPItemClass *) klass;
672     arc_parent_class = (SPGenericEllipseClass*) g_type_class_ref(SP_TYPE_GENERICELLIPSE);
674     sp_object_class->build = sp_arc_build;
675     sp_object_class->write = sp_arc_write;
676     sp_object_class->set = sp_arc_set;
677     sp_object_class->modified = sp_arc_modified;
679     item_class->description = sp_arc_description;
682 static void
683 sp_arc_init(SPArc */*arc*/)
685     /* Nothing special */
688 static void
689 sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
691     if (((SPObjectClass *) arc_parent_class)->build)
692         (* ((SPObjectClass *) arc_parent_class)->build) (object, document, repr);
694     Inkscape::Version version = sp_object_get_sodipodi_version(object);
696     sp_object_read_attr(object, "sodipodi:cx");
697     sp_object_read_attr(object, "sodipodi:cy");
698     sp_object_read_attr(object, "sodipodi:rx");
699     sp_object_read_attr(object, "sodipodi:ry");
701     sp_object_read_attr(object, "sodipodi:start");
702     sp_object_read_attr(object, "sodipodi:end");
703     sp_object_read_attr(object, "sodipodi:open");
706 /*
707  * sp_arc_set_elliptical_path_attribute:
708  *
709  * Convert center to endpoint parameterization and set it to repr.
710  *
711  * See SVG 1.0 Specification W3C Recommendation
712  * ``F.6 Ellptical arc implementation notes'' for more detail.
713  */
714 static gboolean
715 sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr)
717     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
719     Inkscape::SVG::PathString str;
721     Geom::Point p1 = sp_arc_get_xy(arc, ge->start);
722     Geom::Point p2 = sp_arc_get_xy(arc, ge->end);
723     double rx = ge->rx.computed;
724     double ry = ge->ry.computed;
726     str.moveTo(p1);
728     double dt = fmod(ge->end - ge->start, SP_2PI);
729     if (fabs(dt) < 1e-6) {
730         Geom::Point ph = sp_arc_get_xy(arc, (ge->start + ge->end) / 2.0);
731         str.arcTo(rx, ry, 0, true, true, ph)
732            .arcTo(rx, ry, 0, true, true, p2)
733            .closePath();
734     } else {
735         bool fa = (fabs(dt) > M_PI);
736         bool fs = (dt > 0);
737         str.arcTo(rx, ry, 0, fa, fs, p2);
738         if (ge->closed) {
739             Geom::Point center = Geom::Point(ge->cx.computed, ge->cy.computed);
740             str.lineTo(center).closePath();
741         }
742     }
744     repr->setAttribute("d", str.c_str());
745     return true;
748 static Inkscape::XML::Node *
749 sp_arc_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
751     SPGenericEllipse *ge = SP_GENERICELLIPSE(object);
752     SPArc *arc = SP_ARC(object);
754     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
755         repr = xml_doc->createElement("svg:path");
756     }
758     if (flags & SP_OBJECT_WRITE_EXT) {
759         repr->setAttribute("sodipodi:type", "arc");
760         sp_repr_set_svg_double(repr, "sodipodi:cx", ge->cx.computed);
761         sp_repr_set_svg_double(repr, "sodipodi:cy", ge->cy.computed);
762         sp_repr_set_svg_double(repr, "sodipodi:rx", ge->rx.computed);
763         sp_repr_set_svg_double(repr, "sodipodi:ry", ge->ry.computed);
765         // write start and end only if they are non-trivial; otherwise remove
766         gdouble len = fmod(ge->end - ge->start, SP_2PI);
767         if (len < 0.0) len += SP_2PI;
768         if (!(fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8)) {
769             sp_repr_set_svg_double(repr, "sodipodi:start", ge->start);
770             sp_repr_set_svg_double(repr, "sodipodi:end", ge->end);
771             repr->setAttribute("sodipodi:open", (!ge->closed) ? "true" : NULL);
772         } else {
773             repr->setAttribute("sodipodi:end", NULL);
774             repr->setAttribute("sodipodi:start", NULL);
775             repr->setAttribute("sodipodi:open", NULL);
776         }
777     }
779     // write d=
780     sp_arc_set_elliptical_path_attribute(arc, repr);
782     if (((SPObjectClass *) arc_parent_class)->write)
783         ((SPObjectClass *) arc_parent_class)->write(object, xml_doc, repr, flags);
785     return repr;
788 static void
789 sp_arc_set(SPObject *object, unsigned int key, gchar const *value)
791     SPGenericEllipse *ge = SP_GENERICELLIPSE(object);
793     switch (key) {
794         case SP_ATTR_SODIPODI_CX:
795             ge->cx.readOrUnset(value);
796             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
797             break;
798         case SP_ATTR_SODIPODI_CY:
799             ge->cy.readOrUnset(value);
800             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
801             break;
802         case SP_ATTR_SODIPODI_RX:
803             if (!ge->rx.read(value) || ge->rx.computed <= 0.0) {
804                 ge->rx.unset();
805             }
806             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
807             break;
808         case SP_ATTR_SODIPODI_RY:
809             if (!ge->ry.read(value) || ge->ry.computed <= 0.0) {
810                 ge->ry.unset();
811             }
812             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
813             break;
814         case SP_ATTR_SODIPODI_START:
815             if (value) {
816                 sp_svg_number_read_d(value, &ge->start);
817             } else {
818                 ge->start = 0;
819             }
820             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
821             break;
822         case SP_ATTR_SODIPODI_END:
823             if (value) {
824                 sp_svg_number_read_d(value, &ge->end);
825             } else {
826                 ge->end = 2 * M_PI;
827             }
828             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
829             break;
830         case SP_ATTR_SODIPODI_OPEN:
831             ge->closed = (!value);
832             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
833             break;
834         default:
835             if (((SPObjectClass *) arc_parent_class)->set)
836                 ((SPObjectClass *) arc_parent_class)->set(object, key, value);
837             break;
838     }
841 static void
842 sp_arc_modified(SPObject *object, guint flags)
844     if (flags & SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG) {
845         sp_shape_set_shape((SPShape *) object);
846     }
848     if (((SPObjectClass *) arc_parent_class)->modified)
849         ((SPObjectClass *) arc_parent_class)->modified(object, flags);
852 static gchar *sp_arc_description(SPItem *item)
854     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
856     gdouble len = fmod(ge->end - ge->start, SP_2PI);
857     if (len < 0.0) len += SP_2PI;
858     if (!(fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8)) {
859         if (ge->closed) {
860             return g_strdup(_("<b>Segment</b>"));
861         } else {
862             return g_strdup(_("<b>Arc</b>"));
863         }
864     } else {
865         return g_strdup(_("<b>Ellipse</b>"));
866     }
869 void
870 sp_arc_position_set(SPArc *arc, gdouble x, gdouble y, gdouble rx, gdouble ry)
872     g_return_if_fail(arc != NULL);
873     g_return_if_fail(SP_IS_ARC(arc));
875     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
877     ge->cx.computed = x;
878     ge->cy.computed = y;
879     ge->rx.computed = rx;
880     ge->ry.computed = ry;
881     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
882     if (prefs->getDouble("/tools/shapes/arc/start", 0.0) != 0)
883         ge->start = prefs->getDouble("/tools/shapes/arc/start", 0.0);
884     if (prefs->getDouble("/tools/shapes/arc/end", 0.0) != 0)
885         ge->end = prefs->getDouble("/tools/shapes/arc/end", 0.0);
886     if (!prefs->getBool("/tools/shapes/arc/open"))
887         ge->closed = 1;
888     else
889         ge->closed = 0;
891     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
894 Geom::Point sp_arc_get_xy(SPArc *arc, gdouble arg)
896     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
898     return Geom::Point(ge->rx.computed * cos(arg) + ge->cx.computed,
899                      ge->ry.computed * sin(arg) + ge->cy.computed);
903 /*
904   Local Variables:
905   mode:c++
906   c-file-style:"stroustrup"
907   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
908   indent-tabs-mode:nil
909   fill-column:99
910   End:
911 */
912 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :