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