Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical centering with a line break #42

Open
larrybud2004 opened this issue Dec 17, 2023 · 4 comments
Open

Vertical centering with a line break #42

larrybud2004 opened this issue Dec 17, 2023 · 4 comments

Comments

@larrybud2004
Copy link

larrybud2004 commented Dec 17, 2023

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?

@scottchiefbaker
Copy link
Contributor

scottchiefbaker commented Dec 18, 2023

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

@larrybud2004
Copy link
Author

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.

@scottchiefbaker
Copy link
Contributor

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.

@larrybud2004
Copy link
Author

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants