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