Skip to content

Commit 2fbbfa2

Browse files
authored
Merge pull request #383 from bitbank2/master
Speed up internal font drawing for opaque background color
2 parents 4a33fcd + 77d05f5 commit 2fbbfa2

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

Adafruit_GFX.cpp

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,37 @@ 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
250+
needed
251+
@param x starting x coordinate
252+
@param y starting y coordinate
253+
@param w width in pixels
254+
@param h height in pixels
255+
*/
256+
/**************************************************************************/
257+
void Adafruit_GFX::setAddrWindow(uint16_t x, uint16_t y, uint16_t w,
258+
uint16_t h) {
259+
(void)x;
260+
(void)y;
261+
(void)w;
262+
(void)h;
263+
}
264+
265+
/**************************************************************************/
266+
/*!
267+
@brief write len pixels of the given color, overwrite in subclasses if
268+
needed
269+
@param color 16-bit 5-6-5 color to write
270+
@param len number of pixels to write
271+
*/
272+
/**************************************************************************/
273+
void Adafruit_GFX::writeColor(uint16_t color, uint32_t len) {
274+
(void)color;
275+
(void)len;
276+
}
277+
247278
/**************************************************************************/
248279
/*!
249280
@brief End a display-writing routine, overwrite in subclasses if
@@ -1147,28 +1178,41 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
11471178
c++; // Handle 'classic' charset behavior
11481179

11491180
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,
1158-
color);
1159-
} else if (bg != color) {
1160-
if (size_x == 1 && size_y == 1)
1161-
writePixel(x + i, y + j, bg);
1162-
else
1163-
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y, bg);
1181+
if (color != bg) { // faster opaque text
1182+
setAddrWindow(x, y, 5 * size_x, 7 * size_y);
1183+
for (int8_t j = 0; j < 7; j++) { // 7 lines
1184+
uint8_t uc, ucMask = (1 << j);
1185+
for (uint8_t k = 0; k < size_y; k++) { // repeat size_y lines
1186+
for (uint8_t i = 0; i < 5; i++) { // 5 columns
1187+
uc = pgm_read_byte(&font[(c * 5) + i]);
1188+
writeColor((uc & ucMask) ? color : bg, size_x);
1189+
} // for each column
1190+
} // repeat for each line of size_y
1191+
} // for j
1192+
} else { // slower text which doesn't overwrite the background color
1193+
for (int8_t i = 0; i < 5; i++) { // Char bitmap = 5 columns
1194+
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
1195+
for (int8_t j = 0; j < 8; j++, line >>= 1) {
1196+
if (line & 1) {
1197+
if (size_x == 1 && size_y == 1)
1198+
writePixel(x + i, y + j, color);
1199+
else
1200+
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y,
1201+
color);
1202+
} else if (bg != color) {
1203+
if (size_x == 1 && size_y == 1)
1204+
writePixel(x + i, y + j, bg);
1205+
else
1206+
writeFillRect(x + i * size_x, y + j * size_y, size_x, size_y, bg);
1207+
}
11641208
}
11651209
}
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);
1210+
if (bg != color) { // If opaque, draw vertical line for last column
1211+
if (size_x == 1 && size_y == 1)
1212+
writeFastVLine(x + 5, y, 8, bg);
1213+
else
1214+
writeFillRect(x + 5 * size_x, y, size_x, 8 * size_y, bg);
1215+
}
11721216
}
11731217
endWrite();
11741218

Adafruit_GFX.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ 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);
3537
virtual void writePixel(int16_t x, int16_t y, uint16_t color);
3638
virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
3739
uint16_t color);

0 commit comments

Comments
 (0)