1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
package style
import (
"testing"
"golang.org/x/exp/shiny/unit"
"golang.org/x/image/font/gofont/gomono"
"golang.org/x/image/math/fixed"
)
const (
dpi = 150.0
epsilon = 0.5 // TODO: optimize precision
)
var (
fontTtf = gomono.TTF
units = []unit.Unit{unit.Px, unit.Dp, unit.Pt, unit.Mm, unit.In, unit.Em, unit.Ex, unit.Ch}
)
func TestConvert(t *testing.T) {
t.Parallel()
s, err := New(Font(fontTtf), Dpi(dpi))
if err != nil {
t.Error(err)
}
defer func() {
if err := s.Close(); err != nil {
t.Fatal(err)
}
}()
// Trivial
for _, from := range units {
for _, to := range units {
testConvert(t, s, unit.Value{0, from}, to, 0)
}
}
pxPerEm := fixed26ToFloat(s.face.Metrics().Height)
pxPerEx := fixed26ToFloat(s.face.Metrics().XHeight)
advance, ok := s.face.GlyphAdvance('0')
if !ok {
t.Fail()
}
pxPerCh := fixed26ToFloat(advance)
// px → all
px := 123.4
in := px / dpi
from := unit.Value{px, unit.Px}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// dp → all
dp := 123.4
in = dp / 160
px = in * dpi
from = unit.Value{dp, unit.Dp}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, dp)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// Pt → all
pt := 123.4
in = pt / 72
px = in * dpi
from = unit.Value{pt, unit.Pt}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, pt)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// Mm → all
mm := 123.4
in = mm / 25.4
px = in * dpi
from = unit.Value{mm, unit.Mm}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, mm)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// In → all
in = 123.4
px = in * dpi
from = unit.Value{in, unit.In}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// Em → all
em := 123.4
px = em * pxPerEm
in = px / dpi
from = unit.Value{em, unit.Em}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, em)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// Ex → all
ex := 123.4
px = ex * pxPerEx
in = px / dpi
from = unit.Value{ex, unit.Ex}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, ex)
testConvert(t, s, from, unit.Ch, px/pxPerCh)
// Ch → all
ch := 123.4
px = ch * pxPerCh
in = px / dpi
from = unit.Value{ch, unit.Ch}
testConvert(t, s, from, unit.Px, px)
testConvert(t, s, from, unit.Dp, in*160)
testConvert(t, s, from, unit.Pt, in*72)
testConvert(t, s, from, unit.Mm, in*25.4)
testConvert(t, s, from, unit.In, in)
testConvert(t, s, from, unit.Em, px/pxPerEm)
testConvert(t, s, from, unit.Ex, px/pxPerEx)
testConvert(t, s, from, unit.Ch, ch)
}
func testConvert(t *testing.T, s *Style, from unit.Value, to unit.Unit, want float64) {
out := s.Convert(from, to)
if out.U != to || out.F < want-epsilon || out.F > want+epsilon {
t.Errorf("Convert(%v, %v) = %v; want %v", from, to, out, unit.Value{want, to})
}
}
func TestPixels(t *testing.T) {
t.Parallel()
s, err := New(Font(fontTtf), Dpi(dpi))
if err != nil {
t.Error(err)
}
defer func() {
if err := s.Close(); err != nil {
t.Fatal(err)
}
}()
for _, u := range units {
testPixels(t, s, unit.Value{0, u}, 0)
}
testPixels(t, s, unit.Value{123.4, unit.Px}, floatToFixed26(123.4))
testPixels(t, s, unit.Value{123.4, unit.Dp}, floatToFixed26(123.4/160*dpi))
testPixels(t, s, unit.Value{123.4, unit.Pt}, floatToFixed26(123.4/72*dpi))
testPixels(t, s, unit.Value{123.4, unit.Mm}, floatToFixed26(123.4/25.4*dpi))
testPixels(t, s, unit.Value{123.4, unit.In}, floatToFixed26(123.4*dpi))
testPixels(t, s, unit.Value{123.4, unit.Em}, floatToFixed26(123.4).Mul(s.face.Metrics().Height))
testPixels(t, s, unit.Value{123.4, unit.Ex}, floatToFixed26(123.4).Mul(s.face.Metrics().XHeight))
if advance, ok := s.face.GlyphAdvance('0'); ok {
testPixels(t, s, unit.Value{123.4, unit.Ch}, floatToFixed26(123.4).Mul(advance))
} else {
t.Fail()
}
}
func testPixels(t *testing.T, s *Style, in unit.Value, want fixed.Int26_6) {
out := s.Pixels(in)
if out != want {
t.Errorf("Pixels(%#v) = %v; want %v", in, out, want)
}
}
|