Skip to content

Commit 1a0b370

Browse files
committed
Add new anti-aliased (smooth) font examples + bug fixes
Four new examples have been added to show different methods of rendering the anta-aliased fonts. A few minor bugs have been corrected associated with the font rendering. drawNumber and drawFloat handle fixed width mono fonts with no digit movement.
1 parent 22177cf commit 1a0b370

29 files changed

+1033
-41
lines changed

Extensions/Smooth_font.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ void TFT_eSPI::drawGlyph(uint16_t code)
424424

425425
uint8_t pbuffer[gWidth[gNum]];
426426

427-
uint16_t xs = 0;
427+
int16_t xs = 0;
428428
uint32_t dl = 0;
429429

430430
int16_t cy = cursor_y + gFont.maxAscent - gdY[gNum];

Extensions/Smooth_font.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
uint16_t alphaBlend(uint8_t alpha, uint16_t fgc, uint16_t bgc);
1515

16-
void drawGlyph(uint16_t code);
16+
virtual void drawGlyph(uint16_t code);
17+
1718
void showFont(uint32_t td);
1819

1920
fs::File fontFile;

Extensions/Sprite.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,10 +1473,10 @@ void TFT_eSprite::drawGlyph(uint16_t code)
14731473
if (code == '\n') {
14741474
if (_created)
14751475
{
1476-
this->cursor_x = 0;
1477-
this->cursor_y += this->gFont.yAdvance;
1478-
if (this->cursor_y >= _height) this->cursor_y = 0;
1479-
return;
1476+
this->cursor_x = 0;
1477+
this->cursor_y += this->gFont.yAdvance;
1478+
if (this->cursor_y >= _height) this->cursor_y = 0;
1479+
return;
14801480
}
14811481
else
14821482
{
@@ -1511,7 +1511,7 @@ void TFT_eSprite::drawGlyph(uint16_t code)
15111511

15121512
uint8_t pbuffer[this->gWidth[gNum]];
15131513

1514-
uint16_t xs = 0;
1514+
int16_t xs = 0;
15151515
uint16_t dl = 0;
15161516

15171517
for (int y = 0; y < this->gHeight[gNum]; y++)
@@ -1525,7 +1525,8 @@ void TFT_eSprite::drawGlyph(uint16_t code)
15251525
if (pixel != 0xFF)
15261526
{
15271527
if (dl) { drawFastHLine( xs, y + this->cursor_y + this->gFont.maxAscent - this->gdY[gNum], dl, fg); dl = 0; }
1528-
if (pixel>127) drawPixel(x + this->cursor_x + this->gdX[gNum], y + this->cursor_y + this->gFont.maxAscent - this->gdY[gNum], alphaBlend(pixel, fg, bg));
1528+
if (_bpp != 1) drawPixel(x + this->cursor_x + this->gdX[gNum], y + this->cursor_y + this->gFont.maxAscent - this->gdY[gNum], alphaBlend(pixel, fg, bg));
1529+
else if (pixel>127) drawPixel(x + this->cursor_x + this->gdX[gNum], y + this->cursor_y + this->gFont.maxAscent - this->gdY[gNum], fg);
15291530
}
15301531
else
15311532
{

TFT_eSPI.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ TFT_eSPI::TFT_eSPI(int16_t w, int16_t h)
172172
textcolor = bitmap_fg = 0xFFFF; // White
173173
textbgcolor = bitmap_bg = 0x0000; // Black
174174
padX = 0; // No padding
175+
isDigits = false; // No bounding box adjustment
175176
textwrapX = true; // Wrap text at end of line when using print stream
176177
textwrapY = false; // Wrap text at bottom of screen when using print stream
177178
textdatum = TL_DATUM; // Top Left text alignment is default
@@ -1548,7 +1549,7 @@ void TFT_eSPI::fillCircleHelper(int32_t x0, int32_t y0, int32_t r, uint8_t corne
15481549
** Function name: drawEllipse
15491550
** Description: Draw a ellipse outline
15501551
***************************************************************************************/
1551-
void TFT_eSPI::drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
1552+
void TFT_eSPI::drawEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color)
15521553
{
15531554
if (rx<2) return;
15541555
if (ry<2) return;
@@ -1603,7 +1604,7 @@ void TFT_eSPI::drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint1
16031604
** Function name: fillEllipse
16041605
** Description: draw a filled ellipse
16051606
***************************************************************************************/
1606-
void TFT_eSPI::fillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
1607+
void TFT_eSPI::fillEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color)
16071608
{
16081609
if (rx<2) return;
16091610
if (ry<2) return;
@@ -2102,13 +2103,14 @@ int16_t TFT_eSPI::textWidth(const char *string, int font)
21022103
if (found)
21032104
{
21042105
if(str_width == 0 && gdX[gNum] < 0) str_width -= gdX[gNum];
2105-
if (*string) str_width += gxAdvance[gNum];
2106+
if (*string || isDigits) str_width += gxAdvance[gNum];
21062107
else str_width += (gdX[gNum] + gWidth[gNum]);
21072108
}
21082109
else str_width += gFont.spaceWidth + 1;
21092110
}
21102111
}
21112112
}
2113+
isDigits = false;
21122114
return str_width;
21132115
}
21142116
#endif
@@ -2141,8 +2143,8 @@ int16_t TFT_eSPI::textWidth(const char *string, int font)
21412143
{
21422144
uniCode -= pgm_read_byte(&gfxFont->first);
21432145
GFXglyph *glyph = &(((GFXglyph *)pgm_read_dword(&gfxFont->glyph))[uniCode]);
2144-
// If this is not the last character then use xAdvance
2145-
if (*string) str_width += pgm_read_byte(&glyph->xAdvance);
2146+
// If this is not the last character or is a digit then use xAdvance
2147+
if (*string || isDigits) str_width += pgm_read_byte(&glyph->xAdvance);
21462148
// Else use the offset plus width since this can be bigger than xAdvance
21472149
else str_width += ((int8_t)pgm_read_byte(&glyph->xOffset) + pgm_read_byte(&glyph->width));
21482150
}
@@ -2156,6 +2158,7 @@ int16_t TFT_eSPI::textWidth(const char *string, int font)
21562158
#endif
21572159
}
21582160
}
2161+
isDigits = false;
21592162
return str_width * textsize;
21602163
}
21612164

