Code

Optimized text output by not repeating glyph coordinates when they are identical
[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/n-art-bpath.h"
23 #include "libnr/nr-path.h"
24 #include "libnr/nr-matrix-fns.h"
25 #include "svg/svg.h"
26 #include "svg/path-string.h"
27 #include "xml/repr.h"
28 #include "attributes.h"
29 #include "style.h"
30 #include "display/curve.h"
31 #include <glibmm/i18n.h>
33 #include "document.h"
34 #include "sp-ellipse.h"
36 #include "prefs-utils.h"
38 /* Common parent class */
40 #define noELLIPSE_VERBOSE
42 #ifndef M_PI
43 #define M_PI 3.14159265358979323846
44 #endif
46 #define SP_2PI (2 * M_PI)
48 #if 1
49 /* Hmmm... shouldn't this also qualify */
50 /* Whether it is faster or not, well, nobody knows */
51 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
52 #else
53 /* we do not use C99 round(3) function yet */
54 static double sp_round(double x, double y)
55 {
56     double remain;
58     g_assert(y > 0.0);
60     /* return round(x/y) * y; */
62     remain = fmod(x, y);
64     if (remain >= 0.5*y)
65         return x - remain + y;
66     else
67         return x - remain;
68 }
69 #endif
71 static void sp_genericellipse_class_init(SPGenericEllipseClass *klass);
72 static void sp_genericellipse_init(SPGenericEllipse *ellipse);
74 static void sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags);
76 static void sp_genericellipse_snappoints(SPItem const *item, SnapPointsIter p);
78 static void sp_genericellipse_set_shape(SPShape *shape);
79 static void sp_genericellipse_update_patheffect (SPShape *shape, bool write);
81 static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Node *repr,
82                                                     guint flags);
84 static gboolean sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr);
86 static SPShapeClass *ge_parent_class;
88 GType
89 sp_genericellipse_get_type(void)
90 {
91     static GType type = 0;
92     if (!type) {
93         GTypeInfo info = {
94             sizeof(SPGenericEllipseClass),
95             NULL,   /* base_init */
96             NULL,   /* base_finalize */
97             (GClassInitFunc) sp_genericellipse_class_init,
98             NULL,   /* class_finalize */
99             NULL,   /* class_data */
100             sizeof(SPGenericEllipse),
101             16,   /* n_preallocs */
102             (GInstanceInitFunc) sp_genericellipse_init,
103             NULL,   /* value_table */
104         };
105         type = g_type_register_static(SP_TYPE_SHAPE, "SPGenericEllipse", &info, (GTypeFlags)0);
106     }
107     return type;
110 static void sp_genericellipse_class_init(SPGenericEllipseClass *klass)
112     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
113     SPItemClass *item_class = (SPItemClass *) 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     shape_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         double const d = 1.0 / NR::expansion(((SPItemCtx const *) ctx)->i2vp);
147         double const em = style->font_size.computed;
148         double const ex = em * 0.5; // fixme: get from pango or libnrtype
149         ellipse->cx.update(em, ex, d);
150         ellipse->cy.update(em, ex, d);
151         ellipse->rx.update(em, ex, d);
152         ellipse->ry.update(em, ex, d);
153         sp_shape_set_shape((SPShape *) object);
154     }
156     if (((SPObjectClass *) ge_parent_class)->update)
157         ((SPObjectClass *) ge_parent_class)->update(object, ctx, flags);
160 static void
161 sp_genericellipse_update_patheffect(SPShape *shape, bool write)
163     sp_genericellipse_set_shape(shape);
165     if (write) {
166         Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
167         if ( shape->curve != NULL ) {
168             NArtBpath *abp = sp_curve_first_bpath(shape->curve);
169             if (abp) {
170                 gchar *str = sp_svg_write_path(abp);
171                 repr->setAttribute("d", str);
172                 g_free(str);
173             } else {
174                 repr->setAttribute("d", "");
175             }
176         } else {
177             repr->setAttribute("d", NULL);
178         }
179     }
181     ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
185 #define C1 0.552
187 /* fixme: Think (Lauris) */
189 static void sp_genericellipse_set_shape(SPShape *shape)
191     double cx, cy, rx, ry, s, e;
192     double x0, y0, x1, y1, x2, y2, x3, y3;
193     double len;
194     gint slice = FALSE;
195     gint i;
197     SPGenericEllipse *ellipse = (SPGenericEllipse *) shape;
199     if ((ellipse->rx.computed < 1e-18) || (ellipse->ry.computed < 1e-18)) return;
200     if (fabs(ellipse->end - ellipse->start) < 1e-9) return;
202     sp_genericellipse_normalize(ellipse);
204     cx = 0.0;
205     cy = 0.0;
206     rx = ellipse->rx.computed;
207     ry = ellipse->ry.computed;
209     // figure out if we have a slice, guarding against rounding errors
210     len = fmod(ellipse->end - ellipse->start, SP_2PI);
211     if (len < 0.0) len += SP_2PI;
212     if (fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8) {
213         slice = FALSE;
214         ellipse->end = ellipse->start + SP_2PI;
215     } else {
216         slice = TRUE;
217     }
219     NR::Matrix aff = NR::Matrix(NR::scale(rx, ry));
220     aff[4] = ellipse->cx.computed;
221     aff[5] = ellipse->cy.computed;
223     NArtBpath bpath[16];
224     i = 0;
225     if (ellipse->closed) {
226         bpath[i].code = NR_MOVETO;
227     } else {
228         bpath[i].code = NR_MOVETO_OPEN;
229     }
230     bpath[i].x3 = cos(ellipse->start);
231     bpath[i].y3 = sin(ellipse->start);
232     i++;
234     for (s = ellipse->start; s < ellipse->end; s += M_PI_2) {
235         e = s + M_PI_2;
236         if (e > ellipse->end)
237             e = ellipse->end;
238         len = C1 * (e - s) / M_PI_2;
239         x0 = cos(s);
240         y0 = sin(s);
241         x1 = x0 + len * cos(s + M_PI_2);
242         y1 = y0 + len * sin(s + M_PI_2);
243         x3 = cos(e);
244         y3 = sin(e);
245         x2 = x3 + len * cos(e - M_PI_2);
246         y2 = y3 + len * sin(e - M_PI_2);
247 #ifdef ELLIPSE_VERBOSE
248         g_print("step %d s %f e %f coords %f %f %f %f %f %f\n",
249                 i, s, e, x1, y1, x2, y2, x3, y3);
250 #endif
251         bpath[i].code = NR_CURVETO;
252         bpath[i].x1 = x1;
253         bpath[i].y1 = y1;
254         bpath[i].x2 = x2;
255         bpath[i].y2 = y2;
256         bpath[i].x3 = x3;
257         bpath[i].y3 = y3;
258         i++;
259     }
261     if (slice && ellipse->closed) {
262         bpath[i].code = NR_LINETO;
263         bpath[i].x3 = 0.0;
264         bpath[i].y3 = 0.0;
265         i++;
266         bpath[i].code = NR_LINETO;
267         bpath[i].x3 = bpath[0].x3;
268         bpath[i].y3 = bpath[0].y3;
269         i++;
270     } else if (ellipse->closed) {
271         bpath[i-1].x3 = bpath[0].x3;
272         bpath[i-1].y3 = bpath[0].y3;
273     }
275     bpath[i].code = NR_END;
276     SPCurve *c = sp_curve_new_from_bpath(nr_artpath_affine(bpath, aff));
277     g_assert(c != NULL);
279     sp_shape_perform_path_effect(c, SP_SHAPE (ellipse));
280     sp_shape_set_curve_insync((SPShape *) ellipse, c, TRUE);
281     sp_curve_unref(c);
284 static void sp_genericellipse_snappoints(SPItem const *item, SnapPointsIter p)
286     SPGenericEllipse const *ge = SP_GENERICELLIPSE(item);
288     NR::Matrix const i2d = sp_item_i2d_affine(item);
290     /* Add the centre */
291     *p = NR::Point(ge->cx.computed, ge->cy.computed) * i2d;
293     // TODO: add the ends of radii
296 void
297 sp_genericellipse_normalize(SPGenericEllipse *ellipse)
299     ellipse->start = fmod(ellipse->start, SP_2PI);
300     ellipse->end = fmod(ellipse->end, SP_2PI);
302     if (ellipse->start < 0.0)
303         ellipse->start += SP_2PI;
304     double diff = ellipse->start - ellipse->end;
305     if (diff >= 0.0)
306         ellipse->end += diff - fmod(diff, SP_2PI) + SP_2PI;
308     /* Now we keep: 0 <= start < end <= 2*PI */
311 static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
313     SPGenericEllipse *ellipse = SP_GENERICELLIPSE(object);
315     if (flags & SP_OBJECT_WRITE_EXT) {
316         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
317             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
318             repr = xml_doc->createElement("svg:path");
319         }
321         sp_repr_set_svg_double(repr, "sodipodi:cx", ellipse->cx.computed);
322         sp_repr_set_svg_double(repr, "sodipodi:cy", ellipse->cy.computed);
323         sp_repr_set_svg_double(repr, "sodipodi:rx", ellipse->rx.computed);
324         sp_repr_set_svg_double(repr, "sodipodi:ry", ellipse->ry.computed);
326         if (SP_IS_ARC(ellipse))
327             sp_arc_set_elliptical_path_attribute(SP_ARC(object), SP_OBJECT_REPR(object));
328     }
330     if (((SPObjectClass *) ge_parent_class)->write)
331         ((SPObjectClass *) ge_parent_class)->write(object, repr, flags);
333     return repr;
336 /* SVG <ellipse> element */
338 static void sp_ellipse_class_init(SPEllipseClass *klass);
339 static void sp_ellipse_init(SPEllipse *ellipse);
341 static void sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
342 static Inkscape::XML::Node *sp_ellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
343 static void sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value);
344 static gchar *sp_ellipse_description(SPItem *item);
346 static SPGenericEllipseClass *ellipse_parent_class;
348 GType
349 sp_ellipse_get_type(void)
351     static GType type = 0;
352     if (!type) {
353         GTypeInfo info = {
354             sizeof(SPEllipseClass),
355             NULL,   /* base_init */
356             NULL,   /* base_finalize */
357             (GClassInitFunc) sp_ellipse_class_init,
358             NULL,   /* class_finalize */
359             NULL,   /* class_data */
360             sizeof(SPEllipse),
361             16,   /* n_preallocs */
362             (GInstanceInitFunc) sp_ellipse_init,
363             NULL,   /* value_table */
364         };
365         type = g_type_register_static(SP_TYPE_GENERICELLIPSE, "SPEllipse", &info, (GTypeFlags)0);
366     }
367     return type;
370 static void sp_ellipse_class_init(SPEllipseClass *klass)
372     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
373     SPItemClass *item_class = (SPItemClass *) klass;
375     ellipse_parent_class = (SPGenericEllipseClass*) g_type_class_ref(SP_TYPE_GENERICELLIPSE);
377     sp_object_class->build = sp_ellipse_build;
378     sp_object_class->write = sp_ellipse_write;
379     sp_object_class->set = sp_ellipse_set;
381     item_class->description = sp_ellipse_description;
384 static void
385 sp_ellipse_init(SPEllipse *ellipse)
387     /* Nothing special */
390 static void
391 sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
393     if (((SPObjectClass *) ellipse_parent_class)->build)
394         (* ((SPObjectClass *) ellipse_parent_class)->build) (object, document, repr);
396     sp_object_read_attr(object, "cx");
397     sp_object_read_attr(object, "cy");
398     sp_object_read_attr(object, "rx");
399     sp_object_read_attr(object, "ry");
402 static Inkscape::XML::Node *
403 sp_ellipse_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
405     SPGenericEllipse *ellipse;
407     ellipse = SP_GENERICELLIPSE(object);
409     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
410         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
411         repr = xml_doc->createElement("svg:ellipse");
412     }
414     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
415     sp_repr_set_svg_double(repr, "cy", ellipse->cy.computed);
416     sp_repr_set_svg_double(repr, "rx", ellipse->rx.computed);
417     sp_repr_set_svg_double(repr, "ry", ellipse->ry.computed);
419     if (((SPObjectClass *) ellipse_parent_class)->write)
420         (* ((SPObjectClass *) ellipse_parent_class)->write) (object, repr, flags);
422     return repr;
425 static void
426 sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value)
428     SPGenericEllipse *ellipse;
430     ellipse = SP_GENERICELLIPSE(object);
432     switch (key) {
433         case SP_ATTR_CX:
434             ellipse->cx.readOrUnset(value);
435             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
436             break;
437         case SP_ATTR_CY:
438             ellipse->cy.readOrUnset(value);
439             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
440             break;
441         case SP_ATTR_RX:
442             if (!ellipse->rx.read(value) || (ellipse->rx.value <= 0.0)) {
443                 ellipse->rx.unset();
444             }
445             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
446             break;
447         case SP_ATTR_RY:
448             if (!ellipse->ry.read(value) || (ellipse->ry.value <= 0.0)) {
449                 ellipse->ry.unset();
450             }
451             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
452             break;
453         default:
454             if (((SPObjectClass *) ellipse_parent_class)->set)
455                 ((SPObjectClass *) ellipse_parent_class)->set(object, key, value);
456             break;
457     }
460 static gchar *sp_ellipse_description(SPItem *item)
462     return g_strdup(_("<b>Ellipse</b>"));
466 void
467 sp_ellipse_position_set(SPEllipse *ellipse, gdouble x, gdouble y, gdouble rx, gdouble ry)
469     SPGenericEllipse *ge;
471     g_return_if_fail(ellipse != NULL);
472     g_return_if_fail(SP_IS_ELLIPSE(ellipse));
474     ge = SP_GENERICELLIPSE(ellipse);
476     ge->cx.computed = x;
477     ge->cy.computed = y;
478     ge->rx.computed = rx;
479     ge->ry.computed = ry;
481     ((SPObject *)ge)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
484 /* SVG <circle> element */
486 static void sp_circle_class_init(SPCircleClass *klass);
487 static void sp_circle_init(SPCircle *circle);
489 static void sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
490 static Inkscape::XML::Node *sp_circle_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
491 static void sp_circle_set(SPObject *object, unsigned int key, gchar const *value);
492 static gchar *sp_circle_description(SPItem *item);
494 static SPGenericEllipseClass *circle_parent_class;
496 GType
497 sp_circle_get_type(void)
499     static GType type = 0;
500     if (!type) {
501         GTypeInfo info = {
502             sizeof(SPCircleClass),
503             NULL,   /* base_init */
504             NULL,   /* base_finalize */
505             (GClassInitFunc) sp_circle_class_init,
506             NULL,   /* class_finalize */
507             NULL,   /* class_data */
508             sizeof(SPCircle),
509             16,   /* n_preallocs */
510             (GInstanceInitFunc) sp_circle_init,
511             NULL,   /* value_table */
512         };
513         type = g_type_register_static(SP_TYPE_GENERICELLIPSE, "SPCircle", &info, (GTypeFlags)0);
514     }
515     return type;
518 static void
519 sp_circle_class_init(SPCircleClass *klass)
521     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
522     SPItemClass *item_class = (SPItemClass *) klass;
524     circle_parent_class = (SPGenericEllipseClass*) g_type_class_ref(SP_TYPE_GENERICELLIPSE);
526     sp_object_class->build = sp_circle_build;
527     sp_object_class->write = sp_circle_write;
528     sp_object_class->set = sp_circle_set;
530     item_class->description = sp_circle_description;
533 static void
534 sp_circle_init(SPCircle *circle)
536     /* Nothing special */
539 static void
540 sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
542     if (((SPObjectClass *) circle_parent_class)->build)
543         (* ((SPObjectClass *) circle_parent_class)->build)(object, document, repr);
545     sp_object_read_attr(object, "cx");
546     sp_object_read_attr(object, "cy");
547     sp_object_read_attr(object, "r");
550 static Inkscape::XML::Node *
551 sp_circle_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
553     SPGenericEllipse *ellipse;
555     ellipse = SP_GENERICELLIPSE(object);
557     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
558         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
559         repr = xml_doc->createElement("svg:circle");
560     }
562     sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed);
563     sp_repr_set_svg_double(repr, "cy", ellipse->cy.computed);
564     sp_repr_set_svg_double(repr, "r", ellipse->rx.computed);
566     if (((SPObjectClass *) circle_parent_class)->write)
567         ((SPObjectClass *) circle_parent_class)->write(object, repr, flags);
569     return repr;
572 static void
573 sp_circle_set(SPObject *object, unsigned int key, gchar const *value)
575     SPGenericEllipse *ge;
577     ge = SP_GENERICELLIPSE(object);
579     switch (key) {
580         case SP_ATTR_CX:
581             ge->cx.readOrUnset(value);
582             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
583             break;
584         case SP_ATTR_CY:
585             ge->cy.readOrUnset(value);
586             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
587             break;
588         case SP_ATTR_R:
589             if (!ge->rx.read(value) || ge->rx.value <= 0.0) {
590                 ge->rx.unset();
591             }
592             ge->ry = ge->rx;
593             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
594             break;
595         default:
596             if (((SPObjectClass *) circle_parent_class)->set)
597                 ((SPObjectClass *) circle_parent_class)->set(object, key, value);
598             break;
599     }
602 static gchar *sp_circle_description(SPItem *item)
604     return g_strdup(_("<b>Circle</b>"));
607 /* <path sodipodi:type="arc"> element */
609 static void sp_arc_class_init(SPArcClass *klass);
610 static void sp_arc_init(SPArc *arc);
612 static void sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
613 static Inkscape::XML::Node *sp_arc_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
614 static void sp_arc_set(SPObject *object, unsigned int key, gchar const *value);
615 static void sp_arc_modified(SPObject *object, guint flags);
617 static gchar *sp_arc_description(SPItem *item);
619 static SPGenericEllipseClass *arc_parent_class;
621 GType
622 sp_arc_get_type(void)
624     static GType type = 0;
625     if (!type) {
626         GTypeInfo info = {
627             sizeof(SPArcClass),
628             NULL,   /* base_init */
629             NULL,   /* base_finalize */
630             (GClassInitFunc) sp_arc_class_init,
631             NULL,   /* class_finalize */
632             NULL,   /* class_data */
633             sizeof(SPArc),
634             16,   /* n_preallocs */
635             (GInstanceInitFunc) sp_arc_init,
636             NULL,   /* value_table */
637         };
638         type = g_type_register_static(SP_TYPE_GENERICELLIPSE, "SPArc", &info, (GTypeFlags)0);
639     }
640     return type;
643 static void
644 sp_arc_class_init(SPArcClass *klass)
646     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
647     SPItemClass *item_class = (SPItemClass *) klass;
649     arc_parent_class = (SPGenericEllipseClass*) g_type_class_ref(SP_TYPE_GENERICELLIPSE);
651     sp_object_class->build = sp_arc_build;
652     sp_object_class->write = sp_arc_write;
653     sp_object_class->set = sp_arc_set;
654     sp_object_class->modified = sp_arc_modified;
656     item_class->description = sp_arc_description;
659 static void
660 sp_arc_init(SPArc *arc)
662     /* Nothing special */
665 static void
666 sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
668     if (((SPObjectClass *) arc_parent_class)->build)
669         (* ((SPObjectClass *) arc_parent_class)->build) (object, document, repr);
671     Inkscape::Version version = sp_object_get_sodipodi_version(object);
673     sp_object_read_attr(object, "sodipodi:cx");
674     sp_object_read_attr(object, "sodipodi:cy");
675     sp_object_read_attr(object, "sodipodi:rx");
676     sp_object_read_attr(object, "sodipodi:ry");
678     sp_object_read_attr(object, "sodipodi:start");
679     sp_object_read_attr(object, "sodipodi:end");
680     sp_object_read_attr(object, "sodipodi:open");
683 /*
684  * sp_arc_set_elliptical_path_attribute:
685  *
686  * Convert center to endpoint parameterization and set it to repr.
687  *
688  * See SVG 1.0 Specification W3C Recommendation
689  * ``F.6 Ellptical arc implementation notes'' for more detail.
690  */
691 static gboolean
692 sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr)
694     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
696     Inkscape::SVG::PathString str;
698     NR::Point p1 = sp_arc_get_xy(arc, ge->start);
699     NR::Point p2 = sp_arc_get_xy(arc, ge->end);
700     double rx = ge->rx.computed;
701     double ry = ge->ry.computed;
703     str.moveTo(p1);
705     double dt = fmod(ge->end - ge->start, SP_2PI);
706     if (fabs(dt) < 1e-6) {
707         NR::Point ph = sp_arc_get_xy(arc, (ge->start + ge->end) / 2.0);
708         str.arcTo(rx, ry, 0, true, true, ph)
709            .arcTo(rx, ry, 0, true, true, p2)
710            .closePath();
711     } else {
712         bool fa = (fabs(dt) > M_PI);
713         bool fs = (dt > 0);
714         str.arcTo(rx, ry, 0, fa, fs, p2);
715         if (ge->closed) {
716             NR::Point center = NR::Point(ge->cx.computed, ge->cy.computed);
717             str.lineTo(center).closePath();
718         }
719     }
721     repr->setAttribute("d", str.c_str());
722     return true;
725 static Inkscape::XML::Node *
726 sp_arc_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
728     SPGenericEllipse *ge = SP_GENERICELLIPSE(object);
729     SPArc *arc = SP_ARC(object);
731     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
732         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
733         repr = xml_doc->createElement("svg:path");
734     }
736     if (flags & SP_OBJECT_WRITE_EXT) {
737         repr->setAttribute("sodipodi:type", "arc");
738         sp_repr_set_svg_double(repr, "sodipodi:cx", ge->cx.computed);
739         sp_repr_set_svg_double(repr, "sodipodi:cy", ge->cy.computed);
740         sp_repr_set_svg_double(repr, "sodipodi:rx", ge->rx.computed);
741         sp_repr_set_svg_double(repr, "sodipodi:ry", ge->ry.computed);
743         // write start and end only if they are non-trivial; otherwise remove
744         gdouble len = fmod(ge->end - ge->start, SP_2PI);
745         if (len < 0.0) len += SP_2PI;
746         if (!(fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8)) {
747             sp_repr_set_svg_double(repr, "sodipodi:start", ge->start);
748             sp_repr_set_svg_double(repr, "sodipodi:end", ge->end);
749             repr->setAttribute("sodipodi:open", (!ge->closed) ? "true" : NULL);
750         } else {
751             repr->setAttribute("sodipodi:end", NULL);
752             repr->setAttribute("sodipodi:start", NULL);
753             repr->setAttribute("sodipodi:open", NULL);
754         }
755     }
757     // write d=
758     sp_arc_set_elliptical_path_attribute(arc, repr);
760     if (((SPObjectClass *) arc_parent_class)->write)
761         ((SPObjectClass *) arc_parent_class)->write(object, repr, flags);
763     return repr;
766 static void
767 sp_arc_set(SPObject *object, unsigned int key, gchar const *value)
769     SPGenericEllipse *ge = SP_GENERICELLIPSE(object);
771     switch (key) {
772         case SP_ATTR_SODIPODI_CX:
773             ge->cx.readOrUnset(value);
774             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
775             break;
776         case SP_ATTR_SODIPODI_CY:
777             ge->cy.readOrUnset(value);
778             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
779             break;
780         case SP_ATTR_SODIPODI_RX:
781             if (!ge->rx.read(value) || ge->rx.computed <= 0.0) {
782                 ge->rx.unset();
783             }
784             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
785             break;
786         case SP_ATTR_SODIPODI_RY:
787             if (!ge->ry.read(value) || ge->ry.computed <= 0.0) {
788                 ge->ry.unset();
789             }
790             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
791             break;
792         case SP_ATTR_SODIPODI_START:
793             if (value) {
794                 sp_svg_number_read_d(value, &ge->start);
795             } else {
796                 ge->start = 0;
797             }
798             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
799             break;
800         case SP_ATTR_SODIPODI_END:
801             if (value) {
802                 sp_svg_number_read_d(value, &ge->end);
803             } else {
804                 ge->end = 2 * M_PI;
805             }
806             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
807             break;
808         case SP_ATTR_SODIPODI_OPEN:
809             ge->closed = (!value);
810             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
811             break;
812         default:
813             if (((SPObjectClass *) arc_parent_class)->set)
814                 ((SPObjectClass *) arc_parent_class)->set(object, key, value);
815             break;
816     }
819 static void
820 sp_arc_modified(SPObject *object, guint flags)
822     if (flags & SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG) {
823         sp_shape_set_shape((SPShape *) object);
824     }
826     if (((SPObjectClass *) arc_parent_class)->modified)
827         ((SPObjectClass *) arc_parent_class)->modified(object, flags);
830 static gchar *sp_arc_description(SPItem *item)
832     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
834     gdouble len = fmod(ge->end - ge->start, SP_2PI);
835     if (len < 0.0) len += SP_2PI;
836     if (!(fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8)) {
837         if (ge->closed) {
838             return g_strdup(_("<b>Segment</b>"));
839         } else {
840             return g_strdup(_("<b>Arc</b>"));
841         }
842     } else {
843         return g_strdup(_("<b>Ellipse</b>"));
844     }
847 void
848 sp_arc_position_set(SPArc *arc, gdouble x, gdouble y, gdouble rx, gdouble ry)
850     g_return_if_fail(arc != NULL);
851     g_return_if_fail(SP_IS_ARC(arc));
853     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
855     ge->cx.computed = x;
856     ge->cy.computed = y;
857     ge->rx.computed = rx;
858     ge->ry.computed = ry;
859     if (prefs_get_double_attribute("tools.shapes.arc", "start", 0.0) != 0)
860         ge->start = prefs_get_double_attribute("tools.shapes.arc", "start", 0.0);
861     if (prefs_get_double_attribute("tools.shapes.arc", "end", 0.0) != 0)
862         ge->end = prefs_get_double_attribute("tools.shapes.arc", "end", 0.0);
863     if (!prefs_get_string_attribute("tools.shapes.arc", "open"))
864         ge->closed = 1;
865     else
866         ge->closed = 0;
868     ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
871 NR::Point sp_arc_get_xy(SPArc *arc, gdouble arg)
873     SPGenericEllipse *ge = SP_GENERICELLIPSE(arc);
875     return NR::Point(ge->rx.computed * cos(arg) + ge->cx.computed,
876                      ge->ry.computed * sin(arg) + ge->cy.computed);
880 /*
881   Local Variables:
882   mode:c++
883   c-file-style:"stroustrup"
884   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
885   indent-tabs-mode:nil
886   fill-column:99
887   End:
888 */
889 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :