blob: d9ed0198b00d7cf67db8cc325d136841e464a96b [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{
soyud5a7c0e2014-11-28 14:33:53 +0800123 int utf_bytes = 1;
124 unsigned int unicode = 0;
125 unsigned char tmp;
126 tmp = *pIn++;
127 if (tmp < 0x80)
128 {
129 *pOut = tmp;
130 }
131 else
132 {
133 unsigned int high_bit_mask = 0x3F;
134 unsigned int high_bit_shift = 0;
135 int total_bits = 0;
136 while((tmp & 0xC0) == 0xC0)
137 {
138 utf_bytes ++;
Xuefer0eb2aab2015-04-17 02:01:32 +0800139 if(utf_bytes > 6)
140 {
141 *pOut = tmp;
142 return 1;
143 }
soyud5a7c0e2014-11-28 14:33:53 +0800144 tmp = 0xFF & (tmp << 1);
145 total_bits += 6;
146 high_bit_mask >>= 1;
147 high_bit_shift++;
148 unicode <<= 6;
149 unicode |= (*pIn++) & 0x3F;
150 }
151 unicode |= ((tmp >> high_bit_shift) & high_bit_mask) << total_bits;
152 *pOut = unicode;
153 }
154
155 return utf_bytes;
156}
157
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200158static bool gr_ttf_string_cache_equals(void *keyA, void *keyB)
159{
160 StringCacheKey *a = keyA;
161 StringCacheKey *b = keyB;
162 return a->max_width == b->max_width && strcmp(a->text, b->text) == 0;
163}
164
165static int gr_ttf_string_cache_hash(void *key)
166{
167 StringCacheKey *k = key;
168 return fnv_hash(k->text, strlen(k->text));
169}
170
171static bool gr_ttf_font_cache_equals(void *keyA, void *keyB)
172{
173 TrueTypeFontKey *a = keyA;
174 TrueTypeFontKey *b = keyB;
175 return (a->size == b->size) && (a->dpi == b->dpi) && !strcmp(a->path, b->path);
176}
177
178static int gr_ttf_font_cache_hash(void *key)
179{
180 TrueTypeFontKey *k = key;
181
182 uint32_t hash = fnv_hash(k->path, strlen(k->path));
183 hash = fnv_hash_add(hash, k->size);
184 hash = fnv_hash_add(hash, k->dpi);
185 return hash;
186}
187
188void *gr_ttf_loadFont(const char *filename, int size, int dpi)
189{
190 int error;
191 TrueTypeFont *res = NULL;
192
193 pthread_mutex_lock(&font_data.mutex);
194
195 if(font_data.fonts)
196 {
197 TrueTypeFontKey k = {
198 .size = size,
199 .dpi = dpi,
200 .path = (char*)filename
201 };
202
203 res = hashmapGet(font_data.fonts, &k);
204 if(res)
205 {
206 ++res->refcount;
207 goto exit;
208 }
209 }
210
211 if(!font_data.ft_library)
212 {
213 error = FT_Init_FreeType(&font_data.ft_library);
214 if(error)
215 {
216 fprintf(stderr, "Failed to init libfreetype! %d\n", error);
217 goto exit;
218 }
219 }
220
221 FT_Face face;
222 error = FT_New_Face(font_data.ft_library, filename, 0, &face);
223 if(error)
224 {
225 fprintf(stderr, "Failed to load truetype face %s: %d\n", filename, error);
226 goto exit;
227 }
228
229 error = FT_Set_Char_Size(face, 0, size*16, dpi, dpi);
230 if(error)
231 {
232 fprintf(stderr, "Failed to set truetype face size to %d, dpi %d: %d\n", size, dpi, error);
233 FT_Done_Face(face);
234 goto exit;
235 }
236
237 res = malloc(sizeof(TrueTypeFont));
238 memset(res, 0, sizeof(TrueTypeFont));
239 res->type = FONT_TYPE_TTF;
240 res->size = size;
241 res->dpi = dpi;
242 res->face = face;
243 res->max_height = -1;
244 res->base = -1;
245 res->refcount = 1;
246 res->glyph_cache = hashmapCreate(32, hashmapIntHash, hashmapIntEquals);
247 res->string_cache = hashmapCreate(128, gr_ttf_string_cache_hash, gr_ttf_string_cache_equals);
248 pthread_mutex_init(&res->mutex, 0);
249
250 if(!font_data.fonts)
251 font_data.fonts = hashmapCreate(4, gr_ttf_font_cache_hash, gr_ttf_font_cache_equals);
252
253 TrueTypeFontKey *key = malloc(sizeof(TrueTypeFontKey));
254 memset(key, 0, sizeof(TrueTypeFontKey));
255 key->path = strdup(filename);
256 key->size = size;
257 key->dpi = dpi;
258
259 res->key = key;
260
261 hashmapPut(font_data.fonts, key, res);
262
263exit:
264 pthread_mutex_unlock(&font_data.mutex);
265 return res;
266}
267
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500268void *gr_ttf_scaleFont(void *font, int max_width, int measured_width)
269{
270 if (!font)
271 return NULL;
272
273 TrueTypeFont *f = font;
274 float scale_value = (float)(max_width) / (float)(measured_width);
275 int new_size = ((int)((float)f->size * scale_value)) - 1;
276 if (new_size < 1)
277 new_size = 1;
278 const char* file = f->key->path;
279 int dpi = f->dpi;
280 return gr_ttf_loadFont(file, new_size, dpi);
281}
282
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200283static bool gr_ttf_freeFontCache(void *key, void *value, void *context)
284{
285 TrueTypeCacheEntry *e = value;
286 FT_Done_Glyph((FT_Glyph)e->glyph);
287 free(e);
288 free(key);
289 return true;
290}
291
292static bool gr_ttf_freeStringCache(void *key, void *value, void *context)
293{
294 StringCacheKey *k = key;
295 free(k->text);
296 free(k);
297
298 StringCacheEntry *e = value;
299 free(e->surface.data);
300 free(e);
301 return true;
302}
303
304void gr_ttf_freeFont(void *font)
305{
306 pthread_mutex_lock(&font_data.mutex);
307
308 TrueTypeFont *d = font;
309
310 if(--d->refcount == 0)
311 {
312 hashmapRemove(font_data.fonts, d->key);
313
314 if(hashmapSize(font_data.fonts) == 0)
315 {
316 hashmapFree(font_data.fonts);
317 font_data.fonts = NULL;
318 }
319
320 free(d->key->path);
321 free(d->key);
322
323 FT_Done_Face(d->face);
324 hashmapForEach(d->string_cache, gr_ttf_freeStringCache, NULL);
325 hashmapFree(d->string_cache);
326 hashmapForEach(d->glyph_cache, gr_ttf_freeFontCache, NULL);
327 hashmapFree(d->glyph_cache);
328 pthread_mutex_destroy(&d->mutex);
329 free(d);
330 }
331
332 pthread_mutex_unlock(&font_data.mutex);
333}
334
335static TrueTypeCacheEntry *gr_ttf_glyph_cache_peek(TrueTypeFont *font, int char_index)
336{
337 return hashmapGet(font->glyph_cache, &char_index);
338}
339
340static TrueTypeCacheEntry *gr_ttf_glyph_cache_get(TrueTypeFont *font, int char_index)
341{
342 TrueTypeCacheEntry *res = hashmapGet(font->glyph_cache, &char_index);
343 if(!res)
344 {
345 int error = FT_Load_Glyph(font->face, char_index, FT_LOAD_RENDER);
346 if(error)
347 {
348 fprintf(stderr, "Failed to load glyph idx %d: %d\n", char_index, error);
349 return NULL;
350 }
351
352 FT_BitmapGlyph glyph;
353 error = FT_Get_Glyph(font->face->glyph, (FT_Glyph*)&glyph);
354 if(error)
355 {
356 fprintf(stderr, "Failed to copy glyph %d: %d\n", char_index, error);
357 return NULL;
358 }
359
360 res = malloc(sizeof(TrueTypeCacheEntry));
361 memset(res, 0, sizeof(TrueTypeCacheEntry));
362 res->glyph = glyph;
363 FT_Glyph_Get_CBox((FT_Glyph)glyph, FT_GLYPH_BBOX_PIXELS, &res->bbox);
364
365 int *key = malloc(sizeof(int));
366 *key = char_index;
367
368 hashmapPut(font->glyph_cache, key, res);
369 }
370
371 return res;
372}
373
374static int gr_ttf_copy_glyph_to_surface(GGLSurface *dest, FT_BitmapGlyph glyph, int offX, int offY, int base)
375{
376 int y;
377 uint8_t *src_itr = glyph->bitmap.buffer;
378 uint8_t *dest_itr = dest->data;
379
380 if(glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
381 {
382 fprintf(stderr, "Unsupported pixel mode in FT_BitmapGlyph %d\n", glyph->bitmap.pixel_mode);
383 return -1;
384 }
385
386 dest_itr += (offY + base - glyph->top)*dest->stride + (offX + glyph->left);
387
Vojtech Bocek54cf1082015-03-15 16:50:26 +0100388 // FIXME: if glyph->left is negative and everything else is 0 (e.g. letter 'j' in Roboto-Regular),
389 // the result might end up being before the buffer - I'm not sure how to properly handle this.
390 if(dest_itr < dest->data)
391 dest_itr = dest->data;
392
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200393 for(y = 0; y < glyph->bitmap.rows; ++y)
394 {
395 memcpy(dest_itr, src_itr, glyph->bitmap.width);
396 src_itr += glyph->bitmap.pitch;
397 dest_itr += dest->stride;
398 }
399 return 0;
400}
401
Vojtech Boceka482f252015-03-15 17:03:50 +0100402static void gr_ttf_calcMaxFontHeight(TrueTypeFont *f)
403{
404 char c;
405 int char_idx;
406 int error;
407 FT_Glyph glyph;
408 FT_BBox bbox;
409 FT_BBox bbox_glyph;
410 TrueTypeCacheEntry *ent;
411
412 bbox.yMin = bbox_glyph.yMin = LONG_MAX;
413 bbox.yMax = bbox_glyph.yMax = LONG_MIN;
414
415 for(c = '!'; c <= '~'; ++c)
416 {
417 char_idx = FT_Get_Char_Index(f->face, c);
418 ent = gr_ttf_glyph_cache_peek(f, char_idx);
419 if(ent)
420 {
421 bbox.yMin = MIN(bbox.yMin, ent->bbox.yMin);
422 bbox.yMax = MAX(bbox.yMax, ent->bbox.yMax);
423 }
424 else
425 {
426 error = FT_Load_Glyph(f->face, char_idx, 0);
427 if(error)
428 continue;
429
430 error = FT_Get_Glyph(f->face->glyph, &glyph);
431 if(error)
432 continue;
433
434 FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &bbox_glyph);
435 bbox.yMin = MIN(bbox.yMin, bbox_glyph.yMin);
436 bbox.yMax = MAX(bbox.yMax, bbox_glyph.yMax);
437
438 FT_Done_Glyph(glyph);
439 }
440 }
441
442 if(bbox.yMin > bbox.yMax)
443 bbox.yMin = bbox.yMax = 0;
444
445 f->max_height = bbox.yMax - bbox.yMin;
446 f->base = bbox.yMax;
447
448 // FIXME: twrp fonts have some padding on top, I'll add it here
449 // Should be fixed in the themes
450 f->max_height += f->size / 4;
451 f->base += f->size / 4;
452}
453
xiaolue738da52015-02-22 20:49:35 +0800454// 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 +0200455static int gr_ttf_render_text(TrueTypeFont *font, GGLSurface *surface, const char *text, int max_width)
456{
457 TrueTypeFont *f = font;
458 TrueTypeCacheEntry *ent;
xiaolue738da52015-02-22 20:49:35 +0800459 int bytes_rendered = 0, total_w = 0;
soyud5a7c0e2014-11-28 14:33:53 +0800460 int utf_bytes = 0;
461 unsigned int unicode = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200462 int i, x, diff, char_idx, prev_idx = 0;
463 int height, base;
464 FT_Vector delta;
465 uint8_t *data = NULL;
466 const char *text_itr = text;
soyud5a7c0e2014-11-28 14:33:53 +0800467 int *char_idxs;
xiaolue738da52015-02-22 20:49:35 +0800468 int char_idxs_len = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200469
xiaolue738da52015-02-22 20:49:35 +0800470 char_idxs = malloc(strlen(text) * sizeof(int));
471
soyud5a7c0e2014-11-28 14:33:53 +0800472 while(*text_itr)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200473 {
soyud5a7c0e2014-11-28 14:33:53 +0800474 utf_bytes = utf8_to_unicode(text_itr, &unicode);
xiaolue738da52015-02-22 20:49:35 +0800475 text_itr += utf_bytes;
476 bytes_rendered += utf_bytes;
477
soyud5a7c0e2014-11-28 14:33:53 +0800478 char_idx = FT_Get_Char_Index(f->face, unicode);
xiaolue738da52015-02-22 20:49:35 +0800479 char_idxs[char_idxs_len] = char_idx;
480
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200481 ent = gr_ttf_glyph_cache_get(f, char_idx);
482 if(ent)
483 {
484 diff = ent->glyph->root.advance.x >> 16;
485
486 if(FT_HAS_KERNING(f->face) && prev_idx && char_idx)
487 {
488 FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta);
489 diff += delta.x >> 6;
490 }
491
492 if(max_width != -1 && total_w + diff > max_width)
493 break;
494
495 total_w += diff;
496 }
497 prev_idx = char_idx;
xiaolue738da52015-02-22 20:49:35 +0800498 ++char_idxs_len;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200499 }
500
501 if(font->max_height == -1)
Vojtech Boceka482f252015-03-15 17:03:50 +0100502 gr_ttf_calcMaxFontHeight(font);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200503
504 if(font->max_height == -1)
soyud5a7c0e2014-11-28 14:33:53 +0800505 {
506 free(char_idxs);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200507 return -1;
soyud5a7c0e2014-11-28 14:33:53 +0800508 }
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200509
510 height = font->max_height;
511
512 data = malloc(total_w*height);
513 memset(data, 0, total_w*height);
514 x = 0;
515 prev_idx = 0;
516
517 surface->version = sizeof(*surface);
518 surface->width = total_w;
519 surface->height = height;
520 surface->stride = total_w;
521 surface->data = (void*)data;
522 surface->format = GGL_PIXEL_FORMAT_A_8;
523
xiaolue738da52015-02-22 20:49:35 +0800524 for(i = 0; i < char_idxs_len; ++i)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200525 {
soyud5a7c0e2014-11-28 14:33:53 +0800526 char_idx = char_idxs[i];
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200527 if(FT_HAS_KERNING(f->face) && prev_idx && char_idx)
528 {
529 FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta);
530 x += delta.x >> 6;
531 }
532
533 ent = gr_ttf_glyph_cache_get(f, char_idx);
534 if(ent)
535 {
536 gr_ttf_copy_glyph_to_surface(surface, ent->glyph, x, 0, font->base);
537 x += ent->glyph->root.advance.x >> 16;
538 }
539
540 prev_idx = char_idx;
541 }
542
soyud5a7c0e2014-11-28 14:33:53 +0800543 free(char_idxs);
xiaolue738da52015-02-22 20:49:35 +0800544 return bytes_rendered;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200545}
546
547static StringCacheEntry *gr_ttf_string_cache_peek(TrueTypeFont *font, const char *text, int max_width)
548{
549 StringCacheEntry *res;
550 StringCacheKey k = {
551 .text = (char*)text,
552 .max_width = max_width
553 };
554
555 return hashmapGet(font->string_cache, &k);
556}
557
558static StringCacheEntry *gr_ttf_string_cache_get(TrueTypeFont *font, const char *text, int max_width)
559{
560 StringCacheEntry *res;
561 StringCacheKey k = {
562 .text = (char*)text,
563 .max_width = max_width
564 };
565
566 res = hashmapGet(font->string_cache, &k);
567 if(!res)
568 {
569 res = malloc(sizeof(StringCacheEntry));
570 memset(res, 0, sizeof(StringCacheEntry));
xiaolue738da52015-02-22 20:49:35 +0800571 res->rendered_bytes = gr_ttf_render_text(font, &res->surface, text, max_width);
572 if(res->rendered_bytes < 0)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200573 {
574 free(res);
575 return NULL;
576 }
577
578 StringCacheKey *new_key = malloc(sizeof(StringCacheKey));
579 memset(new_key, 0, sizeof(StringCacheKey));
580 new_key->max_width = max_width;
581 new_key->text = strdup(text);
582
583 res->key = new_key;
584
585 if(font->string_cache_tail)
586 {
587 res->prev = font->string_cache_tail;
588 res->prev->next = res;
589 }
590 else
591 font->string_cache_head = res;
592 font->string_cache_tail = res;
593
594 hashmapPut(font->string_cache, new_key, res);
595 }
596 else if(res->next)
597 {
598 // move this entry to the tail of the linked list
599 // if it isn't already there
600 if(res->prev)
601 res->prev->next = res->next;
602
603 res->next->prev = res->prev;
604
605 if(!res->prev)
606 font->string_cache_head = res->next;
607
608 res->next = NULL;
609 res->prev = font->string_cache_tail;
610 res->prev->next = res;
611 font->string_cache_tail = res;
612
613 // truncate old entries
614 if(hashmapSize(font->string_cache) >= STRING_CACHE_MAX_ENTRIES)
615 {
616 printf("Truncating string cache entries.\n");
617 int i;
618 StringCacheEntry *ent;
619 for(i = 0; i < STRING_CACHE_TRUNCATE_ENTRIES; ++i)
620 {
621 ent = font->string_cache_head;
622 font->string_cache_head = ent->next;
623 font->string_cache_head->prev = NULL;
624
625 hashmapRemove(font->string_cache, ent->key);
626
627 gr_ttf_freeStringCache(ent->key, ent, NULL);
628 }
629 }
630 }
631 return res;
632}
633
634int gr_ttf_measureEx(const char *s, void *font)
635{
636 TrueTypeFont *f = font;
637 int res = -1;
638
639 pthread_mutex_lock(&f->mutex);
640 StringCacheEntry *e = gr_ttf_string_cache_get(font, s, -1);
641 if(e)
642 res = e->surface.width;
643 pthread_mutex_unlock(&f->mutex);
644
645 return res;
646}
647
648int gr_ttf_maxExW(const char *s, void *font, int max_width)
649{
650 TrueTypeFont *f = font;
651 TrueTypeCacheEntry *ent;
xiaolue738da52015-02-22 20:49:35 +0800652 int max_bytes = 0, total_w = 0;
653 int utf_bytes, prev_utf_bytes = 0;
654 unsigned int unicode = 0;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200655 int char_idx, prev_idx = 0;
656 FT_Vector delta;
657 StringCacheEntry *e;
658
659 pthread_mutex_lock(&f->mutex);
660
661 e = gr_ttf_string_cache_peek(font, s, max_width);
662 if(e)
663 {
xiaolue738da52015-02-22 20:49:35 +0800664 max_bytes = e->rendered_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200665 pthread_mutex_unlock(&f->mutex);
xiaolue738da52015-02-22 20:49:35 +0800666 return max_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200667 }
668
xiaolue738da52015-02-22 20:49:35 +0800669 while(*s)
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200670 {
soyud5a7c0e2014-11-28 14:33:53 +0800671 utf_bytes = utf8_to_unicode(s, &unicode);
xiaolue738da52015-02-22 20:49:35 +0800672 s += utf_bytes;
673
674 char_idx = FT_Get_Char_Index(f->face, unicode);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200675 if(FT_HAS_KERNING(f->face) && prev_idx && char_idx)
676 {
677 FT_Get_Kerning(f->face, prev_idx, char_idx, FT_KERNING_DEFAULT, &delta);
678 total_w += delta.x >> 6;
679 }
680 prev_idx = char_idx;
681
682 if(total_w > max_width)
xiaolue738da52015-02-22 20:49:35 +0800683 {
684 max_bytes -= prev_utf_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200685 break;
xiaolue738da52015-02-22 20:49:35 +0800686 }
687 prev_utf_bytes = utf_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200688
689 ent = gr_ttf_glyph_cache_get(f, char_idx);
690 if(!ent)
691 continue;
692
693 total_w += ent->glyph->root.advance.x >> 16;
xiaolue738da52015-02-22 20:49:35 +0800694 max_bytes += utf_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200695 }
696 pthread_mutex_unlock(&f->mutex);
xiaolue738da52015-02-22 20:49:35 +0800697 return max_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200698}
699
700int gr_ttf_textExWH(void *context, int x, int y, const char *s, void *pFont, int max_width, int max_height)
701{
702 GGLContext *gl = context;
703 TrueTypeFont *font = pFont;
704
705 // not actualy max width, but max_width + x
706 if(max_width != -1)
707 {
708 max_width -= x;
709 if(max_width <= 0)
710 return 0;
711 }
712
713 pthread_mutex_lock(&font->mutex);
714
715 StringCacheEntry *e = gr_ttf_string_cache_get(font, s, max_width);
716 if(!e)
717 {
718 pthread_mutex_unlock(&font->mutex);
719 return -1;
720 }
721
722 int y_bottom = y + e->surface.height;
xiaolue738da52015-02-22 20:49:35 +0800723 int res = e->rendered_bytes;
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200724
725 if(max_height != -1 && max_height < y_bottom)
726 {
727 y_bottom = max_height;
728 if(y_bottom <= y)
729 {
730 pthread_mutex_unlock(&font->mutex);
731 return 0;
732 }
733 }
734
735 gl->bindTexture(gl, &e->surface);
736 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
737 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
738 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200739
Vojtech Bocek3041c882015-03-06 00:28:21 +0100740 gl->enable(gl, GGL_TEXTURE_2D);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200741 gl->texCoord2i(gl, -x, -y);
742 gl->recti(gl, x, y, x + e->surface.width, y_bottom);
Vojtech Bocek3041c882015-03-06 00:28:21 +0100743 gl->disable(gl, GGL_TEXTURE_2D);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200744
745 pthread_mutex_unlock(&font->mutex);
746 return res;
747}
748
749int gr_ttf_getMaxFontHeight(void *font)
750{
751 int res;
752 TrueTypeFont *f = font;
753
754 pthread_mutex_lock(&f->mutex);
755
756 if(f->max_height == -1)
Vojtech Boceka482f252015-03-15 17:03:50 +0100757 gr_ttf_calcMaxFontHeight(f);
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200758 res = f->max_height;
759
760 pthread_mutex_unlock(&f->mutex);
761 return res;
762}
763
764static bool gr_ttf_dump_stats_count_string_cache(void *key, void *value, void *context)
765{
766 int *string_cache_size = context;
767 StringCacheEntry *e = value;
768 *string_cache_size += e->surface.height*e->surface.width + sizeof(StringCacheEntry);
769 return true;
770}
771
772static bool gr_ttf_dump_stats_font(void *key, void *value, void *context)
773{
774 TrueTypeFontKey *k = key;
775 TrueTypeFont *f = value;
776 int *total_string_cache_size = context;
777 int string_cache_size = 0;
778
779 pthread_mutex_lock(&f->mutex);
780
781 hashmapForEach(f->string_cache, gr_ttf_dump_stats_count_string_cache, &string_cache_size);
782
783 printf(" Font %s (size %d, dpi %d):\n"
784 " refcount: %d\n"
785 " max_height: %d\n"
786 " base: %d\n"
787 " glyph_cache: %d entries\n"
788 " string_cache: %d entries (%.2f kB)\n",
789 k->path, k->size, k->dpi,
790 f->refcount, f->max_height, f->base,
791 hashmapSize(f->glyph_cache),
792 hashmapSize(f->string_cache), ((double)string_cache_size)/1024);
793
794 pthread_mutex_unlock(&f->mutex);
795
796 *total_string_cache_size += string_cache_size;
797 return true;
798}
799
800void gr_ttf_dump_stats(void)
801{
802 pthread_mutex_lock(&font_data.mutex);
803
804 printf("TrueType fonts system stats: ");
805 if(!font_data.fonts)
806 printf("no truetype fonts loaded.\n");
807 else
808 {
809 int total_string_cache_size = 0;
810 printf("%d fonts loaded.\n", hashmapSize(font_data.fonts));
811 hashmapForEach(font_data.fonts, gr_ttf_dump_stats_font, &total_string_cache_size);
812 printf(" Total string cache size: %.2f kB\n", ((double)total_string_cache_size)/1024);
813 }
814
815 pthread_mutex_unlock(&font_data.mutex);
816}