1.10
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 <cairo.h>
15#include <egt/color.h>
16#include <egt/detail/meta.h>
17#include <egt/flags.h>
18#include <egt/font.h>
19#include <egt/geometry.h>
20#include <egt/pattern.h>
21#include <egt/types.h>
22#include <string>
23
24namespace egt
25{
26inline namespace v1
27{
28
29class Image;
30
44class EGT_API Painter
45{
46public:
47
65 {
66 explicit AutoSaveRestore(Painter& painter)
67 : m_painter(painter)
68 {
69 m_painter.save();
70 }
71
76
78 {
79 m_painter.restore();
80 }
81
83 };
84
98 struct AutoGroup
99 {
100 explicit AutoGroup(Painter& painter)
101 : m_painter(painter)
102 {
103 m_painter.push_group();
104 }
105
106 AutoGroup(const AutoGroup&) = delete;
107 AutoGroup& operator=(const AutoGroup&) = delete;
108 AutoGroup(AutoGroup&&) = delete;
110
112 {
113 m_painter.pop_group();
114 }
115
117 };
118
119 Painter() = delete;
120
129 explicit Painter(shared_cairo_t cr) noexcept;
130
136 void save();
137
143 void restore();
144
151
157 void pop_group();
158
162 Painter& set(const Pattern& pattern);
163
167 Painter& set(const Font& font);
168
174 Painter& line_width(float width);
175
181 template<class T>
183 {
184 cairo_move_to(m_cr.get(), point.x(), point.y());
185
186 return *this;
187 }
188
189 template<class T>
191 {
192 cairo_line_to(m_cr.get(), point.x(), point.y());
193
194 return *this;
195 }
196
203 template<class T>
204 Painter& draw(const T& start, const T& end)
205 {
206 cairo_move_to(m_cr.get(), start.x(), start.y());
207 cairo_line_to(m_cr.get(), end.x(), end.y());
208
209 return *this;
210 }
211
217 template<class T>
219 {
220 cairo_move_to(m_cr.get(), line.start().x(), line.start().y());
221 cairo_line_to(m_cr.get(), line.end().x(), line.end().y());
222
223 return *this;
224 }
225
231 template<class T>
233 {
234 if (rect.empty())
235 return *this;
236
237 cairo_rectangle(m_cr.get(),
238 rect.x(),
239 rect.y(),
240 rect.width(),
241 rect.height());
242
243 return *this;
244 }
245
251 template<class T>
253 {
254 if (arc.empty())
255 return *this;
256
257 cairo_arc(m_cr.get(), arc.center().x(), arc.center().y(),
258 arc.radius(), arc.angle1(), arc.angle2());
259
260 return *this;
261 }
262
268 template<class T>
270 {
271 if (arc.empty())
272 return *this;
273
274 cairo_arc(m_cr.get(), arc.center().x(), arc.center().y(),
275 arc.radius(), arc.angle1(), arc.angle2());
276
277 return *this;
278 }
279
283 Painter& draw(const Image& image);
284
288 Painter& mask(const Image& image, const Point& point = {});
289
294 Painter& draw(const Rect& rect,
295 const Image& image);
296
297 enum class TextDrawFlag : uint32_t
298 {
299 shadow = detail::bit(0),
300 };
301
303
307 Painter& draw(const std::string& str, const TextDrawFlags& flags = {});
308
310
312
314
315 Painter& paint(float alpha);
316
318
319 Painter& translate(const PointF& point);
320
321 Painter& translate(const Point& point);
322
323 Painter& rotate(float angle);
324
325 Size text_size(const std::string& text);
326
327 void color_at(const Point& point, const Color& color) noexcept;
328 static void color_at(cairo_surface_t* image, const Point& point, const Color& color) noexcept;
329 Color color_at(const Point& point) noexcept;
330 static Color color_at(cairo_surface_t* image, const Point& point) noexcept;
331
332 Painter& flood(const Point& point, const Color& color);
333
334 static void flood(cairo_surface_t* image,
335 const Point& point, const Color& color);
336
340 EGT_NODISCARD inline shared_cairo_t context() const
341 {
342 return m_cr;
343 }
344
348 static inline Size surface_to_size(const shared_cairo_surface_t& surface)
349 {
350 return {cairo_image_surface_get_width(surface.get()),
351 cairo_image_surface_get_height(surface.get())};
352 }
353
357 static inline Size surface_to_size(cairo_surface_t* surface)
358 {
359 return {cairo_image_surface_get_width(surface),
360 cairo_image_surface_get_height(surface)};
361 }
362
363protected:
364
369};
370
371}
372}
373
374#endif
An Arc consists of a radius and two angles.
Definition geometry.h:1123
constexpr void radius(Dim radius) noexcept
Set the radius.
Definition geometry.h:1154
constexpr void angle1(float angle) noexcept
Set the angle1.
Definition geometry.h:1156
EGT_NODISCARD EGT_API bool empty() const noexcept
Returns true if the arc has no radius.
constexpr void center(const PointType< Dim > &center) noexcept
Set the center point.
Definition geometry.h:1160
constexpr void angle2(float angle) noexcept
Set the angle2.
Definition geometry.h:1158
A basic circle with a center point and radius.
Definition geometry.h:1204
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:35
Raster image resource used for drawing or displaying.
Definition image.h:39
A line, with a starting and ending point.
Definition geometry.h:1043
EGT_NODISCARD constexpr PointType< Dim > start() const noexcept
Get the start point of the line.
Definition geometry.h:1061
EGT_NODISCARD constexpr PointType< Dim > end() const noexcept
Get the end point of the line.
Definition geometry.h:1063
Drawing interface for 2D graphics.
Definition painter.h:45
Painter & draw(const Image &image)
Draw an image surface at the specified point.
Painter & paint(float alpha)
Painter & draw(const Rect &rect, const Image &image)
Painter & flood(const Point &point, const Color &color)
void push_group()
Push a group onto the stack.
Painter & paint()
Painter & draw(const std::string &str, const TextDrawFlags &flags={})
Draw text inside the specified rectangle.
Color color_at(const Point &point) noexcept
Painter & draw(const CircleType< T > &arc)
Create a circle.
Definition painter.h:269
Painter & set(const Pattern &pattern)
Set the current color.
static Color color_at(cairo_surface_t *image, const Point &point) noexcept
Painter & set(const Font &font)
Set the active font.
Painter(shared_cairo_t cr) noexcept
Construct a Painter with an existing context.
Painter & draw(const ArcType< T > &arc)
Create an arc.
Definition painter.h:252
static void color_at(cairo_surface_t *image, const Point &point, const Color &color) noexcept
Painter & line_width(float width)
Set the current line width.
TextDrawFlag
Definition painter.h:298
shared_cairo_t m_cr
Cairo context.
Definition painter.h:368
Painter & draw(const RectType< T > &rect)
Create a rectangle.
Definition painter.h:232
Painter & fill()
Painter & draw(const PointType< T, detail::Compatible::normal > &point)
Move to a point.
Definition painter.h:182
Painter & translate(const PointF &point)
void save()
Save the state of the current context.
Painter & stroke()
Size text_size(const std::string &text)
static Size surface_to_size(cairo_surface_t *surface)
Get a Size from a surface.
Definition painter.h:357
Painter & mask(const Image &image, const Point &point={})
Draw an image as a mask.
Painter & translate(const Point &point)
void color_at(const Point &point, const Color &color) noexcept
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:204
Painter & line(const PointType< T, detail::Compatible::normal > &point)
Definition painter.h:190
EGT_NODISCARD shared_cairo_t context() const
Get the current underlying context the painter is using.
Definition painter.h:340
Painter & draw(const LineType< T > &line)
Create a line.
Definition painter.h:218
Painter & rotate(float angle)
Painter & clip()
static void flood(cairo_surface_t *image, const Point &point, const Color &color)
void restore()
Restore the previous saved state of the current context.
static Size surface_to_size(const shared_cairo_surface_t &surface)
Get a Size from a surface.
Definition painter.h:348
A Pattern which can store one or more colors at different offsets (steps) which can be used to create...
Definition pattern.h:55
Simple x,y coordinate.
Definition geometry.h:63
EGT_NODISCARD constexpr Dim x() const noexcept
Get the x value.
Definition geometry.h:192
EGT_NODISCARD constexpr Dim y() const noexcept
Get the y value.
Definition geometry.h:194
A rectangle with a point and a size.
Definition geometry.h:595
EGT_NODISCARD constexpr Dim width() const noexcept
Get the width value.
Definition geometry.h:913
EGT_NODISCARD constexpr Dim x() const noexcept
Get the x value.
Definition geometry.h:903
EGT_NODISCARD constexpr bool empty() const noexcept
Returns true if the rectangle has no width or height.
Definition geometry.h:829
EGT_NODISCARD constexpr Dim height() const noexcept
Get the height value.
Definition geometry.h:915
EGT_NODISCARD constexpr Dim y() const noexcept
Get the y value.
Definition geometry.h:905
std::shared_ptr< cairo_surface_t > shared_cairo_surface_t
Shared pointer for a cairo surface.
Definition types.h:29
std::shared_ptr< cairo_t > shared_cairo_t
Shared pointer for a cairo context.
Definition types.h:35
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:99
Painter & m_painter
Definition painter.h:116
AutoGroup(Painter &painter)
Definition painter.h:100
AutoGroup & operator=(const AutoGroup &)=delete
AutoGroup & operator=(AutoGroup &&)=delete
AutoGroup(AutoGroup &&)=delete
AutoGroup(const AutoGroup &)=delete
~AutoGroup()
Definition painter.h:111
Scoped save() and restore() for a Painter.
Definition painter.h:65
AutoSaveRestore & operator=(const AutoSaveRestore &)=delete
Painter & m_painter
Definition painter.h:82
AutoSaveRestore(const AutoSaveRestore &)=delete
AutoSaveRestore & operator=(AutoSaveRestore &&)=delete
AutoSaveRestore(Painter &painter)
Definition painter.h:66
AutoSaveRestore(AutoSaveRestore &&)=delete
~AutoSaveRestore()
Definition painter.h:77