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