| 1 | package typeface |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/zlib" |
| 6 | "crypto/sha256" |
| 7 | "encoding/binary" |
| 8 | "encoding/hex" |
| 9 | "errors" |
| 10 | "net/http/httptest" |
| 11 | "os" |
| 12 | "sort" |
| 13 | "testing" |
| 14 | |
| 15 | "github.com/andybalholm/brotli" |
| 16 | ) |
| 17 | |
| 18 | // The test font is Liberation Sans Regular (SIL OFL 1.1; the licence is |
| 19 | // beside it in testdata). Every other file these tests read is built from |
| 20 | // it here: WOFF and WOFF2 copies, and synthetic sfnts with one table |
| 21 | // swapped or patched. |
| 22 | |
| 23 | type table struct { |
| 24 | tag string |
| 25 | data []byte |
| 26 | } |
| 27 | |
| 28 | func liberation(t *testing.T) []byte { |
| 29 | t.Helper() |
| 30 | b, err := os.ReadFile("testdata/LiberationSans-Regular.ttf") |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | return b |
| 35 | } |
| 36 | |
| 37 | // tablesOf reads an sfnt's version and tables, in directory order. |
| 38 | func tablesOf(t *testing.T, sfnt []byte) (uint32, []table) { |
| 39 | t.Helper() |
| 40 | n := int(binary.BigEndian.Uint16(sfnt[4:])) |
| 41 | var ts []table |
| 42 | for i := 0; i < n; i++ { |
| 43 | rec := sfnt[12+16*i:] |
| 44 | off := binary.BigEndian.Uint32(rec[8:]) |
| 45 | length := binary.BigEndian.Uint32(rec[12:]) |
| 46 | ts = append(ts, table{string(rec[:4]), bytes.Clone(sfnt[off : off+length])}) |
| 47 | } |
| 48 | return binary.BigEndian.Uint32(sfnt), ts |
| 49 | } |
| 50 | |
| 51 | // buildSFNT writes an sfnt with the tables sorted by tag, each padded to |
| 52 | // four bytes. Checksums are left zero: Inspect does not read them. |
| 53 | func buildSFNT(version uint32, ts []table) []byte { |
| 54 | ts = append([]table(nil), ts...) |
| 55 | sort.Slice(ts, func(i, j int) bool { return ts[i].tag < ts[j].tag }) |
| 56 | var b []byte |
| 57 | b = binary.BigEndian.AppendUint32(b, version) |
| 58 | b = binary.BigEndian.AppendUint16(b, uint16(len(ts))) |
| 59 | b = append(b, 0, 0, 0, 0, 0, 0) // searchRange and friends: unread |
| 60 | off := 12 + 16*len(ts) |
| 61 | for _, tb := range ts { |
| 62 | b = append(b, tb.tag...) |
| 63 | b = binary.BigEndian.AppendUint32(b, 0) |
| 64 | b = binary.BigEndian.AppendUint32(b, uint32(off)) |
| 65 | b = binary.BigEndian.AppendUint32(b, uint32(len(tb.data))) |
| 66 | off += (len(tb.data) + 3) &^ 3 |
| 67 | } |
| 68 | for _, tb := range ts { |
| 69 | b = append(b, tb.data...) |
| 70 | for len(b)%4 != 0 { |
| 71 | b = append(b, 0) |
| 72 | } |
| 73 | } |
| 74 | return b |
| 75 | } |
| 76 | |
| 77 | func withTable(ts []table, tag string, data []byte) []table { |
| 78 | out := withoutTable(ts, tag) |
| 79 | return append(out, table{tag, data}) |
| 80 | } |
| 81 | |
| 82 | func withoutTable(ts []table, tags ...string) []table { |
| 83 | var out []table |
| 84 | for _, tb := range ts { |
| 85 | drop := false |
| 86 | for _, tag := range tags { |
| 87 | drop = drop || tb.tag == tag |
| 88 | } |
| 89 | if !drop { |
| 90 | out = append(out, tb) |
| 91 | } |
| 92 | } |
| 93 | return out |
| 94 | } |
| 95 | |
| 96 | func tableData(ts []table, tag string) []byte { |
| 97 | for _, tb := range ts { |
| 98 | if tb.tag == tag { |
| 99 | return tb.data |
| 100 | } |
| 101 | } |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | // patched is Liberation with its OS/2 fsType and fsSelection set. |
| 106 | func patched(t *testing.T, fsType, fsSelection uint16) []byte { |
| 107 | t.Helper() |
| 108 | v, ts := tablesOf(t, liberation(t)) |
| 109 | os2 := bytes.Clone(tableData(ts, "OS/2")) |
| 110 | binary.BigEndian.PutUint16(os2[8:], fsType) |
| 111 | binary.BigEndian.PutUint16(os2[62:], fsSelection) |
| 112 | return buildSFNT(v, withTable(ts, "OS/2", os2)) |
| 113 | } |
| 114 | |
| 115 | // cff is Liberation's other tables in an OTTO with a CFF table of as |
| 116 | // many glyphs where its glyf and loca were (outlines_test.go). |
| 117 | func cff(t *testing.T) []byte { |
| 118 | t.Helper() |
| 119 | return otto(t, "CFF ", cffTable) |
| 120 | } |
| 121 | |
| 122 | type axis struct { |
| 123 | tag string |
| 124 | min, def, max float64 |
| 125 | } |
| 126 | |
| 127 | func fvar(axes ...axis) []byte { |
| 128 | var b []byte |
| 129 | b = binary.BigEndian.AppendUint16(b, 1) // majorVersion |
| 130 | b = binary.BigEndian.AppendUint16(b, 0) // minorVersion |
| 131 | b = binary.BigEndian.AppendUint16(b, 16) // axesArrayOffset |
| 132 | b = binary.BigEndian.AppendUint16(b, 2) // reserved |
| 133 | b = binary.BigEndian.AppendUint16(b, uint16(len(axes))) |
| 134 | b = binary.BigEndian.AppendUint16(b, 20) // axisSize |
| 135 | b = binary.BigEndian.AppendUint16(b, 0) // instanceCount |
| 136 | b = binary.BigEndian.AppendUint16(b, uint16(4+4*len(axes))) |
| 137 | for _, a := range axes { |
| 138 | b = append(b, a.tag...) |
| 139 | for _, v := range []float64{a.min, a.def, a.max} { |
| 140 | b = binary.BigEndian.AppendUint32(b, uint32(int32(v*65536))) |
| 141 | } |
| 142 | b = binary.BigEndian.AppendUint16(b, 0) // flags |
| 143 | b = binary.BigEndian.AppendUint16(b, 256) // axisNameID |
| 144 | } |
| 145 | return b |
| 146 | } |
| 147 | |
| 148 | func variable(t *testing.T, axes ...axis) []byte { |
| 149 | t.Helper() |
| 150 | v, ts := tablesOf(t, liberation(t)) |
| 151 | return buildSFNT(v, withTable(ts, "fvar", fvar(axes...))) |
| 152 | } |
| 153 | |
| 154 | type nameRec struct { |
| 155 | platform, encoding, language, id uint16 |
| 156 | value string |
| 157 | } |
| 158 | |
| 159 | // nameTable builds a format 0 name table: platform 1 strings in Mac |
| 160 | // Roman (ASCII here, plus é as 0x8E), everything else UTF-16BE. |
| 161 | func nameTable(recs ...nameRec) []byte { |
| 162 | var storage []byte |
| 163 | var b []byte |
| 164 | b = binary.BigEndian.AppendUint16(b, 0) |
| 165 | b = binary.BigEndian.AppendUint16(b, uint16(len(recs))) |
| 166 | b = binary.BigEndian.AppendUint16(b, uint16(6+12*len(recs))) |
| 167 | for _, r := range recs { |
| 168 | var s []byte |
| 169 | if r.platform == 1 { |
| 170 | for _, c := range r.value { |
| 171 | if c == 'é' { |
| 172 | s = append(s, 0x8E) |
| 173 | } else { |
| 174 | s = append(s, byte(c)) |
| 175 | } |
| 176 | } |
| 177 | } else { |
| 178 | for _, c := range r.value { |
| 179 | s = binary.BigEndian.AppendUint16(s, uint16(c)) |
| 180 | } |
| 181 | } |
| 182 | for _, v := range []uint16{r.platform, r.encoding, r.language, r.id, uint16(len(s)), uint16(len(storage))} { |
| 183 | b = binary.BigEndian.AppendUint16(b, v) |
| 184 | } |
| 185 | storage = append(storage, s...) |
| 186 | } |
| 187 | return append(b, storage...) |
| 188 | } |
| 189 | |
| 190 | func named(t *testing.T, recs ...nameRec) []byte { |
| 191 | t.Helper() |
| 192 | v, ts := tablesOf(t, liberation(t)) |
| 193 | return buildSFNT(v, withTable(ts, "name", nameTable(recs...))) |
| 194 | } |
| 195 | |
| 196 | // buildWOFF wraps an sfnt in WOFF 1.0, each table zlib-compressed (or |
| 197 | // stored when that is no smaller, as the format requires). |
| 198 | func buildWOFF(t *testing.T, sfnt []byte) []byte { |
| 199 | t.Helper() |
| 200 | flavor, ts := tablesOf(t, sfnt) |
| 201 | sort.Slice(ts, func(i, j int) bool { return ts[i].tag < ts[j].tag }) |
| 202 | type entry struct { |
| 203 | tag string |
| 204 | comp []byte |
| 205 | origLength int |
| 206 | offset int |
| 207 | storedLength int |
| 208 | } |
| 209 | var es []entry |
| 210 | off := 44 + 20*len(ts) |
| 211 | for _, tb := range ts { |
| 212 | var z bytes.Buffer |
| 213 | w := zlib.NewWriter(&z) |
| 214 | w.Write(tb.data) |
| 215 | w.Close() |
| 216 | comp := z.Bytes() |
| 217 | if len(comp) >= len(tb.data) { |
| 218 | comp = tb.data |
| 219 | } |
| 220 | es = append(es, entry{tb.tag, comp, len(tb.data), off, len(comp)}) |
| 221 | off += (len(comp) + 3) &^ 3 |
| 222 | } |
| 223 | var b []byte |
| 224 | b = append(b, "wOFF"...) |
| 225 | b = binary.BigEndian.AppendUint32(b, flavor) |
| 226 | b = binary.BigEndian.AppendUint32(b, uint32(off)) // length |
| 227 | b = binary.BigEndian.AppendUint16(b, uint16(len(ts))) |
| 228 | b = binary.BigEndian.AppendUint16(b, 0) |
| 229 | b = binary.BigEndian.AppendUint32(b, uint32(len(sfnt))) |
| 230 | b = append(b, make([]byte, 4+20)...) // version, metadata, private |
| 231 | for _, e := range es { |
| 232 | b = append(b, e.tag...) |
| 233 | b = binary.BigEndian.AppendUint32(b, uint32(e.offset)) |
| 234 | b = binary.BigEndian.AppendUint32(b, uint32(e.storedLength)) |
| 235 | b = binary.BigEndian.AppendUint32(b, uint32(e.origLength)) |
| 236 | b = binary.BigEndian.AppendUint32(b, 0) |
| 237 | } |
| 238 | for _, e := range es { |
| 239 | b = append(b, e.comp...) |
| 240 | for len(b)%4 != 0 { |
| 241 | b = append(b, 0) |
| 242 | } |
| 243 | } |
| 244 | return b |
| 245 | } |
| 246 | |
| 247 | var testWOFF2Tags = []string{"cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post", "cvt ", "fpgm", "glyf", "loca", "prep", "CFF ", "VORG", "EBDT", "EBLC", "gasp", "hdmx", "kern", "LTSH", "PCLT", "VDMX", "vhea", "vmtx", "BASE", "GDEF", "GPOS", "GSUB", "EBSC", "JSTF", "MATH", "CBDT", "CBLC", "COLR", "CPAL", "SVG ", "sbix", "acnt", "avar", "bdat", "bloc", "bsln", "cvar", "fdsc", "feat", "fmtx", "fvar", "gvar", "hsty", "just", "lcar", "mort", "morx", "opbd", "prop", "trak", "Zapf", "Silf", "Glat", "Gloc", "Feat", "Sill"} |
| 248 | |
| 249 | func encodeBase128(n uint32) []byte { |
| 250 | var out []byte |
| 251 | for { |
| 252 | out = append([]byte{byte(n & 0x7F)}, out...) |
| 253 | n >>= 7 |
| 254 | if n == 0 { |
| 255 | break |
| 256 | } |
| 257 | } |
| 258 | for i := 0; i < len(out)-1; i++ { |
| 259 | out[i] |= 0x80 |
| 260 | } |
| 261 | return out |
| 262 | } |
| 263 | |
| 264 | // woff2Entry is a WOFF2 directory entry as written: lengths may lie, |
| 265 | // which is the point of some refusals. |
| 266 | type woff2Entry struct { |
| 267 | tag string |
| 268 | version byte // transform version, the flags' top two bits |
| 269 | origLength uint32 |
| 270 | transform uint32 // written only when the entry is transformed |
| 271 | } |
| 272 | |
| 273 | func woff2Transformed(e woff2Entry) bool { |
| 274 | switch e.tag { |
| 275 | case "glyf", "loca": |
| 276 | return e.version != 3 |
| 277 | default: |
| 278 | return e.version != 0 |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | func assembleWOFF2(flavor uint32, es []woff2Entry, compressed []byte) []byte { |
| 283 | var dir []byte |
| 284 | for _, e := range es { |
| 285 | idx := 63 |
| 286 | for i, tag := range testWOFF2Tags { |
| 287 | if tag == e.tag { |
| 288 | idx = i |
| 289 | } |
| 290 | } |
| 291 | dir = append(dir, e.version<<6|byte(idx)) |
| 292 | if idx == 63 { |
| 293 | dir = append(dir, e.tag...) |
| 294 | } |
| 295 | dir = append(dir, encodeBase128(e.origLength)...) |
| 296 | if woff2Transformed(e) { |
| 297 | dir = append(dir, encodeBase128(e.transform)...) |
| 298 | } |
| 299 | } |
| 300 | total := 48 + len(dir) + len(compressed) |
| 301 | padded := (total + 3) &^ 3 |
| 302 | var b []byte |
| 303 | b = append(b, "wOF2"...) |
| 304 | b = binary.BigEndian.AppendUint32(b, flavor) |
| 305 | b = binary.BigEndian.AppendUint32(b, uint32(padded)) |
| 306 | b = binary.BigEndian.AppendUint16(b, uint16(len(es))) |
| 307 | b = binary.BigEndian.AppendUint16(b, 0) |
| 308 | b = binary.BigEndian.AppendUint32(b, 0) // totalSfntSize: unread |
| 309 | b = binary.BigEndian.AppendUint32(b, uint32(len(compressed))) |
| 310 | b = append(b, make([]byte, 4+20)...) // version, metadata, private |
| 311 | b = append(b, dir...) |
| 312 | b = append(b, compressed...) |
| 313 | return append(b, make([]byte, padded-total)...) |
| 314 | } |
| 315 | |
| 316 | func brotliOf(data []byte) []byte { |
| 317 | var z bytes.Buffer |
| 318 | w := brotli.NewWriterLevel(&z, 5) |
| 319 | w.Write(data) |
| 320 | w.Close() |
| 321 | return z.Bytes() |
| 322 | } |
| 323 | |
| 324 | // woff2Parts is an sfnt's tables as WOFF2 entries, glyf and loca with |
| 325 | // the null transform, and their concatenation (the uncompressed stream). |
| 326 | func woff2Parts(t *testing.T, sfnt []byte) (uint32, []woff2Entry, []byte) { |
| 327 | t.Helper() |
| 328 | flavor, ts := tablesOf(t, sfnt) |
| 329 | var es []woff2Entry |
| 330 | var stream []byte |
| 331 | for _, tb := range ts { |
| 332 | e := woff2Entry{tag: tb.tag, origLength: uint32(len(tb.data))} |
| 333 | if tb.tag == "glyf" || tb.tag == "loca" { |
| 334 | e.version = 3 |
| 335 | } |
| 336 | es = append(es, e) |
| 337 | stream = append(stream, tb.data...) |
| 338 | } |
| 339 | return flavor, es, stream |
| 340 | } |
| 341 | |
| 342 | func buildWOFF2(t *testing.T, sfnt []byte) []byte { |
| 343 | t.Helper() |
| 344 | flavor, es, stream := woff2Parts(t, sfnt) |
| 345 | return assembleWOFF2(flavor, es, brotliOf(stream)) |
| 346 | } |
| 347 | |
| 348 | func mustInspect(t *testing.T, data []byte) Face { |
| 349 | t.Helper() |
| 350 | f, err := Inspect(data) |
| 351 | if err != nil { |
| 352 | t.Fatalf("Inspect: %v", err) |
| 353 | } |
| 354 | return f |
| 355 | } |
| 356 | |
| 357 | func TestInspectReadsTheNamesAndStyle(t *testing.T) { |
| 358 | data := liberation(t) |
| 359 | f := mustInspect(t, data) |
| 360 | sum := sha256.Sum256(data) |
| 361 | want := Face{ |
| 362 | Ref: hex.EncodeToString(sum[:]), |
| 363 | Format: "ttf", |
| 364 | Outline: "truetype", |
| 365 | Family: "Liberation Sans", |
| 366 | Style: "Regular", |
| 367 | Weight: 400, |
| 368 | WeightMin: 400, |
| 369 | WeightMax: 400, |
| 370 | } |
| 371 | if f != want { |
| 372 | t.Fatalf("Inspect(Liberation Sans)\n got %+v\nwant %+v", f, want) |
| 373 | } |
| 374 | if f.ContentType() != "font/ttf" { |
| 375 | t.Errorf("ContentType = %q", f.ContentType()) |
| 376 | } |
| 377 | |
| 378 | t.Run("italic from fsSelection", func(t *testing.T) { |
| 379 | if f := mustInspect(t, patched(t, 0, 0x0001)); !f.Italic { |
| 380 | t.Errorf("fsSelection bit 0 set, Italic = false") |
| 381 | } |
| 382 | }) |
| 383 | t.Run("italic from macStyle", func(t *testing.T) { |
| 384 | v, ts := tablesOf(t, liberation(t)) |
| 385 | head := bytes.Clone(tableData(ts, "head")) |
| 386 | binary.BigEndian.PutUint16(head[44:], 0x0002) |
| 387 | if f := mustInspect(t, buildSFNT(v, withTable(ts, "head", head))); !f.Italic { |
| 388 | t.Errorf("macStyle bit 1 set, Italic = false") |
| 389 | } |
| 390 | }) |
| 391 | t.Run("typographic names win, Windows before Mac", func(t *testing.T) { |
| 392 | f := mustInspect(t, named(t, |
| 393 | nameRec{1, 0, 0, 1, "Mac Family"}, |
| 394 | nameRec{1, 0, 0, 2, "Mac Style"}, |
| 395 | nameRec{1, 0, 0, 16, "Mac Typo"}, |
| 396 | nameRec{3, 1, 0x0409, 1, "Win Family"}, |
| 397 | nameRec{3, 1, 0x0409, 2, "Bold"}, |
| 398 | nameRec{3, 1, 0x0409, 16, "Café Sans"}, |
| 399 | nameRec{3, 1, 0x0409, 17, "Semibold"}, |
| 400 | )) |
| 401 | if f.Family != "Café Sans" || f.Style != "Semibold" { |
| 402 | t.Errorf("Family, Style = %q, %q; want Café Sans, Semibold", f.Family, f.Style) |
| 403 | } |
| 404 | }) |
| 405 | t.Run("legacy names, Mac Roman when there is nothing else", func(t *testing.T) { |
| 406 | f := mustInspect(t, named(t, |
| 407 | nameRec{1, 0, 0, 1, "Café Mac"}, |
| 408 | nameRec{1, 0, 0, 2, "Italic"}, |
| 409 | )) |
| 410 | if f.Family != "Café Mac" || f.Style != "Italic" { |
| 411 | t.Errorf("Family, Style = %q, %q; want Café Mac, Italic", f.Family, f.Style) |
| 412 | } |
| 413 | }) |
| 414 | } |
| 415 | |
| 416 | func TestInspectReadsWOFFAndWOFF2(t *testing.T) { |
| 417 | ttf := mustInspect(t, liberation(t)) |
| 418 | for _, c := range []struct { |
| 419 | format Format |
| 420 | data []byte |
| 421 | ctype string |
| 422 | }{ |
| 423 | {"woff", buildWOFF(t, liberation(t)), "font/woff"}, |
| 424 | {"woff2", buildWOFF2(t, liberation(t)), "font/woff2"}, |
| 425 | } { |
| 426 | t.Run(string(c.format), func(t *testing.T) { |
| 427 | f := mustInspect(t, c.data) |
| 428 | sum := sha256.Sum256(c.data) |
| 429 | want := ttf |
| 430 | want.Ref = hex.EncodeToString(sum[:]) |
| 431 | want.Format = c.format |
| 432 | if f != want { |
| 433 | t.Fatalf("\n got %+v\nwant %+v", f, want) |
| 434 | } |
| 435 | if f.ContentType() != c.ctype { |
| 436 | t.Errorf("ContentType = %q, want %q", f.ContentType(), c.ctype) |
| 437 | } |
| 438 | }) |
| 439 | } |
| 440 | t.Run("woff2 of a cff", func(t *testing.T) { |
| 441 | f := mustInspect(t, buildWOFF2(t, cff(t))) |
| 442 | if f.Format != "woff2" || f.Outline != "cff" { |
| 443 | t.Errorf("Format, Outline = %q, %q", f.Format, f.Outline) |
| 444 | } |
| 445 | }) |
| 446 | } |
| 447 | |
| 448 | func TestInspectReadsCFFAndVariable(t *testing.T) { |
| 449 | f := mustInspect(t, cff(t)) |
| 450 | if f.Format != "otf" || f.Outline != "cff" || f.Family != "Liberation Sans" || f.ContentType() != "font/otf" { |
| 451 | t.Errorf("CFF: %+v (content type %q)", f, f.ContentType()) |
| 452 | } |
| 453 | |
| 454 | v := mustInspect(t, variable(t, axis{"wdth", 75, 100, 100}, axis{"wght", 100, 400, 900})) |
| 455 | if !v.Variable || v.Weight != 400 || v.WeightMin != 100 || v.WeightMax != 900 { |
| 456 | t.Errorf("variable: Variable %v, Weight %d, range %d..%d; want true, 400, 100..900", |
| 457 | v.Variable, v.Weight, v.WeightMin, v.WeightMax) |
| 458 | } |
| 459 | |
| 460 | w := mustInspect(t, buildWOFF2(t, variable(t, axis{"wght", 100, 400, 900}))) |
| 461 | if !w.Variable || w.WeightMin != 100 || w.WeightMax != 900 { |
| 462 | t.Errorf("variable woff2: %+v", w) |
| 463 | } |
| 464 | |
| 465 | s := mustInspect(t, variable(t, axis{"wdth", 75, 100, 100})) |
| 466 | if !s.Variable || s.WeightMin != 400 || s.WeightMax != 400 { |
| 467 | t.Errorf("fvar with no wght axis: %+v; want variable, weight range 400..400", s) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | func TestPicturesOnlyForStaticTrueTypeThatMayEmbed(t *testing.T) { |
| 472 | for _, c := range []struct { |
| 473 | name string |
| 474 | data []byte |
| 475 | want bool |
| 476 | }{ |
| 477 | {"ttf, installable", liberation(t), true}, |
| 478 | {"preview and print", patched(t, 0x0004, 0), true}, |
| 479 | {"editable", patched(t, 0x0008, 0), true}, |
| 480 | {"editable, no subsetting", patched(t, 0x0108, 0), false}, |
| 481 | {"installable, no subsetting", patched(t, 0x0100, 0), false}, |
| 482 | {"restricted licence", patched(t, 0x0002, 0), false}, |
| 483 | {"restricted with preview and print", patched(t, 0x0006, 0), false}, |
| 484 | {"bitmap embedding only", patched(t, 0x0200, 0), false}, |
| 485 | {"cff", cff(t), false}, |
| 486 | {"variable", variable(t, axis{"wght", 100, 400, 900}), false}, |
| 487 | {"variable in width only", variable(t, axis{"wdth", 75, 100, 100}), false}, |
| 488 | {"woff", buildWOFF(t, liberation(t)), false}, |
| 489 | {"woff2", buildWOFF2(t, liberation(t)), false}, |
| 490 | } { |
| 491 | f := mustInspect(t, c.data) |
| 492 | if got := f.Pictures(); got != c.want { |
| 493 | t.Errorf("%s: Pictures() = %v, want %v (%+v)", c.name, got, c.want, f) |
| 494 | } |
| 495 | } |
| 496 | if f := mustInspect(t, patched(t, 0x0002, 0)); f.FSType != 0x0002 { |
| 497 | t.Errorf("FSType = %#x, want 0x2", f.FSType) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | func TestHeaders(t *testing.T) { |
| 502 | for format, ctype := range map[Format]string{"ttf": "font/ttf", "otf": "font/otf", "woff": "font/woff", "woff2": "font/woff2"} { |
| 503 | w := httptest.NewRecorder() |
| 504 | Headers(w, format) |
| 505 | h := w.Header() |
| 506 | for k, v := range map[string]string{ |
| 507 | "Content-Type": ctype, |
| 508 | "X-Content-Type-Options": "nosniff", |
| 509 | "Cache-Control": "private, max-age=31536000, immutable", |
| 510 | "Content-Security-Policy": "default-src 'none'", |
| 511 | } { |
| 512 | if h.Get(k) != v { |
| 513 | t.Errorf("Headers(%s): %s = %q, want %q", format, k, h.Get(k), v) |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func TestInspectRefuses(t *testing.T) { |
| 520 | ttf := liberation(t) |
| 521 | |
| 522 | truncatedDir := ttf[:12+16*3] |
| 523 | |
| 524 | pastEnd := bytes.Clone(ttf) |
| 525 | binary.BigEndian.PutUint32(pastEnd[12+16*4+12:], uint32(len(ttf))) // the fifth table's length |
| 526 | |
| 527 | tooBig := make([]byte, MaxBytes+1) |
| 528 | copy(tooBig, ttf) |
| 529 | |
| 530 | flavor, es, stream := woff2Parts(t, ttf) |
| 531 | good := brotliOf(stream) |
| 532 | badStream := bytes.Clone(good) |
| 533 | for i := 8; i < len(badStream) && i < 64; i++ { |
| 534 | badStream[i] ^= 0xA5 |
| 535 | } |
| 536 | shortStream := assembleWOFF2(flavor, es, good[:len(good)/2]) |
| 537 | longer := append([]woff2Entry(nil), es...) |
| 538 | longer[0].origLength += 4096 // claims more than the stream holds |
| 539 | extra := append([]woff2Entry(nil), es...) |
| 540 | extra[0].origLength -= 4 // the stream holds more than the tables claim |
| 541 | bomb := append([]woff2Entry(nil), es...) |
| 542 | bomb[0].origLength = 4*MaxBytes + 1 |
| 543 | transformedName := append([]woff2Entry(nil), es...) |
| 544 | for i := range transformedName { |
| 545 | if transformedName[i].tag == "name" { |
| 546 | transformedName[i].version = 1 |
| 547 | transformedName[i].transform = transformedName[i].origLength |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | woff := buildWOFF(t, ttf) |
| 552 | badZlib := bytes.Clone(woff) |
| 553 | { |
| 554 | // Corrupt the name table's compressed bytes. |
| 555 | n := int(binary.BigEndian.Uint16(woff[12:])) |
| 556 | for i := 0; i < n; i++ { |
| 557 | rec := woff[44+20*i:] |
| 558 | if string(rec[:4]) == "name" { |
| 559 | off := binary.BigEndian.Uint32(rec[4:]) |
| 560 | for j := off + 2; j < off+40; j++ { |
| 561 | badZlib[j] ^= 0xA5 |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | badGlyf := bytes.Clone(woff) |
| 568 | { |
| 569 | // Corrupt the glyf table's compressed bytes: a table Inspect |
| 570 | // does not otherwise read. |
| 571 | n := int(binary.BigEndian.Uint16(woff[12:])) |
| 572 | for i := 0; i < n; i++ { |
| 573 | rec := woff[44+20*i:] |
| 574 | if string(rec[:4]) == "glyf" { |
| 575 | off := binary.BigEndian.Uint32(rec[4:]) |
| 576 | for j := off + 2; j < off+40; j++ { |
| 577 | badGlyf[j] ^= 0xA5 |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | v, ts := tablesOf(t, ttf) |
| 584 | shortFvar := fvar(axis{"wght", 100, 400, 900}) |
| 585 | binary.BigEndian.PutUint16(shortFvar[8:], 40) // 40 axes, room for one |
| 586 | |
| 587 | pngHeader := []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00") |
| 588 | |
| 589 | for _, c := range []struct { |
| 590 | name string |
| 591 | data []byte |
| 592 | want error |
| 593 | }{ |
| 594 | {"empty", nil, ErrNotAFont}, |
| 595 | {"a PNG", pngHeader, ErrNotAFont}, |
| 596 | {"a font collection", append([]byte("ttcf"), ttf[4:]...), ErrNotAFont}, |
| 597 | {"a truncated table directory", truncatedDir, ErrMalformed}, |
| 598 | {"a table past the end", pastEnd, ErrMalformed}, |
| 599 | {"no tables", append(bytes.Clone(ttf[:4]), make([]byte, 8)...), ErrMalformed}, |
| 600 | {"over MaxBytes", tooBig, ErrTooBig}, |
| 601 | {"no OS/2", buildSFNT(v, withoutTable(ts, "OS/2")), ErrMalformed}, |
| 602 | {"no name", buildSFNT(v, withoutTable(ts, "name")), ErrMalformed}, |
| 603 | {"no family name", named(t, nameRec{3, 1, 0x409, 2, "Regular"}), ErrMalformed}, |
| 604 | {"no outlines", buildSFNT(v, withoutTable(ts, "glyf", "loca")), ErrMalformed}, |
| 605 | {"a TrueType without glyf", buildSFNT(v, withoutTable(ts, "glyf")), ErrMalformed}, |
| 606 | {"a TrueType without loca", buildSFNT(v, withoutTable(ts, "loca")), ErrMalformed}, |
| 607 | {"an OTTO without CFF", buildSFNT(0x4F54544F, withoutTable(ts, "glyf", "loca")), ErrMalformed}, |
| 608 | {"an OTTO with glyf but no CFF", buildSFNT(0x4F54544F, ts), ErrMalformed}, |
| 609 | {"an fvar shorter than its axes", buildSFNT(v, withTable(ts, "fvar", shortFvar)), ErrMalformed}, |
| 610 | {"a truncated WOFF", woff[:len(woff)/2], ErrMalformed}, |
| 611 | {"a WOFF whose zlib is bad", badZlib, ErrMalformed}, |
| 612 | {"a WOFF whose glyf stream is corrupt", badGlyf, ErrMalformed}, |
| 613 | {"a WOFF2 whose brotli stream is bad", assembleWOFF2(flavor, es, badStream), ErrMalformed}, |
| 614 | {"a WOFF2 whose brotli stream is cut short", shortStream, ErrMalformed}, |
| 615 | {"a WOFF2 table past the stream", assembleWOFF2(flavor, longer, good), ErrMalformed}, |
| 616 | {"a WOFF2 stream longer than its tables", assembleWOFF2(flavor, extra, good), ErrMalformed}, |
| 617 | {"a WOFF2 that unpacks too big", assembleWOFF2(flavor, bomb, good), ErrTooBig}, |
| 618 | {"a WOFF2 with a transformed name table", assembleWOFF2(flavor, transformedName, good), ErrMalformed}, |
| 619 | } { |
| 620 | t.Run(c.name, func(t *testing.T) { |
| 621 | f, err := Inspect(c.data) |
| 622 | if !errors.Is(err, c.want) { |
| 623 | t.Fatalf("Inspect = %+v, %v; want %v", f, err, c.want) |
| 624 | } |
| 625 | t.Log(err) |
| 626 | }) |
| 627 | } |
| 628 | } |
| 629 | |