Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Font KEYWORD1
Image KEYWORD1

##########################################
# Methods and Functions
##########################################
# Methods and Functions
##########################################

begin KEYWORD2
end KEYWORD2
Expand All @@ -28,6 +28,8 @@ fill KEYWORD2
noFill KEYWORD2
stroke KEYWORD2
noStroke KEYWORD2
setTextColor KEYWORD2
noTextColor KEYWORD2

line KEYWORD2
point KEYWORD2
Expand Down
71 changes: 47 additions & 24 deletions src/ArduinoGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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;

Expand Down Expand Up @@ -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 = "";
}
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions src/ArduinoGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down