-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathufontlib.c
581 lines (496 loc) · 16.9 KB
/
ufontlib.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "ufontlib.h"
#if ESP_IDF_VERSION < (4, 0, 0) || ARDUINO_ARCH_ESP32
#include "rom/miniz.h"
#else
#include "esp32/rom/miniz.h"
#endif
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
uint8_t mask; /* char data will be bitwise AND with this */
uint8_t lead; /* start bytes of current char in utf-8 encoded character */
uint32_t beg; /* beginning of codepoint range */
uint32_t end; /* end of codepoint range */
int bits_stored; /* the number of bits from the codepoint that fits in char */
} utf_t;
/*
* UTF-8 decode inspired from rosetta code
* https://rosettacode.org/wiki/UTF-8_encode_and_decode#C
*/
static utf_t *utf[] = {
/* mask lead beg end bits */
[0] = &(utf_t){ 0b00111111, 0b10000000, 0, 0, 6 },
[1] = &(utf_t){ 0b01111111, 0b00000000, 0000, 0177, 7 },
[2] = &(utf_t){ 0b00011111, 0b11000000, 0200, 03777, 5 },
[3] = &(utf_t){ 0b00001111, 0b11100000, 04000, 0177777, 4 },
[4] = &(utf_t){ 0b00000111, 0b11110000, 0200000, 04177777, 3 },
&(utf_t){ 0 },
};
/**
* static decompressor object for compressed fonts.
*/
static tinfl_decompressor decomp;
static inline int min(int x, int y) { return x < y ? x : y; }
static inline int max(int x, int y) { return x > y ? x : y; }
static int utf8_len(const uint8_t ch)
{
int len = 0;
for (utf_t **u = utf; (*u)->mask; ++u) {
if ((ch & ~(*u)->mask) == (*u)->lead) {
break;
}
++len;
}
if (len > 4) { /* Malformed leading byte */
assert("invalid unicode.");
}
return len;
}
static uint32_t next_cp(const uint8_t **string)
{
if (**string == 0) {
return 0;
}
int bytes = utf8_len(**string);
const uint8_t *chr = *string;
*string += bytes;
int shift = utf[0]->bits_stored * (bytes - 1);
uint32_t codep = (*chr++ & utf[bytes]->mask) << shift;
for (int i = 1; i < bytes; ++i, ++chr) {
shift -= utf[0]->bits_stored;
codep |= ((const uint8_t) *chr & utf[0]->mask) << shift;
}
return codep;
}
UFontFontProperties ufont_font_properties_default()
{
UFontFontProperties props = {
.fg_color = 0, .bg_color = 15, .fallback_glyph = 0, .flags = UFONT_DRAW_ALIGN_LEFT
};
return props;
}
const UFontGlyph *ufont_get_glyph(const UFontData *font, uint32_t code_point)
{
const UFontUnicodeInterval *intervals = font->intervals;
for (unsigned int i = 0; i < font->interval_count; i++) {
const UFontUnicodeInterval *interval = &intervals[i];
if (code_point >= interval->first && code_point <= interval->last) {
return &font->glyph[interval->offset + (code_point - interval->first)];
}
if (code_point < interval->first) {
return NULL;
}
}
return NULL;
}
static int do_uncompress(uint8_t *dest, size_t uncompressed_size, const uint8_t *source, size_t source_size)
{
if (uncompressed_size == 0 || dest == NULL || source_size == 0 || source == NULL) {
return -1;
}
tinfl_init(&decomp);
// we know everything will fit into the buffer.
tinfl_status decomp_status = tinfl_decompress(&decomp, source, &source_size, dest, dest, &uncompressed_size, TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
if (decomp_status != TINFL_STATUS_DONE) {
return decomp_status;
}
return 0;
}
/*!
@brief Draw a single character to a pre-allocated buffer.
*/
static enum UFontDrawError draw_char(const UFontData *font, void *buffer,
int *cursor_x, int cursor_y, uint32_t cp,
const UFontFontProperties *props)
{
assert(props != NULL);
const UFontGlyph *glyph = ufont_get_glyph(font, cp);
if (!glyph) {
glyph = ufont_get_glyph(font, props->fallback_glyph);
}
if (!glyph) {
return UFONT_DRAW_GLYPH_FALLBACK_FAILED;
}
uint32_t offset = glyph->data_offset;
uint16_t width = glyph->width, height = glyph->height;
int left = glyph->left;
int byte_width = (width / 2 + width % 2);
unsigned long bitmap_size = byte_width * height;
const uint8_t *bitmap = NULL;
if (font->compressed) {
uint8_t *tmp_bitmap = (uint8_t *) malloc(bitmap_size);
if (tmp_bitmap == NULL && bitmap_size) {
fprintf(stderr, "malloc failed.");
return UFONT_DRAW_FAILED_ALLOC;
}
do_uncompress(tmp_bitmap, bitmap_size, &font->bitmap[offset],
glyph->compressed_size);
bitmap = tmp_bitmap;
} else {
bitmap = &font->bitmap[offset];
}
uint8_t color_lut[16];
for (int c = 0; c < 16; c++) {
int color_difference = (int) props->fg_color - (int) props->bg_color;
color_lut[c] = max(0, min(15, props->bg_color + c * color_difference / 15));
}
bool background_needed = props->flags & UFONT_DRAW_BACKGROUND;
for (int y = 0; y < height; y++) {
int yy = cursor_y - glyph->top + y;
int start_pos = *cursor_x + left;
bool byte_complete = start_pos % 2;
int x = max(0, -start_pos);
int max_x = start_pos + width;
uint8_t color;
for (int xx = start_pos; xx < max_x; xx++) {
uint8_t bm = bitmap[y * byte_width + x / 2];
if ((x & 1) == 0) {
bm = bm & 0xF;
} else {
bm = bm >> 4;
}
if (background_needed || bm) {
color = color_lut[bm] << 4;
ufont_draw_pixel(xx, yy, color, buffer);
}
byte_complete = !byte_complete;
x++;
}
}
if (font->compressed) {
free((uint8_t *) bitmap);
}
*cursor_x += glyph->advance_x;
return UFONT_DRAW_SUCCESS;
}
/*!
* @brief Calculate the bounds of a character when drawn at (x, y), move the
* cursor (*x) forward, adjust the given bounds.
*/
static void get_char_bounds(const UFontData *font, uint32_t cp, int *x, int *y,
int *minx, int *miny, int *maxx, int *maxy,
const UFontFontProperties *props)
{
assert(props != NULL);
const UFontGlyph *glyph = ufont_get_glyph(font, cp);
if (!glyph) {
glyph = ufont_get_glyph(font, props->fallback_glyph);
}
if (!glyph) {
return;
}
int x1 = *x + glyph->left, y1 = *y + glyph->top - glyph->height,
x2 = x1 + glyph->width, y2 = y1 + glyph->height;
// background needs to be taken into account
if (props->flags & UFONT_DRAW_BACKGROUND) {
*minx = min(*x, min(*minx, x1));
*maxx = max(max(*x + glyph->advance_x, x2), *maxx);
*miny = min(*y + font->descender, min(*miny, y1));
*maxy = max(*y + font->ascender, max(*maxy, y2));
} else {
if (x1 < *minx)
*minx = x1;
if (y1 < *miny)
*miny = y1;
if (x2 > *maxx)
*maxx = x2;
if (y2 > *maxy)
*maxy = y2;
}
*x += glyph->advance_x;
}
UFontRect ufont_get_string_rect(const UFontData *font, const char *string,
int x, int y, int margin, const UFontFontProperties *properties)
{
assert(properties != NULL);
UFontFontProperties props = *properties;
props.flags |= UFONT_DRAW_BACKGROUND;
UFontRect temp = { .x = x, .y = y, .width = 0, .height = 0 };
if (*string == '\0')
return temp;
int minx = 100000, miny = 100000, maxx = -1, maxy = -1;
int temp_x = x;
int temp_y = y + font->ascender;
// Go through each line and get it's co-ordinates
uint32_t c;
while ((c = next_cp((const uint8_t **) &string))) {
if (c == 0x000A) // newline
{
temp_x = x;
temp_y += font->advance_y;
} else
get_char_bounds(font, c, &temp_x, &temp_y, &minx, &miny, &maxx, &maxy, &props);
}
temp.width = maxx - x + (margin * 2);
temp.height = maxy - miny + (margin * 2);
return temp;
}
void ufont_get_text_bounds(const UFontData *font, const char *string,
const int *x, const int *y,
int *x1, int *y1, int *w, int *h,
const UFontFontProperties *properties)
{
// FIXME: Does not respect alignment!
assert(properties != NULL);
UFontFontProperties props = *properties;
if (*string == '\0') {
*w = 0;
*h = 0;
*y1 = *y;
*x1 = *x;
return;
}
int minx = 100000, miny = 100000, maxx = -1, maxy = -1;
int original_x = *x;
int temp_x = *x;
int temp_y = *y;
uint32_t c;
while ((c = next_cp((const uint8_t **) &string))) {
get_char_bounds(font, c, &temp_x, &temp_y, &minx, &miny, &maxx, &maxy, &props);
}
*x1 = min(original_x, minx);
*w = maxx - *x1;
*y1 = miny;
*h = maxy - miny;
}
static enum UFontDrawError ufont_write_line(
const UFontData *font, const char *string, int *cursor_x,
int *cursor_y, void *framebuffer,
const UFontFontProperties *properties)
{
assert(framebuffer != NULL);
if (*string == '\0') {
return UFONT_DRAW_SUCCESS;
}
assert(properties != NULL);
UFontFontProperties props = *properties;
enum UFontFontFlags alignment_mask = UFONT_DRAW_ALIGN_LEFT | UFONT_DRAW_ALIGN_RIGHT | UFONT_DRAW_ALIGN_CENTER;
enum UFontFontFlags alignment = props.flags & alignment_mask;
// alignments are mutually exclusive!
if ((alignment & (alignment - 1)) != 0) {
return UFONT_DRAW_INVALID_FONT_FLAGS;
}
int x1 = 0, y1 = 0, w = 0, h = 0;
int tmp_cur_x = *cursor_x;
int tmp_cur_y = *cursor_y;
ufont_get_text_bounds(font, string, &tmp_cur_x, &tmp_cur_y, &x1, &y1, &w, &h, &props);
// no printable characters
if (w < 0 || h < 0) {
return UFONT_DRAW_NO_DRAWABLE_CHARACTERS;
}
int local_cursor_x = *cursor_x;
int local_cursor_y = *cursor_y;
uint32_t c;
int cursor_x_init = local_cursor_x;
int cursor_y_init = local_cursor_y;
switch (alignment) {
case UFONT_DRAW_ALIGN_LEFT: {
break;
}
case UFONT_DRAW_ALIGN_CENTER: {
local_cursor_x -= w / 2;
break;
}
case UFONT_DRAW_ALIGN_RIGHT: {
local_cursor_x -= w;
break;
}
default:
break;
}
//FIXME: needed from ufont_draw_hline
//uint8_t bg = props.bg_color;
if (props.flags & UFONT_DRAW_BACKGROUND) {
for (int l = local_cursor_y - font->ascender;
l < local_cursor_y - font->descender; l++) {
//FIXME: following function is not implemented
//ufont_draw_hline(local_cursor_x, l, w, bg << 4, framebuffer);
}
}
enum UFontDrawError err = UFONT_DRAW_SUCCESS;
while ((c = next_cp((const uint8_t **) &string))) {
err |= draw_char(font, framebuffer, &local_cursor_x, local_cursor_y, c, &props);
}
*cursor_x += local_cursor_x - cursor_x_init;
*cursor_y += local_cursor_y - cursor_y_init;
return err;
}
enum UFontDrawError ufont_write_default(const UFontData *font, const char *string, int *cursor_x,
int *cursor_y, void *framebuffer)
{
const UFontFontProperties props = ufont_font_properties_default();
return ufont_write_string(font, string, cursor_x, cursor_y, framebuffer, &props);
}
enum UFontDrawError ufont_write_string(
const UFontData *font, const char *string, int *cursor_x,
int *cursor_y, void *framebuffer,
const UFontFontProperties *properties)
{
char *token, *newstring, *tofree;
if (string == NULL) {
fprintf(stderr, "cannot draw a NULL string!");
return UFONT_DRAW_STRING_INVALID;
}
tofree = newstring = strdup(string);
if (newstring == NULL) {
fprintf(stderr, "cannot allocate string copy!");
return UFONT_DRAW_FAILED_ALLOC;
}
enum UFontDrawError err = UFONT_DRAW_SUCCESS;
// taken from the strsep manpage
int line_start = *cursor_x;
while ((token = strsep(&newstring, "\n")) != NULL) {
*cursor_x = line_start;
err |= ufont_write_line(font, token, cursor_x, cursor_y, framebuffer, properties);
*cursor_y += font->advance_y;
}
free(tofree);
return err;
}
UFontData *ufont_load_font(const void *ufont, const void *glyph, const void *intervals, const void *bitmap)
{
struct __attribute__((__packed__))
{
uint32_t interval_count;
uint8_t compressed;
uint16_t advance_y;
uint16_t ascender;
uint16_t descender;
} serialized_ufont;
memcpy(&serialized_ufont, ufont, sizeof(serialized_ufont));
UFontData *loaded_font = malloc(sizeof(UFontData));
loaded_font->bitmap = bitmap;
loaded_font->glyph = glyph;
loaded_font->intervals = intervals;
loaded_font->interval_count = serialized_ufont.interval_count;
loaded_font->compressed = serialized_ufont.compressed;
loaded_font->advance_y = serialized_ufont.advance_y;
loaded_font->ascender = serialized_ufont.ascender;
loaded_font->descender = serialized_ufont.descender;
return loaded_font;
}
#define GET_LIST_ENTRY(list_item, type, list_head_member) \
((type *) (((char *) (list_item)) - ((unsigned long) &((type *) 0)->list_head_member)))
#define LIST_FOR_EACH(item, head) \
for (item = (head)->next; item != (head); item = item->next)
#define MUTABLE_LIST_FOR_EACH(item, tmp, head) \
for (item = (head)->next, tmp = item->next; item != (head); item = tmp, tmp = item->next)
struct UFListHead;
struct UFListHead
{
struct UFListHead *next;
struct UFListHead *prev;
};
static inline void uflist_insert(struct UFListHead *new_item, struct UFListHead *prev_head, struct UFListHead *next_head)
{
new_item->prev = prev_head;
new_item->next = next_head;
next_head->prev = new_item;
prev_head->next = new_item;
}
static inline void uflist_append(struct UFListHead *head, struct UFListHead *new_item)
{
uflist_insert(new_item, head->prev, head);
}
static inline void uflist_remove(struct UFListHead *remove_item)
{
remove_item->prev->next = remove_item->next;
remove_item->next->prev = remove_item->prev;
}
static inline void uflist_init(struct UFListHead *list_item)
{
list_item->prev = list_item;
list_item->next = list_item;
}
typedef struct
{
struct UFListHead list_head;
const char *handle;
UFontData *font;
} UFont;
struct UFontManager
{
struct UFListHead font_list;
};
UFontManager *ufont_manager_new()
{
UFontManager *ufont_manager = malloc(sizeof(UFontManager));
uflist_init(&ufont_manager->font_list);
return ufont_manager;
}
void ufont_manager_register(UFontManager *ufont_manager, const char *handle, UFontData *font)
{
UFont *ufont = malloc(sizeof(UFont));
ufont->handle = strdup(handle);
ufont->font = font;
uflist_append(&ufont_manager->font_list, &ufont->list_head);
}
UFontData *ufont_manager_find_by_handle(UFontManager *ufont_manager, const char *handle)
{
struct UFListHead *item;
LIST_FOR_EACH (item, &ufont_manager->font_list) {
UFont *ufont = GET_LIST_ENTRY(item, UFont, list_head);
if (!strcmp(handle, ufont->handle)) {
return ufont->font;
}
}
return NULL;
}
#ifdef __ORDER_LITTLE_ENDIAN__
#ifdef __GNUC__
#define UF_ENDIAN_SWAP_32(value) __builtin_bswap32(value)
#else
#define UF_ENDIAN_SWAP_32(value) ((((value) & 0xFF) << 24) | (((value) & 0xFF00) << 8) | \
(((value) & 0xFF0000) >> 8) | (((value) & 0xFF000000) >> 24))
#endif
#else
#define UF_ENDIAN_SWAP_32(value) (value)
#endif
struct UFIFFRecord
{
const char name[4];
uint32_t size;
};
static uint32_t ufont_iff_align(uint32_t size)
{
return ((size + 4 - 1) >> 2) << 2;
}
static int ufont_iff_is_valid_ufl(const void *iff)
{
return memcmp(iff, "UFL0", 4) == 0;
}
UFontData *ufont_parse(const void *iff_binary, int buf_size)
{
if (ufont_iff_is_valid_ufl(iff_binary)) {
return NULL;
}
const uint8_t *data = iff_binary;
int current_pos = 12;
uint32_t iff_size = UF_ENDIAN_SWAP_32(*(((uint32_t *) (data + 4))));
int file_size = iff_size;
if (buf_size < file_size) {
fprintf(stderr, "warning: buffer holding IFF %i is smaller than IFF size: %i", buf_size, file_size);
return NULL;
}
const void *ufont = NULL;
const void *glyph = NULL;
const void *intervals = NULL;
const void *bitmap = NULL;
do {
struct UFIFFRecord *current_record = (struct UFIFFRecord *) (data + current_pos);
if (!memcmp(current_record->name, "uFH0", 4)) {
ufont = data + current_pos + sizeof(struct UFIFFRecord);
} else if (!memcmp(current_record->name, "uFP0", 4)) {
glyph = data + current_pos + sizeof(struct UFIFFRecord);
} else if (!memcmp(current_record->name, "uFI0", 4)) {
intervals = data + current_pos + sizeof(struct UFIFFRecord);
} else if (!memcmp(current_record->name, "uFB0", 4)) {
bitmap = data + current_pos + sizeof(struct UFIFFRecord);
}
current_pos += ufont_iff_align(UF_ENDIAN_SWAP_32(current_record->size) + 8);
} while (current_pos < file_size);
return ufont_load_font(ufont, glyph, intervals, bitmap);
}