Boost.UI
User Interface Boost library
painter.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Kolya Kosenko
2 
3 // Distributed under the Boost Software License, Version 1.0.
4 // See http://www.boost.org/LICENSE_1_0.txt
5 
7 
8 #ifndef BOOST_UI_PAINTER_HPP
9 #define BOOST_UI_PAINTER_HPP
10 
11 #include <boost/ui/config.hpp>
12 
13 #ifdef BOOST_HAS_PRAGMA_ONCE
14 #pragma once
15 #endif
16 
17 #include <boost/ui/def.hpp>
18 #include <boost/ui/color.hpp>
19 #include <boost/ui/image.hpp>
20 #include <boost/ui/font.hpp>
21 #include <boost/ui/string.hpp>
22 
23 #include <boost/noncopyable.hpp>
24 #include <boost/core/scoped_enum.hpp>
25 
26 #include <vector>
27 #include <boost/range/begin.hpp>
28 #include <boost/range/end.hpp>
29 #include <boost/range/size.hpp>
30 
31 namespace boost {
32 namespace ui {
33 
34 #ifndef DOXYGEN
35 
36 namespace detail {
37 class painter_impl;
38 } // namespace detail
39 
40 #endif
41 
44 BOOST_SCOPED_ENUM_DECLARE_BEGIN(line_cap)
45 {
46  butt,
47  round,
48  square
49 }
51 BOOST_SCOPED_ENUM_DECLARE_END(line_cap)
52 
53 BOOST_SCOPED_ENUM_DECLARE_BEGIN(line_join)
56 {
57  round,
58  bevel,
59  miter
60 }
61 BOOST_SCOPED_ENUM_DECLARE_END(line_join)
62 
63 
69 class BOOST_UI_DECL painter
70 {
71  painter(detail::painter_impl* impl);
72 
73 public:
75  typedef double gcoord_type;
76 
79  { save_raw(); return *this; }
80 
83  { restore_raw(); return *this; }
84 
87  { scale_raw(x, y); return *this; }
88 
91  { rotate_raw(angle); return *this; }
92 
95  { translate_raw(x, y); return *this; }
96 
97  template <class T>
99  { return translate(p.x(), p.y()); }
101 
104  { fill_color_raw(c); return *this; }
105 
108  { stroke_color_raw(c); return *this; }
109 
112  { clear_rect_raw(x, y, width, height); return *this; }
113 
114  template <class T>
116  { return clear_rect(r.x(), r.y(), r.width(), r.height()); }
117 
118  template <class T>
120  { return clear_rect(point.x(), point.y(), size.width(), size.height()); }
121 
122  template <class T>
123  painter& clear_rect(const basic_point<T>& point1, const basic_point<T>& point2)
124  { return clear_rect(point1.x(), point1.y(), point2.x() - point1.x(), point2.y() - point1.y()); }
126 
129  { fill_rect_raw(x, y, width, height); return *this; }
130 
131  template <class T>
133  { return fill_rect(r.x(), r.y(), r.width(), r.height()); }
134 
135  template <class T>
137  { return fill_rect(point.x(), point.y(), size.width(), size.height()); }
138 
139  template <class T>
140  painter& fill_rect(const basic_point<T>& point1, const basic_point<T>& point2)
141  { return fill_rect(point1.x(), point1.y(), point2.x() - point1.x(), point2.y() - point1.y()); }
143 
146  { stroke_rect_raw(x, y, width, height); return *this; }
147 
148  template <class T>
150  { return stroke_rect(r.x(), r.y(), r.width(), r.height()); }
151 
152  template <class T>
154  { return stroke_rect(point.x(), point.y(), size.width(), size.height()); }
155 
156  template <class T>
157  painter& stroke_rect(const basic_point<T>& point1, const basic_point<T>& point2)
158  { return stroke_rect(point1.x(), point1.y(), point2.x() - point1.x(), point2.y() - point1.y()); }
160 
163  { fill_text_raw(text, x, y); return *this; }
164 
165  template <class T>
166  painter& fill_text(const uistring& text, const basic_point<T>& p)
167  { return fill_text(text, p.x(), p.y()); }
169 
172  { draw_image_raw(img, dx, dy); return *this; }
173 
174  template <class T>
175  painter& draw_image(const image& img, const basic_point<T>& p)
176  { return draw_image(img, p.x(), p.y()); }
178 
181  { begin_path_raw(); return *this; }
182 
185  { fill_raw(); return *this; }
186 
189  { stroke_raw(); return *this; }
190 
193  { line_width_raw(width); return *this; }
194 
197  { line_cap_raw(lc); return *this; }
198 
201  { line_join_raw(lj); return *this; }
202 
204  template <class Iterator>
205  painter& line_dash(Iterator first, Iterator last)
206  {
207  std::vector<gcoord_type> segments;
208  std::copy(first, last, std::back_inserter(segments));
209  line_dash_raw(segments);
210  return *this;
211  }
212 
213  template <class Range>
214  painter& line_dash(const Range& r)
215  { return line_dash(boost::begin(r), boost::end(r)); }
216 
217 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
218  template <class T>
219  painter& line_dash(std::initializer_list<T> list)
220  {
221  std::vector<gcoord_type> segments;
222  segments.reserve(list.size());
223  std::copy(list.begin(), list.end(), std::back_inserter(segments));
224  line_dash_raw(segments);
225  return *this;
226  }
227 #endif
228 
231  painter& line_dash(nullopt_t) { reset_line_dash_raw(); return *this; }
232  painter& reset_line_dash() { reset_line_dash_raw(); return *this; }
234 
236  painter& font(const ui::font& f)
237  { font_raw(f); return *this; }
238 
240  ui::font font() const;
241 
244  { close_path_raw(); return *this; }
245 
248  { move_to_raw(x, y); return *this; }
249 
250  template <class T>
252  { return move_to(p.x(), p.y()); }
254 
257  { line_to_raw(x, y); return *this; }
258 
259  template <class T>
261  { return line_to(p.x(), p.y()); }
263 
267  { quadratic_curve_to_raw(cpx, cpy, x, y); return *this; }
268 
269  template <class T>
271  { return quadratic_curve_to(cp.x(), cp.y(), p.x(), p.y()); }
273 
276  gcoord_type cp2x, gcoord_type cp2y,
278  { bezier_curve_to_raw(cp1x, cp1y, cp2x, cp2y, x, y); return *this; }
279 
280  template <class T>
282  const basic_point<T>& cp2,
283  const basic_point<T>& p)
284  { return bezier_curve_to(cp1.x(), cp1.y(), cp2.x(), cp2.y(), p.x(), p.y()); }
286 
289  gcoord_type start_angle, gcoord_type end_angle, bool anticlockwise = false)
290  { arc_raw(x, y, radius, start_angle, end_angle, anticlockwise); return *this; }
291 
292  template <class T>
294  gcoord_type start_angle, gcoord_type end_angle, bool anticlockwise = false)
295  { return arc(p.x(), p.y(), radius, start_angle, end_angle, anticlockwise); }
297 
300  { rect_raw(x, y, w, h); return *this; }
301 
302  template <class T>
304  { return rect(r.x(), r.y(), r.width(), r.height()); }
305 
306  template <class T>
308  { return rect(point.x(), point.y(), size.width(), size.height()); }
309 
310  template <class T>
311  painter& rect(const basic_point<T>& point1, const basic_point<T>& point2)
312  { return rect(point1.x(), point1.y(), point2.x() - point1.x(), point2.y() - point1.y()); }
314 
315  class state_saver;
316 
318  typedef void* native_handle_type;
319 
321  native_handle_type native_handle();
323 
324 private:
325  void save_raw();
326  void restore_raw();
327  void scale_raw(gcoord_type x, gcoord_type y);
328  void rotate_raw(gcoord_type angle);
329  void translate_raw(gcoord_type x, gcoord_type y);
330  void fill_color_raw(const color& c);
331  void stroke_color_raw(const color& c);
332  void clear_rect_raw(gcoord_type x, gcoord_type y, gcoord_type width, gcoord_type height);
333  void fill_rect_raw(gcoord_type x, gcoord_type y, gcoord_type width, gcoord_type height);
334  void stroke_rect_raw(gcoord_type x, gcoord_type y, gcoord_type width, gcoord_type height);
335  void fill_text_raw(const uistring& text, gcoord_type x, gcoord_type y);
336  void draw_image_raw(const image& img, gcoord_type dx, gcoord_type dy);
337  void begin_path_raw();
338  void fill_raw();
339  void stroke_raw();
340  void line_width_raw(gcoord_type width);
341  void line_cap_raw(ui::line_cap lc);
342  void line_join_raw(ui::line_join lj);
343  void line_dash_raw(const std::vector<gcoord_type>& segments);
344  void reset_line_dash_raw();
345  void font_raw(const ui::font& f);
346  void close_path_raw();
347  void move_to_raw(gcoord_type x, gcoord_type y);
348  void line_to_raw(gcoord_type x, gcoord_type y);
349  void quadratic_curve_to_raw(gcoord_type cpx, gcoord_type cpy,
350  gcoord_type x, gcoord_type y);
351  void bezier_curve_to_raw(gcoord_type cp1x, gcoord_type cp1y,
352  gcoord_type cp2x, gcoord_type cp2y,
353  gcoord_type x, gcoord_type y);
354  void arc_raw(gcoord_type x, gcoord_type y, gcoord_type radius,
355  gcoord_type start_angle, gcoord_type end_angle, bool anticlockwise);
356  void rect_raw(gcoord_type x, gcoord_type y, gcoord_type w, gcoord_type h);
357 
358  detail::painter_impl* m_impl;
359 
360  friend class canvas;
361 };
362 
365 
366 class painter::state_saver : private boost::noncopyable
367 {
368 public:
370  state_saver(painter& c) : m_painter(c) { m_painter.save(); }
371 
373  ~state_saver() { m_painter.restore(); }
374 
375 private:
376  painter& m_painter;
377 };
378 
382 
386 
390 
391 } // namespace ui
392 } // namespace boost
393 
394 #endif // BOOST_UI_PAINTER_HPP
boost::ui::painter::line_dash
painter & line_dash(const Range &r)
Sets line dashes.
Definition: painter.hpp:214
boost::ui::painter::line_dash
painter & line_dash(nullopt_t)
Resets line dashes.
Definition: painter.hpp:231
boost::ui::painter::bezier_curve_to
painter & bezier_curve_to(gcoord_type cp1x, gcoord_type cp1y, gcoord_type cp2x, gcoord_type cp2y, gcoord_type x, gcoord_type y)
Creates cubic Bezier curve with control points (cp1x, cp1y) and (cp2x, cp2y)
Definition: painter.hpp:275
boost::ui::uistring
Helper class to convert string between UI and application logic only.
Definition: string.hpp:49
boost::ui::painter::fill_rect
painter & fill_rect(const basic_point< T > &point, const basic_size< T > &size)
Paints the given rectangle onto the painter, using the current fill style.
Definition: painter.hpp:136
boost::ui::gsize
basic_size< painter::gcoord_type > gsize
2D size geometry coordinates
Definition: painter.hpp:381
boost::ui::painter::quadratic_curve_to
painter & quadratic_curve_to(gcoord_type cpx, gcoord_type cpy, gcoord_type x, gcoord_type y)
Creates quadratic Bezier curve with control point (cpx, cpy)
Definition: painter.hpp:265
boost::ui::basic_point::y
T y() const
Returns y coordinate.
Definition: coord.hpp:179
boost::ui::painter::font
painter & font(const ui::font &f)
Sets font.
Definition: painter.hpp:236
boost::ui::painter::native_handle_type
void * native_handle_type
Implementation-defined painter type.
Definition: painter.hpp:315
boost::ui::painter::line_width
painter & line_width(gcoord_type width)
Sets line width (default is 1)
Definition: painter.hpp:192
boost::ui::rect
basic_rect< coord_type > rect
2D rectangle coordinates
Definition: coord.hpp:424
boost::ui::painter::draw_image
painter & draw_image(const image &img, gcoord_type dx, gcoord_type dy)
Draws the given image onto the painter.
Definition: painter.hpp:171
boost::ui::basic_point
2D point data structure with custom coordinates type
Definition: coord.hpp:119
boost::ui::painter::line_to
painter & line_to(const basic_point< T > &p)
Connects the last point in the subpath to the specified point using a straight line.
Definition: painter.hpp:260
config.hpp
Configuration options.
boost::ui::painter::clear_rect
painter & clear_rect(const basic_point< T > &point1, const basic_point< T > &point2)
Clears all pixels on the painter in the given rectangle to transparent black.
Definition: painter.hpp:123
boost::ui::painter::fill_rect
painter & fill_rect(const basic_rect< T > &r)
Paints the given rectangle onto the painter, using the current fill style.
Definition: painter.hpp:132
boost::ui::painter::quadratic_curve_to
painter & quadratic_curve_to(const basic_point< T > &cp, const basic_point< T > &p)
Creates quadratic Bezier curve with control point (cpx, cpy)
Definition: painter.hpp:270
font.hpp
Font class.
boost::ui::painter::stroke_color
painter & stroke_color(const color &c)
Sets the current color used for filling shapes.
Definition: painter.hpp:107
boost::ui::painter::line_to
painter & line_to(gcoord_type x, gcoord_type y)
Connects the last point in the subpath to the specified point using a straight line.
Definition: painter.hpp:256
boost::ui::painter::bezier_curve_to
painter & bezier_curve_to(const basic_point< T > &cp1, const basic_point< T > &cp2, const basic_point< T > &p)
Creates cubic Bezier curve with control points (cp1x, cp1y) and (cp2x, cp2y)
Definition: painter.hpp:281
def.hpp
Definitions.
boost::ui::basic_rect::width
T width() const
Returns width.
Definition: coord.hpp:372
boost
Boost C++ libraries namespace.
Definition: window.hpp:19
boost::ui::painter::arc
painter & arc(gcoord_type x, gcoord_type y, gcoord_type radius, gcoord_type start_angle, gcoord_type end_angle, bool anticlockwise=false)
Creates an arc.
Definition: painter.hpp:288
boost::ui::painter::clear_rect
painter & clear_rect(const basic_rect< T > &r)
Clears all pixels on the painter in the given rectangle to transparent black.
Definition: painter.hpp:115
boost::ui::painter::stroke_rect
painter & stroke_rect(gcoord_type x, gcoord_type y, gcoord_type width, gcoord_type height)
Paints the box that outlines the given rectangle onto the painter, using the current stroke style.
Definition: painter.hpp:145
boost::ui::painter::stroke_rect
painter & stroke_rect(const basic_rect< T > &r)
Paints the box that outlines the given rectangle onto the painter, using the current stroke style.
Definition: painter.hpp:149
boost::ui::painter::state_saver::state_saver
state_saver(painter &c)
Saves current painter state in the stack.
Definition: painter.hpp:370
boost::ui::painter::scale
painter & scale(gcoord_type x, gcoord_type y)
Adds scaling transformation.
Definition: painter.hpp:86
boost::ui::basic_rect::height
T height() const
Returns height.
Definition: coord.hpp:383
boost::ui::line_cap::round
@ round
semi-circle with the diameter equal to the line width is added
boost::ui::painter::fill_rect
painter & fill_rect(const basic_point< T > &point1, const basic_point< T > &point2)
Paints the given rectangle onto the painter, using the current fill style.
Definition: painter.hpp:140
boost::ui::painter::gcoord_type
double gcoord_type
Graphics coordinates signed number type.
Definition: painter.hpp:75
boost::ui::painter::stroke_rect
painter & stroke_rect(const basic_point< T > &point1, const basic_point< T > &point2)
Paints the box that outlines the given rectangle onto the painter, using the current stroke style.
Definition: painter.hpp:157
boost::ui::gpoint
basic_point< painter::gcoord_type > gpoint
2D point geometry coordinates
Definition: painter.hpp:385
boost::ui::basic_point::x
T x() const
Returns x coordinate.
Definition: coord.hpp:171
string.hpp
String class and operations.
boost::ui::basic_rect::x
T x() const
Returns x coordinate.
Definition: coord.hpp:352
boost::ui::painter::stroke_rect
painter & stroke_rect(const basic_point< T > &point, const basic_size< T > &size)
Paints the box that outlines the given rectangle onto the painter, using the current stroke style.
Definition: painter.hpp:153
boost::ui::painter::fill_text
painter & fill_text(const uistring &text, const basic_point< T > &p)
Fills the given text at the given position.
Definition: painter.hpp:166
boost::ui::painter::move_to
painter & move_to(const basic_point< T > &p)
Creates a new subpath with the specified point as its first (and only) point.
Definition: painter.hpp:251
boost::ui::line_cap
line_cap
Enumaration of line endings types.
Definition: painter.hpp:44
boost::ui::painter::translate
painter & translate(gcoord_type x, gcoord_type y)
Adds translation transformation.
Definition: painter.hpp:94
boost::ui::painter::move_to
painter & move_to(gcoord_type x, gcoord_type y)
Creates a new subpath with the specified point as its first (and only) point.
Definition: painter.hpp:247
boost::ui::painter::rotate
painter & rotate(gcoord_type angle)
Adds rotation transformation.
Definition: painter.hpp:90
boost::ui::painter::translate
painter & translate(const basic_point< T > &p)
Adds translation transformation.
Definition: painter.hpp:98
boost::ui::painter::close_path
painter & close_path()
Connects the last point to the first point in the subpath.
Definition: painter.hpp:243
boost::ui::line_cap::butt
@ butt
no additional line cap is added
boost::ui::line_join
line_join
Enumaration of line join types.
Definition: painter.hpp:55
boost::ui::painter::draw_image
painter & draw_image(const image &img, const basic_point< T > &p)
Draws the given image onto the painter.
Definition: painter.hpp:175
boost::ui::basic_size::height
const T & height() const
Retruns height.
Definition: coord.hpp:74
boost::ui::painter::fill_color
painter & fill_color(const color &c)
Sets the current color used for filling shapes.
Definition: painter.hpp:103
boost::ui::grect
basic_rect< painter::gcoord_type > grect
2D rectangle geometry coordinates
Definition: painter.hpp:389
boost::ui::basic_size::width
const T & width() const
Retruns width.
Definition: coord.hpp:71
boost::ui::painter::line_join
painter & line_join(ui::line_join lj)
Sets type of line join, default value is miter.
Definition: painter.hpp:200
image.hpp
Image class.
color.hpp
Color class.
boost::ui::painter::state_saver
Saves state in the constructor and restores it in the destructor.
Definition: painter.hpp:366
boost::ui::basic_size
2D size of object data structure with custom coordinates type
Definition: coord.hpp:48
boost::ui::painter::save
painter & save()
Push state.
Definition: painter.hpp:78
boost::ui::basic_rect::y
T y() const
Returns y coordinate.
Definition: coord.hpp:362
boost::ui::canvas
Widget for drawing.
Definition: canvas.hpp:29
boost::ui::painter::line_dash
painter & line_dash(std::initializer_list< T > list)
Sets line dashes.
Definition: painter.hpp:219
boost::ui::painter::fill_text
painter & fill_text(const uistring &text, gcoord_type x, gcoord_type y)
Fills the given text at the given position.
Definition: painter.hpp:162
boost::ui::painter::restore
painter & restore()
Pop state.
Definition: painter.hpp:82
boost::ui::painter::begin_path
painter & begin_path()
Resets the current path.
Definition: painter.hpp:180
boost::ui::painter::clear_rect
painter & clear_rect(const basic_point< T > &point, const basic_size< T > &size)
Clears all pixels on the painter in the given rectangle to transparent black.
Definition: painter.hpp:119
boost::ui::painter::rect
painter & rect(const basic_rect< T > &r)
Creates rectangular closed subpath.
Definition: painter.hpp:303
boost::ui::basic_rect
2D rectangle plane figure data structure with custom coordinates type
Definition: coord.hpp:259
boost::ui::painter::line_dash
painter & line_dash(Iterator first, Iterator last)
Sets line dashes.
Definition: painter.hpp:205
boost::ui::painter::rect
painter & rect(const basic_point< T > &point, const basic_size< T > &size)
Creates rectangular closed subpath.
Definition: painter.hpp:307
boost::ui::color
Color class.
Definition: color.hpp:31
boost::ui::painter::line_cap
painter & line_cap(ui::line_cap lc)
Sets type of line endings, default value is butt.
Definition: painter.hpp:196
boost::ui::line_cap::square
@ square
boost::ui::painter::state_saver::~state_saver
~state_saver()
Restores painter state from the stack.
Definition: painter.hpp:373
boost::ui::painter::stroke
painter & stroke()
Strokes the current path.
Definition: painter.hpp:188
boost::ui::painter
2D graphics rendering painter
Definition: painter.hpp:69
boost::ui::painter::arc
painter & arc(const basic_point< T > &p, gcoord_type radius, gcoord_type start_angle, gcoord_type end_angle, bool anticlockwise=false)
Creates an arc.
Definition: painter.hpp:293
boost::ui::painter::fill_rect
painter & fill_rect(gcoord_type x, gcoord_type y, gcoord_type width, gcoord_type height)
Paints the given rectangle onto the painter, using the current fill style.
Definition: painter.hpp:128
boost::ui::image
Image class.
Definition: image.hpp:28
boost::ui::painter::rect
painter & rect(const basic_point< T > &point1, const basic_point< T > &point2)
Creates rectangular closed subpath.
Definition: painter.hpp:311
boost::ui::painter::fill
painter & fill()
Fills the current path.
Definition: painter.hpp:184
boost::ui::painter::rect
painter & rect(gcoord_type x, gcoord_type y, gcoord_type w, gcoord_type h)
Creates rectangular closed subpath.
Definition: painter.hpp:299
boost::ui::font
Font class.
Definition: font.hpp:30
boost::ui::painter::clear_rect
painter & clear_rect(gcoord_type x, gcoord_type y, gcoord_type width, gcoord_type height)
Clears all pixels on the painter in the given rectangle to transparent black.
Definition: painter.hpp:111
boost::ui::painter::reset_line_dash
painter & reset_line_dash()
Resets line dashes.
Definition: painter.hpp:232