Move texture draw to blf_util.c, now both font used it.
authorDiego Borghetti <bdiego@gmail.com>
Thu, 23 Apr 2009 21:57:41 +0000 (21:57 +0000)
committerDiego Borghetti <bdiego@gmail.com>
Thu, 23 Apr 2009 21:57:41 +0000 (21:57 +0000)
A little cleanup on the internal font, it's possible load the old
bmfont with: BLF_load_mem(name, NULL, 0) where name can be: helv,
helvb or scr.

Note that the internal font also support both draw, texture and bitmap,
by default always used texture.

Remove some old lang function that I left there and don't exist any more
because the locale are now in the RNA.

Small changes to Style's, so if we build without freetype2 by default
go back to the internal font, this is a little ugly (and have the old
problem of scale) but now blender always show text (need work a little
more there).

source/blender/blenfont/BLF_api.h
source/blender/blenfont/intern/blf.c
source/blender/blenfont/intern/blf_glyph.c
source/blender/blenfont/intern/blf_internal.c
source/blender/blenfont/intern/blf_internal.h
source/blender/blenfont/intern/blf_internal_types.h
source/blender/blenfont/intern/blf_lang.c
source/blender/blenfont/intern/blf_util.c
source/blender/editors/interface/interface_style.c

index adf9742cc9fe3c6b3ecde8708e81bb975c95e4a7..510ff550acc91b8ff41b4db65351c8c3cb92c777 100644 (file)
@@ -43,6 +43,12 @@ int BLF_load_mem(char *name, unsigned char *mem, int mem_size);
 void BLF_set(int fontid);
 int BLF_get(void);
 
+/*
+ * Return the font type, can be freetype2 or internal, -1 if
+ * some error happen (no current font).
+ */
+int BLF_type_get(void);
+
 void BLF_aspect(float aspect);
 void BLF_position(float x, float y, float z);
 void BLF_size(int size, int dpi);
@@ -74,25 +80,17 @@ void BLF_blur(int size);
 void BLF_enable(int option);
 void BLF_disable(int option);
 
-/* Read the .Blanguages file, return 1 on success or 0 if fails. */
-int BLF_lang_init(void);
-
-/* Free the memory allocate for the .Blanguages. */
-void BLF_lang_exit(void);
-
-/* Set the current Language. */
-void BLF_lang_set(int id);
-
-/* Return a string with all the Language available. */
-char *BLF_lang_pup(void);
-
-/* Return the number of invalid lines in the .Blanguages file,
- * zero means no error found.
+/*
+ * Search the path directory to the locale files, this try all
+ * the case for Linux, Win and Mac.
  */
-int BLF_lang_error(void);
+void BLF_lang_init(void);
 
-/* Return the code string for the specified language code. */
-char *BLF_lang_find_code(short langid);
+/* Set the current locale. */
+void BLF_lang_set(const char *);
+
+/* Set the current encoding name. */
+void BLF_lang_encoding_name(const char *str);
 
 /* Add a path to the font dir paths. */
 void BLF_dir_add(const char *path);
@@ -114,4 +112,8 @@ void BLF_dir_free(char **dirs, int count);
 #define BLF_MODE_TEXTURE 0
 #define BLF_MODE_BITMAP 1
 
+/* font->type */
+#define BLF_FONT_FREETYPE2 0
+#define BLF_FONT_INTERNAL 1
+
 #endif /* BLF_API_H */
index e5c1e675294cdbd05d2219c1044dc55257d76866..e43d7905cf91e3997e36bf0870ba210b9019335e 100644 (file)
@@ -185,7 +185,7 @@ int BLF_load_mem(char *name, unsigned char *mem, int mem_size)
        if (!font) {
 #ifdef WITH_FREETYPE2
                if (!mem || !mem_size) {
-                       printf("Can't load font, %s from memory!!\n", name);
+                       printf("Can't load font: %s from memory!!\n", name);
                        return(-1);
                }
 
@@ -193,7 +193,7 @@ int BLF_load_mem(char *name, unsigned char *mem, int mem_size)
 #endif /* WITH_FREETYPE2 */
 
                if (!font) {
-                       printf("Can't load font, %s from memory!!\n", name);
+                       printf("Can't load font: %s from memory!!\n", name);
                        return(-1);
                }
        }
