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