github.com/SahandAslani/gomobile@v0.0.0-20210909130135-2cb2d44c09b2/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 //go:build linux || darwin 6 // +build linux darwin 7 8 package font 9 10 import ( 11 "bytes" 12 "fmt" 13 "testing" 14 ) 15 16 func looksLikeATTF(b []byte) error { 17 if len(b) < 256 { 18 return fmt.Errorf("not a TTF: not enough data") 19 } 20 b = b[:256] 21 22 // Look for the 4-byte magic header. See 23 // http://www.microsoft.com/typography/otspec/otff.htm 24 switch string(b[:4]) { 25 case "\x00\x01\x00\x00", "ttcf": 26 // No-op. 27 default: 28 return fmt.Errorf("not a TTF: missing magic header") 29 } 30 31 // Look for a glyf table. 32 if i := bytes.Index(b, []byte("glyf")); i < 0 { 33 return fmt.Errorf(`not a TTF: missing "glyf" table`) 34 } else if i%0x10 != 0x0c { 35 return fmt.Errorf(`not a TTF: invalid "glyf" offset 0x%02x`, i) 36 } 37 return nil 38 } 39 40 func TestLoadDefault(t *testing.T) { 41 def, err := buildDefault() 42 if err != nil { 43 t.Skip("default font not found") 44 } 45 if err := looksLikeATTF(def); err != nil { 46 t.Errorf("default font: %v", err) 47 } 48 } 49 50 func TestLoadMonospace(t *testing.T) { 51 mono, err := buildMonospace() 52 if err != nil { 53 t.Skip("mono font not found") 54 } 55 if err := looksLikeATTF(mono); err != nil { 56 t.Errorf("monospace font: %v", err) 57 } 58 }