Debian Codegraph @

exp/font/font_test.go

raw symbols SCIP
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build linux || darwin
6
7 package font
8
9 import (
10 "bytes"
11 "fmt"
12 "testing"
13 )
14
15 func looksLikeATTF(b []byte) error {
16 if len(b) < 256 {
17 return fmt.Errorf("not a TTF: not enough data")
18 }
19 b = b[:256]
20
21 // Look for the 4-byte magic header. See
22 // http://www.microsoft.com/typography/otspec/otff.htm
23 switch string(b[:4]) {
24 case "\x00\x01\x00\x00", "ttcf":
25 // No-op.
26 default:
27 return fmt.Errorf("not a TTF: missing magic header")
28 }
29
30 // Look for a glyf table.
31 if i := bytes.Index(b, []byte("glyf")); i < 0 {
32 return fmt.Errorf(`not a TTF: missing "glyf" table`)
33 } else if i%0x10 != 0x0c {
34 return fmt.Errorf(`not a TTF: invalid "glyf" offset 0x%02x`, i)
35 }
36 return nil
37 }
38
39 func TestLoadDefault(t *testing.T) {
40 def, err := buildDefault()
41 if err != nil {
42 t.Skip("default font not found")
43 }
44 if err := looksLikeATTF(def); err != nil {
45 t.Errorf("default font: %v", err)
46 }
47 }
48
49 func TestLoadMonospace(t *testing.T) {
50 mono, err := buildMonospace()
51 if err != nil {
52 t.Skip("mono font not found")
53 }
54 if err := looksLikeATTF(mono); err != nil {
55 t.Errorf("monospace font: %v", err)
56 }
57 }