Skip to content

Allow the fixed fonts to be scaled up. #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
35 changes: 35 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,41 @@ Nothing
int h = YourScreen.textFontHeight();
```

### `textSize()`

#### Description

Set a text scale factor

#### Syntax

```
YourScreen.textSize(scale)
YourScreen.textSize(scaleX, scaleY)
```

#### Parameters

scale: scale factor used for both x and y
scaleX: x scale factor
scaleY: y scale factor

#### Returns

Nothing

#### Example

```
YourScreen.beginDraw();
YourScreen.clear();
YourScreen.stroke(255, 255, 255);
YourScreen.textFont(Font_5x7);
YourScreen.textSize(5);
YourScreen.text("abc", 0, 1);
YourScreen.endDraw();
```

### `set()`

#### Description
Expand Down
6 changes: 4 additions & 2 deletions examples/ASCIIDraw/ASCIIDraw.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

#include <ArduinoGraphics.h>

const byte canvasWidth = 61;
const byte canvasHeight = 27;
const byte fontSize = 3;
const byte canvasWidth = fontSize * (5 * 7) + 26;
const byte canvasHeight = fontSize * 7 + 20;

class ASCIIDrawClass : public ArduinoGraphics {
public:
Expand Down Expand Up @@ -85,6 +86,7 @@ void setup() {
ASCIIDraw.stroke('@', 0, 0);
const char text[] = "ARDUINO";
ASCIIDraw.textFont(Font_5x7);
ASCIIDraw.textSize(fontSize);
const byte textWidth = strlen(text) * ASCIIDraw.textFontWidth();
const byte textHeight = ASCIIDraw.textFontHeight();
const byte textX = (canvasWidth - textWidth) / 2;
Expand Down
57 changes: 36 additions & 21 deletions src/ArduinoGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
ArduinoGraphics::ArduinoGraphics(int width, int height) :
_width(width),
_height(height),
_font(NULL)
_font(NULL),
_textSizeX(1),
_textSizeY(1)
{
}

Expand Down Expand Up @@ -241,7 +243,7 @@ void ArduinoGraphics::text(const char* str, int x, int y)
uint8_t const c = (uint8_t)*str++;

if (c == '\n') {
y += _font->height;
y += _font->height * _textSizeY;
} else if (c == '\r') {
x = 0;
} else if (c == 0xc2 || c == 0xc3) {
Expand All @@ -254,10 +256,10 @@ void ArduinoGraphics::text(const char* str, int x, int y)
}

if (b) {
bitmap(b, x, y, _font->width, _font->height);
bitmap(b, x, y, _font->width, _font->height, _textSizeX, _textSizeY);
}

x += _font->width;
x += _font->width * _textSizeX;
}
}
}
Expand All @@ -269,38 +271,51 @@ void ArduinoGraphics::textFont(const Font& which)

int ArduinoGraphics::textFontWidth() const
{
return (_font ? _font->width : 0);
return (_font ? _font->width * _textSizeX : 0);
}

int ArduinoGraphics::textFontHeight() const
{
return (_font ? _font->height : 0);
return (_font ? _font->height* _textSizeY : 0);
}

void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int width, int height)
void ArduinoGraphics::textSize(uint8_t sx, uint8_t sy)
{
if (!_stroke) {
_textSizeX = (sx > 0)? sx : 1;
_textSizeY = (sy > 0)? sy : 1;
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x, uint8_t scale_y) {
if (!_stroke || !scale_x || !scale_y) {
return;
}

if ((data == NULL) || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > _height)) {
if ((data == nullptr) || ((x + (w * scale_x) < 0)) || ((y + (h * scale_y) < 0)) || (x > _width) || (y > _height)) {
// offscreen
return;
}

for (int j = 0; j < height; j++) {
int xStart = x;
for (int j = 0; j < h; j++) {
uint8_t b = data[j];

for (int i = 0; i < width; i++) {
if (b & (1 << (7 - i))) {
set(x + i, y + j, _strokeR, _strokeG, _strokeB);
} else {
set(x + i, y + j, _backgroundR, _backgroundG, _backgroundB);
for (uint8_t ys = 0; ys < scale_y; ys++) {
if (ys >= _height) return;
x = xStart; // reset for each row
for (int i = 0; i < w; i++) {
if (b & (1 << (7 - i))) {
for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _strokeR, _strokeG, _strokeB);
} else {
for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _backgroundR, _backgroundG, _backgroundB);
}
if (x >= _width) break;
}
y++;
}
}
}


void ArduinoGraphics::imageRGB(const Image& img, int x, int y, int width, int height)
{
const uint8_t* data = img.data();
Expand Down Expand Up @@ -359,7 +374,7 @@ void ArduinoGraphics::image(const Image& img, int x, int y)

void ArduinoGraphics::image(const Image& img, int x, int y, int width, int height)
{
if (!img || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > height)) {
if (!img || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > _height)) {
// offscreen
return;
}
Expand Down Expand Up @@ -438,7 +453,7 @@ void ArduinoGraphics::endText(int scrollDirection)
beginDraw();
int const text_x = _textX - i;
text(_textBuffer, text_x, _textY);
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height);
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textSizeX, _textSizeY);
endDraw();

delay(_textScrollSpeed);
Expand All @@ -450,7 +465,7 @@ void ArduinoGraphics::endText(int scrollDirection)
beginDraw();
int const text_x = _textX - (scrollLength - i - 1);
text(_textBuffer, text_x, _textY);
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height);
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textSizeX, _textSizeY);
endDraw();

delay(_textScrollSpeed);
Expand All @@ -462,7 +477,7 @@ void ArduinoGraphics::endText(int scrollDirection)
beginDraw();
int const text_y = _textY - i;
text(_textBuffer, _textX, text_y);
bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1);
bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1, _textSizeX, _textSizeY);
endDraw();

delay(_textScrollSpeed);
Expand All @@ -474,7 +489,7 @@ void ArduinoGraphics::endText(int scrollDirection)
beginDraw();
int const text_y = _textY - (scrollLength - i - 1);
text(_textBuffer, _textX, text_y);
bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1);
bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1, _textSizeX, _textSizeY);
endDraw();

delay(_textScrollSpeed);
Expand Down
7 changes: 6 additions & 1 deletion src/ArduinoGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class ArduinoGraphics : public Print {
virtual void text(const char* str, int x = 0, int y = 0);
virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); }
virtual void textFont(const Font& which);
virtual void textSize(uint8_t s) {textSize(s, s);}
virtual void textSize(uint8_t sx, uint8_t sy);

virtual int textFontWidth() const;
virtual int textFontHeight() const;
Expand All @@ -91,7 +93,8 @@ class ArduinoGraphics : public Print {
virtual void textScrollSpeed(unsigned long speed = 150);

protected:
virtual void bitmap(const uint8_t* data, int x, int y, int width, int height);
virtual void bitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x = 1,
uint8_t scale_y = 1);
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 @@ -114,6 +117,8 @@ class ArduinoGraphics : public Print {
uint8_t _textR, _textG, _textB;
int _textX;
int _textY;
uint8_t _textSizeX;
uint8_t _textSizeY;
unsigned long _textScrollSpeed;
};

Expand Down
Loading