Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[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;
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                 if (font->b_fbuf) {
201                         chx= pen_x + ((int)g->pos_x);
202
203                         diff= g->height - ((int)g->pos_y);
204
205                         if (diff > 0) {
206                                 if (g->pitch < 0)
207                                         pen_y += diff;
208                                 else
209                                         pen_y -= diff;
210                         }
211                         else if (diff < 0) {
212                                 if (g->pitch < 0)
213                                         pen_y -= diff;
214                                 else
215                                         pen_y += diff;
216                         }
217
218
219                         if (g->pitch < 0)
220                                 chy= pen_y - ((int)g->pos_y);
221                         else
222                                 chy= pen_y + ((int)g->pos_y);
223
224                         if (chx >= 0 && chx < font->bw && pen_y >= 0 && pen_y < font->bh) {
225                                 if (g->pitch < 0)
226                                         yb= 0;
227                                 else
228                                         yb= g->height-1;
229
230                                 for (y= 0; y < g->height; y++) {
231                                         for (x= 0; x < g->width; x++) {
232                                                 fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
233                                                 data= g->bitmap + x + (yb * g->pitch);
234                                                 a= data[0]/255.0f;
235
236                                                 if (a == 1.0) {
237                                                         fbuf[0]= font->b_col[0];
238                                                         fbuf[1]= font->b_col[1];
239                                                         fbuf[2]= font->b_col[2];
240                                                 }
241                                                 else {
242                                                         fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
243                                                         fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
244                                                         fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
245                                                 }
246                                         }
247
248                                         if (g->pitch < 0)
249                                                 yb++;
250                                         else
251                                                 yb--;
252                                 }
253                         }
254
255                         if (diff > 0) {
256                                 if (g->pitch < 0)
257                                         pen_x -= diff;
258                                 else
259                                         pen_y += diff;
260                         }
261                         else if (diff < 0) {
262                                 if (g->pitch < 0)
263                                         pen_x += diff;
264                                 else
265                                         pen_y -= diff;
266                         }
267
268                 }
269
270                 pen_x += g->advance;
271                 g_prev= g;
272         }
273 }
274
275 void blf_font_boundbox(FontBLF *font, char *str, rctf *box)
276 {
277         unsigned int c;
278         GlyphBLF *g, *g_prev;
279         FT_Vector delta;
280         FT_UInt glyph_index;
281         rctf gbox;
282         int pen_x, pen_y;
283         int i, has_kerning, st;
284
285         if (!font->glyph_cache)
286                 return;
287
288         box->xmin= 32000.0f;
289         box->xmax= -32000.0f;
290         box->ymin= 32000.0f;
291         box->ymax= -32000.0f;
292
293         i= 0;
294         pen_x= 0;
295         pen_y= 0;
296         has_kerning= FT_HAS_KERNING(font->face);
297         g_prev= NULL;
298
299         while (str[i]) {
300                 c= blf_utf8_next((unsigned char *)str, &i);
301                 if (c == 0)
302                         break;
303
304                 g= blf_glyph_search(font->glyph_cache, c);
305                 if (!g) {
306                         glyph_index= FT_Get_Char_Index(font->face, c);
307                         g= blf_glyph_add(font, glyph_index, c);
308                 }
309
310                 /* if we don't found a glyph, skip it. */
311                 if (!g)
312                         continue;
313
314                 if (has_kerning && g_prev) {
315                         delta.x= 0;
316                         delta.y= 0;
317
318                         if (font->flags & BLF_KERNING_DEFAULT)
319                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
320                         else
321                                 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
322
323                         if (st == 0)
324                                 pen_x += delta.x >> 6;
325                 }
326
327                 gbox.xmin= g->box.xmin + pen_x;
328                 gbox.xmax= g->box.xmax + pen_x;
329                 gbox.ymin= g->box.ymin + pen_y;
330                 gbox.ymax= g->box.ymax + pen_y;
331
332                 if (gbox.xmin < box->xmin)
333                         box->xmin= gbox.xmin;
334                 if (gbox.ymin < box->ymin)
335                         box->ymin= gbox.ymin;
336
337                 if (gbox.xmax > box->xmax)
338                         box->xmax= gbox.xmax;
339                 if (gbox.ymax > box->ymax)
340                         box->ymax= gbox.ymax;
341
342                 pen_x += g->advance;
343                 g_prev= g;
344         }
345
346         if (box->xmin > box->xmax) {
347                 box->xmin= 0.0f;
348                 box->ymin= 0.0f;
349                 box->xmax= 0.0f;
350                 box->ymax= 0.0f;
351         }
352 }
353
354 void blf_font_width_and_height(FontBLF *font, char *str, float *width, float *height)
355 {
356         rctf box;
357
358         if (font->glyph_cache) {
359                 blf_font_boundbox(font, str, &box);
360                 *width= ((box.xmax - box.xmin) * font->aspect);
361                 *height= ((box.ymax - box.ymin) * font->aspect);
362         }
363 }
364
365 float blf_font_width(FontBLF *font, char *str)
366 {
367         rctf box;
368
369         if (!font->glyph_cache)
370                 return(0.0f);
371
372         blf_font_boundbox(font, str, &box);
373         return((box.xmax - box.xmin) * font->aspect);
374 }
375
376 float blf_font_height(FontBLF *font, char *str)
377 {
378         rctf box;
379
380         if (!font->glyph_cache)
381                 return(0.0f);
382
383         blf_font_boundbox(font, str, &box);
384         return((box.ymax - box.ymin) * font->aspect);
385 }
386
387 float blf_font_fixed_width(FontBLF *font)
388 {
389         GlyphBLF *g;
390         FT_UInt glyph_index;
391         unsigned int c = ' ';
392
393         if (!font->glyph_cache)
394                 return 0.0f;
395
396         glyph_index= FT_Get_Char_Index(font->face, c);
397         g= blf_glyph_search(font->glyph_cache, c);
398         if (!g)
399                 g= blf_glyph_add(font, glyph_index, c);
400
401         /* if we don't find the glyph. */
402         if (!g)
403                 return 0.0f;
404         
405         return g->advance;
406 }
407
408 void blf_font_free(FontBLF *font)
409 {
410         GlyphCacheBLF *gc;
411
412         font->glyph_cache= NULL;
413         while (font->cache.first) {
414                 gc= font->cache.first;
415                 BLI_remlink(&font->cache, gc);
416                 blf_glyph_cache_free(gc);
417         }
418
419         FT_Done_Face(font->face);
420         if (font->filename)
421                 MEM_freeN(font->filename);
422         if (font->name)
423                 MEM_freeN(font->name);
424         MEM_freeN(font);
425 }
426
427 void blf_font_fill(FontBLF *font)
428 {
429         font->aspect= 1.0f;
430         font->pos[0]= 0.0f;
431         font->pos[1]= 0.0f;
432         font->angle= 0.0f;
433         Mat4One(font->mat);
434         font->clip_rec.xmin= 0.0f;
435         font->clip_rec.xmax= 0.0f;
436         font->clip_rec.ymin= 0.0f;
437         font->clip_rec.ymax= 0.0f;
438         font->flags= 0;
439         font->dpi= 0;
440         font->size= 0;
441         font->cache.first= NULL;
442         font->cache.last= NULL;
443         font->glyph_cache= NULL;
444         font->blur= 0;
445         font->max_tex_size= -1;
446         font->b_fbuf= NULL;
447         font->b_cbuf= NULL;
448         font->bw= 0;
449         font->bh= 0;
450         font->bch= 0;
451         font->b_col[0]= 0;
452         font->b_col[1]= 0;
453         font->b_col[2]= 0;
454         font->b_col[3]= 0;
455 }
456
457 FontBLF *blf_font_new(char *name, char *filename)
458 {
459         FontBLF *font;
460         FT_Error err;
461         char *mfile;
462
463         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
464         err= FT_New_Face(global_ft_lib, filename, 0, &font->face);
465         if (err) {
466                 MEM_freeN(font);
467                 return(NULL);
468         }
469
470         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
471         if (err) {
472                 printf("Can't set the unicode character map!\n");
473                 FT_Done_Face(font->face);
474                 MEM_freeN(font);
475                 return(NULL);
476         }
477
478         mfile= blf_dir_metrics_search(filename);
479         if (mfile) {
480                 err= FT_Attach_File(font->face, mfile);
481                 MEM_freeN(mfile);
482         }
483
484         font->name= BLI_strdup(name);
485         font->filename= BLI_strdup(filename);
486         blf_font_fill(font);
487         return(font);
488 }
489
490 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
491 {
492         FT_Open_Args open;
493
494         open.flags= FT_OPEN_MEMORY;
495         open.memory_base= (FT_Byte *)mem;
496         open.memory_size= mem_size;
497         FT_Attach_Stream(font->face, &open);
498 }
499
500 FontBLF *blf_font_new_from_mem(char *name, unsigned char *mem, int mem_size)
501 {
502         FontBLF *font;
503         FT_Error err;
504
505         font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
506         err= FT_New_Memory_Face(global_ft_lib, mem, mem_size, 0, &font->face);
507         if (err) {
508                 MEM_freeN(font);
509                 return(NULL);
510         }
511
512         err= FT_Select_Charmap(font->face, ft_encoding_unicode);
513         if (err) {
514                 printf("Can't set the unicode character map!\n");
515                 FT_Done_Face(font->face);
516                 MEM_freeN(font);
517                 return(NULL);
518         }
519
520         font->name= BLI_strdup(name);
521         font->filename= NULL;
522         blf_font_fill(font);
523         return(font);
524 }