Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-line.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * SVG <line> implementation
4 *
5 * Authors:
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * Abhishek Sharma
8 * Jon A. Cruz <jon@joncruz.org>
9 *
10 * Copyright (C) 1999-2002 Lauris Kaplinski
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#include "sp-line.h"
16
17#include <glibmm/i18n.h>
18
19#include "attributes.h"
20#include "style.h"
21#include "sp-guide.h"
22#include "display/curve.h"
23
25 this->x1.unset();
26 this->y1.unset();
27 this->x2.unset();
28 this->y2.unset();
29}
30
31SPLine::~SPLine() = default;
32
35
36 this->readAttr(SPAttr::X1);
37 this->readAttr(SPAttr::Y1);
38 this->readAttr(SPAttr::X2);
39 this->readAttr(SPAttr::Y2);
40}
41
42void SPLine::set(SPAttr key, const gchar* value) {
43 /* fixme: we should really collect updates */
44
45 switch (key) {
46 case SPAttr::X1:
47 this->x1.readOrUnset(value);
48 this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
49 break;
50
51 case SPAttr::Y1:
52 this->y1.readOrUnset(value);
53 this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
54 break;
55
56 case SPAttr::X2:
57 this->x2.readOrUnset(value);
58 this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
59 break;
60
61 case SPAttr::Y2:
62 this->y2.readOrUnset(value);
63 this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
64 break;
65
66 default:
67 SPShape::set(key, value);
68 break;
69 }
70}
71
72void SPLine::update(SPCtx *ctx, guint flags) {
73 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
74 SPStyle const *style = this->style;
75 SPItemCtx const *ictx = (SPItemCtx const *) ctx;
76 double const w = ictx->viewport.width();
77 double const h = ictx->viewport.height();
78 double const em = style->font_size.computed;
79 double const ex = em * 0.5; // fixme: get from pango or libnrtype.
80
81 this->x1.update(em, ex, w);
82 this->x2.update(em, ex, w);
83 this->y1.update(em, ex, h);
84 this->y2.update(em, ex, h);
85
86 this->set_shape();
87 }
88
89 SPShape::update(ctx, flags);
90}
91
93 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
94 repr = xml_doc->createElement("svg:line");
95 }
96
97 if (repr != this->getRepr()) {
98 repr->mergeFrom(this->getRepr(), "id");
99 }
100
101 repr->setAttributeSvgDouble("x1", this->x1.computed);
102 repr->setAttributeSvgDouble("y1", this->y1.computed);
103 repr->setAttributeSvgDouble("x2", this->x2.computed);
104 repr->setAttributeSvgDouble("y2", this->y2.computed);
105
106 SPShape::write(xml_doc, repr, flags);
107
108 return repr;
109}
110
111const char* SPLine::typeName() const {
112 return "path";
113}
114
115const char* SPLine::displayName() const {
116 return _("Line");
117}
118
120 Geom::Point points[2];
121 Geom::Affine const i2dt(this->i2dt_affine());
122
123 points[0] = Geom::Point(this->x1.computed, this->y1.computed)*i2dt;
124 points[1] = Geom::Point(this->x2.computed, this->y2.computed)*i2dt;
125
126 SPGuide::createSPGuide(this->document, points[0], points[1]);
127}
128
129
131 Geom::Point points[2];
132
133 points[0] = Geom::Point(this->x1.computed, this->y1.computed);
134 points[1] = Geom::Point(this->x2.computed, this->y2.computed);
135
136 points[0] *= transform;
137 points[1] *= transform;
138
139 this->x1.computed = points[0][Geom::X];
140 this->y1.computed = points[0][Geom::Y];
141 this->x2.computed = points[1][Geom::X];
142 this->y2.computed = points[1][Geom::Y];
143
144 this->adjust_stroke(transform.descrim());
145
146 this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
147
148 return Geom::identity();
149}
150
152 SPCurve c;
153
154 c.moveto(this->x1.computed, this->y1.computed);
155 c.lineto(this->x2.computed, this->y2.computed);
156
157 // *_insync does not call update, avoiding infinite recursion when set_shape is called by update
158 setCurveInsync(std::move(c));
160
161 // LPE's cannot be applied to lines. (the result can (generally) not be represented as SPLine)
162}
163
164/*
165 Local Variables:
166 mode:c++
167 c-file-style:"stroustrup"
168 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
169 indent-tabs-mode:nil
170 fill-column:99
171 End:
172*/
173// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Lookup dictionary for attributes/properties.
SPAttr
Definition attributes.h:27
3x3 matrix representing an affine transformation.
Definition affine.h:70
Coord descrim() const
Calculate the descriminant.
Definition affine.cpp:434
C height() const
Get the vertical extent of the rectangle.
C width() const
Get the horizontal extent of the rectangle.
Two-dimensional point that doubles as a vector.
Definition point.h:66
Interface for refcounted XML nodes.
Definition node.h:80
virtual void mergeFrom(Node const *src, char const *key, bool extension=false, bool clean=false)=0
Merge all children of another node with the current.
bool setAttributeSvgDouble(Util::const_char_ptr key, double val)
For attributes where an exponent is allowed.
Definition node.cpp:111
Wrapper around a Geom::PathVector object.
Definition curve.h:26
void moveto(Geom::Point const &p)
Perform a moveto to a point, thus starting a new subpath.
Definition curve.cpp:138
Typed SVG document implementation.
Definition document.h:103
static SPGuide * createSPGuide(SPDocument *doc, Geom::Point const &pt1, Geom::Point const &pt2)
Definition sp-guide.cpp:209
Geom::Affine i2dt_affine() const
Returns the transformation from item to desktop coords.
Definition sp-item.cpp:1828
Geom::Affine transform
Definition sp-item.h:138
void adjust_stroke(double ex)
Definition sp-item.cpp:1479
SVGLength x1
Definition sp-line.h:27
void set_shape() override
Definition sp-line.cpp:151
SVGLength y1
Definition sp-line.h:28
void set(SPAttr key, char const *value) override
Definition sp-line.cpp:42
SPLine()
Definition sp-line.cpp:24
void convert_to_guides() const override
Definition sp-line.cpp:119
void update(SPCtx *ctx, unsigned int flags) override
Definition sp-line.cpp:72
const char * typeName() const override
The item's type name, not node tag name.
Definition sp-line.cpp:111
Geom::Affine set_transform(Geom::Affine const &transform) override
Definition sp-line.cpp:130
SVGLength x2
Definition sp-line.h:29
~SPLine() override
const char * displayName() const override
The item's type name as a translated human string.
Definition sp-line.cpp:115
SVGLength y2
Definition sp-line.h:30
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Definition sp-line.cpp:33
Inkscape::XML::Node * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned int flags) override
Definition sp-line.cpp:92
Inkscape::XML::Node * repr
Definition sp-object.h:193
SPDocument * document
Definition sp-object.h:188
SPStyle * style
Represents the style properties, whether from presentation attributes, the style attribute,...
Definition sp-object.h:248
void readAttr(char const *key)
Read value of key attribute from XML node into object.
Inkscape::XML::Node * getRepr()
Returns the XML representation of tree.
void requestDisplayUpdate(unsigned int flags)
Queues an deferred update of this object's display.
Base class for shapes, including <path> element.
Definition sp-shape.h:38
SPCurve const * curve() const
Return a borrowed pointer to the curve (if any exists) or NULL if there is no curve.
Definition sp-shape.cpp:970
void update(SPCtx *ctx, unsigned int flags) override
Definition sp-shape.cpp:125
void setCurveInsync(SPCurve const *)
Definition sp-shape.cpp:958
void set(SPAttr key, char const *value) override
Definition sp-shape.cpp:115
void setCurveBeforeLPE(SPCurve const *)
Definition sp-shape.cpp:942
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Definition sp-shape.cpp:63
Inkscape::XML::Node * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned int flags) override
Definition sp-shape.cpp:120
An SVG style object.
Definition style.h:45
T< SPAttr::FONT_SIZE, SPIFontSize > font_size
Size of the font.
Definition style.h:116
void readOrUnset(char const *str, Unit u=NONE, float v=0, float c=0)
void unset(Unit u=NONE, float v=0, float c=0)
float computed
Definition svg-length.h:50
void update(double em, double ex, double scale)
const double w
Definition conic-4.cpp:19
double c[8][4]
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Affine identity()
Create an identity matrix.
Definition affine.h:210
static cairo_user_data_key_t key
SPGuide – a guideline.
Interface for XML documents.
Definition document.h:43
virtual Node * createElement(char const *name)=0
Unused.
Definition sp-object.h:94
Contains transformations to document/viewport and the viewport size.
Definition sp-item.h:92
Geom::Rect viewport
Viewport size.
Definition sp-item.h:97
SPStyle - a style object for SPItem objects.