1.11
painter.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_PAINTER_H
7#define EGT_PAINTER_H
8
14#include <egt/color.h>
15#include <egt/detail/meta.h>
16#include <egt/flags.h>
17#include <egt/font.h>
18#include <egt/geometry.h>
19#include <egt/pattern.h>
20#include <egt/types.h>
21#include <functional>
22#include <iosfwd>
23#include <memory>
24#include <string>
25
26namespace egt
27{
28inline namespace v1
29{
30
31namespace detail
32{
33class InternalContext;
34}
35
36class Image;
37class Surface;
38class Widget;
39
53class EGT_API Painter
54{
55public:
56
60 enum class AntiAlias
61 {
62 system, // default
63 none,
64 gray,
65 subpixel,
66 fast,
67 good,
68 best,
69 };
70
74 enum class LineCap
75 {
76 butt,
77 round,
78 square,
79 };
80
85 using SubordinateFilter = std::function<bool(const Widget&)>;
86
104 {
105 explicit AutoSaveRestore(Painter& painter)
106 : m_painter(painter)
107 {
108 m_painter.save();
109 }
110
115
117 {
118 m_painter.restore();
119 }
120
122 };
123
138 {
139 explicit AutoGroup(Painter& painter)
140 : m_painter(painter)
141 {
142 m_painter.push_group();
143 }
144
145 AutoGroup(const AutoGroup&) = delete;
146 AutoGroup& operator=(const AutoGroup&) = delete;
147 AutoGroup(AutoGroup&&) = delete;
149
151 {
152 m_painter.pop_group();
153 }
154
156 };
157
158 Painter() = delete;
159
160 explicit Painter(Surface& surface) noexcept;
162
168 void save();
169
175 void restore();
176
184
192
201 EGT_NODISCARD bool filter_subordinate(const Widget& subordinate) const;
202
211
218
225
231 void pop_group();
232
236 Painter& set(const Pattern& pattern);
237
241 Painter& set(const Font& font);
242
248 Painter& line_width(float width);
249
256
260 EGT_NODISCARD Painter::LineCap line_cap() const;
261
269 Painter& set_dash(const double* dashes, size_t num_dashes, double offset);
270
277
281 EGT_NODISCARD Painter::AntiAlias antialias() const;
282
291 Painter& alpha_blending(bool enabled);
292
296 EGT_NODISCARD bool alpha_blending() const;
297
303 template<class T>
305 {
306 return move_to(point);
307 }
308
309 template<class T>
311 {
312 return line_to(point);
313 }
314
321 template<class T>
322 Painter& draw(const T& start, const T& end)
323 {
324 return move_to(start).line_to(end);
325 }
326
332 template<class T>
334 {
335 return move_to(line.start()).line_to(line.end());
336 }
337
343 template<class T>
345 {
346 return rectangle(rect);
347 }
348
354 template<class T>
356 {
357 return this->arc(arc);
358 }
359
363 Painter& draw(const Image& image);
364
368 Painter& source(const Pattern& pattern);
369
373 Painter& source(const Color& color);
374
378 Painter& source(const Surface& surface, const PointF& point = {});
379
383 Painter& source(const Image& image, const PointF& point = {});
384
388 Painter& mask(const Surface& surface, const PointF& point = {});
389
390 Painter& mask(const Image& image, const Point& point = {});
391
397 Painter& draw(const Color& color, const RectF& rect = {}, bool preserve = false);
398
404 Painter& draw(const Pattern& pattern, const RectF& rect = {}, bool preserve = false);
405
411 Painter& draw(const Surface& surface, const PointF& point, const RectF& rect = {});
412
418 Painter& draw(const Image& image, const PointF& point, const RectF& rect = {});
419
420 enum class TextDrawFlag : uint32_t
421 {
422 shadow = detail::bit(0),
423 };
424
426
430 Painter& draw(const std::string& str, const TextDrawFlags& flags = {});
431
433
435
437
439
440 Painter& paint(float alpha);
441
443
444 Painter& move_to(const PointF& point);
445
446 Painter& line_to(const PointF& point);
447
448 Painter& rectangle(const RectF& rect);
449
450 Painter& arc(const ArcF& arc);
451
452 Painter& translate(const PointF& point);
453
454 Painter& translate(const Point& point);
455
456 Painter& scale(float sx, float sy);
457
458 Painter& rotate(float angle);
459
460 Painter& show_text(const char* utf8);
461
462 Painter& show_text(const std::string& str) { return show_text(str.c_str()); }
463
468 EGT_NODISCARD Font::FontExtents extents() const;
469
476 EGT_NODISCARD Font::TextExtents extents(const std::string& text) const;
477
478 Size text_size(const std::string& text);
479
480 void color_at(const Point& point, const Color& color) noexcept;
481 Color color_at(const Point& point) noexcept;
482
483 Painter& flood(const Point& point, const Color& color);
484
488 EGT_NODISCARD const detail::InternalContext& context() const { return *m_cr; }
489
493 EGT_NODISCARD const Surface& target() const { return m_surface; }
494
495 EGT_NODISCARD Surface& target() { return m_surface; }
496
509 void sync_for_cpu(bool skip_source = false) const;
510
511protected:
512
517
521 std::unique_ptr<detail::InternalContext> m_cr;
522
524};
525
527EGT_API std::ostream& operator<<(std::ostream& os, const Painter::LineCap& cap);
528
530EGT_API std::ostream& operator<<(std::ostream& os, const Painter::AntiAlias& antialias);
531
532}
533}
534
535#endif
An Arc consists of a radius and two angles.
Definition geometry.h:1136
32 bit RGBA color.
Definition color.h:41
Utility class for managing a set of flags with the ability to observe changes to the flags.
Definition flags.h:40
Manages a font and properties of a font.
Definition font.h:41
Font extent.
Definition font.h:50
Text extent.
Definition font.h:62
Raster image resource used for drawing or displaying.
Definition image.h:38
A line, with a starting and ending point.
Definition geometry.h:1056
EGT_NODISCARD constexpr PointType< Dim > start() const noexcept
Get the start point of the line.
Definition geometry.h:1074
EGT_NODISCARD constexpr PointType< Dim > end() const noexcept
Get the end point of the line.
Definition geometry.h:1076
Drawing interface for 2D graphics.
Definition painter.h:54
Painter & draw(const Image &image)
Draw an image surface at the specified point.
Painter & paint(float alpha)
Painter & source(const Color &color)
Set the source pattern from a Color.
std::unique_ptr< detail::InternalContext > m_cr
Internal context.
Definition painter.h:521
void low_fidelity()
Configure low fidelity options.
Painter & flood(const Point &point, const Color &color)
Painter & antialias(Painter::AntiAlias value)
Set the type of anti-aliasing.
void push_group()
Push a group onto the stack.
Painter & draw(const Color &color, const RectF &rect={}, bool preserve=false)
Painter & paint()
EGT_NODISCARD Font::FontExtents extents() const
Get the font extents based on the current context, hence taking into account transformations such as ...
Painter & draw(const std::string &str, const TextDrawFlags &flags={})
Draw text inside the specified rectangle.
EGT_NODISCARD Surface & target()
Definition painter.h:495
Color color_at(const Point &point) noexcept
EGT_NODISCARD bool filter_subordinate(const Widget &subordinate) const
Apply the current subordinate filter.
Painter & set(const Pattern &pattern)
Set the current color.
Painter & fill_preserve()
AntiAlias
Supported types of anti-aliasing.
Definition painter.h:61
Painter & source(const Pattern &pattern)
Set the source pattern from a Pattern.
Painter & set(const Font &font)
Set the active font.
Painter & arc(const ArcF &arc)
Painter & draw(const ArcType< T > &arc)
Create an arc.
Definition painter.h:355
SubordinateFilter set_subordinate_filter(const SubordinateFilter &subordinate_filter)
Set the subordinate filter (nullptr to remove the current filter).
Painter & source(const Surface &surface, const PointF &point={})
Set the source pattern from a Surface.
Painter & show_text(const std::string &str)
Definition painter.h:462
LineCap
Supported line caps.
Definition painter.h:75
Painter & line_width(float width)
Set the current line width.
TextDrawFlag
Definition painter.h:421
EGT_NODISCARD Painter::AntiAlias antialias() const
Get the current type of anti-aliasing.
Painter & draw(const RectType< T > &rect)
Create a rectangle.
Definition painter.h:344
Painter & fill()
Painter & draw(const PointType< T, detail::Compatible::normal > &point)
Move to a point.
Definition painter.h:304
Painter & source(const Image &image, const PointF &point={})
Set the source pattern from an Image.
void sync_for_cpu(bool skip_source=false) const
Claim the painter for being used by the CPU.
EGT_NODISCARD bool alpha_blending() const
Get the alpha blending state, either enabled or disabled.
Painter & move_to(const PointF &point)
Painter(Surface &surface) noexcept
Painter & draw(const Image &image, const PointF &point, const RectF &rect={})
Painter & alpha_blending(bool enabled)
Set the alpha blending state either enabled or disabled.
Surface & m_surface
Definition painter.h:523
Painter & rectangle(const RectF &rect)
std::function< bool(const Widget &)> SubordinateFilter
Return true if the subordinate widget is filtered out, hence should not be drawn by the painter.
Definition painter.h:85
SubordinateFilter m_subordinate_filter
Internal state.
Definition painter.h:516
Painter & translate(const PointF &point)
void save()
Save the state of the current context.
EGT_NODISCARD const detail::InternalContext & context() const
Get the current underlying context the painter is using.
Definition painter.h:488
Painter & stroke()
Painter & line_to(const PointF &point)
Painter & mask(const Surface &surface, const PointF &point={})
Draw an image as a mask.
Painter & draw(const Surface &surface, const PointF &point, const RectF &rect={})
Size text_size(const std::string &text)
Painter & line_cap(Painter::LineCap value)
Set the current line cap.
EGT_NODISCARD const Surface & target() const
Get the target surface the painter is using.
Definition painter.h:493
Painter & scale(float sx, float sy)
Painter & mask(const Image &image, const Point &point={})
Painter & translate(const Point &point)
void color_at(const Point &point, const Color &color) noexcept
EGT_NODISCARD Painter::LineCap line_cap() const
Get the current line cap.
Painter & draw(const Pattern &pattern, const RectF &rect={}, bool preserve=false)
EGT_NODISCARD Font::TextExtents extents(const std::string &text) const
Get the text extents based on the current context, hence taking into account transformations such as ...
void pop_group()
Pop a group off the stack and automatically make it the source.
Painter & draw(const T &start, const T &end)
Create a line from the start point to the end point.
Definition painter.h:322
Painter & line(const PointType< T, detail::Compatible::normal > &point)
Definition painter.h:310
Painter & set_dash(const double *dashes, size_t num_dashes, double offset)
Set the dash pattern to be used by stroke().
void high_fidelity()
Configure high fidelity options.
Painter & draw(const LineType< T > &line)
Create a line.
Definition painter.h:333
void restore_subordinate_filter(SubordinateFilter &&subordinate_filter)
Restore the subordinate filter from a previous value.
Painter & rotate(float angle)
Painter & clip()
Painter & show_text(const char *utf8)
void restore()
Restore the previous saved state of the current context.
A Pattern which can store one or more colors at different offsets (steps) which can be used to create...
Definition pattern.h:59
Simple x,y coordinate.
Definition geometry.h:63
A rectangle with a point and a size.
Definition geometry.h:595
Definition surface.h:34
Base Widget class.
Definition widget.h:53
EGT_API std::ostream & operator<<(std::ostream &os, const Color &color)
Overloaded std::ostream insertion operator.
EGT framework namespace.
Definition animation.h:24
You are encouraged to use this instead of manually calling Painter::push_group() and Painter::pop_gro...
Definition painter.h:138
Painter & m_painter
Definition painter.h:155
AutoGroup(Painter &painter)
Definition painter.h:139
AutoGroup & operator=(const AutoGroup &)=delete
AutoGroup & operator=(AutoGroup &&)=delete
AutoGroup(AutoGroup &&)=delete
AutoGroup(const AutoGroup &)=delete
~AutoGroup()
Definition painter.h:150
Scoped save() and restore() for a Painter.
Definition painter.h:104
AutoSaveRestore & operator=(const AutoSaveRestore &)=delete
Painter & m_painter
Definition painter.h:121
AutoSaveRestore(const AutoSaveRestore &)=delete
AutoSaveRestore & operator=(AutoSaveRestore &&)=delete
AutoSaveRestore(Painter &painter)
Definition painter.h:105
AutoSaveRestore(AutoSaveRestore &&)=delete
~AutoSaveRestore()
Definition painter.h:116