Skip to content

Commit 0ea3666

Browse files
committed
Allow the fixed fonts to be scaled up.
On larger displays, the two built in fonts are almost invisible. One possible solution is to supply some larger fonts. Another approach is to do, like several TFT display drivers do and allow you to scale the system fixed fonts up to a larger size. Decided this was the easiest approach, and did a quick implementation of it. Like the Adafruit displays, I allowed you to set different scale factors for X and Y. I added a scaledBitmap function which is a modified version of the current bitmap function. I also changed all of the text functions to use it. I left the unscalled version of the API, although I have it simply call the scaled version with scales 1 and 1, as since these methods are virtual, potentially some other subclasses might use and/or implement their own version. Updated the textFontWidth and textFontHeight to multiply the height * the scale as the one example sketch here needs it. I also updated the sketch to scale the canvas to take the font size into account. This appears to work with the changes and I have not used or tested the scroll code. Its sizing should be updated teh same way the test sketch was with the changes with the textFontWidth and textFontHeight changes Update api.md Forgot to update the AsciiDraw example sketch Code Review requested changes _textsize_x -> _textSizeX (ditto for y) setTextSize() -> textSize() scaledBitmap -> bitmap with the extra parameters with defaults
1 parent 7709e46 commit 0ea3666

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

docs/api.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,41 @@ Nothing
641641
int h = YourScreen.textFontHeight();
642642
```
643643

644+
### `textSize()`
645+
646+
#### Description
647+
648+
Set a text scale factor
649+
650+
#### Syntax
651+
652+
```
653+
YourScreen.textSize(scale)
654+
YourScreen.textSize(scaleX, scaleY)
655+
```
656+
657+
#### Parameters
658+
659+
scale: scale factor used for both x and y
660+
scaleX: x scale factor
661+
scaleY: y scale factor
662+
663+
#### Returns
664+
665+
Nothing
666+
667+
#### Example
668+
669+
```
670+
YourScreen.beginDraw();
671+
YourScreen.clear();
672+
YourScreen.stroke(255, 255, 255);
673+
YourScreen.textFont(Font_5x7);
674+
YourScreen.textSize(5);
675+
YourScreen.text("abc", 0, 1);
676+
YourScreen.endDraw();
677+
```
678+
644679
### `set()`
645680

646681
#### Description

examples/ASCIIDraw/ASCIIDraw.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
#include <ArduinoGraphics.h>
1616

17-
const byte canvasWidth = 61;
18-
const byte canvasHeight = 27;
17+
const byte fontSize = 3;
18+
const byte canvasWidth = fontSize * (5 * 7) + 26;
19+
const byte canvasHeight = fontSize * 7 + 20;
1920

2021
class ASCIIDrawClass : public ArduinoGraphics {
2122
public:
@@ -85,6 +86,7 @@ void setup() {
8586
ASCIIDraw.stroke('@', 0, 0);
8687
const char text[] = "ARDUINO";
8788
ASCIIDraw.textFont(Font_5x7);
89+
ASCIIDraw.textSize(fontSize);
8890
const byte textWidth = strlen(text) * ASCIIDraw.textFontWidth();
8991
const byte textHeight = ASCIIDraw.textFontHeight();
9092
const byte textX = (canvasWidth - textWidth) / 2;

src/ArduinoGraphics.cpp

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
ArduinoGraphics::ArduinoGraphics(int width, int height) :
2727
_width(width),
2828
_height(height),
29-
_font(NULL)
29+
_font(NULL),
30+
_textSizeX(1),
31+
_textSizeY(1)
3032
{
3133
}
3234

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

243245
if (c == '\n') {
244-
y += _font->height;
246+
y += _font->height * _textSizeY;
245247
} else if (c == '\r') {
246248
x = 0;
247249
} else if (c == 0xc2 || c == 0xc3) {
@@ -254,10 +256,10 @@ void ArduinoGraphics::text(const char* str, int x, int y)
254256
}
255257

256258
if (b) {
257-
bitmap(b, x, y, _font->width, _font->height);
259+
bitmap(b, x, y, _font->width, _font->height, _textSizeX, _textSizeY);
258260
}
259261

260-
x += _font->width;
262+
x += _font->width * _textSizeX;
261263
}
262264
}
263265
}
@@ -269,38 +271,51 @@ void ArduinoGraphics::textFont(const Font& which)
269271

270272
int ArduinoGraphics::textFontWidth() const
271273
{
272-
return (_font ? _font->width : 0);
274+
return (_font ? _font->width * _textSizeX : 0);
273275
}
274276

275277
int ArduinoGraphics::textFontHeight() const
276278
{
277-
return (_font ? _font->height : 0);
279+
return (_font ? _font->height* _textSizeY : 0);
278280
}
279281

280-
void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int width, int height)
282+
void ArduinoGraphics::textSize(uint8_t sx, uint8_t sy)
281283
{
282-
if (!_stroke) {
284+
_textSizeX = (sx > 0)? sx : 1;
285+
_textSizeY = (sy > 0)? sy : 1;
286+
}
287+
288+
289+
void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x, uint8_t scale_y) {
290+
if (!_stroke || !scale_x || !scale_y) {
283291
return;
284292
}
285293

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

291-
for (int j = 0; j < height; j++) {
299+
int xStart = x;
300+
for (int j = 0; j < h; j++) {
292301
uint8_t b = data[j];
293-
294-
for (int i = 0; i < width; i++) {
295-
if (b & (1 << (7 - i))) {
296-
set(x + i, y + j, _strokeR, _strokeG, _strokeB);
297-
} else {
298-
set(x + i, y + j, _backgroundR, _backgroundG, _backgroundB);
302+
for (uint8_t ys = 0; ys < scale_y; ys++) {
303+
if (ys >= _height) return;
304+
x = xStart; // reset for each row
305+
for (int i = 0; i < w; i++) {
306+
if (b & (1 << (7 - i))) {
307+
for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _strokeR, _strokeG, _strokeB);
308+
} else {
309+
for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _backgroundR, _backgroundG, _backgroundB);
310+
}
311+
if (x >= _width) break;
299312
}
313+
y++;
300314
}
301315
}
302316
}
303317

318+
304319
void ArduinoGraphics::imageRGB(const Image& img, int x, int y, int width, int height)
305320
{
306321
const uint8_t* data = img.data();
@@ -359,7 +374,7 @@ void ArduinoGraphics::image(const Image& img, int x, int y)
359374

360375
void ArduinoGraphics::image(const Image& img, int x, int y, int width, int height)
361376
{
362-
if (!img || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > height)) {
377+
if (!img || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > _height)) {
363378
// offscreen
364379
return;
365380
}
@@ -438,7 +453,7 @@ void ArduinoGraphics::endText(int scrollDirection)
438453
beginDraw();
439454
int const text_x = _textX - i;
440455
text(_textBuffer, text_x, _textY);
441-
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height);
456+
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textSizeX, _textSizeY);
442457
endDraw();
443458

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

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

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

480495
delay(_textScrollSpeed);

src/ArduinoGraphics.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class ArduinoGraphics : public Print {
7070
virtual void text(const char* str, int x = 0, int y = 0);
7171
virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); }
7272
virtual void textFont(const Font& which);
73+
virtual void textSize(uint8_t s) {textSize(s, s);}
74+
virtual void textSize(uint8_t sx, uint8_t sy);
7375

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

9395
protected:
94-
virtual void bitmap(const uint8_t* data, int x, int y, int width, int height);
96+
virtual void bitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x = 1,
97+
uint8_t scale_y = 1);
9598
virtual void imageRGB(const Image& img, int x, int y, int width, int height);
9699
virtual void imageRGB24(const Image& img, int x, int y, int width, int height);
97100
virtual void imageRGB16(const Image& img, int x, int y, int width, int height);
@@ -114,6 +117,8 @@ class ArduinoGraphics : public Print {
114117
uint8_t _textR, _textG, _textB;
115118
int _textX;
116119
int _textY;
120+
uint8_t _textSizeX;
121+
uint8_t _textSizeY;
117122
unsigned long _textScrollSpeed;
118123
};
119124

0 commit comments

Comments
 (0)