Skip to content

Commit 9ac47e8

Browse files
Laurence BankLaurence Bank
authored andcommitted
Sped up internal font drawing for opaque background color
1 parent 4a33fcd commit 9ac47e8

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

Adafruit_GFX.cpp

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,36 @@ void Adafruit_GFX::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
244244
fillRect(x, y, w, h, color);
245245
}
246246

247+
/**************************************************************************/
248+
/*!
249+
@brief set the address window (memory area), overwrite in subclasses if needed
250+
*/
251+
/**************************************************************************/
252+
void Adafruit_GFX::setAddrWindow(unsigned short x, unsigned short y, unsigned short w, unsigned short h)
253+
{
254+
(void)x; (void)y; (void)w; (void)h;
255+
}
256+
257+
/**************************************************************************/
258+
/*!
259+
@brief write len pixels of the given color, overwrite in subclasses if needed
260+
*/
261+
/**************************************************************************/
262+
void Adafruit_GFX::writeColor(uint16_t color, uint32_t len)
263+
{
264+
(void)color; (void)len;
265+
}
266+
267+
/**************************************************************************/
268+
/*!
269+
@brief write a buffer of pixels, overwrite in subclasses if needed
270+
*/
271+
void Adafruit_GFX::writePixels(uint16_t *colors, uint32_t len, bool block,
272+
bool bigEndian)
273+
{
274+
(void)colors; (void)len; (void)block; (void)bigEndian;
275+
}
276+
247277
/**************************************************************************/
248278
/*!
249279
@brief End a display-writing routine, overwrite in subclasses if
@@ -1147,28 +1177,41 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
11471177
c++; // Handle 'classic' charset behavior
11481178

11491179
startWrite();
1150-
for (int8_t i = 0; i < 5; i++) { // Char bitmap = 5 columns
1151-
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
1152-
for (int8_t j = 0; j < 8; j++, line >>= 1) {
1153-
if (line & 1) {
1154-
if (size_x == 1 && size_y == 1)
1155-
writePixel(x + i, y + j, color);
1156-
else
1157-
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y,
1180+
if (color != bg) { // faster opaque text
1181+
setAddrWindow(x, y, 5*size_x, 7*size_y);
1182+
for (int8_t j = 0; j < 7; j++) { // 7 lines
1183+
uint8_t uc, ucMask = (1 << j);
1184+
for (uint8_t k=0; k<size_y; k++) { // repeat size_y lines
1185+
for (uint8_t i=0; i<5; i++) { // 5 columns
1186+
uc = pgm_read_byte(&font[(c * 5) + i]);
1187+
writeColor((uc & ucMask) ? color:bg, size_x);
1188+
} // for each column
1189+
} // repeat for each line of size_y
1190+
} // for j
1191+
} else { // slower text which doesn't overwrite the background color
1192+
for (int8_t i = 0; i < 5; i++) { // Char bitmap = 5 columns
1193+
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
1194+
for (int8_t j = 0; j < 8; j++, line >>= 1) {
1195+
if (line & 1) {
1196+
if (size_x == 1 && size_y == 1)
1197+
writePixel(x + i, y + j, color);
1198+
else
1199+
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y,
11581200
color);
1159-
} else if (bg != color) {
1201+
} else if (bg != color) {
1202+
if (size_x == 1 && size_y == 1)
1203+
writePixel(x + i, y + j, bg);
1204+
else
1205+
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y, bg);
1206+
}
1207+
}
1208+
}
1209+
if (bg != color) { // If opaque, draw vertical line for last column
11601210
if (size_x == 1 && size_y == 1)
1161-
writePixel(x + i, y + j, bg);
1211+
writeFastVLine(x + 5, y, 8, bg);
11621212
else
1163-
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y, bg);
1213+
writeFillRect(x + 5 * size_x, y, size_x, 8 * size_y, bg);
11641214
}
1165-
}
1166-
}
1167-
if (bg != color) { // If opaque, draw vertical line for last column
1168-
if (size_x == 1 && size_y == 1)
1169-
writeFastVLine(x + 5, y, 8, bg);
1170-
else
1171-
writeFillRect(x + 5 * size_x, y, size_x, 8 * size_y, bg);
11721215
}
11731216
endWrite();
11741217

Adafruit_GFX.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Adafruit_GFX : public Print {
3232
// These MAY be overridden by the subclass to provide device-specific
3333
// optimized code. Otherwise 'generic' versions are used.
3434
virtual void startWrite(void);
35+
virtual void setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
36+
virtual void writeColor(uint16_t color, uint32_t len);
37+
virtual void writePixels(uint16_t *colors, uint32_t len, bool block,
38+
bool bigEndian);
3539
virtual void writePixel(int16_t x, int16_t y, uint16_t color);
3640
virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
3741
uint16_t color);

0 commit comments

Comments
 (0)