Skip to content

Commit 9dd9cd8

Browse files
Added character bounds clipping to drawChar()
1 parent 0c792aa commit 9dd9cd8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

RGBmatrixPanel.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ size_t RGBmatrixPanel::write(uint8_t c) {
458458
void RGBmatrixPanel::write(uint8_t c) {
459459
#endif
460460
if (c == '\n') {
461-
cursor_y += textsize*8;
462-
cursor_x = 0;
461+
cursor_y += textsize * 8;
462+
cursor_x = 0;
463463
} else if (c == '\r') {
464464
// skip em
465465
} else {
@@ -474,9 +474,16 @@ void RGBmatrixPanel::write(uint8_t c) {
474474
// draw a character
475475
void RGBmatrixPanel::drawChar(
476476
int x, int y, char c, uint16_t color, uint8_t size) {
477-
for (uint8_t i =0; i<5; i++ ) {
477+
478+
if((x >= WIDTH) || // Clip right
479+
(y >= (nRows * 2)) || // Clip bottom
480+
((x + 5 * size - 1) < 0) || // Clip left
481+
((y + 8 * size - 1) < 0)) // Clip top
482+
return;
483+
484+
for (uint8_t i=0; i<5; i++ ) {
478485
uint8_t line = pgm_read_byte(font+(c*5)+i);
479-
for (uint8_t j = 0; j<8; j++) {
486+
for (uint8_t j=0; j<8; j++) {
480487
if (line & 0x1) {
481488
if (size == 1) // default size
482489
drawPixel(x+i, y+j, color);

0 commit comments

Comments
 (0)