1 /*
2 * SVG <polygon> implementation
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * Abhishek Sharma
7 *
8 * Copyright (C) 1999-2002 Lauris Kaplinski
9 * Copyright (C) 2000-2001 Ximian, Inc.
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "config.h"
16 #include "attributes.h"
17 #include "sp-polygon.h"
18 #include "display/curve.h"
19 #include <glibmm/i18n.h>
20 #include <2geom/pathvector.h>
21 #include <2geom/bezier-curve.h>
22 #include <2geom/hvlinesegment.h>
23 #include "helper/geom-curves.h"
24 #include "svg/stringstream.h"
25 #include "xml/repr.h"
26 #include "document.h"
28 static void sp_polygon_class_init(SPPolygonClass *pc);
29 static void sp_polygon_init(SPPolygon *polygon);
31 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static Inkscape::XML::Node *sp_polygon_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
34 static gchar *sp_polygon_description(SPItem *item);
36 static SPShapeClass *parent_class;
38 GType sp_polygon_get_type(void)
39 {
40 static GType type = 0;
42 if (!type) {
43 GTypeInfo info = {
44 sizeof(SPPolygonClass),
45 0, // base_init
46 0, // base_finalize
47 (GClassInitFunc)sp_polygon_class_init,
48 0, // class_finalize
49 0, // class_data
50 sizeof(SPPolygon),
51 0, // n_preallocs
52 (GInstanceInitFunc)sp_polygon_init,
53 0 // value_table
54 };
55 type = g_type_register_static(SP_TYPE_SHAPE, "SPPolygon", &info, static_cast<GTypeFlags>(0));
56 }
58 return type;
59 }
61 static void sp_polygon_class_init(SPPolygonClass *pc)
62 {
63 SPObjectClass *sp_object_class = (SPObjectClass *) pc;
64 SPItemClass *item_class = (SPItemClass *) pc;
66 parent_class = (SPShapeClass *) g_type_class_ref(SP_TYPE_SHAPE);
68 sp_object_class->build = sp_polygon_build;
69 sp_object_class->write = sp_polygon_write;
70 sp_object_class->set = sp_polygon_set;
72 item_class->description = sp_polygon_description;
73 }
75 static void sp_polygon_init(SPPolygon */*polygon*/)
76 {
77 /* Nothing here */
78 }
80 static void sp_polygon_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
81 {
82 if (((SPObjectClass *) parent_class)->build) {
83 ((SPObjectClass *) parent_class)->build(object, document, repr);
84 }
86 object->readAttr( "points" );
87 }
90 /*
91 * sp_svg_write_polygon: Write points attribute for polygon tag.
92 * pathv may only contain paths with only straight line segments
93 * Return value: points attribute string.
94 */
95 static gchar *sp_svg_write_polygon(Geom::PathVector const & pathv)
96 {
97 Inkscape::SVGOStringStream os;
99 for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
100 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_default(); ++cit) {
101 if ( is_straight_curve(*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 shape->setShape();
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(polygon))->setCurve(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 :