FabGL
ESP32 VGA 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 "vgacontroller.h"
38 
39 
40 namespace fabgl {
41 
42 
43 
44 #define FONTINFOFLAGS_ITALIC 1
45 #define FONTINFOFLAGS_UNDERLINE 2
46 #define FONTINFODLAFS_STRIKEOUT 4
47 #define FONTINFOFLAGS_VARWIDTH 8
48 
49 
50 struct FontInfo {
51  uint8_t pointSize;
52  uint8_t width; // used only for fixed width fonts (FONTINFOFLAGS_VARWIDTH = 0)
53  uint8_t height;
54  uint8_t ascent;
55  uint8_t inleading;
56  uint8_t exleading;
57  uint8_t flags;
58  uint16_t weight;
59  uint16_t charset;
60  // when FONTINFOFLAGS_VARWIDTH = 0:
61  // data[] contains 256 items each one representing a single character
62  // when FONTINFOFLAGS_VARWIDTH = 1:
63  // data[] contains 256 items each one representing a single character. First byte contains the
64  // character width. "chptr" is filled with an array of pointers to the single characters.
65  uint8_t const * data;
66  uint32_t const * chptr; // used only for variable width fonts (FONTINFOFLAGS_VARWIDTH = 1)
67 };
68 
69 
70 
92 class CanvasClass {
93 
94 public:
95 
96  CanvasClass();
97 
105  int getWidth() { return VGAController.getViewPortWidth(); }
106 
114  int getHeight() { return VGAController.getViewPortHeight(); }
115 
124  void setOrigin(int X, int Y);
125 
133  void setOrigin(Point const & origin);
134 
140  Point getOrigin() { return m_origin; }
141 
149  void setClippingRect(Rect const & rect);
150 
157 
158 
162  void clear();
163 
174  void setScrollingRegion(int X1, int Y1, int X2, int Y2);
175 
186  void scroll(int offsetX, int offsetY);
187 
203  void moveTo(int X, int Y);
204 
220  void setPenColor(uint8_t red, uint8_t green, uint8_t blue);
221 
232  void setPenColor(Color color);
233 
244  void setPenColor(RGB const & color);
245 
261  void setBrushColor(uint8_t red, uint8_t green, uint8_t blue);
262 
273  void setBrushColor(Color color);
274 
285  void setBrushColor(RGB const & color);
286 
293  void setPixel(int X, int Y);
294 
302  void setPixel(int X, int Y, RGB const & color);
303 
310  void setPixel(Point const & pos, RGB const & color);
311 
330  void lineTo(int X, int Y);
331 
349  void drawLine(int X1, int Y1, int X2, int Y2);
350 
365  void drawRectangle(int X1, int Y1, int X2, int Y2);
366 
372  void drawRectangle(Rect const & rect);
373 
394  void fillRectangle(int X1, int Y1, int X2, int Y2);
395 
407  void fillRectangle(Rect const & rect);
408 
419  void invertRectangle(int X1, int Y1, int X2, int Y2);
420 
428  void invertRectangle(Rect const & rect);
429 
438  void swapRectangle(int X1, int Y1, int X2, int Y2);
439 
454  void drawEllipse(int X, int Y, int width, int height);
455 
476  void fillEllipse(int X, int Y, int width, int height);
477 
483  void waitCompletion(bool waitVSync = true);
484 
515  void drawGlyph(int X, int Y, int width, int height, uint8_t const * data, int index = 0);
516 
533  void setGlyphOptions(GlyphOptions options);
534 
538  void resetGlyphOptions();
539 
540  void renderGlyphsBuffer(int itemX, int itemY, GlyphsBuffer const * glyphsBuffer);
541 
545  void setPaintOptions(PaintOptions options);
546 
550  void resetPaintOptions();
551 
562  static FontInfo const * getPresetFontInfo(int columns, int rows);
563 
572  static FontInfo const * getPresetFontInfoFromHeight(int height, bool fixedWidth);
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 
721  void swapBuffers();
722 
740  void drawPath(Point const * points, int pointsCount);
741 
759  void fillPath(Point const * points, int pointsCount);
760 
772  RGB getPixel(int X, int Y);
773 
774 private:
775 
776  FontInfo const * m_fontInfo;
777  uint8_t m_textHorizRate; // specify character size: 1 = m_fontInfo.width, 2 = m_fontInfo.width * 2, etc...
778 
779  Point m_origin;
780  Rect m_clippingRect;
781 };
782 
783 
784 } // end of namespace
785 
786 
787 
788 extern fabgl::CanvasClass Canvas;
789 
790 
791 
Point getOrigin()
Gets last origin set using setOrigin().
Definition: canvas.h:140
A class with a set of drawing methods.
Definition: canvas.h:92
FontInfo const * getFontInfo()
Gets info about currently selected font.
Definition: canvas.h:579
Represents an RGB color.
Definition: vgacontroller.h:257
int getViewPortWidth()
Determines horizontal size of the viewport.
Definition: vgacontroller.h:700
int getViewPortHeight()
Determines vertical size of the viewport.
Definition: vgacontroller.h:707
Color
This enum defines named colors.
Definition: vgacontroller.h:230
void fillRectangle(int X1, int Y1, int X2, int Y2)
Fills a rectangle using the current brush color.
Definition: canvas.cpp:240
void invertRectangle(int X1, int Y1, int X2, int Y2)
Inverts a rectangle.
Definition: canvas.cpp:258
void setGlyphOptions(GlyphOptions options)
Sets drawing options for the next glyphs.
Definition: canvas.cpp:320
Rect getClippingRect()
Gets last clipping rectangle set using setClippingRect().
Definition: canvas.cpp:83
void drawTextFmt(int X, int Y, const char *format,...)
Draws formatted text at specified position.
Definition: canvas.cpp:436
This file contains fabgl::VGAControllerClass definition and the VGAController instance.
void swapBuffers()
Swaps screen buffer when double buffering is enabled.
Definition: canvas.cpp:521
int getWidth()
Determines the canvas width in pixels.
Definition: canvas.h:105
void moveTo(int X, int Y)
Moves current pen position to the spcified coordinates.
Definition: canvas.cpp:158
void drawLine(int X1, int Y1, int X2, int Y2)
Draws a line specifying initial and ending coordinates.
Definition: canvas.cpp:218
void setPenColor(uint8_t red, uint8_t green, uint8_t blue)
Sets pen (foreground) color specifying color components.
Definition: canvas.cpp:173
void drawPath(Point const *points, int pointsCount)
Draws a sequence of lines.
Definition: canvas.cpp:531
void setClippingRect(Rect const &rect)
Sets clipping rectangle relative to the origin.
Definition: canvas.cpp:74
void fillPath(Point const *points, int pointsCount)
Fills the polygon enclosed in a sequence of lines.
Definition: canvas.cpp:541
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:450
void setPixel(int X, int Y)
Fills a single pixel with the pen color.
Definition: canvas.cpp:134
void setScrollingRegion(int X1, int Y1, int X2, int Y2)
Defines the scrolling region.
Definition: canvas.cpp:125
void waitCompletion(bool waitVSync=true)
Waits for drawing queue to become empty.
Definition: canvas.cpp:91
void setOrigin(int X, int Y)
Sets the axes origin.
Definition: canvas.cpp:59
Represents the coordinate of a point.
Definition: fabutils.h:125
Represents an image with 64 colors image and transparency.
Definition: vgacontroller.h:385
void drawBitmap(int X, int Y, Bitmap const *bitmap)
Draws a bitmap at specified position.
Definition: canvas.cpp:512
RGB getPixel(int X, int Y)
Reads the pixel at specified position.
Definition: canvas.cpp:551
Definition: canvas.cpp:47
Specifies various glyph painting options.
Definition: vgacontroller.h:302
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:302
Represents a rectangle.
Definition: fabutils.h:158
void resetGlyphOptions()
Resets glyph options.
Definition: canvas.cpp:330
void lineTo(int X, int Y)
Draws a line starting from current pen position.
Definition: canvas.cpp:209
static FontInfo const * getPresetFontInfoFromHeight(int height, bool fixedWidth)
Gets the font info that best fits the specified height.
Definition: canvas.cpp:499
void swapRectangle(int X1, int Y1, int X2, int Y2)
Swaps pen and brush colors of the specified rectangle.
Definition: canvas.cpp:273
int getHeight()
Determines the canvas height in pixels.
Definition: canvas.h:114
void drawRectangle(int X1, int Y1, int X2, int Y2)
Draws a rectangle using the current pen color.
Definition: canvas.cpp:225
void selectFont(FontInfo const *fontInfo)
Selects a font to use for the next text drawings.
Definition: canvas.cpp:351
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:392
void setBrushColor(uint8_t red, uint8_t green, uint8_t blue)
Sets brush (background) color specifying color components.
Definition: canvas.cpp:194
void drawEllipse(int X, int Y, int width, int height)
Draws an ellipse specifying center and size, using current pen color.
Definition: canvas.cpp:292
int textExtent(FontInfo const *fontInfo, char const *text)
Calculates text extension in pixels.
Definition: canvas.cpp:415
void fillEllipse(int X, int Y, int width, int height)
Fills an ellipse specifying center and size, using current brush color.
Definition: canvas.cpp:282
void drawText(int X, int Y, char const *text, bool wrap=false)
Draws a string at specified position.
Definition: canvas.cpp:363
void scroll(int offsetX, int offsetY)
Scrolls pixels horizontally and/or vertically.
Definition: canvas.cpp:109
Specifies general paint options.
Definition: vgacontroller.h:501
void setPaintOptions(PaintOptions options)
Sets paint options.
Definition: canvas.cpp:336
void drawChar(int X, int Y, char c)
Draws a character at specified position.
Definition: canvas.cpp:357
void clear()
Fills the entire canvas with the brush color.
Definition: canvas.cpp:100
void resetPaintOptions()
Resets paint options.
Definition: canvas.cpp:345
static FontInfo const * getPresetFontInfo(int columns, int rows)
Gets the font info that best fits the specified number of columns and rows.
Definition: canvas.cpp:487