Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
node.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2
14#include "node.h"
15
16#include <2geom/point.h>
17
18#include "svg/stringstream.h"
20#include "svg/svg-length.h"
21
22namespace Inkscape{
23namespace XML {
24
29
30bool Node::copyAttribute(Util::const_char_ptr key, Node const *source_node, bool remove_if_empty)
31{
32 if (source_node) {
33 if (auto const value = source_node->attribute(key.data())) {
34 if (*value || !remove_if_empty) {
35 setAttribute(key, value);
36 }
37 return true;
38 }
39
40 if (remove_if_empty) {
42 return true;
43 }
44 }
45
46 return false;
47}
48
49bool Node::getAttributeBoolean(Util::const_char_ptr key, bool default_value) const
50{
51 auto v = this->attribute(key.data());
52 if (v == nullptr) {
53 return default_value;
54 }
55
56 if (!g_ascii_strcasecmp(v, "true") ||
57 !g_ascii_strcasecmp(v, "yes") ||
58 !g_ascii_strcasecmp(v, "y") ||
59 (atoi(v) != 0))
60 {
61 return true;
62 } else {
63 return false;
64 }
65}
66
67int Node::getAttributeInt(Util::const_char_ptr key, int default_value) const
68{
69 auto v = this->attribute(key.data());
70 if (v == nullptr) {
71 return default_value;
72 }
73 return atoi(v);
74}
75
76double Node::getAttributeDouble(Util::const_char_ptr key, double default_value) const
77{
78 auto v = this->attribute(key.data());
79 if (v == nullptr) {
80 return default_value;
81 }
82
83 return g_ascii_strtod(v, nullptr);
84}
85
87{
88 this->setAttribute(key, (val) ? "true" : "false");
89 return true;
90}
91
93{
94 char c[32];
95
96 g_snprintf(c, 32, "%d", val);
97
98 this->setAttribute(key, c);
99 return true;
100}
101
103{
105 os << val;
106
107 this->setAttribute(key, os.str());
108 return true;
109}
110
112{
113 g_return_val_if_fail(val == val, false); // tests for nan
114
116 os << val;
117
118 this->setAttribute(key, os.str());
119 return true;
120}
121
123{
124 if (val == default_value) {
125 this->removeAttribute(key);
126 return true;
127 }
128 return this->setAttributeSvgDouble(key, val);
129}
130
132{
133 this->setAttribute(key, val.write());
134 return true;
135}
136
138{
140 os << val[Geom::X] << "," << val[Geom::Y];
141
142 this->setAttribute(key, os.str());
143 return true;
144}
145
147{
148 auto v = this->attribute(key.data());
149 if (v == nullptr) {
150 return default_value;
151 }
152
153 char **strarray = g_strsplit(v, ",", 2);
154
155 if (strarray && strarray[0] && strarray[1]) {
156 double newx, newy;
157 newx = g_ascii_strtod(strarray[0], nullptr);
158 newy = g_ascii_strtod(strarray[1], nullptr);
159 g_strfreev(strarray);
160 return Geom::Point(newx, newy);
161 }
162
163 g_strfreev(strarray);
164 return default_value;
165}
166
168{
169 this->setAttributeImpl(key.data(), (value.data() == nullptr || value.data()[0] == '\0') ? nullptr : value.data());
170}
171
172} // namespace XML
173} // namespace Inkscape
Cartesian point / 2D vector and related operations.
Two-dimensional point that doubles as a vector.
Definition point.h:66
A thin wrapper around std::ostringstream, but writing floating point numbers in the format required b...
std::string str() const
Non-owning reference to 'const char*' Main-purpose: avoid overloads of type f(char*,...
const char * data() const noexcept
Interface for refcounted XML nodes.
Definition node.h:80
bool setAttributeSvgLength(Util::const_char_ptr key, SVGLength const &val)
Definition node.cpp:131
bool copyAttribute(Util::const_char_ptr key, Node const *source_node, bool remove_if_empty=false)
Copy attribute value from another node to this node.
Definition node.cpp:30
double getAttributeDouble(Util::const_char_ptr key, double default_value=0.0) const
Definition node.cpp:76
void setAttributeOrRemoveIfEmpty(Inkscape::Util::const_char_ptr key, Inkscape::Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:167
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:25
bool setAttributeInt(Util::const_char_ptr key, int val)
Definition node.cpp:92
bool setAttributeSvgNonDefaultDouble(Util::const_char_ptr key, double val, double default_value)
Definition node.cpp:122
Geom::Point getAttributePoint(Util::const_char_ptr key, Geom::Point default_value={}) const
Definition node.cpp:146
virtual void setAttributeImpl(char const *key, char const *value)=0
bool setAttributePoint(Util::const_char_ptr key, Geom::Point const &val)
Definition node.cpp:137
bool setAttributeCssDouble(Util::const_char_ptr key, double val)
Set a property attribute to val [slightly rounded], in the format required for CSS properties: in par...
Definition node.cpp:102
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
void removeAttribute(Inkscape::Util::const_char_ptr key)
Remove an attribute of this node.
Definition node.h:280
int getAttributeInt(Util::const_char_ptr key, int default_value=0) const
Definition node.cpp:67
bool setAttributeBoolean(Util::const_char_ptr key, bool val)
Definition node.cpp:86
bool setAttributeSvgDouble(Util::const_char_ptr key, double val)
For attributes where an exponent is allowed.
Definition node.cpp:111
bool getAttributeBoolean(Util::const_char_ptr key, bool default_value=false) const
Parses the boolean value of an attribute "key" in repr and sets val accordingly, or to false if the a...
Definition node.cpp:49
SVG length type.
Definition svg-length.h:22
std::string write() const
TODO: insert short description here.
double c[8][4]
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
TODO: insert short description here.