Code

fix percentage coordinates for rect and line. (svgtestsuite-1.1/coords-units-02-b...
[inkscape.git] / src / sp-line.cpp
1 #define __SP_LINE_C__
3 /*
4  * SVG <line> 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 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include "attributes.h"
18 #include "style.h"
19 #include "sp-line.h"
20 #include "display/curve.h"
21 #include <glibmm/i18n.h>
22 #include <libnr/nr-matrix-fns.h>
23 #include <xml/repr.h>
24 #include "document.h"
26 static void sp_line_class_init (SPLineClass *klass);
27 static void sp_line_init (SPLine *line);
29 static void sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
30 static void sp_line_set (SPObject *object, unsigned int key, const gchar *value);
31 static Inkscape::XML::Node *sp_line_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
33 static gchar *sp_line_description (SPItem * item);
34 static Geom::Matrix sp_line_set_transform(SPItem *item, Geom::Matrix const &xform);
36 static void sp_line_update (SPObject *object, SPCtx *ctx, guint flags);
37 static void sp_line_set_shape (SPShape *shape);
39 static SPShapeClass *parent_class;
41 GType
42 sp_line_get_type (void)
43 {
44         static GType line_type = 0;
46         if (!line_type) {
47                 GTypeInfo line_info = {
48                         sizeof (SPLineClass),
49                         NULL,   /* base_init */
50                         NULL,   /* base_finalize */
51                         (GClassInitFunc) sp_line_class_init,
52                         NULL,   /* klass_finalize */
53                         NULL,   /* klass_data */
54                         sizeof (SPLine),
55                         16,     /* n_preallocs */
56                         (GInstanceInitFunc) sp_line_init,
57                         NULL,   /* value_table */
58                 };
59                 line_type = g_type_register_static (SP_TYPE_SHAPE, "SPLine", &line_info, (GTypeFlags)0);
60         }
61         return line_type;
62 }
64 static void
65 sp_line_class_init (SPLineClass *klass)
66 {
67         parent_class = (SPShapeClass *) g_type_class_ref (SP_TYPE_SHAPE);
69         SPObjectClass *sp_object_class = (SPObjectClass *) klass;
70         sp_object_class->build = sp_line_build;
71         sp_object_class->set = sp_line_set;
72         sp_object_class->write = sp_line_write;
74         SPItemClass *item_class = (SPItemClass *) klass;
75         item_class->description = sp_line_description;
76         item_class->set_transform = sp_line_set_transform;
78         sp_object_class->update = sp_line_update;
80         SPShapeClass *shape_class = (SPShapeClass *) klass;
81         shape_class->set_shape = sp_line_set_shape;
82 }
84 static void
85 sp_line_init (SPLine * line)
86 {
87         line->x1.unset();
88         line->y1.unset();
89         line->x2.unset();
90         line->y2.unset();
91 }
94 static void
95 sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
96 {
97         if (((SPObjectClass *) parent_class)->build) {
98                 ((SPObjectClass *) parent_class)->build (object, document, repr);
99         }
101         sp_object_read_attr (object, "x1");
102         sp_object_read_attr (object, "y1");
103         sp_object_read_attr (object, "x2");
104         sp_object_read_attr (object, "y2");
107 static void
108 sp_line_set (SPObject *object, unsigned int key, const gchar *value)
110         SPLine * line = SP_LINE (object);
112         /* fixme: we should really collect updates */
114         switch (key) {
115         case SP_ATTR_X1:
116                 line->x1.readOrUnset(value);
117                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
118                 break;
119         case SP_ATTR_Y1:
120                 line->y1.readOrUnset(value);
121                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
122                 break;
123         case SP_ATTR_X2:
124                 line->x2.readOrUnset(value);
125                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
126                 break;
127         case SP_ATTR_Y2:
128                 line->y2.readOrUnset(value);
129                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
130                 break;
131         default:
132                 if (((SPObjectClass *) parent_class)->set)
133                         ((SPObjectClass *) parent_class)->set (object, key, value);
134                 break;
135         }
138 static void
139 sp_line_update (SPObject *object, SPCtx *ctx, guint flags)
141         if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
142                 SPLine *line = SP_LINE (object);
144                 SPStyle const *style = object->style;
145         SPItemCtx const *ictx = (SPItemCtx const *) ctx;
146         double const w = (ictx->vp.x1 - ictx->vp.x0);
147         double const h = (ictx->vp.y1 - ictx->vp.y0);
148                 double const em = style->font_size.computed;
149                 double const ex = em * 0.5;  // fixme: get from pango or libnrtype.
150                 line->x1.update(em, ex, w);
151                 line->x2.update(em, ex, w);
152                 line->y1.update(em, ex, h);
153                 line->y2.update(em, ex, h);
155                 sp_shape_set_shape ((SPShape *) object);
156         }
158         if (((SPObjectClass *) parent_class)->update)
159                 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
163 static Inkscape::XML::Node *
164 sp_line_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
166         SPLine *line  = SP_LINE (object);
168         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
169                 repr = xml_doc->createElement("svg:line");
170         }
172         if (repr != SP_OBJECT_REPR (object)) {
173                 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
174         }
176         sp_repr_set_svg_double(repr, "x1", line->x1.computed);
177         sp_repr_set_svg_double(repr, "y1", line->y1.computed);
178         sp_repr_set_svg_double(repr, "x2", line->x2.computed);
179         sp_repr_set_svg_double(repr, "y2", line->y2.computed);
181         if (((SPObjectClass *) (parent_class))->write)
182                 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
184         return repr;
187 static gchar *
188 sp_line_description(SPItem */*item*/)
190     return g_strdup(_("<b>Line</b>"));
193 static Geom::Matrix
194 sp_line_set_transform (SPItem *item, Geom::Matrix const &xform)
196         SPLine *line = SP_LINE (item);
197         Geom::Point points[2];
199         points[0] = Geom::Point(line->x1.computed, line->y1.computed);
200         points[1] = Geom::Point(line->x2.computed, line->y2.computed);
202         points[0] *= xform;
203         points[1] *= xform;
205         line->x1.computed = points[0][Geom::X];
206         line->y1.computed = points[0][Geom::Y];
207         line->x2.computed = points[1][Geom::X];
208         line->y2.computed = points[1][Geom::Y];
210         sp_item_adjust_stroke(item, xform.descrim());
212         SP_OBJECT (item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
214         return Geom::identity();
217 static void
218 sp_line_set_shape (SPShape *shape)
220         SPLine *line = SP_LINE (shape);
222         SPCurve *c = new SPCurve ();
224         c->moveto(line->x1.computed, line->y1.computed);
225         c->lineto(line->x2.computed, line->y2.computed);
227         sp_shape_set_curve_insync (shape, c, TRUE); // *_insync does not call update, avoiding infinite recursion when set_shape is called by update
229         c->unref();