*** empty log message ***
[m17n/libotf.git] / example / otfview.c
index 0a4dabe..cf46900 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
-#include "otf.h"
+#include <sys/stat.h>
+#include <unistd.h>
+#include <libgen.h>
+
+#include <X11/Intrinsic.h>
+#include <X11/StringDefs.h>
+#include <X11/Shell.h>
+#include <X11/Xaw/Command.h>
+#include <X11/Xaw/Toggle.h>
+#include <X11/Xaw/Box.h>
+#include <X11/Xaw/Form.h>
+#include <X11/Xaw/Viewport.h>
 
 #include <ft2build.h>
 #include FT_FREETYPE_H
 
-FT_Library library;
+#include <otf.h>
+
+#define DEFAULT_PIXEL_SIZE 30
+#define DEFAULT_FONT_NAME "6x13"
+XFontStruct *font;
+#define FONT_HEIGHT (font->ascent + font->descent)
+
+XtAppContext context;
+/* Widget structure.
+   +--- frame (form) ------------------+
+   | +--- command_area (box) --------+ |
+   | | quit charmap ...              | |
+   | +-------------------------------+ |
+   | +---- navi_area (box) ----------+ |
+   | | PREV prev label next NEXT     | |
+   | +-------------------------------+ |
+   | +--- glyph_area (form) ---------+ |
+   | | glyph[0]     ...    glyph[15] | |
+   | |   ...                 ...     | |
+   | | glyph[112]   ...    glyph[127]| |
+   | +-------------------------------+ |
+   | +--- render_area (form) --------+ |
+   | | clear                         | |
+   | | +--- raw (box) -------------+ | |
+   | | | raw_label raw_image       | | |
+   | | +--- seq (box) -------------+ | |
+   | | | seq_label seq_image       | | |
+   | | +--- gsub (box) ------------+ | |
+   | | | gsub_label gsub_image     | | |
+   | | +--- gpos (box) ------------+ | |
+   | | | gpos_label gpos_image     | | |
+   | | +---------------------------+ | |
+   | +-------------------------------+ |
+   +-----------------------------------+ */
+Widget shell, frame;
+Widget command_area, quit, *charmap;
+Widget navi_area, PREV, prev, label, next, NEXT;
+Widget glyph_area, glyph[128];
+Widget render_area, clear, raw, seq, gsub, gpos;
+Widget raw_label, raw_image, seq_label, seq_image;
+Widget gsub_label, gsub_image, gpos_label, gpos_image;
+
+int glyph_char[128];
+
+Display *display;
+GC gc, gc_set, gc_or;
+
+typedef struct {
+  Pixmap pixmap;
+  unsigned width, height;
+  int x, y;
+  int advance;
+} BitmapRec;
+
+BitmapRec bitmap[0x10000];
+
+int render_width, render_height;
+Pixmap raw_pixmap, seq_pixmap, gsub_pixmap, gpos_pixmap;
+
 FT_Face face;
 
-#include <X11/Xlib.h>
+struct {
+  int platform_id;
+  int encoding_id;
+  char name[20];
+} charmap_rec[10];
 
-#define FONT_NAME "-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1"
-#define FONT_HEIGHT 14
+int charmap_index;
 
