This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput_test.go
165 lines (159 loc) · 4.16 KB
/
output_test.go
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
// SPDX-License-Identifier: Unlicense OR BSD-3-Clause
package shaping_test
import (
"errors"
"reflect"
"testing"
"github.com/benoitkugler/textlayout/fonts"
"github.com/benoitkugler/textlayout/harfbuzz"
"github.com/go-text/di"
"github.com/go-text/shaping"
"golang.org/x/image/math/fixed"
)
const (
simpleGID fonts.GID = iota
leftExtentGID
rightExtentGID
deepGID
offsetGID
)
var (
expectedFontExtents = shaping.Bounds{
Ascent: fixed.I(int(15)),
Descent: fixed.I(int(-15)),
Gap: fixed.I(int(0)),
}
simpleGlyph = shaping.Glyph{
Glyph: simpleGID,
XAdvance: fixed.I(int(10)),
YAdvance: fixed.I(int(10)),
XOffset: fixed.I(int(0)),
YOffset: fixed.I(int(0)),
Width: fixed.I(int(10)),
Height: -fixed.I(int(10)),
YBearing: fixed.I(int(10)),
}
leftExtentGlyph = shaping.Glyph{
Glyph: leftExtentGID,
XAdvance: fixed.I(int(5)),
YAdvance: fixed.I(int(5)),
XOffset: fixed.I(int(0)),
YOffset: fixed.I(int(0)),
Width: fixed.I(int(10)),
Height: -fixed.I(int(10)),
YBearing: fixed.I(int(10)),
XBearing: fixed.I(int(5)),
}
rightExtentGlyph = shaping.Glyph{
Glyph: rightExtentGID,
XAdvance: fixed.I(int(5)),
YAdvance: fixed.I(int(5)),
XOffset: fixed.I(int(0)),
YOffset: fixed.I(int(0)),
Width: fixed.I(int(10)),
Height: -fixed.I(int(10)),
YBearing: fixed.I(int(10)),
XBearing: fixed.I(int(0)),
}
deepGlyph = shaping.Glyph{
Glyph: deepGID,
XAdvance: fixed.I(int(10)),
YAdvance: fixed.I(int(10)),
XOffset: fixed.I(int(0)),
YOffset: fixed.I(int(0)),
Width: fixed.I(int(10)),
Height: -fixed.I(int(10)),
YBearing: fixed.I(int(0)),
XBearing: fixed.I(int(0)),
}
offsetGlyph = shaping.Glyph{
Glyph: offsetGID,
XAdvance: fixed.I(int(10)),
YAdvance: fixed.I(int(10)),
XOffset: fixed.I(int(2)),
YOffset: fixed.I(int(2)),
Width: fixed.I(int(10)),
Height: -fixed.I(int(10)),
YBearing: fixed.I(int(10)),
XBearing: fixed.I(int(0)),
}
)
// TestRecalculate ensures that the Output.Recalculate function correctly
// computes the bounds, advance, and baseline of the output.
func TestRecalculate(t *testing.T) {
type testcase struct {
Name string
Direction di.Direction
Input []shaping.Glyph
Output shaping.Output
Error error
}
for _, tc := range []testcase{
{
Name: "empty",
Output: shaping.Output{
LineBounds: expectedFontExtents,
},
},
{
Name: "single simple glyph",
Direction: di.DirectionLTR,
Input: []shaping.Glyph{simpleGlyph},
Output: shaping.Output{
Glyphs: []shaping.Glyph{simpleGlyph},
Advance: simpleGlyph.XAdvance,
GlyphBounds: shaping.Bounds{
Ascent: simpleGlyph.YBearing,
Descent: fixed.I(0),
},
LineBounds: expectedFontExtents,
},
},
{
Name: "glyph below baseline",
Direction: di.DirectionLTR,
Input: []shaping.Glyph{simpleGlyph, deepGlyph},
Output: shaping.Output{
Glyphs: []shaping.Glyph{simpleGlyph, deepGlyph},
Advance: simpleGlyph.XAdvance + deepGlyph.XAdvance,
GlyphBounds: shaping.Bounds{
Ascent: simpleGlyph.YBearing,
Descent: deepGlyph.YBearing + deepGlyph.Height,
},
LineBounds: expectedFontExtents,
},
},
{
Name: "single complex glyph",
Direction: di.DirectionLTR,
Input: []shaping.Glyph{offsetGlyph},
Output: shaping.Output{
Glyphs: []shaping.Glyph{offsetGlyph},
Advance: offsetGlyph.XAdvance,
GlyphBounds: shaping.Bounds{
Ascent: offsetGlyph.YBearing + offsetGlyph.YOffset,
Descent: fixed.I(0),
},
LineBounds: expectedFontExtents,
},
},
{
Name: "vertical text not supported",
Direction: di.Direction(harfbuzz.BottomToTop),
Error: shaping.UnimplementedDirectionError{},
},
} {
t.Run(tc.Name, func(t *testing.T) {
output := shaping.Output{
Glyphs: tc.Input,
LineBounds: expectedFontExtents,
}
err := output.RecalculateAll(tc.Direction)
if tc.Error != nil && !errors.As(err, &tc.Error) {
t.Errorf("expected error of type %T, got %T", tc.Error, err)
} else if tc.Error == nil && !reflect.DeepEqual(output, tc.Output) {
t.Errorf("recalculation incorrect: expected %v, got %v", tc.Output, output)
}
})
}
}