Skip to content

Commit 01544e5

Browse files
authored
Merge pull request #228 from adafruit/revert-227-revert-226-master
Revert "Revert "Text magnification factor can be defined separately for X and for Y axis""
2 parents 63db7ab + 60e0e63 commit 01544e5

File tree

2 files changed

+144
-50
lines changed

2 files changed

+144
-50
lines changed

Adafruit_GFX.cpp

Lines changed: 131 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ WIDTH(w), HEIGHT(h)
8484
_height = HEIGHT;
8585
rotation = 0;
8686
cursor_y = cursor_x = 0;
87-
textsize = 1;
87+
textsize_x = textsize_y = 1;
8888
textcolor = textbgcolor = 0xFFFF;
8989
wrap = true;
9090
_cp437 = false;
@@ -772,7 +772,7 @@ void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
772772

773773
/**************************************************************************/
774774
/*!
775-
@brief Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP.
775+
@brief Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP.
776776
Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
777777
C Array can be directly used with this function.
778778
There is no RAM-resident version of this function; if generating bitmaps
@@ -807,7 +807,7 @@ void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
807807

808808
/**************************************************************************/
809809
/*!
810-
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
810+
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
811811
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
812812
@param x Top left corner x coordinate
813813
@param y Top left corner y coordinate
@@ -829,7 +829,7 @@ void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
829829

830830
/**************************************************************************/
831831
/*!
832-
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
832+
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
833833
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
834834
@param x Top left corner x coordinate
835835
@param y Top left corner y coordinate
@@ -916,7 +916,7 @@ void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
916916

917917
/**************************************************************************/
918918
/*!
919-
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
919+
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
920920
For 16-bit display devices; no color reduction performed.
921921
@param x Top left corner x coordinate
922922
@param y Top left corner y coordinate
@@ -938,7 +938,7 @@ void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
938938

939939
/**************************************************************************/
940940
/*!
941-
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
941+
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
942942
For 16-bit display devices; no color reduction performed.
943943
@param x Top left corner x coordinate
944944
@param y Top left corner y coordinate
@@ -1032,13 +1032,31 @@ void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
10321032
/**************************************************************************/
10331033
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
10341034
uint16_t color, uint16_t bg, uint8_t size) {
1035+
drawChar(x, y, c, color, bg, size, size);
1036+
}
1037+
1038+
// Draw a character
1039+
/**************************************************************************/
1040+
/*!
1041+
@brief Draw a single character
1042+
@param x Bottom left corner x coordinate
1043+
@param y Bottom left corner y coordinate
1044+
@param c The 8-bit font-indexed character (likely ascii)
1045+
@param color 16-bit 5-6-5 Color to draw chraracter with
1046+
@param bg 16-bit 5-6-5 Color to fill background with (if same as color, no background)
1047+
@param size_x Font magnification level in X-axis, 1 is 'original' size
1048+
@param size_y Font magnification level in Y-axis, 1 is 'original' size
1049+
*/
1050+
/**************************************************************************/
1051+
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
1052+
uint16_t color, uint16_t bg, uint8_t size_x, uint8_t size_y) {
10351053

10361054
if(!gfxFont) { // 'Classic' built-in font
10371055

10381056
if((x >= _width) || // Clip right
10391057
(y >= _height) || // Clip bottom
1040-
((x + 6 * size - 1) < 0) || // Clip left
1041-
((y + 8 * size - 1) < 0)) // Clip top
1058+
((x + 6 * size_x - 1) < 0) || // Clip left
1059+
((y + 8 * size_y - 1) < 0)) // Clip top
10421060
return;
10431061

10441062
if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
@@ -1048,21 +1066,21 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
10481066
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
10491067
for(int8_t j=0; j<8; j++, line >>= 1) {
10501068
if(line & 1) {
1051-
if(size == 1)
1069+
if(size_x == 1 && size_y == 1)
10521070
writePixel(x+i, y+j, color);
10531071
else
1054-
writeFillRect(x+i*size, y+j*size, size, size, color);
1072+
writeFillRect(x+i*size_x, y+j*size_y, size_x, size_y, color);
10551073
} else if(bg != color) {
1056-
if(size == 1)
1074+
if(size_x == 1 && size_y == 1)
10571075
writePixel(x+i, y+j, bg);
10581076
else
1059-
writeFillRect(x+i*size, y+j*size, size, size, bg);
1077+
writeFillRect(x+i*size_x, y+j*size_y, size_x, size_y, bg);
10601078
}
10611079
}
10621080
}
10631081
if(bg != color) { // If opaque, draw vertical line for last column
1064-
if(size == 1) writeFastVLine(x+5, y, 8, bg);
1065-
else writeFillRect(x+5*size, y, size, 8*size, bg);
1082+
if(size_x == 1 && size_y == 1) writeFastVLine(x+5, y, 8, bg);
1083+
else writeFillRect(x+5*size_x, y, size_x, 8*size_y, bg);
10661084
}
10671085
endWrite();
10681086

