ttf 字体的一点问题

1,274次阅读
没有评论

我们的游戏引擎使用的是 stb 的 truetype 库 来处理 ttf 字体的。最近发现在使用公司提供的 阿里巴巴普惠体 时出了一点问题。

引擎渲染出来的汉字比标称的像素高度矮了不少,想渲染 100 像素高的汉字,结果只有 70+ 像素左右。我们之前测试使用的中文字体( Windows 自带默认字体)没有这个问题。

在网上翻了一下,找到这么一个帖子:https://github.com/nothings/stb/issues/689 以及 imgui 也遇到过类似问题

原来,stb truetype 库提供了两个计算字模大小的 api ,stbtt_ScaleForPixelHeight()stbtt_ScaleForMappingEmToPixels() 分别用两种方式计算字模高度比例。作者比较提倡用前一个 api ,但也保留了后一个 api 的使用场景。

computes a scale factor to produce a font whose EM size is mapped to ‘pixels’ tall. This is probably what traditional APIs compute, but I’m not positive.

另外在获取字体的 ascent 以及 decent 时,也有两个不同的 api : stbtt_GetFontVMetrics()stbtt_GetFontVMetricsOS2()

我很好奇的翻阅了这些 api 的实现,并查阅了能找到的关于 truetype 的文档,得出了下面的结论:

在字体文件中,有两处地方描述了字体的 ascent 和 descent 值。这个值应该是字体设计者创作时使用的像素大小。原本它们应该记录在字体文件头的 hhea 表中,它最初是由 apple 设计的。我们可以在 apple 的网站查到文档

后来,微软在设计字体时,可能是发现这些信息不够用,也或者是不赞成 apple 的某些设计决定。他们又设计了一张新的表叫 OS/2 ,里面也有类似的字段。文档在微软网站上可以找到

为什么两张表有很多重复的字段,在字体文件里放了两份(带来了冗余信息)?我猜恐怕和微软的代码实现风格有关:微软非常系统直接把内存结构映射为文件结构。所以他们选择加一整张表而不是只增加必要的字段在新的表中。

对于大部分字体来说,两张表的 ascent 和 descent 值是一样的。但正如 apple 文档中注释的,没有任何要求它们必须一致。而微软文档的一篇注解 则提到:

Some legacy platforms or applications may not use OS/2 fields at all. Thus, CJK fonts generally should have the same descender value recorded in hhea.descender, OS/2.sTypoDescender, and HorizAxis.ideo (if present) fields, and the same ascender value recorded in hhea.ascender, OS/2.sTypoAscender, and HorizAxis.idtp (if present) fields.

显然,阿里巴巴普惠体没有遵守这一点。

它在 hhea 表中,ascent = 1050 descent =-322 ,在 os/2 中,ascent = 850 descent = -150 。

stbtt_ScaleForPixelHeight() 计算缩放比时,使用的 pixel / (hhea.ascent – hhea.descent) 得到的值小于预期。

为何 stbtt_ScaleForMappingEmToPixels() 可以得到预期值?因为它使用的是 head 表中的 unitsPerEm 字段去计算的,这个值通常等于 ascent – descent (上面的文档中也提到了这一点)。在阿里巴巴普惠体文件中,它也等于 os2.sTypoAscender – os2.sTypoAscender 。

我不清楚为何这个字体会填入这样不同的数据。我猜测可能和 apple / ms 系统采用的默认屏幕 dpi 有关?它间接导致字体设计人员在打包字体时错误填写了某种参数?不过无论如何,我觉得是字体本身的 bug 。虽然,我通过修改我们引擎的代码绕开了这个问题。

 

Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 7 月
 12
3456789
10111213141516
17181920212223
24252627282930
31  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
钉钉又发新版本!把 AI 搬进每一次对话和会议

钉钉又发新版本!把 AI 搬进每一次对话和会议

钉钉又发新版本!把 AI 搬进每一次对话和会议 梦晨 2025-12-11 15:33:51 来源:量子位 A...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
预见未来:96位前沿先锋超万字核心观点总结,抢抓未来产业新高地

预见未来:96位前沿先锋超万字核心观点总结,抢抓未来产业新高地

预见未来:96位前沿先锋超万字核心观点总结,抢抓未来产业新高地 henry 2025-12-11 10:27:...
Meta公开抄阿里Qwen作业,还闭源了…

Meta公开抄阿里Qwen作业,还闭源了…

Meta公开抄阿里Qwen作业,还闭源了… Jay 2025-12-11 11:48:25 来源:量子位 Ja...
MEET2026挤爆了,AI圈今年最该听的20+场演讲&对谈都在这

MEET2026挤爆了,AI圈今年最该听的20+场演讲&对谈都在这

MEET2026挤爆了,AI圈今年最该听的20+场演讲&对谈都在这 西风 2025-12-11 15:...
钉钉又发新版本!把 AI 搬进每一次对话和会议

钉钉又发新版本!把 AI 搬进每一次对话和会议

钉钉又发新版本!把 AI 搬进每一次对话和会议 梦晨 2025-12-11 15:33:51 来源:量子位 A...