forked from notsecure/uTox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.c
357 lines (308 loc) · 9.72 KB
/
text.c
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
350
351
352
353
354
355
356
357
#include "main.h"
static void drawtexth(int x, int y, char_t *str, STRING_IDX length, int d, int h, int hlen, uint16_t lineheight)
{
// Draw cursor
h -= d;
if(h + hlen < 0 || h > length) {
drawtext(x, y, str, length);
return;
} else if(hlen == 0) {
drawtext(x, y, str, length);
int w = textwidth(str, h + hlen);
drawvline(x + w, y, y + lineheight, COLOR_MAIN_TEXT);
return;
}
if(h < 0) {
hlen += h;
h = 0;
if(hlen < 0) {
hlen = 0;
}
}
if(h + hlen > length) {
hlen = length - h;
}
int width;
width = drawtext_getwidth(x, y, str, h);
uint32_t color = setcolor(COLOR_SELECTION_TEXT);
int w = textwidth(str + h, hlen);
drawrectw(x + width, y, w, lineheight, COLOR_SELECTION_BACKGROUND);
drawtext(x + width, y, str + h, hlen);
width += w;
setcolor(color);
drawtext(x + width, y, str + h + hlen, length - (h + hlen));
}
int drawtextmultiline(int x, int right, int y, int top, int bottom, uint16_t lineheight, char_t *data, STRING_IDX length, STRING_IDX h, STRING_IDX hlen, _Bool multiline)
{
uint32_t c1, c2;
_Bool greentext = 0, link = 0, draw = y + lineheight >= top;
int xc = x;
char_t *a = data, *b = a, *end = a + length;
while(1) {
if(a != end) {
if(*a == '>' && (a == data || *(a - 1) == '\n')) {
c1 = setcolor(COLOR_MAIN_QUOTETEXT);
greentext = 1;
}
if((a == data || *(a - 1) == '\n' || *(a - 1) == ' ') && ((end - a >= 7 && memcmp(a, "http://", 7) == 0) || (end - a >= 8 && memcmp(a, "https://", 8) == 0))) {
c2 = setcolor(COLOR_MAIN_URLTEXT);
link = 1;
}
}
if(a == end || *a == ' ' || *a == '\n') {
int count = a - b, w = textwidth(b, count);
while(x + w > right) {
if(multiline && x == xc) {
int fit = textfit(b, count, right - x);
if(draw) {
drawtexth(x, y, b, fit, b - data, h, hlen, lineheight);
}
count -= fit;
b += fit;
y += lineheight;
draw = (y + lineheight >= top && y < bottom);
} else if(!multiline) {
int fit = textfit(b, count, right - x);
if(draw) {
drawtexth(x, y, b, fit, b - data, h, hlen, lineheight);
}
return y + lineheight;
} else {
y += lineheight;
draw = (y + lineheight >= top && y < bottom);
int l = utf8_len(b);
count -= l;
b += l;
}
x = xc;
w = textwidth(b, count);
}
if(draw) {
drawtexth(x, y, b, count, b - data, h, hlen, lineheight);
}
x += w;
b = a;
if(link) {
setcolor(c2);
link = 0;
}
if(a == end) {
if(greentext) {
setcolor(c1);
greentext = 0;
}
break;
}
if(*a == '\n') {
if(greentext) {
setcolor(c1);
greentext = 0;
}
y += lineheight;
draw = (y + lineheight >= top && y < bottom);
b += utf8_len(b);
x = xc;
}
}
a += utf8_len(a);
}
return y + lineheight;
}
STRING_IDX hittextmultiline(int mx, int right, int my, int height, uint16_t lineheight, char_t *str, STRING_IDX length, _Bool multiline)
{
if(my < 0) {
return 0;
}
if(my >= height) {
return length;
}
int x = 0;
char_t *a = str, *b = str, *end = str + length;
while(1) {
if(a == end || *a == '\n' || *a == ' ') {
int count = a - b, w = textwidth(b, a - b);
while(x + w > right && my >= lineheight) {
if(multiline && x == 0) {
int fit = textfit(b, count, right);
count -= fit;
b += fit;
my -= lineheight;
height -= lineheight;
} else if(!multiline) {
break;
} else {
my -= lineheight;
height -= lineheight;
int l = utf8_len(b);
count -= l;
b += l;
}
if(my >= -lineheight && my < 0) {
x = mx;
break;
}
x = 0;
w = textwidth(b, count);
}
if(a == end) {
if(my >= lineheight) {
return length;
}
break;
}
if((my >= 0 && my < lineheight) && (mx < 0 || (mx >= x && mx < x + w))) {
break;
}
x += w;
b = a;
if(*a == '\n') {
if(my >= 0 && my < lineheight) {
x = mx;
return a - str;
}
b += utf8_len(b);
my -= lineheight;
height -= lineheight;
x = 0;
}
}
a += utf8_len(a);
}
int fit;
if(mx >= right) {
fit = textfit(b, a - b, right - x);
} else if(mx - x > 0) {
int len = a - b;
fit = textfit_near(b, len + (a != end), mx - x);
} else {
fit = 0;
}
return (b - str) + fit;
}
int text_height(int right, uint16_t lineheight, char_t *str, STRING_IDX length)
{
int x = 0, y = 0;
char_t *a = str, *b = a, *end = a + length;
while(1) {
if(a == end || *a == ' ' || *a == '\n') {
int count = a - b, w = textwidth(b, count);
while(x + w > right) {
if(x == 0) {
int fit = textfit(b, count, right);
count -= fit;
if(fit == 0 && (count != 0 || *b == '\n')) {
return 0;
}
b += fit;
y += lineheight;
} else {
y += lineheight;
int l = utf8_len(b);
count -= l;
b += l;
}
x = 0;
w = textwidth(b, count);
}
x += w;
b = a;
if(a == end) {
break;
}
if(*a == '\n') {
y += lineheight;
b += utf8_len(b);
x = 0;
}
}
a += utf8_len(a);
}
y += lineheight;
return y;
}
static void textxy(int width, STRING_IDX pp, uint16_t lineheight, char_t *str, STRING_IDX length, int *outx, int *outy)
{
int x = 0, y = 0;
char_t *a = str, *b = str, *end = str + length, *p = str + pp;
while(1) {
if(a == end || *a == '\n' || *a == ' ') {
int count = a - b, w = textwidth(b, a - b);
while(x + w > width) {
if(x == 0) {
int fit = textfit(b, count, width);
if(p >= b && p < b + fit) {
break;
}
count -= fit;
b += fit;
y += lineheight;
} else {
y += lineheight;
int l = utf8_len(b);
count -= l;
b += l;
}
x = 0;
w = textwidth(b, count);
}
if(p >= b && p < b + count) {
w = textwidth(b, p - b);
a = end;
}
x += w;
if(a == end) {
break;
}
b = a;
if(*a == '\n') {
if(p == a) {
break;
}
b += utf8_len(b);
y += lineheight;
x = 0;
}
}
a += utf8_len(a);
}
*outx = x;
*outy = y;
}
STRING_IDX text_lineup(int width, int height, STRING_IDX p, uint16_t lineheight, char_t *str, STRING_IDX length, SCROLLABLE *scroll)
{
//lazy
int x, y;
textxy(width, p, lineheight, str, length, &x, &y);
if(y == 0) {
scroll->d = 0.0;
return p;
}
y -= lineheight;
if(scroll->content_height > height) {
double d1 = (double)y / (double)(scroll->content_height - height);
double d2 = (double)(y - height + lineheight) / (double)(scroll->content_height - height);
if(d1 < scroll->d) {
scroll->d = d1;
} else if(d2 > scroll->d) {
scroll->d = d2;
}
}
return hittextmultiline(x, width, y, INT_MAX, lineheight, str, length, 1);
}
STRING_IDX text_linedown(int width, int height, STRING_IDX p, uint16_t lineheight, char_t *str, STRING_IDX length, SCROLLABLE *scroll)
{
//lazy
int x, y;
textxy(width, p, lineheight, str, length, &x, &y);
y += lineheight;
if(scroll->content_height > height) {
double d1 = (double)y / (double)(scroll->content_height - height);
double d2 = (double)(y - height + lineheight) / (double)(scroll->content_height - height);
if(d2 > scroll->d) {
scroll->d = d2 > 1.0 ? 1.0 : d2;
} else if(d1 < scroll->d) {
scroll->d = d1;
}
}
return hittextmultiline(x, width, y, INT_MAX, lineheight, str, length, 1);
}