libdrawtext

changeset 69:982f2464b8a7

added dtx_printf
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 28 Feb 2013 18:51:33 +0200
parents 1c2c00d432da
children 80a2e0556fc1
files examples/Makefile src/drawgl.c src/drawtext.h
diffstat 3 files changed, 56 insertions(+), 18 deletions(-) [+]
line diff
     1.1 --- a/examples/Makefile	Sun Aug 26 04:43:09 2012 +0300
     1.2 +++ b/examples/Makefile	Thu Feb 28 18:51:33 2013 +0200
     1.3 @@ -12,6 +12,7 @@
     1.4  	cd linlibertine; tar xzvf ../LinLibertineTTF_5.1.3_2011_06_21.tgz
     1.5  	rm -f LinLibertineTTF_5.1.3_2011_06_21.tgz
     1.6  	cp linlibertine/LinLibertine_R.ttf $@
     1.7 +	rm -rf linlibertine
     1.8  
     1.9  fonts/sazanami-mincho.ttf:
    1.10  	mkdir -p fonts
    1.11 @@ -19,6 +20,7 @@
    1.12  	tar xjvf sazanami-20040629.tar.bz2
    1.13  	rm -f sazanami-20040629.tar.bz2
    1.14  	cp sazanami-20040629/sazanami-mincho.ttf $@
    1.15 +	rm -rf sazanami-20040629
    1.16  
    1.17  fonts/klingon.ttf:
    1.18  	mkdir -p fonts
    1.19 @@ -26,3 +28,4 @@
    1.20  	unzip -o tlh_2D00_pIqaD_2D00_US.zip
    1.21  	rm -f tlh_2D00_pIqaD_2D00_US.zip
    1.22  	cp tlh-pIqaD-US/pIqaD.ttf $@
    1.23 +	rm -rf tlh-pIqaD-US
     2.1 --- a/src/drawgl.c	Sun Aug 26 04:43:09 2012 +0300
     2.2 +++ b/src/drawgl.c	Thu Feb 28 18:51:33 2013 +0200
     2.3 @@ -16,6 +16,7 @@
     2.4  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     2.5  */
     2.6  #ifndef NO_OPENGL
     2.7 +#include <stdarg.h>
     2.8  #include <math.h>
     2.9  #include <ctype.h>
    2.10  
    2.11 @@ -117,9 +118,31 @@
    2.12  	dtx_flush();
    2.13  }
    2.14  
    2.15 +static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
    2.16 +{
    2.17 +	struct dtx_glyphmap *gmap;
    2.18 +	float px, py;
    2.19 +	int code = dtx_utf8_char_code(str);
    2.20 +	str = dtx_utf8_next_char((char*)str);
    2.21 +
    2.22 +	if(buf_mode == DTX_LBF && code == '\n') {
    2.23 +		*should_flush = 1;
    2.24 +	}
    2.25 +
    2.26 +	px = *pos_x;
    2.27 +	py = *pos_y;
    2.28 +
    2.29 +	if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
    2.30 +		int idx = code - gmap->cstart;
    2.31 +
    2.32 +		set_glyphmap_texture(gmap);
    2.33 +		add_glyph(gmap->glyphs + idx, px, py);
    2.34 +	}
    2.35 +	return str;
    2.36 +}
    2.37 +
    2.38  void dtx_string(const char *str)
    2.39  {
    2.40 -	struct dtx_glyphmap *gmap;
    2.41  	int should_flush = buf_mode == DTX_NBF;
    2.42  	float pos_x = 0.0f;
    2.43  	float pos_y = 0.0f;
    2.44 @@ -129,23 +152,7 @@
    2.45  	}
    2.46  
    2.47  	while(*str) {
    2.48 -		float px, py;
    2.49 -		int code = dtx_utf8_char_code(str);
    2.50 -		str = dtx_utf8_next_char((char*)str);
    2.51 -
    2.52 -		if(buf_mode == DTX_LBF && code == '\n') {
    2.53 -			should_flush = 1;
    2.54 -		}
    2.55 -
    2.56 -		px = pos_x;
    2.57 -		py = pos_y;
    2.58 -
    2.59 -		if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
    2.60 -			int idx = code - gmap->cstart;
    2.61 -
    2.62 -			set_glyphmap_texture(gmap);
    2.63 -			add_glyph(gmap->glyphs + idx, px, py);
    2.64 -		}
    2.65 +		str = put_char(str, &pos_x, &pos_y, &should_flush);
    2.66  	}
    2.67  
    2.68  	if(should_flush) {
    2.69 @@ -153,6 +160,32 @@
    2.70  	}
    2.71  }
    2.72  
    2.73 +void dtx_printf(const char *fmt, ...)
    2.74 +{
    2.75 +	va_list ap;
    2.76 +	int buf_size;
    2.77 +	char *buf, tmp;
    2.78 +
    2.79 +	if(!dtx_font) {
    2.80 +		return;
    2.81 +	}
    2.82 +
    2.83 +	va_start(ap, fmt);
    2.84 +	buf_size = vsnprintf(&tmp, 0, fmt, ap);
    2.85 +	va_end(ap);
    2.86 +
    2.87 +	if(buf_size == -1) {
    2.88 +		buf_size = 512;
    2.89 +	}
    2.90 +
    2.91 +	buf = alloca(buf_size + 1);
    2.92 +	va_start(ap, fmt);
    2.93 +	vsnprintf(buf, buf_size + 1, fmt, ap);
    2.94 +	va_end(ap);
    2.95 +
    2.96 +	dtx_string(buf);
    2.97 +}
    2.98 +
    2.99  static void qvertex(struct vertex *v, float x, float y, float s, float t)
   2.100  {
   2.101  	v->x = x;
     3.1 --- a/src/drawtext.h	Sun Aug 26 04:43:09 2012 +0300
     3.2 +++ b/src/drawtext.h	Thu Feb 28 18:51:33 2013 +0200
     3.3 @@ -116,6 +116,8 @@
     3.4  /* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */
     3.5  void dtx_string(const char *str);
     3.6  
     3.7 +void dtx_printf(const char *fmt, ...);
     3.8 +
     3.9  /* render any pending glyphs (see dtx_draw_buffering) */
    3.10  void dtx_flush(void);
    3.11