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());
113 }
115 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
116 {
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;
137 }
140 static gboolean polygon_get_value(gchar const **p, gdouble *v)
141 {
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;
158 }
161 static void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
162 {
163 SPPolygon *polygon = SP_POLYGON(object);
165 switch (key) {
166 case SP_ATTR_POINTS: {
167 if (!value) {
168 /* fixme: The points attribute is required. We should handle its absence as per
169 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
170 break;
171 }
172 SPCurve *curve = sp_curve_new();
173 gboolean hascpt = FALSE;
175 gchar const *cptr = value;
176 bool has_error = false;
178 while (TRUE) {
179 gdouble x;
180 if (!polygon_get_value(&cptr, &x)) {
181 break;
182 }
184 gdouble y;
185 if (!polygon_get_value(&cptr, &y)) {
186 /* fixme: It is an error for an odd number of points to be specified. We
187 * should display the points up to now (as we currently do, though perhaps
188 * without the closepath: the spec isn't quite clear on whether to do a
189 * closepath or not, though I'd guess it's best not to do a closepath), but
190 * then flag the document as in error, as per
191 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
192 *
193 * (Ref: http://www.w3.org/TR/SVG11/shapes.html#PolygonElement.) */
194 has_error = true;
195 break;
196 }
198 if (hascpt) {
199 sp_curve_lineto(curve, x, y);
200 } else {
201 sp_curve_moveto(curve, x, y);
202 hascpt = TRUE;
203 }
204 }
206 if (has_error || *cptr != '\0') {
207 /* TODO: Flag the document as in error, as per
208 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
209 } else if (curve->posSet) {
210 /* We've done a moveto but no lineto. I'm not sure how we're supposed to represent
211 * a single-point polygon in SPCurve: sp_curve_closepath at the time of writing
212 * doesn't seem to like simply moveto followed by closepath. The following works,
213 * but won't round-trip properly: I believe it will write as two points rather than
214 * one. */
215 sp_curve_lineto(curve, curve->movePos);
216 } else if (hascpt) {
217 sp_curve_closepath(curve);
218 }
219 sp_shape_set_curve(SP_SHAPE(polygon), curve, TRUE);
220 sp_curve_unref(curve);
221 break;
222 }
223 default:
224 if (((SPObjectClass *) parent_class)->set) {
225 ((SPObjectClass *) parent_class)->set(object, key, value);
226 }
227 break;
228 }
229 }
231 static gchar *sp_polygon_description(SPItem *item)
232 {
233 return g_strdup(_("<b>Polygon</b>"));
234 }
236 /*
237 Local Variables:
238 mode:c++
239 c-file-style:"stroustrup"
240 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
241 indent-tabs-mode:nil
242 fill-column:99
243 End:
244 */
245 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :