Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days.
0

GFX getTextBounds()
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

GFX getTextBounds()

by extan on Sat Nov 27, 2021 3:19 pm

I'm pretty sure I'm gonna feel dumb after hearing the answer, but for now I'm baffled by what I get back after calling:
Code: Select all | TOGGLE FULL SIZE
.getTextBounds(string, x, y, &x1, &y1, &w, &h);

like:
Code: Select all | TOGGLE FULL SIZE
const char HelloWorld[] = "Hello World!";
display.setFont(&FreeMonoBold9pt7b);
int16_t tbx, tby; uint16_t tbw, tbh;
display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
Serial.print("tbx: "); Serial.print(tbx); Serial.print(" tby: "); Serial.println(tby);
Serial.print("tbw: "); Serial.print(tbw); Serial.print(" tbh: "); Serial.println(tbh);


>>>
Code: Select all | TOGGLE FULL SIZE
tbx: 1 tby: -10
tbw: 127 tbh: 11


From the doc:
getTextBounds expects a string, a starting cursor X&Y position (the current cursor position will not be altered), and addresses of two signed and two unsigned 16-bit integers. These last four values will then contain the upper-left corner and the width & height of the area covered by this text — these can then be passed directly as arguments to fillRect().

...
setCursor(x, y), which will place the top left corner of the text wherever you please

and
rectangles and squares can be drawn and filled using the following procedures. Each accepts an X, Y pair for the top-left corner of the rectangle, a width and height (in pixels)

It seems that, in general, GFX defines text and rects relative to the top left corner of the region that contains them.
So I would expect getTextBounds(), when called with something like (HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh) to return something like:
Code: Select all | TOGGLE FULL SIZE
tbx: 1 tby: 1
tbw: 127 tbh: 11

tbx and tby containing (1,1) (or perhaps 0,0), representing the top left corner of the containing rect. Yet, I see (1,-10), which seems more like relative to the bottom left corner of the rect.

What am I missing here?
Attachments
lcds___displays_rect.png
lcds___displays_rect.png (5.73 KiB) Viewed 119 times

extan
 
Posts: 10
Joined: Sun Feb 11, 2018 8:00 pm

Re: GFX getTextBounds()

by extan on Mon Nov 29, 2021 10:37 pm

I'd really like to understand this.

It seems maybe this topic is mostly about hardware problems with adafruit products. Might you suggest a better place to post this question? #adafruit_support_bill #adafruit

Thanks

extan
 
Posts: 10
Joined: Sun Feb 11, 2018 8:00 pm

Please be positive and constructive with your questions and comments.