libdrawtext

changeset 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 11c8b34b0da5
children b599434670cb
files examples/unicode/unicode.c src/drawtext.h src/font.c
diffstat 3 files changed, 38 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/examples/unicode/unicode.c	Sat Sep 17 10:09:58 2011 +0300
     1.2 +++ b/examples/unicode/unicode.c	Sun Sep 18 01:30:30 2011 +0300
     1.3 @@ -58,12 +58,23 @@
     1.4  }
     1.5  
     1.6  /* various UTF-8 strings */
     1.7 -const char *english_text = "Hello world!";
     1.8 +const char *english_text = "Hello g p world!";
     1.9  const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7";
    1.10  const char *russian_text = "\xd0\xa0\xd0\xb0\xd1\x81\xd1\x86\xd0\xb2\xd0\xb5\xd1\x82\xd0\xb0\xd0\xbb\xd0\xb8 \xd1\x8f\xd0\xb1\xd0\xbb\xd0\xbe\xd0\xbd\xd0\xb8 \xd0\xb8 \xd0\xb3\xd1\x80\xd1\x83\xd1\x88\xd0\xb8";
    1.11  const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac";
    1.12  const char *klingon_text = "\xef\xa3\xa3\xef\xa3\x9d\xef\xa3\x93\xef\xa3\x98\xef\xa3\x9d\xef\xa3\xa2\xef\xa3\xa1\xef\xa3\x9d\xef\xa3\x99";
    1.13  
    1.14 +void draw_box(struct dtx_box *box)
    1.15 +{
    1.16 +	glBegin(GL_LINE_LOOP);
    1.17 +	glColor3f(0, 1, 0);
    1.18 +	glVertex2f(box->x, box->y);
    1.19 +	glVertex2f(box->x + box->width, box->y);
    1.20 +	glVertex2f(box->x + box->width, box->y + box->height);
    1.21 +	glVertex2f(box->x, box->y + box->height);
    1.22 +	glEnd();
    1.23 +	glColor3f(1, 1, 1);
    1.24 +}
    1.25  
    1.26  void disp(void)
    1.27  {
     2.1 --- a/src/drawtext.h	Sat Sep 17 10:09:58 2011 +0300
     2.2 +++ b/src/drawtext.h	Sun Sep 18 01:30:30 2011 +0300
     2.3 @@ -169,6 +169,8 @@
     2.4   */
     2.5  float dtx_char_pos(const char *str, int n);
     2.6  
     2.7 +int dtx_char_at_pt(const char *str, float pt);
     2.8 +
     2.9  #ifdef __cplusplus
    2.10  }
    2.11  #endif
     3.1 --- a/src/font.c	Sat Sep 17 10:09:58 2011 +0300
     3.2 +++ b/src/font.c	Sun Sep 18 01:30:30 2011 +0300
     3.3 @@ -531,14 +531,14 @@
     3.4  			if(px + g->orig_x < x0) {
     3.5  				x0 = px + g->orig_x;
     3.6  			}
     3.7 -			if(py + g->orig_y < y0) {
     3.8 -				y0 = py + g->orig_y;
     3.9 +			if(py - g->orig_y < y0) {
    3.10 +				y0 = py - g->orig_y;
    3.11  			}
    3.12  			if(px + g->orig_x + g->width > x1) {
    3.13  				x1 = px + g->orig_x + g->width;
    3.14  			}
    3.15 -			if(py + g->orig_y + g->height > y1) {
    3.16 -				y1 = py + g->orig_y + g->height;
    3.17 +			if(py - g->orig_y + g->height > y1) {
    3.18 +				y1 = py - g->orig_y + g->height;
    3.19  			}
    3.20  		}
    3.21  	}
    3.22 @@ -581,6 +581,26 @@
    3.23  	return pos;
    3.24  }
    3.25  
    3.26 +int dtx_char_at_pt(const char *str, float pt)
    3.27 +{
    3.28 +	int i;
    3.29 +	float prev_pos = 0.0f, pos = 0.0f;
    3.30 +	struct dtx_glyphmap *gmap;
    3.31 +
    3.32 +	for(i=0; *str; i++) {
    3.33 +		int code = dtx_utf8_char_code(str);
    3.34 +		str = dtx_utf8_next_char((char*)str);
    3.35 +
    3.36 +		gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
    3.37 +		pos += gmap->glyphs[i].advance;
    3.38 +
    3.39 +		if(fabs(pt - prev_pos) < fabs(pt - pos)) {
    3.40 +			break;
    3.41 +		}
    3.42 +		prev_pos = pos;
    3.43 +	}
    3.44 +	return i;
    3.45 +}
    3.46  
    3.47  struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
    3.48  {