github.com/andybalholm/giopdf@v0.0.0-20220317170119-aad9a095ad48/cff/parser_test.go (about) 1 package cff 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "math/rand" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/benoitkugler/textlayout/fonts" 13 ) 14 15 func TestParseCFF(t *testing.T) { 16 files := []string{ 17 "test/AAAPKB+SourceSansPro-Bold.cff", 18 "test/AdobeMingStd-Light-Identity-H.cff", 19 "test/YPTQCA+CMR17.cff", 20 } 21 ttfs, err := ioutil.ReadDir("test/ttf") 22 if err != nil { 23 t.Fatal(err) 24 } 25 for _, f := range ttfs { 26 files = append(files, filepath.Join("test/ttf", f.Name())) 27 } 28 29 for _, file := range files { 30 b, err := ioutil.ReadFile(file) 31 if err != nil { 32 t.Fatal(err) 33 } 34 font, err := Parse(bytes.NewReader(b)) 35 if err != nil { 36 t.Fatal(err, "in", file) 37 } 38 fmt.Println(file, "num glyphs:", len(font.charstrings)) 39 40 if font.fdSelect != nil { 41 for i := 0; i < len(font.charstrings); i++ { 42 _, err = font.fdSelect.fontDictIndex(fonts.GID(i)) 43 if err != nil { 44 t.Fatal(err) 45 } 46 } 47 } 48 49 for glyphIndex := range font.charstrings { 50 _, err := font.LoadGlyph(fonts.GID(glyphIndex)) 51 if err != nil { 52 t.Fatalf("can't get extents for %s GID %d: %s", file, glyphIndex, err) 53 } 54 } 55 56 if file == "test/AAAPKB+SourceSansPro-Bold.cff" { 57 glyphIndex := fonts.GID(1) 58 g, err := font.LoadGlyph(glyphIndex) 59 if err != nil { 60 t.Fatalf("can't get extents for %s GID %d: %s", file, glyphIndex, err) 61 } 62 if g.Width != 200 { 63 t.Errorf("unexpected width for glyph %s in %s: got %d, want 200", font.GlyphName(glyphIndex), file, g.Width) 64 } 65 66 matrix := fmt.Sprint(font.FontMatrix) 67 if matrix != "[0.001 0 0 0.001 0 0]" { 68 t.Errorf("Unexpected font matrix in %s: got %v, want [0.001 0 0 0.001 0 0]", file, font.FontMatrix) 69 } 70 } 71 } 72 } 73 74 func TestBulk(t *testing.T) { 75 for _, file := range []string{ 76 "test/AAAPKB+SourceSansPro-Bold.cff", 77 "test/AdobeMingStd-Light-Identity-H.cff", 78 "test/YPTQCA+CMR17.cff", 79 } { 80 b, err := ioutil.ReadFile(file) 81 if err != nil { 82 t.Fatal(err) 83 } 84 for range [100]int{} { 85 for range [500]int{} { // random mutation 86 i := rand.Intn(len(b)) 87 b[i] = byte(rand.Intn(256)) 88 } 89 Parse(bytes.NewReader(b)) // we just check for crashes 90 } 91 } 92 } 93 94 func TestLoader(t *testing.T) { 95 for _, file := range []string{ 96 "test/AAAPKB+SourceSansPro-Bold.cff", 97 "test/AdobeMingStd-Light-Identity-H.cff", 98 "test/YPTQCA+CMR17.cff", 99 } { 100 f, err := os.Open(file) 101 if err != nil { 102 t.Fatal(err) 103 } 104 fonts, err := Load(f) 105 if err != nil { 106 t.Fatal(err) 107 } 108 for _, font := range fonts { 109 font.PoscriptName() 110 _, has := font.PostscriptInfo() 111 if !has { 112 t.Error("expected PS info") 113 } 114 font.LoadSummary() 115 } 116 } 117 } 118 119 func TestCIDFont(t *testing.T) { 120 file := "test/AdobeMingStd-Light-Identity-H.cff" 121 b, err := ioutil.ReadFile(file) 122 if err != nil { 123 t.Fatal(err) 124 } 125 font, err := Parse(bytes.NewReader(b)) 126 if err != nil { 127 t.Fatal(err) 128 } 129 fmt.Println(len(font.localSubrs)) 130 }