Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
turbulence.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/*
6 * Authors:
7 * Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
8 * hugo Rodrigues <haa.rodrigues@gmail.com>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2007 Felipe Sanches
12 * Copyright (C) 2006 Hugo Rodrigues
13 *
14 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 */
16
17#include "turbulence.h"
18
19#include <cmath> // for floor
20#include <cstring> // for strcmp
21
22#include "attributes.h" // for SPAttr
23
24#include "object/filters/sp-filter-primitive.h" // for SPFilterPrimitive
25#include "object/sp-object.h" // for SP_OBJECT_MODIFIED_FLAG
26#include "util/numeric/converters.h" // for read_number
27#include "xml/node.h" // for Node
28
29class SPDocument;
30
31namespace Inkscape {
32class DrawingItem;
33namespace Filters {
34class FilterPrimitive;
35} // namespace Filters
36namespace XML {
37class Document;
38} // namespace XML
39} // namespace Inkscape
40
51
52static bool read_stitchtiles(char const *value)
53{
54 if (!value) {
55 return false; // 'noStitch' is default
56 }
57
58 switch (value[0]) {
59 case 's':
60 if (std::strcmp(value, "stitch") == 0) {
61 return true;
62 }
63 break;
64 case 'n':
65 if (std::strcmp(value, "noStitch") == 0) {
66 return false;
67 }
68 break;
69 }
70
71 return false; // 'noStitch' is default
72}
73
75{
76 if (!value) {
77 return Inkscape::Filters::TURBULENCE_TURBULENCE; // 'turbulence' is default
78 }
79
80 switch (value[0]) {
81 case 'f':
82 if (std::strcmp(value, "fractalNoise") == 0) {
84 }
85 break;
86 case 't':
87 if (std::strcmp(value, "turbulence") == 0) {
89 }
90 break;
91 }
92
93 return Inkscape::Filters::TURBULENCE_TURBULENCE; // 'turbulence' is default
94}
95
96void SPFeTurbulence::set(SPAttr key, char const *value)
97{
98 switch (key) {
100 baseFrequency.set(value);
101
102 // From SVG spec: If two <number>s are provided, the first number represents
103 // a base frequency in the X direction and the second value represents a base
104 // frequency in the Y direction. If one number is provided, then that value is
105 // used for both X and Y.
106 if (baseFrequency.optNumIsSet() == false) {
108 }
109
110 updated = false;
112 break;
113 case SPAttr::NUMOCTAVES: {
114 int n_int = value ? (int)std::floor(Inkscape::Util::read_number(value)) : 1;
115 if (n_int != numOctaves){
117 updated = false;
119 }
120 break;
121 }
122 case SPAttr::SEED: {
123 double n_num = value ? Inkscape::Util::read_number(value) : 0;
124 if (n_num != seed){
125 seed = n_num;
126 updated = false;
128 }
129 break;
130 }
131 case SPAttr::STITCHTILES: {
132 bool n_bool = ::read_stitchtiles(value);
133 if (n_bool != stitchTiles){
135 updated = false;
137 }
138 break;
139 }
140 case SPAttr::TYPE: {
141 auto n_type = ::read_type(value);
142 if (n_type != type) {
143 type = n_type;
144 updated = false;
146 }
147 break;
148 }
149 default:
151 break;
152 }
153}
154
156{
157 // TODO: Don't just clone, but create a new repr node and write all relevant values into it.
158 if (!repr) {
159 repr = getRepr()->duplicate(doc);
160 }
161
162 SPFilterPrimitive::write(doc, repr, flags);
163
164 // turbulence doesn't take input
165 repr->removeAttribute("in");
166
167 return repr;
168}
169
170std::unique_ptr<Inkscape::Filters::FilterPrimitive> SPFeTurbulence::build_renderer(Inkscape::DrawingItem*) const
171{
172 auto turbulence = std::make_unique<Inkscape::Filters::FilterTurbulence>();
174
175 turbulence->set_baseFrequency(0, baseFrequency.getNumber());
176 turbulence->set_baseFrequency(1, baseFrequency.getOptNumber());
177 turbulence->set_numOctaves(numOctaves);
178 turbulence->set_seed(seed);
179 turbulence->set_stitchTiles(stitchTiles);
180 turbulence->set_type(type);
181 turbulence->set_updated(updated);
182
183 return turbulence;
184}
185
186/*
187 Local Variables:
188 mode:c++
189 c-file-style:"stroustrup"
190 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
191 indent-tabs-mode:nil
192 fill-column:99
193 End:
194*/
195// 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
@ STITCHTILES
@ BASEFREQUENCY
@ NUMOCTAVES
SVG drawing item for display.
Interface for refcounted XML nodes.
Definition node.h:80
virtual Node * duplicate(Document *doc) const =0
Create a duplicate of this node.
void removeAttribute(Inkscape::Util::const_char_ptr key)
Remove an attribute of this node.
Definition node.h:280
float getOptNumber(bool or_num=false) const
float getNumber() const
void set(char const *str)
bool optNumIsSet() const
void setOptNumber(float optnum)
Typed SVG document implementation.
Definition document.h:101
NumberOptNumber baseFrequency
Definition turbulence.h:34
std::unique_ptr< Inkscape::Filters::FilterPrimitive > build_renderer(Inkscape::DrawingItem *item) const override
SVGLength x
Definition turbulence.h:35
Inkscape::XML::Node * write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, unsigned flags) override
Inkscape::Filters::FilterTurbulenceType type
Definition turbulence.h:31
void set(SPAttr key, char const *value) override
void build(SPDocument *doc, Inkscape::XML::Node *repr) override
void build(SPDocument *doc, Inkscape::XML::Node *repr) override
Inkscape::XML::Node * write(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, unsigned flags) override
void set(SPAttr key, char const *value) override
void build_renderer_common(Inkscape::Filters::FilterPrimitive *primitive) const
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
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.
static Inkscape::Filters::FilterColorMatrixType read_type(char const *str)
Utility functions to convert ascii representations to numbers.
double read_number(gchar const *value, bool warning=true)
Definition converters.h:42
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
Document level base class for all SVG filter primitives.
Interface for XML documents.
Definition document.h:43
static bool read_stitchtiles(char const *value)
static Inkscape::Filters::FilterTurbulenceType read_type(char const *value)
SVG turbulence filter effect.
Interface for XML nodes.