New
[m17n/libotf.git] / example / otfdraw.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "otf.h"
5
6 #include <ft2build.h>
7 #include FT_FREETYPE_H
8
9 #include <X11/Xlib.h>
10
11 Display *display;
12 int screen;
13 Window win;
14 GC gc;
15
16 void
17 draw_bitmap (FT_Face face, int x, int y)
18 {
19   int i, j;
20   FT_Bitmap *bmp = &face->glyph->bitmap;
21   unsigned char *buf = (unsigned char *) bmp->buffer;
22
23   for (i = 0; i < bmp->rows; i++, buf += bmp->pitch)
24     for (j = 0; j < bmp->width; j++)
25       if (buf[j / 8] & (1 << (7 - (j % 8))))
26         XDrawPoint (display, win, gc, x + j, y + i);
27 }
28
29 void
30 quit (char *msg)
31 {
32   fprintf (stderr, "Error by %s\n", msg);
33   exit (1);
34 }
35
36
37 static void
38 otf_dump_value_record (unsigned value_format, OTF_ValueRecord *value_record)
39 {
40   if (value_format & OTF_XPlacement)
41     printf (" (XPlacement %d)", value_record->XPlacement);
42   if (value_format & OTF_YPlacement)
43     printf (" (YPlacement %d)", value_record->YPlacement);
44 }
45       
46
47
48 void
49 otf_dump_gstring (OTF_GlyphString *gstring)
50 {
51   int i;
52
53   for (i = 0; i < gstring->used; i++)
54     {
55       OTF_Glyph *g = gstring->glyphs + i;
56
57       printf ("%02d: c:%04X, g:%04X class:%d\n",
58               i, g->c, g->glyph_id, g->GlyphClass);
59       // otf_dump_value_record (g->value_format1, &g->value_record1);
60       // otf_dump_value_record (g->value_format2, &g->value_record2);
61     }
62 }
63
64
65 int
66 main (int argc, char **argv)
67 {
68   OTF *otf;
69   int i;
70   OTF_GlyphString gstring;
71
72   if (argc != 2)
73     {
74       fprintf (stderr, "Usage, dtfdump OTF-FILE");
75       exit (1);
76     }
77   
78   otf = otf_open (argv[1]);
79
80   gstring.size = 10;
81   gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * 10);
82   gstring.used = 0;
83 #if 0
84   gstring.glyphs[gstring.used++].c = 0x93F;
85   gstring.glyphs[gstring.used++].c = 0x939;
86   gstring.glyphs[gstring.used++].c = 0x928;
87   gstring.glyphs[gstring.used++].c = 0x94D;
88   gstring.glyphs[gstring.used++].c = 0x926;
89   gstring.glyphs[gstring.used++].c = 0x940;
90 #else
91   gstring.glyphs[gstring.used++].c = 0x92A;
92   gstring.glyphs[gstring.used++].c = 0x94D;
93   gstring.glyphs[gstring.used++].c = 0x930;
94   gstring.glyphs[gstring.used++].c = 0x924;
95   gstring.glyphs[gstring.used++].c = 0x94D;
96   gstring.glyphs[gstring.used++].c = 0x92F;
97   gstring.glyphs[gstring.used++].c = 0x947;
98   gstring.glyphs[gstring.used++].c = 0x915;
99 #endif
100   otf_cmap (otf, &gstring);
101   otf_gdef (otf, &gstring);
102   otf_dump_gstring (&gstring);
103   if (otf_gsub (otf, otf_tag ("deva"), 0, &gstring) < 0)
104     printf ("otf_gsub error\n");
105   else
106     printf ("RESULT of GSUB\n");
107   otf_dump_gstring (&gstring);
108
109   if (otf_gpos (otf, otf_tag ("deva"), 0, &gstring) < 0)
110     printf ("otf_gsub error\n");
111   else
112     printf ("RESULT of GPOS\n");
113   otf_dump_gstring (&gstring);
114
115   {
116     int x, y;
117     int err;
118     FT_Library library;
119     FT_Face face;
120
121     err = FT_Init_FreeType (&library);
122     if (err)
123       quit ("FT_Init_FreeType");
124     err = FT_New_Face (library, argv[1], 0, &face);
125     if (err == FT_Err_Unknown_File_Format)
126       quit ("FT_New_Face: unknown file format");
127     else if (err)
128       quit ("FT_New_Face: another error");
129     err = FT_Set_Char_Size (face, 0, 32*64, 400, 400);
130     if (err)
131       quit ("FT_Set_Char_Size");
132
133     display = XOpenDisplay (NULL);
134     screen = DefaultScreen (display);
135     win = XCreateSimpleWindow (display, RootWindow (display, screen),
136                                0, 0, 800, 600, 1, BlackPixel (display, screen),
137                                WhitePixel (display, screen));
138     gc = DefaultGC (display, screen);
139     XMapWindow (display, win);
140     XSelectInput (display, win, ExposureMask);
141
142     while (1)
143       {
144         XEvent event;
145
146         XNextEvent (display, &event);
147
148         XClearWindow (display, win);
149         x = 20, y = 300;
150         for (i = 0; i < gstring.used; i++)
151           {
152             FT_Load_Glyph (face, gstring.glyphs[i].glyph_id,
153                            FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
154
155             draw_bitmap (face,
156                          x + face->glyph->bitmap_left,
157                          y - face->glyph->bitmap_top);
158             x += face->glyph->advance.x >> 6;
159           }
160       }
161   }
162
163
164   otf_close (otf);
165   exit (0);
166 }