@@ -116,6 +116,7 @@ Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h) : WIDTH(w), HEIGHT(h) {
116116 textcolor = textbgcolor = 0xFFFF ;
117117 wrap = true ;
118118 _cp437 = false ;
119+ _utf8 = false ;
119120 gfxFont = NULL ;
120121}
121122
@@ -1105,15 +1106,15 @@ void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap,
11051106 @brief Draw a single character
11061107 @param x Bottom left corner x coordinate
11071108 @param y Bottom left corner y coordinate
1108- @param c The 8 -bit font-indexed character (likely ascii)
1109+ @param c The 16 -bit font-indexed character (likely ascii)
11091110 @param color 16-bit 5-6-5 Color to draw chraracter with
11101111 @param bg 16-bit 5-6-5 Color to fill background with (if same as color,
11111112 no background)
11121113 @param size Font magnification level, 1 is 'original' size
11131114*/
11141115/* *************************************************************************/
1115- void Adafruit_GFX::drawChar (int16_t x, int16_t y, unsigned char c ,
1116- uint16_t color, uint16_t bg, uint8_t size) {
1116+ void Adafruit_GFX::drawChar (int16_t x, int16_t y, uint16_t c, uint16_t color ,
1117+ uint16_t bg, uint8_t size) {
11171118 drawChar (x, y, c, color, bg, size, size);
11181119}
11191120
@@ -1123,17 +1124,16 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
11231124 @brief Draw a single character
11241125 @param x Bottom left corner x coordinate
11251126 @param y Bottom left corner y coordinate
1126- @param c The 8 -bit font-indexed character (likely ascii)
1127+ @param c The 16 -bit font-indexed character (likely ascii)
11271128 @param color 16-bit 5-6-5 Color to draw chraracter with
11281129 @param bg 16-bit 5-6-5 Color to fill background with (if same as color,
11291130 no background)
11301131 @param size_x Font magnification level in X-axis, 1 is 'original' size
11311132 @param size_y Font magnification level in Y-axis, 1 is 'original' size
11321133*/
11331134/* *************************************************************************/
1134- void Adafruit_GFX::drawChar (int16_t x, int16_t y, unsigned char c,
1135- uint16_t color, uint16_t bg, uint8_t size_x,
1136- uint8_t size_y) {
1135+ void Adafruit_GFX::drawChar (int16_t x, int16_t y, uint16_t c, uint16_t color,
1136+ uint16_t bg, uint8_t size_x, uint8_t size_y) {
11371137
11381138 if (!gfxFont) { // 'Classic' built-in font
11391139
@@ -1178,9 +1178,9 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
11781178 // newlines, returns, non-printable characters, etc. Calling
11791179 // drawChar() directly with 'bad' characters of font may cause mayhem!
11801180
1181- c -= ( uint8_t ) pgm_read_byte (&gfxFont->first );
1182- GFXglyph *glyph = pgm_read_glyph_ptr (gfxFont, c );
1183- uint8_t *bitmap = pgm_read_bitmap_ptr ( gfxFont);
1181+ c -= pgm_read_word (&gfxFont->first );
1182+ GFXglyph *glyph = &(((GFXglyph *) pgm_read_pointer (&gfxFont-> glyph ))[c] );
1183+ uint8_t *bitmap = ( uint8_t *) pgm_read_pointer (& gfxFont-> bitmap );
11841184
11851185 uint16_t bo = pgm_read_word (&glyph->bitmapOffset );
11861186 uint8_t w = pgm_read_byte (&glyph->width ), h = pgm_read_byte (&glyph->height );
@@ -1233,15 +1233,76 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
12331233
12341234 } // End classic vs custom font
12351235}
1236+
1237+ /* *************************************************************************/
1238+ /* !
1239+ @brief Serial UTF-8 decoder
1240+ @param c 8 bit value from encoded stream
1241+ @returns 0 if decoding is not complete yet, 16 bit code point
1242+ otherwise. Can cast to 8 bits for ASCII range (0-255)
1243+ */
1244+ /* *************************************************************************/
1245+
1246+ uint16_t Adafruit_GFX::decodeUTF8 (uint8_t c) {
1247+ // 7 bit Unicode Code Point
1248+ if ((c & 0x80 ) == 0x00 ) {
1249+ decoderState = 0 ;
1250+ return (uint16_t )c;
1251+ }
1252+
1253+ if (decoderState == 0 ) {
1254+ // 11 bit Unicode Code Point
1255+ if ((c & 0xE0 ) == 0xC0 ) {
1256+ decoderBuffer = ((c & 0x1F ) << 6 ); // Save first 5 bits
1257+ decoderState = 1 ;
1258+ return 0 ;
1259+ }
1260+
1261+ // 16 bit Unicode Code Point
1262+ if ((c & 0xF0 ) == 0xE0 ) {
1263+ decoderBuffer = ((c & 0x0F ) << 12 ); // Save first 4 bits
1264+ decoderState = 2 ;
1265+ return 0 ;
1266+ }
1267+
1268+ // 21 bit Unicode Code Point not supported so fall-back to extended ASCII
1269+ if ((c & 0xF8 ) == 0xF0 )
1270+ return (uint16_t )c;
1271+ } else {
1272+ if (decoderState == 2 ) {
1273+ decoderBuffer |=
1274+ ((c & 0x3F ) << 6 ); // Add next 6 bits of 16 bit code point
1275+ decoderState--;
1276+ return 0 ;
1277+ } else // decoderState must be == 1
1278+ {
1279+ decoderBuffer |= (c & 0x3F ); // Add last 6 bits of code point
1280+ decoderState = 0 ;
1281+ return decoderBuffer;
1282+ }
1283+ }
1284+
1285+ decoderState = 0 ;
1286+
1287+ return (uint16_t )c; // fall-back to extended ASCII
1288+ }
1289+
12361290/* *************************************************************************/
12371291/* !
12381292 @brief Print one byte/character of data, used to support print()
1239- @param c The 8-bit ascii character to write
1293+ @param data The 8-bit UTF-8 or ascii character to write
12401294*/
12411295/* *************************************************************************/
1242- size_t Adafruit_GFX::write (uint8_t c) {
1243- if (!gfxFont) { // 'Classic' built-in font
1296+ size_t Adafruit_GFX::write (uint8_t data) {
1297+ uint16_t c = (uint16_t )data;
1298+ if (_utf8)
1299+ c = decodeUTF8 (data);
1300+ if (c == 0 )
1301+ return 1 ;
12441302
1303+ if (!gfxFont) { // 'Classic' built-in font
1304+ if (c > 255 )
1305+ return 1 ; // Stop 16 bit characters
12451306 if (c == ' \n ' ) { // Newline?
12461307 cursor_x = 0 ; // Reset x to zero,
12471308 cursor_y += textsize_y * 8 ; // advance y one line
@@ -1262,9 +1323,10 @@ size_t Adafruit_GFX::write(uint8_t c) {
12621323 cursor_y +=
12631324 (int16_t )textsize_y * (uint8_t )pgm_read_byte (&gfxFont->yAdvance );
12641325 } else if (c != ' \r ' ) {
1265- uint8_t first = pgm_read_byte (&gfxFont->first );
1266- if ((c >= first) && (c <= (uint8_t )pgm_read_byte (&gfxFont->last ))) {
1267- GFXglyph *glyph = pgm_read_glyph_ptr (gfxFont, c - first);
1326+ uint16_t first = pgm_read_word (&gfxFont->first );
1327+ if ((c >= first) && (c <= pgm_read_word (&gfxFont->last ))) {
1328+ GFXglyph *glyph =
1329+ &(((GFXglyph *)pgm_read_pointer (&gfxFont->glyph ))[c - first]);
12681330 uint8_t w = pgm_read_byte (&glyph->width ),
12691331 h = pgm_read_byte (&glyph->height );
12701332 if ((w > 0 ) && (h > 0 )) { // Is there an associated bitmap?
@@ -1378,8 +1440,8 @@ void Adafruit_GFX::charBounds(unsigned char c, int16_t *x, int16_t *y,
13781440 *x = 0 ; // Reset x to zero, advance y by one line
13791441 *y += textsize_y * (uint8_t )pgm_read_byte (&gfxFont->yAdvance );
13801442 } else if (c != ' \r ' ) { // Not a carriage return; is normal char
1381- uint8_t first = pgm_read_byte (&gfxFont->first ),
1382- last = pgm_read_byte (&gfxFont->last );
1443+ uint16_t first = pgm_read_word (&gfxFont->first ),
1444+ last = pgm_read_word (&gfxFont->last );
13831445 if ((c >= first) && (c <= last)) { // Char present in this font?
13841446 GFXglyph *glyph = pgm_read_glyph_ptr (gfxFont, c - first);
13851447 uint8_t gw = pgm_read_byte (&glyph->width ),
0 commit comments