Minor speedups for 3D view text drawing ~10-15% improved frame-rate with particle...
[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 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #include <ft2build.h>
35
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_vec_types.h"
42
43
44 #include "BLI_blenlib.h"
45 #include "BLI_linklist.h"       /* linknode */
46 #include "BLI_math.h"
47
48 #include "BIF_gl.h"
49 #include "BLF_api.h"
50
51 #include "blf_internal_types.h"
52 #include "blf_internal.h"
53
54
55 /* freetype2 handle. */
56 FT_Library global_ft_lib;
57
58 int blf_font_init(void)
59 {
60         return(FT_Init_FreeType(&global_ft_lib));
61 }
62
63 void blf_font_exit(void)
64 {
65         FT_Done_FreeType(global_ft_lib);
66 }
67
68 void blf_font_size(FontBLF *font, int size, int dpi)
69 {
70         GlyphCacheBLF *gc;
71         FT_Error err;
72
73         err= FT_Set_Char_Size(font->face, 0, (size * 64), dpi, dpi);
74         if (err) {
75                 /* FIXME: here we can go through the fixed size and choice a close one */
76                 printf("The current font don't support the size, %d and dpi, %d\n", size, dpi);
77                 return;
78         }
79
80         font->size= size;
81         font->dpi= dpi;
82
83         gc= blf_glyph_cache_find(font, size, dpi);
84         if (gc)
85                 font->glyph_cache= gc;
86         else {
87                 gc= blf_glyph_cache_new(font);
88                 if (gc)
89                         font->glyph_cache= gc;
90                 else
91                         font->glyph_cache= NULL;
92         }
93 }
94
95 void blf_font_draw(FontBLF *font, const char *str)
96 {
97         unsigned int c;
98         GlyphBLF *g, *g_prev;
99         FT_Vector delta;
100         FT_UInt glyph_index;
101         int pen_x, pen_y;
102         int i, has_kerning, st;
103
104         if (!font->glyph_cache)
105                 return;
106
107         i= 0;
108         pen_x= 0;
109         pen_y= 0;
110         has_kerning= FT_HAS_KERNING(font->face);
111         g_prev= NULL;
112
113         while (str[i]) {
114                 c= blf_utf8_next((unsigned char *)str, &i);
115                 if (c == 0)
116                         break;
117
118                 g= blf_glyph_search(font->glyph_cache, c);
119                 if (!g) {
120                         glyph_index= FT_Get_Char_Index(font->face, c);
121                         g= blf_glyph_add(font, glyph_index, c);
122                 }
123
124                 /* if we don't found a glyph, skip it. */
125                 if (!g)
126                         continue;
127
128                 if (has_kerning && g_prev) {
129                         delta.x= 0;
130                         delta.y= 0;
131
132                         if (font->flags & BLF_KERNING_DEFAULT)
133                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
134                         else
135                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
136
137                         if (st == 0)
138                                 pen_x += delta.x >> 6;
139                 }
140
141                 /* do not return this loop if clipped, we want every character tested */
142                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
143
144                 pen_x += g->advance;
145                 g_prev= g;
146         }
147 }
148
149 /* faster version of blf_font_draw, ascii only for view dimensions */
150 void blf_font_draw_ascii(FontBLF *font, const char *str)
151 {
152         char c;
153         GlyphBLF *g, *g_prev;
154         FT_Vector delta;
155         FT_UInt glyph_index;
156         int pen_x, pen_y;
157         int i, has_kerning, st;
158
159         if (!font->glyph_cache)
160                 return;
161
162         i= 0;
163         pen_x= 0;
164         pen_y= 0;
165         has_kerning= FT_HAS_KERNING(font->face);
166         g_prev= NULL;
167
168         /* build ascii on demand */
169         if(font->glyph_ascii_table['0']==NULL) {
170                 for(i=0; i<256; i++) {
171                         g= blf_glyph_search(font->glyph_cache, i);
172                         if (!g) {
173                                 glyph_index= FT_Get_Char_Index(font->face, i);
174                                 g= blf_glyph_add(font, glyph_index, i);
175                         }
176                         font->glyph_ascii_table[i]= g;
177                 }
178         }
179         
180         while ((c= *(str++))) {
181                 g= font->glyph_ascii_table[c];
182
183                 /* if we don't found a glyph, skip it. */
184                 if (!g)
185                         continue;
186
187                 if (has_kerning && g_prev) {
188                         delta.x= 0;
189                         delta.y= 0;
190
191                         if (font->flags & BLF_KERNING_DEFAULT)
192                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
193                         else
194                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
195
196                         if (st == 0)
197                                 pen_x += delta.x >> 6;
198                 }
199
200                 /* do not return this loop if clipped, we want every character tested */
201                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
202
203                 pen_x += g->advance;
204                 g_prev= g;
205         }
206 }
207
208 void blf_font_buffer(FontBLF *font, char *str)
209 {
210         unsigned char *cbuf;
211         unsigned int c;
212         unsigned char b_col_char[3];
213         GlyphBLF *g, *g_prev;
214         FT_Vector delta;
215         FT_UInt glyph_index;
216         float a, *fbuf;
217         int pen_x, y, x, yb;
218         int i, has_kerning, st, chx, chy;
219
220         if (!font->glyph_cache || (!font->b_fbuf && !font->b_cbuf))
221                 return;
222         
223         i= 0;
224         pen_x= (int)font->pos[0];
225         has_kerning= FT_HAS_KERNING(font->face);
226         g_prev= NULL;
227         
228         b_col_char[0]= font->b_col[0] * 255;
229         b_col_char[1]= font->b_col[1] * 255;
230         b_col_char[2]= font->b_col[2] * 255;
231
232         while (str[i]) {
233                 int pen_y;
234                 c= blf_utf8_next((unsigned char *)str, &i);
235                 if (c == 0)
236                         break;
237
238                 g= blf_glyph_search(font->glyph_cache, c);
239                 if (!g) {
240                         glyph_index= FT_Get_Char_Index(font->face, c);
241                         g= blf_glyph_add(font, glyph_index, c);
242                 }
243
244                 /* if we don't found a glyph, skip it. */
245                 if (!g)
246                         continue;
247
248                 if (has_kerning && g_prev) {
249                         delta.x= 0;
250                         delta.y= 0;
251
252                         if (font->flags & BLF_KERNING_DEFAULT)
253                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
254                         else
255                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
256
257                         if (st == 0)
258                                 pen_x += delta.x >> 6;
259                 }
260
261                 chx= pen_x + ((int)g->pos_x);
262                 chy= (int)font->pos[1] + g->height;
263
264                 if (g->pitch < 0) {
265                         pen_y = (int)font->pos[1] + (g->height - (int)g->pos_y);
266                 }
267                 else {
268                         pen_y = (int)font->pos[1] - (g->height - (int)g->pos_y);
269                 }
270
271                 if ((chx + g->width) >= 0 && chx < font->bw && (pen_y + g->height) >= 0 && pen_y < font->bh) {
272                         /* dont draw beyond the buffer bounds */
273                         int width_clip= g->width;
274                         int height_clip= g->height;
275
276                         if (width_clip + chx > font->bw)        width_clip  -= chx + width_clip - font->bw;
277                         if (height_clip + pen_y > font->bh) height_clip -= pen_y + height_clip - font->bh;
278
279                         yb= g->pitch < 0 ? 0 : g->height-1;
280                         
281                         if (font->b_fbuf) {
282                                 for (y=(chy >= 0 ? 0:-chy); y < height_clip; y++) {
283                                         for (x=(chx >= 0 ? 0:-chx); x < width_clip; x++) {
284                                                 
285                                                 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
286
287                                                 if(a > 0.0f) {
288                                                         fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
289                                                         if (a >= 1.0f) {
290                                                                 fbuf[0]= font->b_col[0];
291                                                                 fbuf[1]= font->b_col[1];
292                                                                 fbuf[2]= font->b_col[2];
293                                                         }
294                                                         else {
295                                                                 fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
296                                                                 fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
297                                                                 fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
298                                                         }
299                                                 }
300                                         }
301
302                                         if (g->pitch < 0)
303                                                 yb++;
304                                         else
305                                                 yb--;
306                                 }
307                         }
308
309                         if (font->b_cbuf) {
310                                 for (y= 0; y < height_clip; y++) {
311                                         for (x= 0; x < width_clip; x++) {
312                                                 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
313
314                                                 if(a > 0.0f) {
315                                                         cbuf= font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
316                                                         if (a >= 1.0f) {
317                                                                 cbuf[0]= b_col_char[0];
318                                                                 cbuf[1]= b_col_char[1];
319                                                                 cbuf[2]= b_col_char[2];
320                                                         }
321                                                         else {
322                                                                 cbuf[0]= (b_col_char[0]*a) + (cbuf[0] * (1-a));
323                                                                 cbuf[1]= (b_col_char[1]*a) + (cbuf[1] * (1-a));
324                                                                 cbuf[2]= (b_col_char[2]*a) + (cbuf[2] * (1-a));
325                                                         }
326                                                 }
327                                         }
328
329                                         if (g->pitch < 0)
330                                                 yb++;
331                                         else
332                                                 yb--;
333                                 }
334                         }
335                 }
336
337                 pen_x += g->advance;
338                 g_prev= g;
339         }
340 }
341
342 void blf_font_boundbox(FontBLF *font, char *str, rctf *box)
343 {
344         unsigned int c;
345         GlyphBLF *g, *g_prev;
346         FT_Vector delta;
347         FT_UInt glyph_index;
348         rctf gbox;
349         int pen_x, pen_y;
350         int i, has_kerning, st;
351
352         if (!font->glyph_cache)
353                 return;
354
355         box->xmin= 32000.0f;
356         box->xmax= -32000.0f;
357         box->ymin= 32000.0f;
358         box->ymax= -32000.0f;
359
360         i= 0;
361         pen_x= 0;
362         pen_y= 0;
363         has_kerning= FT_HAS_KERNING(font->face);
364         g_prev= NULL;
365
366         while (str[i]) {
367                 c= blf_utf8_next((unsigned char *)str, &i);
368                 if (c == 0)
369                         break;
370
371                 g= blf_glyph_search(font->glyph_cache, c);
372                 if (!g) {
373                         glyph_index= FT_Get_Char_Index(font->face, c);
374                         g= blf_glyph_add(font, glyph_index, c);
375                 }
376
377                 /* if we don't found a glyph, skip it. */
378                 if (!g)
379                         continue;
380
381                 if (has_kerning && g_prev) {
382                         delta.x= 0;
383                         delta.y= 0;
384
385                         if (font->flags & BLF_KERNING_DEFAULT)
386                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
387                         else
388                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
389
390                         if (st == 0)
391                                 pen_x += delta.x >> 6;
392                 }
393
394                 gbox.xmin= pen_x;
395                 gbox.xmax= pen_x + g->advance;
396                 gbox.ymin= g->box.ymin + pen_y;
397                 gbox.ymax= g->box.ymax + pen_y;
398
399                 if (gbox.xmin < box->xmin)
400                         box->xmin= gbox.xmin;
401                 if (gbox.ymin < box->ymin)
402                         box->ymin= gbox.ymin;
403
404                 if (gbox.xmax > box->xmax)
405                         box->xmax= gbox.xmax;
406                 if (gbox.ymax > box->ymax)
407                         box->ymax= gbox.ymax;
408
409                 pen_x += g->advance;
410                 g_prev= g;
411         }
412
413         if (box->xmin > box->xmax) {
414                 box->xmin= 0.0f;
415                 box->ymin= 0.0f;
416                 box->xmax= 0.0f;
417                 box->ymax= 0.0f;
418         }
419 }
420
421 void blf_font_width_and_height(FontBLF *font, char *str, float *width, float *height)
422 {
423         rctf box;
424
425         if (font->glyph_cache) {
426                 blf_font_boundbox(font, str, &box);
427                 *width= ((box.xmax - box.xmin) * font->aspect);
428                 *height= ((box.ymax - box.ymin) * font->aspect);
429         }
430 }
431
432 float blf_font_width(FontBLF *font, char *str)
433 {
434         rctf box;
435
436         if (!font->glyph_cache)
437                 return(0.0f);
438
439         blf_font_boundbox(font, str, &box);
440         return((box.xmax - box.xmin) * font->aspect);
441 }
442
443 float blf_font_height(FontBLF *font, char *str)
444 {
445         rctf box;
446
447         if (!font->glyph_cache)
448                 return(0.0f);
449
450         blf_font_boundbox(font, str, &box);
451         return((box.ymax - box.ymin) * font->aspect);
452 }
453
454 float blf_font_fixed_width(FontBLF *font)
455 {
456         GlyphBLF *g;
457         FT_UInt glyph_index;
458         unsigned int c = ' ';
459
460         if (!font->glyph_cache)
461                 return 0.0f;
462
463         glyph_index= FT_Get_Char_Index(font->face, c);
464         g= blf_glyph_search(font->glyph_cache, c);
465         if (!g)
466                 g= blf_glyph_add(font, glyph_index, c);
467
468         /* if we don't find the glyph. */
469         if (!g)
470                 return 0.0f;
471         
472         return g->advance;
473 }
474
475 void blf_font_free(FontBLF *font)
476 {
477         GlyphCacheBLF *gc;
478
479         font->glyph_cache= NULL;
480         while (font->cache.first) {
481                 gc= font->cache.first;
482                 BLI_remlink(&font->cache, gc);
483                 blf_glyph_cache_free(gc);
484         }
485
486         FT_Done_Face(font->face);
487         if (font->filename)
488                 MEM_freeN(font->filename);
489         if (font->name)
490                 MEM_freeN(font->name);
491         MEM_freeN(font);
492 }
493
494 static void blf_font_fill(FontBLF *font)
495 {
496         font->aspect= 1.0f;
497         font->pos[0]= 0.0f;
498         font->pos[1]= 0.0f;
499         font->angle= 0.0f;
500         unit_m4(font->mat);
501         font->clip_rec.xmin= 0.0f;
502         font->clip_rec.xmax= 0.0f;
503         font->clip_rec.ymin= 0.0f;
504         font->clip_rec.ymax= 0.0f;
505         font->flags= 0;
506         font->dpi= 0;
507         font->size= 0;
508         font->cache.first= NULL;
509         font->cache.last= NULL;
510         font->glyph_cache= NULL;
511         font->blur= 0;
512         font->max_tex_size= -1;
513         font->b_fbuf= NULL;
514         font->b_cbuf= NULL;
515         font->bw= 0;
516         font->bh= 0;
517         font->bch= 0;
518         font->b_col[0]= 0;
519         font->b_col[1]= 0;
520         font->b_col[2]= 0;
521         font->b_col[3]= 0;
522
523         memset(font->glyph_ascii_table, 0, sizeof(font->glyph_ascii_table));
524 }
525
526 FontBLF *blf_font_new(char *name, char *filename)
527 {
528         FontBLF *font;
529         FT_Error err;
530         char *mfile;
531
532         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
533         err= FT_New_Face(global_ft_lib, filename, 0, &font->face);
534         if (err) {
535                 MEM_freeN(font);
536                 return(NULL);
537         }
538
539         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
540         if (err) {
541                 printf("Can't set the unicode character map!\n");
542                 FT_Done_Face(font->face);
543                 MEM_freeN(font);
544                 return(NULL);
545         }
546
547         mfile= blf_dir_metrics_search(filename);
548         if (mfile) {
549                 err= FT_Attach_File(font->face, mfile);
550                 MEM_freeN(mfile);
551         }
552
553         font->name= BLI_strdup(name);
554         font->filename= BLI_strdup(filename);
555         blf_font_fill(font);
556         return(font);
557 }
558
559 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
560 {
561         FT_Open_Args open;
562
563         open.flags= FT_OPEN_MEMORY;
564         open.memory_base= (FT_Byte *)mem;
565         open.memory_size= mem_size;
566         FT_Attach_Stream(font->face, &open);
567 }
568
569 FontBLF *blf_font_new_from_mem(char *name, unsigned char *mem, int mem_size)
570 {
571         FontBLF *font;
572         FT_Error err;
573
574         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
575         err= FT_New_Memory_Face(global_ft_lib, mem, mem_size, 0, &font->face);
576         if (err) {
577                 MEM_freeN(font);
578                 return(NULL);
579         }
580
581         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
582         if (err) {
583                 printf("Can't set the unicode character map!\n");
584                 FT_Done_Face(font->face);
585                 MEM_freeN(font);
586                 return(NULL);
587         }
588
589         font->name= BLI_strdup(name);
590         font->filename= NULL;
591         blf_font_fill(font);
592         return(font);
593 }