1 #define __SP_POLYLINE_C__
3 /*
4 * SVG <polyline> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 Lauris Kaplinski
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-polyline.h"
18 #include "display/curve.h"
19 #include <glibmm/i18n.h>
20 #include "xml/repr.h"
21 #include "document.h"
23 static void sp_polyline_class_init (SPPolyLineClass *klass);
24 static void sp_polyline_init (SPPolyLine *polyline);
26 static void sp_polyline_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
27 static void sp_polyline_set (SPObject *object, unsigned int key, const gchar *value);
28 static Inkscape::XML::Node *sp_polyline_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
30 static gchar * sp_polyline_description (SPItem * item);
32 static SPShapeClass *parent_class;
34 GType
35 sp_polyline_get_type (void)
36 {
37 static GType polyline_type = 0;
39 if (!polyline_type) {
40 GTypeInfo polyline_info = {
41 sizeof (SPPolyLineClass),
42 NULL, /* base_init */
43 NULL, /* base_finalize */
44 (GClassInitFunc) sp_polyline_class_init,
45 NULL, /* klass_finalize */
46 NULL, /* klass_data */
47 sizeof (SPPolyLine),
48 16, /* n_preallocs */
49 (GInstanceInitFunc) sp_polyline_init,
50 NULL, /* value_table */
51 };
52 polyline_type = g_type_register_static (SP_TYPE_SHAPE, "SPPolyLine", &polyline_info, (GTypeFlags)0);
53 }
54 return polyline_type;
55 }
57 static void
58 sp_polyline_class_init (SPPolyLineClass *klass)
59 {
60 GObjectClass * gobject_class;
61 SPObjectClass * sp_object_class;
62 SPItemClass * item_class;
64 gobject_class = (GObjectClass *) klass;
65 sp_object_class = (SPObjectClass *) klass;
66 item_class = (SPItemClass *) klass;
68 parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
70 sp_object_class->build = sp_polyline_build;
71 sp_object_class->set = sp_polyline_set;
72 sp_object_class->write = sp_polyline_write;
74 item_class->description = sp_polyline_description;
75 }
77 static void
78 sp_polyline_init (SPPolyLine * /*polyline*/)
79 {
80 /* Nothing here */
81 }
83 static void
84 sp_polyline_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
85 {
87 if (((SPObjectClass *) parent_class)->build)
88 ((SPObjectClass *) parent_class)->build (object, document, repr);
90 sp_object_read_attr (object, "points");
91 }
93 static void
94 sp_polyline_set (SPObject *object, unsigned int key, const gchar *value)
95 {
96 SPPolyLine *polyline;
98 polyline = SP_POLYLINE (object);
100 switch (key) {
101 case SP_ATTR_POINTS: {
102 SPCurve * curve;
103 const gchar * cptr;
104 char * eptr;
105 gboolean hascpt;
107 if (!value) break;
108 curve = new SPCurve ();
109 hascpt = FALSE;
111 cptr = value;
112 eptr = NULL;
114 while (TRUE) {
115 gdouble x, y;
117 while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
118 cptr++;
119 }
120 if (!*cptr) break;
122 x = g_ascii_strtod (cptr, &eptr);
123 if (eptr == cptr) break;
124 cptr = eptr;
126 while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
127 cptr++;
128 }
129 if (!*cptr) break;
131 y = g_ascii_strtod (cptr, &eptr);
132 if (eptr == cptr) break;
133 cptr = eptr;
134 if (hascpt) {
135 curve->lineto(x, y);
136 } else {
137 curve->moveto(x, y);
138 hascpt = TRUE;
139 }
140 }
142 sp_shape_set_curve (SP_SHAPE (polyline), curve, TRUE);
143 curve->unref();
144 break;
145 }
146 default:
147 if (((SPObjectClass *) parent_class)->set)
148 ((SPObjectClass *) parent_class)->set (object, key, value);
149 break;
150 }
151 }
153 static Inkscape::XML::Node *
154 sp_polyline_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
155 {
156 SPPolyLine *polyline;
158 polyline = SP_POLYLINE (object);
160 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
161 repr = xml_doc->createElement("svg:polyline");
162 }
164 if (repr != SP_OBJECT_REPR (object)) {
165 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
166 }
168 if (((SPObjectClass *) (parent_class))->write)
169 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
171 return repr;
172 }
174 static gchar *
175 sp_polyline_description(SPItem */*item*/)
176 {
177 return g_strdup(_("<b>Polyline</b>"));
178 }