font_test.go 1.2 KB

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