4
* SVG <line> implementation
7
* Lauris Kaplinski <lauris@kaplinski.com>
9
* Copyright (C) 1999-2002 Lauris Kaplinski
11
* Released under GNU GPL, read the file 'COPYING' for more information
17
#include "attributes.h"
20
#include "display/curve.h"
21
#include <glibmm/i18n.h>
22
#include <libnr/nr-matrix-fns.h>
25
static void sp_line_class_init (SPLineClass *klass);
26
static void sp_line_init (SPLine *line);
28
static void sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
29
static void sp_line_set (SPObject *object, unsigned int key, const gchar *value);
30
static Inkscape::XML::Node *sp_line_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
32
static gchar *sp_line_description (SPItem * item);
33
static NR::Matrix sp_line_set_transform(SPItem *item, NR::Matrix const &xform);
35
static void sp_line_update (SPObject *object, SPCtx *ctx, guint flags);
36
static void sp_line_set_shape (SPShape *shape);
38
static SPShapeClass *parent_class;
41
sp_line_get_type (void)
43
static GType line_type = 0;
46
GTypeInfo line_info = {
49
NULL, /* base_finalize */
50
(GClassInitFunc) sp_line_class_init,
51
NULL, /* klass_finalize */
52
NULL, /* klass_data */
55
(GInstanceInitFunc) sp_line_init,
56
NULL, /* value_table */
58
line_type = g_type_register_static (SP_TYPE_SHAPE, "SPLine", &line_info, (GTypeFlags)0);
64
sp_line_class_init (SPLineClass *klass)
66
parent_class = (SPShapeClass *) g_type_class_ref (SP_TYPE_SHAPE);
68
SPObjectClass *sp_object_class = (SPObjectClass *) klass;
69
sp_object_class->build = sp_line_build;
70
sp_object_class->set = sp_line_set;
71
sp_object_class->write = sp_line_write;
73
SPItemClass *item_class = (SPItemClass *) klass;
74
item_class->description = sp_line_description;
75
item_class->set_transform = sp_line_set_transform;
77
sp_object_class->update = sp_line_update;
79
SPShapeClass *shape_class = (SPShapeClass *) klass;
80
shape_class->set_shape = sp_line_set_shape;
84
sp_line_init (SPLine * line)
94
sp_line_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
96
if (((SPObjectClass *) parent_class)->build) {
97
((SPObjectClass *) parent_class)->build (object, document, repr);
100
sp_object_read_attr (object, "x1");
101
sp_object_read_attr (object, "y1");
102
sp_object_read_attr (object, "x2");
103
sp_object_read_attr (object, "y2");
107
sp_line_set (SPObject *object, unsigned int key, const gchar *value)
109
SPLine * line = SP_LINE (object);
111
/* fixme: we should really collect updates */
115
line->x1.readOrUnset(value);
116
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
119
line->y1.readOrUnset(value);
120
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
123
line->x2.readOrUnset(value);
124
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
127
line->y2.readOrUnset(value);
128
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
131
if (((SPObjectClass *) parent_class)->set)
132
((SPObjectClass *) parent_class)->set (object, key, value);
138
sp_line_update (SPObject *object, SPCtx *ctx, guint flags)
140
if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
141
SPLine *line = SP_LINE (object);
143
SPStyle const *style = object->style;
144
double const d = 1.0 / NR::expansion(((SPItemCtx const *) ctx)->i2vp);
145
double const em = style->font_size.computed;
146
double const ex = em * 0.5; // fixme: get from pango or libnrtype.
147
line->x1.update(em, ex, d);
148
line->x2.update(em, ex, d);
149
line->y1.update(em, ex, d);
150
line->y2.update(em, ex, d);
152
sp_shape_set_shape ((SPShape *) object);
155
if (((SPObjectClass *) parent_class)->update)
156
((SPObjectClass *) parent_class)->update (object, ctx, flags);
160
static Inkscape::XML::Node *
161
sp_line_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
163
SPLine *line = SP_LINE (object);
165
if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
166
repr = sp_repr_new ("svg:line");
169
if (repr != SP_OBJECT_REPR (object)) {
170
repr->mergeFrom(SP_OBJECT_REPR (object), "id");
173
sp_repr_set_svg_double(repr, "x1", line->x1.computed);
174
sp_repr_set_svg_double(repr, "y1", line->y1.computed);
175
sp_repr_set_svg_double(repr, "x2", line->x2.computed);
176
sp_repr_set_svg_double(repr, "y2", line->y2.computed);
178
if (((SPObjectClass *) (parent_class))->write)
179
((SPObjectClass *) (parent_class))->write (object, repr, flags);
185
sp_line_description(SPItem *item)
187
return g_strdup(_("<b>Line</b>"));
191
sp_line_set_transform (SPItem *item, NR::Matrix const &xform)
193
SPLine *line = SP_LINE (item);
196
points[0] = NR::Point(line->x1.computed, line->y1.computed);
197
points[1] = NR::Point(line->x2.computed, line->y2.computed);
202
line->x1.computed = points[0][NR::X];
203
line->y1.computed = points[0][NR::Y];
204
line->x2.computed = points[1][NR::X];
205
line->y2.computed = points[1][NR::Y];
207
sp_item_adjust_stroke(item, NR::expansion(xform));
209
SP_OBJECT (item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
211
return NR::identity();
215
sp_line_set_shape (SPShape *shape)
217
SPLine *line = SP_LINE (shape);
219
SPCurve *c = sp_curve_new ();
221
sp_curve_moveto (c, line->x1.computed, line->y1.computed);
222
sp_curve_lineto (c, line->x2.computed, line->y2.computed);
224
sp_shape_set_curve_insync (shape, c, TRUE); // *_insync does not call update, avoiding infinite recursion when set_shape is called by update