@@ -1084,7 +1102,7 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
10841102
uint8_t xx, yy, bits = 0, bit = 0;
10851103
int16_t xo16 = 0, yo16 = 0;
10861104

1087-
if(size > 1) {
1105+
if(size_x > 1 || size_y > 1) {
10881106
xo16 = xo;
10891107
yo16 = yo;
10901108
}
@@ -1114,11 +1132,11 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
11141132
bits = pgm_read_byte(&bitmap[bo++]);
11151133
}
11161134
if(bits & 0x80) {
1117-
if(size == 1) {
1135+
if(size_x == 1 && size_y == 1) {
11181136
writePixel(x+xo+xx, y+yo+yy, color);
11191137
} else {
1120-
writeFillRect(x+(xo16+xx)*size, y+(yo16+yy)*size,
1121-
size, size, color);
1138+
writeFillRect(x+(xo16+xx)*size_x, y+(yo16+yy)*size_y,
1139+
size_x, size_y, color);
11221140
}
11231141
}
11241142
bits <<= 1;
@@ -1139,21 +1157,21 @@ size_t Adafruit_GFX::write(uint8_t c) {
11391157

11401158
if(c == '\n') { // Newline?
11411159
cursor_x = 0; // Reset x to zero,
1142-
cursor_y += textsize * 8; // advance y one line
1160+
cursor_y += textsize_y * 8; // advance y one line
11431161
} else if(c != '\r') { // Ignore carriage returns
1144-
if(wrap && ((cursor_x + textsize * 6) > _width)) { // Off right?
1162+
if(wrap && ((cursor_x + textsize_x * 6) > _width)) { // Off right?
11451163
cursor_x = 0; // Reset x to zero,
1146-
cursor_y += textsize * 8; // advance y one line
1164+
cursor_y += textsize_y * 8; // advance y one line
11471165
}
1148-
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
1149-
cursor_x += textsize * 6; // Advance x one char
1166+
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize_x, textsize_y);
1167+
cursor_x += textsize_x * 6; // Advance x one char
11501168
}
11511169

11521170
} else { // Custom font
11531171

11541172
if(c == '\n') {
11551173
cursor_x = 0;
1156-
cursor_y += (int16_t)textsize *
1174+
cursor_y += (int16_t)textsize_y *
11571175
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
11581176
} else if(c != '\r') {
11591177
uint8_t first = pgm_read_byte(&gfxFont->first);
@@ -1164,14 +1182,14 @@ size_t Adafruit_GFX::write(uint8_t c) {
11641182
h = pgm_read_byte(&glyph->height);
11651183
if((w > 0) && (h > 0)) { // Is there an associated bitmap?
11661184
int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
1167-
if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) {
1185+
if(wrap && ((cursor_x + textsize_x * (xo + w)) > _width)) {
11681186
cursor_x = 0;
1169-
cursor_y += (int16_t)textsize *
1187+
cursor_y += (int16_t)textsize_y *
11701188
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
11711189
}
1172-
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
1190+
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize_x, textsize_y);
11731191
}
1174-
cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
1192+
cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize_x;
11751193
}
11761194
}
11771195

@@ -1186,7 +1204,19 @@ size_t Adafruit_GFX::write(uint8_t c) {
11861204
*/
11871205
/**************************************************************************/
11881206
void Adafruit_GFX::setTextSize(uint8_t s) {
1189-
textsize = (s > 0) ? s : 1;
1207+
setTextSize(s, s);
1208+
}
1209+
1210+
/**************************************************************************/
1211+
/*!
1212+
@brief Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger.
1213+
@param s_x Desired text width magnification level in X-axis. 1 is default
1214+
@param s_y Desired text width magnification level in Y-axis. 1 is default
1215+
*/
1216+
/**************************************************************************/
1217+
void Adafruit_GFX::setTextSize(uint8_t s_x, uint8_t s_y) {
1218+
textsize_x = (s_x > 0) ? s_x : 1;
1219+
textsize_y = (s_y > 0) ? s_y : 1;
11901220
}
11911221

11921222
/**************************************************************************/
@@ -1253,7 +1283,7 @@ void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
12531283

12541284
if(c == '\n') { // Newline?
12551285
*x = 0; // Reset x to zero, advance y by one line
1256-
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
1286+
*y += textsize_y * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
12571287
} else if(c != '\r') { // Not a carriage return; is normal char
12581288
uint8_t first = pgm_read_byte(&gfxFont->first),
12591289
last = pgm_read_byte(&gfxFont->last);
@@ -1265,41 +1295,42 @@ void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
12651295
xa = pgm_read_byte(&glyph->xAdvance);
12661296
int8_t xo = pgm_read_byte(&glyph->xOffset),
12671297
yo = pgm_read_byte(&glyph->yOffset);
1268-
if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) {
1298+
if(wrap && ((*x+(((int16_t)xo+gw)*textsize_x)) > _width)) {
12691299
*x = 0; // Reset x to zero, advance y by one line
1270-
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
1300+
*y += textsize_y * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
12711301
}
1272-
int16_t ts = (int16_t)textsize,
1273-
x1 = *x + xo * ts,
1274-
y1 = *y + yo * ts,
1275-
x2 = x1 + gw * ts - 1,
1276-
y2 = y1 + gh * ts - 1;
1302+
int16_t tsx = (int16_t)textsize_x,
1303+
tsy = (int16_t)textsize_y,
1304+
x1 = *x + xo * tsx,
1305+
y1 = *y + yo * tsy,
1306+
x2 = x1 + gw * tsx - 1,
1307+
y2 = y1 + gh * tsy - 1;
12771308
if(x1 < *minx) *minx = x1;
12781309
if(y1 < *miny) *miny = y1;
12791310
if(x2 > *maxx) *maxx = x2;
12801311
if(y2 > *maxy) *maxy = y2;
1281-
*x += xa * ts;
1312+
*x += xa * tsx;
12821313
}
12831314
}
12841315

12851316
} else { // Default font
12861317

12871318
if(c == '\n') { // Newline?
12881319
*x = 0; // Reset x to zero,
1289-
*y += textsize * 8; // advance y one line
1320+
*y += textsize_y * 8; // advance y one line
12901321
// min/max x/y unchaged -- that waits for next 'normal' character
12911322
} else if(c != '\r') { // Normal char; ignore carriage returns
1292-
if(wrap && ((*x + textsize * 6) > _width)) { // Off right?
1323+
if(wrap && ((*x + textsize_x * 6) > _width)) { // Off right?
12931324
*x = 0; // Reset x to zero,
1294-
*y += textsize * 8; // advance y one line
1325+
*y += textsize_y * 8; // advance y one line
12951326
}
1296-
int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char
1297-
y2 = *y + textsize * 8 - 1;
1327+
int x2 = *x + textsize_x * 6 - 1, // Lower-right pixel of char
1328+
y2 = *y + textsize_y * 8 - 1;
12981329
if(x2 > *maxx) *maxx = x2; // Track max x, y
12991330
if(y2 > *maxy) *maxy = y2;
13001331
if(*x < *minx) *minx = *x; // Track min x, y
13011332
if(*y < *miny) *miny = *y;
1302-
*x += textsize * 6; // Advance x one char
1333+
*x += textsize_x * 6; // Advance x one char
13031334
}
13041335
}
13051336
}
@@ -1441,6 +1472,33 @@ void Adafruit_GFX_Button::initButton(
14411472
textcolor, label, textsize);
14421473
}
14431474

