Cleanup: remove redundant BKE/BLI/BIF headers
[blender.git] / source / blender / blenfont / intern / blf_glyph.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenfont/intern/blf_glyph.c
28  *  \ingroup blf
29  *
30  * Glyph rendering, texturing and caching. Wraps Freetype and OpenGL functions.
31  */
32
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38
39 #include <ft2build.h>
40
41 #include FT_FREETYPE_H
42 #include FT_GLYPH_H
43 #include FT_OUTLINE_H
44 #include FT_BITMAP_H
45
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_vec_types.h"
49 #include "DNA_userdef_types.h"
50
51 #include "BLI_listbase.h"
52 #include "BLI_rect.h"
53 #include "BLI_threads.h"
54
55 #include "BLF_api.h"
56
57 #ifndef BLF_STANDALONE
58 #  include "GPU_immediate.h"
59 #  include "GPU_extensions.h"
60 #endif
61
62 #include "blf_internal_types.h"
63 #include "blf_internal.h"
64
65 #include "BLI_strict_flags.h"
66 #include "BLI_math_vector.h"
67
68 KerningCacheBLF *blf_kerning_cache_find(FontBLF *font)
69 {
70         KerningCacheBLF *p;
71
72         p = (KerningCacheBLF *)font->kerning_caches.first;
73         while (p) {
74                 if (p->mode == font->kerning_mode)
75                         return p;
76                 p = p->next;
77         }
78         return NULL;
79 }
80
81 /* Create a new glyph cache for the current kerning mode. */
82 KerningCacheBLF *blf_kerning_cache_new(FontBLF *font)
83 {
84         KerningCacheBLF *kc;
85
86         kc = (KerningCacheBLF *)MEM_callocN(sizeof(KerningCacheBLF), "blf_kerning_cache_new");
87         kc->next = NULL;
88         kc->prev = NULL;
89         kc->mode = font->kerning_mode;
90
91         unsigned int i, j;
92         for (i = 0; i < 0x80; i++) {
93                 for (j = 0; j < 0x80; j++) {
94                         GlyphBLF *g = blf_glyph_search(font->glyph_cache, i);
95                         if (!g) {
96                                 FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
97                                 g = blf_glyph_add(font, glyph_index, i);
98                         }
99                         /* Cannot fail since it has been added just before. */
100                         GlyphBLF *g_prev = blf_glyph_search(font->glyph_cache, j);
101
102                         FT_Vector delta = { .x = 0, .y = 0, };
103                         if (FT_Get_Kerning(font->face, g_prev->idx, g->idx, kc->mode, &delta) == 0) {
104                                 kc->table[i][j] = (int)delta.x >> 6;
105                         }
106                         else {
107                                 kc->table[i][j] = 0;
108                         }
109                 }
110         }
111
112         BLI_addhead(&font->kerning_caches, kc);
113         return kc;
114 }
115
116 void blf_kerning_cache_clear(FontBLF *font)
117 {
118         font->kerning_cache = NULL;
119         BLI_freelistN(&font->kerning_caches);
120 }
121
122 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi)
123 {
124         GlyphCacheBLF *p;
125
126         p = (GlyphCacheBLF *)font->cache.first;
127         while (p) {
128                 if (p->size == size && p->dpi == dpi)
129                         return p;
130                 p = p->next;
131         }
132         return NULL;
133 }
134
135 /* Create a new glyph cache for the current size and dpi. */
136 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
137 {
138         GlyphCacheBLF *gc;
139
140         gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
141         gc->next = NULL;
142         gc->prev = NULL;
143         gc->size = font->size;
144         gc->dpi = font->dpi;
145
146         memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
147         memset(gc->bucket, 0, sizeof(gc->bucket));
148
149         gc->textures = (GPUTexture **)MEM_callocN(sizeof(GPUTexture *) * 256, __func__);
150         gc->textures_len = 256;
151         gc->texture_current = BLF_TEXTURE_UNSET;
152         gc->offset_x = 3; /* enough padding for blur */
153         gc->offset_y = 3; /* enough padding for blur */
154         gc->pad = 6;
155
156         gc->glyphs_len_max = (int)font->face->num_glyphs;
157         gc->glyphs_len_free = (int)font->face->num_glyphs;
158         gc->ascender = ((float)font->face->size->metrics.ascender) / 64.0f;
159         gc->descender = ((float)font->face->size->metrics.descender) / 64.0f;
160
161         if (FT_IS_SCALABLE(font->face)) {
162                 gc->glyph_width_max = (int)((float)(font->face->bbox.xMax - font->face->bbox.xMin) *
163                                             (((float)font->face->size->metrics.x_ppem) /
164                                              ((float)font->face->units_per_EM)));
165
166                 gc->glyph_height_max = (int)((float)(font->face->bbox.yMax - font->face->bbox.yMin) *
167                                              (((float)font->face->size->metrics.y_ppem) /
168                                               ((float)font->face->units_per_EM)));
169         }
170         else {
171                 gc->glyph_width_max = (int)(((float)font->face->size->metrics.max_advance) / 64.0f);
172                 gc->glyph_height_max = (int)(((float)font->face->size->metrics.height) / 64.0f);
173         }
174
175         /* can happen with size 1 fonts */
176         CLAMP_MIN(gc->glyph_width_max, 1);
177         CLAMP_MIN(gc->glyph_height_max, 1);
178
179         gc->p2_width = 0;
180         gc->p2_height = 0;
181
182         BLI_addhead(&font->cache, gc);
183         return gc;
184 }
185
186 void blf_glyph_cache_clear(FontBLF *font)
187 {
188         GlyphCacheBLF *gc;
189
190         while ((gc = BLI_pophead(&font->cache))) {
191                 blf_glyph_cache_free(gc);
192         }
193         font->glyph_cache = NULL;
194 }
195
196 void blf_glyph_cache_free(GlyphCacheBLF *gc)
197 {
198         GlyphBLF *g;
199         unsigned int i;
200
201         for (i = 0; i < 257; i++) {
202                 while ((g = BLI_pophead(&gc->bucket[i]))) {
203                         blf_glyph_free(g);
204                 }
205         }
206         for (i = 0; i < gc->textures_len; i++) {
207                 if (gc->textures[i]) {
208                         GPU_texture_free(gc->textures[i]);
209                 }
210         }
211         MEM_freeN(gc->textures);
212         MEM_freeN(gc);
213 }
214
215 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
216 {
217         int i;
218         char error[256];
219
220         /* move the index. */
221         gc->texture_current++;
222
223         if (UNLIKELY(gc->texture_current >= gc->textures_len)) {
224                 gc->textures_len *= 2;
225                 gc->textures = MEM_recallocN((void *)gc->textures, sizeof(GPUTexture *) * gc->textures_len);
226         }
227
228         gc->p2_width = (int)blf_next_p2((unsigned int)((gc->glyphs_len_free * gc->glyph_width_max) + (gc->pad * 2)));
229         if (gc->p2_width > font->tex_size_max) {
230                 gc->p2_width = font->tex_size_max;
231         }
232
233         i = (int)((gc->p2_width - (gc->pad * 2)) / gc->glyph_width_max);
234         gc->p2_height = (int)blf_next_p2((unsigned int)(((gc->glyphs_len_max / i) + 1) * gc->glyph_height_max + (gc->pad * 2)));
235
236         if (gc->p2_height > font->tex_size_max) {
237                 gc->p2_height = font->tex_size_max;
238         }
239
240         unsigned char *pixels = MEM_callocN((size_t)gc->p2_width * (size_t)gc->p2_height, "BLF texture init");
241         GPUTexture *tex = GPU_texture_create_nD(gc->p2_width, gc->p2_height, 0, 2, pixels, GPU_R8, GPU_DATA_UNSIGNED_BYTE, 0, false, error);
242         MEM_freeN(pixels);
243         gc->textures[gc->texture_current] = tex;
244         GPU_texture_bind(tex, 0);
245         GPU_texture_wrap_mode(tex, false);
246         GPU_texture_filters(tex, GPU_NEAREST, GPU_LINEAR);
247         GPU_texture_unbind(tex);
248 }
249
250 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
251 {
252         GlyphBLF *p;
253         unsigned int key;
254
255         key = blf_hash(c);
256         p = gc->bucket[key].first;
257         while (p) {
258                 if (p->c == c)
259                         return p;
260                 p = p->next;
261         }
262         return NULL;
263 }
264
265 GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
266 {
267         FT_GlyphSlot slot;
268         GlyphBLF *g;
269         FT_Error err;
270         FT_Bitmap bitmap, tempbitmap;
271         FT_BBox bbox;
272         unsigned int key;
273
274         g = blf_glyph_search(font->glyph_cache, c);
275         if (g)
276                 return g;
277
278         /* glyphs are dynamically created as needed by font rendering. this means that
279          * to make font rendering thread safe we have to do locking here. note that this
280          * must be a lock for the whole library and not just per font, because the font
281          * renderer uses a shared buffer internally */
282         BLI_spin_lock(font->ft_lib_mutex);
283
284         /* search again after locking */
285         g = blf_glyph_search(font->glyph_cache, c);
286         if (g) {
287                 BLI_spin_unlock(font->ft_lib_mutex);
288                 return g;
289         }
290
291         if (font->flags & BLF_MONOCHROME) {
292                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
293         }
294         else {
295                 int flags = FT_LOAD_NO_BITMAP;
296
297                 if (font->flags & BLF_HINTING_NONE) {
298                         flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING;
299                 }
300                 else if (font->flags & BLF_HINTING_SLIGHT) {
301                         flags |= FT_LOAD_TARGET_LIGHT;
302                 }
303                 else if (font->flags & BLF_HINTING_FULL) {
304                         flags |= FT_LOAD_TARGET_NORMAL;
305                 }
306                 else {
307                         /* Default, hinting disabled until FreeType has been upgraded
308                          * to give good results on all platforms. */
309                         flags |= FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING;
310                 }
311
312                 err = FT_Load_Glyph(font->face, (FT_UInt)index, flags);
313         }
314
315         if (err) {
316                 BLI_spin_unlock(font->ft_lib_mutex);
317                 return NULL;
318         }
319
320         /* get the glyph. */
321         slot = font->face->glyph;
322
323         if (font->flags & BLF_MONOCHROME) {
324                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
325
326                 /* Convert result from 1 bit per pixel to 8 bit per pixel */
327                 /* Accum errors for later, fine if not interested beyond "ok vs any error" */
328                 FT_Bitmap_New(&tempbitmap);
329                 err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */
330                 err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
331                 err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
332         }
333         else {
334                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
335         }
336
337         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) {
338                 BLI_spin_unlock(font->ft_lib_mutex);
339                 return NULL;
340         }
341
342         g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
343         g->c = c;
344         g->idx = (FT_UInt)index;
345         g->offset_x = -1;
346         g->offset_y = -1;
347         bitmap = slot->bitmap;
348         g->width = (int)bitmap.width;
349         g->height = (int)bitmap.rows;
350
351         if (g->width && g->height) {
352                 if (font->flags & BLF_MONOCHROME) {
353                         /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
354                         int i;
355                         for (i = 0; i < (g->width * g->height); i++) {
356                                 bitmap.buffer[i] = bitmap.buffer[i] ? 255 : 0;
357                         }
358                 }
359
360                 g->bitmap = (unsigned char *)MEM_mallocN((size_t)g->width * (size_t)g->height, "glyph bitmap");
361                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, (size_t)g->width * (size_t)g->height);
362         }
363
364         g->advance = ((float)slot->advance.x) / 64.0f;
365         g->advance_i = (int)g->advance;
366         g->pos_x = (float)slot->bitmap_left;
367         g->pos_y = (float)slot->bitmap_top;
368         g->pitch = slot->bitmap.pitch;
369
370         FT_Outline_Get_CBox(&(slot->outline), &bbox);
371         g->box.xmin = ((float)bbox.xMin) / 64.0f;
372         g->box.xmax = ((float)bbox.xMax) / 64.0f;
373         g->box.ymin = ((float)bbox.yMin) / 64.0f;
374         g->box.ymax = ((float)bbox.yMax) / 64.0f;
375
376         key = blf_hash(g->c);
377         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
378
379         BLI_spin_unlock(font->ft_lib_mutex);
380
381         return g;
382 }
383
384 void blf_glyph_free(GlyphBLF *g)
385 {
386         /* don't need free the texture, the GlyphCache already
387          * have a list of all the texture and free it.
388          */
389         if (g->bitmap)
390                 MEM_freeN(g->bitmap);
391         MEM_freeN(g);
392 }
393
394 static void blf_texture_draw(const unsigned char color[4], const float uv[2][2], float x1, float y1, float x2, float y2)
395 {
396         /* Only one vertex per glyph, geometry shader expand it into a quad. */
397         /* TODO Get rid of Geom Shader because it's not optimal AT ALL for the GPU */
398         copy_v4_fl4(GPU_vertbuf_raw_step(&g_batch.pos_step), x1 + g_batch.ofs[0], y1 + g_batch.ofs[1],
399                                                              x2 + g_batch.ofs[0], y2 + g_batch.ofs[1]);
400         copy_v4_v4(GPU_vertbuf_raw_step(&g_batch.tex_step), (float *)uv);
401         copy_v4_v4_uchar(GPU_vertbuf_raw_step(&g_batch.col_step), color);
402         g_batch.glyph_len++;
403         /* Flush cache if it's full. */
404         if (g_batch.glyph_len == BLF_BATCH_DRAW_LEN_MAX) {
405                 blf_batch_draw();
406         }
407 }
408
409 static void blf_texture5_draw(const unsigned char color_in[4], int tex_w, int tex_h, const float uv[2][2],
410                               float x1, float y1, float x2, float y2)
411 {
412         float ofs[2] = { 2 / (float)tex_w, 2 / (float)tex_h };
413         float uv_flag[2][2];
414         copy_v4_v4((float *)uv_flag, (float *)uv);
415         /* flag the x and y component signs for 5x5 blurring */
416         uv_flag[0][0] = -(uv_flag[0][0] - ofs[0]);
417         uv_flag[0][1] = -(uv_flag[0][1] - ofs[1]);
418         uv_flag[1][0] = -(uv_flag[1][0] + ofs[0]);
419         uv_flag[1][1] = -(uv_flag[1][1] + ofs[1]);
420
421         blf_texture_draw(color_in, uv_flag, x1 - 2, y1 + 2, x2 + 2, y2 - 2);
422 }
423
424 static void blf_texture3_draw(const unsigned char color_in[4], int tex_w, int tex_h, const float uv[2][2],
425                               float x1, float y1, float x2, float y2)
426 {
427         float ofs[2] = { 1 / (float)tex_w, 1 / (float)tex_h };
428         float uv_flag[2][2];
429         copy_v4_v4((float *)uv_flag, (float *)uv);
430         /* flag the x component sign for 3x3 blurring */
431         uv_flag[0][0] = -(uv_flag[0][0] - ofs[0]);
432         uv_flag[0][1] =  (uv_flag[0][1] - ofs[1]);
433         uv_flag[1][0] = -(uv_flag[1][0] + ofs[0]);
434         uv_flag[1][1] =  (uv_flag[1][1] + ofs[1]);
435
436         blf_texture_draw(color_in, uv_flag, x1 - 1, y1 + 1, x2 + 1, y2 - 1);
437 }
438
439 static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y)
440 {
441         rect->xmin = floorf(x + g->pos_x);
442         rect->xmax = rect->xmin + (float)g->width;
443         rect->ymin = floorf(y + g->pos_y);
444         rect->ymax = rect->ymin - (float)g->height;
445 }
446
447 void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
448 {
449         rctf rect;
450
451         if ((!g->width) || (!g->height))
452                 return;
453
454         if (g->build_tex == 0) {
455                 GlyphCacheBLF *gc = font->glyph_cache;
456
457                 if (font->tex_size_max == -1)
458                         font->tex_size_max = GPU_max_texture_size();
459
460                 if (gc->texture_current == BLF_TEXTURE_UNSET) {
461                         blf_glyph_cache_texture(font, gc);
462                         gc->offset_x = gc->pad;
463                         gc->offset_y = 3; /* enough padding for blur */
464                 }
465
466                 if (gc->offset_x > (gc->p2_width - gc->glyph_width_max)) {
467                         gc->offset_x = gc->pad;
468                         gc->offset_y += gc->glyph_height_max;
469
470                         if (gc->offset_y > (gc->p2_height - gc->glyph_height_max)) {
471                                 gc->offset_y = 3; /* enough padding for blur */
472                                 blf_glyph_cache_texture(font, gc);
473                         }
474                 }
475
476                 g->tex = gc->textures[gc->texture_current];
477                 g->offset_x = gc->offset_x;
478                 g->offset_y = gc->offset_y;
479
480                 /* prevent glTexSubImage2D from failing if the character
481                  * asks for pixels out of bounds, this tends only to happen
482                  * with very small sizes (5px high or less) */
483                 if (UNLIKELY((g->offset_x + g->width)  > gc->p2_width)) {
484                         g->width  -= (g->offset_x + g->width)  - gc->p2_width;
485                         BLI_assert(g->width > 0);
486                 }
487                 if (UNLIKELY((g->offset_y + g->height) > gc->p2_height)) {
488                         g->height -= (g->offset_y + g->height) - gc->p2_height;
489                         BLI_assert(g->height > 0);
490                 }
491
492                 GPU_texture_update_sub(g->tex, GPU_DATA_UNSIGNED_BYTE, g->bitmap, g->offset_x, g->offset_y, 0, g->width, g->height, 0);
493
494                 g->uv[0][0] = ((float)g->offset_x) / ((float)gc->p2_width);
495                 g->uv[0][1] = ((float)g->offset_y) / ((float)gc->p2_height);
496                 g->uv[1][0] = ((float)(g->offset_x + g->width)) / ((float)gc->p2_width);
497                 g->uv[1][1] = ((float)(g->offset_y + g->height)) / ((float)gc->p2_height);
498
499                 /* update the x offset for the next glyph. */
500                 gc->offset_x += (int)BLI_rctf_size_x(&g->box) + gc->pad;
501
502                 gc->glyphs_len_free--;
503                 g->build_tex = 1;
504         }
505
506         blf_glyph_calc_rect(&rect, g, x, y);
507
508         if (font->flags & BLF_CLIPPING) {
509                 /* intentionally check clipping without shadow offset */
510                 rctf rect_test = rect;
511                 BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]);
512
513                 if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) {
514                         return;
515                 }
516         }
517
518         if (font->tex_bind_state != g->tex) {
519                 blf_batch_draw();
520                 font->tex_bind_state = g->tex;
521                 GPU_texture_bind(font->tex_bind_state, 0);
522         }
523
524         g_batch.tex_bind_state = g->tex;
525
526         if (font->flags & BLF_SHADOW) {
527                 rctf rect_ofs;
528                 blf_glyph_calc_rect(&rect_ofs, g,
529                                     x + (float)font->shadow_x,
530                                     y + (float)font->shadow_y);
531
532                 if (font->shadow == 0) {
533                         blf_texture_draw(font->shadow_color, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
534                 }
535                 else if (font->shadow <= 4) {
536                         blf_texture3_draw(font->shadow_color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv,
537                                           rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
538                 }
539                 else {
540                         blf_texture5_draw(font->shadow_color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv,
541                                           rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
542                 }
543         }
544
545 #if BLF_BLUR_ENABLE
546         switch (font->blur) {
547                 case 3:
548                         blf_texture3_draw(font->color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv,
549                                           rect.xmin, rect.ymin, rect.xmax, rect.ymax);
550                         break;
551                 case 5:
552                         blf_texture5_draw(font->color, font->glyph_cache->p2_width, font->glyph_cache->p2_height, g->uv,
553                                           rect.xmin, rect.ymin, rect.xmax, rect.ymax);
554                         break;
555                 default:
556                         blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
557         }
558 #else
559         blf_texture_draw(font->color, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
560 #endif
561 }