diff --git a/keywords.txt b/keywords.txt index 5c7d60b..c53d9c8 100644 --- a/keywords.txt +++ b/keywords.txt @@ -10,8 +10,8 @@ Font KEYWORD1 Image KEYWORD1 ########################################## -# Methods and Functions -########################################## +# Methods and Functions +########################################## begin KEYWORD2 end KEYWORD2 @@ -28,6 +28,8 @@ fill KEYWORD2 noFill KEYWORD2 stroke KEYWORD2 noStroke KEYWORD2 +setTextColor KEYWORD2 +noTextColor KEYWORD2 line KEYWORD2 point KEYWORD2 diff --git a/src/ArduinoGraphics.cpp b/src/ArduinoGraphics.cpp index bedd1a7..fb97385 100644 --- a/src/ArduinoGraphics.cpp +++ b/src/ArduinoGraphics.cpp @@ -124,6 +124,24 @@ void ArduinoGraphics::noStroke() _stroke = false; } +void ArduinoGraphics::setTextColor(uint8_t r, uint8_t g, uint8_t b) +{ + _text = true; + _textR = r; + _textG = g; + _textB = b; +} + +void ArduinoGraphics::setTextColor(uint32_t color) +{ + setTextColor(COLOR_R(color), COLOR_G(color), COLOR_B(color)); +} + +void ArduinoGraphics::noTextColor() +{ + _text = false; +} + void ArduinoGraphics::line(int x1, int y1, int x2, int y2) { if (!_stroke) { @@ -186,7 +204,7 @@ void ArduinoGraphics::rect(int x, int y, int width, int height) void ArduinoGraphics::text(const char* str, int x, int y) { - if (!_font || !_stroke) { + if (!_font || !_text) { return; } @@ -207,7 +225,7 @@ void ArduinoGraphics::text(const char* str, int x, int y) } if (b) { - bitmap(b, x, y, _font->width, _font->height); + text_bitmap(b, x, y, _font->width, _font->height); } x += _font->width; @@ -254,6 +272,30 @@ void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int width, int h } } +void ArduinoGraphics::text_bitmap(const uint8_t* data, int x, int y, int width, int height) +{ + if (!_text) { + return; + } + + if ((data == NULL) || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > height)) { + // offscreen + return; + } + + for (int j = 0; j < height; j++) { + uint8_t b = data[j]; + + for (int i = 0; i < width; i++) { + if (b & (1 << (7 - i))) { + set(x + i, y + j, _textR, _textG, _textB); + } else { + set(x + i, y + j, _backgroundR, _backgroundG, _backgroundB); + } + } + } +} + void ArduinoGraphics::imageRGB(const Image& img, int x, int y, int width, int height) { const uint8_t* data = img.data(); @@ -362,10 +404,7 @@ void ArduinoGraphics::beginText(int x, int y) void ArduinoGraphics::beginText(int x, int y, uint8_t r, uint8_t g, uint8_t b) { beginText(x, y); - - _textR = r; - _textG = g; - _textB = b; + setTextColor(r, g, b); } void ArduinoGraphics::beginText(int x, int y, uint32_t color) @@ -375,15 +414,6 @@ void ArduinoGraphics::beginText(int x, int y, uint32_t color) void ArduinoGraphics::endText(int scrollDirection) { - // backup the stroke color and set the color to the text color - bool strokeOn = _stroke; - uint8_t strokeR = _strokeR; - uint8_t strokeG = _strokeG; - uint8_t strokeB = _strokeB; - - - stroke(_textR, _textG, _textB); - if (scrollDirection == SCROLL_LEFT) { int scrollLength = _textBuffer.length() * textFontWidth() + _textX; @@ -429,14 +459,7 @@ void ArduinoGraphics::endText(int scrollDirection) text(_textBuffer, _textX, _textY); endDraw(); } - - // restore the stroke color - if (strokeOn) { - stroke(strokeR, strokeG, strokeB); - } else { - noStroke(); - } - + // clear the buffer _textBuffer = ""; } @@ -482,7 +505,7 @@ void ArduinoGraphics::lineHigh(int x1, int y1, int x2, int y2) xi = -1; dx = -dx; } - + int D = 2 * dx - dy; int x = x1; diff --git a/src/ArduinoGraphics.h b/src/ArduinoGraphics.h index f5468b0..5332a6b 100644 --- a/src/ArduinoGraphics.h +++ b/src/ArduinoGraphics.h @@ -56,6 +56,9 @@ class ArduinoGraphics : public Print { void stroke(uint8_t r, uint8_t g, uint8_t b); void stroke(uint32_t color); void noStroke(); + void setTextColor(uint8_t r, uint8_t g, uint8_t b); + void setTextColor(uint32_t color); + void noTextColor(); //virtual void arc(int x, int y, int width, int height, int start, int stop); //virtual void ellipse(int x, int y, int width, int height); @@ -90,6 +93,7 @@ class ArduinoGraphics : public Print { protected: virtual void bitmap(const uint8_t* data, int x, int y, int width, int height); + virtual void text_bitmap(const uint8_t* data, int x, int y, int width, int height); virtual void imageRGB(const Image& img, int x, int y, int width, int height); virtual void imageRGB24(const Image& img, int x, int y, int width, int height); virtual void imageRGB16(const Image& img, int x, int y, int width, int height); @@ -108,6 +112,7 @@ class ArduinoGraphics : public Print { uint8_t _fillR, _fillG, _fillB; uint8_t _strokeR, _strokeG, _strokeB; + bool _text; String _textBuffer; uint8_t _textR, _textG, _textB; int _textX;