Blenfont: fix use incorrect clear of ascii glyph cache, leading to crash
[blender.git] / source / blender / blenfont / intern / blf_font.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/blenfont/intern/blf_font.c
30  *  \ingroup blf
31  */
32
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38
39 #include <ft2build.h>
40
41 #include FT_FREETYPE_H
42 #include FT_GLYPH_H
43
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_vec_types.h"
47
48
49 #include "BLI_blenlib.h"
50 #include "BLI_linklist.h"       /* linknode */
51 #include "BLI_math.h"
52
53 #include "BIF_gl.h"
54 #include "BLF_api.h"
55
56 #include "blf_internal_types.h"
57 #include "blf_internal.h"
58
59
60 /* freetype2 handle ONLY for this file!. */
61 static FT_Library ft_lib;
62
63 int blf_font_init(void)
64 {
65         return FT_Init_FreeType(&ft_lib);
66 }
67
68 void blf_font_exit(void)
69 {
70         FT_Done_FreeType(ft_lib);
71 }
72
73 void blf_font_size(FontBLF *font, int size, int dpi)
74 {
75         GlyphCacheBLF *gc;
76         FT_Error err;
77
78         err= FT_Set_Char_Size(font->face, 0, (size * 64), dpi, dpi);
79         if (err) {
80                 /* FIXME: here we can go through the fixed size and choice a close one */
81                 printf("The current font don't support the size, %d and dpi, %d\n", size, dpi);
82                 return;
83         }
84
85         font->size= size;
86         font->dpi= dpi;
87
88         gc= blf_glyph_cache_find(font, size, dpi);
89         if (gc)
90                 font->glyph_cache= gc;
91         else {
92                 gc= blf_glyph_cache_new(font);
93                 if (gc)
94                         font->glyph_cache= gc;
95                 else
96                         font->glyph_cache= NULL;
97         }
98 }
99
100 static void blf_font_ensure_ascii_table(FontBLF *font)
101 {
102         GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
103
104         /* build ascii on demand */
105         if(glyph_ascii_table['0']==NULL) {
106                 GlyphBLF *g;
107                 unsigned int i;
108                 for(i=0; i<256; i++) {
109                         g= blf_glyph_search(font->glyph_cache, i);
110                         if (!g) {
111                                 FT_UInt glyph_index= FT_Get_Char_Index(font->face, i);
112                                 g= blf_glyph_add(font, glyph_index, i);
113                         }
114                         glyph_ascii_table[i]= g;
115                 }
116         }
117 }
118
119 /* Fast path for runs of ASCII characters. Given that common UTF-8
120  * input will consist of an overwhelming majority of ASCII
121  * characters.
122  */
123
124 /* Note,
125  * blf_font_ensure_ascii_table(font); must be called before this macro */
126
127 #define BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table)             \
128         if(((c)= (str)[i]) < 0x80) {                                              \
129                 g= (glyph_ascii_table)[c];                                            \
130                 i++;                                                                  \
131         }                                                                         \
132         else if ((c= blf_utf8_next((unsigned char *)(str), &(i)))) {              \
133                 if ((g= blf_glyph_search((font)->glyph_cache, c)) == NULL) {          \
134                         g= blf_glyph_add(font, FT_Get_Char_Index((font)->face, c), c);    \
135                 }                                                                     \
136         }                                                                         \
137
138
139 #define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode)                     \
140         const short has_kerning= FT_HAS_KERNING((_font)->face);                   \
141         const FT_UInt kern_mode= (has_kerning == 0) ? 0 :                         \
142                                  (((_font)->flags & BLF_KERNING_DEFAULT) ?        \
143                                   ft_kerning_default : FT_KERNING_UNFITTED)       \
144                                                                                   \
145
146
147 #define BLF_KERNING_STEP(_font, kern_mode, g_prev, g, delta, pen_x)           \
148 {                                                                             \
149         if (g_prev) {                                                             \
150                 delta.x= delta.y= 0;                                                  \
151                 if (FT_Get_Kerning((_font)->face, g_prev->idx, g->idx, kern_mode, &delta) == 0) \
152                         pen_x += delta.x >> 6;                                            \
153         }                                                                         \
154 }                                                                             \
155
156 void blf_font_draw(FontBLF *font, const char *str, unsigned int len)
157 {
158         unsigned int c;
159         GlyphBLF *g, *g_prev= NULL;
160         FT_Vector delta;
161         int pen_x= 0, pen_y= 0;
162         unsigned int i= 0;
163         GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
164
165         BLF_KERNING_VARS(font, has_kerning, kern_mode);
166
167         blf_font_ensure_ascii_table(font);
168
169         while (str[i] && i < len) {
170
171                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
172
173                 if (c == 0)      break;
174                 if (g == NULL)   continue;
175                 if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
176
177                 /* do not return this loop if clipped, we want every character tested */
178                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
179
180                 pen_x += g->advance;
181                 g_prev= g;
182         }
183 }
184
185 /* faster version of blf_font_draw, ascii only for view dimensions */
186 void blf_font_draw_ascii(FontBLF *font, const char *str, unsigned int len)
187 {
188         unsigned char c;
189         GlyphBLF *g, *g_prev= NULL;
190         FT_Vector delta;
191         int pen_x= 0, pen_y= 0;
192         GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
193
194         BLF_KERNING_VARS(font, has_kerning, kern_mode);
195
196         blf_font_ensure_ascii_table(font);
197
198         while ((c= *(str++)) && len--) {
199                 if ((g= glyph_ascii_table[c]) == NULL) continue;
200                 if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
201
202                 /* do not return this loop if clipped, we want every character tested */
203                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
204
205                 pen_x += g->advance;
206                 g_prev= g;
207         }
208 }
209
210 /* Sanity checks are done by BLF_draw_buffer() */
211 void blf_font_buffer(FontBLF *font, const char *str)
212 {
213         unsigned int c;
214         GlyphBLF *g, *g_prev= NULL;
215         FT_Vector delta;
216         int pen_x= (int)font->pos[0], pen_y= 0;
217         unsigned int i= 0;
218         GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
219
220         /* buffer spesific vars*/
221         const unsigned char b_col_char[4]= {font->b_col[0] * 255,
222                                             font->b_col[1] * 255,
223                                             font->b_col[2] * 255,
224                                             font->b_col[3] * 255};
225         unsigned char *cbuf;
226         int chx, chy;
227         int y, x;
228         float a, *fbuf;
229
230         BLF_KERNING_VARS(font, has_kerning, kern_mode);
231
232         blf_font_ensure_ascii_table(font);
233
234         while (str[i]) {
235
236                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
237
238                 if (c == 0)      break;
239                 if (g == NULL)   continue;
240                 if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
241
242                 chx= pen_x + ((int)g->pos_x);
243                 chy= (int)font->pos[1] + g->height;
244
245                 if (g->pitch < 0) {
246                         pen_y = (int)font->pos[1] + (g->height - (int)g->pos_y);
247                 }
248                 else {
249                         pen_y = (int)font->pos[1] - (g->height - (int)g->pos_y);
250                 }
251
252                 if ((chx + g->width) >= 0 && chx < font->bw && (pen_y + g->height) >= 0 && pen_y < font->bh) {
253                         /* dont draw beyond the buffer bounds */
254                         int width_clip= g->width;
255                         int height_clip= g->height;
256                         int yb_start= g->pitch < 0 ? 0 : g->height-1;
257
258                         if (width_clip + chx > font->bw)        width_clip  -= chx + width_clip - font->bw;
259                         if (height_clip + pen_y > font->bh) height_clip -= pen_y + height_clip - font->bh;
260                         
261                         /* drawing below the image? */
262                         if(pen_y < 0) {
263                                 yb_start += (g->pitch < 0) ? -pen_y : pen_y;
264                                 height_clip += pen_y;
265                                 pen_y= 0;
266                         }
267
268                         if (font->b_fbuf) {
269                                 int yb= yb_start;
270                                 for (y=(chy >= 0 ? 0:-chy); y < height_clip; y++) {
271                                         for (x=(chx >= 0 ? 0:-chx); x < width_clip; x++) {
272                                                 
273                                                 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
274
275                                                 if(a > 0.0f) {
276                                                         float alphatest;
277                                                         fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
278                                                         if (a >= 1.0f) {
279                                                                 fbuf[0]= font->b_col[0];
280                                                                 fbuf[1]= font->b_col[1];
281                                                                 fbuf[2]= font->b_col[2];
282                                                                 fbuf[3]= (alphatest= (fbuf[3] + (font->b_col[3]))) < 1.0f ? alphatest : 1.0f;
283                                                         }
284                                                         else {
285                                                                 fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
286                                                                 fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
287                                                                 fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
288                                                                 fbuf[3]= (alphatest= (fbuf[3] + (font->b_col[3]*a))) < 1.0f ? alphatest : 1.0f;
289                                                         }
290                                                 }
291                                         }
292
293                                         if (g->pitch < 0)
294                                                 yb++;
295                                         else
296                                                 yb--;
297                                 }
298                         }
299
300                         if (font->b_cbuf) {
301                                 int yb= yb_start;
302                                 for (y= 0; y < height_clip; y++) {
303                                         for (x= 0; x < width_clip; x++) {
304                                                 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
305
306                                                 if(a > 0.0f) {
307                                                         int alphatest;
308                                                         cbuf= font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
309                                                         if (a >= 1.0f) {
310                                                                 cbuf[0]= b_col_char[0];
311                                                                 cbuf[1]= b_col_char[1];
312                                                                 cbuf[2]= b_col_char[2];
313                                                                 cbuf[3]= (alphatest= ((int)cbuf[3] + (int)b_col_char[3])) < 255 ? alphatest : 255;
314                                                         }
315                                                         else {
316                                                                 cbuf[0]= (b_col_char[0]*a) + (cbuf[0] * (1-a));
317                                                                 cbuf[1]= (b_col_char[1]*a) + (cbuf[1] * (1-a));
318                                                                 cbuf[2]= (b_col_char[2]*a) + (cbuf[2] * (1-a));
319                                                                 cbuf[3]= (alphatest= ((int)cbuf[3] + (int)((font->b_col[3]*a)*255.0f))) < 255 ? alphatest : 255;
320                                                         }
321                                                 }
322                                         }
323
324                                         if (g->pitch < 0)
325                                                 yb++;
326                                         else
327                                                 yb--;
328                                 }
329                         }
330                 }
331
332                 pen_x += g->advance;
333                 g_prev= g;
334         }
335 }
336
337 void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
338 {
339         unsigned int c;
340         GlyphBLF *g, *g_prev= NULL;
341         FT_Vector delta;
342         int pen_x= 0, pen_y= 0;
343         unsigned int i= 0;
344         GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
345
346         rctf gbox;
347
348         BLF_KERNING_VARS(font, has_kerning, kern_mode);
349
350         box->xmin= 32000.0f;
351         box->xmax= -32000.0f;
352         box->ymin= 32000.0f;
353         box->ymax= -32000.0f;
354
355         blf_font_ensure_ascii_table(font);
356
357         while (str[i]) {
358
359                 BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table);
360
361                 if (c == 0)      break;
362                 if (g == NULL)   continue;
363                 if (has_kerning) BLF_KERNING_STEP(font, kern_mode, g_prev, g, delta, pen_x);
364
365                 gbox.xmin= pen_x;
366                 gbox.xmax= pen_x + g->advance;
367                 gbox.ymin= g->box.ymin + pen_y;
368                 gbox.ymax= g->box.ymax + pen_y;
369
370                 if (gbox.xmin < box->xmin) box->xmin= gbox.xmin;
371                 if (gbox.ymin < box->ymin) box->ymin= gbox.ymin;
372
373                 if (gbox.xmax > box->xmax) box->xmax= gbox.xmax;
374                 if (gbox.ymax > box->ymax) box->ymax= gbox.ymax;
375
376                 pen_x += g->advance;
377                 g_prev= g;
378         }
379
380         if (box->xmin > box->xmax) {
381                 box->xmin= 0.0f;
382                 box->ymin= 0.0f;
383                 box->xmax= 0.0f;
384                 box->ymax= 0.0f;
385         }
386 }
387
388 void blf_font_width_and_height(FontBLF *font, const char *str, float *width, float *height)
389 {
390         float xa, ya;
391         rctf box;
392
393         if (font->flags & BLF_ASPECT) {
394                 xa= font->aspect[0];
395                 ya= font->aspect[1];
396         }
397         else {
398                 xa= 1.0f;
399                 ya= 1.0f;
400         }
401
402         blf_font_boundbox(font, str, &box);
403         *width= ((box.xmax - box.xmin) * xa);
404         *height= ((box.ymax - box.ymin) * ya);
405 }
406
407 float blf_font_width(FontBLF *font, const char *str)
408 {
409         float xa;
410         rctf box;
411
412         if (font->flags & BLF_ASPECT)
413                 xa= font->aspect[0];
414         else
415                 xa= 1.0f;
416
417         blf_font_boundbox(font, str, &box);
418         return (box.xmax - box.xmin) * xa;
419 }
420
421 float blf_font_height(FontBLF *font, const char *str)
422 {
423         float ya;
424         rctf box;
425
426         if (font->flags & BLF_ASPECT)
427                 ya= font->aspect[1];
428         else
429                 ya= 1.0f;
430
431         blf_font_boundbox(font, str, &box);
432         return (box.ymax - box.ymin) * ya;
433 }
434
435 float blf_font_fixed_width(FontBLF *font)
436 {
437         const unsigned int c = ' ';
438         GlyphBLF *g= blf_glyph_search(font->glyph_cache, c);
439         if (!g) {
440                 g= blf_glyph_add(font, FT_Get_Char_Index(font->face, c), c);
441
442                 /* if we don't find the glyph. */
443                 if (!g) {
444                         return 0.0f;
445                 }
446         }
447
448         return g->advance;
449 }
450
451 void blf_font_free(FontBLF *font)
452 {
453         GlyphCacheBLF *gc;
454
455         font->glyph_cache= NULL;
456         while (font->cache.first) {
457                 gc= font->cache.first;
458                 BLI_remlink(&font->cache, gc);
459                 blf_glyph_cache_free(gc);
460         }
461
462         FT_Done_Face(font->face);
463         if (font->filename)
464                 MEM_freeN(font->filename);
465         if (font->name)
466                 MEM_freeN(font->name);
467         MEM_freeN(font);
468 }
469
470 static void blf_font_fill(FontBLF *font)
471 {
472         unsigned int i;
473
474         font->aspect[0]= 1.0f;
475         font->aspect[1]= 1.0f;
476         font->aspect[2]= 1.0f;
477         font->pos[0]= 0.0f;
478         font->pos[1]= 0.0f;
479         font->angle= 0.0f;
480
481         for (i= 0; i < 16; i++)
482                 font->m[i]= 0;
483
484         font->clip_rec.xmin= 0.0f;
485         font->clip_rec.xmax= 0.0f;
486         font->clip_rec.ymin= 0.0f;
487         font->clip_rec.ymax= 0.0f;
488         font->flags= 0;
489         font->dpi= 0;
490         font->size= 0;
491         font->cache.first= NULL;
492         font->cache.last= NULL;
493         font->glyph_cache= NULL;
494         font->blur= 0;
495         font->max_tex_size= -1;
496         font->b_fbuf= NULL;
497         font->b_cbuf= NULL;
498         font->bw= 0;
499         font->bh= 0;
500         font->bch= 0;
501         font->b_col[0]= 0;
502         font->b_col[1]= 0;
503         font->b_col[2]= 0;
504         font->b_col[3]= 0;
505         font->ft_lib= ft_lib;
506 }
507
508 FontBLF *blf_font_new(const char *name, const char *filename)
509 {
510         FontBLF *font;
511         FT_Error err;
512         char *mfile;
513
514         font= (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new");
515         err= FT_New_Face(ft_lib, filename, 0, &font->face);
516         if (err) {
517                 MEM_freeN(font);
518                 return NULL;
519         }
520
521         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
522         if (err) {
523                 printf("Can't set the unicode character map!\n");
524                 FT_Done_Face(font->face);
525                 MEM_freeN(font);
526                 return NULL;
527         }
528
529         mfile= blf_dir_metrics_search(filename);
530         if (mfile) {
531                 err= FT_Attach_File(font->face, mfile);
532                 MEM_freeN(mfile);
533         }
534
535         font->name= BLI_strdup(name);
536         font->filename= BLI_strdup(filename);
537         blf_font_fill(font);
538         return font;
539 }
540
541 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
542 {
543         FT_Open_Args open;
544
545         open.flags= FT_OPEN_MEMORY;
546         open.memory_base= (FT_Byte *)mem;
547         open.memory_size= mem_size;
548         FT_Attach_Stream(font->face, &open);
549 }
550
551 FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_size)
552 {
553         FontBLF *font;
554         FT_Error err;
555
556         font= (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new_from_mem");
557         err= FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
558         if (err) {
559                 MEM_freeN(font);
560                 return NULL;
561         }
562
563         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
564         if (err) {
565                 printf("Can't set the unicode character map!\n");
566                 FT_Done_Face(font->face);
567                 MEM_freeN(font);
568                 return NULL;
569         }
570
571         font->name= BLI_strdup(name);
572         font->filename= NULL;
573         blf_font_fill(font);
574         return font;
575 }