Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
drawing-item.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
6 * Authors:
7 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
8 *
9 * Copyright (C) 2011 Authors
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#ifndef INKSCAPE_DISPLAY_DRAWING_ITEM_H
14#define INKSCAPE_DISPLAY_DRAWING_ITEM_H
15
16#include <cstdint>
17#include <exception>
18#include <memory>
19#include <list>
20#include <optional>
21#include <type_traits>
22#include <utility>
23#include <boost/operators.hpp>
24#include <boost/intrusive/list.hpp>
25#include <2geom/rect.h>
26#include <2geom/affine.h>
27
28#include "style-enums.h"
29#include "tags.h"
30
31namespace Glib { class ustring; }
32
33class SPStyle;
34class SPItem;
35
36namespace Inkscape {
37
38class Drawing;
39class DrawingCache;
40class DrawingItem;
41class DrawingPattern;
42class DrawingContext;
43
44namespace Filters { class Filter; }
45
46enum class Antialiasing : unsigned char
47{
49};
50
52{
53 std::uint32_t outline_color;
54 std::optional<Antialiasing> antialiasing_override;
55 bool dithering = false;
56};
57
62
63struct CacheData;
64
65struct CacheRecord : boost::totally_ordered<CacheRecord>
66{
67 bool operator<(CacheRecord const &other) const { return score < other.score; }
68 bool operator==(CacheRecord const &other) const { return score == other.score; }
69 operator DrawingItem*() const { return item; }
70 double score;
71 size_t cache_size;
73};
74using CacheList = std::list<CacheRecord>;
75
76struct InvalidItemException : std::exception
77{
78 char const *what() const noexcept override { return "Invalid item in drawing"; }
79};
80
82{
83public:
95 {
97 STATE_BBOX = 1 << 0, // bounding boxes are up-to-date
98 STATE_CACHE = 1 << 1, // cache extents and clean area are up-to-date
99 STATE_PICK = 1 << 2, // can process pick requests
100 STATE_RENDER = 1 << 3, // can be rendered
101 STATE_BACKGROUND = 1 << 4, // filter background data is up to date
102 STATE_ALL = (1 << 5) - 1,
103 STATE_TOTAL_INV = 1 << 5, // used as a reset flag only
104 };
106 {
107 PICK_NORMAL = 0, // normal pick
108 PICK_STICKY = 1 << 0, // sticky pick - ignore visibility and sensitivity
109 PICK_AS_CLIP = 1 << 1, // pick with no stroke and opaque fill regardless of item style
110 PICK_OUTLINE = 1 << 2 // pick in outline mode
111 };
112
114 DrawingItem(DrawingItem const &) = delete;
116 void unlink();
117 virtual int tag() const { return tag_of<decltype(*this)>; }
118
119 Geom::OptIntRect const &bbox() const { return _bbox; }
120 Geom::OptIntRect const &drawbox() const { return _drawbox; }
121 Geom::OptRect const &itemBounds() const { return _item_bbox; }
122 Geom::Affine const &ctm() const { return _ctm; }
124 Drawing &drawing() const { return _drawing; }
125 DrawingItem *parent() const { return _parent; }
126 bool isAncestorOf(DrawingItem const *item) const;
128 bool unisolatedBlend() const;
129
132 void clearChildren();
133
134 bool visible() const { return _visible; }
135 void setVisible(bool visible);
136 bool sensitive() const { return _sensitive; }
137 void setSensitive(bool sensitive);
138
139 virtual void setStyle(SPStyle const *style, SPStyle const *context_style = nullptr);
140 virtual void setChildrenStyle(SPStyle const *context_style);
141 void setOpacity(float opacity);
142 void setAntialiasing(Antialiasing antialias);
143 void setIsolation(bool isolation); // CSS Compositing and Blending
144 void setBlendMode(SPBlendMode blend_mode);
145 void setTransform(Geom::Affine const &trans);
146 void setClip(DrawingItem *item);
147 void setMask(DrawingItem *item);
148 void setFillPattern(DrawingPattern *pattern);
149 void setStrokePattern(DrawingPattern *pattern);
150 void setZOrder(unsigned zorder);
152 void setFilterRenderer(std::unique_ptr<Filters::Filter> renderer);
153
154 void setKey(unsigned key) { _key = key; }
155 unsigned key() const { return _key; }
157 SPItem *getItem() const { return _item; } // SPItem
158
159 void update(Geom::IntRect const &area = Geom::IntRect::infinite(), UpdateContext const &ctx = UpdateContext(), unsigned flags = STATE_ALL, unsigned reset = 0);
160 unsigned render(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area, unsigned flags = 0, DrawingItem const *stop_at = nullptr) const;
161 unsigned render(DrawingContext &dc, Geom::IntRect const &area, unsigned flags = 0) const;
162 void clip(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area) const;
163 DrawingItem *pick(Geom::Point const &p, double delta, unsigned flags = 0);
164
165 Glib::ustring name() const; // For debugging
166 void recursivePrintTree(unsigned level = 0) const; // For debugging
167
168protected:
169 enum class ChildType : unsigned char
170 {
171 ORPHAN = 0, // No parent - implies !parent.
172 NORMAL = 1, // Contained in children of parent.
173 CLIP = 2, // Referenced by clip of parent.
174 MASK = 3, // Referenced by mask of parent.
175 FILL = 4, // Referenced by fill pattern of parent.
176 STROKE = 5, // Referenced by stroke pattern of parent.
177 ROOT = 6 // Referenced by root of drawing.
178 };
180 {
182 RENDER_STOP = 1
183 };
184 virtual ~DrawingItem(); // Private to prevent deletion of items that are still in use by a snapshot.
185 void _renderOutline(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area, unsigned flags) const;
186 void _markForUpdate(unsigned state, bool propagate);
187 void _markForRendering();
189 double _cacheScore();
191 void _setCached(bool cached, bool persistent = false);
192 virtual unsigned _updateItem(Geom::IntRect const &area, UpdateContext const &ctx, unsigned flags, unsigned reset) { return 0; }
193 virtual unsigned _renderItem(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area, unsigned flags, DrawingItem const *stop_at) const { return RENDER_OK; }
194 virtual void _clipItem(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area) const {}
195 virtual DrawingItem *_pickItem(Geom::Point const &p, double delta, unsigned flags) { return nullptr; }
196 virtual bool _canClip() const { return false; }
197 virtual void _dropPatternCache() {}
198
201
202 using ListHook = boost::intrusive::list_member_hook<>;
204
205 using ChildrenList = boost::intrusive::list<
207 boost::intrusive::member_hook<DrawingItem, ListHook, &DrawingItem::_child_hook>
208 >;
210
211 // Todo: Try to get rid of all of these variables, moving them into the object tree.
212 unsigned _key;
214 SPStyle const *_style; // Not used by DrawingGlyphs
215 SPStyle const *_context_style; // Used for 'context-fill', 'context-stroke'
216
217 float _opacity;
218 std::unique_ptr<Geom::Affine> _transform;
225
230 std::unique_ptr<Inkscape::Filters::Filter> _filter;
231 std::unique_ptr<CacheData> _cache;
234
235 CacheList::iterator _cache_iterator;
236
240
241 unsigned _state : 8;
242 unsigned _propagate_state : 8;
244 unsigned _background_new : 1;
247 unsigned _visible : 1;
248 unsigned _sensitive : 1;
249 unsigned _cached_persistent : 1;
250 unsigned _has_cache_iterator : 1;
251 unsigned _pick_children : 1;
254
255 bool _isolation : 1;
257
258 template<typename F>
259 void defer(F &&f)
260 {
261 // Introduce artificial dependence on a template parameter to allow definition with Drawing forward-declared.
262 auto &drawing = static_cast<std::enable_if_t<(sizeof(F) > 0), Drawing&>>(_drawing);
263 drawing.defer(std::forward<F>(f));
264 }
265
266 friend class Drawing;
267};
268
270void apply_antialias(DrawingContext &dc, Antialiasing antialias);
271
274
275} // namespace Inkscape
276
277#endif // INKSCAPE_DISPLAY_DRAWING_ITEM_H
278
279/*
280 Local Variables:
281 mode:c++
282 c-file-style:"stroustrup"
283 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
284 indent-tabs-mode:nil
285 fill-column:99
286 End:
287*/
288// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
3x3 affine transformation matrix.
Geom::IntRect bounds
Definition canvas.cpp:182
constexpr int tag_of
Convenience function to retrieve the tag (class id) of a given type.
Definition cast.h:26
3x3 matrix representing an affine transformation.
Definition affine.h:70
Axis-aligned generic rectangle that can be empty.
Axis aligned, non-empty, generic rectangle.
static CRect infinite()
Create infinite rectangle.
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Two-dimensional point that doubles as a vector.
Definition point.h:66
Minimal wrapper over Cairo.
SVG drawing item for display.
virtual void _clipItem(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area) const
unsigned _cached_persistent
If set, will always be cached regardless of score.
unsigned _background_accumulate
Whether this element accumulates background (has any ancestor with enable-background: new)
unsigned key() const
void clip(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area) const
Rasterize the clipping path.
DrawingPattern * _fill_pattern
SPItem * _item
Used to associate DrawingItems with SPItems that created them.
Geom::OptIntRect const & bbox() const
void appendChild(DrawingItem *item)
DrawingItem(DrawingItem const &)=delete
boost::intrusive::list_member_hook<> ListHook
boost::intrusive::list< DrawingItem, boost::intrusive::member_hook< DrawingItem, ListHook, &DrawingItem::_child_hook > > ChildrenList
void _setCached(bool cached, bool persistent=false)
Enable / disable storing the rendering in memory.
int getUpdateComplexity() const
Geom::OptIntRect _cacheRect() const
Antialiasing _antialias
antialiasing level (default is Good)
void setItem(SPItem *item)
CacheList::iterator _cache_iterator
unsigned render(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area, unsigned flags=0, DrawingItem const *stop_at=nullptr) const
Rasterize items.
std::unique_ptr< CacheData > _cache
Geom::Affine const & ctm() const
DrawingItem * pick(Geom::Point const &p, double delta, unsigned flags=0)
Get the item under the specified point.
Geom::OptIntRect _drawbox
Full visual bounding box - enlarged by filters, shrunk by clips and masks.
Geom::OptIntRect const & drawbox() const
void setBlendMode(SPBlendMode blend_mode)
void setSensitive(bool sensitive)
Geom::OptIntRect _bbox
Bounding box in display (pixel) coords including stroke.
Drawing & drawing() const
virtual void _dropPatternCache()
void setOpacity(float opacity)
virtual void setStyle(SPStyle const *style, SPStyle const *context_style=nullptr)
Process information related to the new style.
unsigned _sensitive
Whether this item responds to events.
unsigned _has_cache_iterator
If set, _cache_iterator is valid.
std::unique_ptr< Inkscape::Filters::Filter > _filter
SPItem * getItem() const
double _cacheScore()
Compute the caching score.
bool unisolatedBlend() const
virtual unsigned _renderItem(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area, unsigned flags, DrawingItem const *stop_at) const
Geom::OptRect _item_bbox
Geometric bounding box in item's user space.
void setVisible(bool visible)
DrawingItem * _parent
void setZOrder(unsigned zorder)
Move this item to the given place in the Z order of siblings. Does nothing if the item is not a norma...
DrawingItem * parent() const
virtual DrawingItem * _pickItem(Geom::Point const &p, double delta, unsigned flags)
void setAntialiasing(Antialiasing antialias)
virtual void setChildrenStyle(SPStyle const *context_style)
Recursively update children style.
SPStyle const * _style
std::unique_ptr< Geom::Affine > _transform
Incremental transform from parent to this item's coords.
void _markForUpdate(unsigned state, bool propagate)
Marks the item as needing a recomputation of internal data.
virtual int tag() const
Unlink this node and its subtree from the rendering tree and destroy.
DrawingPattern * _stroke_pattern
void setFilterRenderer(std::unique_ptr< Filters::Filter > renderer)
Glib::ustring name() const
virtual bool _canClip() const
void setTransform(Geom::Affine const &trans)
void _renderOutline(DrawingContext &dc, RenderContext &rc, Geom::IntRect const &area, unsigned flags) const
unsigned _background_new
Whether enable-background: new is set for this element.
void setStrokePattern(DrawingPattern *pattern)
void update(Geom::IntRect const &area=Geom::IntRect::infinite(), UpdateContext const &ctx=UpdateContext(), unsigned flags=STATE_ALL, unsigned reset=0)
Update derived data before operations.
DrawingItem & operator=(DrawingItem const &)=delete
bool isAncestorOf(DrawingItem const *item) const
Returns true if item is among the descendants. Will return false if item == this.
unsigned _pick_children
For groups: if true, children are returned from pick(), otherwise the group is returned.
unsigned _key
Auxiliary key used by the object tree for showing clips/masks/patterns.
void prependChild(DrawingItem *item)
SPStyle const * _context_style
Geom::Affine _ctm
Total transform from item coords to display coords.
void setIsolation(bool isolation)
virtual unsigned _updateItem(Geom::IntRect const &area, UpdateContext const &ctx, unsigned flags, unsigned reset)
void setFillPattern(DrawingPattern *pattern)
void _invalidateFilterBackground(Geom::IntRect const &area)
void _markForRendering()
Marks the current visual bounding box of the item for redrawing.
void setKey(unsigned key)
Geom::Affine transform() const
void setMask(DrawingItem *item)
void setItemBounds(Geom::OptRect const &bounds)
Geom::OptRect const & itemBounds() const
void setClip(DrawingItem *item)
void recursivePrintTree(unsigned level=0) const
Drawing tree node used for rendering paints.
void defer(F &&f)
Definition drawing.h:138
Base class for visual SVG elements.
Definition sp-item.h:109
An SVG style object.
Definition style.h:45
RectangularCluster rc
SPItem * item
Affine identity()
Create an identity matrix.
Definition affine.h:210
Helper class to stream background task notifications as a series of messages.
void propagate_antialias(SPShapeRendering shape_rendering, DrawingItem &item)
Propagate element's shape rendering attribute into internal anti-aliasing setting of DrawingItem.
std::list< CacheRecord > CacheList
void apply_antialias(DrawingContext &dc, Antialiasing antialias)
Apply antialias setting to Cairo.
Axis-aligned rectangle.
bool operator==(CacheRecord const &other) const
bool operator<(CacheRecord const &other) const
char const * what() const noexcept override
std::uint32_t outline_color
std::optional< Antialiasing > antialiasing_override
SPStyle enums: named public enums that correspond to SVG property values.
SPShapeRendering
SPBlendMode
int delta