Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
designer.hpp
1 /*
2  Title --- designer.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_DESIGNER_HPP_
23 #define _RPI_HW_DESIGNER_HPP_
24 
25 #include <algorithm>
26 #include <vector>
27 #include <locale>
28 
29 #include <rpi-hw/types.hpp>
30 #include <rpi-hw/exception.hpp>
31 #include <rpi-hw/math.hpp>
32 
33 #include <rpi-hw/font/glyph.hpp>
34 #include <rpi-hw/font/base.hpp>
35 #include <rpi-hw/image/base.hpp>
36 
37 namespace rpihw { // Begin main namespace
38 
39 /*!
40  @class designer
41  @brief The graphic designer.
42 */
43 template < typename T, typename C, uint8_t N = 3 >
44 class designer {
45 
46 public:
47 
48  //! Parameters of text.
49  enum TextFlags {
50 
51  ALIGN_LEFT = 0x01,
52  ALIGN_CENTER = 0x02,
53  ALIGN_RIGHT = 0x04,
54  WORD_WRAP = 0x08,
55  WORD_BREAK = 0x10
56  };
57 
58  /*!
59  @brief Constructor method.
60  @param[in] width The width of the drawing area.
61  @param[in] height The height of the drawing area.
62  */
63  designer( T width, T height );
64 
65  //! Destructor method.
66  virtual ~designer();
67 
68  /*!
69  @brief Sets the pen color.
70  @param[in] color A pointer to the color data.
71  */
72  void setPenColor( const std::vector< C > &color );
73 
74  /*!
75  @brief Gets the current pen color.
76  @return The pointer to the color data.
77  */
78  const std::vector< C > &getPenColor() const;
79 
80  /*!
81  @brief Sets the pen position.
82  @param[in] x The new horizontal pen position.
83  @param[in] y The new vertical pen position.
84  */
85  virtual void setPenPosition( T x, T y );
86 
87  /*!
88  @brief Sets the text font.
89  @param[in] font The new text font.
90  */
91  void setFont( font::base &font );
92 
93  /*!
94  @brief Returns the current text font.
95  @return The text font.
96  */
97  font::base &getFont() const;
98 
99  /*!
100  @brief Draws a point.
101  @param[in] x The horizontal position of the point.
102  @param[in] y The vertical position of the point.
103  */
104  void drawPoint( T x, T y );
105 
106  /*!
107  @brief Draws a line.
108  @param[in] x0 The horizontal position of the first point of the line.
109  @param[in] y0 The vertical position of the first point of the line.
110  @param[in] x1 The horizontal position of the second point of the line.
111  @param[in] y1 The vertical position of the second point of the line.
112  */
113  void drawLine( T x0, T y0, T x1, T y1 );
114 
115  /*!
116  @brief Draws a rectangle.
117  @param[in] x0 The horizontal position of the left-top vertex of the rectangle.
118  @param[in] y0 The vertical position of the left-top vertex of the rectangle.
119  @param[in] x1 The horizontal position of the right-bottom vertex of the rectangle.
120  @param[in] y1 The vertical position of the right-bottom vertex of the rectangle.
121  */
122  void drawRect( T x0, T y0, T x1, T y1 );
123 
124  /*!
125  @brief Draws a circle.
126  @param[in] cx The horizontal position of the center of the circle.
127  @param[in] cy The vertical position of the center of the circle.
128  @param[in] radius The radius of the circle.
129  */
130  void drawCircle( T cx, T cy, T radius );
131 
132  /*!
133  @brief Draws an ellipse.
134  @param[in] cx The horizontal position of the center of the ellipse.
135  @param[in] cy The vertical position of the center of the ellipse.
136  @param[in] a The parameters `a` of the ellipse.
137  @param[in] b The parameters `b` of the ellipse.
138  */
139  void drawEllipse( T cx, T cy, T a, T b );
140 
141  /*!
142  @brief Draws a filled rectangle.
143  @param[in] x0 The horizontal position of the left-top vertex.
144  @param[in] y0 The vertical position of the left-top vertex.
145  @param[in] x1 The horizontal position of the right-bottom vertex.
146  @param[in] y1 The vertical position of the right-bottom vertex.
147  */
148  void fillRect( T x0, T y0, T x1, T y1 );
149 
150  /*!
151  @brief Draws a filled circle.
152  @param[in] cx The horizontal position of the center of the circle.
153  @param[in] cy The vertical position of the center of the circle.
154  @param[in] radius The radius of the circle.
155  */
156  void fillCircle( T cx, T cy, T radius );
157 
158  /*!
159  @brief Draws a filled ellipse.
160  @param[in] cx The horizontal position of the center of the ellipse.
161  @param[in] cy The vertical position of the center of the ellipse.
162  @param[in] a The parameters `a` of the ellipse.
163  @param[in] b The parameters `b` of the ellipse.
164  */
165  void fillEllipse( T cx, T cy, T a, T b );
166 
167  /*!
168  @brief Draws an image.
169  @param[in] img The image object.
170  */
171  void drawImage( const image::base< C > &img );
172 
173  /*!
174  @brief Moves the pen position and draws an image.
175  @param[in] x The horizontal position of the left-top vertex.
176  @param[in] y The vertical position of the left-top vertex.
177  @param[in] img The image object.
178  */
179  void drawImage( T x, T y, const image::base< C > &img );
180 
181  /*!
182  @brief Draws a character.
183  @param[in] charcode The character code.
184  */
185  void drawText( char32_t charcode );
186 
187  /*!
188  @brief Moves the pen position and draws a character.
189  @param[in] x The new horizontal pen position.
190  @param[in] y The new vertical pen position.
191  @param[in] charcode The character code.
192  */
193  void drawText( T x, T y, char32_t charcode );
194 
195  /*!
196  @brief Draws a string.
197  @param[in] it The start of the string.
198  @param[in] end The end of the string.
199  */
200  template < typename iterator >
201  void drawText( iterator it, iterator end );
202 
203  /*!
204  @brief Draws a multiline string.
205  @param[in] it The start of the string.
206  @param[in] end The end of the string.
207  @param[in] width The width of the text area.
208  @param[in] height The height of the text area.
209  @param[in] flags The parameters of the text.
210  */
211  template < typename iterator >
212  void drawText( iterator it, iterator end, T width, T height, uint8_t flags );
213 
214  /*!
215  @brief Moves the pen position and draws a string.
216  @param[in] x The new horizontal pen position.
217  @param[in] y The new vertical pen position.
218  @param[in] it The start of the string.
219  @param[in] end The end of the string.
220  */
221  template < typename iterator >
222  void drawText( T x, T y, iterator it, iterator end );
223 
224  /*!
225  @brief Moves the pen position and draws a multiline string.
226  @param[in] x The new horizontal pen position.
227  @param[in] y The new vertical pen position.
228  @param[in] it The start of the string.
229  @param[in] end The end of the string.
230  @param[in] width The width of the text area.
231  @param[in] height The height of the text area.
232  @param[in] flags The parameters of the text.
233  */
234  template < typename iterator >
235  void drawText( T x, T y, iterator it, iterator end, T width, T height, uint8_t flags );
236 
237  /*!
238  @brief Draws a string.
239  @param[in] text The string to be written.
240  */
241  void drawText( const std::string &text );
242 
243  /*!
244  @brief Draws a multiline string.
245  @param[in] text The string to be written.
246  @param[in] width The width of the text area.
247  @param[in] height The height of the text area.
248  @param[in] flags The parameters of the text.
249  */
250  void drawText( const std::string &text, T width, T height, uint8_t flags = ALIGN_LEFT );
251 
252  /*!
253  @brief Moves the pen position and draws a string.
254  @param[in] x The new horizontal pen position.
255  @param[in] y The new vertical pen position.
256  @param[in] text The string to be written.
257  */
258  void drawText( T x, T y, const std::string &text );
259 
260  /*!
261  @brief Moves the pen position and draws a multiline string.
262  @param[in] x The new horizontal pen position.
263  @param[in] y The new vertical pen position.
264  @param[in] text The string to be written.
265  @param[in] width The width of the text area.
266  @param[in] height The height of the text area.
267  @param[in] flags The parameters of the text.
268  */
269  void drawText( T x, T y, const std::string &text, T width, T height, uint8_t flags = ALIGN_LEFT );
270 
271  /*!
272  @brief Draws a unicode string.
273  @param[in] text The string to be written.
274  */
275  void drawText( const std::u32string &text );
276 
277  /*!
278  @brief Draws a multiline unicode string.
279  @param[in] text The string to be written.
280  @param[in] width The width of the text area.
281  @param[in] height The height of the text area.
282  @param[in] flags The parameters of the text.
283  */
284  void drawText( const std::u32string &text, T width, T height, uint8_t flags = ALIGN_LEFT );
285 
286  /*!
287  @brief Moves the pen position and draws a unicode string.
288  @param[in] x The new horizontal pen position.
289  @param[in] y The new vertical pen position.
290  @param[in] text The string to be written.
291  */
292  void drawText( T x, T y, const std::u32string &text );
293 
294  /*!
295  @brief Moves the pen position and draws a multiline unicode string.
296  @param[in] x The new horizontal pen position.
297  @param[in] y The new vertical pen position.
298  @param[in] text The string to be written.
299  @param[in] width The width of the text area.
300  @param[in] height The height of the text area.
301  @param[in] flags The parameters of the text.
302  */
303  void drawText( T x, T y, const std::u32string &text, T width, T height, uint8_t flags = ALIGN_LEFT );
304 
305 protected:
306 
307  //@{
308  //! Size of the drawing area.
309  T m_area_w, m_area_h;
310  //@}
311 
312  //@{
313  //! Pen position.
314  T m_pos_x, m_pos_y;
315  //@}
316 
317  //! Foreground color.
318  std::vector< C > m_color;
319 
320  //! Text font.
322 
323  /*!
324  @brief Draws a pixel.
325  @param[in] x The horizontal position of the pixel.
326  @param[in] y The vertical position of the pixel.
327  @param[in] color An iterator to the color data.
328  */
329  virtual void drawPixel( T x, T y, color_iterator< C > color ) = 0;
330 
331  /*!
332  @brief Draws a character.
333  @param[in] charcode The character code.
334  */
335  void drawChar( char32_t charcode );
336 
337  //! Draws the symmetric points of the circle.
338  void drawCirclePoints( T cx, T cy, T x, T y );
339 
340  //! Draws the symmetric areas of the circle.
341  void drawCircleAreas( T cx, T cy, T x, T y );
342 
343  //! Draws the symmetric points of the ellipse.
344  void drawEllipsePoints( T cx, T cy, T x, T y );
345 
346  //! Draws the symmetric areas of the ellipse.
347  void drawEllipseAreas( T cx, T cy, T x, T y );
348 };
349 
350 } // End of main namespace
351 
352 
353 // Include inline methods
354 #include <rpi-hw/designer-inl.hpp>
355 
356 #endif /* _RPI_HW_DESIGNER_HPP_ */
T m_pos_x
Pen position.
Definition: designer.hpp:314
virtual void setPenPosition(T x, T y)
Sets the pen position.
void drawCirclePoints(T cx, T cy, T x, T y)
Draws the symmetric points of the circle.
font::base & getFont() const
Returns the current text font.
designer(T width, T height)
Constructor method.
virtual void drawPixel(T x, T y, color_iterator< C > color)=0
Draws a pixel.
typename std::vector< T >::const_iterator color_iterator
Color iterator.
Definition: types.hpp:45
void drawImage(const image::base< C > &img)
Draws an image.
void drawCircleAreas(T cx, T cy, T x, T y)
Draws the symmetric areas of the circle.
void drawChar(char32_t charcode)
Draws a character.
void drawEllipse(T cx, T cy, T a, T b)
Draws an ellipse.
void drawRect(T x0, T y0, T x1, T y1)
Draws a rectangle.
void drawPoint(T x, T y)
Draws a point.
void fillCircle(T cx, T cy, T radius)
Draws a filled circle.
The graphic designer.
Definition: designer.hpp:44
void setPenColor(const std::vector< C > &color)
Sets the pen color.
void setFont(font::base &font)
Sets the text font.
Generic image.
Definition: base.hpp:44
TextFlags
Parameters of text.
Definition: designer.hpp:49
void drawEllipseAreas(T cx, T cy, T x, T y)
Draws the symmetric areas of the ellipse.
font::base * m_font
Text font.
Definition: designer.hpp:321
void drawLine(T x0, T y0, T x1, T y1)
Draws a line.
Generic font.
Definition: base.hpp:50
void fillEllipse(T cx, T cy, T a, T b)
Draws a filled ellipse.
T m_area_w
Size of the drawing area.
Definition: designer.hpp:309
virtual ~designer()
Destructor method.
void fillRect(T x0, T y0, T x1, T y1)
Draws a filled rectangle.
const std::vector< C > & getPenColor() const
Gets the current pen color.
std::vector< C > m_color
Foreground color.
Definition: designer.hpp:318
void drawCircle(T cx, T cy, T radius)
Draws a circle.
void drawText(char32_t charcode)
Draws a character.
void drawEllipsePoints(T cx, T cy, T x, T y)
Draws the symmetric points of the ellipse.