# HG changeset patch # User John Tsiombikas # Date 1316298630 -10800 # Node ID e0957fc471621b11f677d0736b4fdd1dac4fa89e # Parent 11c8b34b0da55ee869b8c79de2d512aceec7594a added dtx_char_at_pt (not tested) diff -r 11c8b34b0da5 -r e0957fc47162 examples/unicode/unicode.c --- a/examples/unicode/unicode.c Sat Sep 17 10:09:58 2011 +0300 +++ b/examples/unicode/unicode.c Sun Sep 18 01:30:30 2011 +0300 @@ -58,12 +58,23 @@ } /* various UTF-8 strings */ -const char *english_text = "Hello world!"; +const char *english_text = "Hello g p world!"; const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7"; 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"; const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac"; 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"; +void draw_box(struct dtx_box *box) +{ + glBegin(GL_LINE_LOOP); + glColor3f(0, 1, 0); + glVertex2f(box->x, box->y); + glVertex2f(box->x + box->width, box->y); + glVertex2f(box->x + box->width, box->y + box->height); + glVertex2f(box->x, box->y + box->height); + glEnd(); + glColor3f(1, 1, 1); +} void disp(void) { diff -r 11c8b34b0da5 -r e0957fc47162 src/drawtext.h --- a/src/drawtext.h Sat Sep 17 10:09:58 2011 +0300 +++ b/src/drawtext.h Sun Sep 18 01:30:30 2011 +0300 @@ -169,6 +169,8 @@ */ float dtx_char_pos(const char *str, int n); +int dtx_char_at_pt(const char *str, float pt); + #ifdef __cplusplus } #endif diff -r 11c8b34b0da5 -r e0957fc47162 src/font.c --- a/src/font.c Sat Sep 17 10:09:58 2011 +0300 +++ b/src/font.c Sun Sep 18 01:30:30 2011 +0300 @@ -531,14 +531,14 @@ if(px + g->orig_x < x0) { x0 = px + g->orig_x; } - if(py + g->orig_y < y0) { - y0 = py + g->orig_y; + if(py - g->orig_y < y0) { + y0 = py - g->orig_y; } if(px + g->orig_x + g->width > x1) { x1 = px + g->orig_x + g->width; } - if(py + g->orig_y + g->height > y1) { - y1 = py + g->orig_y + g->height; + if(py - g->orig_y + g->height > y1) { + y1 = py - g->orig_y + g->height; } } } @@ -581,6 +581,26 @@ return pos; } +int dtx_char_at_pt(const char *str, float pt) +{ + int i; + float prev_pos = 0.0f, pos = 0.0f; + struct dtx_glyphmap *gmap; + + for(i=0; *str; i++) { + int code = dtx_utf8_char_code(str); + str = dtx_utf8_next_char((char*)str); + + gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code); + pos += gmap->glyphs[i].advance; + + if(fabs(pt - prev_pos) < fabs(pt - pos)) { + break; + } + prev_pos = pos; + } + return i; +} struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos) {