-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_layout_test.v
More file actions
277 lines (227 loc) · 5.82 KB
/
_layout_test.v
File metadata and controls
277 lines (227 loc) · 5.82 KB
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
module vglyph
import gg as _
// Test context creation and cleanup
fn test_context_creation() {
mut ctx := new_context(1.0) or {
assert false, 'Failed to create context: ${err}'
return
}
ctx.free()
}
// Test basic layout generation
fn test_layout_simple_text() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
cfg := TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
block: BlockStyle{
width: -1
align: .left
}
}
layout := ctx.layout_text('Hello World', cfg)!
// Should have items
assert layout.items.len > 0
// Should have char rects equal to text length
assert layout.char_rects.len == 'Hello World'.len
// Check content of first item
$if debug {
assert layout.items[0].run_text == 'Hello World'
}
}
// Test empty text
fn test_layout_empty_text() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
cfg := TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
}
layout := ctx.layout_text('', cfg)!
assert layout.items.len == 0
assert layout.char_rects.len == 0
}
// Test wrapping
fn test_layout_wrapping() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
cfg := TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
block: BlockStyle{
width: 50
wrap: .word
}
}
text := 'This is a long text that should wrap'
layout := ctx.layout_text(text, cfg)!
assert layout.items.len > 1
}
// Test hit testing
fn test_hit_test() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
cfg := TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
block: BlockStyle{
width: -1
}
}
// "A" is clearly at 0,0
layout := ctx.layout_text('A', cfg)!
// Test hit at middle of first char
index := layout.hit_test(5, 5)
assert index == 0
// Test miss
miss_index := layout.hit_test(-10, -10)
assert miss_index == -1
}
// Test markup
fn test_layout_markup() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
cfg := TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
use_markup: true
}
// Text with color
text := '<span foreground="#FF0000">Red</span>'
layout := ctx.layout_text(text, cfg)!
assert layout.items.len > 0
// Check color of first item
item := layout.items[0]
// Correct color should be Red (255, 0, 0, 255)
assert item.color.r == 255
assert item.color.g == 0
assert item.color.b == 0
}
// Test hit test rect
fn test_hit_test_rect() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
cfg := TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
block: BlockStyle{
width: -1
}
}
layout := ctx.layout_text('A', cfg)!
// Test hit at middle of first char
rect := layout.hit_test_rect(5, 5) or {
assert false, 'Should have hit'
return
}
// Basic validation that we got a reasonable rect
assert rect.width > 0
assert rect.height > 0
// Test miss
if _ := layout.hit_test_rect(-10, -10) {
assert false, 'Should have missed'
}
}
// Test vertical layout dimensions
fn test_vertical_layout_dimensions() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
// Vertical layout should have width < height for stacked text
mut layout := ctx.layout_text('ABC', TextConfig{
style: TextStyle{
font_name: 'Sans 12'
}
orientation: .vertical
})!
defer { layout.destroy() }
// For 3 chars stacked vertically:
// - visual_width ~ line_height (column width)
// - visual_height ~ 3 * line_height
assert layout.visual_height > layout.visual_width, 'vertical text should be taller than wide'
assert layout.visual_height > 0, 'vertical layout has height'
assert layout.visual_width > 0, 'vertical layout has width'
}
// Test horizontal layout dimensions (regression guard)
fn test_horizontal_layout_dimensions() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
mut layout := ctx.layout_text('ABC', TextConfig{
style: TextStyle{
font_name: 'Sans 12'
}
orientation: .horizontal
})!
defer { layout.destroy() }
// Horizontal text should be wider than tall (single line)
assert layout.visual_width > layout.visual_height, 'horizontal text should be wider than tall'
}
// Test vertical glyph advances
fn test_vertical_glyph_advances() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
mut layout := ctx.layout_text('AB', TextConfig{
style: TextStyle{
font_name: 'Sans 12'
}
orientation: .vertical
})!
defer { layout.destroy() }
// Vertical glyphs should have y_advance (not x_advance)
if layout.glyphs.len >= 2 {
glyph := layout.glyphs[0]
assert glyph.x_advance == 0, 'vertical glyph has no x_advance'
assert glyph.y_advance != 0, 'vertical glyph has y_advance'
}
}
// Test log_attrs extraction during layout
fn test_log_attrs_extraction() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
text := 'Hello'
mut layout := ctx.layout_text(text, TextConfig{
style: TextStyle{
font_name: 'Sans 12'
}
})!
defer { layout.destroy() }
// log_attrs should have len = text.len + 1 (positions before each char + end)
assert layout.log_attrs.len == text.len + 1, 'log_attrs len should be text.len + 1'
}
// Test get_cursor_pos returns valid geometry
fn test_get_cursor_pos() {
mut ctx := new_context(1.0)!
defer { ctx.free() }
text := 'Hello'
mut layout := ctx.layout_text(text, TextConfig{
style: TextStyle{
font_name: 'Sans 20'
}
})!
defer { layout.destroy() }
// Valid index at start
pos := layout.get_cursor_pos(0) or {
assert false, 'cursor pos at 0 should succeed'
return
}
assert pos.height > 0, 'cursor height should be positive'
// Valid index at end (byte_index == text.len)
end_pos := layout.get_cursor_pos(text.len) or {
assert false, 'cursor pos at end should succeed'
return
}
assert end_pos.height > 0, 'cursor height at end should be positive'
// Invalid index
if _ := layout.get_cursor_pos(-1) {
assert false, 'cursor pos at -1 should fail'
}
if _ := layout.get_cursor_pos(text.len + 1) {
assert false, 'cursor pos beyond end should fail'
}
}