708b3708ab7fbcd12b20779324098d98f95cd3f9
[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;
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                         int yb_start= g->pitch < 0 ? 0 : g->height-1;
280
281                         if (width_clip + chx > font->bw)        width_clip  -= chx + width_clip - font->bw;
282                         if (height_clip + pen_y > font->bh) height_clip -= pen_y + height_clip - font->bh;
283                         
284                         /* drawing below the image? */
285                         if(pen_y < 0) {
286                                 yb_start += (g->pitch < 0) ? -pen_y : pen_y;
287                                 height_clip += pen_y;
288                                 pen_y= 0;
289                         }
290
291                         if (font->b_fbuf) {
292                                 int yb= yb_start;
293                                 for (y=(chy >= 0 ? 0:-chy); y < height_clip; y++) {
294                                         for (x=(chx >= 0 ? 0:-chx); x < width_clip; x++) {
295                                                 
296                                                 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
297
298                                                 if(a > 0.0f) {
299                                                         fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
300                                                         if (a >= 1.0f) {
301                                                                 fbuf[0]= font->b_col[0];
302                                                                 fbuf[1]= font->b_col[1];
303                                                                 fbuf[2]= font->b_col[2];
304                                                         }
305                                                         else {
306                                                                 fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
307                                                                 fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
308                                                                 fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
309                                                         }
310                                                 }
311                                         }
312
313                                         if (g->pitch < 0)
314                                                 yb++;
315                                         else
316                                                 yb--;
317                                 }
318                         }
319
320                         if (font->b_cbuf) {
321                                 int yb= yb_start;
322                                 for (y= 0; y < height_clip; y++) {
323                                         for (x= 0; x < width_clip; x++) {
324                                                 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
325
326                                                 if(a > 0.0f) {
327                                                         cbuf= font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
328                                                         if (a >= 1.0f) {
329                                                                 cbuf[0]= b_col_char[0];
330                                                                 cbuf[1]= b_col_char[1];
331                                                                 cbuf[2]= b_col_char[2];
332                                                         }
333                                                         else {
334                                                                 cbuf[0]= (b_col_char[0]*a) + (cbuf[0] * (1-a));
335                                                                 cbuf[1]= (b_col_char[1]*a) + (cbuf[1] * (1-a));
336                                                                 cbuf[2]= (b_col_char[2]*a) + (cbuf[2] * (1-a));
337                                                         }
338                                                 }
339                                         }
340
341                                         if (g->pitch < 0)
342                                                 yb++;
343                                         else
344                                                 yb--;
345                                 }
346                         }
347                 }
348
349                 pen_x += g->advance;
350                 g_prev= g;
351         }
352 }
353
354 void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
355 {
356         unsigned int c;
357         GlyphBLF *g, *g_prev;
358         FT_Vector delta;
359         FT_UInt glyph_index;
360         rctf gbox;
361         int pen_x, pen_y;
362         int i, has_kerning, st;
363
364         if (!font->glyph_cache)
365                 return;
366
367         box->xmin= 32000.0f;
368         box->xmax= -32000.0f;
369         box->ymin= 32000.0f;
370         box->ymax= -32000.0f;
371
372         i= 0;
373         pen_x= 0;
374         pen_y= 0;
375         has_kerning= FT_HAS_KERNING(font->face);
376         g_prev= NULL;
377
378         while (str[i]) {
379                 c= blf_utf8_next((unsigned char *)str, &i);
380                 if (c == 0)
381                         break;
382
383                 g= blf_glyph_search(font->glyph_cache, c);
384                 if (!g) {
385                         glyph_index= FT_Get_Char_Index(font->face, c);
386                         g= blf_glyph_add(font, glyph_index, c);
387                 }
388
389                 /* if we don't found a glyph, skip it. */
390                 if (!g)
391                         continue;
392
393                 if (has_kerning && g_prev) {
394                         delta.x= 0;
395                         delta.y= 0;
396
397                         if (font->flags & BLF_KERNING_DEFAULT)
398                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
399                         else
400                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
401
402                         if (st == 0)
403                                 pen_x += delta.x >> 6;
404                 }
405
406                 gbox.xmin= pen_x;
407                 gbox.xmax= pen_x + g->advance;
408                 gbox.ymin= g->box.ymin + pen_y;
409                 gbox.ymax= g->box.ymax + pen_y;
410
411                 if (gbox.xmin < box->xmin)
412                         box->xmin= gbox.xmin;
413                 if (gbox.ymin < box->ymin)
414                         box->ymin= gbox.ymin;
415
416                 if (gbox.xmax > box->xmax)
417                         box->xmax= gbox.xmax;
418                 if (gbox.ymax > box->ymax)
419                         box->ymax= gbox.ymax;
420
421                 pen_x += g->advance;
422                 g_prev= g;
423         }
424
425         if (box->xmin > box->xmax) {
426                 box->xmin= 0.0f;
427                 box->ymin= 0.0f;
428                 box->xmax= 0.0f;
429                 box->ymax= 0.0f;
430         }
431 }
432
433 void blf_font_width_and_height(FontBLF *font, const char *str, float *width, float *height)
434 {
435         float xa, ya;
436         rctf box;
437
438         if (font->glyph_cache) {
439                 if (font->flags & BLF_ASPECT) {
440                         xa= font->aspect[0];
441                         ya= font->aspect[1];
442                 }
443                 else {
444                         xa= 1.0f;
445                         ya= 1.0f;
446                 }
447
448                 blf_font_boundbox(font, str, &box);
449                 *width= ((box.xmax - box.xmin) * xa);
450                 *height= ((box.ymax - box.ymin) * ya);
451         }
452 }
453
454 float blf_font_width(FontBLF *font, const char *str)
455 {
456         float xa;
457         rctf box;
458
459         if (!font->glyph_cache)
460                 return(0.0f);
461
462         if (font->flags & BLF_ASPECT)
463                 xa= font->aspect[0];
464         else
465                 xa= 1.0f;
466
467         blf_font_boundbox(font, str, &box);
468         return((box.xmax - box.xmin) * xa);
469 }
470
471 float blf_font_height(FontBLF *font, const char *str)
472 {
473         float ya;
474         rctf box;
475
476         if (!font->glyph_cache)
477                 return(0.0f);
478
479         if (font->flags & BLF_ASPECT)
480                 ya= font->aspect[1];
481         else
482                 ya= 1.0f;
483
484         blf_font_boundbox(font, str, &box);
485         return((box.ymax - box.ymin) * ya);
486 }
487
488 float blf_font_fixed_width(FontBLF *font)
489 {
490         GlyphBLF *g;
491         FT_UInt glyph_index;
492         unsigned int c = ' ';
493
494         if (!font->glyph_cache)
495                 return 0.0f;
496
497         glyph_index= FT_Get_Char_Index(font->face, c);
498         g= blf_glyph_search(font->glyph_cache, c);
499         if (!g)
500                 g= blf_glyph_add(font, glyph_index, c);
501
502         /* if we don't find the glyph. */
503         if (!g)
504                 return 0.0f;
505         
506         return g->advance;
507 }
508
509 void blf_font_free(FontBLF *font)
510 {
511         GlyphCacheBLF *gc;
512
513         font->glyph_cache= NULL;
514         while (font->cache.first) {
515                 gc= font->cache.first;
516                 BLI_remlink(&font->cache, gc);
517                 blf_glyph_cache_free(gc);
518         }
519
520         FT_Done_Face(font->face);
521         if (font->filename)
522                 MEM_freeN(font->filename);
523         if (font->name)
524                 MEM_freeN(font->name);
525         MEM_freeN(font);
526 }
527
528 static void blf_font_fill(FontBLF *font)
529 {
530         int i;
531
532         font->aspect[0]= 1.0f;
533         font->aspect[1]= 1.0f;
534         font->aspect[2]= 1.0f;
535         font->pos[0]= 0.0f;
536         font->pos[1]= 0.0f;
537         font->angle= 0.0f;
538
539         for (i= 0; i < 16; i++)
540                 font->m[i]= 0;
541
542         font->clip_rec.xmin= 0.0f;
543         font->clip_rec.xmax= 0.0f;
544         font->clip_rec.ymin= 0.0f;
545         font->clip_rec.ymax= 0.0f;
546         font->flags= 0;
547         font->dpi= 0;
548         font->size= 0;
549         font->cache.first= NULL;
550         font->cache.last= NULL;
551         font->glyph_cache= NULL;
552         font->blur= 0;
553         font->max_tex_size= -1;
554         font->b_fbuf= NULL;
555         font->b_cbuf= NULL;
556         font->bw= 0;
557         font->bh= 0;
558         font->bch= 0;
559         font->b_col[0]= 0;
560         font->b_col[1]= 0;
561         font->b_col[2]= 0;
562         font->b_col[3]= 0;
563         font->ft_lib= ft_lib;
564
565         memset(font->glyph_ascii_table, 0, sizeof(font->glyph_ascii_table));
566 }
567
568 FontBLF *blf_font_new(const char *name, const char *filename)
569 {
570         FontBLF *font;
571         FT_Error err;
572         char *mfile;
573
574         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
575         err= FT_New_Face(ft_lib, filename, 0, &font->face);
576         if (err) {
577                 MEM_freeN(font);
578                 return(NULL);
579         }
580
581         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
582         if (err) {
583                 printf("Can't set the unicode character map!\n");
584                 FT_Done_Face(font->face);
585                 MEM_freeN(font);
586                 return(NULL);
587         }
588
589         mfile= blf_dir_metrics_search(filename);
590         if (mfile) {
591                 err= FT_Attach_File(font->face, mfile);
592                 MEM_freeN(mfile);
593         }
594
595         font->name= BLI_strdup(name);
596         font->filename= BLI_strdup(filename);
597         blf_font_fill(font);
598         return(font);
599 }
600
601 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
602 {
603         FT_Open_Args open;
604
605         open.flags= FT_OPEN_MEMORY;
606         open.memory_base= (FT_Byte *)mem;
607         open.memory_size= mem_size;
608         FT_Attach_Stream(font->face, &open);
609 }
610
611 FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_size)
612 {
613         FontBLF *font;
614         FT_Error err;
615
616         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
617         err= FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
618         if (err) {
619                 MEM_freeN(font);
620                 return(NULL);
621         }
622
623         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
624         if (err) {
625                 printf("Can't set the unicode character map!\n");
626                 FT_Done_Face(font->face);
627                 MEM_freeN(font);
628                 return(NULL);
629         }
630
631         font->name= BLI_strdup(name);
632         font->filename= NULL;
633         blf_font_fill(font);
634         return(font);
635 }