...
1
2
3
4
5 package font
6
7 import (
8 "image"
9 "strings"
10 "testing"
11
12 "golang.org/x/image/math/fixed"
13 )
14
15 const toyAdvance = fixed.Int26_6(10 << 6)
16
17 type toyFace struct{}
18
19 func (toyFace) Close() error {
20 return nil
21 }
22
23 func (toyFace) Glyph(dot fixed.Point26_6, r rune) (image.Rectangle, image.Image, image.Point, fixed.Int26_6, bool) {
24 panic("unimplemented")
25 }
26
27 func (toyFace) GlyphBounds(r rune) (fixed.Rectangle26_6, fixed.Int26_6, bool) {
28 return fixed.Rectangle26_6{
29 Min: fixed.P(2, 0),
30 Max: fixed.P(6, 1),
31 }, toyAdvance, true
32 }
33
34 func (toyFace) GlyphAdvance(r rune) (fixed.Int26_6, bool) {
35 return toyAdvance, true
36 }
37
38 func (toyFace) Kern(r0, r1 rune) fixed.Int26_6 {
39 return 0
40 }
41
42 func (toyFace) Metrics() Metrics {
43 return Metrics{}
44 }
45
46 func TestBound(t *testing.T) {
47 wantBounds := []fixed.Rectangle26_6{
48 {Min: fixed.P(0, 0), Max: fixed.P(0, 0)},
49 {Min: fixed.P(2, 0), Max: fixed.P(6, 1)},
50 {Min: fixed.P(2, 0), Max: fixed.P(16, 1)},
51 {Min: fixed.P(2, 0), Max: fixed.P(26, 1)},
52 }
53
54 for i, wantBound := range wantBounds {
55 s := strings.Repeat("x", i)
56 gotBound, gotAdvance := BoundString(toyFace{}, s)
57 if gotBound != wantBound {
58 t.Errorf("i=%d: bound: got %v, want %v", i, gotBound, wantBound)
59 }
60 wantAdvance := toyAdvance * fixed.Int26_6(i)
61 if gotAdvance != wantAdvance {
62 t.Errorf("i=%d: advance: got %v, want %v", i, gotAdvance, wantAdvance)
63 }
64 }
65 }
66
View as plain text