libdrawtext
view examples/unicode/unicode.c @ 58:11c8b34b0da5
implemented the metrics functions
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Sat, 17 Sep 2011 10:09:58 +0300 |
parents | fe0c54e574ae |
children | 10cfb642d0b8 |
line source
1 /* Unicode libdrawtext example.
2 *
3 * Important parts are marked with XXX comments.
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
8 #ifndef __APPLE__
9 #include <GL/glut.h>
10 #else
11 #include <GLUT/glut.h>
12 #endif
14 #include "drawtext.h"
16 void disp(void);
17 void reshape(int x, int y);
18 void keyb(unsigned char key, int x, int y);
20 /* XXX fonts are represented by the opaque struct dtx_font type, so you
21 * need to create at least one with dtx_open_font (see main).
22 */
23 struct dtx_font *fntmain, *fntcjk, *fntklingon;
25 #define FONT_SZ 24
27 int main(int argc, char **argv)
28 {
29 glutInit(&argc, argv);
30 glutInitWindowSize(512, 384);
31 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
32 glutCreateWindow("libdrawtext example: unicode");
34 glutDisplayFunc(disp);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keyb);
38 /* XXX dtx_open_font opens a font file and returns a pointer to dtx_font */
39 if(!(fntmain = dtx_open_font("serif.ttf", 0))) {
40 return 1;
41 }
42 dtx_prepare_range(fntmain, FONT_SZ, 0, 256); /* ASCII */
43 dtx_prepare_range(fntmain, FONT_SZ, 0x370, 0x400); /* greek */
44 dtx_prepare_range(fntmain, FONT_SZ, 0x400, 0x500); /* cyrilic */
46 if(!(fntcjk = dtx_open_font("cjk.ttf", 0))) {
47 return 1;
48 }
49 dtx_prepare_range(fntcjk, FONT_SZ, 0x4e00, 0x9fc0); /* kanji */
51 if(!(fntklingon = dtx_open_font("klingon.ttf", 0))) {
52 return 1;
53 }
54 dtx_prepare_range(fntklingon, FONT_SZ, 0xf8d0, 0xf900); /* klingon */
56 glutMainLoop();
57 return 0;
58 }
60 /* various UTF-8 strings */
61 const char *english_text = "Hello world!";
62 const char *greek_text = "\xce\x9a\xce\xbf\xcf\x8d\xcf\x81\xce\xb1\xcf\x83\xce\xb7";
63 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";
64 const char *kanji_text = "\xe4\xb9\x97\xe4\xba\xac";
65 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";
68 void disp(void)
69 {
70 glClear(GL_COLOR_BUFFER_BIT);
72 glMatrixMode(GL_MODELVIEW);
73 glLoadIdentity();
75 dtx_use_font(fntmain, FONT_SZ);
77 glTranslatef(-200, 150, 0);
78 /* XXX call dtx_string to draw utf-8 text.
79 * any transformations and the current color apply
80 */
81 dtx_string(english_text);
83 glTranslatef(0, -40, 0);
84 dtx_string(greek_text);
86 glTranslatef(0, -40, 0);
87 dtx_string(russian_text);
89 dtx_use_font(fntcjk, FONT_SZ);
91 glTranslatef(0, -40, 0);
92 dtx_string(kanji_text);
94 dtx_use_font(fntklingon, FONT_SZ);
96 glTranslatef(0, -40, 0);
97 dtx_string(klingon_text);
99 glutSwapBuffers();
100 }
102 void reshape(int x, int y)
103 {
104 glViewport(0, 0, x, y);
106 glMatrixMode(GL_PROJECTION);
107 glLoadIdentity();
108 glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
109 }
111 void keyb(unsigned char key, int x, int y)
112 {
113 if(key == 27) {
114 exit(0);
115 }
116 }