*** 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     otf_perror ("otfdraw:");
82   gstring.size = 10;
83   gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * 30);
84   memset (gstring.glyphs, 0, sizeof (OTF_Glyph) * 30);
85   gstring.used = 0;
86 #if 0
87   gstring.glyphs[gstring.used++].c = 0x93F;
88   gstring.glyphs[gstring.used++].c = 0x939;
89   gstring.glyphs[gstring.used++].c = 0x928;
90   gstring.glyphs[gstring.used++].c = 0x94D;
91   gstring.glyphs[gstring.used++].c = 0x926;
92   gstring.glyphs[gstring.used++].c = 0x940;
93 #else
94   gstring.glyphs[gstring.used++].c = 0x92A;
95   gstring.glyphs[gstring.used++].c = 0x94D;
96   gstring.glyphs[gstring.used++].c = 0x930;
97   gstring.glyphs[gstring.used++].c = 0x924;
98   gstring.glyphs[gstring.used++].c = 0x94D;
99   gstring.glyphs[gstring.used++].c = 0x92F;
100   gstring.glyphs[gstring.used++].c = 0x947;
101   gstring.glyphs[gstring.used++].c = 0x915;
102 #endif
103   otf_drive_cmap (otf, &gstring);
104   otf_drive_gdef (otf, &gstring);
105   otf_dump_gstring (&gstring);
106   if (otf_drive_gsub (otf, otf_tag ("deva"), 0, &gstring) < 0)
107     printf ("otf_gsub error\n");
108   else
109     printf ("RESULT of GSUB\n");
110   otf_dump_gstring (&gstring);
111
112   if (otf_drive_gpos (otf, otf_tag ("deva"), 0, &gstring) < 0)
113     printf ("otf_gsub error\n");
114   else
115     printf ("RESULT of GPOS\n");
116   otf_dump_gstring (&gstring);
117
118   {
119     int x, y;
120     int err;
121     FT_Library library;
122     FT_Face face;
123
124     err = FT_Init_FreeType (&library);
125     if (err)
126       quit ("FT_Init_FreeType");
127     err = FT_New_Face (library, argv[1], 0, &face);
128     if (err == FT_Err_Unknown_File_Format)
129       quit ("FT_New_Face: unknown file format");
130     else if (err)
131       quit ("FT_New_Face: unknown error");
132     err = FT_Set_Char_Size (face, 0, 32*64, 400, 400);
133     if (err)
134       quit ("FT_Set_Char_Size");
135
136     display = XOpenDisplay (NULL);
137     screen = DefaultScreen (display);
138     win = XCreateSimpleWindow (display, RootWindow (display, screen),
139                                0, 0, 800, 600, 1, BlackPixel (display, screen),
140                                WhitePixel (display, screen));
141     gc = DefaultGC (display, screen);
142     XMapWindow (display, win);
143     XSelectInput (display, win, ExposureMask);
144
145     while (1)
146       {
147         XEvent event;
148
149         XNextEvent (display, &event);
150
151         XClearWindow (display, win);
152         x = 20, y = 300;
153         for (i = 0; i < gstring.used; i++)
154           {
155             FT_Load_Glyph (face, gstring.glyphs[i].glyph_id,
156                            FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
157
158             if (gstring.glyphs[i].positioning_type)
159               {
160                 printf ("%d:%d\n", i, gstring.glyphs[i].positioning_type);
161               }
162             draw_bitmap (face,
163                          x + face->glyph->bitmap_left,
164                          y - face->glyph->bitmap_top);
165             x += face->glyph->advance.x >> 6;
166           }
167       }
168   }
169
170
171   otf_close (otf);
172   exit (0);
173 }