/
text.c
476 lines (414 loc) · 10.7 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include "text.h"
#include "gfx.h"
#include "io.h"
int FSIZE_BIG=28;
int FSIZE_MED=24;
int FSIZE_SMALL=20;
int TABULATOR=72;
int OFFSET_MED=10;
int OFFSET_SMALL=5;
int OFFSET_MIN=2;
extern int flash;
extern int scale2res(int s);
static char *sc = "aouAOUzd",
*su= "\xA4\xB6\xBC\x84\x96\x9C\x9F",
*tc="\xE4\xF6\xFC\xC4\xD6\xDC\xDF\xB0";
// from neutrino/src/driver/fontrenderer.cpp
int UTF8ToUnicode(char **textp, const int utf8_encoded) // returns -1 on error
{
int unicode_value, i;
char *text = *textp;
if (utf8_encoded && ((((unsigned char)(*text)) & 0x80) != 0))
{
int remaining_unicode_length;
if ((((unsigned char)(*text)) & 0xf8) == 0xf0) {
unicode_value = ((unsigned char)(*text)) & 0x07;
remaining_unicode_length = 3;
} else if ((((unsigned char)(*text)) & 0xf0) == 0xe0) {
unicode_value = ((unsigned char)(*text)) & 0x0f;
remaining_unicode_length = 2;
} else if ((((unsigned char)(*text)) & 0xe0) == 0xc0) {
unicode_value = ((unsigned char)(*text)) & 0x1f;
remaining_unicode_length = 1;
} else {
(*textp)++;
return -1;
}
*textp += remaining_unicode_length;
for (i = 0; *text && i < remaining_unicode_length; i++) {
text++;
if (((*text) & 0xc0) != 0x80) {
remaining_unicode_length = -1;
return -1; // incomplete or corrupted character
}
unicode_value <<= 6;
unicode_value |= ((unsigned char)(*text)) & 0x3f;
}
} else
unicode_value = (unsigned char)(*text);
(*textp)++;
return unicode_value;
}
void CopyUTF8Char(char **to, char **from)
{
int remaining_unicode_length;
if (!((unsigned char)(**from) & 0x80))
remaining_unicode_length = 1;
else if ((((unsigned char)(**from)) & 0xf8) == 0xf0)
remaining_unicode_length = 4;
else if ((((unsigned char)(**from)) & 0xf0) == 0xe0)
remaining_unicode_length = 3;
else if ((((unsigned char)(**from)) & 0xe0) == 0xc0)
remaining_unicode_length = 2;
else {
(*from)++;
return;
}
while (**from && remaining_unicode_length) {
**to = **from;
(*from)++, (*to)++, remaining_unicode_length--;
}
}
int isValidUTF8(char *text) {
while (*text)
if (-1 == UTF8ToUnicode(&text, 1))
return 0;
return 1;
}
void TranslateString(char *src, size_t size)
{
char *fptr = src;
size_t src_len = strlen(src);
char *tptr_start = alloca(src_len * 4 + 1);
char *tptr = tptr_start;
if (isValidUTF8(src)) {
strncpy(tptr_start, fptr, src_len + 1);
}
else {
while (*fptr) {
int i;
for (i = 0; tc[i] && (tc[i] != *fptr); i++);
if (tc[i]) {
*tptr++ = 0xC3;
*tptr++ = su[i];
fptr++;
} else if (*fptr & 0x80)
fptr++;
else
*tptr++ = *fptr++;
}
*tptr = 0;
}
fptr = tptr_start;
tptr = src;
char *tptr_end = src + size - 4;
while (*fptr && tptr <= tptr_end) {
if (*fptr == '~') {
fptr++;
int i;
for (i = 0; sc[i] && (sc[i] != *fptr); i++);
if (*fptr == 'd') {
*tptr++ = 0xC2;
*tptr++ = 0xb0;
fptr++;
} else if (sc[i]) {
*tptr++ = 0xC3;
*tptr++ = su[i];
fptr++;
} else {
*tptr++ = '~';
}
} else {
CopyUTF8Char(&tptr, &fptr);
}
}
*tptr = 0;
}
/******************************************************************************
* MyFaceRequester
******************************************************************************/
FT_Error MyFaceRequester(FTC_FaceID face_id, FT_Library lib, FT_Pointer request_data __attribute__((unused)), FT_Face *aface)
{
FT_Error result;
result = FT_New_Face(lib, face_id, 0, aface);
if (result)
printf("%s <Font \"%s\" failed>\n", __plugin__, (char*)face_id);
return result;
}
/******************************************************************************
* RenderChar
******************************************************************************/
struct colors_struct
{
uint32_t fgcolor, bgcolor;
uint32_t colors[256];
};
#define COLORS_LRU_SIZE 16
static struct colors_struct *colors_lru_array[COLORS_LRU_SIZE] = { NULL };
static uint32_t *lookup_colors(uint32_t fgcolor, uint32_t bgcolor)
{
struct colors_struct *cs;
int i = 0, j;
for (i = 0; i < COLORS_LRU_SIZE; i++)
if (colors_lru_array[i] && colors_lru_array[i]->fgcolor == fgcolor && colors_lru_array[i]->bgcolor == bgcolor) {
cs = colors_lru_array[i];
for (j = i; j > 0; j--)
colors_lru_array[j] = colors_lru_array[j - 1];
colors_lru_array[0] = cs;
return cs->colors;
}
i--;
cs = colors_lru_array[i];
if (!cs)
cs = (struct colors_struct *) calloc(1, sizeof(struct colors_struct));
for (j = i; j > 0; j--)
colors_lru_array[j] = colors_lru_array[j - 1];
cs->fgcolor = fgcolor;
cs->bgcolor = bgcolor;
int ro = var_screeninfo.red.offset;
int go = var_screeninfo.green.offset;
int bo = var_screeninfo.blue.offset;
int to = var_screeninfo.transp.offset;
int rm = (1 << var_screeninfo.red.length) - 1;
int gm = (1 << var_screeninfo.green.length) - 1;
int bm = (1 << var_screeninfo.blue.length) - 1;
int tm = (1 << var_screeninfo.transp.length) - 1;
int fgr = ((int)fgcolor >> ro) & rm;
int fgg = ((int)fgcolor >> go) & gm;
int fgb = ((int)fgcolor >> bo) & bm;
int fgt = ((int)fgcolor >> to) & tm;
int deltar = (((int)bgcolor >> ro) & rm) - fgr;
int deltag = (((int)bgcolor >> go) & gm) - fgg;
int deltab = (((int)bgcolor >> bo) & bm) - fgb;
int deltat = (((int)bgcolor >> to) & tm) - fgt;
for (i = 0; i < 256; i++)
cs->colors[255 - i] =
(((fgr + deltar * i / 255) & rm) << ro) |
(((fgg + deltag * i / 255) & gm) << go) |
(((fgb + deltab * i / 255) & bm) << bo) |
(((fgt + deltat * i / 255) & tm) << to);
colors_lru_array[0] = cs;
return cs->colors;
}
int RenderChar(FT_ULong currentchar, int _sx, int _sy, int _ex, int color)
{
int row, pitch;
FT_UInt glyphindex;
FT_Vector kerning;
FT_Error err;
if (currentchar == '\r') // display \r in windows edited files
{
if(color != -1)
{
if (_sx + OFFSET_MED < _ex)
RenderBox(_sx, _sy - OFFSET_MED - OFFSET_SMALL, _sx + OFFSET_MED, _sy - OFFSET_SMALL, GRID, color);
else
return -1;
}
return OFFSET_MED;
}
if (currentchar == '\t')
{
/* simulate horizontal TAB */
return scale2res(15);
}
int _d = 0;
if (1)
{
FT_UInt _i = FT_Get_Char_Index(face, 'g');
FTC_SBit _g;
if((err = FTC_SBitCache_Lookup(cache, &desc, _i, &_g, NULL)))
{
printf("%s <FTC_SBitCache_Lookup for Char \"%c\" failed with Errorcode 0x%.2X>\n", __plugin__, 'g', error);
return 0;
}
_d = _g->height - _g->top;
_d += 1;
}
//load char
if(!(glyphindex = FT_Get_Char_Index(face, currentchar)))
{
printf("%s <FT_Get_Char_Index for Char \"%c\" failed\n", __plugin__, (int)currentchar);
return 0;
}
if((err = FTC_SBitCache_Lookup(cache, &desc, glyphindex, &sbit, NULL)))
{
printf("%s <FTC_SBitCache_Lookup for Char \"%c\" failed with Errorcode 0x%.2X>\n", __plugin__, (int)currentchar, error);
return 0;
}
if(use_kerning)
{
FT_Get_Kerning(face, prev_glyphindex, glyphindex, ft_kerning_default, &kerning);
prev_glyphindex = glyphindex;
kerning.x >>= 6;
} else
kerning.x = 0;
//render char
if(color != -1) /* don't render char, return charwidth only */
{
if (_sx + sbit->xadvance >= _ex)
return -1; /* limit to maxwidth */
uint32_t fgcolor;
uint32_t bgcolor = *(lbb + (sy + _sy - _d - 1) * swidth + (sx + _sx + OFFSET_MIN + sbit->left));
if ( color == -2) /* flash */
fgcolor = bgcolor;
else
fgcolor = bgra[color];
uint32_t *colors = lookup_colors(fgcolor, bgcolor);
uint32_t *p = lbb + (sx + _sx + sbit->left + kerning.x) + swidth * (sy + _sy - sbit->top - _d);
uint32_t *r = p + (_ex - _sx); /* end of usable box */
for(row = 0; row < sbit->height; row++)
{
uint32_t *q = p;
uint8_t *s = sbit->buffer + row * sbit->pitch;
for(pitch = 0; pitch < sbit->width; pitch++)
{
if (*s)
*q = colors[*s];
q++; s++;
if (q > r) /* we are past _ex */
break;
}
p += swidth;
r += swidth;
}
}
//return charwidth
return sbit->xadvance + kerning.x;
}
/******************************************************************************
* GetStringLen
******************************************************************************/
int GetStringLen(int _sx, char *string, size_t size)
{
int i, stringlen = 0;
//reset kerning
prev_glyphindex = 0;
//calc len
if(size)
desc.width = desc.height = size;
while(*string) {
switch(*string) {
case '~':
string++;
if(*string=='t')
stringlen=desc.width+TABULATOR*((int)(stringlen/TABULATOR)+1);
else if(*string=='T' && sscanf(string+1,"%4d",&i)==1) {
string+=5;
stringlen=scale2res(i)-_sx;
}
else if(*string=='l' ||
*string=='c' ||
*string=='r' ||
*string=='R' ||
*string=='G' ||
*string=='B' ||
*string=='Y' ||
*string=='S' ||
*string=='F' ||
*string=='C') {
string+=1;
}
break;
default:
stringlen += RenderChar(UTF8ToUnicode(&string, 1), -1, -1, -1, -1);
break;
}
}
return stringlen;
}
void CatchLF(char *text)
{
char *src, *dst;
for (src = dst = text; *src != '\0'; src++)
{
*dst = *src;
if (*dst != 0x0A) dst++;
}
*dst = '\0';
}
void CatchTabs(char *text)
{
int i;
char *tptr=text;
while((tptr=strstr(tptr,"~T"))!=NULL)
{
*(++tptr)='t';
for(i=0; i<4; i++)
{
if(*(++tptr))
{
*tptr=' ';
}
}
}
}
/******************************************************************************
* RenderString
******************************************************************************/
int RenderString(char *string, int _sx, int _sy, int maxwidth, int layout, int size, int color)
{
int stringlen, _ex, charwidth, i, col;
char rstr[BUFSIZE]={0}, *rptr=rstr;
int varcolor=color, butcolor=color;
//set size
strcpy(rstr,string);
desc.width = desc.height = size;
TABULATOR=3*size;
//set alignment
stringlen = GetStringLen(_sx, rstr, size);
if(layout != LEFT)
{
switch(layout)
{
case CENTER: if(stringlen < maxwidth) _sx += (maxwidth - stringlen)/2;
break;
case RIGHT: if(stringlen < maxwidth) _sx += maxwidth - stringlen;
}
}
//reset kerning
prev_glyphindex = 0;
//render string
_ex = _sx + maxwidth;
while(*rptr)
{
if(*rptr=='~')
{
++rptr;
switch(*rptr)
{
case 'R': varcolor=RED; break;
case 'G': varcolor=GREEN; break;
case 'Y': varcolor=YELLOW; break;
case 'B': varcolor=BLUE1; break;
case 'S': varcolor=color; break;
case 'F':
if(butcolor == CMCST)
col = CMCIT;
else if(butcolor == CMCIT)
col = CMCST;
else
col = -2;
varcolor = flash ? col : varcolor;
break;
case 't':
_sx=TABULATOR*((int)(_sx/TABULATOR)+1);
break;
case 'T':
if(sscanf(rptr+1,"%4d",&i)==1)
{
rptr+=4;
_sx=scale2res(i);
}
break;
}
rptr++;
}
else
{
if((charwidth = RenderChar(UTF8ToUnicode(&rptr, 1), _sx, _sy, _ex, varcolor)) == -1) return _sx; /* string > maxwidth */
_sx += charwidth;
}
}
return stringlen;
}