@@ -215,6 +215,16 @@ int BLF_get(void)
        return(global_font_cur);
 }
 
+int BLF_type_get(void)
+{
+       FontBLF *font;
+
+       font= global_font[global_font_cur];
+       if (font)
+               return(font->type);
+       return(-1);
+}
+
 void BLF_enable(int option)
 {
        FontBLF *font;
index bdec76e01ff8beeb111a374a3cc95cfebfcd26a8..510204b0f519938f0aa7f1519fad6e2a0af92618 100644 (file)
@@ -433,69 +433,6 @@ void blf_glyph_free(GlyphBLF *g)
        MEM_freeN(g);
 }
 
-static void blf_glyph_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
-{
-       
-       glBegin(GL_QUADS);
-       glTexCoord2f(uv[0][0], uv[0][1]);
-       glVertex2f(dx, y1);
-       
-       glTexCoord2f(uv[0][0], uv[1][1]);
-       glVertex2f(dx, y2);
-       
-       glTexCoord2f(uv[1][0], uv[1][1]);
-       glVertex2f(dx1, y2);
-       
-       glTexCoord2f(uv[1][0], uv[0][1]);
-       glVertex2f(dx1, y1);
-       glEnd();
-       
-}
-
-static void blf_glyph_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
-{
-       float soft[25]= {
-               1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
-               1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
-               2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
-               1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
-               1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
-       
-       float color[4], *fp= soft;
-       int dx, dy;
-       
-       glGetFloatv(GL_CURRENT_COLOR, color);
-       
-       for(dx=-2; dx<3; dx++) {
-               for(dy=-2; dy<3; dy++, fp++) {
-                       glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
-                       blf_glyph_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
-               }
-       }
-       
-       glColor4fv(color);
-}
-
-static void blf_glyph_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
-{
-       float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 4/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 1/16.0f};
-       float color[4], *fp= soft;
-       int dx, dy;
-       
-       glGetFloatv(GL_CURRENT_COLOR, color);
-       
-       for(dx=-1; dx<2; dx++) {
-               for(dy=-1; dy<2; dy++, fp++) {
-                       glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
-                       blf_glyph_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
-               }
-       }
-       
-       glColor4fv(color);
-}
-
-
-
 int blf_glyph_texture_render(FontBLF *font, GlyphBLF *g, float x, float y)
 {
        GlyphTextureBLF *gt;
@@ -525,11 +462,11 @@ int blf_glyph_texture_render(FontBLF *font, GlyphBLF *g, float x, float y)
                glBindTexture(GL_TEXTURE_2D, gt->tex);
 
        if (font->blur==3)
-               blf_glyph_texture3_draw(gt->uv, dx, y1, dx1, y2);
+               blf_texture3_draw(gt->uv, dx, y1, dx1, y2);
        else if (font->blur==5)
-               blf_glyph_texture5_draw(gt->uv, dx, y1, dx1, y2);
+               blf_texture5_draw(gt->uv, dx, y1, dx1, y2);
        else
-               blf_glyph_texture_draw(gt->uv, dx, y1, dx1, y2);
+               blf_texture_draw(gt->uv, dx, y1, dx1, y2);
        
        return(1);
 }
