Tc Unicode Helper
tcUnicodeHelper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef TC_UNICODE_FONTHELPER_H
7 #define TC_UNICODE_FONTHELPER_H
8 
9 #include <string.h>
10 #include <inttypes.h>
11 #include "Utf8TextProcessor.h"
12 #include "UnicodeFontDefs.h"
13 
14 #define TCUNICODE_API_VERSION 2
15 
16 #if !defined(pgm_read_dword) && (defined(__MBED__) || defined(BUILD_FOR_PICO_CMAKE))
17 #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
18 #define pgm_read_word(addr) (*(const unsigned short *)(addr))
19 #define pgm_read_dword(addr) (*(const unsigned long *)(addr))
20 #define pgm_read_float(addr) (*(const float *)(addr))
21 #define pgm_read_ptr(addr) (*(addr))
22 #define memcpy_P memcpy
23 #endif // pgm_read_byte
24 
30 #if __has_include ("zio_local_definitions.h")
31 # include "zio_local_definitions.h"
32 #endif
33 
34 #ifndef TC_COORD_DEFINED
35 #define TC_COORD_DEFINED
36 
37 namespace tcgfx {
38 
40  struct Coord {
42  Coord() {
43  x = y = 0;
44  }
45 
51  Coord(int x, int y) {
52  this->x = x;
53  this->y = y;
54  }
55 
56  Coord(const Coord &other) = default;
57  Coord& operator = (const Coord& other) = default;
58 
59  int16_t x;
60  int16_t y;
61  };
62 }
63 
64 #endif // TC_COORD_DEFINED
65 
66 using namespace tcgfx;
67 
75 public:
76  virtual ~TextPlotPipeline() = default;
83  virtual void drawPixel(uint16_t x, uint16_t y, uint32_t color) = 0;
88  virtual void setCursor(const Coord& where) = 0;
92  virtual Coord getCursor() = 0;
96  virtual Coord getDimensions() = 0;
97 };
98 
99 #define TC_UNICODE_CHAR_ERROR 0xffffffff
100 
106 private:
107  const uint8_t *bitmapData = nullptr;
108  const UnicodeFontGlyph *glyph = nullptr;
109 public:
113  const uint8_t *getBitmapData() const {
114  return bitmapData;
115  }
116 
120  const UnicodeFontGlyph *getGlyph() const {
121  return glyph;
122  }
123 
124  void setBitmapData(const uint8_t *bm) {
125  GlyphWithBitmap::bitmapData = bm;
126  }
127 
128  void setGlyph(const UnicodeFontGlyph *g) {
129  GlyphWithBitmap::glyph = g;
130  }
131 };
132 
133 void handleUtf8Drawing(void *userData, uint32_t ch);
134 
135 #if __has_include (<Print.h>) || defined(ARDUINO_SAM_DUE)
136 #include <Print.h>
137 class UnicodeFontHandler : public Print {
138 #elif __has_include(<PrintCompat.h>)
139 #include <PrintCompat.h>
140 class UnicodeFontHandler : public Print {
141 #else
142 class UnicodeFontHandler {
143 #endif
144 
145 public:
146  enum HandlerMode {
147  HANDLER_SIZING_TEXT, HANDLER_DRAWING_TEXT
148  };
149 private:
151  TextPlotPipeline *plotter;
152  union {
153  const UnicodeFont *unicodeFont;
154  const GFXfont *adaFont;
155  };
156  bool fontAdafruit = false;
157  HandlerMode handlerMode = HANDLER_DRAWING_TEXT;
158  uint16_t xExtentCurrent = 0;
159  int16_t calculatedBaseline = -1;
160  uint32_t drawColor = 0;
161 public:
172  explicit UnicodeFontHandler(TextPlotPipeline *plotter, tccore::UnicodeEncodingMode mode) : utf8(handleUtf8Drawing, this, mode),
173  plotter(plotter),
174  unicodeFont(nullptr) {}
175  virtual ~UnicodeFontHandler() = default;
176 
182  TextPlotPipeline *getTextPlotPipeline() { return plotter; }
183 
188  void setTextPlotPipeline(TextPlotPipeline *newPipeline) { plotter = newPipeline; }
189 
194  void setFont(const UnicodeFont *font) {
195  unicodeFont = font;
196  fontAdafruit = false;
197  calculatedBaseline = -1;
198  }
199 
204  void setFont(const GFXfont *font) {
205  adaFont = font;
206  fontAdafruit = true;
207  calculatedBaseline = -1;
208  }
209 
216  void setCursor(int16_t x, int16_t y) { plotter->setCursor(Coord(x, y)); }
217 
223  void setCursor(const Coord& where) { plotter->setCursor(where); }
224 
229  void setDrawColor(uint32_t color) { this->drawColor = color; }
230 
235  void writeUnicode(uint32_t unicodeText);
236 
245  Coord textExtents(const char *text, int *baseline, bool progMem = false);
246 
253  Coord textExtents_P(const char *text, int *baseline) { return textExtents(text, baseline, true); }
254 
255 #if !defined(__MBED__) && !defined(BUILD_FOR_PICO_CMAKE)
256 
263  Coord textExtents(const __FlashStringHelper *fh, int *baseline) {
264  return textExtents((const char *) fh, baseline, true);
265  }
266 
267 #endif
268 
274  Coord textExtent(uint32_t theChar);
275 
281  size_t print_P(const char *textPgm);
282 
288  int getBaseline();
289 
296  size_t write(uint8_t data) override;
297 
306  bool findCharInFont(uint32_t ch, GlyphWithBitmap &glyphBitmap) const;
310  int getYAdvance() const {
311  if(adaFont == nullptr) return 0;
312  return pgm_read_byte((fontAdafruit ? &adaFont->yAdvance : &unicodeFont->yAdvance));
313  }
314 
319  void internalHandleUnicodeFont(uint32_t ch);
320 };
321 
322 using namespace tccore;
323 
324 #endif //TC_UNICODE_FONTHELPER_H
contains font rendering structures both for AdafruitGFX fonts and TcUnicode fonts
Definition: UnicodeFontDefs.h:54
Definition: UnicodeFontDefs.h:120
Definition: UnicodeFontDefs.h:82
contains a strict asynchronous UTF-8 decoder that uses very little memory.
UnicodeEncodingMode
Definition: Utf8TextProcessor.h:25
Definition: tcUnicodeHelper.h:105
const uint8_t * getBitmapData() const
Definition: tcUnicodeHelper.h:113
const UnicodeFontGlyph * getGlyph() const
Definition: tcUnicodeHelper.h:120
Definition: tcUnicodeHelper.h:74
virtual Coord getDimensions()=0
virtual void setCursor(const Coord &where)=0
virtual Coord getCursor()=0
virtual void drawPixel(uint16_t x, uint16_t y, uint32_t color)=0
Definition: Utf8TextProcessor.h:37
Definition: tcUnicodeHelper.h:40
Coord()
Definition: tcUnicodeHelper.h:42
Coord(int x, int y)
Definition: tcUnicodeHelper.h:51