libdrawtext

view examples/unicode/unicode.c @ 100:49feeaf50ae8

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Dec 2015 02:55:35 +0200
parents 10cfb642d0b8
children
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";
67 void draw_box(struct dtx_box *box)
68 {
69 glBegin(GL_LINE_LOOP);
70 glColor3f(0, 1, 0);
71 glVertex2f(box->x, box->y);
72 glVertex2f(box->x + box->width, box->y);
73 glVertex2f(box->x + box->width, box->y + box->height);
74 glVertex2f(box->x, box->y + box->height);
75 glEnd();
76 glColor3f(1, 1, 1);
77 }
79 void disp(void)
80 {
81 glClear(GL_COLOR_BUFFER_BIT);
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
86 dtx_use_font(fntmain, FONT_SZ);
88 glTranslatef(-200, 150, 0);
89 /* XXX call dtx_string to draw utf-8 text.
90 * any transformations and the current color apply
91 */
92 dtx_string(english_text);
94 glTranslatef(0, -40, 0);
95 dtx_string(greek_text);
97 glTranslatef(0, -40, 0);
98 dtx_string(russian_text);
100 dtx_use_font(fntcjk, FONT_SZ);
102 glTranslatef(0, -40, 0);
103 dtx_string(kanji_text);
105 dtx_use_font(fntklingon, FONT_SZ);
107 glTranslatef(0, -40, 0);
108 dtx_string(klingon_text);
110 glutSwapBuffers();
111 }
113 void reshape(int x, int y)
114 {
115 glViewport(0, 0, x, y);
117 glMatrixMode(GL_PROJECTION);
118 glLoadIdentity();
119 glOrtho(-x/2, x/2, -y/2, y/2, -1, 1);
120 }
122 void keyb(unsigned char key, int x, int y)
123 {
124 if(key == 27) {
125 exit(0);
126 }
127 }