# HG changeset patch # User John Tsiombikas # Date 1316118759 -10800 # Node ID 59e5858de836449a6c488615ae2c5b4df2e3678a # Parent 4b5bdd7b2cb8553c404e04d51232885cf66eb1e0 fixed a bug in utf-8 decoding diff -r 4b5bdd7b2cb8 -r 59e5858de836 configure --- a/configure Thu Sep 15 13:56:54 2011 +0300 +++ b/configure Thu Sep 15 23:32:39 2011 +0300 @@ -9,7 +9,6 @@ case $1 in --prefix=*) prefix=`echo $1 | sed 's/^--prefix=//'` - shift ;; --enable-opt) opt=true @@ -30,6 +29,7 @@ use_ft2=false ;; esac + shift done echo 'Configuring libdrawtext...' diff -r 4b5bdd7b2cb8 -r 59e5858de836 examples/Makefile --- a/examples/Makefile Thu Sep 15 13:56:54 2011 +0300 +++ b/examples/Makefile Thu Sep 15 23:32:39 2011 +0300 @@ -1,10 +1,19 @@ -fonts = fonts/FreeSerif.ttf +fonts = fonts/FreeSerif.ttf fonts/klingon.ttf .PHONY: all all: $(fonts) cd simple; $(MAKE) fonts/FreeSerif.ttf: + mkdir -p fonts wget http://ftp.gnu.org/gnu/freefont/freefont-ttf-20100919.tar.gz tar xzvf freefont-ttf-20100919.tar.gz - mv freefont-20100919 fonts + rm -f freefont-ttf-20100919.tar.gz + cp freefont-20100919/FreeSerif.ttf $@ + +fonts/klingon.ttf: + mkdir -p fonts + wget http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-00-53-45-33/tlh_2D00_pIqaD_2D00_US.zip + unzip -o tlh_2D00_pIqaD_2D00_US.zip + rm -f tlh_2D00_pIqaD_2D00_US.zip + cp tlh-pIqaD-US/pIqaD.ttf $@ diff -r 4b5bdd7b2cb8 -r 59e5858de836 examples/unicode/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/unicode/Makefile Thu Sep 15 23:32:39 2011 +0300 @@ -0,0 +1,34 @@ +obj = unicode.o +bin = unicode + +CC = gcc +CFLAGS = -pedantic -Wall -g -I../../src +LDFLAGS = -L. -ldrawtext $(libgl) + +lib_so = libdrawtext.so +fonts = serif.ttf klingon.ttf + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -framework GLUT +else + libgl = -lGL -lGLU -lglut +endif + +$(bin): $(obj) $(lib_so) $(fonts) + $(CC) -o $@ $(obj) $(LDFLAGS) + +$(lib_so): ../../libdrawtext.so.0.0 + rm -f $@ + ln -s $< $@ + +serif.ttf: ../fonts/FreeSerif.ttf + rm -f $@ + ln -s $< $@ + +klingon.ttf: ../fonts/klingon.ttf + rm -f $@ + ln -s $< $@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) $(lib_so) diff -r 4b5bdd7b2cb8 -r 59e5858de836 examples/unicode/unicode.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/unicode/unicode.c Thu Sep 15 23:32:39 2011 +0300 @@ -0,0 +1,104 @@ +/* Unicode libdrawtext example. + * + * Important parts are marked with XXX comments. + */ +#include +#include + +#ifndef __APPLE__ +#include +#else +#include +#endif + +#include "drawtext.h" + +void disp(void); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); + +/* XXX fonts are represented by the opaque struct dtx_font type, so you + * need to create at least one with dtx_open_font (see main). + */ +struct dtx_font *freeserif, *klingon; + +#define FONT_SIZE 24 + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitWindowSize(512, 384); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + glutCreateWindow("libdrawtext example: simple"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + + /* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */ + if(!(freeserif = dtx_open_font("serif.ttf", 0))) { + return 1; + } + dtx_prepare_range(freeserif, FONT_SIZE, 0, 256); /* prepare ASCII */ + dtx_prepare_range(freeserif, FONT_SIZE, 0x0370, 0x0400); /* greek */ + dtx_prepare_range(freeserif, FONT_SIZE, 0x4e00, 0x9fc0); /* kanji */ + + if(!(klingon = dtx_open_font("klingon.ttf", 0))) { + return 1; + } + dtx_prepare_range(klingon, FONT_SIZE, 0xf8d0, 0xf900); + + glutMainLoop(); + return 0; +} + +const char *ascii_text = "Hello world!"; +const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7"; +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 disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + dtx_use_font(freeserif, FONT_SIZE); + + glTranslatef(-200, 150, 0); + /* XXX call dtx_string to draw utf-8 text. + * any transformations and the current color apply + */ + dtx_string(ascii_text); + + glTranslatef(0, -40, 0); + dtx_string(greek_text); + + glTranslatef(0, -40, 0); + dtx_string(kanji_text); + + dtx_use_font(klingon, FONT_SIZE); + + glTranslatef(0, -40, 0); + dtx_string(klingon_text); + + glutSwapBuffers(); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-x/2, x/2, -y/2, y/2, -1, 1); +} + +void keyb(unsigned char key, int x, int y) +{ + if(key == 27) { + exit(0); + } +} diff -r 4b5bdd7b2cb8 -r 59e5858de836 src/drawgl.c --- a/src/drawgl.c Thu Sep 15 13:56:54 2011 +0300 +++ b/src/drawgl.c Thu Sep 15 23:32:39 2011 +0300 @@ -32,7 +32,7 @@ static int vattr = -1; static int tattr = -1; static unsigned int font_tex; -static int buf_mode = DTX_LBF; +static int buf_mode = DTX_NBF; static struct dtx_font *font; static int font_sz; diff -r 4b5bdd7b2cb8 -r 59e5858de836 src/font.c --- a/src/font.c Thu Sep 15 13:56:54 2011 +0300 +++ b/src/font.c Thu Sep 15 23:32:39 2011 +0300 @@ -155,7 +155,7 @@ FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72); - if(!(gmap = malloc(sizeof *gmap))) { + if(!(gmap = calloc(1, sizeof *gmap))) { return 0; } diff -r 4b5bdd7b2cb8 -r 59e5858de836 src/utf8.c --- a/src/utf8.c Thu Sep 15 13:56:54 2011 +0300 +++ b/src/utf8.c Thu Sep 15 23:32:39 2011 +0300 @@ -9,7 +9,7 @@ 0xf, /* three-bytes, 4 bits valid */ 0x7 /* four-bytes, 3 bits valid */ }; -static const char first_shift[] = { 7, 5, 4, 3 }; /* see above */ +static const char first_shift[] = { 0, 7, 5, 4, 3 }; /* see above */ #define CONT_PREFIX 0x80 #define CONT_MASK 0x3f @@ -30,7 +30,7 @@ int dtx_utf8_char_code(const char *str) { int i, nbytes, shift, code = 0; - char mask; + int mask; if(!U8_IS_FIRST(*str)) { return -1; @@ -47,9 +47,10 @@ code = (code << shift) | (*str++ & mask); mask = 0x3f; - shift = i == 0 ? first_shift[nbytes] : 6; + shift = 6; } + printf("code: %x\n", code); return code; }