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"
22 static void sp_polyline_class_init (SPPolyLineClass *klass);
23 static void sp_polyline_init (SPPolyLine *polyline);
25 static void sp_polyline_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
26 static void sp_polyline_set (SPObject *object, unsigned int key, const gchar *value);
27 static Inkscape::XML::Node *sp_polyline_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
29 static gchar * sp_polyline_description (SPItem * item);
31 static SPShapeClass *parent_class;
33 GType
34 sp_polyline_get_type (void)
35 {
36 static GType polyline_type = 0;
38 if (!polyline_type) {
39 GTypeInfo polyline_info = {
40 sizeof (SPPolyLineClass),
41 NULL, /* base_init */
42 NULL, /* base_finalize */
43 (GClassInitFunc) sp_polyline_class_init,
44 NULL, /* klass_finalize */
45 NULL, /* klass_data */
46 sizeof (SPPolyLine),
47 16, /* n_preallocs */
48 (GInstanceInitFunc) sp_polyline_init,
49 NULL, /* value_table */
50 };
51 polyline_type = g_type_register_static (SP_TYPE_SHAPE, "SPPolyLine", &polyline_info, (GTypeFlags)0);
52 }
53 return polyline_type;
54 }
56 static void
57 sp_polyline_class_init (SPPolyLineClass *klass)
58 {
59 GObjectClass * gobject_class;
60 SPObjectClass * sp_object_class;
61 SPItemClass * item_class;
63 gobject_class = (GObjectClass *) klass;
64 sp_object_class = (SPObjectClass *) klass;
65 item_class = (SPItemClass *) klass;
67 parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
69 sp_object_class->build = sp_polyline_build;
70 sp_object_class->set = sp_polyline_set;
71 sp_object_class->write = sp_polyline_write;
73 item_class->description = sp_polyline_description;
74 }
76 static void
77 sp_polyline_init (SPPolyLine * polyline)
78 {
79 /* Nothing here */
80 }
82 static void
83 sp_polyline_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
84 {
86 if (((SPObjectClass *) parent_class)->build)
87 ((SPObjectClass *) parent_class)->build (object, document, repr);
89 sp_object_read_attr (object, "points");
90 }
92 static void
93 sp_polyline_set (SPObject *object, unsigned int key, const gchar *value)
94 {
95 SPPolyLine *polyline;
97 polyline = SP_POLYLINE (object);
99 switch (key) {
100 case SP_ATTR_POINTS: {
101 SPCurve * curve;
102 const gchar * cptr;
103 char * eptr;
104 gboolean hascpt;
106 if (!value) break;
107 curve = sp_curve_new ();
108 hascpt = FALSE;
110 cptr = value;
111 eptr = NULL;
113 while (TRUE) {
114 gdouble x, y;
116 while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
117 cptr++;
118 }
119 if (!*cptr) break;
121 x = g_ascii_strtod (cptr, &eptr);
122 if (eptr == cptr) break;
123 cptr = eptr;
125 while (*cptr != '\0' && (*cptr == ',' || *cptr == '\x20' || *cptr == '\x9' || *cptr == '\xD' || *cptr == '\xA')) {
126 cptr++;
127 }
128 if (!*cptr) break;
130 y = g_ascii_strtod (cptr, &eptr);
131 if (eptr == cptr) break;
132 cptr = eptr;
133 if (hascpt) {
134 sp_curve_lineto (curve, x, y);
135 } else {
136 sp_curve_moveto (curve, x, y);
137 hascpt = TRUE;
138 }
139 }
141 sp_shape_set_curve (SP_SHAPE (polyline), curve, TRUE);
142 sp_curve_unref (curve);
143 break;
144 }
145 default:
146 if (((SPObjectClass *) parent_class)->set)
147 ((SPObjectClass *) parent_class)->set (object, key, value);
148 break;
149 }
150 }
152 static Inkscape::XML::Node *
153 sp_polyline_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
154 {
155 SPPolyLine *polyline;
157 polyline = SP_POLYLINE (object);
159 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
160 repr = sp_repr_new ("svg:polyline");
161 }
163 if (repr != SP_OBJECT_REPR (object)) {
164 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
165 }
167 if (((SPObjectClass *) (parent_class))->write)
168 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
170 return repr;
171 }
173 static gchar *
174 sp_polyline_description(SPItem *item)
175 {
176 return g_strdup(_("<b>Polyline</b>"));
177 }