-Display *display;
-int screen;
-Window win;
-XFontStruct *font;
-GC gc_norm, gc_rev, gc_xor;
-unsigned long valuemask;
-unsigned long foreground, background;
-XGCValues values;
+unsigned glyph_width, glyph_height;
+int glyph_x, glyph_y;
+int glyph_index;
+
+struct {
+  int n_glyphs;
+  int glyphs[64];
+} glyph_rec;  
+
+OTF *otf;
+
+void
+create_pixmap (int pixel_size, int index)
+{
+  int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
+  XImage ximage;
+  Pixmap pixmap;
+  
+  if (err)
+    {
+      bitmap[index].pixmap = (Pixmap) 0;
+      return;
+    }
+  ximage.height = face->glyph->bitmap.rows;
+  ximage.width = face->glyph->bitmap.width;
+  ximage.depth = 1;
+  ximage.bits_per_pixel = 1;
+  ximage.xoffset = 0;
+  ximage.format = XYPixmap;
+  ximage.data = (char *) face->glyph->bitmap.buffer;
+  ximage.byte_order = MSBFirst;
+  ximage.bitmap_unit = 8;
+  ximage.bitmap_bit_order = MSBFirst;
+  ximage.bitmap_pad = 8;
+  ximage.bytes_per_line = face->glyph->bitmap.pitch;
+  XInitImage (&ximage);
+  pixmap = XCreatePixmap (display, DefaultRootWindow (display),
+                         glyph_width, glyph_height, 1);
+  XFillRectangle (display, pixmap, gc, 0, 0, glyph_width, glyph_height);
+  XPutImage (display, pixmap, gc, &ximage, 0, 0,
+            glyph_x + face->glyph->bitmap_left,
+            glyph_y - face->glyph->bitmap_top,
+            ximage.width, ximage.height);
+  bitmap[index].pixmap = pixmap;
+  bitmap[index].width = ximage.width;
+  bitmap[index].height = ximage.height;
+  bitmap[index].x = face->glyph->bitmap_left;
+  bitmap[index].y = - face->glyph->bitmap_top;
+  bitmap[index].advance = face->glyph->metrics.horiAdvance >> 6;
+}
 
-typedef struct
+void
+update_glyph_area ()
 {
-  int left, top;
-  int rows;
-  int width;
-  int pitch;
-  unsigned char* buf;
-} Bitmap;
+  int i;
+  Arg arg[2];
+  char buf[16];
+
+  for (i = 0; i < 128; i++)
+    {
+      int index = glyph_index + i;
+      int num_args = 0;
+
+      if (charmap_index >= 0)
+       index = FT_Get_Char_Index (face, (FT_ULong) index);
+      XtSetArg (arg[num_args], XtNbitmap, bitmap[index].pixmap), num_args++;
+      if (! bitmap[index].pixmap)
+       XtSetArg (arg[num_args], XtNlabel, "none"), num_args++;
+      XtSetValues (glyph[i], arg, num_args);
+    }
 
-Bitmap bitmap[0x10000];
+  sprintf (buf, " %04X-%04X ", glyph_index, glyph_index + 0x7F);
+  XtSetArg (arg[0], XtNlabel, buf);
+  XtSetValues (label, arg, 1);
+}
 
 void
-draw_bitmap (int index, int x, int xoff, int width,
-            int y, int yoff, int height, int rev)
+update_render_area ()
 {
-  Bitmap *bmp = bitmap + index;
-  unsigned char *buf = bmp->buf;
-  int i, j;
-  char str[256];
-  int w;
-
-  if (rev)
-    XFillRectangle (display, win, gc_norm, x, y, width, height);
-  XFillRectangle (display, win, gc_xor, x, y + height - FONT_HEIGHT,
-                 width, FONT_HEIGHT);
-  XDrawLine (display, win, gc_xor, x, y, x, y + height - 1);
-  sprintf (str, "%04X", index);
-  w = XTextWidth (font, str, 4);
-  XDrawString (display, win, gc_xor, x + (width - w) / 2, y + height - 2,
-              str, 4);
-
-  if (buf)
+  int i;
+  int x;
+  Arg arg[1];
+
+  XFillRectangle (display, raw_pixmap, gc, 0, 0, render_width, render_height);
+  XFillRectangle (display, seq_pixmap, gc, 0, 0, render_width, render_height);
+  for (i = 0, x = glyph_x; i < glyph_rec.n_glyphs; i++)
     {
-      x += xoff + bmp->left;
-      y += yoff - bmp->top;
-      for (i = 0; i < bmp->rows; i++, buf += bmp->pitch)
-       for (j = 0; j < bmp->width; j++)
-         if (buf[j / 8] & (1 << (7 - (j % 8))))
-           XDrawPoint (display, win, gc_xor, x + j, y + i);
+      BitmapRec *bmp = bitmap + glyph_rec.glyphs[i];
+
+      XCopyArea (display, bmp->pixmap, raw_pixmap, gc,
+                0, 0, glyph_width, glyph_height,
+                (glyph_width + 1) * i + 1, 1);
+      XDrawRectangle (display, raw_pixmap, gc_set,
+                     (glyph_width + 1) * i, 0,
+                     glyph_width + 1, glyph_height + 1);
+      XCopyArea (display, bmp->pixmap, seq_pixmap, gc_or,
+                glyph_x + bmp->x, glyph_y + bmp->y, bmp->width, bmp->height,
+                x + bmp->x, glyph_y + bmp->y);
+      x += bmp->advance;
     }
+  XtSetArg (arg[0], XtNbitmap, raw_pixmap);
+  XtSetValues (raw_image, arg, 1);
+  XtSetArg (arg[0], XtNbitmap, seq_pixmap);
+  XtSetValues (seq_image, arg, 1);
+  if (! otf)
+    return;
+  XFillRectangle (display, gsub_pixmap, gc, 0, 0, render_width, render_height);
+  XFillRectangle (display, gpos_pixmap, gc, 0, 0, render_width, render_height);
+  XtSetArg (arg[0], XtNbitmap, gsub_pixmap);
+  XtSetValues (gsub_image, arg, 1);
+  XtSetArg (arg[0], XtNbitmap, gpos_pixmap);
+  XtSetValues (gpos_image, arg, 1);
 }
 
 void
