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