1
2
3
4
5 package text_test
6
7 import (
8 "testing"
9
10 stdfnt "golang.org/x/image/font"
11
12 "gonum.org/v1/plot/font"
13 "gonum.org/v1/plot/font/liberation"
14 "gonum.org/v1/plot/text"
15 "gonum.org/v1/plot/vg"
16 )
17
18 func TestPlainText(t *testing.T) {
19 type box struct{ w, h, d vg.Length }
20
21 fonts := font.NewCache(liberation.Collection())
22
23 tr12 := font.Font{Variant: "Serif", Size: 12}
24 ti12 := font.Font{Variant: "Serif", Size: 12, Style: stdfnt.StyleItalic}
25 tr42 := font.Font{Variant: "Serif", Size: 42}
26
27 for _, tc := range []struct {
28 txt string
29 fnt font.Font
30 box []box
31 w vg.Length
32 h vg.Length
33 }{
34 {
35 txt: "",
36 box: []box{
37 {0, 10.693359375, 2.595703125},
38 },
39 w: 0,
40 h: 13.2890625,
41 },
42 {
43 txt: " ",
44 box: []box{{3, 10.693359375, 2.595703125}},
45 w: 3,
46 h: 13.2890625,
47 },
48 {
49 txt: "hello",
50 box: []box{{23.994140625, 10.693359375, 2.595703125}},
51 w: 23.994140625,
52 h: 13.2890625,
53 },
54 {
55 txt: "hello",
56 fnt: ti12,
57 box: []box{{23.994140625, 10.693359375, 2.595703125}},
58 w: 23.994140625,
59 h: 13.2890625,
60 },
61 {
62 txt: "hello",
63 fnt: tr42,
64 box: []box{{83.9794921875, 37.4267578125, 9.0849609375}},
65 w: 83.9794921875,
66 h: 46.51171875,
67 },
68 {
69 txt: "hello\n",
70 box: []box{{23.994140625, 10.693359375, 2.595703125}},
71 w: 23.994140625,
72 h: 13.2890625,
73 },
74 {
75 txt: "Agg",
76 box: []box{{20.666015625, 10.693359375, 2.595703125}},
77 w: 20.666015625,
78 h: 13.2890625,
79 },
80 {
81 txt: "Agg",
82 fnt: ti12,
83 box: []box{{19.330078125, 10.693359375, 2.595703125}},
84 w: 19.330078125,
85 h: 13.2890625,
86 },
87 {
88 txt: "Agg",
89 fnt: tr42,
90 box: []box{{72.3310546875, 37.4267578125, 9.0849609375}},
91 w: 72.3310546875,
92 h: 46.51171875,
93 },
94 {
95 txt: "\n",
96 box: []box{
97 {0, 10.693359375, 2.595703125},
98 },
99 w: 0,
100 h: 13.2890625,
101 },
102 {
103 txt: "\n ",
104 box: []box{
105 {0, 10.693359375, 2.595703125},
106 {3, 10.693359375, 2.595703125},
107 },
108 w: 3,
109 h: 27.087890625,
110 },
111 {
112 txt: " \n ",
113 box: []box{
114 {3, 10.693359375, 2.595703125},
115 {3, 10.693359375, 2.595703125},
116 },
117 w: 3,
118 h: 27.087890625,
119 },
120 {
121 txt: "hello\nworld",
122 box: []box{
123 {23.994140625, 10.693359375, 2.595703125},
124 {27.996093750, 10.693359375, 2.595703125},
125 },
126 w: 27.996093750,
127 h: 27.087890625,
128 },
129 {
130 txt: "Agg\nBpp",
131 box: []box{
132 {20.666015625, 10.693359375, 2.595703125},
133 {20.003906250, 10.693359375, 2.595703125},
134 },
135 w: 20.666015625,
136 h: 27.087890625,
137 },
138 } {
139 t.Run(tc.txt, func(t *testing.T) {
140 fnt := tc.fnt
141 if fnt == (font.Font{}) {
142 fnt = tr12
143 }
144
145 sty := text.Style{
146 Font: fnt,
147 Handler: &text.Plain{Fonts: fonts},
148 }
149
150 lines := sty.Handler.Lines(tc.txt)
151 if got, want := len(lines), len(tc.box); got != want {
152 t.Errorf("invalid number of lines: got=%d, want=%d", got, want)
153 }
154
155 for i, line := range lines {
156 var b box
157 b.w, b.h, b.d = sty.Handler.Box(line, sty.Font)
158
159 if got, want := b, tc.box[i]; got != want {
160 t.Errorf("invalid box[%d]: got=%v, want=%v", i, got, want)
161 }
162 }
163
164 w := sty.Width(tc.txt)
165 if got, want := w, tc.w; got != want {
166 t.Errorf("invalid width: got=%v, want=%v", got, want)
167 }
168
169 h := sty.Height(tc.txt)
170 if got, want := h, tc.h; got != want {
171 t.Errorf("invalid height: got=%v, want=%v", got, want)
172 }
173 })
174 }
175 }
176
View as plain text