subset_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package fonts
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. "golang.org/x/image/font/sfnt"
  6. "golang.org/x/image/math/fixed"
  7. )
  8. // sliceToSFNT cuts the Go font to some runes and returns the font file
  9. func sliceToSFNT(t *testing.T, runes string) []byte {
  10. t.Helper()
  11. font, err := ParseSFNT(goFont(t))
  12. if err != nil {
  13. t.Fatalf("ParseSFNT: %v", err)
  14. }
  15. src, err := newSource(font)
  16. if err != nil {
  17. t.Fatalf("newSource: %v", err)
  18. }
  19. var cps []uint32
  20. for _, r := range runes {
  21. cps = append(cps, uint32(r))
  22. }
  23. tables, err := src.slice(cps)
  24. if err != nil {
  25. t.Fatalf("slice: %v", err)
  26. }
  27. out, err := AssembleSFNT(font.Flavor, tables)
  28. if err != nil {
  29. t.Fatalf("AssembleSFNT: %v", err)
  30. }
  31. return out
  32. }
  33. func TestSliceKeepsOnlyRequestedGlyphs(t *testing.T) {
  34. data := sliceToSFNT(t, "AB")
  35. f, err := sfnt.Parse(data)
  36. if err != nil {
  37. t.Fatalf("x/image sfnt.Parse of the slice: %v", err)
  38. }
  39. var buf sfnt.Buffer
  40. for _, tt := range []struct {
  41. r rune
  42. mapped bool
  43. }{{'A', true}, {'B', true}, {'Z', false}} {
  44. gid, err := f.GlyphIndex(&buf, tt.r)
  45. if err != nil {
  46. t.Fatalf("GlyphIndex(%q): %v", tt.r, err)
  47. }
  48. if (gid != 0) != tt.mapped {
  49. t.Errorf("%q mapped = %v, want %v", tt.r, gid != 0, tt.mapped)
  50. }
  51. if tt.mapped {
  52. segs, err := f.LoadGlyph(&buf, gid, fixed.I(32), nil)
  53. if err != nil || len(segs) == 0 {
  54. t.Errorf("glyph of %q: %d segments, %v", tt.r, len(segs), err)
  55. }
  56. }
  57. }
  58. // the glyph of 'Z' is still there, empty
  59. full, _ := ParseSFNT(goFont(t))
  60. info, cmap, _ := Inspect(full)
  61. sliced, _ := ParseSFNT(data)
  62. offsets, err := glyphOffsets(sliced, info.Glyphs)
  63. if err != nil {
  64. t.Fatalf("glyphOffsets: %v", err)
  65. }
  66. z := cmap['Z']
  67. if offsets[z] != offsets[z+1] {
  68. t.Error("the glyph of 'Z' kept its outline")
  69. }
  70. if a := cmap['A']; offsets[a] == offsets[a+1] {
  71. t.Error("the glyph of 'A' lost its outline")
  72. }
  73. if post := sliced.Table("post"); binary.BigEndian.Uint32(post) == 0x00020000 {
  74. t.Error("post table still carries glyph names")
  75. }
  76. }
  77. func TestSliceIsSmaller(t *testing.T) {
  78. whole := len(goFont(t))
  79. if part := len(sliceToSFNT(t, "ABC")); part >= whole/2 {
  80. t.Errorf("slice of 3 letters is %d bytes, the font %d", part, whole)
  81. }
  82. }
  83. func TestSliceWOFF2Decodes(t *testing.T) {
  84. font, _ := ParseSFNT(sliceToSFNT(t, "Hello"))
  85. woff2, err := EncodeWOFF2(font.Flavor, font.Tables, 5)
  86. if err != nil {
  87. t.Fatalf("EncodeWOFF2: %v", err)
  88. }
  89. back, container, err := Decode(woff2)
  90. if err != nil || container != "woff2" {
  91. t.Fatalf("Decode: %v %q", err, container)
  92. }
  93. if _, _, err := Inspect(back); err != nil {
  94. t.Errorf("Inspect of the decoded slice: %v", err)
  95. }
  96. }
  97. func TestSliceableRejectsCFFAndColour(t *testing.T) {
  98. font, _ := ParseSFNT(goFont(t))
  99. if !sliceable(font) {
  100. t.Fatal("the Go font should be sliceable")
  101. }
  102. for _, tag := range []string{"CFF ", "COLR", "sbix"} {
  103. font, _ := ParseSFNT(goFont(t))
  104. font.Tables[tag] = []byte{0, 0, 0, 0}
  105. if sliceable(font) {
  106. t.Errorf("font with %q is sliceable", tag)
  107. }
  108. }
  109. }
  110. // TestMalformedFontsDoNotPanic damages the font at the places parsers trust:
  111. // headers, directories, offsets, lengths. Every step must fail or succeed
  112. // without a panic.
  113. func TestMalformedFontsDoNotPanic(t *testing.T) {
  114. original := goFont(t)
  115. font, _ := ParseSFNT(original)
  116. woff := makeWOFF(t, original)
  117. woff2, _ := EncodeWOFF2(font.Flavor, font.Tables, 1)
  118. // positions worth hitting: the first 512 bytes and the start of every table
  119. var spots []int
  120. for i := 0; i < 256; i++ {
  121. spots = append(spots, i)
  122. }
  123. for i := 0; i < int(binary.BigEndian.Uint16(original[4:])); i++ {
  124. off := int(binary.BigEndian.Uint32(original[12+16*i+8:]))
  125. for k := 0; k < 24; k++ {
  126. spots = append(spots, off+k)
  127. }
  128. }
  129. values := []byte{0x00, 0x80, 0xFF}
  130. exercise := func(data []byte) {
  131. defer func() {
  132. if r := recover(); r != nil {
  133. t.Fatalf("panic: %v", r)
  134. }
  135. }()
  136. f, _, err := Decode(data)
  137. if err != nil {
  138. return
  139. }
  140. if _, _, err := Inspect(f); err != nil {
  141. return
  142. }
  143. src, err := newSource(f)
  144. if err != nil {
  145. return
  146. }
  147. if tables, err := src.slice([]uint32{'A', 'g', 0x00E9}); err == nil {
  148. AssembleSFNT(f.Flavor, tables)
  149. }
  150. }
  151. for _, base := range [][]byte{original, woff, woff2} {
  152. for _, at := range spots {
  153. if at >= len(base) {
  154. continue
  155. }
  156. for _, v := range values {
  157. damaged := append([]byte(nil), base...)
  158. damaged[at] = v
  159. exercise(damaged)
  160. }
  161. }
  162. for cut := 0; cut < len(base); cut += 997 {
  163. exercise(base[:cut])
  164. }
  165. }
  166. }