use assert to check for incorrect use of BLF_draw_ascii()
[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
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include <ft2build.h>
38
39 #include FT_FREETYPE_H
40 #include FT_GLYPH_H
41
42 #include "MEM_guardedalloc.h"
43
44 #include "DNA_vec_types.h"
45
46 #include "BLI_listbase.h"
47 #include "BLI_math.h"
48 #include "BLI_rect.h"
49 #include "BLI_string.h"
50 #include "BLI_string_utf8.h"
51 #include "BLI_threads.h"
52 #include "BLI_linklist.h"  /* linknode */
53 #include "BLI_strict_flags.h"
54
55 #include "BIF_gl.h"
56 #include "BLF_api.h"
57
58 #include "IMB_colormanagement.h"
59
60 #include "blf_internal_types.h"
61 #include "blf_internal.h"
62
63 /* freetype2 handle ONLY for this file!. */
64 static FT_Library ft_lib;
65 static SpinLock ft_lib_mutex;
66
67 int blf_font_init(void)
68 {
69         BLI_spin_init(&ft_lib_mutex);
70         return FT_Init_FreeType(&ft_lib);
71 }
72
73 void blf_font_exit(void)
74 {
75         FT_Done_FreeType(ft_lib);
76         BLI_spin_end(&ft_lib_mutex);
77 }
78
79 void blf_font_size(FontBLF *font, unsigned int size, unsigned int dpi)
80 {
81         GlyphCacheBLF *gc;
82         FT_Error err;
83
84         err = FT_Set_Char_Size(font->face, 0, (FT_F26Dot6)(size * 64), dpi, dpi);
85         if (err) {
86                 /* FIXME: here we can go through the fixed size and choice a close one */
87                 printf("The current font don't support the size, %u and dpi, %u\n", size, dpi);
88                 return;
89         }
90
91         font->size = size;
92         font->dpi = dpi;
93
94         gc = blf_glyph_cache_find(font, size, dpi);
95         if (gc)
96                 font->glyph_cache = gc;
97         else {
98                 gc = blf_glyph_cache_new(font);
99                 if (gc)
100                         font->glyph_cache = gc;
101                 else
102                         font->glyph_cache = NULL;
103         }
104 }
105
106 static void blf_font_ensure_ascii_table(FontBLF *font)
107 {
108         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
109
110         /* build ascii on demand */
111         if (glyph_ascii_table['0'] == NULL) {
112                 GlyphBLF *g;
113                 unsigned int i;
114                 for (i = 0; i < 256; i++) {
115                         g = blf_glyph_search(font->glyph_cache, i);
116                         if (!g) {
117                                 FT_UInt glyph_index = FT_Get_Char_Index(font->face, i);
118                                 g = blf_glyph_add(font, glyph_index, i);
119                         }
120                         glyph_ascii_table[i] = g;
121                 }
122         }
123 }
124
125 /* Fast path for runs of ASCII characters. Given that common UTF-8
126  * input will consist of an overwhelming majority of ASCII
127  * characters.
128  */
129
130 /* Note,
131  * blf_font_ensure_ascii_table(font); must be called before this macro */
132
133 #define BLF_UTF8_NEXT_FAST(_font, _g, _str, _i, _c, _glyph_ascii_table)          \
134         if (((_c) = (_str)[_i]) < 0x80) {                                            \
135                 _g = (_glyph_ascii_table)[_c];                                           \
136                 _i++;                                                                    \
137         }                                                                            \
138         else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \
139                 if ((_g = blf_glyph_search((_font)->glyph_cache, _c)) == NULL) {         \
140                         _g = blf_glyph_add(_font,                                            \
141                                           FT_Get_Char_Index((_font)->face, _c), _c);         \
142                 }                                                                        \
143         } (void)0
144
145
146 #define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode)                        \
147         const short _has_kerning = FT_HAS_KERNING((_font)->face);                    \
148         const FT_UInt _kern_mode = (_has_kerning == 0) ? 0 :                         \
149                                  (((_font)->flags & BLF_KERNING_DEFAULT) ?           \
150                                   ft_kerning_default : FT_KERNING_UNFITTED)          \
151
152
153 #define BLF_KERNING_STEP(_font, _kern_mode, _g_prev, _g, _delta, _pen_x)         \
154 {                                                                                \
155         if (_g_prev) {                                                               \
156                 _delta.x = _delta.y = 0;                                                 \
157                 if (FT_Get_Kerning((_font)->face,                                        \
158                                    (_g_prev)->idx,                                       \
159                                    (_g)->idx,                                            \
160                                    _kern_mode,                                           \
161                                    &(_delta)) == 0)                                      \
162                 {                                                                        \
163                         _pen_x += (int)_delta.x >> 6;                                        \
164                 }                                                                        \
165         }                                                                            \
166 } (void)0
167
168 void blf_font_draw(FontBLF *font, const char *str, size_t len)
169 {
170         unsigned int c;
171         GlyphBLF *g, *g_prev = NULL;
172         FT_Vector delta;
173         int pen_x = 0, pen_y = 0;
174         size_t i = 0;
175         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
176
177         BLF_KERNING_VARS(font, has_kerning, kern_mode);
178
179         blf_font_ensure_ascii_table(font);
180
181         while ((i < len) && str[i]) {
182                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
183
184                 if (c == BLI_UTF8_ERR)
185                         break;
186                 if (g == NULL)
187                         continue;
188                 if (has_kerning)
189                         BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
190
191                 /* do not return this loop if clipped, we want every character tested */
192                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
193
194                 pen_x += (int)g->advance;
195                 g_prev = g;
196         }
197 }
198
199 /* faster version of blf_font_draw, ascii only for view dimensions */
200 void blf_font_draw_ascii(FontBLF *font, const char *str, size_t len)
201 {
202         unsigned char c;
203         GlyphBLF *g, *g_prev = NULL;
204         FT_Vector delta;
205         int pen_x = 0, pen_y = 0;
206         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
207
208         BLF_KERNING_VARS(font, has_kerning, kern_mode);
209
210         blf_font_ensure_ascii_table(font);
211
212         while ((c = *(str++)) && len--) {
213                 BLI_assert(c < 128);
214                 if ((g = glyph_ascii_table[c]) == NULL)
215                         continue;
216                 if (has_kerning)
217                         BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
218
219                 /* do not return this loop if clipped, we want every character tested */
220                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
221
222                 pen_x += (int)g->advance;
223                 g_prev = g;
224         }
225 }
226
227 /* use fixed column width, but an utf8 character may occupy multiple columns */
228 int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth)
229 {
230         unsigned int c;
231         GlyphBLF *g;
232         int col, columns = 0;
233         int pen_x = 0, pen_y = 0;
234         size_t i = 0;
235         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
236
237         blf_font_ensure_ascii_table(font);
238
239         while ((i < len) && str[i]) {
240                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
241
242                 if (c == BLI_UTF8_ERR)
243                         break;
244                 if (g == NULL)
245                         continue;
246
247                 /* do not return this loop if clipped, we want every character tested */
248                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
249
250                 col = BLI_wcwidth((wchar_t)c);
251                 if (col < 0)
252                         col = 1;
253
254                 columns += col;
255                 pen_x += cwidth * col;
256         }
257
258         return columns;
259 }
260
261 /* Sanity checks are done by BLF_draw_buffer() */
262 void blf_font_buffer(FontBLF *font, const char *str)
263 {
264         unsigned int c;
265         GlyphBLF *g, *g_prev = NULL;
266         FT_Vector delta;
267         int pen_x = (int)font->pos[0], pen_y = 0;
268         size_t i = 0;
269         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
270
271         /* buffer specific vars */
272         FontBufInfoBLF *buf_info = &font->buf_info;
273         float b_col_float[4];
274         const unsigned char b_col_char[4] = {
275             (unsigned char)(buf_info->col[0] * 255),
276             (unsigned char)(buf_info->col[1] * 255),
277             (unsigned char)(buf_info->col[2] * 255),
278             (unsigned char)(buf_info->col[3] * 255)};
279
280         unsigned char *cbuf;
281         int chx, chy;
282         int y, x;
283         float a, *fbuf;
284
285         BLF_KERNING_VARS(font, has_kerning, kern_mode);
286
287         blf_font_ensure_ascii_table(font);
288
289         /* another buffer specific call for color conversion */
290         if (buf_info->display) {
291                 copy_v4_v4(b_col_float, buf_info->col);
292                 IMB_colormanagement_display_to_scene_linear_v3(b_col_float, buf_info->display);
293         }
294         else {
295                 srgb_to_linearrgb_v4(b_col_float, buf_info->col);
296         }
297
298         while (str[i]) {
299                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
300
301                 if (c == BLI_UTF8_ERR)
302                         break;
303                 if (g == NULL)
304                         continue;
305                 if (has_kerning)
306                         BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
307
308                 chx = pen_x + ((int)g->pos_x);
309                 chy = (int)font->pos[1] + g->height;
310
311                 if (g->pitch < 0) {
312                         pen_y = (int)font->pos[1] + (g->height - (int)g->pos_y);
313                 }
314                 else {
315                         pen_y = (int)font->pos[1] - (g->height - (int)g->pos_y);
316                 }
317
318                 if ((chx + g->width) >= 0 && chx < buf_info->w && (pen_y + g->height) >= 0 && pen_y < buf_info->h) {
319                         /* don't draw beyond the buffer bounds */
320                         int width_clip = g->width;
321                         int height_clip = g->height;
322                         int yb_start = g->pitch < 0 ? 0 : g->height - 1;
323
324                         if (width_clip + chx > buf_info->w)
325                                 width_clip -= chx + width_clip - buf_info->w;
326                         if (height_clip + pen_y > buf_info->h)
327                                 height_clip -= pen_y + height_clip - buf_info->h;
328                         
329                         /* drawing below the image? */
330                         if (pen_y < 0) {
331                                 yb_start += (g->pitch < 0) ? -pen_y : pen_y;
332                                 height_clip += pen_y;
333                                 pen_y = 0;
334                         }
335
336                         if (buf_info->fbuf) {
337                                 int yb = yb_start;
338                                 for (y = ((chy >= 0) ? 0 : -chy); y < height_clip; y++) {
339                                         for (x = ((chx >= 0) ? 0 : -chx); x < width_clip; x++) {
340                                                 a = *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
341
342                                                 if (a > 0.0f) {
343                                                         float alphatest;
344                                                         fbuf = buf_info->fbuf + buf_info->ch * ((chx + x) + ((pen_y + y) * buf_info->w));
345                                                         if (a >= 1.0f) {
346                                                                 fbuf[0] = b_col_float[0];
347                                                                 fbuf[1] = b_col_float[1];
348                                                                 fbuf[2] = b_col_float[2];
349                                                                 fbuf[3] = (alphatest = (fbuf[3] + (b_col_float[3]))) < 1.0f ? alphatest : 1.0f;
350                                                         }
351                                                         else {
352                                                                 fbuf[0] = (b_col_float[0] * a) + (fbuf[0] * (1.0f - a));
353                                                                 fbuf[1] = (b_col_float[1] * a) + (fbuf[1] * (1.0f - a));
354                                                                 fbuf[2] = (b_col_float[2] * a) + (fbuf[2] * (1.0f - a));
355                                                                 fbuf[3] = (alphatest = (fbuf[3] + (b_col_float[3] * a))) < 1.0f ? alphatest : 1.0f;
356                                                         }
357                                                 }
358                                         }
359
360                                         if (g->pitch < 0)
361                                                 yb++;
362                                         else
363                                                 yb--;
364                                 }
365                         }
366
367                         if (buf_info->cbuf) {
368                                 int yb = yb_start;
369                                 for (y = 0; y < height_clip; y++) {
370                                         for (x = 0; x < width_clip; x++) {
371                                                 a = *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
372
373                                                 if (a > 0.0f) {
374                                                         int alphatest;
375                                                         cbuf = buf_info->cbuf + buf_info->ch * ((chx + x) + ((pen_y + y) * buf_info->w));
376                                                         if (a >= 1.0f) {
377                                                                 cbuf[0] = b_col_char[0];
378                                                                 cbuf[1] = b_col_char[1];
379                                                                 cbuf[2] = b_col_char[2];
380                                                                 
381                                                                 alphatest = (int)cbuf[3] + (int)b_col_char[3];
382                                                                 if (alphatest < 255) {
383                                                                         cbuf[3] = (unsigned char)(alphatest);
384                                                                 }
385                                                                 else {
386                                                                         cbuf[3] = 255;
387                                                                 }
388                                                         }
389                                                         else {
390                                                                 cbuf[0] = (unsigned char)((b_col_char[0] * a) + (cbuf[0] * (1.0f - a)));
391                                                                 cbuf[1] = (unsigned char)((b_col_char[1] * a) + (cbuf[1] * (1.0f - a)));
392                                                                 cbuf[2] = (unsigned char)((b_col_char[2] * a) + (cbuf[2] * (1.0f - a)));
393                                                                 
394                                                                 alphatest = ((int)cbuf[3] + (int)((b_col_float[3] * a) * 255.0f));
395                                                                 if (alphatest < 255) {
396                                                                         cbuf[3] = (unsigned char)(alphatest);
397                                                                 }
398                                                                 else {
399                                                                         cbuf[3] = 255;
400                                                                 }
401                                                         }
402                                                 }
403                                         }
404
405                                         if (g->pitch < 0)
406                                                 yb++;
407                                         else
408                                                 yb--;
409                                 }
410                         }
411                 }
412
413                 pen_x += (int)g->advance;
414                 g_prev = g;
415         }
416 }
417
418 void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
419 {
420         unsigned int c;
421         GlyphBLF *g, *g_prev = NULL;
422         FT_Vector delta;
423         int pen_x = 0, pen_y = 0;
424         size_t i = 0;
425         GlyphBLF **glyph_ascii_table = font->glyph_cache->glyph_ascii_table;
426
427         rctf gbox;
428
429         BLF_KERNING_VARS(font, has_kerning, kern_mode);
430
431         box->xmin = 32000.0f;
432         box->xmax = -32000.0f;
433         box->ymin = 32000.0f;
434         box->ymax = -32000.0f;
435
436         blf_font_ensure_ascii_table(font);
437
438         while (str[i]) {
439                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
440
441                 if (c == BLI_UTF8_ERR)
442                         break;
443                 if (g == NULL)
444                         continue;
445                 if (has_kerning)
446                         BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
447
448                 gbox.xmin = (float)pen_x;
449                 gbox.xmax = (float)pen_x + g->advance;
450                 gbox.ymin = g->box.ymin + (float)pen_y;
451                 gbox.ymax = g->box.ymax + (float)pen_y;
452
453                 if (gbox.xmin < box->xmin) box->xmin = gbox.xmin;
454                 if (gbox.ymin < box->ymin) box->ymin = gbox.ymin;
455
456                 if (gbox.xmax > box->xmax) box->xmax = gbox.xmax;
457                 if (gbox.ymax > box->ymax) box->ymax = gbox.ymax;
458
459                 pen_x += (int)g->advance;
460                 g_prev = g;
461         }
462
463         if (box->xmin > box->xmax) {
464                 box->xmin = 0.0f;
465                 box->ymin = 0.0f;
466                 box->xmax = 0.0f;
467                 box->ymax = 0.0f;
468         }
469 }
470
471 void blf_font_width_and_height(FontBLF *font, const char *str, float *width, float *height)
472 {
473         float xa, ya;
474         rctf box;
475
476         if (font->flags & BLF_ASPECT) {
477                 xa = font->aspect[0];
478                 ya = font->aspect[1];
479         }
480         else {
481                 xa = 1.0f;
482                 ya = 1.0f;
483         }
484
485         blf_font_boundbox(font, str, &box);
486         *width  = (BLI_rctf_size_x(&box) * xa);
487         *height = (BLI_rctf_size_y(&box) * ya);
488 }
489
490 float blf_font_width(FontBLF *font, const char *str)
491 {
492         float xa;
493         rctf box;
494
495         if (font->flags & BLF_ASPECT)
496                 xa = font->aspect[0];
497         else
498                 xa = 1.0f;
499
500         blf_font_boundbox(font, str, &box);
501         return BLI_rctf_size_x(&box) * xa;
502 }
503
504 float blf_font_height(FontBLF *font, const char *str)
505 {
506         float ya;
507         rctf box;
508
509         if (font->flags & BLF_ASPECT)
510                 ya = font->aspect[1];
511         else
512                 ya = 1.0f;
513
514         blf_font_boundbox(font, str, &box);
515         return BLI_rctf_size_y(&box) * ya;
516 }
517
518 float blf_font_fixed_width(FontBLF *font)
519 {
520         const unsigned int c = ' ';
521         GlyphBLF *g = blf_glyph_search(font->glyph_cache, c);
522         if (!g) {
523                 g = blf_glyph_add(font, FT_Get_Char_Index(font->face, c), c);
524
525                 /* if we don't find the glyph. */
526                 if (!g) {
527                         return 0.0f;
528                 }
529         }
530
531         return g->advance;
532 }
533
534 void blf_font_free(FontBLF *font)
535 {
536         GlyphCacheBLF *gc;
537
538         font->glyph_cache = NULL;
539         while ((gc = BLI_pophead(&font->cache))) {
540                 blf_glyph_cache_free(gc);
541         }
542
543         FT_Done_Face(font->face);
544         if (font->filename)
545                 MEM_freeN(font->filename);
546         if (font->name)
547                 MEM_freeN(font->name);
548         MEM_freeN(font);
549 }
550
551 static void blf_font_fill(FontBLF *font)
552 {
553         unsigned int i;
554
555         font->aspect[0] = 1.0f;
556         font->aspect[1] = 1.0f;
557         font->aspect[2] = 1.0f;
558         font->pos[0] = 0.0f;
559         font->pos[1] = 0.0f;
560         font->angle = 0.0f;
561
562         for (i = 0; i < 16; i++)
563                 font->m[i] = 0;
564
565         font->clip_rec.xmin = 0.0f;
566         font->clip_rec.xmax = 0.0f;
567         font->clip_rec.ymin = 0.0f;
568         font->clip_rec.ymax = 0.0f;
569         font->flags = 0;
570         font->dpi = 0;
571         font->size = 0;
572         font->cache.first = NULL;
573         font->cache.last = NULL;
574         font->glyph_cache = NULL;
575         font->blur = 0;
576         font->max_tex_size = -1;
577
578         font->buf_info.fbuf = NULL;
579         font->buf_info.cbuf = NULL;
580         font->buf_info.w = 0;
581         font->buf_info.h = 0;
582         font->buf_info.ch = 0;
583         font->buf_info.col[0] = 0;
584         font->buf_info.col[1] = 0;
585         font->buf_info.col[2] = 0;
586         font->buf_info.col[3] = 0;
587
588         font->ft_lib = ft_lib;
589         font->ft_lib_mutex = &ft_lib_mutex;
590 }
591
592 FontBLF *blf_font_new(const char *name, const char *filename)
593 {
594         FontBLF *font;
595         FT_Error err;
596         char *mfile;
597
598         font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new");
599         err = FT_New_Face(ft_lib, filename, 0, &font->face);
600         if (err) {
601                 MEM_freeN(font);
602                 return NULL;
603         }
604
605         err = FT_Select_Charmap(font->face, ft_encoding_unicode);
606         if (err) {
607                 printf("Can't set the unicode character map!\n");
608                 FT_Done_Face(font->face);
609                 MEM_freeN(font);
610                 return NULL;
611         }
612
613         mfile = blf_dir_metrics_search(filename);
614         if (mfile) {
615                 err = FT_Attach_File(font->face, mfile);
616                 if (err) {
617                         fprintf(stderr, "FT_Attach_File failed to load '%s' with error %d\n", filename, (int)err);
618                 }
619                 MEM_freeN(mfile);
620         }
621
622         font->name = BLI_strdup(name);
623         font->filename = BLI_strdup(filename);
624         blf_font_fill(font);
625         return font;
626 }
627
628 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
629 {
630         FT_Open_Args open;
631
632         open.flags = FT_OPEN_MEMORY;
633         open.memory_base = (const FT_Byte *)mem;
634         open.memory_size = mem_size;
635         FT_Attach_Stream(font->face, &open);
636 }
637
638 FontBLF *blf_font_new_from_mem(const char *name, const unsigned char *mem, int mem_size)
639 {
640         FontBLF *font;
641         FT_Error err;
642
643         font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new_from_mem");
644         err = FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
645         if (err) {
646                 MEM_freeN(font);
647                 return NULL;
648         }
649
650         err = FT_Select_Charmap(font->face, ft_encoding_unicode);
651         if (err) {
652                 printf("Can't set the unicode character map!\n");
653                 FT_Done_Face(font->face);
654                 MEM_freeN(font);
655                 return NULL;
656         }
657
658         font->name = BLI_strdup(name);
659         font->filename = NULL;
660         blf_font_fill(font);
661         return font;
662 }