FabGL
ESP32 Display Controller and Graphics Library
canvas.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
26 
34 #include <stdint.h>
35 #include <stddef.h>
36 
37 #include "displaycontroller.h"
38 
39 
40 namespace fabgl {
41 
42 
43 
66 class Canvas {
67 
68 public:
69 
70  Canvas(DisplayController * displayController);
71 
79  int getWidth() { return m_displayController->getViewPortWidth(); }
80 
88  int getHeight() { return m_displayController->getViewPortHeight(); }
89 
95  void waitCompletion(bool waitVSync = true);
96 
103  void beginUpdate();
104 
108  void endUpdate();
109 
116  void swapBuffers();
117 
126  void setOrigin(int X, int Y);
127 
135  void setOrigin(Point const & origin);
136 
142  Point getOrigin() { return m_origin; }
143 
151  void setClippingRect(Rect const & rect);
152 
159 
160 
164  void clear();
165 
176  void setScrollingRegion(int X1, int Y1, int X2, int Y2);
177 
188  void scroll(int offsetX, int offsetY);
189 
205  void moveTo(int X, int Y);
206 
219  void setPenColor(uint8_t red, uint8_t green, uint8_t blue);
220 
231  void setPenColor(Color color);
232 
243  void setPenColor(RGB888 const & color);
244 
260  void setBrushColor(uint8_t red, uint8_t green, uint8_t blue);
261 
272  void setBrushColor(Color color);
273 
284  void setBrushColor(RGB888 const & color);
285 
292  void setPixel(int X, int Y);
293 
301  void setPixel(int X, int Y, RGB888 const & color);
302 
309  void setPixel(Point const & pos, RGB888 const & color);
310 
329  void lineTo(int X, int Y);
330 
348  void drawLine(int X1, int Y1, int X2, int Y2);
349 
364  void drawRectangle(int X1, int Y1, int X2, int Y2);
365 
371  void drawRectangle(Rect const & rect);
372 
393  void fillRectangle(int X1, int Y1, int X2, int Y2);
394 
406  void fillRectangle(Rect const & rect);
407 
418  void invertRectangle(int X1, int Y1, int X2, int Y2);
419 
427  void invertRectangle(Rect const & rect);
428 
437  void swapRectangle(int X1, int Y1, int X2, int Y2);
438 
453  void drawEllipse(int X, int Y, int width, int height);
454 
475  void fillEllipse(int X, int Y, int width, int height);
476 
507  void drawGlyph(int X, int Y, int width, int height, uint8_t const * data, int index = 0);
508 
525  void setGlyphOptions(GlyphOptions options);
526 
530  void resetGlyphOptions();
531 
532  void renderGlyphsBuffer(int itemX, int itemY, GlyphsBuffer const * glyphsBuffer);
533 
537  void setPaintOptions(PaintOptions options);
538 
542  void resetPaintOptions();
543 
549  FontInfo const * getFontInfo() { return m_fontInfo; }
550 
564  void selectFont(FontInfo const * fontInfo);
565 
580  void drawChar(int X, int Y, char c);
581 
597  void drawText(int X, int Y, char const * text, bool wrap = false);
598 
615  void drawText(FontInfo const * fontInfo, int X, int Y, char const * text, bool wrap = false);
616 
626  void drawTextWithEllipsis(FontInfo const * fontInfo, int X, int Y, char const * text, int maxX);
627 
634  int textExtent(FontInfo const * fontInfo, char const * text);
635 
641  int textExtent(char const * text);
642 
656  void drawTextFmt(int X, int Y, const char *format, ...);
657 
670  void copyRect(int sourceX, int sourceY, int destX, int destY, int width, int height);
671 
683  void drawBitmap(int X, int Y, Bitmap const * bitmap);
684 
702  void drawPath(Point const * points, int pointsCount);
703 
721  void fillPath(Point const * points, int pointsCount);
722 
734  RGB888 getPixel(int X, int Y);
735 
736 private:
737 
738  DisplayController * m_displayController;
739 
740  FontInfo const * m_fontInfo;
741  uint8_t m_textHorizRate; // specify character size: 1 = m_fontInfo.width, 2 = m_fontInfo.width * 2, etc...
742 
743  Point m_origin;
744  Rect m_clippingRect;
745 };
746 
747 
748 } // end of namespace
749 
750 
751 
752 
753 
754 
755 
Represents a 24 bit RGB color.
Definition: displaycontroller.h:206
A class with a set of drawing methods.
Definition: canvas.h:66
void drawEllipse(int X, int Y, int width, int height)
Draws an ellipse specifying center and size, using current pen color.
Definition: canvas.cpp:296
void drawTextFmt(int X, int Y, const char *format,...)
Draws formatted text at specified position.
Definition: canvas.cpp:440
void drawText(int X, int Y, char const *text, bool wrap=false)
Draws a string at specified position.
Definition: canvas.cpp:367
Point getOrigin()
Gets last origin set using setOrigin().
Definition: canvas.h:142
Color
This enum defines named colors.
Definition: displaycontroller.h:180
void drawChar(int X, int Y, char c)
Draws a character at specified position.
Definition: canvas.cpp:361
void beginUpdate()
Suspends drawings.
Definition: canvas.cpp:92
void copyRect(int sourceX, int sourceY, int destX, int destY, int width, int height)
Copies a screen rectangle to the specified position.
Definition: canvas.cpp:454
void setPaintOptions(PaintOptions options)
Sets paint options.
Definition: canvas.cpp:340
Rect getClippingRect()
Gets last clipping rectangle set using setClippingRect().
Definition: canvas.cpp:71
This file contains fabgl::DisplayController definition.
void selectFont(FontInfo const *fontInfo)
Selects a font to use for the next text drawings.
Definition: canvas.cpp:355
void resetGlyphOptions()
Resets glyph options.
Definition: canvas.cpp:334
void setPenColor(uint8_t red, uint8_t green, uint8_t blue)
Sets pen (foreground) color specifying color components.
Definition: canvas.cpp:177
int getHeight()
Determines the canvas height in pixels.
Definition: canvas.h:88
void swapBuffers()
Swaps screen buffer when double buffering is enabled.
Definition: canvas.cpp:475
int textExtent(FontInfo const *fontInfo, char const *text)
Calculates text extension in pixels.
Definition: canvas.cpp:419
void drawTextWithEllipsis(FontInfo const *fontInfo, int X, int Y, char const *text, int maxX)
Draws a string at specified position. Add ellipses before truncation.
Definition: canvas.cpp:396
void waitCompletion(bool waitVSync=true)
Waits for drawing queue to become empty.
Definition: canvas.cpp:79
Represents the coordinate of a point.
Definition: fabutils.h:125
Represents an image.
Definition: displaycontroller.h:414
void moveTo(int X, int Y)
Moves current pen position to the spcified coordinates.
Definition: canvas.cpp:162
Definition: canvas.cpp:31
Specifies various glyph painting options.
Definition: displaycontroller.h:315
Represents a rectangle.
Definition: fabutils.h:158
void fillPath(Point const *points, int pointsCount)
Fills the polygon enclosed in a sequence of lines.
Definition: canvas.cpp:495
virtual int getViewPortHeight()=0
Determines vertical size of the viewport.
void endUpdate()
Resumes drawings after beginUpdate().
Definition: canvas.cpp:98
FontInfo const * getFontInfo()
Gets info about currently selected font.
Definition: canvas.h:549
void setBrushColor(uint8_t red, uint8_t green, uint8_t blue)
Sets brush (background) color specifying color components.
Definition: canvas.cpp:198
void setOrigin(int X, int Y)
Sets the axes origin.
Definition: canvas.cpp:47
void drawLine(int X1, int Y1, int X2, int Y2)
Draws a line specifying initial and ending coordinates.
Definition: canvas.cpp:222
void scroll(int offsetX, int offsetY)
Scrolls pixels horizontally and/or vertically.
Definition: canvas.cpp:113
void setGlyphOptions(GlyphOptions options)
Sets drawing options for the next glyphs.
Definition: canvas.cpp:324
RGB888 getPixel(int X, int Y)
Reads the pixel at specified position.
Definition: canvas.cpp:505
void clear()
Fills the entire canvas with the brush color.
Definition: canvas.cpp:104
void invertRectangle(int X1, int Y1, int X2, int Y2)
Inverts a rectangle.
Definition: canvas.cpp:262
void lineTo(int X, int Y)
Draws a line starting from current pen position.
Definition: canvas.cpp:213
void setScrollingRegion(int X1, int Y1, int X2, int Y2)
Defines the scrolling region.
Definition: canvas.cpp:129
void fillEllipse(int X, int Y, int width, int height)
Fills an ellipse specifying center and size, using current brush color.
Definition: canvas.cpp:286
Represents the base abstract class for display controllers.
Definition: displaycontroller.h:596
void drawPath(Point const *points, int pointsCount)
Draws a sequence of lines.
Definition: canvas.cpp:485
void drawGlyph(int X, int Y, int width, int height, uint8_t const *data, int index=0)
Draws a glyph at specified position.
Definition: canvas.cpp:306
void setPixel(int X, int Y)
Fills a single pixel with the pen color.
Definition: canvas.cpp:138
int getWidth()
Determines the canvas width in pixels.
Definition: canvas.h:79
virtual int getViewPortWidth()=0
Determines horizontal size of the viewport.
void fillRectangle(int X1, int Y1, int X2, int Y2)
Fills a rectangle using the current brush color.
Definition: canvas.cpp:244
void swapRectangle(int X1, int Y1, int X2, int Y2)
Swaps pen and brush colors of the specified rectangle.
Definition: canvas.cpp:277
void resetPaintOptions()
Resets paint options.
Definition: canvas.cpp:349
Specifies general paint options.
Definition: displaycontroller.h:542
void setClippingRect(Rect const &rect)
Sets clipping rectangle relative to the origin.
Definition: canvas.cpp:62
void drawBitmap(int X, int Y, Bitmap const *bitmap)
Draws a bitmap at specified position.
Definition: canvas.cpp:466
void drawRectangle(int X1, int Y1, int X2, int Y2)
Draws a rectangle using the current pen color.
Definition: canvas.cpp:229