OpenFontRender icon indicating copy to clipboard operation
OpenFontRender copied to clipboard

Vertical centering with a line break

Open larrybud2004 opened this issue 8 months ago • 4 comments

I have a function which draws simple large text on the screen, e.g.

`

    ofr.setCursor(tft.width() / 2, tft.height() / 2);

ofr.setFontColor(GLT_COLOR_GOLD, TFT_BLACK);

ofr.setFontSize(46);

ofr.setAlignment(Align::BottomCenter);

ofr.printf(label);

`

This works great for a single line of text: image

But if my "label" has a line break, e.g. \n, the vertical alignment is not correct:

image

Am I doing this wrong or does the library not support this kind of thing?

larrybud2004 avatar Dec 17 '23 15:12 larrybud2004

You may be running in to the same thing I'm seeing in #38? What you're doing in code looks correct.

scottchiefbaker avatar Dec 18 '23 18:12 scottchiefbaker

Note sure it's the same issue directly, but perhaps related in how he calculates the text height? With my example, the single line is centered properly, however I have to set it to bottom alignment for it to look right. Might have just gotten lucky with this particular font size that bottom alignment is centered. If I choose Middle alignment, it ends up too low, which is what you're experiencing.

The alignment of top/middle/bottom is not intuitive at all, and I'm not clear on what it's supposed to be aligning to.

I just have a feeling the height calculation is being done incorrectly.

larrybud2004 avatar Dec 18 '23 18:12 larrybud2004

You can see a better visual example of the text alignment stuff with the code in this PR.

I suspect you are correct, and there is a minor bug in glyph height calculation. I tried looking at the alignment code but it went over my head pretty quick.

scottchiefbaker avatar Dec 18 '23 18:12 scottchiefbaker

Yeah, unfortunately I don't know much about how TTF are stored, or much about typesetting.

One thing that threw me for a minute when trying to set text centered in a box is when lower case letters were used that had a descender (g, y, q).

Not sure if that's related, but when trying to use the Middle alignment it didn't work as I expected it, as the descender was obviously included in the calculation for the height of the character.

For example, try aligning in the middle "Start" and "Play" and you'll see two different results because of the lowercase "y".

larrybud2004 avatar Dec 18 '23 19:12 larrybud2004