OpenType 字体文件组织结构
OpenType 字体文件结构
OpenType 字体的组织
https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font
OpenType 字体格式的关键特征是 TrueType 的 “封装”,它以常规和可扩展的方式,为表的集合提供组织方式。
表字典
开始的 12 个字节为表字典定义,这是字体中字典的顶级字典。如果字体文件仅仅包含一种字体,表字典将在文件中的位置 0 处开始。如果字体文件是一个 OpenType 字体集文件,那么该字体表字典的开始位置需要在 TTCHeader 中得到。
| Offset | Type | Name | Description |
|---|---|---|---|
| 0 | uint32 | sfntVersion | 0x00010000 or 0x4F54544F ('OTTO') — see below. |
| 4 | uint16 | numTables | Number of tables. |
| 6 | uint16 | searchRange | Maximum power of 2 less than or equal to numTables, times 16 ((2floor(log2(numTables))) * 16, where “” is an exponentiation operator). |
| 8 | uint16 | entrySelector | Log2 of the maximum power of 2 less than or equal to numTables (log2(searchRange/16), which is equal to floor(log2(numTables))). |
| 10 | uint16 | rangeShift | numTables times 16, minus searchRange ((numTables * 16) - searchRange). |
| 12 | tableRecord | tableRecords[numTables] | Table records 数组—one for each top-level table in the font |
从位置 12 开始为表记录,每条记录长度为 16 字节。所以表记录的总长度为记录数量 * 16。
表记录
每条表记录长度为 16 个字节,表记录的结构如下所示:
| Offset | Type | Name | Description |
|---|---|---|---|
| 0 | Tag | tableTag | 4 个字节长度的表标识名称 |
| 4 | uint32 | checksum | 校验和 |
| 8 | Offset32 | offset | 从字体文件开始的偏移量 |
| 12 | uint32 | length | 该表长度 |
注意:这里的偏移量是文件中从文件开始位置计算的的绝对位置。
次级表
命名表
命名表支持对于 OpenType 字体关联多语言的字符串。这些字符串可以表示版权提示,字体名称,字体族名称,样式名称以及其它内容。为了保持该表简短,字体厂商可能希望提供一个小的语言集,以后,字体可以被 "本地化"。然后添加翻译之后的字符串。
另外,OpenType 字体需要这些字符串作为语言无关的 命名 ID。在附加的语言变体中,该表也支持平台特定的字符编码变体,需要特定字符串的应用程序可以使用平台 ID、编码 ID、语言 ID 和名称 ID 进行搜索。注意,不同的平台可能包含不同的编码字符串的要求。
如果字体没有包含对于该平台的字符串,许多新的平台可以使用其它平台的字符串。因此,如果对于当前平台的字符串没有包含的话,一些应用程序可能显示错误的字符串。
https://docs.microsoft.com/en-us/typography/opentype/spec/name
命名表头
版本 0 的命名表如下组织,开始 6 个字节,然后是名称记录表。最后是实际的字符串数据存储。
每条名称记录长度为 12 字节。
以版本 0 为例,如果共有 21 条记录,那么记录表的长度为 21 * 12 = 252 字节,加上表头的 6 个字节,那么总共为 258 个字节。则 storageOffset 将为 258。
版本 0 的命名表头
| Offset | Type | Name | Description |
|---|---|---|---|
| 0 | uint16 | version | Table version number (=0). |
| 2 | uint16 | count | Number of name records. |
| 4 | Offset16 | storageOffset | 从该表位置为基准的字符串存储偏移量 |
| 8 | NameRecord | nameRecord[count] | The name records where count is the number of records. |
| (Variable) | Storage for the actual string data. |
见:https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-0
版本 1 命名表头
| Offset | Type | Name | Description |
|---|---|---|---|
| 0 | uint16 | version | Table version number (=1). |
| 2 | uint16 | count | Number of name records. |
| 4 | Offset16 | storageOffset | Offset to start of string storage (from start of table). |
| 8 | NameRecord | nameRecord[count] | The name records where count is the number of records. |
| uint16 | langTagCount | Number of language-tag records. | |
| LangTagRecord | langTagRecord[langTagCount] | The language-tag records where langTagCount is the number of records. | |
| (Variable) | Storage for the actual string data. |
见:https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-1
命名表记录
每条名称记录长度为 12 字节。
这里的偏移量使用以字节为单位的从存储区开始计算。所以,第一个记录的偏移量将为 0 。
| Type | Name | Description |
|---|---|---|
| uint16 | platformID | Platform ID. |
| uint16 | encodingID | Platform-specific encoding ID. |
| uint16 | languageID | Language ID. |
| uint16 | nameID | Name ID. |
| uint16 | length | String length (in bytes). |
| Offset16 | stringOffset | String offset from start of storage area (in bytes). |
见:https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records
其中:
- Platform ID,https://docs.microsoft.com/en-us/typography/opentype/spec/name#platform-ids
- 0: Unicode
- 1: Macintosh
- 2: Windows
- Platform-specific encoding ID,https://docs.microsoft.com/en-us/typography/opentype/spec/name#platform-specific-encoding-and-language-ids-unicode-platform-platform-id--0
- Name ID,https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-ids
字体集文件 TTC 文件结构
字体集文件包含一个 TTC 头结构,该 TTC 头结构必须位于 TTC 文件的开始部分。
版本 1 的 TTC 头
长度为 12 字节,开始的 4 个字节为 ttcf 串。最后是每种字体的偏移量表。偏移量是 4 个字节的整数表示。
| Type | Name | Description |
|---|---|---|
| TAG | ttcTag | Font Collection ID string: 'ttcf' (used for fonts with CFF or CFF2 outlines as well as TrueType outlines) |
| uint16 | majorVersion | Major version of the TTC Header, = 1. |
| uint16 | minorVersion | Minor version of the TTC Header, = 0. |
| uint32 | numFonts | Number of fonts in TTC |
| Offset32 | tableDirectoryOffsets[numFonts] | Array of offsets to the TableDirectory for each font from the beginning of the file |
然后每种字体的结构见前面的 OpenType 字体组织。
版本 2 的 TTC 头
| Type | Name | Description |
|---|---|---|
| TAG | ttcTag | Font Collection ID string: 'ttcf' |
| uint16 | majorVersion | Major version of the TTC Header, = 2. |
| uint16 | minorVersion | Minor version of the TTC Header, = 0. |
| uint32 | numFonts | Number of fonts in TTC |
| Offset32 | tableDirectoryOffsets[numFonts] | Array of offsets to the TableDirectory for each font from the beginning of the file |
| uint32 | dsigTag | Tag indicating that a DSIG table exists, 0x44534947 ('DSIG') (null if no signature) |
| uint32 | dsigLength | The length (in bytes) of the DSIG table (null if no signature) |
| uint32 | dsigOffset | The offset (in bytes) of the DSIG table from the beginning of the TTC file (null if no signature) |
引用
OpenType 字体文件组织结构的更多相关文章
- GetFontResourceInfo 函数: 获取字体文件对应的字体名
在 win 7, gdi32.dll,找不到 GetFontResourceInfo(), 但能找到 GetFontResourceInfoW(), 用法类似. //----------------- ...
- 把vux中的@font-face为base64格式的字体信息解码成可用的字体文件
在最近移动端项目中用到了vux,感觉用着还习惯,当把vux使用到PC端的时候出现了IE浏览器出现,这样的错误信息: CSS3114: @font-face 未能完成 OpenType 嵌入权限检查.权 ...
- [转]TrueType(TTF)字体文件裁剪(支持简体中文,繁体中文TTF字体裁剪)
原文入口: TTF字体文件裁剪(支持简体中文,繁体中文TTF字体裁剪) 对于TrueType(TTF)字体格式的介绍可以看: https://www.cnblogs.com/slysky/p/1131 ...
- @font-face字体文件用法
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- css字体文件
├── glyphicons-halflings-regular.eot├── glyphicons-halflings-regular.svg├── glyphicons-halflings-reg ...
- cocos2d-x 中 TTF 字体文件的位置
cocos2d-x 中,字体文件需要保存在 fonts 文件夹中,如果字体路径中没有 fonts/ 会自动添加上这个文件夹. 如果字体名称没有 .ttf 后缀,也会自动加上这个后缀. unsigned ...
- Android实例-使用自定义字体文件(XE8+小米2)
结果: 1.需要修改DELPHI自身的FMX.FontGlyphs.Android.pas,复制到程序的根目录下(红色部分为修改过的). 2.字体文件从 C:\Windows\Fonts 直接拷贝到A ...
- iOS使用自定义字体的方法(内置和任意下载ttf\otf\ttc字体文件)
最近做了个有关阅读的应用,使用了自定义字体,学习了一下这方面的知识. 1.首先是最简单也普遍的做法,打包内置字符库文件: 把字体库文件添加到工程,如font1.ttf添加到工程,然后在工程plist添 ...
- js分析 猫_眼_电_影 字体文件 @font-face
0. 参考 https://developer.mozilla.org/zh-CN/docs/Web/CSS/@font-face 这是一个叫做@font-face 的CSS @规则 ,它允许网页开发 ...
- js分析 天_眼_查 字体文件
0. 参考 js分析 猫_眼_电_影 字体文件 @font-face 1. 分析 1.1 定位目标元素 1.2 查看网页源代码 1.3 requests 请求提取得到大量错误信息 对比猫_眼_电_影抓 ...
随机推荐
- linux内核调试痛点之函数参数抓捕记
1.linux内核调试工具crash并不能直接显示函数参数,而这个对调试又非常重要 下面是工作中一个实际的问题,我们的进程hang在如下一个内核栈中了,通过栈回溯可知是打开了一个nfs3的网盘文件或者 ...
- 九问 GBase | 如何看待“科技制裁”?如何助力中国数据库国产化落地?
导读: Oracle.SAP.Apple.Google.Github等国际科技巨头纷纷宣布停止在俄罗斯业务,英特尔.AMD.戴尔等科技企业也被曝已中断向俄供货.当全面科技制裁来临,俄罗斯将如何应对此次 ...
- 前端面试题axaios携带 cookies
配置 axios.default.widthCredentials = true;
- 什么是 Ajax,Ajax 的原理,Ajax 都有哪些优点和缺点
ajax是异步的js和xml,是一种创建交互式网页的开发技术,是和服务器进行异步通讯的技术 : 核心就是使用XMLHttpRequest向服务器发送请求获取数据 : 优点: 页面不需要刷新,用户体验良 ...
- 21 如何写出一篇高质量的sci水文
博客配套视频链接: https://www.bilibili.com/video/BV1fW4y1W7dS/ b 站直接看 模型确定, 结果正在跑(或已结束), 目标期刊已定,一般可以定顶刊 从目标期 ...
- 还在使用昂贵的虚拟机?来试试 Devbox,便宜 6 倍!
这篇小短文来介绍一下用虚拟机的场景是怎么被 Devbox 全方位碾压的. Devbox 唯一弱点是公网出口的地方不分配独立的 IP 地址,但是这对我们绝大多数场景是没有影响的,通过域名和端口访问我们的 ...
- AI五子棋_02_03 Get传输数据 公共密钥
AI五子棋 第二步 恭喜你到达第二步! 警告:如果你是直接使用浏览器获得本页地址的话,请你返回第一步. 在前一步里,你得到的这样的返回结果 {"is_success": true, ...
- python调用imgkit将html转图片pdf问题实例wkhtmltox
wkhtmltox的下载地址:https://wkhtmltopdf.org/downloads.html 或者:https://github.com/wkhtmltopdf/wkhtmltopdf ...
- Webstorm 2024 安装使用 (附加永久激活码、补丁)
下载安装 第二步,安装完成之后,下载补丁 下载地址(里面包含激活码) 完成,之后输入激活码 免责声明:本文中的资源均来自互联网,仅供个人学习和交流使用,严禁用于商业行为,下载后请在24小时内从电脑中彻 ...
- 【2024.10.03】NOIP2024 赛前集训-刷题训练(5)
[2024.10.03]NOIP2024 赛前集训-刷题训练(5) NOIP2017 提高组 小凯的疑惑 形式化题面:求最大的正整数 \(w\),满足 \(ax + by = w\) 不存在一对非负整 ...