Commit to continue tomorrow from work.
authorDiego Borghetti <bdiego@gmail.com>
Thu, 12 Feb 2009 05:02:42 +0000 (05:02 +0000)
committerDiego Borghetti <bdiego@gmail.com>
Thu, 12 Feb 2009 05:02:42 +0000 (05:02 +0000)
source/blender/blenfont/intern/blf.c [new file with mode: 0644]
source/blender/blenfont/intern/blf_font.c
source/blender/blenfont/intern/blf_internal_types.h

diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
new file mode 100644 (file)
index 0000000..b503ea0
--- /dev/null
@@ -0,0 +1,208 @@
+/**
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. 
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2009 Blender Foundation.
+ * All rights reserved.
+ *
+ * 
+ * Contributor(s): Blender Foundation
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#if 0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <ft2build.h>
+
+#include FT_FREETYPE_H
+#include FT_GLYPH_H
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_listBase.h"
+
+#include "BKE_utildefines.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_linklist.h"      /* linknode */
+#include "BLI_string.h"
+
+#include "blf_internal_types.h"
+#include "blf_internal.h"
+
+
+/* Max number of font in memory.
+ * Take care that now every font have a glyph cache per size/dpi,
+ * so we don't need load the same font with different size, just
+ * load one and call BLF_size.
+ */
+#define BLF_MAX_FONT 16
+
+/* Font array. */
+FontBLF *global_font[BLF_MAX_FONT];
+
+/* Number of font. */
+int global_font_num= 0;
+
+/* Current font. */
+int global_font_cur= 0;
+
+int BLF_init(void)
+{
+       int i;
+
+       for (i= 0; i < BLF_MAX_FONT; i++)
+               global_font[i]= NULL;
+
+       return(blf_font_init());
+}
+
+int blf_search(char *name)
+{
+       FontBLF *font;
+       int i;
+
+       for (i= 0; i < global_font_num; i++) {
+               font= global_font[i];
+               if (font && (!strcmp(font->name, name)))
+                       return(i);
+       }
+       return(-1);
+}
+
+int BLF_load(char *name)
+{
+       FontBLF *font;
+       char *filename;
+       int i;
+
+       if (!name)
+               return(-1);
+
+       /* check if we already load this font. */
+       i= blf_search(name);
+       if (i >= 0)
+               return(i);
+
+       if (global_font_num+1 >= BLF_MAX_FONT)
+               return(-1);
+
+       filename= blf_dir_search(name);
+       if (!filename)
+               return(-1);
+
+       font= blf_font_new(name, filename);
+       MEM_freeN(filename);
+
+       if (!font)
+               return(-1);
+
+       global_font[global_font_num]= font;
+       i= global_font_num;
+       global_font_num++;
+       return(i);
+}
+
+int BLF_load_mem(char *name, unsigned char *mem, int mem_size)
+{
+       FontBLF *font;
+       int i;
+
+       if (!name || !mem || !mem_size)
+               return(-1);
+
+       i= blf_search(name);
+       if (i >= 0)
+               return(i);
+
+       if (global_font_num+1 >= BLF_MAX_FONT)
+               return(-1);
+
+       font= blf_font_new_from_mem(name, mem, size);
+       if (!font)
+               return(-1);
+
+       global_font[global_font_num]= font;
+       i= global_font_num;
+       global_font_num++;
+       return(i);
+}
+
+void BLF_set(int fontid)
+{
+       if (fontid >= 0 && fontid < global_font_num)
+               global_font_cur= fontid;
+}
+
+void BLF_aspect(float aspect)
+{
+       FontBLF *font;
+
+       font= global_font[global_font_cur];
+       if (font)
+               font->aspect= aspect;
+}
+
+void BLF_position(float x, float y, float z)
+{
+       FontBLF *font;
+
+       font= global_font[global_font_cur];
+       if (font) {
+               font->pos[0]= x;
+               font->pos[1]= y;
+               font->pos[2]= z;
+       }
+}
+
+void BLF_size(int size, int dpi)
+{
+       FontBLF *font;
+
+       font= global_font[global_font_cur];
+       if (font)
+               blf_font_size(font, size, dpi);
+}
+
+void BLF_draw(char *str)
+{
+       FontBLF *font;
+
+       font= global_font[global_font_cur];
+       if (font) {
+               glEnable(GL_BLEND);
+               glEnable(GL_TEXTURE_2D);
+               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+               glPushMatrix();
+               glTranslatef(font->pos[0], font->pos[1], font->pos[2]);
+
+               blf_font_draw(font, str);
+
+               glPopMatrix();
+               glDisable(GL_TEXTURE_2D);
+               glDisable(GL_BLEND);
+       }
+}
+
+#endif
index 42043a58781a73fae80dbb5e5054bb71b3ba39f7..28b33640692563377c46f530655cf68fe0f241f4 100644 (file)
@@ -64,27 +64,8 @@ void blf_font_exit(void)
        FT_Done_Freetype(global_ft_lib);
 }
 
