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.
English: Gotham Round (TTF)
Chinese: Noto Sans CJK (OTF)
Thai: Noto San Thai (TTF)
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.
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.
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.
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. Bothstb
andFreeType
backends show the same result.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 byfons__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 usingunit per em
. Now the font size is consistent across different font files.