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;
280 if (width_clip + chx > font->bw) width_clip -= chx + width_clip - font->bw;
281 if (height_clip + pen_y > font->bh) height_clip -= pen_y + height_clip - font->bh;
283 yb= g->pitch < 0 ? 0 : g->height-1;
286 for (y=(chy >= 0 ? 0:-chy); y < height_clip; y++) {
287 for (x=(chx >= 0 ? 0:-chx); x < width_clip; x++) {
289 a= *(g->bitmap + x + (yb * g->pitch)) / 255.0f;
292 fbuf= font->b_fbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
294 fbuf[0]= font->b_col[0];
295 fbuf[1]= font->b_col[1];
296 fbuf[2]= font->b_col[2];
299 fbuf[0]= (font->b_col[0]*a) + (fbuf[0] * (1-a));
300 fbuf[1]= (font->b_col[1]*a) + (fbuf[1] * (1-a));
301 fbuf[2]= (font->b_col[2]*a) + (fbuf[2] * (1-a));
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;
319 cbuf= font->b_cbuf + font->bch * ((chx + x) + ((pen_y + y)*font->bw));
321 cbuf[0]= b_col_char[0];
322 cbuf[1]= b_col_char[1];
323 cbuf[2]= b_col_char[2];
326 cbuf[0]= (b_col_char[0]*a) + (cbuf[0] * (1-a));
327 cbuf[1]= (b_col_char[1]*a) + (cbuf[1] * (1-a));
328 cbuf[2]= (b_col_char[2]*a) + (cbuf[2] * (1-a));
346 void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
349 GlyphBLF *g, *g_prev;
354 int i, has_kerning, st;
356 if (!font->glyph_cache)
360 box->xmax= -32000.0f;
362 box->ymax= -32000.0f;
367 has_kerning= FT_HAS_KERNING(font->face);
371 c= blf_utf8_next((unsigned char *)str, &i);
375 g= blf_glyph_search(font->glyph_cache, c);
377 glyph_index= FT_Get_Char_Index(font->face, c);
378 g= blf_glyph_add(font, glyph_index, c);
381 /* if we don't found a glyph, skip it. */
385 if (has_kerning && g_prev) {
389 if (font->flags & BLF_KERNING_DEFAULT)
390 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, ft_kerning_default, &delta);
392 st= FT_Get_Kerning(font->face, g_prev->idx, g->idx, FT_KERNING_UNFITTED, &delta);
395 pen_x += delta.x >> 6;
399 gbox.xmax= pen_x + g->advance;
400 gbox.ymin= g->box.ymin + pen_y;
401 gbox.ymax= g->box.ymax + pen_y;
403 if (gbox.xmin < box->xmin)
404 box->xmin= gbox.xmin;
405 if (gbox.ymin < box->ymin)
406 box->ymin= gbox.ymin;
408 if (gbox.xmax > box->xmax)
409 box->xmax= gbox.xmax;
410 if (gbox.ymax > box->ymax)
411 box->ymax= gbox.ymax;
417 if (box->xmin > box->xmax) {
425 void blf_font_width_and_height(FontBLF *font, const char *str, float *width, float *height)
430 if (font->glyph_cache) {
431 if (font->flags & BLF_ASPECT) {
440 blf_font_boundbox(font, str, &box);
441 *width= ((box.xmax - box.xmin) * xa);
442 *height= ((box.ymax - box.ymin) * ya);
446 float blf_font_width(FontBLF *font, const char *str)
451 if (!font->glyph_cache)
454 if (font->flags & BLF_ASPECT)
459 blf_font_boundbox(font, str, &box);
460 return((box.xmax - box.xmin) * xa);
463 float blf_font_height(FontBLF *font, const char *str)
468 if (!font->glyph_cache)
471 if (font->flags & BLF_ASPECT)
476 blf_font_boundbox(font, str, &box);
477 return((box.ymax - box.ymin) * ya);
480 float blf_font_fixed_width(FontBLF *font)
484 unsigned int c = ' ';
486 if (!font->glyph_cache)
489 glyph_index= FT_Get_Char_Index(font->face, c);
490 g= blf_glyph_search(font->glyph_cache, c);
492 g= blf_glyph_add(font, glyph_index, c);
494 /* if we don't find the glyph. */
501 void blf_font_free(FontBLF *font)
505 font->glyph_cache= NULL;
506 while (font->cache.first) {
507 gc= font->cache.first;
508 BLI_remlink(&font->cache, gc);
509 blf_glyph_cache_free(gc);
512 FT_Done_Face(font->face);
514 MEM_freeN(font->filename);
516 MEM_freeN(font->name);
520 static void blf_font_fill(FontBLF *font)
524 font->aspect[0]= 1.0f;
525 font->aspect[1]= 1.0f;
526 font->aspect[2]= 1.0f;
531 for (i= 0; i < 16; i++)
534 font->clip_rec.xmin= 0.0f;
535 font->clip_rec.xmax= 0.0f;
536 font->clip_rec.ymin= 0.0f;
537 font->clip_rec.ymax= 0.0f;
541 font->cache.first= NULL;
542 font->cache.last= NULL;
543 font->glyph_cache= NULL;
545 font->max_tex_size= -1;
555 font->ft_lib= ft_lib;
557 memset(font->glyph_ascii_table, 0, sizeof(font->glyph_ascii_table));
560 FontBLF *blf_font_new(const char *name, const char *filename)
566 font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
567 err= FT_New_Face(ft_lib, filename, 0, &font->face);
573 err= FT_Select_Charmap(font->face, ft_encoding_unicode);
575 printf("Can't set the unicode character map!\n");
576 FT_Done_Face(font->face);
581 mfile= blf_dir_metrics_search(filename);
583 err= FT_Attach_File(font->face, mfile);
587 font->name= BLI_strdup(name);
588 font->filename= BLI_strdup(filename);
593 void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
597 open.flags= FT_OPEN_MEMORY;
598 open.memory_base= (FT_Byte *)mem;
599 open.memory_size= mem_size;
600 FT_Attach_Stream(font->face, &open);
603 FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_size)
608 font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
609 err= FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
615 err= FT_Select_Charmap(font->face, ft_encoding_unicode);
617 printf("Can't set the unicode character map!\n");
618 FT_Done_Face(font->face);
623 font->name= BLI_strdup(name);
624 font->filename= NULL;