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