-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_layout.cpp
More file actions
249 lines (233 loc) · 7.97 KB
/
4coder_layout.cpp
File metadata and controls
249 lines (233 loc) · 7.97 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
/*
4coder_layout.cpp - Implementation of basic lookup routines on line layout data.
*/
// TOP
internal i64
layout_nearest_pos_to_xy(Layout_Item_List list, Vec2_f32 p){
i64 closest_match = 0;
if (p.y < 0.f){
closest_match = list.manifested_index_range.min;
}
else if (p.y >= list.height){
closest_match = list.manifested_index_range.max;
}
else{
if (0.f < p.x && p.x < max_f32){
f32 closest_x = -max_f32;
for (Layout_Item_Block *block = list.first;
block != 0;
block = block->next){
i64 count = block->item_count;
Layout_Item *item = block->items;
for (i32 i = 0; i < count; i += 1, item += 1){
if (HasFlag(item->flags, LayoutItemFlag_Ghost_Character)){
continue;
}
// NOTE(allen): This only works if we build layouts in y-sorted order.
if (p.y < item->rect.y0){
goto double_break;
}
if (item->padded_y1 <= p.y){
continue;
}
f32 dist0 = p.x - item->rect.x0;
f32 dist1 = item->rect.x1 - p.x;
if (dist0 >= 0.f && dist1 > 0.f){
closest_match = item->index;
goto double_break;
}
// NOTE(allen): One of dist0 and dist1 are negative, but certainly not both.
// 1. Take the negative one.
// 2. If the negative distance is larger than closest_x, then this is closer.
f32 neg_dist = Min(dist0, dist1);
if (closest_x < neg_dist){
closest_x = neg_dist;
closest_match = item->index;
}
}
}
double_break:;
}
else{
if (p.x == max_f32){
Layout_Item *prev_item = 0;
for (Layout_Item_Block *block = list.first;
block != 0;
block = block->next){
i64 count = block->item_count;
Layout_Item *item = block->items;
for (i32 i = 0; i < count; i += 1, item += 1){
if (HasFlag(item->flags, LayoutItemFlag_Ghost_Character)){
continue;
}
if (p.y < item->rect.y0){
goto double_break_2;
}
prev_item = item;
if (item->padded_y1 <= p.y){
continue;
}
}
}
double_break_2:;
if (prev_item != 0){
closest_match = prev_item->index;
}
else{
closest_match = list.manifested_index_range.max;
}
}
else{
Layout_Item *closest_item = 0;
for (Layout_Item_Block *block = list.first;
block != 0;
block = block->next){
i64 count = block->item_count;
Layout_Item *item = block->items;
for (i32 i = 0; i < count; i += 1, item += 1){
if (HasFlag(item->flags, LayoutItemFlag_Ghost_Character)){
continue;
}
// NOTE(allen): This only works if we build layouts in y-sorted order.
if (p.y < item->rect.y0){
goto double_break_3;
}
if (item->padded_y1 <= p.y){
continue;
}
closest_item = item;
goto double_break_3;
}
}
double_break_3:;
if (closest_item != 0){
closest_match = closest_item->index;
}
else{
closest_match = list.manifested_index_range.min;
}
}
}
}
return(closest_match);
}
internal Layout_Item*
layout_get_first_with_index(Layout_Item_List list, i64 index){
Layout_Item *result = 0;
Layout_Item *prev = 0;
for (Layout_Item_Block *block = list.first;
block != 0;
block = block->next){
i64 count = block->item_count;
Layout_Item *item = block->items;
for (i32 i = 0; i < count; i += 1, item += 1){
if (HasFlag(item->flags, LayoutItemFlag_Ghost_Character)){
continue;
}
if (item->index > index){
result = prev;
goto done;
}
if (item->index == index){
result = item;
goto done;
}
prev = item;
}
}
if (result == 0){
result = prev;
}
done:;
return(result);
}
internal Rect_f32
layout_box_of_pos(Layout_Item_List list, i64 index){
Rect_f32 result = {};
Layout_Item *item = layout_get_first_with_index(list, index);
if (item != 0){
result = item->rect;
}
return(result);
}
function Rect_f32
layout_padded_box_of_pos(Layout_Item_List list, i64 index){
Rect_f32 result = {};
Layout_Item *item = layout_get_first_with_index(list, index);
if (item != 0){
result.x0 = item->rect.x0;
result.y0 = item->rect.y0;
result.x1 = item->rect.x1;
result.y1 = item->padded_y1;
}
return(result);
}
internal i64
layout_get_pos_at_character(Layout_Item_List list, i64 character){
i64 result = 0;
if (character <= 0){
result = list.manifested_index_range.min;
}
else if (character >= list.character_count){
result = list.manifested_index_range.max;
}
else{
i64 counter = 0;
i64 next_counter = 0;
for (Layout_Item_Block *node = list.first;
node != 0;
node = node->next, counter = next_counter){
next_counter = counter + node->character_count;
if (character >= next_counter){
continue;
}
i64 count = node->item_count;
i64 relative_character = character - counter;
i64 relative_character_counter = 0;
Layout_Item *item = node->items;
for (i64 i = 0; i < count; i += 1, item += 1){
if (HasFlag(item->flags, LayoutItemFlag_Ghost_Character)){
continue;
}
if (relative_character_counter == relative_character){
result = item->index;
break;
}
relative_character_counter += 1;
}
break;
}
}
return(result);
}
internal i64
layout_character_from_pos(Layout_Item_List list, i64 index){
i64 result = 0;
i64 prev_index = -1;
if (index <= list.manifested_index_range.first){
result = 0;
}
else if (index > list.manifested_index_range.one_past_last){
result = list.character_count - 1;
}
else{
for (Layout_Item_Block *node = list.first;
node != 0;
node = node->next){
Layout_Item *item = node->items;
i64 count = node->item_count;
for (i64 i = 0; i < count; i += 1, item += 1){
if (item->index >= index){
goto double_break;
}
if (item->index > prev_index){
prev_index = item->index;
result += 1;
}
}
}
}
double_break:;
return(result);
}
// BOTTOM