memononen / nanovg

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

Set the same size, use different ttf file, the text size of the display difference #382

Open tailangjun opened 7 years ago

tailangjun commented 7 years ago

In order to show Simplified Chinese, I tried several different ttf files. I found that set the same size, use different ttf file, the text size of the display difference. Should we need to read out some parameters from ttf file and set to fontstash? 华文黑体.ttf is bigger than UILabel, use the same fontsize. PingFang Regular.ttf is smaller than UILabel, use the same fontsize.

1.use PingFang Regular.ttf usepingfang

2.华文黑体.ttf usehuawen

memononen commented 7 years ago

Can you post the values of fonsVertMetrics() for both fonts?

tailangjun commented 7 years ago

1.use PingFang Regular.ttf

pingfang

2.use 华文黑体.ttf

default
memononen commented 7 years ago

Looks like there's two ways to calculate the ascender and descender, one used by OSX and another one by Windows (of course!): https://stackoverflow.com/questions/19856094/computing-font-truetype-hhea-values-ascender-descender

stb_truetype (font rasterizer used by nanovg) is using the OSX variant. Can you try to change the following functions in stb_truetype.h, to use the windows variant.

STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)
{
    if (info->os2 != 0 && ttSHORT(info->data+info->os2 + 0) > 0) { // version > 0
        if (ascent ) *ascent  = ttSHORT(info->data+info->os2 + 68); // sTypoAscender
        if (descent) *descent = ttSHORT(info->data+info->os2 + 70); // sTypoDescender
        if (lineGap) *lineGap = ttSHORT(info->data+info->os2 + 72); // sTypoLineGap
    } else {
        if (ascent ) *ascent  = ttSHORT(info->data+info->hhea + 4);
        if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
        if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
    }
}
STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)
{
    int fheight, ascent = 0, descent = 0;
    stbtt_GetFontVMetrics(info, &ascent, &descent, NULL);

   fheight = ascent - descent;
   return (float) height / fheight;
}