-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.c
152 lines (128 loc) · 3.84 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
#include "main.h"
static void drawtexth(int x, int y, char_t *str, uint16_t length, int d, int h, int hlen, uint16_t lineheight)
{
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, BLACK);
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(TEXT_HIGHLIGHT);
int w = textwidth(str + h, hlen);
drawrectw(x + width, y, w, lineheight, TEXT_HIGHLIGHT_BG);
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, uint16_t lineheight, char_t *data, uint16_t length, uint16_t h, uint16_t hlen, _Bool multiline)
{
int xc = x;
char_t *a = data, *b = a, *end = a + length;
while(1) {
if(a == end || *a == ' ' || *a == '\n') {
int count = a - b, w = textwidth(b, count);
if(x + w > right) {
if(x != xc || *a == '\n') {
if(!multiline) {
drawtexth(x, y, b, count, b - data, h, hlen, lineheight);
return y + lineheight;
}
y += lineheight;
int l = utf8_len(b);
count -= l;
b += l;
}
x = xc;
w = textwidth(b, count);
}
drawtexth(x, y, b, count, b - data, h, hlen, lineheight);
x += w;
b = a;
if(a == end) {
break;
}
if(*a == '\n') {
y += lineheight;
b += utf8_len(b);
x = xc;
}
}
a += utf8_len(a);
}
return y + lineheight;
}
uint16_t hittextmultiline(int mx, int right, int my, int height, uint16_t lineheight, char_t *str, uint16_t length, _Bool multiline)
{
if(mx < 0 && 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);
if(x + w > right) {
if(x != 0 || *a == '\n') {
if(!multiline) {
break;
}
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 || (mx < 0 && my >= 0 && my <= lineheight) || (mx >= x && mx < x + w && my >= 0 && (my < lineheight || height == lineheight))) {
break;
}
x += w;
b = a;
if(*a == '\n') {
b += utf8_len(b);
if(my >= 0 && my < lineheight) {
x = mx;
break;
}
if(!multiline) {
return 0;
}
my -= lineheight;
height -= lineheight;
x = 0;
}
}
a += utf8_len(a);
}
int fit;
if(mx - x > 0) {
int len = a - b;
fit = textfit(b, len, mx - x);
} else {
fit = 0;
}
return (b - str) + fit;
}