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

getStrWidth() returns wrong estimation if "°" in input String #2578

Open
MaBaTa opened this issue Jan 20, 2025 · 4 comments
Open

getStrWidth() returns wrong estimation if "°" in input String #2578

MaBaTa opened this issue Jan 20, 2025 · 4 comments

Comments

@MaBaTa
Copy link

MaBaTa commented Jan 20, 2025

I have been tring to display a temperature horizontaly centered with the use of getStrWidth().

But the String is sligtly of to the left, so i assume the estimation of getStrWidth() counts the "°" as a wider symbol than it is.

Yes i have checked without the "°" symbol and with different fonts, it always worked without and never with the Symbol.

Happy if i can help to improve this absolutly great library.
Thanks!
MaBaTa

@olikraus
Copy link
Owner

Thanks for your feedback, however this is not a bug. Instead your string "°" probably contains an UTF-8 encoded glyph, which can't be handled by getStrWidth(), instead use getUTF8Width().
I have updated the documentation for getStrWidth() by adding a warning.

@MaBaTa
Copy link
Author

MaBaTa commented Jan 21, 2025

Thanks for your fast response!

after switching to getUTF8Width() everything works like expected, Thanks again!

with "°" i ment '°' so the degree symbol, i thought that isn't a special glyph.
And after checking it actually is part of the extended ASCII Table, so getStrWidth() works ony for the non extended one?

@MaBaTa
Copy link
Author

MaBaTa commented Jan 21, 2025

I have updated the documentation for getStrWidth() by adding a warning.

Little typo there by the way

@olikraus
Copy link
Owner

for the non extended one?

It has nothing todo with u8g2 or u8g2 fonts. The simple truth is: You have entered an UTF8 string, so you have to use the UTF8 U8g2 functions.

You might not know, but almost all editors (including Arduino IDE) will use UTF8.
Actually your string contains three bytes: 0xC2 0xB0 0x00 (the last byte is the string terminator)
The byte sequence 0xC2 0xB0 is called UTF-8 sequence and describes the degree symbol.

And yes, at the end you need a font which includes the degree symbol, otherwise the width will be unknown.

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

No branches or pull requests

2 participants