-FontBLF *blf_font_new(char *name)
+void blf_font_fill(FontBLF *font)
 {
-       FontBLF *font;
-       FT_Error err;
-
-       font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
-       err= FT_New_Face(global_ft_lib, name, 0, &font->face);
-       if (err) {
-               MEM_freeN(font);
-               return(NULL);
-       }
-
-       err= FT_Select_Charmap(font->face, ft_encoding_unicode);
-       if (err) {
-               printf("Warning: FT_Select_Charmap fail!!\n");
-               FT_Done_Face(font->face);
-               MEM_freeN(font);
-               return(NULL);
-       }
-
-       font->name= MEM_strdup(name);
        font->ref= 1;
        font->aspect= 1.0f;
        font->pos[0]= 0.0f;
@@ -104,6 +85,59 @@ FontBLF *blf_font_new(char *name)
        font->cache.last= NULL;
        font->glyph_cache= NULL;
        glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
+}
+
+FontBLF *blf_font_new(char *name, char *filename)
+{
+       FontBLF *font;
+       FT_Error err;
+
+       font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new");
+       err= FT_New_Face(global_ft_lib, filename, 0, &font->face);
+       if (err) {
+               printf("BLF: Can't load font: %s\n", filename);
+               MEM_freeN(font);
+               return(NULL);
+       }
+
+       err= FT_Select_Charmap(font->face, ft_encoding_unicode);
+       if (err) {
+               printf("Warning: FT_Select_Charmap fail!!\n");
+               FT_Done_Face(font->face);
+               MEM_freeN(font);
+               return(NULL);
+       }
+
+       font->name= MEM_strdup(name);
+       font->filename= MEM_strdup(filename);
+       blf_font_fill(font);
+       return(font);
+}
+
+FontBLF *blf_font_new_from_mem(char *name, unsigned char *mem, int mem_size)
+{
+       FontBLF *font;
+       FT_Error err;
+
+       font= (FontBLF *)MEM_mallocN(sizeof(FontBLF), "blf_font_new_from_mem");
+       err= FT_New_Memory_Face(global_ft_lib, mem, size, 0, &font->face);
+       if (err) {
+               printf("BLF: Can't load font: %s, from memory!!\n", name);
+               MEM_freeN(font);
+               return(NULL);
+       }
+
+       err= FT_Select_Charmap(font->face, ft_encoding_unicode);
+       if (err) {
+               printf("BLF: FT_Select_Charmap fail!!\n");
+               FT_Done_Face(font->face);
+               MEM_freeN(font);
+               return(NULL);
+       }
+
+       font->name= MEM_strdup(name);
+       font->filename= NULL;
+       blf_font_fill(font);
        return(font);
 }
 
@@ -132,4 +166,47 @@ void blf_font_size(FontBLF *font, int size, int dpi)
        }
 }
 
+void blf_font_draw(FontBLF *font, char *str)
+{
+       unsigned int c;
+       GlyphBLF *g, *g_prev;
+       FT_Vector delta;
+       FT_UInt glyph_index;
+       int pen_x, pen_y;
+       int i, has_kerning;
+
+       i= 0;
+       pen_x= 0;
+       pen_y= 0;
+       has_kerning= FT_HAS_KERNING(font->face);
+       g_prev= NULL;
+
+       while (str[i]) {
+               c= blf_uf8_next((unsigned char *)str, &i);
+               if (c == 0)
+                       break;
+
+               glyph_index= FT_Get_Char_Index(face, c);
+               g= blf_glyph_search(font->glyph_cache, glyph_index);
+               if (!g)
+                       g= blf_glyph_add(font, glyph_index, c);
+
+               /* if we don't found a glyph, skip it. */
+               if (!g)
+                       continue;
+
+               if (use_kering && g_prev) {
+                       delta.x= 0;
+                       delta.y= 0;
+
+                       FT_Get_Kerning(font->face, g_prev->index, glyph_index, FT_KERNING_MODE_UNFITTED, &delta);
+                       pen_x += delta.x >> 6;
+               }
+
+               blf_glyph_render(g, (float)pen_x, (float)pen_y);
+               pen_x += g->advance;
+               g_prev= g;
+       }
+}
+
 #endif /* zero!! */
index 396230410b20c36e32ea987bb2f460b378f6058e..5aacb2343bb23918e41bc547f70b9ec44ac033b4 100644 (file)
@@ -127,8 +127,12 @@ typedef struct _GlyphBLF {
 } GlyphBLF;
 
 typedef struct FontBLF {
+       /* font name. */
        char *name;
 
+       /* filename or NULL. */
+       char *filename;
+
        /* reference count. */
        int ref;