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