@@ -3715,11 +3718,11 @@ size_t TFT_eSPI::write(uint8_t utf8)
37153718

37163719
//fontFile = SPIFFS.open( _gFontFilename, "r" );
37173720

3718-
if(!fontFile)
3719-
{
3720-
fontLoaded = false;
3721-
return 0;
3722-
}
3721+
//if(!fontFile)
3722+
//{
3723+
// fontLoaded = false;
3724+
// return 0;
3725+
//}
37233726

37243727
drawGlyph(unicode);
37253728

@@ -4147,6 +4150,7 @@ int16_t TFT_eSPI::drawString(const char *string, int poX, int poY)
41474150
{
41484151
return drawString(string, poX, poY, textfont);
41494152
}
4153+
41504154
// With font number
41514155
int16_t TFT_eSPI::drawString(const char *string, int poX, int poY, int font)
41524156
{
@@ -4171,22 +4175,22 @@ int16_t TFT_eSPI::drawString(const char *string, int poX, int poY, int font)
41714175
}
41724176
#endif
41734177

4174-
if (textdatum || padX)
4175-
{
41764178

4177-
// If it is not font 1 (GLCD or free font) get the baseline and pixel height of the font
4179+
// If it is not font 1 (GLCD or free font) get the baseline and pixel height of the font
41784180
#ifdef SMOOTH_FONT
4179-
if(fontLoaded) {
4180-
baseline = gFont.maxAscent;
4181-
cheight = fontHeight(0);
4182-
}
4183-
4184-
else
4181+
if(fontLoaded) {
4182+
baseline = gFont.maxAscent;
4183+
cheight = fontHeight();
4184+
}
4185+
else
41854186
#endif
4186-
if (font!=1) {
4187-
baseline = pgm_read_byte( &fontdata[font].baseline ) * textsize;
4188-
cheight = fontHeight(font);
4189-
}
4187+
if (font!=1) {
4188+
baseline = pgm_read_byte( &fontdata[font].baseline ) * textsize;
4189+
cheight = fontHeight(font);
4190+
}
4191+
4192+
if (textdatum || padX)
4193+
{
41904194

41914195
switch(textdatum) {
41924196
case TC_DATUM:
@@ -4418,13 +4422,15 @@ int16_t TFT_eSPI::drawRightString(const char *string, int dX, int poY, int font)
44184422
***************************************************************************************/
44194423
int16_t TFT_eSPI::drawNumber(long long_num, int poX, int poY)
44204424
{
4425+
isDigits = true; // Eliminate jiggle in monospaced fonts
44214426
char str[12];
44224427
ltoa(long_num, str, 10);
44234428
return drawString(str, poX, poY, textfont);
44244429
}
44254430

44264431
int16_t TFT_eSPI::drawNumber(long long_num, int poX, int poY, int font)
44274432
{
4433+
isDigits = true; // Eliminate jiggle in monospaced fonts
44284434
char str[12];
44294435
ltoa(long_num, str, 10);
44304436
return drawString(str, poX, poY, font);
@@ -4444,6 +4450,7 @@ int16_t TFT_eSPI::drawFloat(float floatNumber, int dp, int poX, int poY)
44444450

44454451
int16_t TFT_eSPI::drawFloat(float floatNumber, int dp, int poX, int poY, int font)
44464452
{
4453+
isDigits = true;
44474454
char str[14]; // Array to contain decimal string
44484455
uint8_t ptr = 0; // Initialise pointer for array
44494456
int8_t digits = 1; // Count the digits to avoid array overflow
@@ -4572,7 +4579,7 @@ void TFT_eSPI::setTextFont(uint8_t f)
45724579

45734580

45744581
/***************************************************************************************
4575-
** Function name: spiBlockWrite
4582+
** Function name: writeBlock
45764583
** Description: Write a block of pixels of the same colour
45774584
***************************************************************************************/
45784585
//Clear screen test 76.8ms theoretical. 81.5ms TFT_eSPI, 967ms Adafruit_ILI9341
@@ -4964,6 +4971,10 @@ void TFT_eSPI::getSetup(setup_t &tft_settings)
49644971

49654972
#include "Extensions/Sprite.cpp"
49664973

4974+
// #ifdef ESP32
4975+
// #include "Extensions/pSprite.cpp"
4976+
// #endif
4977+
49674978
#ifdef SMOOTH_FONT
49684979
#include "Extensions/Smooth_font.cpp"
49694980
#endif

TFT_eSPI.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ class TFT_eSPI : public Print {
562562
fillCircle(int32_t x0, int32_t y0, int32_t r, uint32_t color),
563563
fillCircleHelper(int32_t x0, int32_t y0, int32_t r, uint8_t cornername, int32_t delta, uint32_t color),
564564

565-
drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color),
566-
fillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color),
565+
drawEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color),
566+
fillEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color),
567567

568568
drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color),
569569
fillTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color),
@@ -719,6 +719,7 @@ class TFT_eSPI : public Print {
719719
uint8_t glyph_ab, // glyph height above baseline
720720
glyph_bb; // glyph height below baseline
721721

722+
bool isDigits; // adjust bounding box for numbers to reduce visual jiggling
722723
bool textwrapX, textwrapY; // If set, 'wrap' text at right and optionally bottom edge of display
723724
bool _swapBytes; // Swap the byte order for TFT pushImage()
724725
bool locked, inTransaction; // Transaction and mutex lock flags for ESP32
@@ -749,4 +750,9 @@ class TFT_eSPI : public Print {
749750
// Load the Sprite Class
750751
#include "Extensions/Sprite.h"
751752

753+
// #ifdef ESP32
754+
// // Load the Sprite Class
755+
// #include "Extensions/pSprite.h"
756+
// #endif
757+
752758
#endif

User_Setup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@
236236
// #define SPI_FREQUENCY 40000000 // Maximum to use SPIFFS
237237
// #define SPI_FREQUENCY 80000000
238238

239-
#define SPI_READ_FREQUENCY 20000000 // Optional reduced SPI frequency for reading TFT
239+
// Optional reduced SPI frequency for reading TFT
240+
#define SPI_READ_FREQUENCY 20000000
240241

241242
// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
242243
#define SPI_TOUCH_FREQUENCY 2500000

User_Setup_Select.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
//#include <User_Setups/Setup20_ILI9488.h> // Setup file for ESP8266 and ILI9488 SPI bus TFT
4444
//#include <User_Setups/Setup21_ILI9488.h> // Setup file for ESP32 and ILI9488 SPI bus TFT
4545

46+
//#include <User_Setups/Setup43_ST7735.h> // Setup file configured for my ST7735S 80x160
47+
4648
//#include <User_Setups/SetupX_Template.h>
4749

4850

User_Setups/Setup1_ILI9341.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@
197197
// #define SPI_FREQUENCY 5000000
198198
// #define SPI_FREQUENCY 10000000
199199
// #define SPI_FREQUENCY 20000000
200-
#define SPI_FREQUENCY 27000000 // Actually sets it to 26.67MHz = 80/3
201-
// #define SPI_FREQUENCY 40000000 // Maximum to use SPIFFS
200+
// #define SPI_FREQUENCY 27000000 // Actually sets it to 26.67MHz = 80/3
201+
#define SPI_FREQUENCY 40000000 // Maximum to use SPIFFS
202202
// #define SPI_FREQUENCY 80000000
203203

204+
#define SPI_READ_FREQUENCY 20000000
205+
204206
// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
205207
#define SPI_TOUCH_FREQUENCY 2500000
206208

User_Setups/Setup21_ILI9488.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
//#define ILI9163_DRIVER
2020
//#define S6D02A1_DRIVER
2121
//#define RPI_ILI9486_DRIVER // 20MHz maximum SPI
22+
//#define HX8357D_DRIVER
23+
//#define ILI9481_DRIVER
24+
//#define ILI9486_DRIVER
2225
#define ILI9488_DRIVER
2326

2427
// For M5Stack ESP32 module with integrated display ONLY, remove // in line below

0 commit comments

Comments
 (0)