-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuffer_test.go
342 lines (277 loc) · 9.42 KB
/
buffer_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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package harfbuzz
import (
"testing"
"github.com/benoitkugler/textlayout/fonts"
"github.com/benoitkugler/textlayout/language"
)
// ported from harfbuzz/test/api/test-buffer.c Copyright © 2011 Google, Inc. Behdad Esfahbod
var utf32 = [7]rune{'a', 'b', 0x20000, 'd', 'e', 'f', 'g'}
const (
bufferEmpty = iota
bufferOneByOne
bufferUtf32
bufferNumTypes
)
func newTestBuffer(kind int) *Buffer {
b := NewBuffer()
switch kind {
case bufferEmpty:
case bufferOneByOne:
for i := 1; i < len(utf32)-1; i++ {
b.AddRune(utf32[i], i)
}
case bufferUtf32:
b.AddRunes(utf32[:], 1, len(utf32)-2)
}
return b
}
func testBufferProperties(b *Buffer, t *testing.T) {
/* test default properties */
assert(t, b.Props.Direction == 0)
assert(t, b.Props.Script == 0)
assert(t, b.Props.Language == "")
b.Props.Language = language.NewLanguage("fa")
assert(t, b.Props.Language == language.NewLanguage("Fa"))
/* test clear_contents clears all these properties: */
// hb_buffer_clear_contents (b);
// assert (t,hb_buffer_get_unicode_funcs (b) == ufuncs);
// assert (t,hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
// assert (t,hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
// assert (t,hb_buffer_get_language (b) == NULL);
/* but not these: */
// assert (t,hb_buffer_get_flags (b) != HB_BUFFER_FLAGS_DEFAULT);
// assert (t,hb_buffer_get_replacement_codepoint (b) != HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT);
/* test reset clears all properties */
// hb_buffer_set_direction (b, HB_DIRECTION_RTL);
// assert (t,hb_buffer_get_direction (b) == HB_DIRECTION_RTL);
// hb_buffer_set_script (b, HB_SCRIPT_ARABIC);
// assert (t,hb_buffer_get_script (b) == HB_SCRIPT_ARABIC);
// hb_buffer_set_language (b, hb_language_from_string ("fa", -1));
// assert (t,hb_buffer_get_language (b) == hb_language_from_string ("Fa", -1));
// hb_buffer_set_flags (b, HB_BUFFER_FLAG_BOT);
// assert (t,hb_buffer_get_flags (b) == HB_BUFFER_FLAG_BOT);
// hb_buffer_set_replacement_codepoint (b, (unsigned int) -1);
// assert (t,hb_buffer_get_replacement_codepoint (b) == (unsigned int) -1);
// hb_buffer_reset (b);
// assert (t,hb_buffer_get_unicode_funcs (b) == hb_unicode_funcs_get_default ());
// assert (t,hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
// assert (t,hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
// assert (t,hb_buffer_get_language (b) == NULL);
// assert (t,hb_buffer_get_flags (b) == HB_BUFFER_FLAGS_DEFAULT);
// assert (t,hb_buffer_get_replacement_codepoint (b) == HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT);
}
func testBufferContents(b *Buffer, kind int, t *testing.T) {
if kind == bufferEmpty {
assertEqualInt(t, len(b.Info), 0)
return
}
glyphs := b.Info
L := len(glyphs)
assertEqualInt(t, L, 5)
assertEqualInt(t, len(b.Pos), 5)
for _, g := range glyphs {
assertEqualInt(t, int(g.Mask), 0)
assertEqualInt(t, int(g.glyphProps), 0)
assertEqualInt(t, int(g.ligProps), 0)
assertEqualInt(t, int(g.syllable), 0)
assertEqualInt(t, int(g.unicode), 0)
assertEqualInt(t, int(g.complexAux), 0)
assertEqualInt(t, int(g.complexCategory), 0)
}
for i, g := range glyphs {
cluster := 1 + i
assertEqualInt(t, int(g.codepoint), int(utf32[1+i]))
assertEqualInt(t, g.Cluster, cluster)
}
/* reverse, test, and reverse back */
b.Reverse()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[L-i]))
}
b.Reverse()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[1+i]))
}
// reverse_clusters works same as reverse for now since each codepoint is
// in its own cluster
b.reverseClusters()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[L-i]))
}
b.reverseClusters()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[1+i]))
}
/* now form a cluster and test again */
glyphs[2].Cluster = glyphs[1].Cluster
/* reverse, test, and reverse back */
b.Reverse()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[L-i]))
}
b.Reverse()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[1+i]))
}
// reverse_clusters twice still should return the original string,
// but when applied once, the 1-2 cluster should be retained.
b.reverseClusters()
for i, g := range glyphs {
j := L - 1 - i
if j == 1 {
j = 2
} else if j == 2 {
j = 1
}
assertEqualInt(t, int(g.codepoint), int(utf32[1+j]))
}
b.reverseClusters()
for i, g := range glyphs {
assertEqualInt(t, int(g.codepoint), int(utf32[1+i]))
}
/* test reset clears content */
b.Clear()
assertEqualInt(t, len(b.Info), 0)
assertEqualInt(t, len(b.Pos), 0)
}
func testBufferPositions(b *Buffer, t *testing.T) {
/* Without shaping, positions should all be zero */
assertEqualInt(t, len(b.Info), len(b.Pos))
for _, pos := range b.Pos {
assertEqualInt(t, 0, int(pos.XAdvance))
assertEqualInt(t, 0, int(pos.YAdvance))
assertEqualInt(t, 0, int(pos.XOffset))
assertEqualInt(t, 0, int(pos.YOffset))
assertEqualInt(t, 0, int(pos.attachChain))
assertEqualInt(t, 0, int(pos.attachType))
}
// /* test reset clears content */
// hb_buffer_reset (b);
// assertEqualInt (t, hb_buffer_get_length (b), ==, 0);
}
func TestBuffer(t *testing.T) {
for i := 0; i < bufferNumTypes; i++ {
buffer := newTestBuffer(i)
testBufferProperties(buffer, t)
testBufferContents(buffer, i, t)
testBufferPositions(buffer, t)
}
}
/*
* Comparing buffers.
*/
/**
*
* Flags from comparing two #hb_buffer_t's.
*
* Buffer with different #hb_buffer_content_type_t cannot be meaningfully
* compared in any further detail.
*
* For buffers with differing length, the per-glyph comparison is not
* attempted, though we do still scan reference buffer for dotted circle and
* `.notdef` glyphs.
*
* If the buffers have the same length, we compare them glyph-by-glyph and
* report which aspect(s) of the glyph info/position are different.
*
* Since: 1.5.0
*/
const (
bufferDiffFlagEqual = 0x0000
// /* Buffers with different content_type cannot be meaningfully compared
// * in any further detail. */
// HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH = 0x0001
/* For buffers with differing length, the per-glyph comparison is not
* attempted, though we do still scan reference for dottedcircle / .notdef
* glyphs. */
HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH = 0x0002
/* We want to know if dottedcircle / .notdef glyphs are present in the
* reference, as we may not care so much about other differences in this
* case. */
HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT = 0x0004
HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT = 0x0008
/* If the buffers have the same length, we compare them glyph-by-glyph
* and report which aspect(s) of the glyph info/position are different. */
HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH = 0x0010
HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH = 0x0020
HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH = 0x0040
HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH = 0x0080
)
/**
* hb_buffer_diff:
* @buffer: a buffer.
* @reference: other buffer to compare to.
* @dottedcircleGlyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepont_t) -1.
* @positionFuzz: allowed absolute difference in position values.
*
* If dottedcircleGlyph is (hb_codepoint_t) -1 then #HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
* and #HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned. This should be used by most
* callers if just comparing two buffers is needed.
*
* Since: 1.5.0
**/
func bufferDiff(buffer, reference *Buffer, dottedcircleGlyph fonts.GID, positionFuzz int32) int {
// if (buffer.content_type != reference.content_type && buffer.len && reference.len){
// return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;}
result := bufferDiffFlagEqual
contains := dottedcircleGlyph != ^fonts.GID(0)
count := len(reference.Info)
if len(buffer.Info) != count {
/*
* we can't compare glyph-by-glyph, but we do want to know if there
* are .notdef or dottedcircle glyphs present in the reference buffer
*/
info := reference.Info
for i := 0; i < count; i++ {
if contains && info[i].Glyph == dottedcircleGlyph {
result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
}
if contains && info[i].Glyph == 0 {
result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT
}
}
result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH
return result
}
if count == 0 {
return result
}
bufInfo := buffer.Info
refInfo := reference.Info
for i := 0; i < count; i++ {
if bufInfo[i].codepoint != refInfo[i].codepoint {
result |= HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH
}
if bufInfo[i].Cluster != refInfo[i].Cluster {
result |= HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH
}
if (bufInfo[i].Mask & ^refInfo[i].Mask & glyphFlagDefined) != 0 {
result |= HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH
}
if contains && refInfo[i].Glyph == dottedcircleGlyph {
result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
}
if contains && refInfo[i].Glyph == 0 {
result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT
}
}
isDifferent := func(a, b int32) bool {
d := a - b
if d < 0 {
d = -d
}
return d > positionFuzz
}
bufPos := buffer.Pos
refPos := reference.Pos
for i := 0; i < count; i++ {
if isDifferent(bufPos[i].XAdvance, refPos[i].XAdvance) ||
isDifferent(bufPos[i].YAdvance, refPos[i].YAdvance) ||
isDifferent(bufPos[i].XOffset, refPos[i].XOffset) ||
isDifferent(bufPos[i].YOffset, refPos[i].YOffset) {
result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH
break
}
}
return result
}