blf - further shrink drawing functions & some style changes.

This commit is contained in:
Campbell Barton 2011-09-18 10:34:13 +00:00
parent 249b41762a
commit 53845a37d9
6 changed files with 120 additions and 150 deletions

View File

@ -66,7 +66,7 @@ void BLF_size(int fontid, int size, int dpi);
| m[3] m[7] m[11] m[15] |
*/
void BLF_matrix(int fontid, double *m);
void BLF_matrix(int fontid, const double m[16]);
/* Draw the string using the default font, size and dpi. */
void BLF_draw_default(float x, float y, float z, const char *str, size_t len);

View File

@ -78,8 +78,8 @@ int blf_mono_font_render= -1;
static FontBLF *BLF_get(int fontid)
{
if (fontid >= 0 && fontid < BLF_MAX_FONT)
return(global_font[fontid]);
return(NULL);
return global_font[fontid];
return NULL;
}
int BLF_init(int points, int dpi)
@ -91,7 +91,7 @@ int BLF_init(int points, int dpi)
global_font_points= points;
global_font_dpi= dpi;
return(blf_font_init());
return blf_font_init();
}
void BLF_exit(void)
@ -128,9 +128,9 @@ static int blf_search(const char *name)
for (i= 0; i < BLF_MAX_FONT; i++) {
font= global_font[i];
if (font && (!strcmp(font->name, name)))
return(i);
return i;
}
return(-1);
return -1;
}
int BLF_load(const char *name)
@ -140,24 +140,24 @@ int BLF_load(const char *name)
int i;
if (!name)
return(-1);
return -1;
/* check if we already load this font. */
i= blf_search(name);
if (i >= 0) {
/*font= global_font[i];*/ /*UNUSED*/
return(i);
return i;
}
if (global_font_num+1 >= BLF_MAX_FONT) {
printf("Too many fonts!!!\n");
return(-1);
return -1;
}
filename= blf_dir_search(name);
if (!filename) {
printf("Can't find font: %s\n", name);
return(-1);
return -1;
}
font= blf_font_new(name, filename);
@ -165,13 +165,13 @@ int BLF_load(const char *name)
if (!font) {
printf("Can't load font: %s\n", name);
return(-1);
return -1;
}
global_font[global_font_num]= font;
i= global_font_num;
global_font_num++;
return(i);
return i;
}
int BLF_load_unique(const char *name)
@ -181,20 +181,20 @@ int BLF_load_unique(const char *name)
int i;
if (!name)
return(-1);
return -1;
/* Don't search in the cache!! make a new
* object font, this is for keep fonts threads safe.
*/
if (global_font_num+1 >= BLF_MAX_FONT) {
printf("Too many fonts!!!\n");
return(-1);
return -1;
}
filename= blf_dir_search(name);
if (!filename) {
printf("Can't find font: %s\n", name);
return(-1);
return -1;
}
font= blf_font_new(name, filename);
@ -202,13 +202,13 @@ int BLF_load_unique(const char *name)
if (!font) {
printf("Can't load font: %s\n", name);
return(-1);
return -1;
}
global_font[global_font_num]= font;
i= global_font_num;
global_font_num++;
return(i);
return i;
}
void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size)
@ -226,34 +226,34 @@ int BLF_load_mem(const char *name, unsigned char *mem, int mem_size)
int i;
if (!name)
return(-1);
return -1;
i= blf_search(name);
if (i >= 0) {
/*font= global_font[i];*/ /*UNUSED*/
return(i);
return i;
}
if (global_font_num+1 >= BLF_MAX_FONT) {
printf("Too many fonts!!!\n");
return(-1);
return -1;
}
if (!mem || !mem_size) {
printf("Can't load font: %s from memory!!\n", name);
return(-1);
return -1;
}
font= blf_font_new_from_mem(name, mem, mem_size);
if (!font) {
printf("Can't load font: %s from memory!!\n", name);
return(-1);
return -1;
}
global_font[global_font_num]= font;
i= global_font_num;
global_font_num++;
return(i);
return i;
}
int BLF_load_mem_unique(const char *name, unsigned char *mem, int mem_size)
@ -262,7 +262,7 @@ int BLF_load_mem_unique(const char *name, unsigned char *mem, int mem_size)
int i;
if (!name)
return(-1);
return -1;
/*
* Don't search in the cache, make a new object font!
@ -270,24 +270,24 @@ int BLF_load_mem_unique(const char *name, unsigned char *mem, int mem_size)
*/
if (global_font_num+1 >= BLF_MAX_FONT) {
printf("Too many fonts!!!\n");
return(-1);
return -1;
}
if (!mem || !mem_size) {
printf("Can't load font: %s from memory!!\n", name);
return(-1);
return -1;
}
font= blf_font_new_from_mem(name, mem, mem_size);
if (!font) {
printf("Can't load font: %s from memory!!\n", name);
return(-1);
return -1;
}
global_font[global_font_num]= font;
i= global_font_num;
global_font_num++;
return(i);
return i;
}
void BLF_enable(int fontid, int option)
@ -338,26 +338,25 @@ void BLF_aspect(int fontid, float x, float y, float z)
}
}
void BLF_matrix(int fontid, double *m)
void BLF_matrix(int fontid, const double m[16])
{
FontBLF *font;
int i;
font= BLF_get(fontid);
if (font) {
for (i= 0; i < 16; i++)
font->m[i]= m[i];
memcpy(font->m, m, sizeof(font->m));
}
}
void BLF_position(int fontid, float x, float y, float z)
{
FontBLF *font;
float remainder;
float xa, ya, za;
font= BLF_get(fontid);
if (font) {
float xa, ya, za;
float remainder;
if (font->flags & BLF_ASPECT) {
xa= font->aspect[0];
ya= font->aspect[1];
@ -546,8 +545,8 @@ float BLF_width(int fontid, const char *str)
font= BLF_get(fontid);
if (font && font->glyph_cache)
return(blf_font_width(font, str));
return(0.0f);
return blf_font_width(font, str);
return 0.0f;
}
float BLF_fixed_width(int fontid)
@ -562,19 +561,16 @@ float BLF_fixed_width(int fontid)
float BLF_width_default(const char *str)
{
float width;
if (global_font_default == -1)
global_font_default= blf_search("default");
if (global_font_default == -1) {
printf("Error: Can't found default font!!\n");
return(0.0f);
return 0.0f;
}
BLF_size(global_font_default, global_font_points, global_font_dpi);
width= BLF_width(global_font_default, str);
return(width);
return BLF_width(global_font_default, str);
}
float BLF_height(int fontid, const char *str)
@ -583,8 +579,8 @@ float BLF_height(int fontid, const char *str)
font= BLF_get(fontid);
if (font && font->glyph_cache)
return(blf_font_height(font, str));
return(0.0f);
return blf_font_height(font, str);
return 0.0f;
}
float BLF_height_max(int fontid)
@ -593,8 +589,8 @@ float BLF_height_max(int fontid)
font= BLF_get(fontid);
if (font && font->glyph_cache)
return(font->glyph_cache->max_glyph_height);
return(0.0f);
return font->glyph_cache->max_glyph_height;
return 0.0f;
}
float BLF_width_max(int fontid)
@ -602,11 +598,10 @@ float BLF_width_max(int fontid)
FontBLF *font;
font= BLF_get(fontid);
if (font) {
if(font->glyph_cache)
return(font->glyph_cache->max_glyph_width);
if (font && font->glyph_cache) {
return font->glyph_cache->max_glyph_width;
}
return(0.0f);
return 0.0f;
}
float BLF_descender(int fontid)
@ -614,11 +609,10 @@ float BLF_descender(int fontid)
FontBLF *font;
font= BLF_get(fontid);
if (font) {
if(font->glyph_cache)
return(font->glyph_cache->descender);
if (font && font->glyph_cache) {
return font->glyph_cache->descender;
}
return(0.0f);
return 0.0f;
}
float BLF_ascender(int fontid)
@ -626,28 +620,25 @@ float BLF_ascender(int fontid)
FontBLF *font;
font= BLF_get(fontid);
if (font) {
if(font->glyph_cache)
return(font->glyph_cache->ascender);
if (font && font->glyph_cache) {
return font->glyph_cache->ascender;
}
return(0.0f);
return 0.0f;
}
float BLF_height_default(const char *str)
{
float height;
if (global_font_default == -1)
global_font_default= blf_search("default");
if (global_font_default == -1) {
printf("Error: Can't found default font!!\n");
return(0.0f);
return 0.0f;
}
BLF_size(global_font_default, global_font_points, global_font_dpi);
height= BLF_height(global_font_default, str);
return(height);
return BLF_height(global_font_default, str);
}
void BLF_rotation(int fontid, float angle)

