Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
pattern-manipulation.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include <glibmm/fileutils.h>
4#include <glibmm/i18n.h>
6#include "colors/color.h"
7#include "document.h"
9#include "object/sp-pattern.h"
10#include "xml/repr.h"
11
12std::vector<SPDocument *> sp_get_stock_patterns() {
14 return !sp_get_pattern_list(doc).empty();
15 });
16 if (patterns.empty()) {
17 g_warning("No stock patterns!");
18 }
19 return patterns;
20}
21
22std::vector<SPPattern*> sp_get_pattern_list(SPDocument* source) {
23 std::vector<SPPattern*> list;
24 if (!source) return list;
25
26 std::vector<SPObject*> patterns = source->getResourceList("pattern");
27 for (auto pattern : patterns) {
28 auto p = cast<SPPattern>(pattern);
29 if (p && p == p->rootPattern() && p->hasChildren()) { // only if this is a vali root pattern
30 list.push_back(cast<SPPattern>(pattern));
31 }
32 }
33
34 return list;
35}
36
38{
39 if (!pattern) return;
40
42 sp_repr_css_set_property_string(css, "fill", c.toString());
43 pattern->changeCSS(css, "style");
45}
46
47void sp_pattern_set_transform(SPPattern* pattern, const Geom::Affine& transform) {
48 if (!pattern) return;
49
50 // for now, this is that simple
51 pattern->transform_multiply(transform, true);
52}
53
55 if (!pattern) return;
56
57 // TODO: verify
58 pattern->setAttributeDouble("x", offset.x());
59 pattern->setAttributeDouble("y", offset.y());
60}
61
63 if (!pattern) return;
64
65 //TODO: make smarter to keep existing value when possible
66 pattern->setAttribute("preserveAspectRatio", uniform ? "xMidYMid" : "none");
67}
68
69void sp_pattern_set_gap(SPPattern* link_pattern, Geom::Scale gap_percent) {
70 if (!link_pattern) return;
71 auto root = link_pattern->rootPattern();
72 if (!root || root == link_pattern) {
73 g_assert(false && "Setting pattern gap requires link and root patterns objects");
74 return;
75 }
76
77 auto set_gap = [=](double size, double percent, const char* attr) {
78 if (percent == 0.0 || size <= 0.0) {
79 // no gap
80 link_pattern->removeAttribute(attr);
81 }
82 else if (percent > 0.0) {
83 // positive gap
84 link_pattern->setAttributeDouble(attr, size + size * percent / 100.0);
85 }
86 else if (percent < 0.0 && percent > -100.0) {
87 // negative gap - overlap
88 percent = -percent;
89 link_pattern->setAttributeDouble(attr, size - size * percent / 100.0);
90 }
91 };
92
93 set_gap(root->width(), gap_percent[Geom::X], "width");
94 set_gap(root->height(), gap_percent[Geom::Y], "height");
95}
96
98 Geom::Scale gap(0, 0);
99
100 if (!link_pattern) return gap;
101 auto root = link_pattern->rootPattern();
102 if (!root || root == link_pattern) {
103 g_assert(false && "Reading pattern gap requires link and root patterns objects");
104 return gap;
105 }
106
107 auto get_gap = [=](double root_size, double link_size) {
108 if (root_size > 0.0 && link_size > 0.0) {
109 if (link_size > root_size) {
110 return (link_size - root_size) / root_size;
111 }
112 else if (link_size < root_size) {
113 return -link_size / root_size;
114 }
115 }
116 return 0.0;
117 };
118
119 return Geom::Scale(
120 get_gap(root->width(), link_pattern->width()) * 100.0,
121 get_gap(root->height(), link_pattern->height()) * 100.0
122 );
123}
124
125
126std::string sp_get_pattern_label(SPPattern* pattern) {
127 if (!pattern) return std::string();
128
129 Inkscape::XML::Node* repr = pattern->getRepr();
130 if (auto label = pattern->getAttribute("inkscape:label")) {
131 if (*label) {
132 return std::string(gettext(label));
133 }
134 }
135 const char* stock_id = _(repr->attribute("inkscape:stockid"));
136 const char* pat_id = stock_id ? stock_id : _(repr->attribute("id"));
137 return std::string(pat_id ? pat_id : "");
138}
139
3x3 matrix representing an affine transformation.
Definition affine.h:70
Two-dimensional point that doubles as a vector.
Definition point.h:66
Scaling from the origin.
Definition transforms.h:150
Interface for refcounted XML nodes.
Definition node.h:80
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
Typed SVG document implementation.
Definition document.h:101
std::vector< SPObject * > const getResourceList(char const *key)
void setAttributeDouble(Inkscape::Util::const_char_ptr key, double value)
void setAttribute(Inkscape::Util::const_char_ptr key, Inkscape::Util::const_char_ptr value)
void removeAttribute(char const *key)
void changeCSS(SPCSSAttr *css, char const *attr)
Inkscape::XML::Node * getRepr()
Returns the XML representation of tree.
char const * getAttribute(char const *name) const
void transform_multiply(Geom::Affine postmul, bool set)
double width() const
SPPattern const * rootPattern() const
double height() const
std::vector< SPDocument * > get_paint_documents(std::function< bool(SPDocument *)> const &filter)
RootCluster root
std::shared_ptr< Css const > css
Geom::IntPoint size
double c[8][4]
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
double offset
Glib::ustring label
std::vector< SPDocument * > sp_get_stock_patterns()
std::string sp_get_pattern_label(SPPattern *pattern)
void sp_pattern_set_uniform_scale(SPPattern *pattern, bool uniform)
void sp_pattern_set_gap(SPPattern *link_pattern, Geom::Scale gap_percent)
Geom::Scale sp_pattern_get_gap(SPPattern *link_pattern)
void sp_pattern_set_offset(SPPattern *pattern, const Geom::Point &offset)
void sp_pattern_set_color(SPPattern *pattern, Inkscape::Colors::Color const &c)
void sp_pattern_set_transform(SPPattern *pattern, const Geom::Affine &transform)
std::vector< SPPattern * > sp_get_pattern_list(SPDocument *source)
SPCSSAttr * sp_repr_css_attr_new()
Creates an empty SPCSSAttr (a class for manipulating CSS style properties).
Definition repr-css.cpp:67
void sp_repr_css_attr_unref(SPCSSAttr *css)
Unreferences an SPCSSAttr (will be garbage collected if no references remain).
Definition repr-css.cpp:76
void sp_repr_css_set_property_string(SPCSSAttr *css, char const *name, std::string const &value)
Set a style property to a standard string.
Definition repr-css.cpp:234
C facade to Inkscape::XML::Node.
SVG <pattern> implementation.
TODO: insert short description here.
double uniform()