libdrawtext

diff src/font.c @ 59:e0957fc47162

added dtx_char_at_pt (not tested)
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 18 Sep 2011 01:30:30 +0300
parents 7e0c702f1223
children 365eba4dccec
line diff
     1.1 --- a/src/font.c	Sat Sep 17 10:09:58 2011 +0300
     1.2 +++ b/src/font.c	Sun Sep 18 01:30:30 2011 +0300
     1.3 @@ -531,14 +531,14 @@
     1.4  			if(px + g->orig_x < x0) {
     1.5  				x0 = px + g->orig_x;
     1.6  			}
     1.7 -			if(py + g->orig_y < y0) {
     1.8 -				y0 = py + g->orig_y;
     1.9 +			if(py - g->orig_y < y0) {
    1.10 +				y0 = py - g->orig_y;
    1.11  			}
    1.12  			if(px + g->orig_x + g->width > x1) {
    1.13  				x1 = px + g->orig_x + g->width;
    1.14  			}
    1.15 -			if(py + g->orig_y + g->height > y1) {
    1.16 -				y1 = py + g->orig_y + g->height;
    1.17 +			if(py - g->orig_y + g->height > y1) {
    1.18 +				y1 = py - g->orig_y + g->height;
    1.19  			}
    1.20  		}
    1.21  	}
    1.22 @@ -581,6 +581,26 @@
    1.23  	return pos;
    1.24  }
    1.25  
    1.26 +int dtx_char_at_pt(const char *str, float pt)
    1.27 +{
    1.28 +	int i;
    1.29 +	float prev_pos = 0.0f, pos = 0.0f;
    1.30 +	struct dtx_glyphmap *gmap;
    1.31 +
    1.32 +	for(i=0; *str; i++) {
    1.33 +		int code = dtx_utf8_char_code(str);
    1.34 +		str = dtx_utf8_next_char((char*)str);
    1.35 +
    1.36 +		gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
    1.37 +		pos += gmap->glyphs[i].advance;
    1.38 +
    1.39 +		if(fabs(pt - prev_pos) < fabs(pt - pos)) {
    1.40 +			break;
    1.41 +		}
    1.42 +		prev_pos = pos;
    1.43 +	}
    1.44 +	return i;
    1.45 +}
    1.46  
    1.47  struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
    1.48  {