Code

moving trunk for module inkscape
[inkscape.git] / src / sp-polygon.cpp
1 #define __SP_POLYGON_C__
3 /*
4  * SVG <polygon> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 1999-2002 Lauris Kaplinski
10  * Copyright (C) 2000-2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include "config.h"
17 #include "attributes.h"
18 #include "sp-polygon.h"
19 #include "display/curve.h"
20 #include <glibmm/i18n.h>
21 #include "libnr/n-art-bpath.h"
22 #include "svg/stringstream.h"
23 #include "xml/repr.h"
25 static void sp_polygon_class_init(SPPolygonClass *pc);
26 static void sp_polygon_init(SPPolygon *polygon);
28 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
29 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
30 static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value);
32 static gchar *sp_polygon_description(SPItem *item);
34 static SPShapeClass *parent_class;
36 GType sp_polygon_get_type(void)
37 {
38     static GType polygon_type = 0;
40     if (!polygon_type) {
41         GTypeInfo polygon_info = {
42             sizeof(SPPolygonClass),
43             NULL, NULL,
44             (GClassInitFunc) sp_polygon_class_init,
45             NULL, NULL,
46             sizeof(SPPolygon),
47             16,
48             (GInstanceInitFunc) sp_polygon_init,
49             NULL,   /* value_table */
50         };
51         polygon_type = g_type_register_static(SP_TYPE_SHAPE, "SPPolygon", &polygon_info, (GTypeFlags) 0);
52     }
54     return polygon_type;
55 }
57 static void sp_polygon_class_init(SPPolygonClass *pc)
58 {
59     SPObjectClass *sp_object_class = (SPObjectClass *) pc;
60     SPItemClass *item_class = (SPItemClass *) pc;
62     parent_class = (SPShapeClass *) g_type_class_ref(SP_TYPE_SHAPE);
64     sp_object_class->build = sp_polygon_build;
65     sp_object_class->write = sp_polygon_write;
66     sp_object_class->set = sp_polygon_set;
68     item_class->description = sp_polygon_description;
69 }
71 static void sp_polygon_init(SPPolygon *polygon)
72 {
73     /* Nothing here */
74 }
76 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
77 {
78     if (((SPObjectClass *) parent_class)->build) {
79         ((SPObjectClass *) parent_class)->build(object, document, repr);
80     }
82     sp_object_read_attr(object, "points");
83 }
86 /*
87  * sp_svg_write_polygon: Write points attribute for polygon tag.
88  * @bpath:
89  *
90  * Return value: points attribute string.
91  */
92 static gchar *sp_svg_write_polygon(const NArtBpath *bpath)
93 {
94     g_return_val_if_fail(bpath != NULL, NULL);
96     Inkscape::SVGOStringStream os;
98     for (int i = 0; bpath[i].code != NR_END; i++) {
99         switch (bpath [i].code) {
100             case NR_LINETO:
101             case NR_MOVETO:
102             case NR_MOVETO_OPEN:
103                 os << bpath [i].x3 << "," << bpath [i].y3 << " ";
104                 break;
106             case NR_CURVETO:
107             default:
108                 g_assert_not_reached();
109         }
110     }
112     return g_strdup(os.str().c_str());
115 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
117     SPShape *shape = SP_SHAPE(object);
118     // Tolerable workaround: we need to update the object's curve before we set points=
119     // because it's out of sync when e.g. some extension attrs of the polygon or star are changed in XML editor
120     sp_shape_set_shape(shape);
122     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
123         repr = sp_repr_new("svg:polygon");
124     }
126     /* We can safely write points here, because all subclasses require it too (Lauris) */
127     NArtBpath *abp = sp_curve_first_bpath(shape->curve);
128     gchar *str = sp_svg_write_polygon(abp);
129     repr->setAttribute("points", str);
130     g_free(str);
132     if (((SPObjectClass *) (parent_class))->write) {
133         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
134     }
136     return repr;
140 static gboolean polygon_get_value(gchar const **p, gdouble *v)
142     while (**p != '\0' && (**p == ',' || **p == '\x20' || **p == '\x9' || **p == '\xD' || **p == '\xA')) {
143         (*p)++;
144     }
146     if (*p == '\0') {
147         return false;
148     }
150     gchar *e = NULL;
151     *v = g_ascii_strtod(*p, &e);
152     if (e == *p) {
153         return false;
154     }
156     *p = e;
157     return true;
161 static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
163     SPPolygon *polygon = SP_POLYGON(object);
165     switch (key) {
166         case SP_ATTR_POINTS: {
167             if (!value) {
168                 break;
169             }
170             SPCurve *curve = sp_curve_new();
171             gboolean hascpt = FALSE;
173             const gchar *cptr = value;
175             while (TRUE) {
176                 gdouble x;
177                 if (!polygon_get_value(&cptr, &x)) {
178                     break;
179                 }
181                 gdouble y;
182                 if (!polygon_get_value(&cptr, &y)) {
183                     break;
184                 }
186                 if (hascpt) {
187                     sp_curve_lineto(curve, x, y);
188                 } else {
189                     sp_curve_moveto(curve, x, y);
190                     hascpt = TRUE;
191                 }
192             }
194             /* TODO: if *cptr != '\0' or if the break came after parsing an x without a y then
195              * there's an error, which should be handled according to
196              * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
198             sp_curve_closepath(curve);
199             sp_shape_set_curve(SP_SHAPE(polygon), curve, TRUE);
200             sp_curve_unref(curve);
201             break;
202         }
203         default:
204             if (((SPObjectClass *) parent_class)->set) {
205                 ((SPObjectClass *) parent_class)->set(object, key, value);
206             }
207             break;
208     }
211 static gchar *sp_polygon_description(SPItem *item)
213     return g_strdup(_("<b>Polygon</b>"));
216 /*
217   Local Variables:
218   mode:c++
219   c-file-style:"stroustrup"
220   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
221   indent-tabs-mode:nil
222   fill-column:99
223   End:
224 */
225 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :