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-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#pragma once
28
29
30
38#include <stdint.h>
39#include <stddef.h>
40
41#include "displaycontroller.h"
42
43
44namespace fabgl {
45
46
47
70class Canvas {
71
72public:
73
74 Canvas(BitmappedDisplayController * displayController);
75
83 int getWidth() { return m_displayController->getViewPortWidth(); }
84
92 int getHeight() { return m_displayController->getViewPortHeight(); }
93
99 void waitCompletion(bool waitVSync = true);
100
107 void beginUpdate();
108
112 void endUpdate();
113
120 void swapBuffers();
121
130 void setOrigin(int X, int Y);
131
139 void setOrigin(Point const & origin);
140
146 Point getOrigin() { return m_origin; }
147
155 void setClippingRect(Rect const & rect);
156
163
164
168 void clear();
169
173 void reset();
174
185 void setScrollingRegion(int X1, int Y1, int X2, int Y2);
186
197 void scroll(int offsetX, int offsetY);
198
214 void moveTo(int X, int Y);
215
228 void setPenColor(uint8_t red, uint8_t green, uint8_t blue);
229
240 void setPenColor(Color color);
241
252 void setPenColor(RGB888 const & color);
253
269 void setBrushColor(uint8_t red, uint8_t green, uint8_t blue);
270
281 void setBrushColor(Color color);
282
293 void setBrushColor(RGB888 const & color);
294
305 void setPenWidth(int value);
306
318 void setLineEnds(LineEnds value);
319
326 void setPixel(int X, int Y);
327
335 void setPixel(int X, int Y, RGB888 const & color);
336
343 void setPixel(Point const & pos, RGB888 const & color);
344
363 void lineTo(int X, int Y);
364
382 void drawLine(int X1, int Y1, int X2, int Y2);
383
398 void drawRectangle(int X1, int Y1, int X2, int Y2);
399
405 void drawRectangle(Rect const & rect);
406
427 void fillRectangle(int X1, int Y1, int X2, int Y2);
428
440 void fillRectangle(Rect const & rect);
441
452 void invertRectangle(int X1, int Y1, int X2, int Y2);
453
461 void invertRectangle(Rect const & rect);
462
471 void swapRectangle(int X1, int Y1, int X2, int Y2);
472
487 void drawEllipse(int X, int Y, int width, int height);
488
509 void fillEllipse(int X, int Y, int width, int height);
510
541 void drawGlyph(int X, int Y, int width, int height, uint8_t const * data, int index = 0);
542
559 void setGlyphOptions(GlyphOptions options);
560
564 void resetGlyphOptions();
565
566 void renderGlyphsBuffer(int itemX, int itemY, GlyphsBuffer const * glyphsBuffer);
567
571 void setPaintOptions(PaintOptions options);
572
576 void resetPaintOptions();
577
583 FontInfo const * getFontInfo() { return m_fontInfo; }
584
598 void selectFont(FontInfo const * fontInfo);
599
614 void drawChar(int X, int Y, char c);
615
631 void drawText(int X, int Y, char const * text, bool wrap = false);
632
649 void drawText(FontInfo const * fontInfo, int X, int Y, char const * text, bool wrap = false);
650
660 void drawTextWithEllipsis(FontInfo const * fontInfo, int X, int Y, char const * text, int maxX);
661
668 int textExtent(FontInfo const * fontInfo, char const * text);
669
675 int textExtent(char const * text);
676
690 void drawTextFmt(int X, int Y, const char *format, ...);
691
704 void copyRect(int sourceX, int sourceY, int destX, int destY, int width, int height);
705
717 void drawBitmap(int X, int Y, Bitmap const * bitmap);
718
731 void drawPath(Point const * points, int pointsCount);
732
745 void fillPath(Point const * points, int pointsCount);
746
758 RGB888 getPixel(int X, int Y);
759
760private:
761
762 BitmappedDisplayController * m_displayController;
763
764 FontInfo const * m_fontInfo;
765 uint8_t m_textHorizRate; // specify character size: 1 = m_fontInfo.width, 2 = m_fontInfo.width * 2, etc...
766
767 Point m_origin;
768 Rect m_clippingRect;
769};
770
771
772} // end of namespace
773
774
775
776
777
778
779
int getViewPortHeight()
Determines vertical size of the viewport.
int getViewPortWidth()
Determines horizontal size of the viewport.
Represents the base abstract class for bitmapped display controllers.
void fillRectangle(int X1, int Y1, int X2, int Y2)
Fills a rectangle using the current brush color.
Definition: canvas.cpp:278
int getHeight()
Determines the canvas height in pixels.
Definition: canvas.h:92
void waitCompletion(bool waitVSync=true)
Waits for drawing queue to become empty.
Definition: canvas.cpp:84
void setScrollingRegion(int X1, int Y1, int X2, int Y2)
Defines the scrolling region.
Definition: canvas.cpp:145
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:430
void swapRectangle(int X1, int Y1, int X2, int Y2)
Swaps pen and brush colors of the specified rectangle.
Definition: canvas.cpp:311
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:490
void setPixel(int X, int Y)
Fills a single pixel with the pen color.
Definition: canvas.cpp:154
void swapBuffers()
Swaps screen buffer when double buffering is enabled.
Definition: canvas.cpp:511
void drawBitmap(int X, int Y, Bitmap const *bitmap)
Draws a bitmap at specified position.
Definition: canvas.cpp:502
void drawEllipse(int X, int Y, int width, int height)
Draws an ellipse specifying center and size, using current pen color.
Definition: canvas.cpp:330
void setPaintOptions(PaintOptions options)
Sets paint options.
Definition: canvas.cpp:374
void setOrigin(int X, int Y)
Sets the axes origin.
Definition: canvas.cpp:52
void setClippingRect(Rect const &rect)
Sets clipping rectangle relative to the origin.
Definition: canvas.cpp:67
void setPenWidth(int value)
Sets pen width for lines, rectangles and paths.
Definition: canvas.cpp:220
void drawRectangle(int X1, int Y1, int X2, int Y2)
Draws a rectangle using the current pen color.
Definition: canvas.cpp:263
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:340
Rect getClippingRect()
Gets last clipping rectangle set using setClippingRect().
Definition: canvas.cpp:76
int getWidth()
Determines the canvas width in pixels.
Definition: canvas.h:83
void drawPath(Point const *points, int pointsCount)
Draws a sequence of lines.
Definition: canvas.cpp:521
void fillEllipse(int X, int Y, int width, int height)
Fills an ellipse specifying center and size, using current brush color.
Definition: canvas.cpp:320
void setBrushColor(uint8_t red, uint8_t green, uint8_t blue)
Sets brush (background) color specifying color components.
Definition: canvas.cpp:214
Point getOrigin()
Gets last origin set using setOrigin().
Definition: canvas.h:146
int textExtent(FontInfo const *fontInfo, char const *text)
Calculates text extension in pixels.
Definition: canvas.cpp:453
void invertRectangle(int X1, int Y1, int X2, int Y2)
Inverts a rectangle.
Definition: canvas.cpp:296
RGB888 getPixel(int X, int Y)
Reads the pixel at specified position.
Definition: canvas.cpp:543
void drawLine(int X1, int Y1, int X2, int Y2)
Draws a line specifying initial and ending coordinates.
Definition: canvas.cpp:256
void setPenColor(uint8_t red, uint8_t green, uint8_t blue)
Sets pen (foreground) color specifying color components.
Definition: canvas.cpp:193
void clear()
Fills the entire canvas with the brush color.
Definition: canvas.cpp:109
void resetPaintOptions()
Resets paint options.
Definition: canvas.cpp:383
void drawChar(int X, int Y, char c)
Draws a character at specified position.
Definition: canvas.cpp:395
void reset()
Resets paint state and other display controller settings.
Definition: canvas.cpp:118
void resetGlyphOptions()
Resets glyph options.
Definition: canvas.cpp:368
FontInfo const * getFontInfo()
Gets info about currently selected font.
Definition: canvas.h:583
void drawText(int X, int Y, char const *text, bool wrap=false)
Draws a string at specified position.
Definition: canvas.cpp:401
void drawTextFmt(int X, int Y, const char *format,...)
Draws formatted text at specified position.
Definition: canvas.cpp:474
void lineTo(int X, int Y)
Draws a line starting from current pen position.
Definition: canvas.cpp:247
void moveTo(int X, int Y)
Moves current pen position to the spcified coordinates.
Definition: canvas.cpp:178
void scroll(int offsetX, int offsetY)
Scrolls pixels horizontally and/or vertically.
Definition: canvas.cpp:129
void setGlyphOptions(GlyphOptions options)
Sets drawing options for the next glyphs.
Definition: canvas.cpp:358
void endUpdate()
Resumes drawings after beginUpdate().
Definition: canvas.cpp:103
void selectFont(FontInfo const *fontInfo)
Selects a font to use for the next text drawings.
Definition: canvas.cpp:389
void fillPath(Point const *points, int pointsCount)
Fills the polygon enclosed in a sequence of lines.
Definition: canvas.cpp:532
void setLineEnds(LineEnds value)
Sets line ends shape.
Definition: canvas.cpp:229
void beginUpdate()
Suspends drawings.
Definition: canvas.cpp:97
A class with a set of drawing methods.
Definition: canvas.h:70
uint8_t width
uint8_t const * data
int16_t X
int16_t Y
uint8_t height
This file contains fabgl::BitmappedDisplayController definition.
int16_t X1
Definition: fabutils.h:0
int16_t Y2
Definition: fabutils.h:3
int16_t X2
Definition: fabutils.h:2
int16_t Y1
Definition: fabutils.h:1
LineEnds
This enum defines line ends when pen width is greater than 1.
Color
This enum defines named colors.
Represents an image.
Specifies general paint options.
Represents the coordinate of a point.
Definition: fabutils.h:213
Represents a 24 bit RGB color.
Represents a rectangle.
Definition: fabutils.h:248
Specifies various glyph painting options.