libdrawtext

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