View File

@ -62,10 +62,10 @@ static DirBLF *blf_dir_find(const char *path)
p= global_font_dir.first;
while (p) {
if (BLI_path_cmp(p->path, path) == 0)
return(p);
return p;
p= p->next;
}
return(NULL);
return NULL;
}
void BLF_dir_add(const char *path)
@ -102,7 +102,7 @@ char **BLF_dir_get(int *ndir)
count= BLI_countlist(&global_font_dir);
if (!count)
return(NULL);
return NULL;
dirs= (char **)MEM_mallocN(sizeof(char *) * count, "BLF_dir_get");
p= global_font_dir.first;
@ -113,7 +113,7 @@ char **BLF_dir_get(int *ndir)
p= p->next;
}
*ndir= i;
return(dirs);
return dirs;
}
void BLF_dir_free(char **dirs, int count)
@ -147,8 +147,8 @@ char *blf_dir_search(const char *file)
if (BLI_exist(file))
s= BLI_strdup(file);
}
return(s);
return s;
}
#if 0 // UNUSED
@ -171,9 +171,9 @@ int blf_dir_split(const char *str, char *file, int *size)
file[i+4]= '\0';
s++;
*size= atoi(s);
return(1);
return 1;
}
return(0);
return 0;
}
#endif
@ -188,9 +188,9 @@ char *blf_dir_metrics_search(const char *filename)
mfile= BLI_strdup(filename);
s= strrchr(mfile, '.');
if (s) {
if (strlen(s) < 4) {
if (strnlen(s, 4) < 4) {
MEM_freeN(mfile);
return(NULL);
return NULL;
}
s++;
s[0]= 'a';
@ -199,14 +199,14 @@ char *blf_dir_metrics_search(const char *filename)
/* first check .afm */
if (BLI_exist(s))
return(s);
return s;
/* and now check .pfm */
s[0]= 'p';
if (BLI_exist(s))
return(s);
return s;
}
MEM_freeN(mfile);
return(NULL);
return NULL;
}