-draw_big_bitmat (int index,
-                int x, int xoff, int width,
-                int y, int yoff, int height)
+QuitProc (Widget w, XtPointer client_data, XtPointer call_data)
 {
-  FT_Bitmap *bmp;
-  unsigned char *buf;
-  int i, j;
+  XtAppSetExitFlag (XtWidgetToApplicationContext (w));
+}
 
-  FT_Set_Pixel_Sizes (face, 0, 120);
-  if (FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME))
+void
+GlyphProc (Widget w, XtPointer client_data, XtPointer call_data)
+{
+  int old_glyph_index = glyph_index;
+
+  if ((int) client_data == -2 && glyph_index > 0)
+    glyph_index = (glyph_index - 1) & 0xF000;
+  else if ((int) client_data == -1 && glyph_index > 0)
+    glyph_index -= 0x80;
+  else if ((int) client_data == 1 && glyph_index < 0xFF80)
+    glyph_index += 0x80;
+  else if ((int) client_data == 2 && glyph_index < 0xF000)
+    glyph_index = (glyph_index + 0x1000) & 0xF000;
+  if (glyph_index != old_glyph_index)
+    update_glyph_area ();
+}
+
+void
+CharmapProc (Widget w, XtPointer client_data, XtPointer call_data)
+{
+  if (charmap_index == (int) client_data)
     return;
-  
-  bmp = &face->glyph->bitmap;
-  buf = bmp->buffer;
-  XFillRectangle (display, win, gc_rev, x, y, width, height);
-  XDrawRectangle (display, win, gc_norm, x - 1, y - 1, width + 1, height + 1);
-  x += xoff + face->glyph->bitmap_left;
-  y += yoff - face->glyph->bitmap_top;
-  for (i = 0; i < bmp->rows; i++, buf += bmp->pitch)
-    for (j = 0; j < bmp->width; j++)
-      if (buf[j / 8] & (1 << (7 - (j % 8))))
-       XDrawPoint (display, win, gc_norm, x + j, y + i);
+  charmap_index = (int) client_data;
+  if (charmap_index >= 0)
+    FT_Set_Charmap (face, face->charmaps[charmap_index]);
+  update_glyph_area ();
 }
 
 void
-quit (char *msg)
+RenderProc (Widget w, XtPointer client_data, XtPointer call_data)
 {
-  fprintf (stderr, "Error by %s\n", msg);
-  exit (1);
+  if ((int) client_data < 0)
+    {
+      glyph_rec.n_glyphs = 0;
+      update_render_area ();
+    }
+  else if (glyph_rec.n_glyphs < 64)
+    {
+      int index = glyph_index + (int) client_data;
+      if (charmap_index >= 0)
+       index = FT_Get_Char_Index (face, (FT_ULong) index);
+      glyph_rec.glyphs[glyph_rec.n_glyphs++] = index;
+      update_render_area ();
+    }
 }
 
