4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2009 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/blenfont/intern/blf_font.c
41 #include FT_FREETYPE_H
44 #include "MEM_guardedalloc.h"
46 #include "DNA_vec_types.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_linklist.h" /* linknode */
56 #include "blf_internal_types.h"
57 #include "blf_internal.h"
60 /* freetype2 handle ONLY for this file!. */
61 static FT_Library ft_lib;
63 int blf_font_init(void)
65 return(FT_Init_FreeType(&ft_lib));
68 void blf_font_exit(void)
70 FT_Done_FreeType(ft_lib);
73 void blf_font_size(FontBLF *font, int size, int dpi)
78 err= FT_Set_Char_Size(font->face, 0, (size * 64), dpi, dpi);
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);
88 gc= blf_glyph_cache_find(font, size, dpi);
90 font->glyph_cache= gc;
92 gc= blf_glyph_cache_new(font);
94 font->glyph_cache= gc;
96 font->glyph_cache= NULL;
100 void blf_font_draw(FontBLF *font, const char *str, unsigned int len)
103 GlyphBLF *g, *g_prev;
107 int i, has_kerning, st;
109 if (!font->glyph_cache)
115 has_kerning= FT_HAS_KERNING(font->face);
118 while (str[i] && i < len) {
119 c= blf_utf8_next((unsigned char *)str, &i);
123 g= blf_glyph_search(font->glyph_cache, c);
125 glyph_index= FT_Get_Char_Index(font->face, c);
126 g= blf_glyph_add(font, glyph_index, c);
129 /* if we don't found a glyph, skip it. */
133 if (has_kerning && g_prev) {
137 if (font->flags & BLF_KERNING_DEFAULT)
138 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
140 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
143 pen_x += delta.x >> 6;
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);
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)
158 GlyphBLF *g, *g_prev;
162 int i, has_kerning, st;
164 if (!font->glyph_cache)
169 has_kerning= FT_HAS_KERNING(font->face);
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);
177 glyph_index= FT_Get_Char_Index(font->face, i);
178 g= blf_glyph_add(font, glyph_index, i);
180 font->glyph_ascii_table[i]= g;
184 while ((c= *(str++)) && len--) {
185 g= font->glyph_ascii_table[c];
187 /* if we don't found a glyph, skip it. */
191 if (has_kerning && g_prev) {
195 if (font->flags & BLF_KERNING_DEFAULT)
196 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
198 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
201 pen_x += delta.x >> 6;
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);
212 void blf_font_buffer(FontBLF *font, const char *str)
216 unsigned char b_col_char[3];
217 GlyphBLF *g, *g_prev;
222 int i, has_kerning, st, chx, chy;
224 if (!font->glyph_cache || (!font->b_fbuf && !font->b_cbuf))
228 pen_x= (int)font->pos[0];
229 has_kerning= FT_HAS_KERNING(font->face);
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;
238 c= blf_utf8_next((unsigned char *)str, &i);
242 g= blf_glyph_search(font->glyph_cache, c);
244 glyph_index= FT_Get_Char_Index(font->face, c);
245 g= blf_glyph_add(font, glyph_index, c);
248 /* if we don't found a glyph, skip it. */
252 if (has_kerning && g_prev) {
256 if (font->flags & BLF_KERNING_DEFAULT)
257 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
259 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
262 pen_x += delta.x >> 6;
265 chx= pen_x + ((int)g->pos_x);
266 chy= (int)font->pos[1] + g->height;
269 pen_y = (int)font->pos[1] + (g->height - (int)g->pos_y);
272 pen_y = (int)font->pos[1] - (g->height - (int)g->pos_y);
275 if ((chx + g->width) >= 0 && chx < font->bw && (pen_y + g->height) >= 0 && pen_y < font->bh) {
276 /* dont draw beyond the buffer bounds */
277 int width_clip= g->width;
278 int height_clip= g->height;
279 int yb_start= g->pitch < 0 ? 0 : g->height-1;
281 if (width_clip + chx > font->bw) width_clip -= chx + width_clip - font->bw;
282 if (height_clip + pen_y > font->bh) height_clip -= pen_y + height_clip - font->bh;
284 /* drawing below the image? */
286 yb_start += (g->pitch < 0) ? -pen_y : pen_y;
287 height_clip += pen_y;
293 for (y=(chy >= 0 ? 0:-chy); y < height_clip; y++) {
294 for (x=(chx >= 0 ? 0:-chx); x < width_clip; x++) {
296 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
299 fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
301 fbuf[0]= font->b_col[0];
302 fbuf[1]= font->b_col[1];
303 fbuf[2]= font->b_col[2];
306 fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
307 fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
308 fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
322 for (y= 0; y < height_clip; y++) {
323 for (x= 0; x < width_clip; x++) {
324 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
327 cbuf= font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
329 cbuf[0]= b_col_char[0];
330 cbuf[1]= b_col_char[1];
331 cbuf[2]= b_col_char[2];
334 cbuf[0]= (b_col_char[0]*a) + (cbuf[0] * (1-a));
335 cbuf[1]= (b_col_char[1]*a) + (cbuf[1] * (1-a));
336 cbuf[2]= (b_col_char[2]*a) + (cbuf[2] * (1-a));
354 void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
357 GlyphBLF *g, *g_prev;
362 int i, has_kerning, st;
364 if (!font->glyph_cache)
368 box->xmax= -32000.0f;
370 box->ymax= -32000.0f;
375 has_kerning= FT_HAS_KERNING(font->face);
379 c= blf_utf8_next((unsigned char *)str, &i);
383 g= blf_glyph_search(font->glyph_cache, c);
385 glyph_index= FT_Get_Char_Index(font->face, c);
386 g= blf_glyph_add(font, glyph_index, c);
389 /* if we don't found a glyph, skip it. */
393 if (has_kerning && g_prev) {
397 if (font->flags & BLF_KERNING_DEFAULT)
398 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
400 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
403 pen_x += delta.x >> 6;
407 gbox.xmax= pen_x + g->advance;
408 gbox.ymin= g->box.ymin + pen_y;
409 gbox.ymax= g->box.ymax + pen_y;
411 if (gbox.xmin < box->xmin)
412 box->xmin= gbox.xmin;
413 if (gbox.ymin < box->ymin)
414 box->ymin= gbox.ymin;
416 if (gbox.xmax > box->xmax)
417 box->xmax= gbox.xmax;
418 if (gbox.ymax > box->ymax)
419 box->ymax= gbox.ymax;
425 if (box->xmin > box->xmax) {
433 void blf_font_width_and_height(FontBLF *font, const char *str, float *width, float *height)
438 if (font->glyph_cache) {
439 if (font->flags & BLF_ASPECT) {
448 blf_font_boundbox(font, str, &box);
449 *width= ((box.xmax - box.xmin) * xa);
450 *height= ((box.ymax - box.ymin) * ya);
454 float blf_font_width(FontBLF *font, const char *str)
459 if (!font->glyph_cache)
462 if (font->flags & BLF_ASPECT)
467 blf_font_boundbox(font, str, &box);
468 return((box.xmax - box.xmin) * xa);
471 float blf_font_height(FontBLF *font, const char *str)
476 if (!font->glyph_cache)
479 if (font->flags & BLF_ASPECT)
484 blf_font_boundbox(font, str, &box);
485 return((box.ymax - box.ymin) * ya);
488 float blf_font_fixed_width(FontBLF *font)
492 unsigned int c = ' ';
494 if (!font->glyph_cache)
497 glyph_index= FT_Get_Char_Index(font->face, c);
498 g= blf_glyph_search(font->glyph_cache, c);
500 g= blf_glyph_add(font, glyph_index, c);
502 /* if we don't find the glyph. */
509 void blf_font_free(FontBLF *font)
513 font->glyph_cache= NULL;
514 while (font->cache.first) {
515 gc= font->cache.first;
516 BLI_remlink(&font->cache, gc);
517 blf_glyph_cache_free(gc);
520 FT_Done_Face(font->face);
522 MEM_freeN(font->filename);
524 MEM_freeN(font->name);
528 static void blf_font_fill(FontBLF *font)
532 font->aspect[0]= 1.0f;
533 font->aspect[1]= 1.0f;
534 font->aspect[2]= 1.0f;
539 for (i= 0; i < 16; i++)
542 font->clip_rec.xmin= 0.0f;
543 font->clip_rec.xmax= 0.0f;
544 font->clip_rec.ymin= 0.0f;
545 font->clip_rec.ymax= 0.0f;
549 font->cache.first= NULL;
550 font->cache.last= NULL;
551 font->glyph_cache= NULL;
553 font->max_tex_size= -1;
563 font->ft_lib= ft_lib;
565 memset(font->glyph_ascii_table, 0, sizeof(font->glyph_ascii_table));
568 FontBLF *blf_font_new(const char *name, const char *filename)
574 font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
575 err= FT_New_Face(ft_lib, filename, 0, &font->face);
581 err= FT_Select_Charmap(font->face, ft_encoding_unicode);
583 printf("Can't set the unicode character map!\n");
584 FT_Done_Face(font->face);
589 mfile= blf_dir_metrics_search(filename);
591 err= FT_Attach_File(font->face, mfile);
595 font->name= BLI_strdup(name);
596 font->filename= BLI_strdup(filename);
601 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
605 open.flags= FT_OPEN_MEMORY;
606 open.memory_base= (FT_Byte *)mem;
607 open.memory_size= mem_size;
608 FT_Attach_Stream(font->face, &open);
611 FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_size)
616 font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
617 err= FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
623 err= FT_Select_Charmap(font->face, ft_encoding_unicode);
625 printf("Can't set the unicode character map!\n");
626 FT_Done_Face(font->face);
631 font->name= BLI_strdup(name);
632 font->filename= NULL;