1475+
/**************************************************************************/
1476+
/*!
1477+
@brief Initialize button with our desired color/size/settings
1478+
@param gfx Pointer to our display so we can draw to it!
1479+
@param x The X coordinate of the center of the button
1480+
@param y The Y coordinate of the center of the button
1481+
@param w Width of the buttton
1482+
@param h Height of the buttton
1483+
@param outline Color of the outline (16-bit 5-6-5 standard)
1484+
@param fill Color of the button fill (16-bit 5-6-5 standard)
1485+
@param textcolor Color of the button label (16-bit 5-6-5 standard)
1486+
@param label Ascii string of the text inside the button
1487+
@param textsize_x The font magnification in X-axis of the label text
1488+
@param textsize_y The font magnification in Y-axis of the label text
1489+
*/
1490+
/**************************************************************************/
1491+
// Classic initButton() function: pass center & size
1492+
void Adafruit_GFX_Button::initButton(
1493+
Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
1494+
uint16_t outline, uint16_t fill, uint16_t textcolor,
1495+
char *label, uint8_t textsize_x, uint8_t textsize_y)
1496+
{
1497+
// Tweak arguments and pass to the newer initButtonUL() function...
1498+
initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
1499+
textcolor, label, textsize_x, textsize_y);
1500+
}
1501+
14441502
/**************************************************************************/
14451503
/*!
14461504
@brief Initialize button with our desired color/size/settings, with upper-left coordinates
@@ -1460,6 +1518,30 @@ void Adafruit_GFX_Button::initButtonUL(
14601518
Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
14611519
uint16_t outline, uint16_t fill, uint16_t textcolor,
14621520
char *label, uint8_t textsize)
1521+
{
1522+
initButtonUL(gfx, x1, y1, w, h, outline, fill, textcolor, label, textsize, textsize);
1523+
}
1524+
1525+
/**************************************************************************/
1526+
/*!
1527+
@brief Initialize button with our desired color/size/settings, with upper-left coordinates
1528+
@param gfx Pointer to our display so we can draw to it!
1529+
@param x1 The X coordinate of the Upper-Left corner of the button
1530+
@param y1 The Y coordinate of the Upper-Left corner of the button
1531+
@param w Width of the buttton
1532+
@param h Height of the buttton
1533+
@param outline Color of the outline (16-bit 5-6-5 standard)
1534+
@param fill Color of the button fill (16-bit 5-6-5 standard)
1535+
@param textcolor Color of the button label (16-bit 5-6-5 standard)
1536+
@param label Ascii string of the text inside the button
1537+
@param textsize_x The font magnification in X-axis of the label text
1538+
@param textsize_y The font magnification in Y-axis of the label text
1539+
*/
1540+
/**************************************************************************/
1541+
void Adafruit_GFX_Button::initButtonUL(
1542+
Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
1543+
uint16_t outline, uint16_t fill, uint16_t textcolor,
1544+
char *label, uint8_t textsize_x, uint8_t textsize_y)
14631545
{
14641546
_x1 = x1;
14651547
_y1 = y1;
@@ -1468,7 +1550,8 @@ void Adafruit_GFX_Button::initButtonUL(
14681550
_outlinecolor = outline;
14691551
_fillcolor = fill;
14701552
_textcolor = textcolor;
1471-
_textsize = textsize;
1553+
_textsize_x = textsize_x;
1554+
_textsize_y = textsize_y;
14721555
_gfx = gfx;
14731556
strncpy(_label, label, 9);
14741557
}
@@ -1496,10 +1579,10 @@ void Adafruit_GFX_Button::drawButton(boolean inverted) {
14961579
_gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
14971580
_gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
14981581

1499-
_gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
1500-
_y1 + (_h/2) - (4 * _textsize));
1582+
_gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize_x),
1583+
_y1 + (_h/2) - (4 * _textsize_y));
15011584
_gfx->setTextColor(text);
1502-
_gfx->setTextSize(_textsize);
1585+
_gfx->setTextSize(_textsize_x, _textsize_y);
15031586
_gfx->print(_label);
15041587
}
15051588

0 commit comments

Comments
 (0)