Code

struct SPCurve => class SPCurve
[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"
24 #include "document.h"
26 static void sp_polygon_class_init(SPPolygonClass *pc);
27 static void sp_polygon_init(SPPolygon *polygon);
29 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
30 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
31 static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value);
33 static gchar *sp_polygon_description(SPItem *item);
35 static SPShapeClass *parent_class;
37 GType sp_polygon_get_type(void)
38 {
39     static GType polygon_type = 0;
41     if (!polygon_type) {
42         GTypeInfo polygon_info = {
43             sizeof(SPPolygonClass),
44             NULL, NULL,
45             (GClassInitFunc) sp_polygon_class_init,
46             NULL, NULL,
47             sizeof(SPPolygon),
48             16,
49             (GInstanceInitFunc) sp_polygon_init,
50             NULL,   /* value_table */
51         };
52         polygon_type = g_type_register_static(SP_TYPE_SHAPE, "SPPolygon", &polygon_info, (GTypeFlags) 0);
53     }
55     return polygon_type;
56 }
58 static void sp_polygon_class_init(SPPolygonClass *pc)
59 {
60     SPObjectClass *sp_object_class = (SPObjectClass *) pc;
61     SPItemClass *item_class = (SPItemClass *) pc;
63     parent_class = (SPShapeClass *) g_type_class_ref(SP_TYPE_SHAPE);
65     sp_object_class->build = sp_polygon_build;
66     sp_object_class->write = sp_polygon_write;
67     sp_object_class->set = sp_polygon_set;
69     item_class->description = sp_polygon_description;
70 }
72 static void sp_polygon_init(SPPolygon */*polygon*/)
73 {
74     /* Nothing here */
75 }
77 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
78 {
79     if (((SPObjectClass *) parent_class)->build) {
80         ((SPObjectClass *) parent_class)->build(object, document, repr);
81     }
83     sp_object_read_attr(object, "points");
84 }
87 /*
88  * sp_svg_write_polygon: Write points attribute for polygon tag.
89  * @bpath:
90  *
91  * Return value: points attribute string.
92  */
93 static gchar *sp_svg_write_polygon(const NArtBpath *bpath)
94 {
95     g_return_val_if_fail(bpath != NULL, NULL);
97     Inkscape::SVGOStringStream os;
99     for (int i = 0; bpath[i].code != NR_END; i++) {
100         switch (bpath [i].code) {
101             case NR_LINETO:
102             case NR_MOVETO:
103             case NR_MOVETO_OPEN:
104                 os << bpath [i].x3 << "," << bpath [i].y3 << " ";
105                 break;
107             case NR_CURVETO:
108             default:
109                 g_assert_not_reached();
110         }
111     }
113     return g_strdup(os.str().c_str());
116 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
118     SPShape *shape = SP_SHAPE(object);
119     // Tolerable workaround: we need to update the object's curve before we set points=
120     // because it's out of sync when e.g. some extension attrs of the polygon or star are changed in XML editor
121     sp_shape_set_shape(shape);
123     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
124         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
125         repr = xml_doc->createElement("svg:polygon");
126     }
128     /* We can safely write points here, because all subclasses require it too (Lauris) */
129     NArtBpath *abp = shape->curve->first_bpath();
130     gchar *str = sp_svg_write_polygon(abp);
131     repr->setAttribute("points", str);
132     g_free(str);
134     if (((SPObjectClass *) (parent_class))->write) {
135         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
136     }
138     return repr;
142 static gboolean polygon_get_value(gchar const **p, gdouble *v)
144     while (**p != '\0' && (**p == ',' || **p == '\x20' || **p == '\x9' || **p == '\xD' || **p == '\xA')) {
145         (*p)++;
146     }
148     if (*p == '\0') {
149         return false;
150     }
152     gchar *e = NULL;
153     *v = g_ascii_strtod(*p, &e);
154     if (e == *p) {
155         return false;
156     }
158     *p = e;
159     return true;
163 static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
165     SPPolygon *polygon = SP_POLYGON(object);
167     switch (key) {
168         case SP_ATTR_POINTS: {
169             if (!value) {
170                 /* fixme: The points attribute is required.  We should handle its absence as per
171                  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
172                 break;
173             }
174             SPCurve *curve = new SPCurve();
175             gboolean hascpt = FALSE;
177             gchar const *cptr = value;
178             bool has_error = false;
180             while (TRUE) {
181                 gdouble x;
182                 if (!polygon_get_value(&cptr, &x)) {
183                     break;
184                 }
186                 gdouble y;
187                 if (!polygon_get_value(&cptr, &y)) {
188                     /* fixme: It is an error for an odd number of points to be specified.  We
189                      * should display the points up to now (as we currently do, though perhaps
190                      * without the closepath: the spec isn't quite clear on whether to do a
191                      * closepath or not, though I'd guess it's best not to do a closepath), but
192                      * then flag the document as in error, as per
193                      * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
194                      *
195                      * (Ref: http://www.w3.org/TR/SVG11/shapes.html#PolygonElement.) */
196                     has_error = true;
197                     break;
198                 }
200                 if (hascpt) {
201                     curve->lineto(x, y);
202                 } else {
203                     curve->moveto(x, y);
204                     hascpt = TRUE;
205                 }
206             }
208             if (has_error || *cptr != '\0') {
209                 /* TODO: Flag the document as in error, as per
210                  * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
211             } else if (curve->posSet) {
212                 /* We've done a moveto but no lineto.  I'm not sure how we're supposed to represent
213                  * a single-point polygon in SPCurve: sp_curve_closepath at the time of writing
214                  * doesn't seem to like simply moveto followed by closepath.  The following works,
215                  * but won't round-trip properly: I believe it will write as two points rather than
216                  * one. */
217                 curve->lineto(curve->movePos);
218             } else if (hascpt) {
219                 curve->closepath();
220             }
221             sp_shape_set_curve(SP_SHAPE(polygon), curve, TRUE);
222             curve->unref();
223             break;
224         }
225         default:
226             if (((SPObjectClass *) parent_class)->set) {
227                 ((SPObjectClass *) parent_class)->set(object, key, value);
228             }
229             break;
230     }
233 static gchar *sp_polygon_description(SPItem */*item*/)
235     return g_strdup(_("<b>Polygon</b>"));
238 /*
239   Local Variables:
240   mode:c++
241   c-file-style:"stroustrup"
242   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
243   indent-tabs-mode:nil
244   fill-column:99
245   End:
246 */
247 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :