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