font_test.go 1.3 KB

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