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