+void
+create_widgets ()
+{
+  String quit_action = "<KeyPress>q: set() notify() unset()";
+  String PREV_action = "Shift<KeyPress>p: set() notify() unset()";
+  String prev_action = "~Shift<KeyPress>p: set() notify() unset()";
+  String next_action = "~Shift<KeyPress>n: set() notify() unset()";
+  String NEXT_action = "Shift<KeyPress>n: set() notify() unset()";
+  Arg arg[10];
+  int i, j;
+
+  frame = XtCreateManagedWidget ("frame", formWidgetClass, shell, NULL, 0);
+  XtSetArg (arg[0], XtNleft, XawChainLeft);
+  XtSetArg (arg[1], XtNright, XawChainLeft);
+  XtSetArg (arg[2], XtNtop, XawChainTop);
+  XtSetArg (arg[3], XtNbottom, XawChainTop);
+  XtSetArg (arg[4], XtNborderWidth, 0);
+  XtSetArg (arg[5], XtNorientation, XtorientHorizontal);
+  command_area = XtCreateManagedWidget ("command-area", boxWidgetClass,
+                                       frame, arg, 6);
+  XtSetArg (arg[6], XtNfromVert, command_area);
+  navi_area = XtCreateManagedWidget ("navi-area", boxWidgetClass,
+                                    frame, arg, 7);
+  XtSetArg (arg[4], XtNborderWidth, 1);
+  XtSetArg (arg[5], XtNfromVert, navi_area);
+  XtSetArg (arg[6], XtNdefaultDistance, 0);
+  glyph_area = XtCreateManagedWidget ("glyph-area", formWidgetClass,
+                                     frame, arg, 7);
+  XtSetArg (arg[4], XtNborderWidth, 0);
+  XtSetArg (arg[5], XtNfromVert, glyph_area);
+  render_area = XtCreateManagedWidget ("render-area", formWidgetClass,
+                                      frame, arg, 6);
+
+  XtSetArg (arg[0], XtNaccelerators, XtParseAcceleratorTable (quit_action));
+  quit = XtCreateManagedWidget ("quit", commandWidgetClass,
+                               command_area, arg, 1);
+  XtAddCallback (quit, XtNcallback, QuitProc, NULL);
+
+  charmap = alloca (sizeof (Widget) * (face->num_charmaps + 1));
+  XtSetArg (arg[0], XtNstate, True);
+  charmap[0] = XtCreateManagedWidget (charmap_rec[0].name, toggleWidgetClass,
+                                     command_area, arg, 1);
+  XtAddCallback (charmap[0], XtNcallback, CharmapProc, (XtPointer) -1);
+  XtSetArg (arg[0], XtNradioGroup, charmap[0]);
+  for (i = 0; i < face->num_charmaps; i++)
+    {
+      charmap[i + 1] = XtCreateManagedWidget (charmap_rec[i + 1].name,
+                                             toggleWidgetClass,
+                                             command_area, arg, 1);
+      XtAddCallback (charmap[i + 1], XtNcallback, CharmapProc, (XtPointer) i);
+    }
+
+  XtSetArg (arg[0], XtNlabel, "<< (P)");
+  XtSetArg (arg[1], XtNaccelerators, XtParseAcceleratorTable (PREV_action));
+  PREV = XtCreateManagedWidget ("PREV", commandWidgetClass,
+                               navi_area, arg, 2);
+  XtAddCallback (PREV, XtNcallback, GlyphProc, (XtPointer) -2);
+  XtSetArg (arg[0], XtNlabel, "< (p)");
+  XtSetArg (arg[1], XtNaccelerators, XtParseAcceleratorTable (prev_action));
+  prev = XtCreateManagedWidget ("prev", commandWidgetClass,
+                               navi_area, arg, 2);
+  XtAddCallback (prev, XtNcallback, GlyphProc, (XtPointer) -1);
+  XtSetArg (arg[0], XtNlabel, " 0000 ");
+  label = XtCreateManagedWidget ("label", labelWidgetClass,
+                                navi_area, arg, 1);
+  XtSetArg (arg[0], XtNlabel, "(n) >");
+  XtSetArg (arg[1], XtNaccelerators, XtParseAcceleratorTable (next_action));
+  next = XtCreateManagedWidget ("next", commandWidgetClass,
+                               navi_area, arg, 2);
+  XtAddCallback (next, XtNcallback, GlyphProc, (XtPointer) 1);
+  XtSetArg (arg[0], XtNlabel, "(N) >>");
+  XtSetArg (arg[1], XtNaccelerators, XtParseAcceleratorTable (NEXT_action));
+  NEXT = XtCreateManagedWidget ("NEXT", commandWidgetClass,
+                               navi_area, arg, 2);
+  XtAddCallback (NEXT, XtNcallback, GlyphProc, (XtPointer) 2);
+
+  XtSetArg (arg[0], XtNleft, XawChainLeft);
+  XtSetArg (arg[1], XtNright, XawChainLeft);
+  XtSetArg (arg[2], XtNtop, XawChainTop);
+  XtSetArg (arg[3], XtNbottom, XawChainTop);
+  for (i = 0; i < 8; i++)
+    for (j = 0; j < 16; j++)
+      {
+       int k = i * 16 + j;
+       int num_args = 4;
+
+       XtSetArg (arg[num_args], XtNwidth, glyph_width), num_args++;
+       XtSetArg (arg[num_args], XtNheight, glyph_height), num_args++;
+       if (j > 0)
+         XtSetArg (arg[num_args], XtNfromHoriz, glyph[k - 1]), num_args++;
+       if (i > 0)
+         XtSetArg (arg[num_args], XtNfromVert, glyph[k - 16]), num_args++;
+       glyph[k] = XtCreateManagedWidget ("glyph", commandWidgetClass,
+                                         glyph_area, arg, num_args);
+       XtAddCallback (glyph[k], XtNcallback, RenderProc, (XtPointer) k);
+      }
+
+  XtSetArg (arg[0], XtNleft, XawChainLeft);
+  XtSetArg (arg[1], XtNright, XawChainLeft);
+  XtSetArg (arg[2], XtNtop, XawChainTop);
+  XtSetArg (arg[3], XtNbottom, XawChainTop);
+  clear = XtCreateManagedWidget ("clear", commandWidgetClass,
+                                render_area, arg, 4);
+  XtAddCallback (clear, XtNcallback, RenderProc, (XtPointer) -1);
+  XtSetArg (arg[4], XtNorientation, XtorientHorizontal);
+  XtSetArg (arg[5], XtNborderWidth, 0);
+  XtSetArg (arg[6], XtNfromVert, clear);
+  raw = XtCreateManagedWidget ("raw", boxWidgetClass,
+                               render_area, arg, 7);
+  XtSetArg (arg[0], XtNborderWidth, 0);
+  XtSetArg (arg[1], XtNlabel, "raw: ");
+  raw_label = XtCreateManagedWidget ("raw-label", labelWidgetClass,
+                                     raw, arg, 2);
+  XtSetArg (arg[1], XtNbitmap, raw_pixmap);
+  raw_image = XtCreateManagedWidget ("raw-image", labelWidgetClass,
+                                     raw, arg, 2);
+  XtSetArg (arg[6], XtNfromVert, raw);
+  seq = XtCreateManagedWidget ("seq", boxWidgetClass,
+                               render_area, arg, 7);
+  XtSetArg (arg[0], XtNborderWidth, 0);
+  XtSetArg (arg[1], XtNlabel, "seq: ");
+  seq_label = XtCreateManagedWidget ("seq-label", labelWidgetClass,
+                                     seq, arg, 2);
+  XtSetArg (arg[1], XtNbitmap, seq_pixmap);
+  seq_image = XtCreateManagedWidget ("seq-image", labelWidgetClass,
+                                     seq, arg, 2);
+  if (otf)
+    {
+      XtSetArg (arg[6], XtNfromVert, seq);
+      gsub = XtCreateManagedWidget ("gsub", boxWidgetClass,
+                                   render_area, arg, 7);
+      XtSetArg (arg[0], XtNborderWidth, 0);
+      XtSetArg (arg[1], XtNlabel, "gsub: ");
+      gsub_label = XtCreateManagedWidget ("gsub-label", labelWidgetClass,
+                                         gsub, arg, 2);
+      gsub_image = XtCreateManagedWidget ("gsub-image", labelWidgetClass,
+                                         gsub, arg, 1);
+      XtSetArg (arg[6], XtNfromVert, gsub);
+      gpos = XtCreateManagedWidget ("gpos", boxWidgetClass,
+                                   render_area, arg, 7);
+      XtSetArg (arg[0], XtNborderWidth, 0);
+      XtSetArg (arg[1], XtNlabel, "gpos: ");
+      gpos_label = XtCreateManagedWidget ("gpos-label", labelWidgetClass,
+                                         gpos, arg, 2);
+      gpos_image = XtCreateManagedWidget ("gpos-image", labelWidgetClass,
+                                         gpos, arg, 1);
+    }
+
+  XtInstallAllAccelerators (shell, shell);
+}
+
+
+/* Format MSG by FMT and print the result to the stderr, and exit.  */
+
+#define FATAL_ERROR(fmt, arg)  \
+  do {                         \
+    fprintf (stderr, fmt, arg);        \
+    exit (1);                  \
+  } while (0)
 
 int
 main (int argc, char **argv)
 {
-  OTF *otf;
+  FT_Library library;
+
+  OTF_GlyphString gstring;
+  OTF_Glyph *g;
+
   int err;
-  int i, max_i, j;
-  int width, height, max_width, max_height;
-  int cols = 16, rows = 8;
-  int x0, y0, x1, y1;
-  int big_height;
-  int first_idx;
-  int left_idx = -1, right_idx = -1;
-  int update_mask;
-#define UPDATE_LEFT 1
-#define UPDATE_RIGHT 2
-#define UPDATE_MAIN 4
+  int i;
+  int pixel_size = DEFAULT_PIXEL_SIZE;
+
+  gstring.size = gstring.used = 256;
+  g = calloc (256, sizeof (OTF_Glyph));
+  gstring.glyphs = g;
+
+  shell = XtOpenApplication (&context, "OTFView", NULL, 0, &argc, argv, NULL,
+                            shellWidgetClass, NULL, 0);
+  display = XtDisplay (shell);
 
   if (argc != 2)
-    {
-      fprintf (stderr, "Usage, otfview OTF-FILE");
-      exit (1);
-    }
+    FATAL_ERROR ("%s\n", "Usage: otfview [ X-OPTION ... ]  OTF-FILE");
   
-  otf = OTF_open (argv[1]);
-  if (! otf)
+  if (strstr (argv[1], ".ttf")
+      || strstr (argv[1], ".TTF")
+      || strstr (argv[1], ".otf")
+      || strstr (argv[1], ".OTF"))
     {
-      OTF_perror ("otfview");
-      exit (1);
+      otf = OTF_open (argv[1]);
+      if (! otf
+         || OTF_get_table (otf, "head") < 0
+         || OTF_get_table (otf, "cmap") < 0
+         || (OTF_get_table (otf, "gsub") < 0
+             && OTF_get_table (otf, "gpos") < 0))
+       otf = NULL;
     }
 
-  err = FT_Init_FreeType (&library);
-  if (err)
-    quit ("FT_Init_FreeType");
+  if ((err = FT_Init_FreeType (&library)))
+    FATAL_ERROR ("%s\n", "FT_Init_FreeType: error");
   err = FT_New_Face (library, argv[1], 0, &face);
   if (err == FT_Err_Unknown_File_Format)
-    quit ("FT_New_Face: unknown file format");
+    FATAL_ERROR ("%s\n", "FT_New_Face: unknown file format");
   else if (err)
-    quit ("FT_New_Face: unknown error");
-  err = FT_Set_Pixel_Sizes (face, 0, 24);
-  if (err)
-    quit ("FT_Set_Char_Size");
+    FATAL_ERROR ("%s\n", "FT_New_Face: unknown error");
+  if ((err = FT_Set_Pixel_Sizes (face, 0, pixel_size)))
+    FATAL_ERROR ("%s\n", "FT_Set_Char_Size: error");
 
-  x0 = x1 = y0 = y1 = 0;
-  for (i = 0; i < 0x10000; i++)
-    {
-      err = FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
-      if (err)
-       bitmap[i].buf = NULL;
-      else
-       {
-         Bitmap *bmp = bitmap + i;
-         int bmpsize;
-
-         max_i = i;
-         bmp->left = face->glyph->bitmap_left;
-         bmp->top = face->glyph->bitmap_top;
-         bmp->rows = face->glyph->bitmap.rows;
-         bmp->width = face->glyph->bitmap.width;
-         bmp->pitch = face->glyph->bitmap.pitch;
-         bmpsize = bmp->rows * bmp->pitch;
-         bmp->buf = malloc (bmpsize);
-         memcpy (bmp->buf, face->glyph->bitmap.buffer, bmpsize);
-         if (x0 > bmp->left)
-           x0 = bmp->left;
-         if (y0 > - bmp->top)
-           y0 = - bmp->top;
-         if (x1 < bmp->left + bmp->width)
-           x1 = bmp->left + bmp->width;
-         if (y1 < bmp->rows - bmp->top)
-           y1 = bmp->rows - bmp->top;
-       }
-    }
+  {
+    char title[256];
+    Arg arg[1];
 
-  max_height = y1 - y0;
-  max_width = x1 - x0;
+    sprintf (title, "%s family:%s style:%s",
+            basename (argv[1]), face->family_name, face->style_name);
+    XtSetArg (arg[0], XtNtitle, title);
+    XtSetValues (shell, arg, 1);
+  }
 
-  display = XOpenDisplay (NULL);
-  screen = DefaultScreen (display);
 
-  big_height = max_height * 5 + 10;
-  
-  width = max_width * cols;
-  height = big_height + (max_height + FONT_HEIGHT) * rows;
-
-  win = XCreateSimpleWindow (display, RootWindow (display, screen),
-                            0, 0, width, height, 1,
-                            BlackPixel (display, screen),
-                            WhitePixel (display, screen));
-
-  font = XLoadQueryFont (display, FONT_NAME);
-  if (! font)
-    font = XLoadQueryFont (display, "fixed");
-  valuemask = GCForeground | GCBackground | GCFunction | GCFont;
-
-  values.foreground = BlackPixel (display, screen);
-  values.background = WhitePixel (display, screen);
-  values.function = GXcopy;
-  values.font = font->fid;
-  gc_norm = XCreateGC (display, win, valuemask, &values);
-
-  values.foreground = WhitePixel (display, screen);
-  values.background = BlackPixel (display, screen);
-  values.function = GXcopy;
-  gc_rev = XCreateGC (display, win, valuemask, &values);
-
-  values.foreground = BlackPixel (display, screen);
-  values.background = WhitePixel (display, screen);
-  values.function = values.foreground ? GXxor : GXequiv;
-  gc_xor = XCreateGC (display, win, valuemask, &values);
-
-  XMapWindow (display, win);
-  XSelectInput (display, win, ExposureMask | KeyPressMask | ButtonPressMask);
-
-  first_idx = 0;
-  update_mask = 0;
-  while (1)
+  glyph_width = ((face->bbox.xMax - face->bbox.xMin)
+                * pixel_size / face->units_per_EM);
+  glyph_height = ((face->bbox.yMax - face->bbox.yMin)
+                 *  pixel_size / face->units_per_EM);
+  glyph_x = - (face->bbox.xMin * pixel_size / face->units_per_EM);
+  glyph_y = face->bbox.yMax * pixel_size / face->units_per_EM;
+
+  charmap_rec[0].platform_id = -1;
+  charmap_rec[0].encoding_id = -1;
+  strcpy (charmap_rec[0].name, "no charmap");
+
+  for (i = 0; i < face->num_charmaps; i++)
     {
-      XEvent event;
-
-      XNextEvent (display, &event);
-
-      switch (event.type)
-       {
-       case ButtonPress:
-         {
-           int x = event.xbutton.x;
-           int y = event.xbutton.y;
-
-           if (y < big_height)
-             {
-               if (x < width / 2)
-                 {
-                   if (first_idx > 0)
-                     {
-                       first_idx -= cols * rows;
-                       update_mask |= UPDATE_MAIN;
-                       goto redraw;
-                     }
-                 }
-               else
-                 {
-                   if (first_idx + cols * rows <= max_i)
-                     {
-                       first_idx += cols * rows;
-                       update_mask |= UPDATE_MAIN;
-                       goto redraw;
-                     }
-                 }
-             }
-           else
-             {
-               if (event.xbutton.button == Button1)
-                 {
-                   left_idx = (first_idx
-                               + ((y - big_height)
-                                  / (max_height + FONT_HEIGHT)
-                                  * cols)
-                               + x / max_width);
-                   update_mask |= UPDATE_LEFT;
-                 }
-               else
-                 {
-                   right_idx = (first_idx
-                                + ((y - big_height)
-                                   / (max_height + FONT_HEIGHT)
-                                   * cols)
-                                + x / max_width);
-                   update_mask |= UPDATE_RIGHT;
-                 }
-               goto redraw;
-             }
-         }
-         break;
-
-       case KeyPress:
-         break;
-
-       default:
-         update_mask = UPDATE_LEFT | UPDATE_RIGHT | UPDATE_MAIN;
-         goto redraw;
-       }
-      continue;
-
-    redraw:
-      if (update_mask == (UPDATE_LEFT | UPDATE_RIGHT | UPDATE_MAIN))
-       {
-         XClearWindow (display, win);
-         XDrawLine (display, win, gc_norm, 0, big_height - 1,
-                    width, big_height - 1);
-       }
-
-      if (update_mask & UPDATE_MAIN)
-       {
-         XFillRectangle (display, win, gc_rev, 0, big_height, width, height);
-         for (i = 0; i < rows; i++)
-           for (j = 0; j < cols; j++)
-             draw_bitmap (first_idx + i * cols + j,
-                          max_width * j, -x0, max_width,
-                          big_height + (max_height + FONT_HEIGHT) * i,
-                          -y0, max_height + FONT_HEIGHT,  0);
-       }
-      if (update_mask & UPDATE_LEFT)      
-       draw_big_bitmat (left_idx, max_width * 2, -x0 * 5, max_width * 5,
-                        5, -y0 * 5, max_height * 5);
-      if (update_mask & UPDATE_RIGHT)      
-       draw_big_bitmat (right_idx, max_width * 9, -x0 * 5, max_width * 5,
-                        5, -y0 * 5, max_height * 5);
-      update_mask = 0;
+      charmap_rec[i + 1].platform_id = face->charmaps[i]->platform_id;
+      charmap_rec[i + 1].encoding_id = face->charmaps[i]->encoding_id;
+      sprintf (charmap_rec[i + 1].name, "%d-%d",
+              charmap_rec[i + 1].platform_id, charmap_rec[i + 1].encoding_id);
+      if (face->charmaps[i]->platform_id == 0
+         || (face->charmaps[i]->platform_id == 3
+             && face->charmaps[i]->encoding_id == 1))
+       strcat (charmap_rec[i + 1].name, " (unicode)");
+      else if (face->charmaps[i]->platform_id == 1
+              && face->charmaps[i]->encoding_id == 0)
+       strcat (charmap_rec[i + 1].name, " (apple-roman)");
     }
 
-  OTF_close (otf);
+  render_width = (glyph_width + 1) * 15 + 1;
+  render_height = glyph_height + 2;
+  raw_pixmap = XCreatePixmap (display, DefaultRootWindow (display),
+                             render_width, render_height, 1);
+  seq_pixmap = XCreatePixmap (display, DefaultRootWindow (display),
+                             render_width, render_height, 1);
+  gsub_pixmap = XCreatePixmap (display, DefaultRootWindow (display),
+                              render_width, render_height, 1);
+  gpos_pixmap = XCreatePixmap (display, DefaultRootWindow (display),
+                              render_width, render_height, 1);
+  {
+    unsigned long valuemask =  GCFunction | GCLineWidth;
+    XGCValues values;
+
+    gc = XCreateGC (display, raw_pixmap, (unsigned long) 0, NULL);
+    values.function = GXset;
+    values.line_width = 1;
+    gc_set = XCreateGC (display, raw_pixmap, valuemask, &values);
+    values.function = GXor;
+    gc_or = XCreateGC (display, raw_pixmap, valuemask, &values);
+  }
+
+  for (i = 0; i < 0x10000; i++)
+    create_pixmap (pixel_size, i);
+  create_widgets ();
+  glyph_index = 0;
+  charmap_index = -1;
+  update_glyph_area ();
+  update_render_area ();
+
+  XtRealizeWidget (shell);
+  XtAppMainLoop (context);
+
   exit (0);
 }