*** empty log message ***
[m17n/libotf.git] / example / otfview.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include <otf.h>
8
9 #include <ft2build.h>
10 #include FT_FREETYPE_H
11
12 #include <X11/Xlib.h>
13 #include <X11/keysym.h>
14 #include <X11/Xutil.h>
15
16 #define PIXEL_SIZE 40
17
18 //#define FONT_NAME "-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1"
19 #define FONT_NAME "6x13"
20 #define FONT_HEIGHT 14
21
22 int font_height, font_width;
23
24 FT_Library library;
25 FT_Face face;
26
27 Display *display;
28 int screen;
29 Window win;
30 XFontStruct *font;
31 GC gc_norm, gc_rev, gc_xor;
32 unsigned long valuemask;
33 unsigned long foreground, background;
34 XGCValues values;
35
36 typedef struct
37 {
38   int advance;
39   int left, top;
40   int rows;
41   int width;
42   int pitch;
43   int unicode;
44   unsigned char* buf;
45 } Bitmap;
46
47 Bitmap bitmap[0x10000];
48
49 /* Unicode to glyph index mapping table.  */
50 int utog[0x10000];
51
52 void
53 draw_bitmap (int index, int x, int y)
54 {
55   Bitmap *bmp = bitmap + index;
56   unsigned char *buf = bmp->buf;
57   int i, j;
58
59   if (buf)
60     {
61       x += bmp->left;
62       y -= bmp->top;
63       for (i = 0; i < bmp->rows; i++, buf += bmp->pitch)
64         for (j = 0; j < bmp->width; j++)
65           if (buf[j / 8] & (1 << (7 - (j % 8))))
66             XDrawPoint (display, win, gc_norm, x + j, y + i);
67     }
68 }
69
70 void
71 quit (char *msg)
72 {
73   fprintf (stderr, "Error by %s\n", msg);
74   exit (1);
75 }
76
77
78 int
79 read_unicode_seq (char *filename, int *code)
80 {
81   FILE *fp = fopen (filename, "r");
82   int i = 0;
83       
84   if (! fp)
85     {
86       fprintf (stderr, "File \"%s\" can't be opened.\n", filename);
87       exit (1);
88     }
89   while (i < 256
90          && fscanf (fp, "%x", code + i) == 1)
91     i++;
92   fclose (fp);
93   return i;
94 }
95
96
97 int
98 main (int argc, char **argv)
99 {
100   OTF *otf;
101   int err;
102   int i, j, max_glyph_idx;
103   int first_idx;
104   int update_mask;
105 #define UPDATE_RENDERING 1
106 #define UPDATE_BITMAP 2
107   OTF_GlyphString gstring;
108   OTF_Glyph *g;
109   int unicode_seq[256];
110   int n_codes = 0;
111   /* Window structure.
112
113   +-------------------------+
114   | +--- rendering area --+ |
115   | |Unicode: ...         | |
116   | |   cmap: ...         | |
117   | |   GSUB: ...         | |
118   | |   GPOS: ...         | |
119   | +---------------------+ |
120   | +--- bitmap area -----+ |
121   | |                     | |
122   | |                     | |
123   | |                     | |
124   | +---------------------+ |
125   +-------------------------+
126   */
127   int margin = 2;
128   int win_width, win_height;
129   int cols = 16, rows = 8;
130   int max_glyph_width, max_glyph_height;
131   int inner_width, rendering_area_height, bitmap_area_height;
132   int x, y, x0, y0, x1, y1;
133   char buf[1024];
134
135   gstring.size = gstring.used = 256;
136   g = calloc (256, sizeof (OTF_Glyph));
137   gstring.glyphs = g;
138
139   if (argc != 2 && argc != 3)
140     {
141       fprintf (stderr, "Usage, otfview OTF-FILE [CODE-FILE]\n");
142       exit (1);
143     }
144   
145   otf = OTF_open (argv[1]);
146   if (! otf
147       || OTF_get_table (otf, "head") < 0
148       || OTF_get_table (otf, "cmap") < 0)
149     {
150       OTF_perror ("otfview");
151       exit (1);
152     }
153
154   err = FT_Init_FreeType (&library);
155   if (err)
156     quit ("FT_Init_FreeType");
157   err = FT_New_Face (library, argv[1], 0, &face);
158   if (err == FT_Err_Unknown_File_Format)
159     quit ("FT_New_Face: unknown file format");
160   else if (err)
161     quit ("FT_New_Face: unknown error");
162   err = FT_Set_Pixel_Sizes (face, 0, PIXEL_SIZE);
163   if (err)
164     quit ("FT_Set_Char_Size");
165
166   memset (utog, 0, sizeof (utog));
167   x0 = x1 = y0 = y1 = 0;
168   for (i = 0; i < 0x10000; i++)
169     {
170       err = FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
171       if (err)
172         bitmap[i].buf = NULL;
173       else
174         {
175           Bitmap *bmp = bitmap + i;
176           int bmpsize;
177
178           max_glyph_idx = i;
179           bmp->advance = face->glyph->metrics.horiAdvance >> 6;
180           bmp->left = face->glyph->bitmap_left;
181           bmp->top = face->glyph->bitmap_top;
182           bmp->rows = face->glyph->bitmap.rows;
183           bmp->width = face->glyph->bitmap.width;
184           bmp->pitch = face->glyph->bitmap.pitch;
185           bmpsize = bmp->rows * bmp->pitch;
186           bmp->buf = malloc (bmpsize);
187           memcpy (bmp->buf, face->glyph->bitmap.buffer, bmpsize);
188           if (x0 > bmp->left)
189             x0 = bmp->left;
190           if (y0 > - bmp->top)
191             y0 = - bmp->top;
192           if (x1 < bmp->left + bmp->width)
193             x1 = bmp->left + bmp->width;
194           if (y1 < bmp->rows - bmp->top)
195             y1 = bmp->rows - bmp->top;
196         }
197     }
198
199   max_glyph_height = y1 - y0;
200   max_glyph_width = x1 - x0;
201
202   for (i = 0; i < 0x10000; i++)
203     {
204       gstring.glyphs[i & 0xFF].c = i;
205       if ((i & 0xFF) == 0xFF)
206         {
207           OTF_drive_cmap (otf, &gstring);
208           for (j = 0; j < 0x100; j++)
209             {
210               utog[(i & 0xFF00) + j] = gstring.glyphs[j].glyph_id;
211               if (gstring.glyphs[j].glyph_id > 0)
212                 bitmap[gstring.glyphs[j].glyph_id].unicode = (i & 0xFF00) + j;
213             }
214         }
215     }
216
217   if (argc == 3)
218     n_codes = read_unicode_seq (argv[2], unicode_seq);
219
220   display = XOpenDisplay (NULL);
221   screen = DefaultScreen (display);
222   font = XLoadQueryFont (display, FONT_NAME);
223   if (! font)
224     font = XLoadQueryFont (display, "fixed");
225   font_height = font->ascent + font->descent;
226   font_width = font->max_bounds.width; 
227
228   inner_width = (max_glyph_width + 1) * cols + font_width * 4 + 2;
229   rendering_area_height= (max_glyph_height + 1) * 3 + font_height;
230   bitmap_area_height = (max_glyph_height + 1) * rows + font_height + 2;
231   win_width = inner_width + margin * 2;
232   win_height = rendering_area_height + bitmap_area_height + margin * 3;
233
234   win = XCreateSimpleWindow (display, RootWindow (display, screen),
235                              0, 0, win_width, win_height, 1,
236                              BlackPixel (display, screen),
237                              WhitePixel (display, screen));
238
239   valuemask = GCForeground | GCBackground | GCFunction | GCFont;
240
241   values.foreground = BlackPixel (display, screen);
242   values.background = WhitePixel (display, screen);
243   values.function = GXcopy;
244   values.font = font->fid;
245   gc_norm = XCreateGC (display, win, valuemask, &values);
246
247   values.foreground = WhitePixel (display, screen);
248   values.background = BlackPixel (display, screen);
249   values.function = GXcopy;
250   gc_rev = XCreateGC (display, win, valuemask, &values);
251
252   values.foreground = BlackPixel (display, screen);
253   values.background = WhitePixel (display, screen);
254   values.function = values.foreground ? GXxor : GXequiv;
255   gc_xor = XCreateGC (display, win, valuemask, &values);
256
257   XMapWindow (display, win);
258   XSelectInput (display, win, ExposureMask | KeyPressMask | ButtonPressMask);
259
260   first_idx = 0;
261   update_mask = 0;
262   while (1)
263     {
264       XEvent event;
265
266       XNextEvent (display, &event);
267
268       switch (event.type)
269         {
270         case ButtonPress:
271           {
272             int x = event.xbutton.x;
273             int y = event.xbutton.y;
274
275             if (margin <= x && x < margin + inner_width
276                 && margin + 1 <= y && y < margin + 1 + font_height)
277               {
278                 n_codes = read_unicode_seq (argv[2], unicode_seq);
279                 update_mask = UPDATE_RENDERING;
280                 goto redraw;
281               }
282           }
283           break;
284
285         case KeyPress:
286           {
287             char buf[512];
288             KeySym keysym;
289             int n;
290
291             n = XLookupString ((XKeyEvent *) &event, buf, 512, &keysym, NULL);
292             if (! n)
293               break;
294             if (buf[0] == 'q')
295               goto finish;
296             if (buf[0] == 'n' || buf[0] == ' ')
297               {
298                 if (first_idx + cols * rows <= max_glyph_idx)
299                   {
300                     first_idx += cols * rows;
301                     update_mask |= UPDATE_BITMAP;
302                     goto redraw;
303                   }
304               }
305             else if (buf[0] == 'p'
306                      || keysym == XK_BackSpace || keysym == XK_Delete)
307               {
308                 if (first_idx > 0)
309                   {
310                     first_idx -= cols * rows;
311                     update_mask |= UPDATE_BITMAP;
312                     goto redraw;
313                   }
314               }
315           }
316           break;
317
318         default:
319           update_mask = UPDATE_RENDERING | UPDATE_BITMAP;
320           goto redraw;
321         }
322       continue;
323
324     redraw:
325       if (update_mask == (UPDATE_RENDERING | UPDATE_BITMAP))
326         {
327           XClearWindow (display, win);
328           x = margin;
329           y = margin + font->ascent;
330           XDrawImageString (display, win, gc_norm, x, y, "Unicode: ", 9);
331           y += font_height + (max_glyph_height - font_height) / 2;
332           XDrawImageString (display, win, gc_norm, x, y, "   cmap: ", 9);
333           y += max_glyph_height + 1;
334           XDrawImageString (display, win, gc_norm, x, y, "   GSUB: ", 9);
335           y += max_glyph_height + 1;
336           XDrawImageString (display, win, gc_norm, x, y, "   GPOS: ", 9);
337
338           y = margin * 2 + rendering_area_height;
339           XDrawLine (display, win, gc_norm, x, y, x + inner_width - 1, y);
340           y += font_height + 1;
341           for (i = 0; i <= rows; i++, y += max_glyph_height + 1)
342             XDrawLine (display, win, gc_norm, x, y, x + inner_width - 1, y);
343           y = margin * 2 + rendering_area_height;
344           XDrawLine (display, win, gc_norm, x, y,
345                      x, y + bitmap_area_height - 1);
346           x += font_width * 4 + 1;
347           for (i = 0; i <= cols; i++, x += max_glyph_width + 1)
348             XDrawLine (display, win, gc_norm, x, y,
349                        x, y + bitmap_area_height - 1);
350           y += font->ascent + 1;
351           x = (margin + font_width * 4 + 2
352                + (max_glyph_width - font_width * 4) / 2);
353           for (i = 0; i < cols; i++, x += max_glyph_width + 1)
354             {
355               sprintf (buf, "xxx%X", i);
356               XDrawImageString (display, win, gc_norm, x, y, buf, 4);
357             }
358         }
359
360       if (update_mask & UPDATE_RENDERING)      
361         {
362           x = margin + font_width * 9;
363           y = margin + font->ascent;
364           for (i = 0; i < n_codes; i++)
365             {
366               sprintf (buf + i * 5, "%04X ", unicode_seq[i]);
367               gstring.glyphs[i].c = unicode_seq[i];
368             }
369           gstring.used = n_codes;
370           XDrawImageString (display, win, gc_norm, x, y, buf, n_codes * 5);
371
372           OTF_drive_cmap (otf, &gstring);
373           y = margin + font_height + 1;
374           for (i = 0; i < n_codes; i++, x += max_glyph_width)
375             draw_bitmap (gstring.glyphs[i].glyph_id, x - x0, y - y0);
376
377           OTF_drive_gsub (otf, &gstring, "deva", NULL, NULL);
378           x = margin + font_width * 9;
379           y += max_glyph_height;
380           for (i = 0; i < gstring.used; i++, x += max_glyph_width)
381             draw_bitmap (gstring.glyphs[i].glyph_id, x - x0, y - y0);
382
383           OTF_drive_gpos (otf, &gstring, "deva", NULL, NULL);
384           x = margin + font_width * 9 - x0;
385           y += max_glyph_height - y0;
386           for (i = 0; i < gstring.used; i++)
387             {
388               int xoff = 0, yoff = 0;
389               OTF_Glyph *g = gstring.glyphs + i;
390
391               switch (g->positioning_type)
392                 {
393                 case 1: case 2:
394                   if (g->f.f1.format & OTF_XPlacement)
395                     xoff = PIXEL_SIZE * ((double) (g->f.f1.value->XPlacement)
396                                          * 100 / otf->head->unitsPerEm);
397                   if (g->f.f1.format & OTF_YPlacement)
398                     yoff = PIXEL_SIZE * ((double) (g->f.f1.value->YPlacement)
399                                          * 100 / otf->head->unitsPerEm);
400                   break;
401
402                 case 4:
403                   xoff = (PIXEL_SIZE
404                           * ((double) (g->f.f4.base_anchor->XCoordinate
405                                        - g->f.f4.mark_anchor->XCoordinate)
406                              * 100 / otf->head->unitsPerEm));
407                   yoff = (PIXEL_SIZE
408                           * ((double) (g->f.f4.base_anchor->YCoordinate
409                                        - g->f.f4.mark_anchor->YCoordinate)
410                              * 100 / otf->head->unitsPerEm));
411                   break;
412                 }
413
414               draw_bitmap (gstring.glyphs[i].glyph_id, x + xoff, y + yoff);
415               x += bitmap[gstring.glyphs[i].glyph_id].advance;
416             }
417         }
418
419       if (update_mask & UPDATE_BITMAP)
420         {
421           x = margin + 1;
422           y = (margin * 2 + rendering_area_height + font_height + 2
423                + (max_glyph_height - font_height) / 2 + font->ascent);
424           for (i = 0; i < rows; i++, y += max_glyph_height + 1)
425             {
426               sprintf (buf, "%03Xx", (first_idx + i * cols) / 16);
427               XDrawImageString (display, win, gc_norm, x, y, buf, 4);
428             }
429           x += font_width * 4 + 1;
430           y = margin * 2 + rendering_area_height + font_height + 2;
431           for (i = 0; i < rows; i++)
432             for (j = 0; j < cols; j++)
433               {
434                 XClearArea (display, win, x + (max_glyph_width + 1) * j,
435                             y + (max_glyph_height + 1) * i,
436                             max_glyph_width, max_glyph_height, False);
437                 draw_bitmap (first_idx + i * cols + j,
438                              x + (max_glyph_width + 1) * j - x0,
439                              y + (max_glyph_height + 1) * i - y0);
440               }
441         }
442       update_mask = 0;
443     }
444
445  finish:
446   OTF_close (otf);
447   exit (0);
448 }