Adafruit GFX getTextBounds, cursor position vs bounding box top left?

Hi

Using Adafruit GFX library and the getTextBounds, x,y is the cursor position, and x1,y1 returns the upper left position of the text bounding box, so how does these values differ from x and y?

From adafruit webpage:

tft.getTextBounds(string, x, y, &x1, &y1, &w, &h);

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().

I suggest to write a short example program calling the function, to see what the difference is, if any. The nice thing about Arduino is that you can do that very quickly, and clear up any confusion.

If the intent of the function and/or the documentation is not clear, continue with the example and add a filledRect() call to erase the area in question.

The x and y parameters specificy where, (in this hypothetical scenario), you would place the cursor. 0,0 often makes sense, but for this example lets imagine you use to x:20, y:20.

You also pass (the address of, &) x1 and y1. After getTextBounds() has run, x1 and y1 will have been set to the location of the top-left corner of the text.

For the default font: this is the same as x and y (20, 20)

For custom fonts: the text is often not drawn from the top-left corner. setCursor() often specifies where the first character would sit, if you were to handwrite it on a piece of ruled paper.

Handwriting

Most of the text will likely be above the cursor; a small part may be left of the cursor. getTextBounds() with x=20 y=20 could give you values like x1=18 y1=10.

AHa, ok, so different fonts may have different reference points for the cursor position. Then it makes sense. Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.