index bc569e3d08010f41eecfd338ff548e5f6f794a5e..414ec5abead72bd30a3529fa48eeda237b6425c0 100644 (file)
@@ -141,7 +141,58 @@ int blf_internal_get_texture(FontBLF *font)
 
 void blf_internal_size(FontBLF *font, int size, int dpi)
 {
-       return;
+       /* dpi don't work here and size is limit to:
+        * helv: 10, 12
+        * helvb: 8, 10, 12
+        * scr: 12, 14, 15
+        */
+       font->dpi= dpi;
+
+       if (font->size != size) {
+               if (!strcmp(font->name, "helv")) {
+                       if (size == 12) {
+                               font->engine= (void *)&blf_font_helv12;
+                               font->size= 12;
+                       }
+                       else {
+                               font->engine= (void *)&blf_font_helv10;
+                               font->size= 10;
+                       }
+               }
+               else if (!strcmp(font->name, "helvb")) {
+                       if (size == 10) {
+                               font->engine= (void *)&blf_font_helvb10;
+                               font->size= 10;
+                       }
+                       else if (size == 12) {
+                               font->engine= (void *)&blf_font_helvb12;
+                               font->size= 12;
+                       }
+                       else {
+                               font->engine= (void *)&blf_font_helvb8;
+                               font->size= 8;
+                       }
+               }
+               else { /* scr */
+                       if (size == 14) {
+                               font->engine= (void *)&blf_font_scr14;
+                               font->size= 14;
+                       }
+                       else if (size == 15) {
+                               font->engine= (void *)&blf_font_scr15;
+                               font->size= 15;
+                       }
+                       else {
+                               font->engine= (void *)&blf_font_scr12;
+                               font->size= 12;
+                       }
+               }
+
+               if (font->mode == BLF_MODE_TEXTURE) {
+                       if (blf_internal_get_texture(font) != 0)
+                               printf("Can't create texture font!!\n");
+               }
+       }
 }
 
 void blf_internal_texture_draw(FontBLF *font, char *str)
@@ -149,7 +200,8 @@ void blf_internal_texture_draw(FontBLF *font, char *str)
        FontDataBLF *data;
        CharDataBLF *cd;
        unsigned char c;
-       float pos, cell_x, cell_y, x, y, z;
+       float pos, x, y;
+       float uv[2][2];
        int base_line;
        GLint cur_tex;
        float dx, dx1, dy, dy1;
@@ -159,7 +211,6 @@ void blf_internal_texture_draw(FontBLF *font, char *str)
        pos= 0;
        x= 0.0f;
        y= 0.0f;
-       z= 0.0f;
 
        glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
        if (cur_tex != data->texid)
@@ -169,8 +220,10 @@ void blf_internal_texture_draw(FontBLF *font, char *str)
                cd= &data->chars[c];
 
                if (cd->data_offset != -1) {
-                       cell_x= (c%16)/16.0;
-                       cell_y= (c/16)/16.0;
+                       uv[0][0]= ((c%16)/16.0) + 1.0/16.0;
+                       uv[0][1]= (c/16)/16.0;
+                       uv[1][0]= (c%16)/16.0;
+                       uv[1][1]= ((c/16)/16.0) + 1.0/16.0;
 
                        dx= x + pos + 16.0;
                        dx1= x + pos + 0.0;
@@ -189,19 +242,12 @@ void blf_internal_texture_draw(FontBLF *font, char *str)
                                        goto next_tex_char;
                        }
 
-                       glBegin(GL_QUADS);
-                       glTexCoord2f(cell_x + 1.0/16.0, cell_y);
-                       glVertex3f(dx, dy, z);
-
-                       glTexCoord2f(cell_x + 1.0/16.0, cell_y + 1.0/16.0);
-                       glVertex3f(dx, dy1, z);
-
-                       glTexCoord2f(cell_x, cell_y + 1.0/16.0);
-                       glVertex3f(dx1, dy1, z);
-
-                       glTexCoord2f(cell_x, cell_y);
-                       glVertex3f(dx1, dy, z);
-                       glEnd();
+                       if (font->blur == 3)
+                               blf_texture3_draw(uv, dx, dy, dx1, dy1);
+                       else if (font->blur == 5)
+                               blf_texture5_draw(uv, dx, dy, dx1, dy1);
+                       else
+                               blf_texture_draw(uv, dx, dy, dx1, dy1);
                }
 next_tex_char:
                pos += cd->advance;
