github.com/champo/mobile@v0.0.0-20190107162257-dc0771356504/exp/font/font_test.go (about) 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 // +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 TestLoadFonts(t *testing.T) { 40 if err := looksLikeATTF(Default()); err != nil { 41 t.Errorf("default font: %v", err) 42 } 43 if err := looksLikeATTF(Monospace()); err != nil { 44 t.Errorf("monospace font: %v", err) 45 } 46 }