Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
node.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
13#ifndef SEEN_INKSCAPE_XML_NODE_H
14#define SEEN_INKSCAPE_XML_NODE_H
15
16#include <vector>
17
18#include <2geom/point.h>
19
20#include "gc-anchored.h"
21#include "inkgc/gc-alloc.h"
22#include "node-iterators.h"
23#include "util/const_char_ptr.h"
24
25class SVGLength;
26
27namespace Inkscape {
28namespace XML {
29
30class AttributeRecord;
31struct Document;
32class NodeObserver;
33
34using AttributeVector = std::vector<AttributeRecord, Inkscape::GC::Alloc<AttributeRecord>>;
35
39enum class NodeType
40{
43 TEXT_NODE,
45 PI_NODE
46};
47
48// careful; GC::Anchored should only appear once in the inheritance
49// hierarchy; otherwise there will be leaks
50
81public:
82 Node() = default;
83 ~Node() override = default;
84
94 virtual NodeType type() const = 0;
95
104 virtual char const *name() const = 0;
109 virtual int code() const = 0;
110
119 virtual unsigned position() const = 0;
120
125 virtual unsigned childCount() const = 0;
126
135 virtual char const *content() const = 0;
136
148 virtual char const *attribute(char const *key) const = 0;
149
159 virtual const AttributeVector & attributeList() const=0;
160
172 virtual bool matchAttributeName(char const *partial_name) const = 0;
173
188 virtual void setPosition(int pos) = 0;
189
197 virtual void setContent(char const *value) = 0;
198
200
210
224 bool copyAttribute(Util::const_char_ptr key, Node const *source_node, bool remove_if_empty = false);
225
232 bool getAttributeBoolean(Util::const_char_ptr key, bool default_value = false) const;
233
234 int getAttributeInt(Util::const_char_ptr key, int default_value = 0) const;
235
236 double getAttributeDouble(Util::const_char_ptr key, double default_value = 0.0) const;
237
239
241
248
255
257 double val, double default_value);
258
260
262
264
273
281
283
291 virtual void setCodeUnsafe(int code) = 0;
292
305 virtual Document *document() = 0;
306 virtual Document const *document() const = 0;
308
310
321 virtual Node *root() = 0;
322 virtual Node const *root() const = 0;
324
326
333 virtual Node *parent() = 0;
334 virtual Node const *parent() const = 0;
336
338
348 virtual Node *next() = 0;
349 virtual Node const *next() const = 0;
350 virtual Node *prev() = 0;
351 virtual Node const *prev() const = 0;
353
355
362 virtual Node *firstChild() = 0;
363 virtual Node const *firstChild() const = 0;
365
367
374 virtual Node *lastChild() = 0;
375 virtual Node const *lastChild() const = 0;
377
379
387 virtual Node *nthChild(unsigned index) = 0;
388 virtual Node const *nthChild(unsigned index) const = 0;
390
409 virtual Node *duplicate(Document *doc) const = 0;
410
420 virtual void addChild(Node *child, Node *after) = 0;
421
430 void addChildAtPos(Node *child, unsigned pos)
431 {
432 Node *after = (pos == 0) ? nullptr : nthChild(pos - 1);
433 addChild(child, after);
434 }
435
440 virtual void appendChild(Node *child) = 0;
441
452 virtual void removeChild(Node *child) = 0;
453
462 virtual void changeOrder(Node *child, Node *after) = 0;
463
469 virtual void cleanOriginal(Node *src, char const *key) = 0;
470
476 virtual bool equal(Node const *other, bool recursive, bool skip_ids = false) = 0;
494 virtual void mergeFrom(Node const *src, char const *key, bool extension = false, bool clean = false) = 0;
495
531
543
549
550 virtual void recursivePrintTree(unsigned level) = 0;
551
555
557 iterator begin() { return iterator(this->firstChild()); }
559 iterator end() { return iterator(nullptr); }
560
562 bool operator==(const std::string &name) const { return this->name() == name; }
563
574 template <typename T>
576 {
577 return findChildPath(list.cbegin(), list.cend());
578 }
579
581 template <typename iterT>
582 Node *findChildPath(iterT itr, iterT end)
583 {
584 if (itr == end) {
585 return this;
586 }
587
588 for (auto &child : *this) {
589 if (child == *itr) {
590 auto found = child.findChildPath(std::next(itr), end);
591 if (found != nullptr) {
592 return found;
593 }
594 }
595 }
596
597 return nullptr;
598 }
599
600protected:
601 Node(Node const &)
602 : Anchored()
603 {}
604
605 virtual void setAttributeImpl(char const *key, char const *value) = 0;
606};
607
608} // namespace XML
609} // namespace Inkscape
610
611#endif
612/*
613 Local Variables:
614 mode:c++
615 c-file-style:"stroustrup"
616 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
617 indent-tabs-mode:nil
618 fill-column:99
619 End:
620*/
621// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Cairo::RefPtr< Cairo::Region > clean
Definition canvas.cpp:183
Two-dimensional point that doubles as a vector.
Definition point.h:66
A base class for anchored objects.
Definition gc-anchored.h:46
Non-owning reference to 'const char*' Main-purpose: avoid overloads of type f(char*,...
const char * data() const noexcept
Interface for XML node observers.
Interface for refcounted XML nodes.
Definition node.h:80
virtual Node const * root() const =0
bool setAttributeSvgLength(Util::const_char_ptr key, SVGLength const &val)
Definition node.cpp:131
virtual Node * parent()=0
Get the parent of this node.
virtual Node * next()=0
Get the next sibling of this node.
virtual void synthesizeEvents(NodeObserver &observer)=0
Generate a sequence of events corresponding to the state of this node.
virtual Node * prev()=0
Node * findChildPath(iterT itr, iterT end)
template reshuffling to make the more useful findChildPath cleaner
Definition node.h:582
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
virtual void addSubtreeObserver(NodeObserver &observer)=0
Add an object that will be notified of the changes to this node and its descendants.
virtual void addChild(Node *child, Node *after)=0
Insert another node as a child of this node.
virtual void recursivePrintTree(unsigned level)=0
virtual void setCodeUnsafe(int code)=0
Set the integer GQuark code for the name of the node.
Node * findChildPath(T list)
depth first search to find a node
Definition node.h:575
virtual void setPosition(int pos)=0
Set the position of this node in parent's child order.
virtual void changeOrder(Node *child, Node *after)=0
Move a given node in this node's child order.
virtual Node const * next() const =0
~Node() override=default
virtual int code() const =0
Get the integer code corresponding to the node's name.
virtual void appendChild(Node *child)=0
Append a node as the last child of this node.
virtual Node const * prev() const =0
virtual char const * name() const =0
Get the name of the element node.
virtual void cleanOriginal(Node *src, char const *key)=0
Remove all elements that not in src node.
virtual const AttributeVector & attributeList() const =0
Get a list of the node's attributes.
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
iterator end()
Helper to use the standard lib container functions.
Definition node.h:559
Node(Node const &)
Definition node.h:601
void addChildAtPos(Node *child, unsigned pos)
Insert another node as a child of this node.
Definition node.h:430
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:25
virtual Document const * document() const =0
virtual void removeSubtreeObserver(NodeObserver &observer)=0
Remove an object from the subtree observers list.
virtual Node * nthChild(unsigned index)=0
Get the child of this node with a given index.
bool setAttributeInt(Util::const_char_ptr key, int val)
Definition node.cpp:92
virtual Node * duplicate(Document *doc) const =0
Create a duplicate of this node.
virtual Node const * parent() const =0
virtual Node const * nthChild(unsigned index) const =0
virtual Node * firstChild()=0
Get the first child of this node.
virtual unsigned position() const =0
Get the index of this node in parent's child order.
virtual void setContent(char const *value)=0
Set the content of a text or comment node.
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 bool equal(Node const *other, bool recursive, bool skip_ids=false)=0
Compare 2 nodes equality.
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
virtual void mergeFrom(Node const *src, char const *key, bool extension=false, bool clean=false)=0
Merge all children of another node with the current.
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
virtual char const * content() const =0
Get the content of a text or comment node.
virtual Node const * firstChild() const =0
virtual Node const * lastChild() const =0
virtual void addObserver(NodeObserver &observer)=0
Add an object that will be notified of the changes to this node.
virtual bool matchAttributeName(char const *partial_name) const =0
Check whether this node has any attribute that matches a string.
virtual Document * document()=0
Get the node's associated document.
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
virtual void removeChild(Node *child)=0
Remove a child of this node.
virtual unsigned childCount() const =0
Get the number of children of this node.
iterator begin()
Iterator over children.
Definition node.h:557
virtual NodeType type() const =0
Get the type of the node.
bool operator==(const std::string &name) const
Compare a node by looking at its name to a string.
Definition node.h:562
virtual void removeObserver(NodeObserver &observer)=0
Remove an object from the list of observers.
bool setAttributeSvgDouble(Util::const_char_ptr key, double val)
For attributes where an exponent is allowed.
Definition node.cpp:111
virtual Node * lastChild()=0
Get the last child of this node.
virtual Node * root()=0
Get the root node of this node's document.
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
Inkscape::XML::NodeSiblingIterator iterator
Definition node.h:554
SVG length type.
Definition svg-length.h:22
NodeObserver const * observer
Provides const_char_ptr
Inkscape::GC::Alloc - GC-aware STL allocator.
Inkscape::Util::ForwardPointerIterator< Node, NodeSiblingIteratorStrategy > NodeSiblingIterator
NodeType
Enumeration containing all supported node types.
Definition node.h:40
@ DOCUMENT_NODE
Top-level document node. Do not confuse with the root node.
@ PI_NODE
Processing instruction node, e.g. <?xml version="1.0" encoding="utf-8" standalone="no"?...
@ COMMENT_NODE
Comment node, e.g. <!– some comment –>.
@ ELEMENT_NODE
Regular element node, e.g. <group />.
@ TEXT_NODE
Text node, e.g. "Some text" in <group>Some text</group> is represented by a text node.
std::vector< AttributeRecord, Inkscape::GC::Alloc< AttributeRecord > > AttributeVector
Definition node.h:34
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
Ocnode * child[8]
Definition quantize.cpp:33
Interface for XML documents.
Definition document.h:43
int index