Skip to content

Commit 6d1e09e

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
1 parent 7709e46 commit 6d1e09e

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

examples/ASCIIDraw/ASCIIDraw.ino

+4-2
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.setTextSize(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

+40-19
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+
_textsize_x(1),
31+
_textsize_y(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 * _textsize_y;
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+
scaledBitmap(b, x, y, _font->width, _font->height, _textsize_x, _textsize_y);
258260
}
259261

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

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

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

282+
void ArduinoGraphics::setTextSize(uint8_t sx, uint8_t sy)
283+
{
284+
_textsize_x = (sx > 0)? sx : 1;
285+
_textsize_y = (sy > 0)? sy : 1;
286+
}
287+
288+
280289
void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int width, int height)
281290
{
282-
if (!_stroke) {
291+
// forward to scaled version
292+
scaledBitmap(data, x, y, width, height, 1, 1);
293+
}
294+
295+
void ArduinoGraphics::scaledBitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x, uint8_t scale_y) {
296+
if (!_stroke || !scale_x || !scale_y) {
283297
return;
284298
}
285299

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

291-
for (int j = 0; j < height; j++) {
305+
int xStart = x;
306+
for (int j = 0; j < h; j++) {
292307
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);
308+
for (uint8_t ys = 0; ys < scale_y; ys++) {
309+
if (ys >= _height) return;
310+
x = xStart; // reset for each row
311+
for (int i = 0; i < w; i++) {
312+
if (b & (1 << (7 - i))) {
313+
for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _strokeR, _strokeG, _strokeB);
314+
} else {
315+
for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _backgroundR, _backgroundG, _backgroundB);
316+
}
317+
if (x >= _width) break;
299318
}
319+
y++;
300320
}
301321
}
302322
}
303323

324+
304325
void ArduinoGraphics::imageRGB(const Image& img, int x, int y, int width, int height)
305326
{
306327
const uint8_t* data = img.data();
@@ -438,7 +459,7 @@ void ArduinoGraphics::endText(int scrollDirection)
438459
beginDraw();
439460
int const text_x = _textX - i;
440461
text(_textBuffer, text_x, _textY);
441-
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height);
462+
scaledBitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textsize_x, _textsize_y);
442463
endDraw();
443464

444465
delay(_textScrollSpeed);
@@ -450,7 +471,7 @@ void ArduinoGraphics::endText(int scrollDirection)
450471
beginDraw();
451472
int const text_x = _textX - (scrollLength - i - 1);
452473
text(_textBuffer, text_x, _textY);
453-
bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height);
474+
scaledBitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textsize_x, _textsize_y);
454475
endDraw();
455476

456477
delay(_textScrollSpeed);
@@ -462,7 +483,7 @@ void ArduinoGraphics::endText(int scrollDirection)
462483
beginDraw();
463484
int const text_y = _textY - i;
464485
text(_textBuffer, _textX, text_y);
465-
bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1);
486+
scaledBitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1, _textsize_x, _textsize_y);
466487
endDraw();
467488

468489
delay(_textScrollSpeed);
@@ -474,7 +495,7 @@ void ArduinoGraphics::endText(int scrollDirection)
474495
beginDraw();
475496
int const text_y = _textY - (scrollLength - i - 1);
476497
text(_textBuffer, _textX, text_y);
477-
bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1);
498+
scaledBitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1, _textsize_x, _textsize_y);
478499
endDraw();
479500

480501
delay(_textScrollSpeed);

src/ArduinoGraphics.h

+6
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 setTextSize(uint8_t s) {setTextSize(s, s);}
74+
virtual void setTextSize(uint8_t sx, uint8_t sy);
7375

7476
virtual int textFontWidth() const;
7577
virtual int textFontHeight() const;
@@ -92,6 +94,8 @@ class ArduinoGraphics : public Print {
9294

9395
protected:
9496
virtual void bitmap(const uint8_t* data, int x, int y, int width, int height);
97+
virtual void scaledBitmap(const uint8_t* data, int x, int y, int width, int height,
98+
uint8_t scale_x, uint8_t);
9599
virtual void imageRGB(const Image& img, int x, int y, int width, int height);
96100
virtual void imageRGB24(const Image& img, int x, int y, int width, int height);
97101
virtual void imageRGB16(const Image& img, int x, int y, int width, int height);
@@ -114,6 +118,8 @@ class ArduinoGraphics : public Print {
114118
uint8_t _textR, _textG, _textB;
115119
int _textX;
116120
int _textY;
121+
uint8_t _textsize_x;
122+
uint8_t _textsize_y;
117123
unsigned long _textScrollSpeed;
118124
};
119125

0 commit comments

Comments
 (0)