blob: db70236a4afad4a1d34b06380b191d01a291db12 [file] [log] [blame]
Vojtech Bocek76ee9032014-09-07 15:01:56 +02001#include <stdbool.h>
2#include <stdlib.h>
3#include <unistd.h>
4
5#include <errno.h>
6#include <stdio.h>
7
8#include "minui.h"
9
10#include <cutils/hashmap.h>
11#include <ft2build.h>
12#include FT_FREETYPE_H
13#include FT_GLYPH_H
14
15#include <pixelflinger/pixelflinger.h>
16#include <pthread.h>
17
18#define STRING_CACHE_MAX_ENTRIES 400
19#define STRING_CACHE_TRUNCATE_ENTRIES 150
20
21typedef struct
22{
23 int size;
24 int dpi;
25 char *path;
26} TrueTypeFontKey;
27
28typedef struct
29{
30 int type;
31 int refcount;
32 int size;
33 int dpi;
34 int max_height;
35 int base;
36 FT_Face face;
37 Hashmap *glyph_cache;
38 Hashmap *string_cache;
39 struct StringCacheEntry *string_cache_head;
40 struct StringCacheEntry *string_cache_tail;
41 pthread_mutex_t mutex;
42 TrueTypeFontKey *key;
43} TrueTypeFont;
44
45typedef struct
46{
47 FT_BBox bbox;
48 FT_BitmapGlyph glyph;
49} TrueTypeCacheEntry;
50
51typedef struct
52{
53 char *text;
54 int max_width;
55} StringCacheKey;
56
57struct StringCacheEntry
58{
59 GGLSurface surface;
xiaolue738da52015-02-22 20:49:35 +080060 int rendered_bytes; // number of bytes from C string rendered, not number of UTF8 characters!
Vojtech Bocek76ee9032014-09-07 15:01:56 +020061 StringCacheKey *key;
62 struct StringCacheEntry *prev;
63 struct StringCacheEntry *next;
64};
65
66typedef struct StringCacheEntry StringCacheEntry;
67
xiaolue738da52015-02-22 20:49:35 +080068typedef struct
Vojtech Bocek76ee9032014-09-07 15:01:56 +020069{
70 FT_Library ft_library;
71 Hashmap *fonts;
72 pthread_mutex_t mutex;
73} FontData;
74
75static FontData font_data = {
76 .ft_library = NULL,
77 .fonts = NULL,
78 .mutex = PTHREAD_MUTEX_INITIALIZER,
79};
80
81#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
82#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
83
84// 32bit FNV-1a hash algorithm
85// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
86static const uint32_t FNV_prime = 16777619U;
87static const uint32_t offset_basis = 2166136261U;
88
89static uint32_t fnv_hash(void *data, uint32_t len)
90{
91 uint8_t *d8 = data;
92 uint32_t *d32 = data;
93 uint32_t i, max;
94 uint32_t hash = offset_basis;
95
96 max = len/4;
97
98 // 32 bit data
99 for(i = 0; i < max; ++i)
100 {
101 hash ^= *d32++;
102 hash *= FNV_prime;
103 }
104
105 // last bits
106 for(i *= 4; i < len; ++i)
107 {
108 hash ^= (uint32_t) d8[i];
109 hash *= FNV_prime;
110 }
111 return hash;
112}
113
114static inline uint32_t fnv_hash_add(uint32_t cur_hash, uint32_t word)
115{
116 cur_hash ^= word;
117 cur_hash *= FNV_prime;
118 return cur_hash;
119}
120
soyud5a7c0e2014-11-28 14:33:53 +0800121int utf8_to_unicode(unsigned char* pIn, unsigned int *pOut)
122{
123 if(pIn == NULL || pOut == NULL)
124 return 0;
125
126 int utf_bytes = 1;
127 unsigned int unicode = 0;
128 unsigned char tmp;
129 tmp = *pIn++;
130 if (tmp < 0x80)
131 {
132 *pOut = tmp;
133 }
134 else
135 {
136 unsigned int high_bit_mask = 0x3F;
137 unsigned int high_bit_shift = 0;
138 int total_bits = 0;
139 while((tmp & 0xC0) == 0xC0)
140 {
141 utf_bytes ++;
142 if(utf_bytes > 6) return 0;
143 tmp = 0xFF & (tmp << 1);
144 total_bits += 6;
145 high_bit_mask >>= 1;
146 high_bit_shift++;
147 unicode <<= 6;
148 unicode |= (*pIn++) & 0x3F;
149 }
150 unicode |= ((tmp >> high_bit_shift) & high_bit_mask) << total_bits;
151 *pOut = unicode;
152 }
153
154 return utf_bytes;
155}
156
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200157static bool gr_ttf_string_cache_equals(void *keyA, void *keyB)
158{
159 StringCacheKey *a = keyA;
160 StringCacheKey *b = keyB;
161 return a->max_width == b->max_width && strcmp(a->text, b->text) == 0;
162}
163
164static int gr_ttf_string_cache_hash(void *key)
165{
166 StringCacheKey *k = key;
167 return fnv_hash(k->text, strlen(k->text));
168}
169
170static bool gr_ttf_font_cache_equals(void *keyA, void *keyB)
171{
172 TrueTypeFontKey *a = keyA;
173 TrueTypeFontKey *b = keyB;
174 return (a->size == b->size) && (a->dpi == b->dpi) && !strcmp(a->path, b->path);
175}
176
177static int gr_ttf_font_cache_hash(void *key)
178{
179 TrueTypeFontKey *k = key;
180
181 uint32_t hash = fnv_hash(k->path, strlen(k->path));
182 hash = fnv_hash_add(hash, k->size);
183 hash = fnv_hash_add(hash, k->dpi);
184 return hash;
185}
186
187void *gr_ttf_loadFont(const char *filename, int size, int dpi)
188{
189 int error;
190 TrueTypeFont *res = NULL;
191
192 pthread_mutex_lock(&font_data.mutex);
193
194 if(font_data.fonts)
195 {
196 TrueTypeFontKey k = {
197 .size = size,
198 .dpi = dpi,
199 .path = (char*)filename
200 };
201
202 res = hashmapGet(font_data.fonts, &k);
203 if(res)
204 {
205 ++res->refcount;
206 goto exit;
207 }
208 }
209
210 if(!font_data.ft_library)
211 {
212 error = FT_Init_FreeType(&font_data.ft_library);
213 if(error)
214 {
215 fprintf(stderr, "Failed to init libfreetype! %d\n", error);
216 goto exit;
217 }
218 }
219
220 FT_Face face;
221 error = FT_New_Face(font_data.ft_library, filename, 0, &face);
222 if(error)
223 {
224 fprintf(stderr, "Failed to load truetype face %s: %d\n", filename, error);
225 goto exit;
226 }
227
228 error = FT_Set_Char_Size(face, 0, size*16, dpi, dpi);
229 if(error)
230 {
231 fprintf(stderr, "Failed to set truetype face size to %d, dpi %d: %d\n", size, dpi, error);
232 FT_Done_Face(face);
233 goto exit;
234 }
235
236 res = malloc(sizeof(TrueTypeFont));
237 memset(res, 0, sizeof(TrueTypeFont));
238 res->type = FONT_TYPE_TTF;
239 res->size = size;
240 res->dpi = dpi;
241 res->face = face;
242 res->max_height = -1;
243 res->base = -1;
244 res->refcount = 1;
245 res->glyph_cache = hashmapCreate(32, hashmapIntHash, hashmapIntEquals);
246 res->string_cache = hashmapCreate(128, gr_ttf_string_cache_hash, gr_ttf_string_cache_equals);
247 pthread_mutex_init(&res->mutex, 0);
248
249 if(!font_data.fonts)
250 font_data.fonts = hashmapCreate(4, gr_ttf_font_cache_hash, gr_ttf_font_cache_equals);
251
252 TrueTypeFontKey *key = malloc(sizeof(TrueTypeFontKey));
253 memset(key, 0, sizeof(TrueTypeFontKey));
254 key->path = strdup(filename);
255 key->size = size;
256 key->dpi = dpi;
257
258 res->key = key;
259
260 hashmapPut(font_data.fonts, key, res);
261
262exit:
263 pthread_mutex_unlock(&font_data.mutex);
264 return res;
265}
266
267static bool gr_ttf_freeFontCache(void *key, void *value, void *context)
268{
269 TrueTypeCacheEntry *e = value;
270 FT_Done_Glyph((FT_Glyph)e->glyph);
271 free(e);
272 free(key);
273 return true;
274}
275
276static bool gr_ttf_freeStringCache(void *key, void *value, void *context)
277{
278 StringCacheKey *k = key;
279 free(k->text);
280 free(k);
281
282 StringCacheEntry *e = value;
283 free(e->surface.data);
284 free(e);
285 return true;
286}
287
288void gr_ttf_freeFont(void *font)
289{
290 pthread_mutex_lock(&font_data.mutex);
291
292 TrueTypeFont *d = font;
293
294 if(--d->refcount == 0)
295 {
296 hashmapRemove(font_data.fonts, d->key);
297
298 if(hashmapSize(font_data.fonts) == 0)
299 {
300 hashmapFree(font_data.fonts);
301 font_data.fonts = NULL;
302 }
303
304 free(d->key->path);
305 free(d->key);
306
307 FT_Done_Face(d->face);
308 hashmapForEach(d->string_cache, gr_ttf_freeStringCache, NULL);
309 hashmapFree(d->string_cache);
310 hashmapForEach(d->glyph_cache, gr_ttf_freeFontCache, NULL);
311 hashmapFree(d->glyph_cache);
312 pthread_mutex_destroy(&d->mutex);
313 free(d);
314 }
315
316 pthread_mutex_unlock(&font_data.mutex);
317}
318
319static TrueTypeCacheEntry *gr_ttf_glyph_cache_peek(TrueTypeFont *font, int char_index)
320{
321 return hashmapGet(font->glyph_cache, &char_index);
322}
323
324static TrueTypeCacheEntry *gr_ttf_glyph_cache_get(TrueTypeFont *font, int char_index)
325{
326 TrueTypeCacheEntry *res = hashmapGet(font->glyph_cache, &char_index);
327 if(!res)
328 {
329 int error = FT_Load_Glyph(font->face, char_index, FT_LOAD_RENDER);
330 if(error)
331 {
332 fprintf(stderr, "Failed to load glyph idx %d: %d\n", char_index, error);
333 return NULL;
334 }
335
336 FT_BitmapGlyph glyph;
337 error = FT_Get_Glyph(font->face->glyph, (FT_Glyph*)&glyph);
338 if(error)
339 {
340 fprintf(stderr, "Failed to copy glyph %d: %d\n", char_index, error);
341 return NULL;
342 }
343
344 res = malloc(sizeof(TrueTypeCacheEntry));
345 memset(res, 0, sizeof(TrueTypeCacheEntry));
346 res->glyph = glyph;
347 FT_Glyph_Get_CBox((FT_Glyph)glyph, FT_GLYPH_BBOX_PIXELS, &res->bbox);
348
349 int *key = malloc(sizeof(int));
350 *key = char_index;
351
352 hashmapPut(font->glyph_cache, key, res);
353 }
354
355 return res;
356}
357
358static int gr_ttf_copy_glyph_to_surface(GGLSurface *dest, FT_BitmapGlyph glyph, int offX, int offY, int base)
359{
360 int y;
361 uint8_t *src_itr = glyph->bitmap.buffer;
362 uint8_t *dest_itr = dest->data;
363
364 if(glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
365 {
366 fprintf(stderr, "Unsupported pixel mode in FT_BitmapGlyph %d\n", glyph->bitmap.pixel_mode);
367 return -1;
368 }
369
370 dest_itr += (offY + base - glyph->top)*dest->stride + (offX + glyph->left);
371
372 for(y = 0; y < glyph->bitmap.rows; ++y)
373 {
374 memcpy(dest_itr, src_itr, glyph->bitmap.width);
375 src_itr += glyph->bitmap.pitch;
376 dest_itr += dest->stride;
377 }
378 return 0;
379}
380
xiaolue738da52015-02-22 20:49:35 +0800381// returns number of bytes from const char *text rendered to fit max_width, not number of UTF8 characters!
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200382static int gr_ttf_render_text(TrueTypeFont *font, GGLSurface *surface, const char *text, int max_width)
383{
384 TrueTypeFont *f = font;
385 TrueTypeCacheEntry *ent;
xiaolue738da52015-02-22 20:49:35 +0800386 int bytes_rendered = 0, total_w = 0;
soyud5a7c0e2014-11-28 14:33:53 +0800387 int utf_bytes = 0;
388 unsigned int unicode = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200389 int i, x, diff, char_idx, prev_idx = 0;
390 int height, base;
391 FT_Vector delta;
392 uint8_t *data = NULL;
393 const char *text_itr = text;
soyud5a7c0e2014-11-28 14:33:53 +0800394 int *char_idxs;
xiaolue738da52015-02-22 20:49:35 +0800395 int char_idxs_len = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200396
xiaolue738da52015-02-22 20:49:35 +0800397 char_idxs = malloc(strlen(text) * sizeof(int));
398
soyud5a7c0e2014-11-28 14:33:53 +0800399 while(*text_itr)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200400 {
soyud5a7c0e2014-11-28 14:33:53 +0800401 utf_bytes = utf8_to_unicode(text_itr, &unicode);
xiaolue738da52015-02-22 20:49:35 +0800402 text_itr += utf_bytes;
403 bytes_rendered += utf_bytes;
404
soyud5a7c0e2014-11-28 14:33:53 +0800405 char_idx = FT_Get_Char_Index(f->face, unicode);
xiaolue738da52015-02-22 20:49:35 +0800406 char_idxs[char_idxs_len] = char_idx;
407
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200408 ent = gr_ttf_glyph_cache_get(f, char_idx);
409 if(ent)
410 {
411 diff = ent->glyph->root.advance.x >> 16;
412
413 if(FT_HAS_KERNING(f->face) && prev_idx && char_idx)
414 {
415 FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta);
416 diff += delta.x >> 6;
417 }
418
419 if(max_width != -1 && total_w + diff > max_width)
420 break;
421
422 total_w += diff;
423 }
424 prev_idx = char_idx;
xiaolue738da52015-02-22 20:49:35 +0800425 ++char_idxs_len;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200426 }
427
428 if(font->max_height == -1)
429 gr_ttf_getMaxFontHeight(font);
430
431 if(font->max_height == -1)
soyud5a7c0e2014-11-28 14:33:53 +0800432 {
433 free(char_idxs);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200434 return -1;
soyud5a7c0e2014-11-28 14:33:53 +0800435 }
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200436
437 height = font->max_height;
438
439 data = malloc(total_w*height);
440 memset(data, 0, total_w*height);
441 x = 0;
442 prev_idx = 0;
443
444 surface->version = sizeof(*surface);
445 surface->width = total_w;
446 surface->height = height;
447 surface->stride = total_w;
448 surface->data = (void*)data;
449 surface->format = GGL_PIXEL_FORMAT_A_8;
450
xiaolue738da52015-02-22 20:49:35 +0800451 for(i = 0; i < char_idxs_len; ++i)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200452 {
soyud5a7c0e2014-11-28 14:33:53 +0800453 char_idx = char_idxs[i];
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200454 if(FT_HAS_KERNING(f->face) && prev_idx && char_idx)
455 {
456 FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta);
457 x += delta.x >> 6;
458 }
459
460 ent = gr_ttf_glyph_cache_get(f, char_idx);
461 if(ent)
462 {
463 gr_ttf_copy_glyph_to_surface(surface, ent->glyph, x, 0, font->base);
464 x += ent->glyph->root.advance.x >> 16;
465 }
466
467 prev_idx = char_idx;
468 }
469
soyud5a7c0e2014-11-28 14:33:53 +0800470 free(char_idxs);
xiaolue738da52015-02-22 20:49:35 +0800471 return bytes_rendered;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200472}
473
474static StringCacheEntry *gr_ttf_string_cache_peek(TrueTypeFont *font, const char *text, int max_width)
475{
476 StringCacheEntry *res;
477 StringCacheKey k = {
478 .text = (char*)text,
479 .max_width = max_width
480 };
481
482 return hashmapGet(font->string_cache, &k);
483}
484
485static StringCacheEntry *gr_ttf_string_cache_get(TrueTypeFont *font, const char *text, int max_width)
486{
487 StringCacheEntry *res;
488 StringCacheKey k = {
489 .text = (char*)text,
490 .max_width = max_width
491 };
492
493 res = hashmapGet(font->string_cache, &k);
494 if(!res)
495 {
496 res = malloc(sizeof(StringCacheEntry));
497 memset(res, 0, sizeof(StringCacheEntry));
xiaolue738da52015-02-22 20:49:35 +0800498 res->rendered_bytes = gr_ttf_render_text(font, &res->surface, text, max_width);
499 if(res->rendered_bytes < 0)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200500 {
501 free(res);
502 return NULL;
503 }
504
505 StringCacheKey *new_key = malloc(sizeof(StringCacheKey));
506 memset(new_key, 0, sizeof(StringCacheKey));
507 new_key->max_width = max_width;
508 new_key->text = strdup(text);
509
510 res->key = new_key;
511
512 if(font->string_cache_tail)
513 {
514 res->prev = font->string_cache_tail;
515 res->prev->next = res;
516 }
517 else
518 font->string_cache_head = res;
519 font->string_cache_tail = res;
520
521 hashmapPut(font->string_cache, new_key, res);
522 }
523 else if(res->next)
524 {
525 // move this entry to the tail of the linked list
526 // if it isn't already there
527 if(res->prev)
528 res->prev->next = res->next;
529
530 res->next->prev = res->prev;
531
532 if(!res->prev)
533 font->string_cache_head = res->next;
534
535 res->next = NULL;
536 res->prev = font->string_cache_tail;
537 res->prev->next = res;
538 font->string_cache_tail = res;
539
540 // truncate old entries
541 if(hashmapSize(font->string_cache) >= STRING_CACHE_MAX_ENTRIES)
542 {
543 printf("Truncating string cache entries.\n");
544 int i;
545 StringCacheEntry *ent;
546 for(i = 0; i < STRING_CACHE_TRUNCATE_ENTRIES; ++i)
547 {
548 ent = font->string_cache_head;
549 font->string_cache_head = ent->next;
550 font->string_cache_head->prev = NULL;
551
552 hashmapRemove(font->string_cache, ent->key);
553
554 gr_ttf_freeStringCache(ent->key, ent, NULL);
555 }
556 }
557 }
558 return res;
559}
560
561int gr_ttf_measureEx(const char *s, void *font)
562{
563 TrueTypeFont *f = font;
564 int res = -1;
565
566 pthread_mutex_lock(&f->mutex);
567 StringCacheEntry *e = gr_ttf_string_cache_get(font, s, -1);
568 if(e)
569 res = e->surface.width;
570 pthread_mutex_unlock(&f->mutex);
571
572 return res;
573}
574
575int gr_ttf_maxExW(const char *s, void *font, int max_width)
576{
577 TrueTypeFont *f = font;
578 TrueTypeCacheEntry *ent;
xiaolue738da52015-02-22 20:49:35 +0800579 int max_bytes = 0, total_w = 0;
580 int utf_bytes, prev_utf_bytes = 0;
581 unsigned int unicode = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200582 int char_idx, prev_idx = 0;
583 FT_Vector delta;
584 StringCacheEntry *e;
585
586 pthread_mutex_lock(&f->mutex);
587
588 e = gr_ttf_string_cache_peek(font, s, max_width);
589 if(e)
590 {
xiaolue738da52015-02-22 20:49:35 +0800591 max_bytes = e->rendered_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200592 pthread_mutex_unlock(&f->mutex);
xiaolue738da52015-02-22 20:49:35 +0800593 return max_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200594 }
595
xiaolue738da52015-02-22 20:49:35 +0800596 while(*s)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200597 {
soyud5a7c0e2014-11-28 14:33:53 +0800598 utf_bytes = utf8_to_unicode(s, &unicode);
xiaolue738da52015-02-22 20:49:35 +0800599 s += utf_bytes;
600
601 char_idx = FT_Get_Char_Index(f->face, unicode);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200602 if(FT_HAS_KERNING(f->face) && prev_idx && char_idx)
603 {
604 FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta);
605 total_w += delta.x >> 6;
606 }
607 prev_idx = char_idx;
608
609 if(total_w > max_width)
xiaolue738da52015-02-22 20:49:35 +0800610 {
611 max_bytes -= prev_utf_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200612 break;
xiaolue738da52015-02-22 20:49:35 +0800613 }
614 prev_utf_bytes = utf_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200615
616 ent = gr_ttf_glyph_cache_get(f, char_idx);
617 if(!ent)
618 continue;
619
620 total_w += ent->glyph->root.advance.x >> 16;
xiaolue738da52015-02-22 20:49:35 +0800621 max_bytes += utf_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200622 }
623 pthread_mutex_unlock(&f->mutex);
xiaolue738da52015-02-22 20:49:35 +0800624 return max_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200625}
626
627int gr_ttf_textExWH(void *context, int x, int y, const char *s, void *pFont, int max_width, int max_height)
628{
629 GGLContext *gl = context;
630 TrueTypeFont *font = pFont;
631
632 // not actualy max width, but max_width + x
633 if(max_width != -1)
634 {
635 max_width -= x;
636 if(max_width <= 0)
637 return 0;
638 }
639
640 pthread_mutex_lock(&font->mutex);
641
642 StringCacheEntry *e = gr_ttf_string_cache_get(font, s, max_width);
643 if(!e)
644 {
645 pthread_mutex_unlock(&font->mutex);
646 return -1;
647 }
648
649 int y_bottom = y + e->surface.height;
xiaolue738da52015-02-22 20:49:35 +0800650 int res = e->rendered_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200651
652 if(max_height != -1 && max_height < y_bottom)
653 {
654 y_bottom = max_height;
655 if(y_bottom <= y)
656 {
657 pthread_mutex_unlock(&font->mutex);
658 return 0;
659 }
660 }
661
662 gl->bindTexture(gl, &e->surface);
663 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
664 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
665 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200666
Vojtech Bocek3041c882015-03-06 00:28:21 +0100667 gl->enable(gl, GGL_TEXTURE_2D);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200668 gl->texCoord2i(gl, -x, -y);
669 gl->recti(gl, x, y, x + e->surface.width, y_bottom);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100670 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200671
672 pthread_mutex_unlock(&font->mutex);
673 return res;
674}
675
676int gr_ttf_getMaxFontHeight(void *font)
677{
678 int res;
679 TrueTypeFont *f = font;
680
681 pthread_mutex_lock(&f->mutex);
682
683 if(f->max_height == -1)
684 {
685 char c;
686 int char_idx;
687 int error;
688 FT_Glyph glyph;
689 FT_BBox bbox;
690 FT_BBox bbox_glyph;
691 TrueTypeCacheEntry *ent;
692
693 bbox.yMin = bbox_glyph.yMin = LONG_MAX;
694 bbox.yMax = bbox_glyph.yMax = LONG_MIN;
695
696 for(c = '!'; c <= '~'; ++c)
697 {
698 char_idx = FT_Get_Char_Index(f->face, c);
699 ent = gr_ttf_glyph_cache_peek(f, char_idx);
700 if(ent)
701 {
702 bbox.yMin = MIN(bbox.yMin, ent->bbox.yMin);
703 bbox.yMax = MAX(bbox.yMax, ent->bbox.yMax);
704 }
705 else
706 {
707 error = FT_Load_Glyph(f->face, char_idx, 0);
708 if(error)
709 continue;
710
711 error = FT_Get_Glyph(f->face->glyph, &glyph);
712 if(error)
713 continue;
714
715 FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox_glyph);
716 bbox.yMin = MIN(bbox.yMin, bbox_glyph.yMin);
717 bbox.yMax = MAX(bbox.yMax, bbox_glyph.yMax);
718
719 FT_Done_Glyph(glyph);
720 }
721 }
722
723 if(bbox.yMin > bbox.yMax)
724 bbox.yMin = bbox.yMax = 0;
725
726 f->max_height = bbox.yMax - bbox.yMin;
727 f->base = bbox.yMax;
728
729 // FIXME: twrp fonts have some padding on top, I'll add it here
730 // Should be fixed in the themes
731 f->max_height += f->size / 4;
732 f->base += f->size / 4;
733 }
734
735 res = f->max_height;
736
737 pthread_mutex_unlock(&f->mutex);
738 return res;
739}
740
741static bool gr_ttf_dump_stats_count_string_cache(void *key, void *value, void *context)
742{
743 int *string_cache_size = context;
744 StringCacheEntry *e = value;
745 *string_cache_size += e->surface.height*e->surface.width + sizeof(StringCacheEntry);
746 return true;
747}
748
749static bool gr_ttf_dump_stats_font(void *key, void *value, void *context)
750{
751 TrueTypeFontKey *k = key;
752 TrueTypeFont *f = value;
753 int *total_string_cache_size = context;
754 int string_cache_size = 0;
755
756 pthread_mutex_lock(&f->mutex);
757
758 hashmapForEach(f->string_cache, gr_ttf_dump_stats_count_string_cache, &string_cache_size);
759
760 printf(" Font %s (size %d, dpi %d):\n"
761 " refcount: %d\n"
762 " max_height: %d\n"
763 " base: %d\n"
764 " glyph_cache: %d entries\n"
765 " string_cache: %d entries (%.2f kB)\n",
766 k->path, k->size, k->dpi,
767 f->refcount, f->max_height, f->base,
768 hashmapSize(f->glyph_cache),
769 hashmapSize(f->string_cache), ((double)string_cache_size)/1024);
770
771 pthread_mutex_unlock(&f->mutex);
772
773 *total_string_cache_size += string_cache_size;
774 return true;
775}
776
777void gr_ttf_dump_stats(void)
778{
779 pthread_mutex_lock(&font_data.mutex);
780
781 printf("TrueType fonts system stats: ");
782 if(!font_data.fonts)
783 printf("no truetype fonts loaded.\n");
784 else
785 {
786 int total_string_cache_size = 0;
787 printf("%d fonts loaded.\n", hashmapSize(font_data.fonts));
788 hashmapForEach(font_data.fonts, gr_ttf_dump_stats_font, &total_string_cache_size);
789 printf(" Total string cache size: %.2f kB\n", ((double)total_string_cache_size)/1024);
790 }
791
792 pthread_mutex_unlock(&font_data.mutex);
793}