Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

True Type Font 原理解析 #3

Open
RedCrazyGhost opened this issue Mar 9, 2023 · 0 comments
Open

True Type Font 原理解析 #3

RedCrazyGhost opened this issue Mar 9, 2023 · 0 comments
Labels
Apple fa-brands fa-apple

Comments

@RedCrazyGhost
Copy link
Owner

RedCrazyGhost commented Mar 9, 2023

前言

想快速了解TrueTypeFont的同学,请点击查看Apple 字体表

还未写完!

介绍

TrueTypeFont是由美国苹果公司和微软公司共同开发的一种字体标准。文件扩展名为.ttf

原理

mac/linux 系统可以使用hexdump -C命令查看ttf的二进制

hexdump -C *.ttf
00000000  00 01 00 00 00 10 01 00  00 04 00 00 47 50 4f 53  |............GPOS|
          |----1----| |-2-| |-3-|  |-4-| |-5-| |----6----|
00000010  44 76 4c 75 00 00 04 58  00 00 00 20 47 53 55 42  |DvLu...X... GSUB|
          |----7----| |----8----|  |----9----| |---10----|
00000020  3c 7e 2b 10 00 00 04 78  00 00 00 f0 4f 53 2f 32  |<~+....x....OS/2|
00000030  70 20 5e f1 00 00 02 3c  00 00 00 60 63 6d 61 70  |p ^....<...`cmap|
00000040  00 0c 00 84 00 00 02 9c  00 00 00 34 67 61 73 70  |...........4gasp|
00000050  00 00 00 0b 00 00 04 50  00 00 00 08 67 6c 79 66  |.......P....glyf|
00000060  89 10 b6 c1 00 00 01 0c  00 00 00 a0 68 65 61 64  |............head|
00000070  1b ce e9 70 00 00 01 d4  00 00 00 36 68 68 65 61  |...p.......6hhea|
00000080  08 8b 02 ba 00 00 02 18  00 00 00 24 68 6d 74 78  |...........$hmtx|
00000090  0a 26 01 ce 00 00 02 0c  00 00 00 0c 6c 6f 63 61  |.&..........loca|
000000a0  00 28 00 50 00 00 01 cc  00 00 00 08 6d 61 78 70  |.(.P........maxp|
000000b0  01 92 02 0b 00 00 01 ac  00 00 00 20 6e 61 6d 65  |........... name|
000000c0  19 74 33 e9 00 00 02 d8  00 00 01 58 70 6f 73 74  |.t3........Xpost|
000000d0  ff b8 00 32 00 00 04 30  00 00 00 20 70 72 65 70  |...2...0... prep|
000000e0  70 02 04 12 00 00 02 d0  00 00 00 08 76 68 65 61  |p...........vhea|
000000f0  04 64 00 d9 00 00 05 70  00 00 00 24 76 6d 74 78  |.d.....p...$vmtx|
00000100  04 7d 00 bf 00 00 05 68  00 00 00 08 00 01 00 5c  |.}.....h.......\|
00000110  ff af 01 9f 02 8b 00 18  00 00 13 26 35 34 3f 01  |...........&54?.|
00000120  3e 01 33 32 16 15 11 14  06 23 22 26 35 11 07 06  |>.32.....#"&5...|
00000130  23 22 26 68 0c 10 82 1e  30 17 26 26 2c 1b 1b 2a  |#"&h....0.&&,..*|
00000140  6b 06 06 0e 1d 01 da 1a  14 17 08 42 10 12 2f 27  |k..........B../'|
00000150  fd a6 14 18 18 14 02 12  39 03 17 00 00 01 00 78  |........9......x|
00000160  01 b3 03 53 02 f7 00 18  00 00 01 36 33 32 1f 01  |...S.......632..|
00000170  1e 01 15 14 06 23 21 22  26 35 34 36 33 21 27 26  |.....#!"&5463!'&|
00000180  35 34 36 02 a2 18 15 18  08 42 10 12 2f 27 fd a6  |546......B../'..|
00000190  14 17 17 14 02 12 39 03  17 02 ea 0d 11 82 1e 30  |......9........0|
000001a0  17 26 26 2b 1c 1b 2b 6a  06 06 0e 1d 00 01 00 00  |.&&+..+j........|
000001b0  00 03 01 81 00 2a 00 00  00 00 00 02 00 04 00 20  |.....*......... |
000001c0  00 60 00 60 01 00 00 08  00 00 00 00 00 00 00 00  |.`.`............|
000001d0  00 28 00 50 00 01 00 00  00 01 00 00 d9 4e b0 17  |.(.P.........N..|
000001e0  5f 0f 3c f5 00 03 03 e8  00 00 00 00 dc 01 d1 d8  |_.<.............|
000001f0  00 00 00 00 dc 01 d4 c2  ff 7e fe 1c 05 36 03 d4  |.........~...6..|
00000200  00 00 00 09 00 02 00 00  00 00 00 00 03 e8 00 fa  |................|
00000210  02 56 00 5c 03 e8 00 78  00 01 00 00 03 d4 fe 1c  |.V.\...x........|
00000220  00 00 05 4e ff 7e ff 4c  05 36 00 01 00 00 00 00  |...N.~.L.6......|
00000230  00 00 00 00 00 00 00 00  00 00 00 03 00 04 03 d8  |................|
00000240  01 90 00 05 00 08 02 8a  02 58 00 00 00 4b 02 8a  |.........X...K..|
00000250  02 58 00 00 01 5e 00 32  01 31 00 00 00 02 06 00  |.X...^.2.1......|
00000260  04 01 01 01 01 01 00 00  00 01 00 00 00 00 00 00  |................|
00000270  00 00 00 00 00 00 48 4e  59 49 01 40 00 31 00 31  |......HNYI.@.1.1|
00000280  03 20 ff 38 00 00 03 d4  01 e4 00 04 00 9f 00 00  |. .8............|
00000290  00 00 01 fd 02 b7 00 00  00 20 00 01 00 00 00 02  |......... ......|
000002a0  00 00 00 03 00 00 00 14  00 03 00 01 00 00 00 14  |................|
000002b0  00 04 00 20 00 00 00 04  00 04 00 01 00 00 00 31  |... ...........1|
000002c0  ff ff 00 00 00 31 ff ff  ff d0 00 01 00 00 00 00  |.....1..........|
000002d0  b8 01 ff 85 b8 00 04 8d  00 00 00 07 00 5a 00 03  |.............Z..|
000002e0  00 01 04 09 00 00 00 74  00 00 00 03 00 01 04 09  |.......t........|
000002f0  00 01 00 14 00 74 00 03  00 01 04 09 00 02 00 0e  |.....t..........|
00000300  00 88 00 03 00 01 04 09  00 03 00 3c 00 96 00 03  |...........<....|
00000310  00 01 04 09 00 04 00 14  00 74 00 03 00 01 04 09  |.........t......|
00000320  00 05 00 18 00 d2 00 03  00 01 04 09 00 06 00 14  |................|
00000330  00 ea 00 28 00 63 00 29  00 20 00 43 00 6f 00 70  |...(.c.). .C.o.p|
00000340  00 79 00 72 00 69 00 67  00 68 00 74 00 20 00 42  |.y.r.i.g.h.t. .B|
00000350  00 65 00 69 00 6a 00 69  00 6e 00 67 00 20 00 48  |.e.i.j.i.n.g. .H|
00000360  00 61 00 6e 00 79 00 69  00 20 00 49 00 6e 00 6e  |.a.n.y.i. .I.n.n|
00000370  00 6f 00 76 00 61 00 74  00 69 00 6f 00 6e 00 20  |.o.v.a.t.i.o.n. |
00000380  00 54 00 65 00 63 00 68  00 6e 00 6f 00 6c 00 6f  |.T.e.c.h.n.o.l.o|
00000390  00 67 00 79 00 20 00 43  00 6f 00 2e 00 2c 00 4c  |.g.y. .C.o...,.L|
000003a0  00 74 00 64 00 2e 00 48  00 59 00 43 00 75 00 59  |.t.d...H.Y.C.u.Y|
000003b0  00 75 00 61 00 6e 00 20  00 4a 00 52 00 65 00 67  |.u.a.n. .J.R.e.g|
000003c0  00 75 00 6c 00 61 00 72  00 48 00 61 00 6e 00 79  |.u.l.a.r.H.a.n.y|
000003d0  00 69 00 20 00 48 00 59  00 43 00 75 00 59 00 75  |.i. .H.Y.C.u.Y.u|
000003e0  00 61 00 6e 00 2d 00 4a  00 20 00 52 00 65 00 67  |.a.n.-.J. .R.e.g|
000003f0  00 75 00 6c 00 61 00 72  00 20 00 76 00 36 00 2e  |.u.l.a.r. .v.6..|
00000400  00 30 00 30 00 56 00 65  00 72 00 73 00 69 00 6f  |.0.0.V.e.r.s.i.o|
00000410  00 6e 00 20 00 36 00 2e  00 30 00 30 00 48 00 59  |.n. .6...0.0.H.Y|
00000420  00 43 00 75 00 59 00 75  00 61 00 6e 00 2d 00 4a  |.C.u.Y.u.a.n.-.J|
00000430  00 03 00 00 00 00 00 00  ff b5 00 32 00 00 00 00  |...........2....|
00000440  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000450  00 01 00 01 ff ff 00 0a  00 01 00 00 00 0a 00 1c  |................|
00000460  00 1e 00 01 44 46 4c 54  00 08 00 04 00 00 00 00  |....DFLT........|
00000470  ff ff 00 00 00 00 00 00  00 01 00 00 00 0a 00 5c  |...............\|
00000480  00 ca 00 04 44 46 4c 54  00 1a 63 79 72 6c 00 28  |....DFLT..cyrl.(|
00000490  67 72 65 6b 00 36 6c 61  74 6e 00 44 00 04 00 00  |grek.6latn.D....|
000004a0  00 00 ff ff 00 02 00 00  00 04 00 04 00 00 00 00  |................|
000004b0  ff ff 00 02 00 01 00 05  00 04 00 00 00 00 ff ff  |................|
000004c0  00 02 00 02 00 06 00 04  00 00 00 00 ff ff 00 02  |................|
000004d0  00 03 00 07 00 08 76 65  72 74 00 32 76 65 72 74  |......vert.2vert|
000004e0  00 38 76 65 72 74 00 40  76 65 72 74 00 48 76 72  |.8vert.@vert.Hvr|
000004f0  74 32 00 50 76 72 74 32  00 56 76 72 74 32 00 5e  |t2.Pvrt2.Vvrt2.^|
00000500  76 72 74 32 00 66 00 00  00 01 00 00 00 00 00 02  |vrt2.f..........|
00000510  00 00 00 02 00 00 00 02  00 00 00 01 00 00 00 02  |................|
00000520  00 00 00 03 00 00 00 01  00 04 00 00 00 02 00 04  |................|
00000530  00 06 00 00 00 02 00 04  00 05 00 00 00 02 00 04  |................|
00000540  00 07 00 08 00 12 00 12  00 12 00 12 00 12 00 12  |................|
00000550  00 12 00 12 00 01 00 00  00 01 00 08 00 01 00 06  |................|
00000560  00 01 00 01 00 01 00 01  03 e8 00 96 00 95 00 29  |...............)|
00000570  00 01 00 00 01 f4 fe 0c  00 00 03 e8 ff 4c fe e4  |.............L..|
00000580  03 20 00 00 00 01 00 00  00 00 00 00 00 00 00 00  |. ..............|
00000590  00 00 00 01                                       |....|
00000594
标记序号 类型 名称 解释
1 uint32 scalerType 0x74727565(true)和0x00010000都表示TTF格式
2 uint16 numTables 表数量
3 uint16 searchRange 搜索范围 (maximum的二次幂<=numTables)*16
4 uint16 entrySelector 条目选择器 log2(maximum的二次幂<=numTables)
5 uint16 rangeShift 范围偏移 (numTables*16-searchRange)
6、10 uint32 tag 4字节标识符
7 uint32 checkSum 校验和
8 uint32 offset 从sfnt的偏移量
9 uint32 length 表长度(字节长度)

tag : 0x47504f53 = GPOS
checkSum : 0x44764c75
offset : 0x00000458 = 1112
length : 0x00000020 = 32
GPOS的表格内容从1112的位置开始,总长度32,具体内容需要根据GPOS表格规则解析

参考内容

TrueTypeFont 维基百科

Apple 字体表

TTF字体格式入门 LiangJ

@RedCrazyGhost RedCrazyGhost added the Apple fa-brands fa-apple label Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apple fa-brands fa-apple
Projects
None yet
Development

No branches or pull requests

1 participant