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 请求提取得到大量错误信息 对比猫_眼_电_影抓 ...
随机推荐
- USB总线-Linux内核USB设备驱动ftrace分析(十一)
1.简介 USB Gadget Driver定义了很多trace event,使用者可以在用户空间通过ftrace接口,追踪USB Gadget Driver的行为. USB设备控制器驱动定义的tra ...
- USB Type-C的工作原理与技术分析
USB TYPE-C更加深入的应用,是从USB3.1开始的,这是因为从USB3.1开始,USB的功能开始变得更加丰富起来. USB 3.1基本规格 有SS字样的代表支持PD,有SS和10的USB标志代 ...
- Microsoft 发布 .NET 9 RC 2
距离最终版本还有一个月的时间,Microsoft 已经交付了 .NET 9 的第二个也是最后一个候选版本..NET 团队在公告帖子中写道[1],"当我们为 11 月的 .NET 9 正式发布 ...
- 活动预告 | 中国数据库联盟(ACDU)中国行定档深圳,一起揭秘数据库前沿技术
在当今数字化时代,数据库是各行各业中最核心的信息管理系统之一.随着技术的飞速发展,数据库领域也不断涌现出新的前沿技术和创新应用.数据库运维和开发人员需要紧跟前沿技术,才能保持竞争力,并实现更高效.更智 ...
- 自定义指令 v-imgerror 当图片的 src 资源 无效 就替换 默认的 src 显示图片
// 回顾自定义指令 // 作用 : 自定义一些对dom操作的快捷指令 // 前提:指令就是用来操作 dom (v-if /v-show/v-for ....) // 语法:Vue.directive ...
- webpack 的优点
1. 社区庞大,活跃,紧跟技术的前言,不断发展迭代 : 2. 专注处理模块化的项目,可以开箱即用 : 3. 通过 loader 扩展,可以把所有类型的文件解析打包 : 4. 通过plugin 扩展 , ...
- 中国移动基于 Kubernetes 的物联网边缘计算应用实践
作者:何毓川,中移物联网,云计算开发高级工程师 EdgeBox简介 中移物联网是中国移动集团在物联网方向的专业研发子公司,在各个垂直行业都有非常丰富和完成的解决方案. 本文通过中移物联网的物联网边缘计 ...
- 在 KubeSphere 上部署 Apache Pulsar
作者介绍:徐文涛,StreamNative Content Strategist,热爱云原生与开源技术,活跃于本地化/文档/技术博客贡献,持有 K8s CKA/CKAD/CKS 认证. Apache ...
- nvm安装使用教程
一.简介 既然你来了,那就不用解释太多,只需要知道 nvm是一款nodejs版本管理工具,通过它可以让我们切换不同版本的 nodejs. 二.下载nvm 1.在安装nvm之前,你要先确定是否安装了no ...
- Cartographer的扫描匹配
cartographer 代码思想解读(1)- 相关匹配 cartographer在2016年开源后一直在使用,但是一直未仔细阅读并分析其核心代码结构.目前网上可以找到许多博主对其分析和理解.其car ...