OLD | NEW |
1 // Copyright 2012 The Freetype-Go Authors. All rights reserved. | 1 // Copyright 2012 The Freetype-Go Authors. All rights reserved. |
2 // Use of this source code is governed by your choice of either the | 2 // Use of this source code is governed by your choice of either the |
3 // FreeType License or the GNU General Public License version 2 (or | 3 // FreeType License or the GNU General Public License version 2 (or |
4 // any later version), both of which can be found in the LICENSE file. | 4 // any later version), both of which can be found in the LICENSE file. |
5 | 5 |
6 package truetype | 6 package truetype |
7 | 7 |
8 import ( | 8 import ( |
9 "bufio" | 9 "bufio" |
10 "fmt" | 10 "fmt" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 {904, 566, 33}, | 67 {904, 566, 33}, |
68 {667, 1200, 3}, | 68 {667, 1200, 3}, |
69 }, | 69 }, |
70 End: []int{8, 11}, | 70 End: []int{8, 11}, |
71 } | 71 } |
72 if got, want := fmt.Sprint(g0), fmt.Sprint(g1); got != want { | 72 if got, want := fmt.Sprint(g0), fmt.Sprint(g1); got != want { |
73 t.Errorf("GlyphBuf:\ngot %v\nwant %v", got, want) | 73 t.Errorf("GlyphBuf:\ngot %v\nwant %v", got, want) |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 func testScaling(t *testing.T, filename string, hinter *Hinter) { | 77 var scalingTestCases = []struct { |
78 » b, err := ioutil.ReadFile("../../testdata/luxisr.ttf") | 78 » name string |
79 » if err != nil { | 79 » size int32 |
80 » » t.Fatalf("ReadFile: %v", err) | 80 » // hintingBrokenAt, if non-negative, is the glyph index n for which |
81 » } | 81 » // only the first n glyphs are known to be correctly hinted. |
82 » font, err := Parse(b) | 82 » // TODO: remove this field, when hinting is completely implemented. |
83 » if err != nil { | 83 » hintingBrokenAt int |
84 » » t.Fatalf("Parse: %v", err) | 84 }{ |
85 » } | 85 » {"luxisr", 12, -1}, |
86 » f, err := os.Open("../../testdata/" + filename) | 86 » // TODO: uncomment the fonts below, once they get past Parse and |
87 » if err != nil { | 87 » // GlyphBuf.Load, and the unhinted values match C Freetype. |
88 » » t.Fatalf("Open: %v", err) | 88 » //{"x-arial-bold", 11, 0}, |
89 » } | 89 » //{"x-deja-vu-sans-oblique", 17, 0}, |
90 » defer f.Close() | 90 » //{"x-droid-sans-japanese", 9, 0}, |
| 91 » //{"x-inconsolata", 10, 0}, |
| 92 » //{"x-times-new-roman", 13, 0}, |
| 93 } |
91 | 94 |
92 » wants := [][]Point{} | 95 func testScaling(t *testing.T, hinter *Hinter) { |
93 » scanner := bufio.NewScanner(f) | 96 loop: |
94 » for scanner.Scan() { | 97 » for _, tc := range scalingTestCases { |
95 » » text := scanner.Text() | 98 » » b, err := ioutil.ReadFile(fmt.Sprintf("../../testdata/%s.ttf", t
c.name)) |
96 » » if text == "" { | 99 » » if err != nil { |
97 » » » wants = append(wants, []Point{}) | 100 » » » // The "x-foo" fonts are optional tests, as they are not
checked |
98 » » » continue | 101 » » » // in for copyright or file size reasons. |
| 102 » » » if strings.HasPrefix(tc.name, "x-") { |
| 103 » » » » t.Logf("%s: ReadFile: %v", tc.name, err) |
| 104 » » » } else { |
| 105 » » » » t.Errorf("%s: ReadFile: %v", tc.name, err) |
| 106 » » » } |
| 107 » » » continue loop |
99 } | 108 } |
100 » » ss := strings.Split(text, ",") | 109 » » font, err := Parse(b) |
101 » » points := make([]Point, len(ss)) | 110 » » if err != nil { |
102 » » for i, s := range ss { | 111 » » » t.Errorf("%s: Parse: %v", tc.name, err) |
103 » » » p := &points[i] | 112 » » » continue loop |
104 » » » if _, err := fmt.Sscanf(s, "%d %d %d", &p.X, &p.Y, &p.Fl
ags); err != nil { | 113 » » } |
105 » » » » t.Fatalf("Sscanf: %v", err) | 114 » » hinting := "sans" |
| 115 » » if hinter != nil { |
| 116 » » » hinting = "with" |
| 117 » » } |
| 118 » » f, err := os.Open(fmt.Sprintf( |
| 119 » » » "../../testdata/%s-%dpt-%s-hinting.txt", tc.name, tc.siz
e, hinting)) |
| 120 » » if err != nil { |
| 121 » » » t.Errorf("%s: Open: %v", tc.name, err) |
| 122 » » » continue loop |
| 123 » » } |
| 124 » » defer f.Close() |
| 125 |
| 126 » » wants := [][]Point{} |
| 127 » » scanner := bufio.NewScanner(f) |
| 128 » » for scanner.Scan() { |
| 129 » » » text := scanner.Text() |
| 130 » » » if text == "" { |
| 131 » » » » wants = append(wants, []Point{}) |
| 132 » » » » continue |
106 } | 133 } |
| 134 ss := strings.Split(text, ",") |
| 135 points := make([]Point, len(ss)) |
| 136 for i, s := range ss { |
| 137 p := &points[i] |
| 138 if _, err := fmt.Sscanf(s, "%d %d %d", &p.X, &p.
Y, &p.Flags); err != nil { |
| 139 t.Errorf("%s: Sscanf: %v", tc.name, err) |
| 140 continue loop |
| 141 } |
| 142 } |
| 143 wants = append(wants, points) |
107 } | 144 } |
108 » » wants = append(wants, points) | 145 » » if err := scanner.Err(); err != nil && err != io.EOF { |
109 » } | 146 » » » t.Errorf("%s: Scanner: %v", tc.name, err) |
110 » if err := scanner.Err(); err != nil && err != io.EOF { | 147 » » » continue loop |
111 » » t.Fatalf("Scanner: %v", err) | 148 » » } |
112 » } | |
113 | 149 |
114 » const fontSize = 12 | 150 » » glyphBuf := NewGlyphBuf() |
115 » glyphBuf := NewGlyphBuf() | 151 » » for i, want := range wants { |
116 » for i, want := range wants { | 152 » » » // TODO: completely implement hinting. For now, only the
first |
117 » » if err = glyphBuf.Load(font, fontSize*64, Index(i), hinter); err
!= nil { | 153 » » » // tc.hintingBrokenAt glyphs of the test case's font are
correctly hinted. |
118 » » » t.Fatalf("Load: %v", err) | 154 » » » if hinter != nil && i == tc.hintingBrokenAt { |
119 » » } | 155 » » » » break |
120 » » got := glyphBuf.Point | 156 » » » } |
121 » » for i := range got { | 157 |
122 » » » got[i].Flags &= 0x01 | 158 » » » if err = glyphBuf.Load(font, tc.size*64, Index(i), hinte
r); err != nil { |
123 » » } | 159 » » » » t.Errorf("%s: glyph #%d: Load: %v", tc.name, i,
err) |
124 » » if !reflect.DeepEqual(got, want) { | 160 » » » » continue loop |
125 » » » t.Errorf("glyph #%d:\ngot %v\nwant %v\n", i, got, want) | 161 » » » } |
| 162 » » » got := glyphBuf.Point |
| 163 » » » for i := range got { |
| 164 » » » » got[i].Flags &= 0x01 |
| 165 » » » } |
| 166 » » » if !reflect.DeepEqual(got, want) { |
| 167 » » » » t.Errorf("%s: glyph #%d:\ngot %v\nwant %v\n", t
c.name, i, got, want) |
| 168 » » » } |
126 } | 169 } |
127 } | 170 } |
128 } | 171 } |
129 | 172 |
130 func TestScalingSansHinting(t *testing.T) { | 173 func TestScalingSansHinting(t *testing.T) { |
131 » testScaling(t, "luxisr-12pt-sans-hinting.txt", nil) | 174 » testScaling(t, nil) |
132 } | 175 } |
133 | 176 |
134 func TestScalingWithHinting(t *testing.T) { | 177 func TestScalingWithHinting(t *testing.T) { |
135 » testScaling(t, "luxisr-12pt-with-hinting.txt", &Hinter{}) | 178 » testScaling(t, &Hinter{}) |
136 } | 179 } |
OLD | NEW |