Debian Codegraph @

internal/magic/font.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "slices"
7 )
8
9 // Woff matches a Web Open Font Format file.
10 func Woff(raw []byte, _ uint32) bool {
11 return bytes.HasPrefix(raw, []byte("wOFF"))
12 }
13
14 // Woff2 matches a Web Open Font Format version 2 file.
15 func Woff2(raw []byte, _ uint32) bool {
16 return bytes.HasPrefix(raw, []byte("wOF2"))
17 }
18
19 // Otf matches an OpenType font file.
20 func Otf(raw []byte, _ uint32) bool {
21 // After OTTO an little endian int16 specifies the number of tables.
22 // Since the number of tables cannot exceed 256, the first byte of the
23 // int16 is always 0. PUID: fmt/520
24 return len(raw) > 48 && bytes.HasPrefix(raw, []byte("OTTO\x00")) &&
25 bytes.Contains(raw[12:48], []byte("CFF "))
26 }
27
28 // Ttf matches a TrueType font file.
29 func Ttf(raw []byte, limit uint32) bool {
30 if !bytes.HasPrefix(raw, []byte{0x00, 0x01, 0x00, 0x00}) {
31 return false
32 }
33 // We cannot rely on the first 4 bytes because of false-positives.
34 // We have to digg deeper into the SFNT tables.
35 return hasSFNTTable(raw)
36 }
37
38 func hasSFNTTable(raw []byte) bool {
39 if len(raw) < 16 {
40 return false
41 }
42
43 // libmagic says there are 47 table names in specification, but it seems
44 // they reached 49 in the meantime.
45 // https://github.com/file/file/blob/5184ca2471c0e801c156ee120a90e669fe27b31d/magic/Magdir/fonts#L279
46 // At the same time, the TrueType docs seem misleading:
47 // 1. https://developer.apple.com/fonts/TrueType-Reference-Manual/index.html
48 // 2. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html
49 // Page 1. has 48 tables. Page 2. has 49 tables. The diff is the gcid table.
50 // Take a permissive approach.
51 possibleTables := []uint32{
52 0x61636e74, // "acnt"
53 0x616e6b72, // "ankr"
54 0x61766172, // "avar"
55 0x62646174, // "bdat"
56 0x62686564, // "bhed"
57 0x626c6f63, // "bloc"
58 0x62736c6e, // "bsln"
59 0x636d6170, // "cmap"
60 0x63766172, // "cvar"
61 0x63767420, // "cvt "
62 0x45425343, // "EBSC"
63 0x66647363, // "fdsc"
64 0x66656174, // "feat"
65 0x666d7478, // "fmtx"
66 0x666f6e64, // "fond"
67 0x6670676d, // "fpgm"
68 0x66766172, // "fvar"
69 0x67617370, // "gasp"
70 0x67636964, // "gcid"
71 0x676c7966, // "glyf"
72 0x67766172, // "gvar"
73 0x68646d78, // "hdmx"
74 0x68656164, // "head"
75 0x68686561, // "hhea"
76 0x686d7478, // "hmtx"
77 0x6876676c, // "hvgl"
78 0x6876706d, // "hvpm"
79 0x6a757374, // "just"
80 0x6b65726e, // "kern"
81 0x6b657278, // "kerx"
82 0x6c636172, // "lcar"
83 0x6c6f6361, // "loca"
84 0x6c746167, // "ltag"
85 0x6d617870, // "maxp"
86 0x6d657461, // "meta"
87 0x6d6f7274, // "mort"
88 0x6d6f7278, // "morx"
89 0x6e616d65, // "name"
90 0x6f706264, // "opbd"
91 0x4f532f32, // "OS/2"
92 // The above tables come from the original Apple TTF specification,
93 // but the later Microsoft specification has additional tables.
94 // Common tables: https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats
95 // Layout tables: https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2
96 // Even if the Microsoft specification says OpenType, the tables are
97 // valid for TrueType as well.
98 0x47535542, // "GSUB"
99 0x47504f53, // "GPOS"
100 0x42415345, // "BASE"
101 0x4a535446, // "JSTF"
102 0x47444546, // "GDEF"
103 0x4d415448, // "MATH"
104 0x43424454, // "CBDT"
105 0x43424c43, // "CBLC"
106 0x43464620, // "CFF "
107 0x43464632, // "CFF2"
108 0x434f4c52, // "COLR"
109 0x4350414c, // "CPAL"
110 0x44534947, // "DSIG"
111 0x45424454, // "EBDT"
112 0x45424c43, // "EBLC"
113 0x48564152, // "HVAR"
114 0x4c545348, // "LTSH"
115 0x4d455247, // "MERG"
116 0x4d564152, // "MVAR"
117 0x50434c54, // "PCLT"
118 0x706f7374, // "post"
119 0x70726570, // "prep"
120 0x73626978, // "sbix"
121 0x53544154, // "STAT"
122 0x53564720, // "SVG "
123 0x56444d58, // "VDMX"
124 0x76686561, // "vhea"
125 0x766d7478, // "vmtx"
126 0x564f5247, // "VORG"
127 0x56564152, // "VVAR"
128 }
129 ourTable := binary.BigEndian.Uint32(raw[12:16])
130 return slices.Contains(possibleTables, ourTable)
131 }
132
133 // Eot matches an Embedded OpenType font file.
134 func Eot(raw []byte, limit uint32) bool {
135 return len(raw) > 35 &&
136 bytes.Equal(raw[34:36], []byte{0x4C, 0x50}) &&
137 (bytes.Equal(raw[8:11], []byte{0x02, 0x00, 0x01}) ||
138 bytes.Equal(raw[8:11], []byte{0x01, 0x00, 0x00}) ||
139 bytes.Equal(raw[8:11], []byte{0x02, 0x00, 0x02}))
140 }
141
142 // Ttc matches a TrueType Collection font file.
143 func Ttc(raw []byte, limit uint32) bool {
144 return len(raw) > 7 &&
145 bytes.HasPrefix(raw, []byte("ttcf")) &&
146 (bytes.Equal(raw[4:8], []byte{0x00, 0x01, 0x00, 0x00}) ||
147 bytes.Equal(raw[4:8], []byte{0x00, 0x02, 0x00, 0x00}))
148 }