libdrawtext

diff src/drawgl.c @ 58:11c8b34b0da5

implemented the metrics functions
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sat, 17 Sep 2011 10:09:58 +0300
parents 095ff7ca4e74
children 9d44f6b0591f
line diff
     1.1 --- a/src/drawgl.c	Fri Sep 16 08:42:07 2011 +0300
     1.2 +++ b/src/drawgl.c	Sat Sep 17 10:09:58 2011 +0300
     1.3 @@ -39,7 +39,6 @@
     1.4  	struct vertex v[6];
     1.5  };
     1.6  
     1.7 -static int init(void);
     1.8  static void cleanup(void);
     1.9  static void add_glyph(struct glyph *g, float x, float y);
    1.10  
    1.11 @@ -51,11 +50,8 @@
    1.12  static unsigned int font_tex;
    1.13  static int buf_mode = DTX_NBF;
    1.14  
    1.15 -static struct dtx_font *font;
    1.16 -static int font_sz;
    1.17  
    1.18 -
    1.19 -static int init(void)
    1.20 +int dtx_gl_init(void)
    1.21  {
    1.22  	if(qbuf) {
    1.23  		return 0;	/* already initialized */
    1.24 @@ -78,14 +74,6 @@
    1.25  }
    1.26  
    1.27  
    1.28 -void dtx_use_font(struct dtx_font *fnt, int sz)
    1.29 -{
    1.30 -	init();
    1.31 -
    1.32 -	font = fnt;
    1.33 -	font_sz = sz;
    1.34 -}
    1.35 -
    1.36  void dtx_vertex_attribs(int vert_attr, int tex_attr)
    1.37  {
    1.38  	vattr = vert_attr;
    1.39 @@ -120,7 +108,7 @@
    1.40  {
    1.41  	struct dtx_glyphmap *gmap;
    1.42  
    1.43 -	if(!font || !(gmap = dtx_get_font_glyphmap(font, font_sz, code))) {
    1.44 +	if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
    1.45  		return;
    1.46  	}
    1.47  	set_glyphmap_texture(gmap);
    1.48 @@ -136,39 +124,27 @@
    1.49  	float pos_x = 0.0f;
    1.50  	float pos_y = 0.0f;
    1.51  
    1.52 -	if(!font) {
    1.53 +	if(!dtx_font) {
    1.54  		return;
    1.55  	}
    1.56  
    1.57  	while(*str) {
    1.58 +		float px, py;
    1.59  		int code = dtx_utf8_char_code(str);
    1.60  		str = dtx_utf8_next_char((char*)str);
    1.61  
    1.62 -		switch(code) {
    1.63 -		case '\n':
    1.64 -			if(buf_mode == DTX_LBF) {
    1.65 -				should_flush = 1;
    1.66 -			}
    1.67 -			pos_x = 0.0;
    1.68 -			pos_y -= gmap->line_advance;
    1.69 -			break;
    1.70 +		if(buf_mode == DTX_LBF && code == '\n') {
    1.71 +			should_flush = 1;
    1.72 +		}
    1.73  
    1.74 -		case '\t':
    1.75 -			pos_x = fmod(pos_x, 4.0) + 4.0;
    1.76 -			break;
    1.77 +		px = pos_x;
    1.78 +		py = pos_y;
    1.79  
    1.80 -		case '\r':
    1.81 -			pos_x = 0.0;
    1.82 -			break;
    1.83 +		if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
    1.84 +			int idx = code - gmap->cstart;
    1.85  
    1.86 -		default:
    1.87 -			if((gmap = dtx_get_font_glyphmap(font, font_sz, code))) {
    1.88 -				int idx = code - gmap->cstart;
    1.89 -
    1.90 -				set_glyphmap_texture(gmap);
    1.91 -				add_glyph(gmap->glyphs + idx, pos_x, pos_y);
    1.92 -				pos_x += gmap->glyphs[idx].advance;
    1.93 -			}
    1.94 +			set_glyphmap_texture(gmap);
    1.95 +			add_glyph(gmap->glyphs + idx, px, py);
    1.96  		}
    1.97  	}
    1.98