memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.15k stars 771 forks source link

Fixes the issue that font size is inconsistent. #558

Closed olliwang closed 4 years ago

olliwang commented 4 years ago

This commit fixes the issue that font size is inconsistent between different font files. stb_truetype.h is also updated to the latest version to support additional font types such as OpenType fonts.

The inconsistent font size issue was mentioned at #382 and I experienced the same issue for my app a long time ago. I always see different font sizes (even if they set to the same value) with different font files on screen.

Here is the current NanoVG's implementation result compared to Apple's Pages app. There are three font files used for English / Chinese / Thai, respectively.

NanoVG Font Test 1

The Pages app shows bolder font for some reason, but that doesn't matter. Here we can see the stb backend can't render OpenType fonts. That's because NanoVG uses a very old stb_truetype.h by now. After updating the header file to the latest version. Both stb and FreeType backends show the same result.

NanoVG Font Test 2

However, the font sizes are still inconsistent. Only English character's font size is correct. After some investigation of fontstash.h. I found that this was caused by fons__tt_getPixelWidthScale(). The ascender and descender are used to calculate the font height and thus the scale. However, for some reason, the calculated font height diffs a lot in different font files. After some experiments, I managed to fix it by calculating the scale using unit per em. Now the font size is consistent across different font files.

NanoVG Font Test 3

memononen commented 4 years ago

Thanks @olliwang, excellent work as always!

olliwang commented 4 years ago

You are very welcome. Cheers! :D