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-2020 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(BitmappedDisplayController * 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 
169  void reset();
170 
181  void setScrollingRegion(int X1, int Y1, int X2, int Y2);
182 
193  void scroll(int offsetX, int offsetY);
194 
210  void moveTo(int X, int Y);
211 
224  void setPenColor(uint8_t red, uint8_t green, uint8_t blue);
225 
236  void setPenColor(Color color);
237 
248  void setPenColor(RGB888 const & color);
249 
265  void setBrushColor(uint8_t red, uint8_t green, uint8_t blue);
266 
277  void setBrushColor(Color color);
278 
289  void setBrushColor(RGB888 const & color);
290 
301  void setPenWidth(int value);
302 
314  void setLineEnds(LineEnds value);
315 
322  void setPixel(int X, int Y);
323 
331  void setPixel(int X, int Y, RGB888 const & color);
332 
339  void setPixel(Point const & pos, RGB888 const & color);
340 
359  void lineTo(int X, int Y);
360 
378  void drawLine(int X1, int Y1, int X2, int Y2);
379 
394  void drawRectangle(int X1, int Y1, int X2, int Y2);
395 
401  void drawRectangle(Rect const & rect);
402 
423  void fillRectangle(int X1, int Y1, int X2, int Y2);
424 
436  void fillRectangle(Rect const & rect);
437 
448  void invertRectangle(int X1, int Y1, int X2, int Y2);
449 
457  void invertRectangle(Rect const & rect);
458 
467  void swapRectangle(int X1, int Y1, int X2, int Y2);
468 
483  void drawEllipse(int X, int Y, int width, int height);
484 
505  void fillEllipse(int X, int Y, int width, int height);
506 
537  void drawGlyph(int X, int Y, int width, int height, uint8_t const * data, int index = 0);
538 
555  void setGlyphOptions(GlyphOptions options);
556 
560  void resetGlyphOptions();
561 
562  void renderGlyphsBuffer(int itemX, int itemY, GlyphsBuffer const * glyphsBuffer);
563 
567  void setPaintOptions(PaintOptions options);
568 
572  void resetPaintOptions();
573 
579  FontInfo const * getFontInfo() { return m_fontInfo; }
580 
594  void selectFont(FontInfo const * fontInfo);
595 
610  void drawChar(int X, int Y, char c);
611 
627  void drawText(int X, int Y, char const * text, bool wrap = false);
628 
645  void drawText(FontInfo const * fontInfo, int X, int Y, char const * text, bool wrap = false);
646 
656  void drawTextWithEllipsis(FontInfo const * fontInfo, int X, int Y, char const * text, int maxX);
657 
664  int textExtent(FontInfo const * fontInfo, char const * text);
665 
671  int textExtent(char const * text);
672 
686  void drawTextFmt(int X, int Y, const char *format, ...);
687 
700  void copyRect(int sourceX, int sourceY, int destX, int destY, int width, int height);
701 
713  void drawBitmap(int X, int Y, Bitmap const * bitmap);
714 
727  void drawPath(Point const * points, int pointsCount);
728 
741  void fillPath(Point const * points, int pointsCount);
742 
754  RGB888 getPixel(int X, int Y);
755 
756 private:
757 
758  BitmappedDisplayController * m_displayController;
759 
760  FontInfo const * m_fontInfo;
761  uint8_t m_textHorizRate; // specify character size: 1 = m_fontInfo.width, 2 = m_fontInfo.width * 2, etc...
762 
763  Point m_origin;
764  Rect m_clippingRect;
765 };
766 
767 
768 } // end of namespace
769 
770 
771 
772 
773 
774 
775 
int16_t X2
Definition: fabutils.h:150
int textExtent(FontInfo const *fontInfo, char const *text)
Calculates text extension in pixels.
Definition: canvas.cpp:445
Represents a 24 bit RGB color.
A class with a set of drawing methods.
Definition: canvas.h:66
void swapBuffers()
Swaps screen buffer when double buffering is enabled.
Definition: canvas.cpp:503
int getHeight()
Determines the canvas height in pixels.
Definition: canvas.h:88
int16_t Y2
Definition: fabutils.h:151
void reset()
Resets paint state and other display controller settings.
Definition: canvas.cpp:113
int16_t Y1
Definition: fabutils.h:149
void drawTextFmt(int X, int Y, const char *format,...)
Draws formatted text at specified position.
Definition: canvas.cpp:466
void scroll(int offsetX, int offsetY)
Scrolls pixels horizontally and/or vertically.
Definition: canvas.cpp:121
int16_t Y
void selectFont(FontInfo const *fontInfo)
Selects a font to use for the next text drawings.
Definition: canvas.cpp:381
void resetGlyphOptions()
Resets glyph options.
Definition: canvas.cpp:360
This file contains fabgl::BitmappedDisplayController definition.
uint8_t const * data
Color
This enum defines named colors.
RGB888 getPixel(int X, int Y)
Reads the pixel at specified position.
Definition: canvas.cpp:535
Represents the base abstract class for bitmapped display controllers.
Rect getClippingRect()
Gets last clipping rectangle set using setClippingRect().
Definition: canvas.cpp:71
int getWidth()
Determines the canvas width in pixels.
Definition: canvas.h:79
void setPaintOptions(PaintOptions options)
Sets paint options.
Definition: canvas.cpp:366
int16_t X1
Definition: fabutils.h:148
LineEnds
This enum defines line ends when pen width is greater than 1.
void fillEllipse(int X, int Y, int width, int height)
Fills an ellipse specifying center and size, using current brush color.
Definition: canvas.cpp:312
void swapRectangle(int X1, int Y1, int X2, int Y2)
Swaps pen and brush colors of the specified rectangle.
Definition: canvas.cpp:303
void setBrushColor(uint8_t red, uint8_t green, uint8_t blue)
Sets brush (background) color specifying color components.
Definition: canvas.cpp:206
void setClippingRect(Rect const &rect)
Sets clipping rectangle relative to the origin.
Definition: canvas.cpp:62
virtual int getViewPortWidth()=0
Determines horizontal size of the viewport.
virtual int getViewPortHeight()=0
Determines vertical size of the viewport.
void waitCompletion(bool waitVSync=true)
Waits for drawing queue to become empty.
Definition: canvas.cpp:79
void endUpdate()
Resumes drawings after beginUpdate().
Definition: canvas.cpp:98
Represents the coordinate of a point.
Definition: fabutils.h:158
Represents an image.
void fillRectangle(int X1, int Y1, int X2, int Y2)
Fills a rectangle using the current brush color.
Definition: canvas.cpp:270
Definition: canvas.cpp:31
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:332
Specifies various glyph painting options.
Represents a rectangle.
Definition: fabutils.h:191
void setLineEnds(LineEnds value)
Sets line ends shape.
Definition: canvas.cpp:221
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:422
void drawChar(int X, int Y, char c)
Draws a character at specified position.
Definition: canvas.cpp:387
int16_t X
void beginUpdate()
Suspends drawings.
Definition: canvas.cpp:92
void setScrollingRegion(int X1, int Y1, int X2, int Y2)
Defines the scrolling region.
Definition: canvas.cpp:137
void clear()
Fills the entire canvas with the brush color.
Definition: canvas.cpp:104
void setGlyphOptions(GlyphOptions options)
Sets drawing options for the next glyphs.
Definition: canvas.cpp:350
void drawLine(int X1, int Y1, int X2, int Y2)
Draws a line specifying initial and ending coordinates.
Definition: canvas.cpp:248
Point getOrigin()
Gets last origin set using setOrigin().
Definition: canvas.h:142
void drawEllipse(int X, int Y, int width, int height)
Draws an ellipse specifying center and size, using current pen color.
Definition: canvas.cpp:322
void setPenColor(uint8_t red, uint8_t green, uint8_t blue)
Sets pen (foreground) color specifying color components.
Definition: canvas.cpp:185
void drawPath(Point const *points, int pointsCount)
Draws a sequence of lines.
Definition: canvas.cpp:513
void drawText(int X, int Y, char const *text, bool wrap=false)
Draws a string at specified position.
Definition: canvas.cpp:393
uint8_t height
void invertRectangle(int X1, int Y1, int X2, int Y2)
Inverts a rectangle.
Definition: canvas.cpp:288
void setPixel(int X, int Y)
Fills a single pixel with the pen color.
Definition: canvas.cpp:146
void setPenWidth(int value)
Sets pen width for lines, rectangles and paths.
Definition: canvas.cpp:212
void fillPath(Point const *points, int pointsCount)
Fills the polygon enclosed in a sequence of lines.
Definition: canvas.cpp:524
void setOrigin(int X, int Y)
Sets the axes origin.
Definition: canvas.cpp:47
void moveTo(int X, int Y)
Moves current pen position to the spcified coordinates.
Definition: canvas.cpp:170
void resetPaintOptions()
Resets paint options.
Definition: canvas.cpp:375
FontInfo const * getFontInfo()
Gets info about currently selected font.
Definition: canvas.h:579
Specifies general paint options.
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:482
uint8_t width
void drawRectangle(int X1, int Y1, int X2, int Y2)
Draws a rectangle using the current pen color.
Definition: canvas.cpp:255
void lineTo(int X, int Y)
Draws a line starting from current pen position.
Definition: canvas.cpp:239
void drawBitmap(int X, int Y, Bitmap const *bitmap)
Draws a bitmap at specified position.
Definition: canvas.cpp:494