svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22625:22668
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #include <ft2build.h>
35
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_listBase.h"
42 #include "DNA_vec_types.h"
43
44 #include "BKE_utildefines.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_linklist.h"       /* linknode */
48 #include "BLI_string.h"
49 #include "BLI_arithb.h"
50
51 #include "BIF_gl.h"
52 #include "BLF_api.h"
53
54 #include "blf_internal_types.h"
55 #include "blf_internal.h"
56
57
58 /* freetype2 handle. */
59 FT_Library global_ft_lib;
60
61 int blf_font_init(void)
62 {
63         return(FT_Init_FreeType(&global_ft_lib));
64 }
65
66 void blf_font_exit(void)
67 {
68         FT_Done_FreeType(global_ft_lib);
69 }
70
71 void blf_font_size(FontBLF *font, int size, int dpi)
72 {
73         GlyphCacheBLF *gc;
74         FT_Error err;
75
76         err= FT_Set_Char_Size(font->face, 0, (size * 64), dpi, dpi);
77         if (err) {
78                 /* FIXME: here we can go through the fixed size and choice a close one */
79                 printf("The current font don't support the size, %d and dpi, %d\n", size, dpi);
80                 return;
81         }
82
83         font->size= size;
84         font->dpi= dpi;
85
86         gc= blf_glyph_cache_find(font, size, dpi);
87         if (gc)
88                 font->glyph_cache= gc;
89         else {
90                 gc= blf_glyph_cache_new(font);
91                 if (gc)
92                         font->glyph_cache= gc;
93                 else
94                         font->glyph_cache= NULL;
95         }
96 }
97
98 void blf_font_draw(FontBLF *font, char *str)
99 {
100         unsigned int c;
101         GlyphBLF *g, *g_prev;
102         FT_Vector delta;
103         FT_UInt glyph_index;
104         int pen_x, pen_y;
105         int i, has_kerning, st;
106
107         if (!font->glyph_cache)
108                 return;
109
110         i= 0;
111         pen_x= 0;
112         pen_y= 0;
113         has_kerning= FT_HAS_KERNING(font->face);
114         g_prev= NULL;
115
116         while (str[i]) {
117                 c= blf_utf8_next((unsigned char *)str, &i);
118                 if (c == 0)
119                         break;
120
121                 g= blf_glyph_search(font->glyph_cache, c);
122                 if (!g) {
123                         glyph_index= FT_Get_Char_Index(font->face, c);
124                         g= blf_glyph_add(font, glyph_index, c);
125                 }
126
127                 /* if we don't found a glyph, skip it. */
128                 if (!g)
129                         continue;
130
131                 if (has_kerning && g_prev) {
132                         delta.x= 0;
133                         delta.y= 0;
134
135                         if (font->flags & BLF_KERNING_DEFAULT)
136                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
137                         else
138                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
139
140                         if (st == 0)
141                                 pen_x += delta.x >> 6;
142                 }
143
144                 /* do not return this loop if clipped, we want every character tested */
145                 blf_glyph_render(font, g, (float)pen_x, (float)pen_y);
146
147                 pen_x += g->advance;
148                 g_prev= g;
149         }
150 }
151
152 void blf_font_buffer(FontBLF *font, char *str)
153 {
154         unsigned char *data, *cbuf;
155         unsigned int c;
156         GlyphBLF *g, *g_prev;
157         FT_Vector delta;
158         FT_UInt glyph_index;
159         float a, *fbuf;
160         int pen_x, pen_y, y, x, yb, diff;
161         int i, has_kerning, st, chx, chy;
162
163         if (!font->glyph_cache)
164                 return;
165
166         i= 0;
167         pen_x= (int)font->pos[0];
168         pen_y= (int)font->pos[1];
169         has_kerning= FT_HAS_KERNING(font->face);
170         g_prev= NULL;
171
172         while (str[i]) {
173                 c= blf_utf8_next((unsigned char *)str, &i);
174                 if (c == 0)
175                         break;
176
177                 g= blf_glyph_search(font->glyph_cache, c);
178                 if (!g) {
179                         glyph_index= FT_Get_Char_Index(font->face, c);
180                         g= blf_glyph_add(font, glyph_index, c);
181                 }
182
183                 /* if we don't found a glyph, skip it. */
184                 if (!g)
185                         continue;
186
187                 if (has_kerning && g_prev) {
188                         delta.x= 0;
189                         delta.y= 0;
190
191                         if (font->flags & BLF_KERNING_DEFAULT)
192                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
193                         else
194                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
195
196                         if (st == 0)
197                                 pen_x += delta.x >> 6;
198                 }
199
200                 chx= pen_x + ((int)g->pos_x);
201                 diff= g->height - ((int)g->pos_y);
202                 if (diff > 0) {
203                         if (g->pitch < 0)
204                                 pen_y += diff;
205                         else
206                                 pen_y -= diff;
207                 }
208                 else if (diff < 0) {
209                         if (g->pitch < 0)
210                                 pen_y -= diff;
211                         else
212                                 pen_y += diff;
213                 }
214
215                 if (g->pitch < 0)
216                         chy= pen_y - ((int)g->pos_y);
217                 else
218                         chy= pen_y + ((int)g->pos_y);
219
220                 if (font->b_fbuf) {
221                         if (chx >= 0 && chx < font->bw && pen_y >= 0 && pen_y < font->bh) {
222                                 if (g->pitch < 0)
223                                         yb= 0;
224                                 else
225                                         yb= g->height-1;
226
227                                 for (y= 0; y < g->height; y++) {
228                                         for (x= 0; x < g->width; x++) {
229                                                 fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
230                                                 data= g->bitmap + x + (yb * g->pitch);
231                                                 a= data[0]/255.0f;
232
233                                                 if (a == 1.0) {
234                                                         fbuf[0]= font->b_col[0];
235                                                         fbuf[1]= font->b_col[1];
236                                                         fbuf[2]= font->b_col[2];
237                                                 }
238                                                 else {
239                                                         fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
240                                                         fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
241                                                         fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
242                                                 }
243                                         }
244
245                                         if (g->pitch < 0)
246                                                 yb++;
247                                         else
248                                                 yb--;
249                                 }
250                         }
251                 }
252
253                 if (font->b_cbuf) {
254                         if (chx >= 0 && chx < font->bw && pen_y >= 0 && pen_y < font->bh) {
255                                 if (g->pitch < 0)
256                                         yb= 0;
257                                 else
258                                         yb= g->height-1;
259
260                                 for (y= 0; y < g->height; y++) {
261                                         for (x= 0; x < g->width; x++) {
262                                                 cbuf= font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
263                                                 data= g->bitmap + x + (yb * g->pitch);
264                                                 a= data[0];
265
266                                                 if (a == 256) {
267                                                         cbuf[0]= font->b_col[0];
268                                                         cbuf[1]= font->b_col[1];
269                                                         cbuf[2]= font->b_col[2];
270                                                 }
271                                                 else {
272                                                         cbuf[0]= (font->b_col[0]*a) + (cbuf[0] * (256-a));
273                                                         cbuf[1]= (font->b_col[1]*a) + (cbuf[1] * (256-a));
274                                                         cbuf[2]= (font->b_col[2]*a) + (cbuf[2] * (256-a));
275                                                 }
276                                         }
277
278                                         if (g->pitch < 0)
279                                                 yb++;
280                                         else
281                                                 yb--;
282                                 }
283                         }
284                 }
285
286                 if (diff > 0) {
287                         if (g->pitch < 0)
288                                 pen_x -= diff;
289                         else
290                                 pen_y += diff;
291                 }
292                 else if (diff < 0) {
293                         if (g->pitch < 0)
294                                 pen_x += diff;
295                         else
296                                 pen_y -= diff;
297                 }
298
299                 pen_x += g->advance;
300                 g_prev= g;
301         }
302 }
303
304 void blf_font_boundbox(FontBLF *font, char *str, rctf *box)
305 {
306         unsigned int c;
307         GlyphBLF *g, *g_prev;
308         FT_Vector delta;
309         FT_UInt glyph_index;
310         rctf gbox;
311         int pen_x, pen_y;
312         int i, has_kerning, st;
313
314         if (!font->glyph_cache)
315                 return;
316
317         box->xmin= 32000.0f;
318         box->xmax= -32000.0f;
319         box->ymin= 32000.0f;
320         box->ymax= -32000.0f;
321
322         i= 0;
323         pen_x= 0;
324         pen_y= 0;
325         has_kerning= FT_HAS_KERNING(font->face);
326         g_prev= NULL;
327
328         while (str[i]) {
329                 c= blf_utf8_next((unsigned char *)str, &i);
330                 if (c == 0)
331                         break;
332
333                 g= blf_glyph_search(font->glyph_cache, c);
334                 if (!g) {
335                         glyph_index= FT_Get_Char_Index(font->face, c);
336                         g= blf_glyph_add(font, glyph_index, c);
337                 }
338
339                 /* if we don't found a glyph, skip it. */
340                 if (!g)
341                         continue;
342
343                 if (has_kerning && g_prev) {
344                         delta.x= 0;
345                         delta.y= 0;
346
347                         if (font->flags & BLF_KERNING_DEFAULT)
348                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
349                         else
350                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
351
352                         if (st == 0)
353                                 pen_x += delta.x >> 6;
354                 }
355
356                 gbox.xmin= g->box.xmin + pen_x;
357                 gbox.xmax= g->box.xmax + pen_x;
358                 gbox.ymin= g->box.ymin + pen_y;
359                 gbox.ymax= g->box.ymax + pen_y;
360
361                 if (gbox.xmin < box->xmin)
362                         box->xmin= gbox.xmin;
363                 if (gbox.ymin < box->ymin)
364                         box->ymin= gbox.ymin;
365
366                 if (gbox.xmax > box->xmax)
367                         box->xmax= gbox.xmax;
368                 if (gbox.ymax > box->ymax)
369                         box->ymax= gbox.ymax;
370
371                 pen_x += g->advance;
372                 g_prev= g;
373         }
374
375         if (box->xmin > box->xmax) {
376                 box->xmin= 0.0f;
377                 box->ymin= 0.0f;
378                 box->xmax= 0.0f;
379                 box->ymax= 0.0f;
380         }
381 }
382
383 void blf_font_width_and_height(FontBLF *font, char *str, float *width, float *height)
384 {
385         rctf box;
386
387         if (font->glyph_cache) {
388                 blf_font_boundbox(font, str, &box);
389                 *width= ((box.xmax - box.xmin) * font->aspect);
390                 *height= ((box.ymax - box.ymin) * font->aspect);
391         }
392 }
393
394 float blf_font_width(FontBLF *font, char *str)
395 {
396         rctf box;
397
398         if (!font->glyph_cache)
399                 return(0.0f);
400
401         blf_font_boundbox(font, str, &box);
402         return((box.xmax - box.xmin) * font->aspect);
403 }
404
405 float blf_font_height(FontBLF *font, char *str)
406 {
407         rctf box;
408
409         if (!font->glyph_cache)
410                 return(0.0f);
411
412         blf_font_boundbox(font, str, &box);
413         return((box.ymax - box.ymin) * font->aspect);
414 }
415
416 float blf_font_fixed_width(FontBLF *font)
417 {
418         GlyphBLF *g;
419         FT_UInt glyph_index;
420         unsigned int c = ' ';
421
422         if (!font->glyph_cache)
423                 return 0.0f;
424
425         glyph_index= FT_Get_Char_Index(font->face, c);
426         g= blf_glyph_search(font->glyph_cache, c);
427         if (!g)
428                 g= blf_glyph_add(font, glyph_index, c);
429
430         /* if we don't find the glyph. */
431         if (!g)
432                 return 0.0f;
433         
434         return g->advance;
435 }
436
437 void blf_font_free(FontBLF *font)
438 {
439         GlyphCacheBLF *gc;
440
441         font->glyph_cache= NULL;
442         while (font->cache.first) {
443                 gc= font->cache.first;
444                 BLI_remlink(&font->cache, gc);
445                 blf_glyph_cache_free(gc);
446         }
447
448         FT_Done_Face(font->face);
449         if (font->filename)
450                 MEM_freeN(font->filename);
451         if (font->name)
452                 MEM_freeN(font->name);
453         MEM_freeN(font);
454 }
455
456 void blf_font_fill(FontBLF *font)
457 {
458         font->aspect= 1.0f;
459         font->pos[0]= 0.0f;
460         font->pos[1]= 0.0f;
461         font->angle= 0.0f;
462         Mat4One(font->mat);
463         font->clip_rec.xmin= 0.0f;
464         font->clip_rec.xmax= 0.0f;
465         font->clip_rec.ymin= 0.0f;
466         font->clip_rec.ymax= 0.0f;
467         font->flags= 0;
468         font->dpi= 0;
469         font->size= 0;
470         font->cache.first= NULL;
471         font->cache.last= NULL;
472         font->glyph_cache= NULL;
473         font->blur= 0;
474         font->max_tex_size= -1;
475         font->b_fbuf= NULL;
476         font->b_cbuf= NULL;
477         font->bw= 0;
478         font->bh= 0;
479         font->bch= 0;
480         font->b_col[0]= 0;
481         font->b_col[1]= 0;
482         font->b_col[2]= 0;
483         font->b_col[3]= 0;
484 }
485
486 FontBLF *blf_font_new(char *name, char *filename)
487 {
488         FontBLF *font;
489         FT_Error err;
490         char *mfile;
491
492         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
493         err= FT_New_Face(global_ft_lib, filename, 0, &font->face);
494         if (err) {
495                 MEM_freeN(font);
496                 return(NULL);
497         }
498
499         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
500         if (err) {
501                 printf("Can't set the unicode character map!\n");
502                 FT_Done_Face(font->face);
503                 MEM_freeN(font);
504                 return(NULL);
505         }
506
507         mfile= blf_dir_metrics_search(filename);
508         if (mfile) {
509                 err= FT_Attach_File(font->face, mfile);
510                 MEM_freeN(mfile);
511         }
512
513         font->name= BLI_strdup(name);
514         font->filename= BLI_strdup(filename);
515         blf_font_fill(font);
516         return(font);
517 }
518
519 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
520 {
521         FT_Open_Args open;
522
523         open.flags= FT_OPEN_MEMORY;
524         open.memory_base= (FT_Byte *)mem;
525         open.memory_size= mem_size;
526         FT_Attach_Stream(font->face, &open);
527 }
528
529 FontBLF *blf_font_new_from_mem(char *name, unsigned char *mem, int mem_size)
530 {
531         FontBLF *font;
532         FT_Error err;
533
534         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
535         err= FT_New_Memory_Face(global_ft_lib, mem, mem_size, 0, &font->face);
536         if (err) {
537                 MEM_freeN(font);
538                 return(NULL);
539         }
540
541         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
542         if (err) {
543                 printf("Can't set the unicode character map!\n");
544                 FT_Done_Face(font->face);
545                 MEM_freeN(font);
546                 return(NULL);
547         }
548
549         font->name= BLI_strdup(name);
550         font->filename= NULL;
551         blf_font_fill(font);
552         return(font);
553 }