oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1package typeface
2
3import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "testing"
8)
9
10// Outlines are checked before a file is kept, as far as it is cheap to:
11// for TrueType, that loca's offsets climb and stay inside glyf, that each
12// glyph holds at least its header, and that maxp's glyph count agrees
13// with loca and hmtx; for CFF and CFF2, that the header and the top-level
14// INDEXes parse inside the table and the CharStrings count is maxp's.
15// Outline-table presence alone was taken as validation before (final
16// review, I1). A WOFF2's transformed glyf and loca are not rebuilt, so
17// they are not checked here; the browser's own sanitiser still is.
18
19// buildIndex is a CFF INDEX of items: a count of countSize bytes (2 in
20// CFF, 4 in CFF2), then 4-byte offsets and the data.
21func buildIndex(countSize int, items ...[]byte) []byte {
22 var b []byte
23 if countSize == 4 {
24 b = binary.BigEndian.AppendUint32(b, uint32(len(items)))
25 } else {
26 b = binary.BigEndian.AppendUint16(b, uint16(len(items)))
27 }
28 if len(items) == 0 {
29 return b
30 }
31 b = append(b, 4)
32 off := uint32(1)
33 b = binary.BigEndian.AppendUint32(b, off)
34 for _, it := range items {
35 off += uint32(len(it))
36 b = binary.BigEndian.AppendUint32(b, off)
37 }
38 for _, it := range items {
39 b = append(b, it...)
40 }
41 return b
42}
43
44// charStrings is n glyphs, each a lone endchar.
45func charStrings(n int) [][]byte {
46 out := make([][]byte, n)
47 for i := range out {
48 out[i] = []byte{14}
49 }
50 return out
51}
52
53// dictInt is a DICT operand in its five-byte form, so a DICT's length
54// does not depend on the numbers in it.
55func dictInt(v int32) []byte {
56 return binary.BigEndian.AppendUint32([]byte{29}, uint32(v))
57}
58
59// cffTable is a CFF table with one font of n glyphs: header, Name INDEX,
60// Top DICT INDEX (CharStrings only), empty String and Global Subr
61// INDEXes, then the CharStrings INDEX.
62func cffTable(n int) []byte {
63 header := []byte{1, 0, 4, 4}
64 name := buildIndex(2, []byte("Test"))
65 empty := buildIndex(2)
66 // The Top DICT INDEX's size is fixed (one item of 6 bytes), so the
67 // CharStrings offset can be worked out before it is written.
68 topLen := len(buildIndex(2, make([]byte, 6)))
69 at := len(header) + len(name) + topLen + 2*len(empty)
70 top := buildIndex(2, append(dictInt(int32(at)), 17))
71 b := append(append(append(header, name...), top...), empty...)
72 b = append(b, empty...)
73 return append(b, buildIndex(2, charStrings(n)...)...)
74}
75
76// cff2Table is a CFF2 table of n glyphs: header, Top DICT (CharStrings
77// only), an empty Global Subr INDEX, then the CharStrings INDEX.
78func cff2Table(n int) []byte {
79 const headerSize, topLen = 5, 6
80 at := headerSize + topLen + 4
81 top := append(dictInt(int32(at)), 17)
82 b := []byte{2, 0, headerSize}
83 b = binary.BigEndian.AppendUint16(b, topLen)
84 b = append(b, top...)
85 b = append(b, buildIndex(4)...)
86 return append(b, buildIndex(4, charStrings(n)...)...)
87}
88
89// numGlyphs is maxp's glyph count in tables ts.
90func numGlyphs(ts []table) int {
91 return int(binary.BigEndian.Uint16(tableData(ts, "maxp")[4:]))
92}
93
94// otto is Liberation's other tables in an OTTO, with outlines cffTag
95// built by make for Liberation's glyph count, where glyf and loca were.
96func otto(t *testing.T, cffTag string, make func(n int) []byte) []byte {
97 t.Helper()
98 _, ts := tablesOf(t, liberation(t))
99 ts = withoutTable(ts, "glyf", "loca")
100 return buildSFNT(sigOTTO, append(ts, table{cffTag, make(numGlyphs(ts))}))
101}
102
103// liberationWith is Liberation with table tag changed by edit.
104func liberationWith(t *testing.T, tag string, edit func(b []byte) []byte) []byte {
105 t.Helper()
106 v, ts := tablesOf(t, liberation(t))
107 return buildSFNT(v, withTable(ts, tag, edit(bytes.Clone(tableData(ts, tag)))))
108}
109
110// locaAt and setLoca read and write Liberation's long-format loca.
111func locaAt(b []byte, i int) uint32 { return binary.BigEndian.Uint32(b[4*i:]) }
112func setLoca(b []byte, i int, v uint32) { binary.BigEndian.PutUint32(b[4*i:], v) }
113
114// someGlyph is the first glyph after .notdef longer than n bytes.
115func someGlyph(t *testing.T, loca []byte, n uint32) int {
116 t.Helper()
117 for i := 1; 4*(i+2) <= len(loca); i++ {
118 if locaAt(loca, i+1)-locaAt(loca, i) > n {
119 return i
120 }
121 }
122 t.Fatal("no glyph long enough")
123 return 0
124}
125
126func TestInspectReadsGenuineCFFAndCFF2(t *testing.T) {
127 for _, c := range []struct {
128 name string
129 data []byte
130 }{
131 {"cff", otto(t, "CFF ", cffTable)},
132 {"cff2", otto(t, "CFF2", cff2Table)},
133 {"woff of a cff", buildWOFF(t, otto(t, "CFF ", cffTable))},
134 {"woff2 of a cff2", buildWOFF2(t, otto(t, "CFF2", cff2Table))},
135 } {
136 t.Run(c.name, func(t *testing.T) {
137 if f := mustInspect(t, c.data); f.Outline != CFF {
138 t.Errorf("Outline = %q", f.Outline)
139 }
140 })
141 }
142}
143
144// A WOFF2's transformed glyf and loca are not rebuilt, so their bytes are
145// not checked: the file is taken on its other tables.
146func TestInspectTakesATransformedWOFF2Glyf(t *testing.T) {
147 flavor, es, _ := woff2Parts(t, liberation(t))
148 var stream []byte
149 _, ts := tablesOf(t, liberation(t))
150 for i := range es {
151 switch es[i].tag {
152 case "glyf":
153 es[i].version, es[i].transform = 0, 64
154 stream = append(stream, make([]byte, 64)...)
155 case "loca":
156 es[i].version, es[i].transform = 0, 0
157 default:
158 stream = append(stream, tableData(ts, es[i].tag)...)
159 }
160 }
161 mustInspect(t, assembleWOFF2(flavor, es, brotliOf(stream)))
162}
163
164func TestInspectRefusesBrokenOutlines(t *testing.T) {
165 _, ts := tablesOf(t, liberation(t))
166 loca := tableData(ts, "loca")
167 glyfLen := uint32(len(tableData(ts, "glyf")))
168 g := someGlyph(t, loca, 16)
169
170 backwards := liberationWith(t, "loca", func(b []byte) []byte {
171 setLoca(b, g+1, locaAt(b, g)-2)
172 return b
173 })
174 pastGlyf := liberationWith(t, "loca", func(b []byte) []byte {
175 setLoca(b, len(b)/4-1, glyfLen+4)
176 return b
177 })
178 shortGlyph := liberationWith(t, "loca", func(b []byte) []byte {
179 setLoca(b, g+1, locaAt(b, g)+4)
180 return b
181 })
182 moreGlyphs := liberationWith(t, "maxp", func(b []byte) []byte {
183 binary.BigEndian.PutUint16(b[4:], uint16(numGlyphs(ts)+10))
184 return b
185 })
186 badLocFormat := liberationWith(t, "head", func(b []byte) []byte {
187 binary.BigEndian.PutUint16(b[50:], 2)
188 return b
189 })
190 shortHmtx := liberationWith(t, "hmtx", func(b []byte) []byte { return b[:100] })
191 noMetrics := liberationWith(t, "hhea", func(b []byte) []byte {
192 binary.BigEndian.PutUint16(b[34:], 0)
193 return b
194 })
195 tooManyMetrics := liberationWith(t, "hhea", func(b []byte) []byte {
196 binary.BigEndian.PutUint16(b[34:], uint16(numGlyphs(ts)+1))
197 return b
198 })
199 v, _ := tablesOf(t, liberation(t))
200 empty := buildSFNT(v, withTable(withTable(ts, "glyf", nil), "loca", nil))
201 noMaxp := buildSFNT(v, withoutTable(ts, "maxp"))
202
203 cffWith := func(edit func(b []byte) []byte) []byte {
204 return otto(t, "CFF ", func(n int) []byte { return edit(cffTable(n)) })
205 }
206 stub := cffWith(func([]byte) []byte { return []byte{1, 0, 4, 1} })
207 fewerCharStrings := otto(t, "CFF ", func(n int) []byte { return cffTable(n - 1) })
208 cffCutShort := cffWith(func(b []byte) []byte { return b[:len(b)-1] })
209 cffBadVersion := cffWith(func(b []byte) []byte { b[0] = 3; return b })
210 cffTwoNames := cffWith(func(b []byte) []byte {
211 binary.BigEndian.PutUint16(b[4:], 2) // the Name INDEX's count
212 return b
213 })
214 cffOffsetsBack := cffWith(func(b []byte) []byte {
215 // The Name INDEX's second offset, before its first.
216 binary.BigEndian.PutUint32(b[4+2+1+4:], 0)
217 return b
218 })
219 cffNoCharStrings := cffWith(func(b []byte) []byte {
220 // The Top DICT's operator, 17, made a different one (15,
221 // charset), so the DICT names no CharStrings.
222 i := bytes.Index(b, []byte{17, 0, 0})
223 if i < 0 {
224 t.Fatal("no CharStrings operator")
225 }
226 b[i] = 15
227 return b
228 })
229 cff2CutShort := otto(t, "CFF2", func(n int) []byte { return cff2Table(n)[:12] })
230 cff2Fewer := otto(t, "CFF2", func(n int) []byte { return cff2Table(n - 1) })
231
232 for _, c := range []struct {
233 name string
234 data []byte
235 }{
236 {"loca going backwards", backwards},
237 {"loca past the end of glyf", pastGlyf},
238 {"a glyph shorter than its header", shortGlyph},
239 {"maxp counting more glyphs than loca holds", moreGlyphs},
240 {"an indexToLocFormat that is neither", badLocFormat},
241 {"hmtx shorter than its metrics", shortHmtx},
242 {"no horizontal metrics", noMetrics},
243 {"more metrics than glyphs", tooManyMetrics},
244 {"an empty glyf and loca", empty},
245 {"no maxp", noMaxp},
246 {"a WOFF of a backwards loca", buildWOFF(t, backwards)},
247 {"a WOFF2 of a loca past glyf", buildWOFF2(t, pastGlyf)},
248 {"a stub CFF", stub},
249 {"a CFF with fewer CharStrings than glyphs", fewerCharStrings},
250 {"a CFF cut short", cffCutShort},
251 {"a CFF of an unknown version", cffBadVersion},
252 {"a CFF with two fonts", cffTwoNames},
253 {"a CFF INDEX whose offsets go back", cffOffsetsBack},
254 {"a CFF with no CharStrings", cffNoCharStrings},
255 {"a CFF2 cut short", cff2CutShort},
256 {"a CFF2 with fewer CharStrings than glyphs", cff2Fewer},
257 {"a WOFF2 of a stub CFF", buildWOFF2(t, stub)},
258 } {
259 t.Run(c.name, func(t *testing.T) {
260 f, err := Inspect(c.data)
261 if !errors.Is(err, ErrMalformed) {
262 t.Fatalf("Inspect = %+v, %v; want ErrMalformed", f, err)
263 }
264 t.Log(err)
265 })
266 }
267}
268