Debian Codegraph @

internal/magic/font.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6 )
7
8 // Woff matches a Web Open Font Format file.
9 func Woff(raw []byte, _ uint32) bool {
10 return bytes.HasPrefix(raw, []byte("wOFF"))
11 }
12
13 // Woff2 matches a Web Open Font Format version 2 file.
14 func Woff2(raw []byte, _ uint32) bool {
15 return bytes.HasPrefix(raw, []byte("wOF2"))
16 }
17
18 // Otf matches an OpenType font file.
19 func Otf(raw []byte, _ uint32) bool {
20 // After OTTO an little endian int16 specifies the number of tables.
21 // Since the number of tables cannot exceed 256, the first byte of the
22 // int16 is always 0. PUID: fmt/520
23 return len(raw) > 48 && bytes.HasPrefix(raw, []byte("OTTO\x00")) &&
24 bytes.Contains(raw[12:48], []byte("CFF "))
25 }
26
27 // Ttf matches a TrueType font file.
28 func Ttf(raw []byte, limit uint32) bool {
29 if !bytes.HasPrefix(raw, []byte{0x00, 0x01, 0x00, 0x00}) {
30 return false
31 }
32 return hasSFNTTable(raw)
33 }
34
35 func hasSFNTTable(raw []byte) bool {
36 // 49 possible tables as explained below
37 if len(raw) < 16 || binary.BigEndian.Uint16(raw[4:]) >= 49 {
38 return false
39 }
40
41 // libmagic says there are 47 table names in specification, but it seems
42 // they reached 49 in the meantime.
43 // https://github.com/file/file/blob/5184ca2471c0e801c156ee120a90e669fe27b31d/magic/Magdir/fonts#L279
44 // At the same time, the TrueType docs seem misleading:
45 // 1. https://developer.apple.com/fonts/TrueType-Reference-Manual/index.html
46 // 2. https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html
47 // Page 1. has 48 tables. Page 2. has 49 tables. The diff is the gcid table.
48 // Take a permissive approach.
49 possibleTables := []uint32{
50 0x61636e74, // "acnt"
51 0x616e6b72, // "ankr"
52 0x61766172, // "avar"
53 0x62646174, // "bdat"
54 0x62686564, // "bhed"
55 0x626c6f63, // "bloc"
56 0x62736c6e, // "bsln"
57 0x636d6170, // "cmap"
58 0x63766172, // "cvar"
59 0x63767420, // "cvt "
60 0x45425343, // "EBSC"
61 0x66647363, // "fdsc"
62 0x66656174, // "feat"
63 0x666d7478, // "fmtx"
64 0x666f6e64, // "fond"
65 0x6670676d, // "fpgm"
66 0x66766172, // "fvar"
67 0x67617370, // "gasp"
68 0x67636964, // "gcid"
69 0x676c7966, // "glyf"
70 0x67766172, // "gvar"
71 0x68646d78, // "hdmx"
72 0x68656164, // "head"
73 0x68686561, // "hhea"
74 0x686d7478, // "hmtx"
75 0x6876676c, // "hvgl"
76 0x6876706d, // "hvpm"
77 0x6a757374, // "just"
78 0x6b65726e, // "kern"
79 0x6b657278, // "kerx"
80 0x6c636172, // "lcar"
81 0x6c6f6361, // "loca"
82 0x6c746167, // "ltag"
83 0x6d617870, // "maxp"
84 0x6d657461, // "meta"
85 0x6d6f7274, // "mort"
86 0x6d6f7278, // "morx"
87 0x6e616d65, // "name"
88 0x6f706264, // "opbd"
89 0x4f532f32, // "OS/2"
90 }
91 ourTable := binary.BigEndian.Uint32(raw[12:16])
92 for _, t := range possibleTables {
93 if ourTable == t {
94 return true
95 }
96 }
97 return false
98 }
99
100 // Eot matches an Embedded OpenType font file.
101 func Eot(raw []byte, limit uint32) bool {
102 return len(raw) > 35 &&
103 bytes.Equal(raw[34:36], []byte{0x4C, 0x50}) &&
104 (bytes.Equal(raw[8:11], []byte{0x02, 0x00, 0x01}) ||
105 bytes.Equal(raw[8:11], []byte{0x01, 0x00, 0x00}) ||
106 bytes.Equal(raw[8:11], []byte{0x02, 0x00, 0x02}))
107 }
108
109 // Ttc matches a TrueType Collection font file.
110 func Ttc(raw []byte, limit uint32) bool {
111 return len(raw) > 7 &&
112 bytes.HasPrefix(raw, []byte("ttcf")) &&
113 (bytes.Equal(raw[4:8], []byte{0x00, 0x01, 0x00, 0x00}) ||
114 bytes.Equal(raw[4:8], []byte{0x00, 0x02, 0x00, 0x00}))
115 }