@@ -328,39 +374,19 @@ FontBLF *blf_internal_new(char *name)
        font->name= BLI_strdup(name);
        font->filename= NULL;
 
-       if (!strcmp(name, "helv10")) {
+       if (!strcmp(name, "helv")) {
                font->engine= (void *)&blf_font_helv10;
                font->size= 10;
        }
 #ifndef BLF_INTERNAL_MINIMAL
-       else if (!strcmp(name, "helv12")) {
-               font->engine= (void *)&blf_font_helv12;
-               font->size= 12;
-       }
-       else if (!strcmp(name, "helvb8")) {
+       else if (!strcmp(name, "helvb")) {
                font->engine= (void *)&blf_font_helvb8;
                font->size= 8;
        }
-       else if (!strcmp(name, "helvb10")) {
-               font->engine= (void *)&blf_font_helvb10;
-               font->size= 10;
-       }
-       else if (!strcmp(name, "helvb12")) {
-               font->engine= (void *)&blf_font_helvb12;
-               font->size= 12;
-       }
-       else if (!strcmp(name, "scr12")) {
+       else if (!strcmp(name, "scr")) {
                font->engine= (void *)&blf_font_scr12;
                font->size= 12;
        }
-       else if (!strcmp(name, "scr14")) {
-               font->engine= (void *)&blf_font_scr14;
-               font->size= 14;
-       }
-       else if (!strcmp(name, "scr15")) {
-               font->engine= (void *)&blf_font_scr15;
-               font->size= 15;
-       }
 #endif
        else
                font->engine= NULL;
index 87d5938db9c917d5c5f55eae305548a662fe66f1..a042eefb4d9623d9a8701208e7dfc0493f8a5cbe 100644 (file)
@@ -32,6 +32,10 @@ unsigned int blf_next_p2(unsigned int x);
 unsigned int blf_hash(unsigned int val);
 int blf_utf8_next(unsigned char *buf, int *iindex);
 
+void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2);
+void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2);
+void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2);
+
 char *blf_dir_search(const char *file);
 int blf_dir_split(const char *str, char *file, int *size);
 
index cf42dc69f7769ff13f0bdc6073f4e2fd662c9af0..da2d3c17f7971cd9d8527f8f7d4be02a134dcf9d 100644 (file)
@@ -223,12 +223,4 @@ typedef struct DirBLF {
        char *path;
 } DirBLF;
 
-/* font->clip_mode */
-#define BLF_CLIP_DISABLE 0
-#define BLF_CLIP_OUT 1
-
-/* font->type */
-#define BLF_FONT_FREETYPE2 0
-#define BLF_FONT_INTERNAL 1
-
 #endif /* BLF_INTERNAL_TYPES_H */
index c871435ff0f8c7160a7344a4bcf6d4a633c407c7..024172d6db4df33067e4d4730d9b741c019ddeb3 100644 (file)
@@ -107,7 +107,7 @@ void BLF_lang_init(void)
        }
 }
 
-void BLF_lang_set(char *str)
+void BLF_lang_set(const char *str)
 {
 #if defined (_WIN32) || defined(__APPLE__)
        char envstr[12];
@@ -140,14 +140,13 @@ void BLF_lang_set(char *str)
 
        setlocale(LC_NUMERIC, "C");
 #endif
-
+       textdomain(DOMAIN_NAME);
        bindtextdomain(DOMAIN_NAME, global_messagepath);
        /* bind_textdomain_codeset(DOMAIN_NAME, global_encoding_name); */
-       textdomain(DOMAIN_NAME);
        strcpy(global_language, str);
 }
 
