Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
sp-radial-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-radial-gradient.h"
12
13#include "attributes.h" // for SPAttr
14#include <2geom/rect.h> // for Rect
15#include "style.h" // for SPStyle
16
17#include "display/drawing-paintserver.h" // for DrawingRadialGradient, Draw...
18#include "object/sp-gradient-units.h" // for SPGradientUnits
19#include "object/sp-gradient-vector.h" // for SPGradientVector
20#include "object/sp-gradient.h" // for SPGradient
21#include "object/sp-item.h" // for SPItemCtx
22#include "object/sp-object.h" // for SP_OBJECT_MODIFIED_FLAG
23#include "style-internal.h" // for SPIFontSize
24#include "xml/document.h" // for Document
25#include "xml/node.h" // for Node
26
27class SPDocument;
28
29/*
30 * Radial Gradient
31 */
33 this->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
34 this->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
35 this->r.unset(SVGLength::PERCENT, 0.5, 0.5);
36 this->fx.unset(SVGLength::PERCENT, 0.5, 0.5);
37 this->fy.unset(SVGLength::PERCENT, 0.5, 0.5);
38 this->fr.unset(SVGLength::PERCENT, 0.5, 0.5);
39}
40
42
56
60void SPRadialGradient::set(SPAttr key, gchar const *value) {
61
62 switch (key) {
63 case SPAttr::CX:
64 if (!this->cx.read(value)) {
65 this->cx.unset(SVGLength::PERCENT, 0.5, 0.5);
66 }
67
68 if (!this->fx._set) {
69 this->fx.value = this->cx.value;
70 this->fx.computed = this->cx.computed;
71 }
72
73 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
74 break;
75
76 case SPAttr::CY:
77 if (!this->cy.read(value)) {
78 this->cy.unset(SVGLength::PERCENT, 0.5, 0.5);
79 }
80
81 if (!this->fy._set) {
82 this->fy.value = this->cy.value;
83 this->fy.computed = this->cy.computed;
84 }
85
86 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
87 break;
88
89 case SPAttr::R:
90 if (!this->r.read(value)) {
91 this->r.unset(SVGLength::PERCENT, 0.5, 0.5);
92 }
93
94 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
95 break;
96
97 case SPAttr::FX:
98 if (!this->fx.read(value)) {
99 this->fx.unset(this->cx.unit, this->cx.value, this->cx.computed);
100 }
101
102 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
103 break;
104
105 case SPAttr::FY:
106 if (!this->fy.read(value)) {
107 this->fy.unset(this->cy.unit, this->cy.value, this->cy.computed);
108 }
109
110 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
111 break;
112
113 case SPAttr::FR:
114 if (!this->fr.read(value)) {
115 this->fr.unset(SVGLength::PERCENT, 0.0, 0.0);
116 }
117 this->requestModified(SP_OBJECT_MODIFIED_FLAG);
118 break;
119
120 default:
121 SPGradient::set(key, value);
122 break;
123 }
124}
125
126void
128{
129 // To do: Verify flags.
130 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
131
132 SPItemCtx const *ictx = reinterpret_cast<SPItemCtx const *>(ctx);
133
135 double w = ictx->viewport.width();
136 double h = ictx->viewport.height();
137 double d = sqrt ((w*w + h*h)/2.0);
138 double const em = style->font_size.computed;
139 double const ex = 0.5 * em; // fixme: get x height from pango or libnrtype.
140
141 this->cx.update(em, ex, w);
142 this->cy.update(em, ex, h);
143 this->r.update(em, ex, d);
144 this->fx.update(em, ex, w);
145 this->fy.update(em, ex, h);
146 this->fr.update(em, ex, d);
147 }
148 }
149}
150
155 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
156 repr = xml_doc->createElement("svg:radialGradient");
157 }
158
159 if ((flags & SP_OBJECT_WRITE_ALL) || this->cx._set) {
160 repr->setAttributeSvgDouble("cx", this->cx.computed);
161 }
162
163 if ((flags & SP_OBJECT_WRITE_ALL) || this->cy._set) {
164 repr->setAttributeSvgDouble("cy", this->cy.computed);
165 }
166
167 if ((flags & SP_OBJECT_WRITE_ALL) || this->r._set) {
168 repr->setAttributeSvgDouble("r", this->r.computed);
169 }
170
171 if ((flags & SP_OBJECT_WRITE_ALL) || this->fx._set) {
172 repr->setAttributeSvgDouble("fx", this->fx.computed);
173 }
174
175 if ((flags & SP_OBJECT_WRITE_ALL) || this->fy._set) {
176 repr->setAttributeSvgDouble("fy", this->fy.computed);
177 }
178
179 if ((flags & SP_OBJECT_WRITE_ALL) || this->fr._set) {
180 repr->setAttributeSvgDouble("fr", this->fr.computed);
181 }
182
183 SPGradient::write(xml_doc, repr, flags);
184
185 return repr;
186}
187
188std::unique_ptr<Inkscape::DrawingPaintServer> SPRadialGradient::create_drawing_paintserver()
189{
190 ensureVector();
191 return std::make_unique<Inkscape::DrawingRadialGradient>(getSpread(), getUnits(), gradientTransform,
193}
194
195/*
196 Local Variables:
197 mode:c++
198 c-file-style:"stroustrup"
199 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200 indent-tabs-mode:nil
201 fill-column:99
202 End:
203*/
204// 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.
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.
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 * write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, unsigned int flags) override
Write radial gradient attributes to associated repr.
std::unique_ptr< Inkscape::DrawingPaintServer > create_drawing_paintserver() override
void build(SPDocument *document, Inkscape::XML::Node *repr) override
Set radial gradient attributes from associated repr.
void set(SPAttr key, char const *value) override
Set radial gradient attribute.
~SPRadialGradient() override
void update(SPCtx *ctx, unsigned int flags) override
T< SPAttr::FONT_SIZE, SPIFontSize > font_size
Size of the font.
Definition style.h:116
bool read(char const *str)
float value
Definition svg-length.h:47
bool _set
Definition svg-length.h:41
void unset(Unit u=NONE, float v=0, float c=0)
Unit unit
Definition svg-length.h:44
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.