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