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