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::Document *doc, Inkscape::XML::Node *repr, guint flags);
32 static gchar *sp_polygon_description(SPItem *item);
34 static SPShapeClass *parent_class;
36 GType sp_polygon_get_type(void)
37 {
38 static GType type = 0;
40 if (!type) {
41 GTypeInfo info = {
42 sizeof(SPPolygonClass),
43 0, // base_init
44 0, // base_finalize
45 (GClassInitFunc)sp_polygon_class_init,
46 0, // class_finalize
47 0, // class_data
48 sizeof(SPPolygon),
49 0, // n_preallocs
50 (GInstanceInitFunc)sp_polygon_init,
51 0 // value_table
52 };
53 type = g_type_register_static(SP_TYPE_SHAPE, "SPPolygon", &info, static_cast<GTypeFlags>(0));
54 }
56 return type;
57 }
59 static void sp_polygon_class_init(SPPolygonClass *pc)
60 {
61 SPObjectClass *sp_object_class = (SPObjectClass *) pc;
62 SPItemClass *item_class = (SPItemClass *) pc;
64 parent_class = (SPShapeClass *) g_type_class_ref(SP_TYPE_SHAPE);
66 sp_object_class->build = sp_polygon_build;
67 sp_object_class->write = sp_polygon_write;
68 sp_object_class->set = sp_polygon_set;
70 item_class->description = sp_polygon_description;
71 }
73 static void sp_polygon_init(SPPolygon */*polygon*/)
74 {
75 /* Nothing here */
76 }
78 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
79 {
80 if (((SPObjectClass *) parent_class)->build) {
81 ((SPObjectClass *) parent_class)->build(object, document, repr);
82 }
84 sp_object_read_attr(object, "points");
85 }
88 /*
89 * sp_svg_write_polygon: Write points attribute for polygon tag.
90 * @bpath:
91 *
92 * Return value: points attribute string.
93 */
94 static gchar *sp_svg_write_polygon(const NArtBpath *bpath)
95 {
96 g_return_val_if_fail(bpath != NULL, NULL);
98 Inkscape::SVGOStringStream os;
100 for (int i = 0; bpath[i].code != NR_END; i++) {
101 switch (bpath [i].code) {
102 case NR_LINETO:
103 case NR_MOVETO:
104 case NR_MOVETO_OPEN:
105 os << bpath [i].x3 << "," << bpath [i].y3 << " ";
106 break;
108 case NR_CURVETO:
109 default:
110 g_assert_not_reached();
111 }
112 }
114 return g_strdup(os.str().c_str());
115 }
117 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
118 {
119 SPShape *shape = SP_SHAPE(object);
120 // Tolerable workaround: we need to update the object's curve before we set points=
121 // because it's out of sync when e.g. some extension attrs of the polygon or star are changed in XML editor
122 sp_shape_set_shape(shape);
124 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
125 repr = xml_doc->createElement("svg:polygon");
126 }
128 /* We can safely write points here, because all subclasses require it too (Lauris) */
129 NArtBpath const * abp = shape->curve->get_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, xml_doc, repr, flags);
136 }
138 return repr;
139 }
142 static gboolean polygon_get_value(gchar const **p, gdouble *v)
143 {
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;
160 }
163 void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
164 {
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 }
231 }
233 static gchar *sp_polygon_description(SPItem */*item*/)
234 {
235 return g_strdup(_("<b>Polygon</b>"));
236 }
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 :