Canvas graphics library  0.1.0
This library is developed to perform canvas graphics in memory buffers
canvas.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2019, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
29 #ifndef _NANO_CANVAS_H_
30 #define _NANO_CANVAS_H_
31 
32 #include "point.h"
33 #include "rect.h"
34 #include "font.h"
35 #include "canvas_types.h"
36 
46 template <uint8_t BPP>
48 {
49 public:
51  static const uint8_t BITS_PER_PIXEL = BPP;
52 
55 
62  {
63  }
64 
75  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
76  {
77  begin(w, h, bytes);
78  }
79 
90  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
91 
97  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
98 
103  const NanoPoint offsetEnd() const
104  {
105  return offset + (NanoPoint){ (lcdint_t)(m_w-1), (lcdint_t)(m_h-1) };
106  }
107 
112  const NanoRect rect() const
113  {
114  return { offset, offsetEnd() };
115  }
116 
123  void putPixel(lcdint_t x, lcdint_t y);
124 
130  void putPixel(const NanoPoint &p);
131 
139  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
140 
148  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
149 
158  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
159 
165  void drawLine(const NanoRect &rect);
166 
175  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__ ((noinline));
176 
182  void drawRect(const NanoRect &rect);
183 
192  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__ ((noinline));
193 
199  void fillRect(const NanoRect &rect);
200 
215  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__ ((noinline));
216 
226  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__ ((noinline));
227 
231  void clear() __attribute__ ((noinline));
232 
237  size_t write(uint8_t c);
238 
244  uint8_t printChar(uint8_t c);
245 
256  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL) __attribute__ ((noinline));
257 
268  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
269 
275  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
276 
281  void setColor(uint16_t color) { m_color = color; }
282 
291  void setFont( NanoFont &font ) { m_font = &font; };
292 
302  void setFixedFont( const uint8_t *progmemFont )
303  {
304  g_canvas_font.loadFixedFont( progmemFont );
305  setFont( g_canvas_font );
306  }
307 
318  void setFreeFont( const uint8_t *progmemFont, const uint8_t *secondaryFont = nullptr )
319  {
320  g_canvas_font.loadFreeFont( progmemFont );
321  setFont( g_canvas_font );
322  }
323 
325  uint8_t * getData() { return m_buf; }
326 
328  lcduint_t width() { return m_w; }
329 
336  lcduint_t pitch() { return BPP == 1 ? m_w : (m_w * BPP / 8); }
337 
339  lcduint_t height() { return m_h; }
340 
341 protected:
346  uint8_t m_textMode;
348  uint8_t * m_buf;
349  uint16_t m_color;
350  NanoFont *m_font = nullptr;
351 };
352 
356 template <uint8_t BPP>
357 class NanoCanvasBase: public NanoCanvasOps<BPP>
358 {
359 public:
361 };
362 
368 template <lcduint_t W, lcduint_t H, uint8_t BPP>
369 class NanoCanvas: public NanoCanvasBase<BPP>
370 {
371 public:
372  NanoCanvas(): NanoCanvasBase<BPP>( W, H, m_buffer )
373  {
374  }
375 
376 private:
377  uint8_t m_buffer[W * H * BPP / 8]{};
378 };
379 
381 //
382 // 1-BIT GRAPHICS
383 //
385 
386 enum
387 {
388  BLACK = 0x00,
389  WHITE = 0xFF,
390 };
391 
396 class NanoCanvas1: public NanoCanvasBase<1>
397 {
398 public:
399  using NanoCanvasBase::NanoCanvasBase;
400 };
401 
408 {
409 public:
410  using NanoCanvasBase::NanoCanvasBase;
411 };
412 
419 {
420 public:
421  using NanoCanvasBase::NanoCanvasBase;
422 };
423 
425 //
426 // 8-BIT GRAPHICS
427 //
429 
434 class NanoCanvas4: public NanoCanvasBase<4>
435 {
436 public:
437  using NanoCanvasBase::NanoCanvasBase;
438 };
439 
441 //
442 // 8-BIT GRAPHICS
443 //
445 
450 class NanoCanvas8: public NanoCanvasBase<8>
451 {
452 public:
453  using NanoCanvasBase::NanoCanvasBase;
454 };
455 
457 //
458 // 16-BIT GRAPHICS
459 //
461 
466 class NanoCanvas16: public NanoCanvasBase<16>
467 {
468 public:
469  using NanoCanvasBase::NanoCanvasBase;
470 };
471 
476 #endif
477 
lcduint_t pitch()
Definition: canvas.h:336
const NanoPoint offsetEnd() const
Definition: canvas.h:103
uint8_t lcduint_t
Definition: canvas_types.h:81
void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline))
NanoFont * m_font
current set font to use with NanoCanvas
Definition: canvas.h:350
Definition: rect.h:42
size_t write(uint8_t c)
void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2)
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:344
int8_t lcdint_t
Definition: canvas_types.h:79
White color.
Definition: canvas.h:389
void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline))
Draws 8-bit color bitmap in color buffer. Draws 8-bit color bitmap in color buffer.
void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL) __attribute__((noinline))
uint8_t * getData()
Definition: canvas.h:325
lcdint_t y
Definition: point.h:45
lcduint_t width()
Definition: canvas.h:328
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:75
void clear() __attribute__((noinline))
void putPixel(lcdint_t x, lcdint_t y)
Definition: font.h:45
void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline))
lcduint_t height()
Definition: canvas.h:339
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:51
void setFixedFont(const uint8_t *progmemFont)
Definition: canvas.h:302
void setColor(uint16_t color)
Definition: canvas.h:281
NanoCanvasOps()
Definition: canvas.h:61
uint8_t * m_buf
Canvas data.
Definition: canvas.h:348
void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline))
Draws monochrome bitmap in color buffer using color, specified via setColor() method Draws monochrome...
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:345
void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2)
void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL)
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:97
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:347
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:275
void setFont(NanoFont &font)
Definition: canvas.h:291
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
Black color.
Definition: canvas.h:388
void loadFixedFont(const uint8_t *progmemFont)
struct _NanoPoint NanoPoint
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:349
EFontStyle
Definition: canvas_types.h:90
NanoPoint offset
Definition: canvas.h:54
lcdint_t x
Definition: point.h:43
const NanoRect rect() const
Definition: canvas.h:112
void setFreeFont(const uint8_t *progmemFont, const uint8_t *secondaryFont=nullptr)
Definition: canvas.h:318
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:342
void loadFreeFont(const uint8_t *progmemFont)
uint8_t printChar(uint8_t c)
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:343
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:346