This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8_textedit.h
351 lines (265 loc) · 8.98 KB
/
utf8_textedit.h
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
343
344
345
346
347
348
349
struct Mutable_String
{
u8 *str;
u64 size;
u64 cap;
operator String() {
return {str, size};
}
};
struct Text_Input
{
Mutable_String buffer;
s64 cursor_pos, mark_pos;
f32 target_scroll_x;
f32 scroll_anim_start, scroll_anim_t; // for animation
f32 current_scroll_x;
// @todo store how much bytes one should skip for draw only calls when text input isn't dirty
b32 is_dirty; // @todo store last width of text input so it automatically becoes dirty if width changes
// could be set to -1.f to force dirty state
};
static String truncate_invalid_utf8_ending(String input)
{
// This function assumes that our input was a valid utf8 string at some point
// but it got truncated without much care - possibly in the middle of a codepoint.
// It removes up to 3 bytes if string's ending is an incorrect utf8 sequence.
String result = input;
s32 follower_byte_count = 0; // counter for bytes with 10xxxxxx bits
s64 end_at = get_max(0, input.size - 4);
for (s64 index = input.size-1; index >= end_at; index -= 1)
{
u8 byte = input.str[index];
u8 byte_class = utf8_top5b_class[byte >> 3];
if (byte_class == 0)
{
follower_byte_count += 1;
}
else
{
if (byte_class != follower_byte_count + 1)
{
result.size = index;
}
break;
}
}
return result;
}
struct Text_Replace_Range_Result
{
b32 did_anything;
s64 cursor_pos;
};
static Text_Replace_Range_Result text_replace_range(Mutable_String *buffer,
s64 selection_start, s64 one_past_selection_end,
String insert_text)
{
selection_start = get_min(get_max(0, selection_start), (s64)buffer->size);
one_past_selection_end = get_min(get_max(0, one_past_selection_end), (s64)buffer->size);
if (selection_start > one_past_selection_end)
{
s64 temp = selection_start;
selection_start = one_past_selection_end;
one_past_selection_end = temp;
}
Text_Replace_Range_Result result = {};
s64 to_delete = one_past_selection_end - selection_start;
s64 initial_buffer_size_delta = insert_text.size - to_delete;
{ // truncate insert_text to fit in the buffer;
s64 max_positive_buffer_delta = buffer->cap - buffer->size;
if (max_positive_buffer_delta < initial_buffer_size_delta)
{
s64 reduce_size = initial_buffer_size_delta - max_positive_buffer_delta;
assert((s64)insert_text.size >= reduce_size);
insert_text.size -= reduce_size;
insert_text = truncate_invalid_utf8_ending(insert_text);
}
}
s64 buffer_size_delta = insert_text.size - to_delete;
if ((!buffer_size_delta && !to_delete) ||
(initial_buffer_size_delta > 0 && buffer_size_delta < 0))
{
return result;
}
if (buffer_size_delta > 0)
{
for (s64 index = buffer->size - 1;
index >= one_past_selection_end;
index -= 1)
{
buffer->str[index + buffer_size_delta] = buffer->str[index];
}
}
else if (buffer_size_delta < 0)
{
for (s64 index = one_past_selection_end;
index < (s64)buffer->size;
index += 1)
{
buffer->str[index + buffer_size_delta] = buffer->str[index];
}
}
for (u64 index = 0; index < insert_text.size; index += 1)
{
buffer->str[selection_start + index] = insert_text.str[index];
}
buffer->size += buffer_size_delta;
result.did_anything = true;
result.cursor_pos = selection_start + insert_text.size;
return result;
}
static void text_input_write(Text_Input *text_input, String new_text)
{
Text_Replace_Range_Result res = text_replace_range(&text_input->buffer, text_input->cursor_pos,
text_input->mark_pos, new_text);
if (res.did_anything)
{
text_input->cursor_pos = text_input->mark_pos = res.cursor_pos;
text_input->is_dirty = true;
}
}
static b32 text_input_has_selection(Text_Input *text_input)
{
return (text_input->cursor_pos != text_input->mark_pos);
}
static s64 str_move_pos_by_codepoints(String text, s64 pos, s64 move_by_codepoint_count)
{
if (pos > (s64)text.size) {
pos = text.size;
} else if (pos < 0) {
pos = 0;
}
else
{
s64 change = 0;
if (move_by_codepoint_count > 0)
{
text = str_skip(text, pos);
while (move_by_codepoint_count && text.size)
{
Unicode_Consume consume = utf8_consume(text);
text = str_skip(text, consume.inc);
change += consume.inc;
move_by_codepoint_count -= 1;
}
}
else if (move_by_codepoint_count < 0)
{
text = str_prefix(text, pos);
while (move_by_codepoint_count && text.size)
{
Unicode_Consume_Reverse consume = utf8_consume_reverse(text);
text = str_chop(text, consume.dec);
change -= consume.dec;
move_by_codepoint_count += 1;
}
}
pos += change;
}
return pos;
}
static b32 is_word_separator(u32 c)
{
return (is_whitespace(c) ||
(c >= '!' && c <= '/') ||
(c >= ':' && c <= '@') ||
(c >= '[' && c <= '^') ||
(c >= '{' && c <= '~'));
}
static s64 str_move_pos_by_words(String text, s64 pos, s64 move_by_word_count)
{
if (pos > (s64)text.size) {
pos = text.size;
} else if (pos < 0) {
pos = 0;
}
else
{
s64 change = 0;
b32 skipping_over_separators = true;
if (move_by_word_count > 0)
{
text = str_skip(text, pos);
while (text.size)
{
Unicode_Consume consume = utf8_consume(text);
text = str_skip(text, consume.inc);
if (is_word_separator(consume.codepoint))
{
if (!skipping_over_separators)
{
move_by_word_count -= 1;
skipping_over_separators = true;
}
}
else
{
if (!move_by_word_count) {
break;
}
skipping_over_separators = false;
}
change += consume.inc;
}
}
else if (move_by_word_count < 0)
{
text = str_prefix(text, pos);
while (text.size)
{
Unicode_Consume_Reverse consume = utf8_consume_reverse(text);
text = str_chop(text, consume.dec);
if (is_word_separator(consume.codepoint))
{
if (!skipping_over_separators)
{
move_by_word_count += 1;
if (!move_by_word_count) {
break;
}
skipping_over_separators = true;
}
}
else
{
skipping_over_separators = false;
}
change -= consume.dec;
}
}
pos += change;
}
return pos;
}
enum Text_Input_Move_Flags
{
TextInputMove_Select = (1 << 0),
TextInputMove_ByWords = (1 << 1),
TextInputMove_ByMax = (1 << 2),
};
static void text_input_move_cursor(Text_Input *text_input, s64 move_by, u32 flags)
{
text_input->is_dirty = true;
if (!flags && text_input_has_selection(text_input))
{
if (move_by > 0) {
s64 max_pos = get_max(text_input->cursor_pos, text_input->mark_pos);
text_input->cursor_pos = text_input->mark_pos = max_pos;
} else {
s64 min_pos = get_min(text_input->cursor_pos, text_input->mark_pos);
text_input->cursor_pos = text_input->mark_pos = min_pos;
}
return;
}
if (flags & TextInputMove_ByMax) {
text_input->cursor_pos = (move_by >= 0 ? text_input->buffer.size : 0);
} else if (flags & TextInputMove_ByWords) {
text_input->cursor_pos = str_move_pos_by_words(text_input->buffer, text_input->cursor_pos, move_by);
} else {
text_input->cursor_pos = str_move_pos_by_codepoints(text_input->buffer, text_input->cursor_pos, move_by);
}
if (!(flags & TextInputMove_Select))
{
text_input->mark_pos = text_input->cursor_pos;
}
}