Cleanup: use eGPU prefix for GPU enum types
[blender.git] / source / blender / blenfont / intern / blf_font.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_font.c
28  *  \ingroup blf
29  *
30  * Deals with drawing text to OpenGL or bitmap buffers.
31  *
32  * Also low level functions for managing \a FontBLF.
33  */
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <math.h>
40
41 #include <ft2build.h>
42
43 #include FT_FREETYPE_H
44 #include FT_GLYPH_H
45
46 #include "MEM_guardedalloc.h"
47
48 #include "DNA_vec_types.h"
49
50 #include "BLI_listbase.h"
51 #include "BLI_math.h"
52 #include "BLI_rect.h"
53 #include "BLI_string.h"
54 #include "BLI_string_utf8.h"
55 #include "BLI_threads.h"
56 #include "BLI_alloca.h"
57
58 #include "BIF_gl.h"
59 #include "BLF_api.h"
60
61 #include "UI_interface.h"
62
63 #include "GPU_immediate.h"
64 #include "GPU_matrix.h"
65 #include "GPU_batch.h"
66
67 #include "blf_internal_types.h"
68 #include "blf_internal.h"
69
70 #include "BLI_strict_flags.h"
71
72 #ifdef WIN32
73 #  define FT_New_Face FT_New_Face__win32_compat
74 #endif
75
76 /* Batching buffer for drawing. */
77 BatchBLF g_batch;
78
79 /* freetype2 handle ONLY for this file!. */
80 static FT_Library ft_lib;
81 static SpinLock ft_lib_mutex;
82
83 /* -------------------------------------------------------------------- */
84 /** \name Glyph Batching
85  * \{ */
86 /**
87  * Drawcalls are precious! make them count!
88  * Since most of the Text elems are not covered by other UI elements, we can
89  * group some strings together and render them in one drawcall. This behavior
90  * is on demand only, between BLF_batch_start() and BLF_batch_end().
91  **/
92 static void blf_batch_draw_init(void)
93 {
94         GPUVertFormat format = {0};
95         g_batch.pos_loc = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
96         g_batch.tex_loc = GPU_vertformat_attr_add(&format, "tex", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
97         g_batch.col_loc = GPU_vertformat_attr_add(&format, "col", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
98
99         g_batch.verts = GPU_vertbuf_create_with_format_ex(&format, GPU_USAGE_STREAM);
100         GPU_vertbuf_data_alloc(g_batch.verts, BLF_BATCH_DRAW_LEN_MAX);
101
102         GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step);
103         GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.tex_loc, &g_batch.tex_step);
104         GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step);
105         g_batch.glyph_len = 0;
106
107         g_batch.batch = GPU_batch_create_ex(GPU_PRIM_POINTS, g_batch.verts, NULL, GPU_BATCH_OWNS_VBO);
108 }
109
110 static void blf_batch_draw_exit(void)
111 {
112         GPU_BATCH_DISCARD_SAFE(g_batch.batch);
113 }
114
115 void blf_batch_draw_vao_clear(void)
116 {
117         if (g_batch.batch) {
118                 GPU_batch_vao_cache_clear(g_batch.batch);
119         }
120 }
121
122 void blf_batch_draw_begin(FontBLF *font)
123 {
124         if (g_batch.batch == NULL) {
125                 blf_batch_draw_init();
126         }
127
128         const bool font_changed = (g_batch.font != font);
129         const bool simple_shader = ((font->flags & (BLF_ROTATION | BLF_MATRIX | BLF_ASPECT)) == 0);
130         const bool shader_changed = (simple_shader != g_batch.simple_shader);
131
132         g_batch.active = g_batch.enabled && simple_shader;
133
134         if (simple_shader) {
135                 /* Offset is applied to each glyph. */
136                 g_batch.ofs[0] = floorf(font->pos[0]);
137                 g_batch.ofs[1] = floorf(font->pos[1]);
138         }
139         else {
140                 /* Offset is baked in modelview mat. */
141                 zero_v2(g_batch.ofs);
142         }
143
144         if (g_batch.active) {
145                 float gpumat[4][4];
146                 GPU_matrix_model_view_get(gpumat);
147
148                 bool mat_changed = (memcmp(gpumat, g_batch.mat, sizeof(g_batch.mat)) != 0);
149
150                 if (mat_changed) {
151                         /* Modelviewmat is no longer the same.
152                          * Flush cache but with the previous mat. */
153                         GPU_matrix_push();
154                         GPU_matrix_set(g_batch.mat);
155                 }
156
157                 /* flush cache if config is not the same. */
158                 if (mat_changed || font_changed || shader_changed) {
159                         blf_batch_draw();
160                         g_batch.simple_shader = simple_shader;
161                         g_batch.font = font;
162                 }
163                 else {
164                         /* Nothing changed continue batching. */
165                         return;
166                 }
167
168                 if (mat_changed) {
169                         GPU_matrix_pop();
170                         /* Save for next memcmp. */
171                         memcpy(g_batch.mat, gpumat, sizeof(g_batch.mat));
172                 }
173         }
174         else {
175                 /* flush cache */
176                 blf_batch_draw();
177                 g_batch.font = font;
178                 g_batch.simple_shader = simple_shader;
179         }
180 }
181
182 void blf_batch_draw(void)
183 {
184         if (g_batch.glyph_len == 0)
185                 return;
186
187         GPU_blend(true);
188         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
189
190         /* We need to flush widget base first to ensure correct ordering. */
191         UI_widgetbase_draw_cache_flush();
192
193         GPU_texture_bind(g_batch.tex_bind_state, 0);
194         GPU_vertbuf_data_len_set(g_batch.verts, g_batch.glyph_len);
195         GPU_vertbuf_use(g_batch.verts); /* send data */
196
197         eGPUBuiltinShader shader = (g_batch.simple_shader) ? GPU_SHADER_TEXT_SIMPLE : GPU_SHADER_TEXT;
198         GPU_batch_program_set_builtin(g_batch.batch, shader);
199         GPU_batch_uniform_1i(g_batch.batch, "glyph", 0);
200         GPU_batch_draw(g_batch.batch);
201
202         GPU_blend(false);
203
204         /* restart to 1st vertex data pointers */
205         GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.pos_loc, &g_batch.pos_step);
206         GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.tex_loc, &g_batch.tex_step);
207         GPU_vertbuf_attr_get_raw_data(g_batch.verts, g_batch.col_loc, &g_batch.col_step);
208         g_batch.glyph_len = 0;
209 }
210
211 static void blf_batch_draw_end(void)
212 {
213         if (!g_batch.active) {
214                 blf_batch_draw();
215         }
216 }
217
218 /** \} */
219
220 /* -------------------------------------------------------------------- */
221
222 int blf_font_init(void)
223 {
224         memset(&g_batch, 0, sizeof(g_batch));
225         BLI_spin_init(&ft_lib_mutex);
226         return FT_Init_FreeType(&ft_lib);
227 }
228
229 void blf_font_exit(void)
230 {
231         FT_Done_FreeType(ft_lib);
232         BLI_spin_end(&ft_lib_mutex);
233         blf_batch_draw_exit();
234 }
235
236 void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
237 {
238         GlyphCacheBLF *gc;
239         FT_Error err;
240
241         gc = blf_glyph_cache_find(font, size, dpi);
242         if (gc) {
243                 font->glyph_cache = gc;
244                 /* Optimization: do not call FT_Set_Char_Size if size did not change. */
245                 if (font->size == size && font->dpi == dpi)
246                         return;
247         }
248
249         err = FT_Set_Char_Size(font->face, 0, (FT_F26Dot6)(size * 64), dpi, dpi);
250         if (err) {
251                 /* FIXME: here we can go through the fixed size and choice a close one */
252                 printf("The current font don't support the size, %u and dpi, %u\n", size, dpi);
253                 return;
254         }
255
256         font->size = size;
257         font->dpi = dpi;
258
259         if (!gc) {
260                 gc = blf_glyph_cache_new(font);
261                 if (gc)
262                         font->glyph_cache = gc;
263                 else
264                         font->glyph_cache = NULL;
265         }
266 }
267
268 static void blf_font_ensure_ascii_table(FontBLF *font)
269 {
270         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
271
272         /* build ascii on demand */
273         if (glyph_ascii_table['0'] == NULL) {
274                 GlyphBLF *g;
275                 unsigned int i;
276                 for (i = 0; i < 256; i++) {
277                         g = blf_glyph_search(font->glyph_cache, i);
278                         if (!g) {
279                                 FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
280                                 g = blf_glyph_add(font, glyph_index, i);
281                         }
282                         glyph_ascii_table[i] = g;
283                 }
284         }
285 }
286
287 static void blf_font_ensure_ascii_kerning(FontBLF *font, const FT_UInt kern_mode)
288 {
289         KerningCacheBLF *kc = font->kerning_cache;
290
291         font->kerning_mode = kern_mode;
292
293         if (!kc || kc->mode != kern_mode) {
294                 font->kerning_cache = kc = blf_kerning_cache_find(font);
295                 if (!kc) {
296                         font->kerning_cache = kc = blf_kerning_cache_new(font);
297                 }
298         }
299 }
300
301 /* Fast path for runs of ASCII characters. Given that common UTF-8
302  * input will consist of an overwhelming majority of ASCII
303  * characters.
304  */
305
306 /* Note,
307  * blf_font_ensure_ascii_table(font); must be called before this macro */
308
309 #define BLF_UTF8_NEXT_FAST(_font, _g, _str, _i, _c, _glyph_ascii_table)          \
310         if (((_c) = (_str)[_i]) < 0x80) {                                            \
311                 _g = (_glyph_ascii_table)[_c];                                           \
312                 _i++;                                                                    \
313         }                                                                            \
314         else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \
315                 if ((_g = blf_glyph_search((_font)->glyph_cache, _c)) == NULL) {         \
316                         _g = blf_glyph_add(_font,                                            \
317                                           FT_Get_Char_Index((_font)->face, _c), _c);         \
318                 }                                                                        \
319         } (void)0
320
321
322 #define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode)                        \
323         const bool _has_kerning = FT_HAS_KERNING((_font)->face) != 0;                \
324         const FT_UInt _kern_mode = (_has_kerning == 0) ? 0 :                         \
325                                  (((_font)->flags & BLF_KERNING_DEFAULT) ?           \
326                                   ft_kerning_default : (FT_UInt)FT_KERNING_UNFITTED) \
327
328 /* Note,
329  * blf_font_ensure_ascii_kerning(font, kern_mode); must be called before this macro */
330
331 #define BLF_KERNING_STEP_FAST(_font, _kern_mode, _g_prev, _g, _c_prev, _c, _pen_x) \
332 {                                                                                \
333         if (_g_prev) {                                                               \
334                 FT_Vector _delta;                                                        \
335                 if (_c_prev < 0x80 && _c < 0x80) {                                       \
336                         _pen_x += (_font)->kerning_cache->table[_c][_c_prev];                 \
337                 }                                                                        \
338                 else if (FT_Get_Kerning((_font)->face,                                   \
339                                         (_g_prev)->idx,                                  \
340                                         (_g)->idx,                                       \
341                                         _kern_mode,                                      \
342                                         &(_delta)) == 0)                                 \
343                 {                                                                        \
344                         _pen_x += (int)_delta.x >> 6;                                        \
345                 }                                                                        \
346         }                                                                            \
347 } (void)0
348
349 #define BLF_KERNING_STEP(_font, _kern_mode, _g_prev, _g, _delta, _pen_x)         \
350 {                                                                                \
351         if (_g_prev) {                                                               \
352                 _delta.x = _delta.y = 0;                                                 \
353                 if (FT_Get_Kerning((_font)->face,                                        \
354                                    (_g_prev)->idx,                                       \
355                                    (_g)->idx,                                            \
356                                    _kern_mode,                                           \
357                                    &(_delta)) == 0)                                      \
358                 {                                                                        \
359                         _pen_x += (int)_delta.x >> 6;                                        \
360                 }                                                                        \
361         }                                                                            \
362 } (void)0
363
364 static void blf_font_draw_ex(
365         FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info,
366         int pen_y)
367 {
368         unsigned int c, c_prev = BLI_UTF8_ERR;
369         GlyphBLF *g, *g_prev = NULL;
370         int pen_x = 0;
371         size_t i = 0;
372         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
373
374         if (len == 0) {
375                 /* early output, don't do any IMM OpenGL. */
376                 return;
377         }
378
379         BLF_KERNING_VARS(font, has_kerning, kern_mode);
380
381         blf_font_ensure_ascii_table(font);
382         blf_font_ensure_ascii_kerning(font, kern_mode);
383
384         blf_batch_draw_begin(font);
385
386         while ((i < len) && str[i]) {
387                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
388
389                 if (UNLIKELY(c == BLI_UTF8_ERR))
390                         break;
391                 if (UNLIKELY(g == NULL))
392                         continue;
393                 if (has_kerning)
394                         BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x);
395
396                 /* do not return this loop if clipped, we want every character tested */
397                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
398
399                 pen_x += g->advance_i;
400                 g_prev = g;
401                 c_prev = c;
402         }
403
404         blf_batch_draw_end();
405
406         if (r_info) {
407                 r_info->lines = 1;
408                 r_info->width = pen_x;
409         }
410 }
411 void blf_font_draw(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
412 {
413         blf_font_draw_ex(font, str, len, r_info, 0);
414 }
415
416 /* faster version of blf_font_draw, ascii only for view dimensions */
417 static void blf_font_draw_ascii_ex(
418         FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info,
419         int pen_y)
420 {
421         unsigned int c, c_prev = BLI_UTF8_ERR;
422         GlyphBLF *g, *g_prev = NULL;
423         int pen_x = 0;
424         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
425
426         BLF_KERNING_VARS(font, has_kerning, kern_mode);
427
428         blf_font_ensure_ascii_table(font);
429         blf_font_ensure_ascii_kerning(font, kern_mode);
430
431         blf_batch_draw_begin(font);
432
433         while ((c = *(str++)) && len--) {
434                 BLI_assert(c < 128);
435                 if ((g = glyph_ascii_table[c]) == NULL)
436                         continue;
437                 if (has_kerning)
438                         BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x);
439
440                 /* do not return this loop if clipped, we want every character tested */
441                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
442
443                 pen_x += g->advance_i;
444                 g_prev = g;
445                 c_prev = c;
446         }
447
448         blf_batch_draw_end();
449
450         if (r_info) {
451                 r_info->lines = 1;
452                 r_info->width = pen_x;
453         }
454 }
455 void blf_font_draw_ascii(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
456 {
457         blf_font_draw_ascii_ex(font, str, len, r_info, 0);
458 }
459
460 /* use fixed column width, but an utf8 character may occupy multiple columns */
461 int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth)
462 {
463         unsigned int c;
464         GlyphBLF *g;
465         int col, columns = 0;
466         int pen_x = 0, pen_y = 0;
467         size_t i = 0;
468         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
469
470         blf_font_ensure_ascii_table(font);
471
472         blf_batch_draw_begin(font);
473
474         while ((i < len) && str[i]) {
475                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
476
477                 if (UNLIKELY(c == BLI_UTF8_ERR))
478                         break;
479                 if (UNLIKELY(g == NULL))
480                         continue;
481
482                 /* do not return this loop if clipped, we want every character tested */
483                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
484
485                 col = BLI_wcwidth((wchar_t)c);
486                 if (col < 0)
487                         col = 1;
488
489                 columns += col;
490                 pen_x += cwidth * col;
491         }
492
493         blf_batch_draw_end();
494
495         return columns;
496 }
497
498 /* Sanity checks are done by BLF_draw_buffer() */
499 static void blf_font_draw_buffer_ex(
500         FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info,
501         int pen_y)
502 {
503         unsigned int c, c_prev = BLI_UTF8_ERR;
504         GlyphBLF *g, *g_prev = NULL;
505         int pen_x = (int)font->pos[0];
506         int pen_y_basis = (int)font->pos[1] + pen_y;
507         size_t i = 0;
508         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
509
510         /* buffer specific vars */
511         FontBufInfoBLF *buf_info = &font->buf_info;
512         const float *b_col_float = buf_info->col_float;
513         const unsigned char *b_col_char = buf_info->col_char;
514         int chx, chy;
515         int y, x;
516
517         BLF_KERNING_VARS(font, has_kerning, kern_mode);
518
519         blf_font_ensure_ascii_table(font);
520         blf_font_ensure_ascii_kerning(font, kern_mode);
521
522         /* another buffer specific call for color conversion */
523
524         while ((i < len) && str[i]) {
525                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
526
527                 if (UNLIKELY(c == BLI_UTF8_ERR))
528                         break;
529                 if (UNLIKELY(g == NULL))
530                         continue;
531                 if (has_kerning)
532                         BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x);
533
534                 chx = pen_x + ((int)g->pos_x);
535                 chy = pen_y_basis + g->height;
536
537                 if (g->pitch < 0) {
538                         pen_y = pen_y_basis + (g->height - (int)g->pos_y);
539                 }
540                 else {
541                         pen_y = pen_y_basis - (g->height - (int)g->pos_y);
542                 }
543
544                 if ((chx + g->width) >= 0 && chx < buf_info->w && (pen_y + g->height) >= 0 && pen_y < buf_info->h) {
545                         /* don't draw beyond the buffer bounds */
546                         int width_clip = g->width;
547                         int height_clip = g->height;
548                         int yb_start = g->pitch < 0 ? 0 : g->height - 1;
549
550                         if (width_clip + chx > buf_info->w)
551                                 width_clip -= chx + width_clip - buf_info->w;
552                         if (height_clip + pen_y > buf_info->h)
553                                 height_clip -= pen_y + height_clip - buf_info->h;
554
555                         /* drawing below the image? */
556                         if (pen_y < 0) {
557                                 yb_start += (g->pitch < 0) ? -pen_y : pen_y;
558                                 height_clip += pen_y;
559                                 pen_y = 0;
560                         }
561
562                         if (buf_info->fbuf) {
563                                 int yb = yb_start;
564                                 for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) {
565                                         for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) {
566                                                 const char a_byte = *(g->bitmap + x + (yb * g->pitch));
567                                                 if (a_byte) {
568                                                         const float a = (a_byte / 255.0f) * b_col_float[3];
569                                                         const size_t buf_ofs = (
570                                                                 ((size_t)(chx + x) + ((size_t)(pen_y + y) * (size_t)buf_info->w)) *
571                                                                 (size_t)buf_info->ch);
572                                                         float *fbuf = buf_info->fbuf + buf_ofs;
573
574                                                         if (a >= 1.0f) {
575                                                                 fbuf[0] = b_col_float[0];
576                                                                 fbuf[1] = b_col_float[1];
577                                                                 fbuf[2] = b_col_float[2];
578                                                                 fbuf[3] = 1.0f;
579                                                         }
580                                                         else {
581                                                                 fbuf[0] = (b_col_float[0] * a) + (fbuf[0] * (1.0f - a));
582                                                                 fbuf[1] = (b_col_float[1] * a) + (fbuf[1] * (1.0f - a));
583                                                                 fbuf[2] = (b_col_float[2] * a) + (fbuf[2] * (1.0f - a));
584                                                                 fbuf[3] = MIN2(fbuf[3] + a, 1.0f); /* clamp to 1.0 */
585                                                         }
586                                                 }
587                                         }
588
589                                         if (g->pitch < 0)
590                                                 yb++;
591                                         else
592                                                 yb--;
593                                 }
594                         }
595
596                         if (buf_info->cbuf) {
597                                 int yb = yb_start;
598                                 for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) {
599                                         for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) {
600                                                 const char a_byte = *(g->bitmap + x + (yb * g->pitch));
601
602                                                 if (a_byte) {
603                                                         const float a = (a_byte / 255.0f) * b_col_float[3];
604                                                         const size_t buf_ofs = (
605                                                                 ((size_t)(chx + x) + ((size_t)(pen_y + y) * (size_t)buf_info->w)) *
606                                                                 (size_t)buf_info->ch);
607                                                         unsigned char *cbuf = buf_info->cbuf + buf_ofs;
608
609                                                         if (a >= 1.0f) {
610                                                                 cbuf[0] = b_col_char[0];
611                                                                 cbuf[1] = b_col_char[1];
612                                                                 cbuf[2] = b_col_char[2];
613                                                                 cbuf[3] = 255;
614                                                         }
615                                                         else {
616                                                                 cbuf[0] = (unsigned char)((b_col_char[0] * a) + (cbuf[0] * (1.0f - a)));
617                                                                 cbuf[1] = (unsigned char)((b_col_char[1] * a) + (cbuf[1] * (1.0f - a)));
618                                                                 cbuf[2] = (unsigned char)((b_col_char[2] * a) + (cbuf[2] * (1.0f - a)));
619                                                                 cbuf[3] = (unsigned char)MIN2((int)cbuf[3] + (int)(a * 255), 255); /* clamp to 255 */
620                                                         }
621                                                 }
622                                         }
623
624                                         if (g->pitch < 0)
625                                                 yb++;
626                                         else
627                                                 yb--;
628                                 }
629                         }
630                 }
631
632                 pen_x += g->advance_i;
633                 g_prev = g;
634                 c_prev = c;
635         }
636
637         if (r_info) {
638                 r_info->lines = 1;
639                 r_info->width = pen_x;
640         }
641 }
642 void blf_font_draw_buffer(
643         FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
644 {
645         blf_font_draw_buffer_ex(font, str, len, r_info, 0);
646 }
647
648 static bool blf_font_width_to_strlen_glyph_process(
649         FontBLF *font, const bool has_kerning, const FT_UInt kern_mode,
650         const uint c_prev, const uint c, GlyphBLF *g_prev, GlyphBLF *g,
651         int *pen_x, const int width_i)
652 {
653         if (UNLIKELY(c == BLI_UTF8_ERR)) {
654                 return true;  /* break the calling loop. */
655         }
656         if (UNLIKELY(g == NULL)) {
657                 return false;  /* continue the calling loop. */
658         }
659         if (has_kerning) {
660                 BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, *pen_x);
661         }
662
663         *pen_x += g->advance_i;
664
665         return (*pen_x >= width_i);
666 }
667
668 size_t blf_font_width_to_strlen(FontBLF *font, const char *str, size_t len, float width, float *r_width)
669 {
670         unsigned int c, c_prev;
671         GlyphBLF *g, *g_prev;
672         int pen_x, width_new;
673         size_t i, i_prev;
674         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
675         const int width_i = (int)width;
676
677         BLF_KERNING_VARS(font, has_kerning, kern_mode);
678
679         blf_font_ensure_ascii_table(font);
680         if (has_kerning) {
681                 blf_font_ensure_ascii_kerning(font, kern_mode);
682         }
683
684         for (i_prev = i = 0, width_new = pen_x = 0, g_prev = NULL, c_prev = 0;
685              (i < len) && str[i];
686              i_prev = i, width_new = pen_x, c_prev = c, g_prev = g)
687         {
688                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
689
690                 if (blf_font_width_to_strlen_glyph_process(
691                         font, has_kerning, kern_mode,
692                         c_prev, c, g_prev, g,
693                         &pen_x, width_i))
694                 {
695                         break;
696                 }
697         }
698
699         if (r_width) {
700                 *r_width = (float)width_new;
701         }
702
703         return i_prev;
704 }
705
706 size_t blf_font_width_to_rstrlen(FontBLF *font, const char *str, size_t len, float width, float *r_width)
707 {
708         unsigned int c, c_prev;
709         GlyphBLF *g, *g_prev;
710         int pen_x, width_new;
711         size_t i, i_prev, i_tmp;
712         char *s, *s_prev;
713         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
714         const int width_i = (int)width;
715
716         BLF_KERNING_VARS(font, has_kerning, kern_mode);
717
718         blf_font_ensure_ascii_table(font);
719         if (has_kerning) {
720                 blf_font_ensure_ascii_kerning(font, kern_mode);
721         }
722
723         i = BLI_strnlen(str, len);
724         s = BLI_str_find_prev_char_utf8(str, &str[i]);
725         i = (size_t)((s != NULL) ? s - str : 0);
726         s_prev = BLI_str_find_prev_char_utf8(str, s);
727         i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0);
728
729         i_tmp = i;
730         BLF_UTF8_NEXT_FAST(font, g, str, i_tmp, c, glyph_ascii_table);
731         for (width_new = pen_x = 0;
732              (s != NULL);
733              i = i_prev, s = s_prev, c = c_prev, g = g_prev, g_prev = NULL, width_new = pen_x)
734         {
735                 s_prev = BLI_str_find_prev_char_utf8(str, s);
736                 i_prev = (size_t)((s_prev != NULL) ? s_prev - str : 0);
737
738                 if (s_prev != NULL) {
739                         i_tmp = i_prev;
740                         BLF_UTF8_NEXT_FAST(font, g_prev, str, i_tmp, c_prev, glyph_ascii_table);
741                         BLI_assert(i_tmp == i);
742                 }
743
744                 if (blf_font_width_to_strlen_glyph_process(
745                         font, has_kerning, kern_mode,
746                         c_prev, c, g_prev, g,
747                         &pen_x, width_i))
748                 {
749                         break;
750                 }
751         }
752
753         if (r_width) {
754                 *r_width = (float)width_new;
755         }
756
757         return i;
758 }
759
760 static void blf_font_boundbox_ex(
761         FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info,
762         int pen_y)
763 {
764         unsigned int c, c_prev = BLI_UTF8_ERR;
765         GlyphBLF *g, *g_prev = NULL;
766         int pen_x = 0;
767         size_t i = 0;
768         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
769
770         rctf gbox;
771
772         BLF_KERNING_VARS(font, has_kerning, kern_mode);
773
774         box->xmin = 32000.0f;
775         box->xmax = -32000.0f;
776         box->ymin = 32000.0f;
777         box->ymax = -32000.0f;
778
779         blf_font_ensure_ascii_table(font);
780         blf_font_ensure_ascii_kerning(font, kern_mode);
781
782         while ((i < len) && str[i]) {
783                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
784
785                 if (UNLIKELY(c == BLI_UTF8_ERR))
786                         break;
787                 if (UNLIKELY(g == NULL))
788                         continue;
789                 if (has_kerning)
790                         BLF_KERNING_STEP_FAST(font, kern_mode, g_prev, g, c_prev, c, pen_x);
791
792                 gbox.xmin = (float)pen_x;
793                 gbox.xmax = (float)pen_x + g->advance;
794                 gbox.ymin = g->box.ymin + (float)pen_y;
795                 gbox.ymax = g->box.ymax + (float)pen_y;
796
797                 if (gbox.xmin < box->xmin) box->xmin = gbox.xmin;
798                 if (gbox.ymin < box->ymin) box->ymin = gbox.ymin;
799
800                 if (gbox.xmax > box->xmax) box->xmax = gbox.xmax;
801                 if (gbox.ymax > box->ymax) box->ymax = gbox.ymax;
802
803                 pen_x += g->advance_i;
804                 g_prev = g;
805                 c_prev = c;
806         }
807
808         if (box->xmin > box->xmax) {
809                 box->xmin = 0.0f;
810                 box->ymin = 0.0f;
811                 box->xmax = 0.0f;
812                 box->ymax = 0.0f;
813         }
814
815         if (r_info) {
816                 r_info->lines = 1;
817                 r_info->width = pen_x;
818         }
819 }
820 void blf_font_boundbox(FontBLF *font, const char *str, size_t len, rctf *r_box, struct ResultBLF *r_info)
821 {
822         blf_font_boundbox_ex(font, str, len, r_box, r_info, 0);
823 }
824
825
826 /* -------------------------------------------------------------------- */
827 /** \name Word-Wrap Support
828  * \{ */
829
830
831 /**
832  * Generic function to add word-wrap support for other existing functions.
833  *
834  * Wraps on spaces and respects newlines.
835  * Intentionally ignores non-unix newlines, tabs and more advanced text formatting.
836  *
837  * \note If we want rich text - we better have a higher level API to handle that
838  * (color, bold, switching fonts... etc).
839  */
840 static void blf_font_wrap_apply(
841         FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info,
842         void (*callback)(FontBLF *font, const char *str, size_t len, int pen_y, void *userdata),
843         void *userdata)
844 {
845         unsigned int c;
846         GlyphBLF *g, *g_prev = NULL;
847         FT_Vector delta;
848         int pen_x = 0, pen_y = 0;
849         size_t i = 0;
850         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
851         int lines = 0;
852         int pen_x_next = 0;
853
854         BLF_KERNING_VARS(font, has_kerning, kern_mode);
855
856         struct WordWrapVars {
857                 int wrap_width;
858                 size_t start, last[2];
859         } wrap = {font->wrap_width != -1 ? font->wrap_width : INT_MAX, 0, {0, 0}};
860
861         blf_font_ensure_ascii_table(font);
862         // printf("%s wrapping (%d, %d) `%s`:\n", __func__, len, strlen(str), str);
863         while ((i < len) && str[i]) {
864
865                 /* wrap vars */
866                 size_t i_curr = i;
867                 bool do_draw = false;
868
869                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
870
871                 if (UNLIKELY(c == BLI_UTF8_ERR))
872                         break;
873                 if (UNLIKELY(g == NULL))
874                         continue;
875                 if (has_kerning)
876                         BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
877
878                 /**
879                  * Implementation Detail (utf8).
880                  *
881                  * Take care with single byte offsets here,
882                  * since this is utf8 we can't be sure a single byte is a single character.
883                  *
884                  * This is _only_ done when we know for sure the character is ascii (newline or a space).
885                  */
886                 pen_x_next = pen_x + g->advance_i;
887                 if (UNLIKELY((pen_x_next >= wrap.wrap_width) && (wrap.start != wrap.last[0]))) {
888                         do_draw = true;
889                 }
890                 else if (UNLIKELY(((i < len) && str[i]) == 0)) {
891                         /* need check here for trailing newline, else we draw it */
892                         wrap.last[0] = i + ((g->c != '\n') ? 1 : 0);
893                         wrap.last[1] = i;
894                         do_draw = true;
895                 }
896                 else if (UNLIKELY(g->c == '\n')) {
897                         wrap.last[0] = i_curr + 1;
898                         wrap.last[1] = i;
899                         do_draw = true;
900                 }
901                 else if (UNLIKELY(g->c != ' ' && (g_prev ? g_prev->c == ' ' : false))) {
902                         wrap.last[0] = i_curr;
903                         wrap.last[1] = i_curr;
904                 }
905
906                 if (UNLIKELY(do_draw)) {
907                         // printf("(%03d..%03d)  `%.*s`\n",
908                         //        wrap.start, wrap.last[0], (wrap.last[0] - wrap.start) - 1, &str[wrap.start]);
909
910                         callback(font, &str[wrap.start], (wrap.last[0] - wrap.start) - 1, pen_y, userdata);
911                         wrap.start = wrap.last[0];
912                         i = wrap.last[1];
913                         pen_x = 0;
914                         pen_y -= font->glyph_cache->glyph_height_max;
915                         g_prev = NULL;
916                         lines += 1;
917                         continue;
918                 }
919
920                 pen_x = pen_x_next;
921                 g_prev = g;
922         }
923
924         // printf("done! lines: %d, width, %d\n", lines, pen_x_next);
925
926         if (r_info) {
927                 r_info->lines = lines;
928                 /* width of last line only (with wrapped lines) */
929                 r_info->width = pen_x_next;
930         }
931 }
932
933 /* blf_font_draw__wrap */
934 static void blf_font_draw__wrap_cb(FontBLF *font, const char *str, size_t len, int pen_y, void *UNUSED(userdata))
935 {
936         blf_font_draw_ex(font, str, len, NULL, pen_y);
937 }
938 void blf_font_draw__wrap(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
939 {
940         blf_font_wrap_apply(font, str, len, r_info, blf_font_draw__wrap_cb, NULL);
941 }
942
943 /* blf_font_boundbox__wrap */
944 static void blf_font_boundbox_wrap_cb(FontBLF *font, const char *str, size_t len, int pen_y, void *userdata)
945 {
946         rctf *box = userdata;
947         rctf box_single;
948
949         blf_font_boundbox_ex(font, str, len, &box_single, NULL, pen_y);
950         BLI_rctf_union(box, &box_single);
951 }
952 void blf_font_boundbox__wrap(FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info)
953 {
954         box->xmin = 32000.0f;
955         box->xmax = -32000.0f;
956         box->ymin = 32000.0f;
957         box->ymax = -32000.0f;
958
959         blf_font_wrap_apply(font, str, len, r_info, blf_font_boundbox_wrap_cb, box);
960 }
961
962 /* blf_font_draw_buffer__wrap */
963 static void blf_font_draw_buffer__wrap_cb(FontBLF *font, const char *str, size_t len, int pen_y, void *UNUSED(userdata))
964 {
965         blf_font_draw_buffer_ex(font, str, len, NULL, pen_y);
966 }
967 void blf_font_draw_buffer__wrap(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
968 {
969         blf_font_wrap_apply(font, str, len, r_info, blf_font_draw_buffer__wrap_cb, NULL);
970 }
971
972 /** \} */
973
974
975 void blf_font_width_and_height(
976         FontBLF *font, const char *str, size_t len,
977         float *r_width, float *r_height, struct ResultBLF *r_info)
978 {
979         float xa, ya;
980         rctf box;
981
982         if (font->flags & BLF_ASPECT) {
983                 xa = font->aspect[0];
984                 ya = font->aspect[1];
985         }
986         else {
987                 xa = 1.0f;
988                 ya = 1.0f;
989         }
990
991         if (font->flags & BLF_WORD_WRAP) {
992                 blf_font_boundbox__wrap(font, str, len, &box, r_info);
993         }
994         else {
995                 blf_font_boundbox(font, str, len, &box, r_info);
996         }
997         *r_width  = (BLI_rctf_size_x(&box) * xa);
998         *r_height = (BLI_rctf_size_y(&box) * ya);
999 }
1000
1001 float blf_font_width(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
1002 {
1003         float xa;
1004         rctf box;
1005
1006         if (font->flags & BLF_ASPECT)
1007                 xa = font->aspect[0];
1008         else
1009                 xa = 1.0f;
1010
1011         if (font->flags & BLF_WORD_WRAP) {
1012                 blf_font_boundbox__wrap(font, str, len, &box, r_info);
1013         }
1014         else {
1015                 blf_font_boundbox(font, str, len, &box, r_info);
1016         }
1017         return BLI_rctf_size_x(&box) * xa;
1018 }
1019
1020 float blf_font_height(FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
1021 {
1022         float ya;
1023         rctf box;
1024
1025         if (font->flags & BLF_ASPECT)
1026                 ya = font->aspect[1];
1027         else
1028                 ya = 1.0f;
1029
1030         if (font->flags & BLF_WORD_WRAP) {
1031                 blf_font_boundbox__wrap(font, str, len, &box, r_info);
1032         }
1033         else {
1034                 blf_font_boundbox(font, str, len, &box, r_info);
1035         }
1036         return BLI_rctf_size_y(&box) * ya;
1037 }
1038
1039 float blf_font_fixed_width(FontBLF *font)
1040 {
1041         const unsigned int c = ' ';
1042         GlyphBLF *g = blf_glyph_search(font->glyph_cache, c);
1043         if (!g) {
1044                 g = blf_glyph_add(font, FT_Get_Char_Index(font->face, c), c);
1045
1046                 /* if we don't find the glyph. */
1047                 if (!g) {
1048                         return 0.0f;
1049                 }
1050         }
1051
1052         return g->advance;
1053 }
1054
1055 int blf_font_count_missing_chars(FontBLF *font, const char *str, const size_t len, int *r_tot_chars)
1056 {
1057         int missing = 0;
1058         size_t i = 0;
1059
1060         *r_tot_chars = 0;
1061         while (i < len) {
1062                 unsigned int c;
1063
1064                 if ((c = str[i]) < 0x80) {
1065                         i++;
1066                 }
1067                 else if ((c = BLI_str_utf8_as_unicode_step(str, &i)) != BLI_UTF8_ERR) {
1068                         if (FT_Get_Char_Index((font)->face, c) == 0) {
1069                                 missing++;
1070                         }
1071                 }
1072                 (*r_tot_chars)++;
1073         }
1074         return missing;
1075 }
1076
1077 void blf_font_free(FontBLF *font)
1078 {
1079         GlyphCacheBLF *gc;
1080
1081         font->glyph_cache = NULL;
1082         while ((gc = BLI_pophead(&font->cache))) {
1083                 blf_glyph_cache_free(gc);
1084         }
1085
1086         blf_kerning_cache_clear(font);
1087
1088         FT_Done_Face(font->face);
1089         if (font->filename)
1090                 MEM_freeN(font->filename);
1091         if (font->name)
1092                 MEM_freeN(font->name);
1093         MEM_freeN(font);
1094 }
1095
1096 static void blf_font_fill(FontBLF *font)
1097 {
1098         font->aspect[0] = 1.0f;
1099         font->aspect[1] = 1.0f;
1100         font->aspect[2] = 1.0f;
1101         font->pos[0] = 0.0f;
1102         font->pos[1] = 0.0f;
1103         font->angle = 0.0f;
1104
1105         for (int i = 0; i < 16; i++)
1106                 font->m[i] = 0;
1107
1108         /* annoying bright color so we can see where to add BLF_color calls */
1109         font->color[0] = 255;
1110         font->color[1] = 255;
1111         font->color[2] = 0;
1112         font->color[3] = 255;
1113
1114         font->clip_rec.xmin = 0.0f;
1115         font->clip_rec.xmax = 0.0f;
1116         font->clip_rec.ymin = 0.0f;
1117         font->clip_rec.ymax = 0.0f;
1118         font->flags = 0;
1119         font->dpi = 0;
1120         font->size = 0;
1121         BLI_listbase_clear(&font->cache);
1122         BLI_listbase_clear(&font->kerning_caches);
1123         font->glyph_cache = NULL;
1124         font->kerning_cache = NULL;
1125 #if BLF_BLUR_ENABLE
1126         font->blur = 0;
1127 #endif
1128         font->tex_size_max = -1;
1129
1130         font->buf_info.fbuf = NULL;
1131         font->buf_info.cbuf = NULL;
1132         font->buf_info.w = 0;
1133         font->buf_info.h = 0;
1134         font->buf_info.ch = 0;
1135         font->buf_info.col_init[0] = 0;
1136         font->buf_info.col_init[1] = 0;
1137         font->buf_info.col_init[2] = 0;
1138         font->buf_info.col_init[3] = 0;
1139
1140         font->ft_lib = ft_lib;
1141         font->ft_lib_mutex = &ft_lib_mutex;
1142 }
1143
1144 FontBLF *blf_font_new(const char *name, const char *filename)
1145 {
1146         FontBLF *font;
1147         FT_Error err;
1148         char *mfile;
1149
1150         font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new");
1151         err = FT_New_Face(ft_lib, filename, 0, &font->face);
1152         if (err) {
1153                 MEM_freeN(font);
1154                 return NULL;
1155         }
1156
1157         err = FT_Select_Charmap(font->face, ft_encoding_unicode);
1158         if (err) {
1159                 printf("Can't set the unicode character map!\n");
1160                 FT_Done_Face(font->face);
1161                 MEM_freeN(font);
1162                 return NULL;
1163         }
1164
1165         mfile = blf_dir_metrics_search(filename);
1166         if (mfile) {
1167                 err = FT_Attach_File(font->face, mfile);
1168                 if (err) {
1169                         fprintf(stderr, "FT_Attach_File failed to load '%s' with error %d\n", filename, (int)err);
1170                 }
1171                 MEM_freeN(mfile);
1172         }
1173
1174         font->name = BLI_strdup(name);
1175         font->filename = BLI_strdup(filename);
1176         blf_font_fill(font);
1177         return font;
1178 }
1179
1180 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
1181 {
1182         FT_Open_Args open;
1183
1184         open.flags = FT_OPEN_MEMORY;
1185         open.memory_base = (const FT_Byte *)mem;
1186         open.memory_size = mem_size;
1187         FT_Attach_Stream(font->face, &open);
1188 }
1189
1190 FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, int mem_size)
1191 {
1192         FontBLF *font;
1193         FT_Error err;
1194
1195         font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new_from_mem");
1196         err = FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
1197         if (err) {
1198                 MEM_freeN(font);
1199                 return NULL;
1200         }
1201
1202         err = FT_Select_Charmap(font->face, ft_encoding_unicode);
1203         if (err) {
1204                 printf("Can't set the unicode character map!\n");
1205                 FT_Done_Face(font->face);
1206                 MEM_freeN(font);
1207                 return NULL;
1208         }
1209
1210         font->name = BLI_strdup(name);
1211         font->filename = NULL;
1212         blf_font_fill(font);
1213         return font;
1214 }