View File

@ -62,7 +62,7 @@ static FT_Library ft_lib;
int blf_font_init(void)
{
return(FT_Init_FreeType(&ft_lib));
return FT_Init_FreeType(&ft_lib);
}
void blf_font_exit(void)
@ -156,19 +156,14 @@ static void blf_font_ensure_ascii_table(FontBLF *font)
void blf_font_draw(FontBLF *font, const char *str, unsigned int len)
{
unsigned int c;
GlyphBLF *g, *g_prev;
GlyphBLF *g, *g_prev= NULL;
FT_Vector delta;
int pen_x, pen_y;
unsigned int i;
int pen_x= 0, pen_y= 0;
unsigned int i= 0;
GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
BLF_KERNING_VARS(font, has_kerning, kern_mode);
i= 0;
pen_x= 0;
pen_y= 0;
g_prev= NULL;
blf_font_ensure_ascii_table(font);
while (str[i] && i < len) {
@ -190,18 +185,14 @@ void blf_font_draw(FontBLF *font, const char *str, unsigned int len)
/* faster version of blf_font_draw, ascii only for view dimensions */
void blf_font_draw_ascii(FontBLF *font, const char *str, unsigned int len)
{
char c;
GlyphBLF *g, *g_prev;
unsigned char c;
GlyphBLF *g, *g_prev= NULL;
FT_Vector delta;
int pen_x, pen_y;
int pen_x= 0, pen_y= 0;
GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
BLF_KERNING_VARS(font, has_kerning, kern_mode);
pen_x= 0;
pen_y= 0;
g_prev= NULL;
blf_font_ensure_ascii_table(font);
while ((c= *(str++)) && len--) {
@ -219,27 +210,24 @@ void blf_font_draw_ascii(FontBLF *font, const char *str, unsigned int len)
/* Sanity checks are done by BLF_draw_buffer() */
void blf_font_buffer(FontBLF *font, const char *str)
{
unsigned char *cbuf;
unsigned int c;
unsigned char b_col_char[4];
GlyphBLF *g, *g_prev;
GlyphBLF *g, *g_prev= NULL;
FT_Vector delta;
float a, *fbuf;
int pen_x, pen_y, y, x;
int chx, chy;
unsigned int i;
int pen_x= (int)font->pos[0], pen_y= 0;
unsigned int i= 0;
GlyphBLF **glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
BLF_KERNING_VARS(font, has_kerning, kern_mode);
/* buffer spesific vars*/
const unsigned char b_col_char[4]= {font->b_col[0] * 255,
font->b_col[1] * 255,
font->b_col[2] * 255,
font->b_col[3] * 255};
unsigned char *cbuf;
int chx, chy;
int y, x;
float a, *fbuf;
i= 0;
pen_x= (int)font->pos[0];
g_prev= NULL;
b_col_char[0]= font->b_col[0] * 255;
b_col_char[1]= font->b_col[1] * 255;
b_col_char[2]= font->b_col[2] * 255;
b_col_char[3]= font->b_col[3] * 255;
BLF_KERNING_VARS(font, has_kerning, kern_mode);
blf_font_ensure_ascii_table(font);
@ -349,10 +337,10 @@ void blf_font_buffer(FontBLF *font, const char *str)
void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
{
unsigned int c;
GlyphBLF *g, *g_prev;
GlyphBLF *g, *g_prev= NULL;
FT_Vector delta;
int pen_x, pen_y;
unsigned int i;
int pen_x= 0, pen_y= 0;
unsigned int i= 0;
GlyphBLF **glyph_ascii_table;
rctf gbox;
@ -364,11 +352,6 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
box->ymin= 32000.0f;
box->ymax= -32000.0f;
i= 0;
pen_x= 0;
pen_y= 0;
g_prev= NULL;
blf_font_ensure_ascii_table(font);
glyph_ascii_table= font->glyph_cache->glyph_ascii_table;
@ -385,15 +368,11 @@ void blf_font_boundbox(FontBLF *font, const char *str, rctf *box)
gbox.ymin= g->box.ymin + pen_y;
gbox.ymax= g->box.ymax + pen_y;
if (gbox.xmin < box->xmin)
box->xmin= gbox.xmin;
if (gbox.ymin < box->ymin)
box->ymin= gbox.ymin;
if (gbox.xmin < box->xmin) box->xmin= gbox.xmin;
if (gbox.ymin < box->ymin) box->ymin= gbox.ymin;
if (gbox.xmax > box->xmax)
box->xmax= gbox.xmax;
if (gbox.ymax > box->ymax)
box->ymax= gbox.ymax;
if (gbox.xmax > box->xmax) box->xmax= gbox.xmax;
if (gbox.ymax > box->ymax) box->ymax= gbox.ymax;
pen_x += g->advance;
g_prev= g;
@ -437,7 +416,7 @@ float blf_font_width(FontBLF *font, const char *str)
xa= 1.0f;
blf_font_boundbox(font, str, &box);
return((box.xmax - box.xmin) * xa);
return (box.xmax - box.xmin) * xa;
}
float blf_font_height(FontBLF *font, const char *str)
@ -451,7 +430,7 @@ float blf_font_height(FontBLF *font, const char *str)
ya= 1.0f;
blf_font_boundbox(font, str, &box);
return((box.ymax - box.ymin) * ya);
return (box.ymax - box.ymin) * ya;
}
float blf_font_fixed_width(FontBLF *font)
@ -537,7 +516,7 @@ FontBLF *blf_font_new(const char *name, const char *filename)
err= FT_New_Face(ft_lib, filename, 0, &font->face);
if (err) {
MEM_freeN(font);
return(NULL);
return NULL;
}
err= FT_Select_Charmap(font->face, ft_encoding_unicode);
@ -545,7 +524,7 @@ FontBLF *blf_font_new(const char *name, const char *filename)
printf("Can't set the unicode character map!\n");
FT_Done_Face(font->face);
MEM_freeN(font);
return(NULL);
return NULL;
}
mfile= blf_dir_metrics_search(filename);
@ -557,7 +536,7 @@ FontBLF *blf_font_new(const char *name, const char *filename)
font->name= BLI_strdup(name);
font->filename= BLI_strdup(filename);
blf_font_fill(font);
return(font);
return font;
}
void blf_font_attach_from_mem(FontBLF *font, const unsigned char *mem, int mem_size)
@ -579,7 +558,7 @@ FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_siz
err= FT_New_Memory_Face(ft_lib, mem, mem_size, 0, &font->face);
if (err) {
MEM_freeN(font);
return(NULL);
return NULL;
}
err= FT_Select_Charmap(font->face, ft_encoding_unicode);
@ -587,11 +566,11 @@ FontBLF *blf_font_new_from_mem(const char *name, unsigned char *mem, int mem_siz
printf("Can't set the unicode character map!\n");
FT_Done_Face(font->face);
MEM_freeN(font);
return(NULL);
return NULL;
}
font->name= BLI_strdup(name);
font->filename= NULL;
blf_font_fill(font);
return(font);
return font;
}

View File

@ -64,10 +64,10 @@ GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, int size, int dpi)
p= (GlyphCacheBLF *)font->cache.first;
while (p) {
if (p->size == size && p->dpi == dpi)
return(p);
return p;
p= p->next;
}
return(NULL);
return NULL;
}
/* Create a new glyph cache for the current size and dpi. */
@ -114,7 +114,7 @@ GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
gc->p2_height= 0;
BLI_addhead(&font->cache, gc);
return(gc);
return gc;
}
void blf_glyph_cache_clear(FontBLF *font)
@ -204,10 +204,10 @@ GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
p= gc->bucket[key].first;
while (p) {
if (p->c == c)
return(p);
return p;
p= p->next;
}
return(NULL);
return NULL;
}
GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
@ -222,14 +222,14 @@ GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
g= blf_glyph_search(font->glyph_cache, c);
if (g)
return(g);
return g;
if (sharp)
err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
else
err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP); /* Sure about NO_* flags? */
if (err)
return(NULL);
return NULL;
/* get the glyph. */
slot= font->face->glyph;
@ -248,7 +248,7 @@ GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
}
if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
return(NULL);
return NULL;
g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
g->next= NULL;
@ -294,7 +294,7 @@ GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
key= blf_hash(g->c);
BLI_addhead(&(font->glyph_cache->bucket[key]), g);
return(g);
return g;
}
void blf_glyph_free(GlyphBLF *g)
@ -383,7 +383,7 @@ int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
float xo, yo;
if ((!g->width) || (!g->height))
return(1);
return 1;
if (g->build_tex == 0) {
GlyphCacheBLF *gc= font->glyph_cache;
@ -449,13 +449,13 @@ int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
if (font->flags & BLF_CLIPPING) {
if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
return(0);
return 0;
if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
return(0);
return 0;
if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
return(0);
return 0;
if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
return(0);
return 0;
}
if (font->tex_bind_state != g->tex) {
@ -500,5 +500,5 @@ int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
break;
}
return(1);
return 1;
}

View File

@ -46,7 +46,7 @@ unsigned int blf_next_p2(unsigned int x)
x |= (x >> 2);
x |= (x >> 1);
x += 1;
return(x);
return x;
}
unsigned int blf_hash(unsigned int val)
@ -60,7 +60,7 @@ unsigned int blf_hash(unsigned int val)
key ^= (key >> 13);
key += ~(key << 9);
key ^= (key >> 17);
return(key % 257);
return key % 257;
}
/*
@ -85,7 +85,7 @@ int blf_utf8_next(unsigned char *buf, unsigned int *iindex)
d= buf[index++];
if (!d)
return(0);
return 0;
while (buf[index] && ((buf[index] & 0xc0) == 0x80))
index++;
@ -124,5 +124,5 @@ int blf_utf8_next(unsigned char *buf, unsigned int *iindex)
r |= (d4 & 0x3f);
}
*iindex= index;
return(r);
return r;
}