libdrawtext
changeset 3:fe0c54e574ae
fixed a bug in utf-8 decoding
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 15 Sep 2011 23:32:39 +0300 |
parents | d8c3ce3fa588 |
children | 17fed026b24b |
files | configure examples/Makefile examples/unicode/Makefile examples/unicode/unicode.c src/drawgl.c src/font.c src/utf8.c |
diffstat | 7 files changed, 156 insertions(+), 8 deletions(-) [+] |
line diff
1.1 --- a/configure Thu Sep 15 13:56:54 2011 +0300 1.2 +++ b/configure Thu Sep 15 23:32:39 2011 +0300 1.3 @@ -9,7 +9,6 @@ 1.4 case $1 in 1.5 --prefix=*) 1.6 prefix=`echo $1 | sed 's/^--prefix=//'` 1.7 - shift 1.8 ;; 1.9 --enable-opt) 1.10 opt=true 1.11 @@ -30,6 +29,7 @@ 1.12 use_ft2=false 1.13 ;; 1.14 esac 1.15 + shift 1.16 done 1.17 1.18 echo 'Configuring libdrawtext...'
2.1 --- a/examples/Makefile Thu Sep 15 13:56:54 2011 +0300 2.2 +++ b/examples/Makefile Thu Sep 15 23:32:39 2011 +0300 2.3 @@ -1,10 +1,19 @@ 2.4 -fonts = fonts/FreeSerif.ttf 2.5 +fonts = fonts/FreeSerif.ttf fonts/klingon.ttf 2.6 2.7 .PHONY: all 2.8 all: $(fonts) 2.9 cd simple; $(MAKE) 2.10 2.11 fonts/FreeSerif.ttf: 2.12 + mkdir -p fonts 2.13 wget http://ftp.gnu.org/gnu/freefont/freefont-ttf-20100919.tar.gz 2.14 tar xzvf freefont-ttf-20100919.tar.gz 2.15 - mv freefont-20100919 fonts 2.16 + rm -f freefont-ttf-20100919.tar.gz 2.17 + cp freefont-20100919/FreeSerif.ttf $@ 2.18 + 2.19 +fonts/klingon.ttf: 2.20 + mkdir -p fonts 2.21 + wget http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-00-53-45-33/tlh_2D00_pIqaD_2D00_US.zip 2.22 + unzip -o tlh_2D00_pIqaD_2D00_US.zip 2.23 + rm -f tlh_2D00_pIqaD_2D00_US.zip 2.24 + cp tlh-pIqaD-US/pIqaD.ttf $@
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/examples/unicode/Makefile Thu Sep 15 23:32:39 2011 +0300 3.3 @@ -0,0 +1,34 @@ 3.4 +obj = unicode.o 3.5 +bin = unicode 3.6 + 3.7 +CC = gcc 3.8 +CFLAGS = -pedantic -Wall -g -I../../src 3.9 +LDFLAGS = -L. -ldrawtext $(libgl) 3.10 + 3.11 +lib_so = libdrawtext.so 3.12 +fonts = serif.ttf klingon.ttf 3.13 + 3.14 +ifeq ($(shell uname -s), Darwin) 3.15 + libgl = -framework OpenGL -framework GLUT 3.16 +else 3.17 + libgl = -lGL -lGLU -lglut 3.18 +endif 3.19 + 3.20 +$(bin): $(obj) $(lib_so) $(fonts) 3.21 + $(CC) -o $@ $(obj) $(LDFLAGS) 3.22 + 3.23 +$(lib_so): ../../libdrawtext.so.0.0 3.24 + rm -f $@ 3.25 + ln -s $< $@ 3.26 + 3.27 +serif.ttf: ../fonts/FreeSerif.ttf 3.28 + rm -f $@ 3.29 + ln -s $< $@ 3.30 + 3.31 +klingon.ttf: ../fonts/klingon.ttf 3.32 + rm -f $@ 3.33 + ln -s $< $@ 3.34 + 3.35 +.PHONY: clean 3.36 +clean: 3.37 + rm -f $(obj) $(bin) $(lib_so)
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/examples/unicode/unicode.c Thu Sep 15 23:32:39 2011 +0300 4.3 @@ -0,0 +1,104 @@ 4.4 +/* Unicode libdrawtext example. 4.5 + * 4.6 + * Important parts are marked with XXX comments. 4.7 + */ 4.8 +#include <stdio.h> 4.9 +#include <stdlib.h> 4.10 + 4.11 +#ifndef __APPLE__ 4.12 +#include <GL/glut.h> 4.13 +#else 4.14 +#include <GLUT/glut.h> 4.15 +#endif 4.16 + 4.17 +#include "drawtext.h" 4.18 + 4.19 +void disp(void); 4.20 +void reshape(int x, int y); 4.21 +void keyb(unsigned char key, int x, int y); 4.22 + 4.23 +/* XXX fonts are represented by the opaque struct dtx_font type, so you 4.24 + * need to create at least one with dtx_open_font (see main). 4.25 + */ 4.26 +struct dtx_font *freeserif, *klingon; 4.27 + 4.28 +#define FONT_SIZE 24 4.29 + 4.30 +int main(int argc, char **argv) 4.31 +{ 4.32 + glutInit(&argc, argv); 4.33 + glutInitWindowSize(512, 384); 4.34 + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 4.35 + glutCreateWindow("libdrawtext example: simple"); 4.36 + 4.37 + glutDisplayFunc(disp); 4.38 + glutReshapeFunc(reshape); 4.39 + glutKeyboardFunc(keyb); 4.40 + 4.41 + /* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */ 4.42 + if(!(freeserif = dtx_open_font("serif.ttf", 0))) { 4.43 + return 1; 4.44 + } 4.45 + dtx_prepare_range(freeserif, FONT_SIZE, 0, 256); /* prepare ASCII */ 4.46 + dtx_prepare_range(freeserif, FONT_SIZE, 0x0370, 0x0400); /* greek */ 4.47 + dtx_prepare_range(freeserif, FONT_SIZE, 0x4e00, 0x9fc0); /* kanji */ 4.48 + 4.49 + if(!(klingon = dtx_open_font("klingon.ttf", 0))) { 4.50 + return 1; 4.51 + } 4.52 + dtx_prepare_range(klingon, FONT_SIZE, 0xf8d0, 0xf900); 4.53 + 4.54 + glutMainLoop(); 4.55 + return 0; 4.56 +} 4.57 + 4.58 +const char *ascii_text = "Hello world!"; 4.59 +const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7"; 4.60 +const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac"; 4.61 +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"; 4.62 + 4.63 + 4.64 +void disp(void) 4.65 +{ 4.66 + glClear(GL_COLOR_BUFFER_BIT); 4.67 + 4.68 + glMatrixMode(GL_MODELVIEW); 4.69 + glLoadIdentity(); 4.70 + 4.71 + dtx_use_font(freeserif, FONT_SIZE); 4.72 + 4.73 + glTranslatef(-200, 150, 0); 4.74 + /* XXX call dtx_string to draw utf-8 text. 4.75 + * any transformations and the current color apply 4.76 + */ 4.77 + dtx_string(ascii_text); 4.78 + 4.79 + glTranslatef(0, -40, 0); 4.80 + dtx_string(greek_text); 4.81 + 4.82 + glTranslatef(0, -40, 0); 4.83 + dtx_string(kanji_text); 4.84 + 4.85 + dtx_use_font(klingon, FONT_SIZE); 4.86 + 4.87 + glTranslatef(0, -40, 0); 4.88 + dtx_string(klingon_text); 4.89 + 4.90 + glutSwapBuffers(); 4.91 +} 4.92 + 4.93 +void reshape(int x, int y) 4.94 +{ 4.95 + glViewport(0, 0, x, y); 4.96 + 4.97 + glMatrixMode(GL_PROJECTION); 4.98 + glLoadIdentity(); 4.99 + glOrtho(-x/2, x/2, -y/2, y/2, -1, 1); 4.100 +} 4.101 + 4.102 +void keyb(unsigned char key, int x, int y) 4.103 +{ 4.104 + if(key == 27) { 4.105 + exit(0); 4.106 + } 4.107 +}
5.1 --- a/src/drawgl.c Thu Sep 15 13:56:54 2011 +0300 5.2 +++ b/src/drawgl.c Thu Sep 15 23:32:39 2011 +0300 5.3 @@ -32,7 +32,7 @@ 5.4 static int vattr = -1; 5.5 static int tattr = -1; 5.6 static unsigned int font_tex; 5.7 -static int buf_mode = DTX_LBF; 5.8 +static int buf_mode = DTX_NBF; 5.9 5.10 static struct dtx_font *font; 5.11 static int font_sz;
6.1 --- a/src/font.c Thu Sep 15 13:56:54 2011 +0300 6.2 +++ b/src/font.c Thu Sep 15 23:32:39 2011 +0300 6.3 @@ -155,7 +155,7 @@ 6.4 6.5 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72); 6.6 6.7 - if(!(gmap = malloc(sizeof *gmap))) { 6.8 + if(!(gmap = calloc(1, sizeof *gmap))) { 6.9 return 0; 6.10 } 6.11
7.1 --- a/src/utf8.c Thu Sep 15 13:56:54 2011 +0300 7.2 +++ b/src/utf8.c Thu Sep 15 23:32:39 2011 +0300 7.3 @@ -9,7 +9,7 @@ 7.4 0xf, /* three-bytes, 4 bits valid */ 7.5 0x7 /* four-bytes, 3 bits valid */ 7.6 }; 7.7 -static const char first_shift[] = { 7, 5, 4, 3 }; /* see above */ 7.8 +static const char first_shift[] = { 0, 7, 5, 4, 3 }; /* see above */ 7.9 7.10 #define CONT_PREFIX 0x80 7.11 #define CONT_MASK 0x3f 7.12 @@ -30,7 +30,7 @@ 7.13 int dtx_utf8_char_code(const char *str) 7.14 { 7.15 int i, nbytes, shift, code = 0; 7.16 - char mask; 7.17 + int mask; 7.18 7.19 if(!U8_IS_FIRST(*str)) { 7.20 return -1; 7.21 @@ -47,9 +47,10 @@ 7.22 7.23 code = (code << shift) | (*str++ & mask); 7.24 mask = 0x3f; 7.25 - shift = i == 0 ? first_shift[nbytes] : 6; 7.26 + shift = 6; 7.27 } 7.28 7.29 + printf("code: %x\n", code); 7.30 return code; 7.31 } 7.32