Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-linear-gradient.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright (C) 2018 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11#include "sp-linear-gradient.h"
12
13#include "attributes.h" // for SPAttr
14#include <2geom/rect.h> // for Rect
15#include "style-internal.h" // for SPIFontSize
16#include "style.h" // for SPStyle
17
18#include "display/drawing-paintserver.h" // for DrawingLinearGradient, Draw...
19#include "object/sp-gradient-units.h" // for SPGradientUnits
20#include "object/sp-gradient-vector.h" // for SPGradientVector
21#include "object/sp-gradient.h" // for SPGradient
22#include "object/sp-item.h" // for SPItemCtx
23#include "object/sp-object.h" // for SP_OBJECT_MODIFIED_FLAG
24#include "xml/document.h" // for Document
25#include "xml/node.h" // for Node
26
27class SPDocument;
28
29/*
30 * Linear Gradient
31 */
33 this->x1.unset(SVGLength::PERCENT, 0.0, 0.0);
34 this->y1.unset(SVGLength::PERCENT, 0.0, 0.0);
35 this->x2.unset(SVGLength::PERCENT, 1.0, 1.0);
36 this->y2.unset(SVGLength::PERCENT, 0.0, 0.0);
37}
38
40
42 this->readAttr(SPAttr::X1);
43 this->readAttr(SPAttr::Y1);
44 this->readAttr(SPAttr::X2);
45 this->readAttr(SPAttr::Y2);
46
47 SPGradient::build(document, repr);
48}
49
53void SPLinearGradient::set(SPAttr key, const gchar* value) {
54 switch (key) {
55 case SPAttr::X1:
56 this->x1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
57 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
58 break;
59
60 case SPAttr::Y1:
61 this->y1.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
62 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
63 break;
64
65 case SPAttr::X2:
66 this->x2.readOrUnset(value, SVGLength::PERCENT, 1.0, 1.0);
67 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
68 break;
69
70 case SPAttr::Y2:
71 this->y2.readOrUnset(value, SVGLength::PERCENT, 0.0, 0.0);
72 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
73 break;
74
75 default:
76 SPGradient::set(key, value);
77 break;
78 }
79}
80
81void
83{
84 // To do: Verify flags.
85 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
86
87 SPItemCtx const *ictx = reinterpret_cast<SPItemCtx const *>(ctx);
88
90 double w = ictx->viewport.width();
91 double h = ictx->viewport.height();
92 double const em = style->font_size.computed;
93 double const ex = 0.5 * em; // fixme: get x height from pango or libnrtype.
94
95 this->x1.update(em, ex, w);
96 this->y1.update(em, ex, h);
97 this->x2.update(em, ex, w);
98 this->y2.update(em, ex, h);
99 }
100 }
101}
102
107 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
108 repr = xml_doc->createElement("svg:linearGradient");
109 }
110
111 if ((flags & SP_OBJECT_WRITE_ALL) || this->x1._set) {
112 repr->setAttributeSvgDouble("x1", this->x1.computed);
113 }
114
115 if ((flags & SP_OBJECT_WRITE_ALL) || this->y1._set) {
116 repr->setAttributeSvgDouble("y1", this->y1.computed);
117 }
118
119 if ((flags & SP_OBJECT_WRITE_ALL) || this->x2._set) {
120 repr->setAttributeSvgDouble("x2", this->x2.computed);
121 }
122
123 if ((flags & SP_OBJECT_WRITE_ALL) || this->y2._set) {
124 repr->setAttributeSvgDouble("y2", this->y2.computed);
125 }
126
127 SPGradient::write(xml_doc, repr, flags);
128
129 return repr;
130}
131
132std::unique_ptr<Inkscape::DrawingPaintServer> SPLinearGradient::create_drawing_paintserver()
133{
134 ensureVector();
135 return std::make_unique<Inkscape::DrawingLinearGradient>(getSpread(), getUnits(), gradientTransform,
137}
138
139/*
140 Local Variables:
141 mode:c++
142 c-file-style:"stroustrup"
143 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
144 indent-tabs-mode:nil
145 fill-column:99
146 End:
147*/
148// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
Lookup dictionary for attributes/properties.
SPAttr
Definition attributes.h:27
C height() const
Get the vertical extent of the rectangle.
C width() const
Get the horizontal extent of the rectangle.
Interface for refcounted XML nodes.
Definition node.h:80
bool setAttributeSvgDouble(Util::const_char_ptr key, double val)
For attributes where an exponent is allowed.
Definition node.cpp:111
Typed SVG document implementation.
Definition document.h:101
Gradient.
Definition sp-gradient.h:86
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Virtual build: set gradient attributes from its associated repr.
SPGradientVector vector
Linear and Radial Gradients.
Inkscape::XML::Node * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned int flags) override
Write gradient attributes to repr.
Geom::Affine gradientTransform
gradientTransform attribute
Definition sp-gradient.h:99
SPGradientSpread getSpread() const
SPGradientUnits getUnits() const
void set(SPAttr key, char const *value) override
Set gradient attribute to value.
void ensureVector()
Forces vector to be built, if not present (i.e.
std::unique_ptr< Inkscape::DrawingPaintServer > create_drawing_paintserver() override
Inkscape::XML::Node * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned int flags) override
Callback: write attributes to associated repr.
void update(SPCtx *ctx, guint flags) override
void set(SPAttr key, char const *value) override
Callback: set attribute.
~SPLinearGradient() override
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Virtual build: set gradient attributes from its associated repr.
Inkscape::XML::Node * repr
Definition sp-object.h:193
void requestModified(unsigned int flags)
Requests that a modification notification signal be emitted later (e.g.
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.
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)
bool _set
Definition svg-length.h:41
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
Representation of paint servers used when rendering.
static cairo_user_data_key_t key
Axis-aligned rectangle.
TODO: insert short description here.
@ SP_GRADIENT_UNITS_USERSPACEONUSE
TODO: insert short description here.
Some things pertinent to all visible shapes: SPItem, SPItemView, SPItemCtx.
TODO: insert short description here.
Interface for XML documents.
Definition document.h:43
virtual Node * createElement(char const *name)=0
Unused.
Definition sp-object.h:94
std::vector< SPGradientStop > stops
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 internal: classes that are internal to SPStyle.
SPStyle - a style object for SPItem objects.
Interface for XML documents.
Interface for XML nodes.