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 <2geom/pathvector.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 * pathv may only contain paths with only straight line segments
91 * Return value: points attribute string.
92 */
93 static gchar *sp_svg_write_polygon(Geom::PathVector const & pathv)
94 {
95 Inkscape::SVGOStringStream os;
97 for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
98 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
99 if ( dynamic_cast<Geom::LineSegment const *>(&*cit) ||
100 dynamic_cast<Geom::HLineSegment const *>(&*cit) ||
101 dynamic_cast<Geom::VLineSegment const *>(&*cit) )
102 {
103 os << cit->finalPoint()[0] << "," << cit->finalPoint()[1] << " ";
104 } else {
105 g_error("sp_svg_write_polygon: polygon path contains non-straight line segments");
106 }
107 }
108 }
110 return g_strdup(os.str().c_str());
111 }
113 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
114 {
115 SPShape *shape = SP_SHAPE(object);
116 // Tolerable workaround: we need to update the object's curve before we set points=
117 // because it's out of sync when e.g. some extension attrs of the polygon or star are changed in XML editor
118 sp_shape_set_shape(shape);
120 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
121 repr = xml_doc->createElement("svg:polygon");
122 }
124 /* We can safely write points here, because all subclasses require it too (Lauris) */
125 gchar *str = sp_svg_write_polygon(shape->curve->get_pathvector());
126 repr->setAttribute("points", str);
127 g_free(str);
129 if (((SPObjectClass *) (parent_class))->write) {
130 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
131 }
133 return repr;
134 }
137 static gboolean polygon_get_value(gchar const **p, gdouble *v)
138 {
139 while (**p != '\0' && (**p == ',' || **p == '\x20' || **p == '\x9' || **p == '\xD' || **p == '\xA')) {
140 (*p)++;
141 }
143 if (*p == '\0') {
144 return false;
145 }
147 gchar *e = NULL;
148 *v = g_ascii_strtod(*p, &e);
149 if (e == *p) {
150 return false;
151 }
153 *p = e;
154 return true;
155 }
158 void sp_polygon_set(SPObject *object, unsigned int key, const gchar *value)
159 {
160 SPPolygon *polygon = SP_POLYGON(object);
162 switch (key) {
163 case SP_ATTR_POINTS: {
164 if (!value) {
165 /* fixme: The points attribute is required. We should handle its absence as per
166 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
167 break;
168 }
169 SPCurve *curve = new SPCurve();
170 gboolean hascpt = FALSE;
172 gchar const *cptr = value;
173 bool has_error = false;
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 /* fixme: It is an error for an odd number of points to be specified. We
184 * should display the points up to now (as we currently do, though perhaps
185 * without the closepath: the spec isn't quite clear on whether to do a
186 * closepath or not, though I'd guess it's best not to do a closepath), but
187 * then flag the document as in error, as per
188 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing.
189 *
190 * (Ref: http://www.w3.org/TR/SVG11/shapes.html#PolygonElement.) */
191 has_error = true;
192 break;
193 }
195 if (hascpt) {
196 curve->lineto(x, y);
197 } else {
198 curve->moveto(x, y);
199 hascpt = TRUE;
200 }
201 }
203 if (has_error || *cptr != '\0') {
204 /* TODO: Flag the document as in error, as per
205 * http://www.w3.org/TR/SVG11/implnote.html#ErrorProcessing. */
206 } else if (hascpt) {
207 /* We might have done a moveto but no lineto. I'm not sure how we're supposed to represent
208 * a single-point polygon in SPCurve. TODO: add a testcase with only one coordinate pair */
209 curve->closepath();
210 }
211 sp_shape_set_curve(SP_SHAPE(polygon), curve, TRUE);
212 curve->unref();
213 break;
214 }
215 default:
216 if (((SPObjectClass *) parent_class)->set) {
217 ((SPObjectClass *) parent_class)->set(object, key, value);
218 }
219 break;
220 }
221 }
223 static gchar *sp_polygon_description(SPItem */*item*/)
224 {
225 return g_strdup(_("<b>Polygon</b>"));
226 }
228 /*
229 Local Variables:
230 mode:c++
231 c-file-style:"stroustrup"
232 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
233 indent-tabs-mode:nil
234 fill-column:99
235 End:
236 */
237 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :