openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.89k stars 2.55k forks source link

Fix ofTrueTypeFont: wrong width calc (only when string contains \n) #7868

Closed hiroMTB closed 7 months ago

hiroMTB commented 7 months ago

Problem

ofTrueTypeFont::stringWidth() and ofTrueTypeFont::getStringBoundingBox() give a wrong width value because these ignore \n, and width value piles up, getting longer and longer if you have multiple \n.

screenshots

Link to the current code

Tested with ...

Gist

https://gist.github.com/hiroMTB/e5c746c6403da714f74cf4f37b711451

dimitre commented 7 months ago

Great one! I'm wondering if in the future in another PR we can test calculating it inside tab getGlyphProperties(' ').advance property. I see a lot of duplicated code with getGlyphProperties(' ').advance * spaceSize * TAB_WIDTH and some of them are different than others like

g.props.advance + getGlyphProperties(' ').advance * spaceSize * TAB_WIDTH
hiroMTB commented 7 months ago

I agree, we could make a helper function like getTabWidth()

ofTrueTypeFont class needs good update and tidy up. But at the same time, it's easy to add another bug and hard to find it because we need to see output graphics.

That makes me(or contributers) nervous to touch this class, and avoid sending PR. It might be nice to have a test code. (e.g. check bounding box for different strings input, for different font, check kerning value etc, only for default font). Or render a png of font example app and compare before after the commit so that we make sure it doesn't break other feature.

hiroMTB commented 7 months ago

Also I found my changes have strange indent. I will fix it but it looks good on Xcode.

hiroMTB commented 7 months ago

ready for merge😀