-void BLF_lang_encoding(char *str)
+void BLF_lang_encoding(const char *str)
 {
        strcpy(global_encoding_name, str);
        /* bind_textdomain_codeset(DOMAIN_NAME, encoding_name); */
index a4ccbedc38b95cb23876614e15174dd3519ec6db..8de6aea754b8acecbac488b9e560afe046f1b97a 100644 (file)
@@ -30,6 +30,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "BIF_gl.h"
+
 
 unsigned int blf_next_p2(unsigned int x)
 {
@@ -129,3 +131,64 @@ int blf_utf8_next(unsigned char *buf, int *iindex)
        *iindex= index;
        return(r);
 }
+
+void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
+{
+       
+       glBegin(GL_QUADS);
+       glTexCoord2f(uv[0][0], uv[0][1]);
+       glVertex2f(dx, y1);
+       
+       glTexCoord2f(uv[0][0], uv[1][1]);
+       glVertex2f(dx, y2);
+       
+       glTexCoord2f(uv[1][0], uv[1][1]);
+       glVertex2f(dx1, y2);
+       
+       glTexCoord2f(uv[1][0], uv[0][1]);
+       glVertex2f(dx1, y1);
+       glEnd();
+       
+}
+
+void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
+{
+       float soft[25]= {
+               1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
+               1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
+               2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
+               1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
+               1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
+       
+       float color[4], *fp= soft;
+       int dx, dy;
+       
+       glGetFloatv(GL_CURRENT_COLOR, color);
+       
+       for(dx=-2; dx<3; dx++) {
+               for(dy=-2; dy<3; dy++, fp++) {
+                       glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
+                       blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
+               }
+       }
+       
+       glColor4fv(color);
+}
+
+void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
+{
+       float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 4/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 1/16.0f};
+       float color[4], *fp= soft;
+       int dx, dy;
+       
+       glGetFloatv(GL_CURRENT_COLOR, color);
+       
+       for(dx=-1; dx<2; dx++) {
+               for(dy=-1; dy<2; dy++, fp++) {
+                       glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
+                       blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
+               }
+       }
+       
+       glColor4fv(color);
+}
index 99702097329b8c3deb422ca0149e22c91076b904..87e70523870125bb927fa361bd548dec2d63e1e5 100644 (file)
@@ -165,7 +165,11 @@ void uiStyleFontDraw(uiFontStyle *fs, rcti *rect, char *str)
                xofs= rect->xmax - rect->xmin - BLF_width(str);
        
        /* clip is very strict, so we give it some space */
-       BLF_clipping(rect->xmin-4, rect->ymin-4, rect->xmax+4, rect->ymax+4);
+       if (BLF_type_get() == BLF_FONT_INTERNAL)
+               BLF_clipping(rect->xmin-4, rect->ymin-4, rect->xmax+8, rect->ymax+4);
+       else
+               BLF_clipping(rect->xmin-4, rect->ymin-4, rect->xmax+4, rect->ymax+4);
+
        BLF_enable(BLF_CLIPPING);
        
        if(fs->shadow) 
@@ -229,12 +233,21 @@ void uiStyleInit(void)
                        if(font->blf_id == -1)
                                font->blf_id= BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
                }
-                       
+
+               if (font->blf_id == -1) {
+                       /* when all fail, we go back to the internal font. */
+                       font->blf_id= BLF_load_mem("helv", NULL, 0);
+               }
+
                if (font->blf_id == -1)
                        printf("uiStyleInit error, no fonts available\n");
                else {
                        BLF_set(font->blf_id);
-                       BLF_size(11, U.dpi); /* ? just for speed to initialize? */
+                       /* ? just for speed to initialize?
+                        * Yes but only if we used the freetype2 library,
+                        * this build the glyph cache and create the texture.
+                        */
+                       BLF_size(11, U.dpi);
                        BLF_size(12, U.dpi);
                        BLF_size(14, U.dpi);
                }