Canvas.h
1/* Copyright (C) 2015-2024 Marc Boris Duerner
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7
8 As a special exception, you may use this file as part of a free
9 software library without restriction. Specifically, if other files
10 instantiate templates or use macros or inline functions from this
11 file, or you compile this file and link it with other files to
12 produce an executable, this file does not by itself cause the
13 resulting executable to be covered by the GNU General Public
14 License. This exception does not however invalidate any other
15 reasons why the executable file might be covered by the GNU Library
16 General Public License.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301 USA
27*/
28
29#ifndef PT_GFX_CANVAS_H
30#define PT_GFX_CANVAS_H
31
32#include <Pt/Gfx/Api.h>
33#include <Pt/Gfx/Size.h>
34#include <Pt/Gfx/Point.h>
35#include <Pt/Gfx/Rect.h>
36#include <Pt/Gfx/Scaling.h>
37#include <Pt/Gfx/Pen.h>
38#include <Pt/Gfx/Brush.h>
39#include <Pt/Gfx/Font.h>
40#include <Pt/Gfx/CompositionMode.h>
41#include <Pt/Gfx/FontMetrics.h>
42#include <Pt/Gfx/TextMetrics.h>
43#include <Pt/Gfx/Transform.h>
44#include <Pt/Gfx/Path.h>
45
46#include <Pt/String.h>
47#include <Pt/Types.h>
48
49#include <cstddef>
50
51namespace Pt {
52
53namespace Gfx {
54
55class Bitmap;
56
65class PT_GFX_API Canvas
66{
67 friend class PaintSurface;
68
69 protected:
70 Canvas();
71
72 enum DirtyFlags : unsigned
73 {
74 DirtyTransform = 0x01,
75 DirtyComposition = 0x02,
76 DirtyPen = 0x04,
77 DirtyBrush = 0x08,
78 DirtyFont = 0x10,
79 DirtyClip = 0x20,
80 DirtyAll = 0x3F
81 };
82
83 public:
86 virtual ~Canvas();
87
90 const PointF& origin() const;
91
94 const RectF& region() const;
95
98 void setRegion(const RectF& r);
99
102 const Scaling& scaling() const;
103
107
110 const Gfx::ImageFormat& format() const;
111
114 const Gfx::Transform& transform() const;
115
119
123
124 public:
127 void beginPaint(const Gfx::Paint& paint);
128
132
135 bool isActive() const;
136
137 public:
141
144 void setPen(const Pen& pen);
145
148 void setBrush(const Brush& brush);
149
152 void setFont(const Gfx::Font& font);
153
156 void setClip(const RectF& clip);
157
160 void resetClip();
161
162 public:
165 void drawLine(const PointF& from, const PointF& to);
166
169 void drawPolyline(const Gfx::PointF* ps, const size_t n);
170
173 void fillPolygon(const Gfx::PointF* ps, const size_t n);
174
177 void drawRect(const Gfx::RectF& rectangle);
178
181 void fillRect(const Gfx::RectF& rectangle);
182
185 void drawEllipse(const Gfx::PointF& topLeft, const Gfx::SizeF& size);
186
189 void fillEllipse(const Gfx::PointF& topLeft, const Gfx::SizeF& size);
190
191 public:
194 void setPath(const Path& path);
195
198 void drawPath();
199
202 void fillPath();
203
206 void drawPath(const Path& path);
207
210 void fillPath(const Path& path);
211
212 public:
215 const FontMetrics& fontMetrics() const;
216
220
223 void drawText(const PointF& to, const Pt::String& text,
224 const Transform* tform = 0);
225
226 public:
229 void drawImage(const Gfx::PointF& to,
230 const Gfx::Image& image,
231 const Gfx::RectF* rect = 0);
232
235 void drawBitmap(const Gfx::PointF& to,
236 const Gfx::Bitmap& bitmap,
237 const Gfx::RectF* rect = 0);
238
239 protected:
240 virtual void onBeginPaint(const Gfx::Paint& paint) = 0;
241
242 virtual void onFinishPaint() = 0;
243
244 protected:
245 virtual void onSetTransform(const Gfx::Transform& tx) = 0;
246
247 virtual void onApplyTransform() = 0;
248
249 virtual void onSetCompositionMode(const Gfx::CompositionMode& mode) = 0;
250
251 virtual void onApplyCompositionMode() = 0;
252
253 virtual void onSetPen(const Pen& pen) = 0;
254
255 virtual void onApplyPen() = 0;
256
257 virtual void onSetBrush(const Brush& pen) = 0;
258
259 virtual void onApplyBrush() = 0;
260
261 virtual void onSetFont(const Gfx::Font& font) = 0;
262
263 virtual void onApplyFont() = 0;
264
265 virtual void onSetClip(const Gfx::RectF* clip) = 0;
266
267 virtual void onApplyClip() = 0;
268
269 protected:
270 void invalidate(unsigned flags);
271
272 protected:
273 virtual void onDrawLine(const PointF& from, const PointF& to) = 0;
274
275 virtual void onDrawPolyline(const Gfx::PointF* pts, const size_t n) = 0;
276
277 virtual void onFillPolygon(const Gfx::PointF* ps, const size_t n) = 0;
278
279 virtual void onDrawRect(const Gfx::RectF& rectangle) = 0;
280
281 virtual void onFillRect(const Gfx::RectF& rectangle) = 0;
282
283 virtual void onDrawEllipse(const Gfx::PointF& topLeft, const Gfx::SizeF& size) = 0;
284
285 virtual void onFillEllipse(const Gfx::PointF& topLeft, const Gfx::SizeF& size) = 0;
286
287 protected:
288 virtual void onSetPath(const Path& path) = 0;
289
290 virtual void onDrawPath() = 0;
291
292 virtual void onFillPath() = 0;
293
294 virtual void onDrawPath(const Path& path) = 0;
295
296 virtual void onFillPath(const Path& path) = 0;
297
298 protected:
299 virtual const Gfx::FontMetrics& onGetFontMetrics() const = 0;
300
301 virtual Gfx::TextMetrics onGetTextMetrics(const Pt::String& text) const = 0;
302
303 virtual void onDrawText(const Gfx::PointF& to,
304 const Pt::String& text,
305 const Gfx::Transform* transform) = 0;
306
307 protected:
308 virtual void onDrawImage(const Gfx::PointF& to,
309 const Gfx::Image& image,
310 const Gfx::RectF* rect = 0) = 0;
311
312 protected:
313 void applyState();
314
315 private:
316 void attachSurface(PaintSurface& surface);
317
318 void detachSurface(PaintSurface& surface);
319
320 void updateTransform();
321
322 private:
323 PaintSurface* _surface;
324 PaintSurface* _active;
325 RectF _region;
326 Gfx::Scaling _scaling;
327 Transform _viewTx;
328 Transform _userTx;
329 Transform _tx;
330 unsigned _dirty;
331};
332
333} // namespace
334
335} // namespace
336
337#endif
Off-screen image that can be painted.
Definition Bitmap.h:71
Fill color, gradient or texture.
Definition Brush.h:151
void drawPolyline(const Gfx::PointF *ps, const size_t n)
Draws a connected sequence of line segments.
void setClip(const RectF &clip)
Sets the clip rectangle.
void drawPath(const Path &path)
Draws the given path.
bool isActive() const
Returns true if painting is currently active.
void beginPaint(const Gfx::Paint &paint)
Begins painting with the given paint state.
virtual ~Canvas()
Destroys the canvas.
void setPath(const Path &path)
Sets the current path.
void drawImage(const Gfx::PointF &to, const Gfx::Image &image, const Gfx::RectF *rect=0)
Draws an image.
void resetTransform()
Resets the transform to the identity matrix.
void setTransform(const Gfx::Transform &tx)
Sets the current transform.
const RectF & region() const
Returns the current paint region.
const PointF & origin() const
Returns the logical origin of the current region.
const Gfx::Transform & transform() const
Returns the current transform.
void finishPaint()
Ends the current painting operation.
void drawBitmap(const Gfx::PointF &to, const Gfx::Bitmap &bitmap, const Gfx::RectF *rect=0)
Draws a bitmap.
void drawLine(const PointF &from, const PointF &to)
Draws a line between two points.
void setBrush(const Brush &brush)
Sets the current brush.
void setRegion(const RectF &r)
Sets the paint region.
const FontMetrics & fontMetrics() const
Returns metrics for the current font.
const Gfx::ImageFormat & format() const
Returns the pixel format of the target.
const Scaling & scaling() const
Returns the logical-to-physical scaling.
void resetClip()
Clears the clip rectangle.
void fillPath(const Path &path)
Fills the given path.
void setScaling(const Scaling &scaling)
Sets the logical-to-physical scaling.
void fillRect(const Gfx::RectF &rectangle)
Fills a rectangle.
void setPen(const Pen &pen)
Sets the current pen.
void setFont(const Gfx::Font &font)
Sets the current font.
void drawRect(const Gfx::RectF &rectangle)
Draws the outline of a rectangle.
void drawText(const PointF &to, const Pt::String &text, const Transform *tform=0)
Draws a line of text.
void drawPath()
Draws the current path.
void fillPolygon(const Gfx::PointF *ps, const size_t n)
Fills a polygon.
void drawEllipse(const Gfx::PointF &topLeft, const Gfx::SizeF &size)
Draws the outline of an ellipse.
TextMetrics textMetrics(const Pt::String &text) const
Measures a line of text.
void fillEllipse(const Gfx::PointF &topLeft, const Gfx::SizeF &size)
Fills an ellipse.
void setCompositionMode(const Gfx::CompositionMode &mode)
Sets the current composition mode.
void fillPath()
Fills the current path.
How new pixels combine with existing pixels.
Definition CompositionMode.h:49
Ascent, descent and line height of a font.
Definition FontMetrics.h:50
Font family, size and style.
Definition Font.h:63
Runtime pixel format.
Definition ImageFormat.h:72
Pen, brush, font and composition mode.
Definition Paint.h:53
Sequence of lines and curves.
Definition Path.h:81
Outline color, width and style.
Definition Pen.h:59
Conversion between logical units and pixels.
Definition Scaling.h:52
Width and bounds of a text run.
Definition TextMetrics.h:49
2D affine transform.
Definition Transform.h:54
Unicode capable basic_string.
Definition Api-String.h:67
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33