*** empty log message ***
[m17n/libotf.git] / example / otfview.c
index 5706e4c..626bce8 100644 (file)
@@ -15,7 +15,6 @@
 
 #define PIXEL_SIZE 40
 
-//#define FONT_NAME "-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1"
 #define FONT_NAME "6x13"
 #define FONT_HEIGHT 14
 
@@ -52,9 +51,14 @@ int utog[0x10000];
 void
 draw_bitmap (int index, int x, int y)
 {
-  Bitmap *bmp = bitmap + index;
-  unsigned char *buf = bmp->buf;
+  Bitmap *bmp;
+  unsigned char *buf;
   int i, j;
+  unsigned code = FT_Get_Char_Index (face, (FT_ULong) index);
+  //unsigned code = index;
+
+  bmp = bitmap + code;
+  buf = bmp->buf;
 
   if (buf)
     {
@@ -97,9 +101,9 @@ read_unicode_seq (char *filename, int *code)
 int
 main (int argc, char **argv)
 {
-  OTF *otf;
+  OTF *otf = NULL;
   int err;
-  int i, j, max_glyph_idx;
+  int i, j;
   int first_idx;
   int update_mask;
 #define UPDATE_RENDERING 1
@@ -108,6 +112,9 @@ main (int argc, char **argv)
   OTF_Glyph *g;
   int unicode_seq[256];
   int n_codes = 0;
+  int pixel_size = PIXEL_SIZE;
+  int raw_mode = 0;
+
   /* Window structure.
 
   +-------------------------+
@@ -142,15 +149,33 @@ main (int argc, char **argv)
       exit (1);
     }
   
-  otf = OTF_open (argv[1]);
-  if (! otf
-      || OTF_get_table (otf, "head") < 0
-      || OTF_get_table (otf, "cmap") < 0)
+  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_perror ("otfview");
+         otf = NULL;
+       }
     }
 
+  {
+    char *p = getenv ("PIXEL_SIZE");
+    int n;
+
+    if (p && sscanf (p, "%d", &n) == 1)
+      pixel_size = n;
+
+    p = getenv ("RAW_MODE");
+    if (p)
+      raw_mode = 1;
+  }
+
   err = FT_Init_FreeType (&library);
   if (err)
     quit ("FT_Init_FreeType");
@@ -159,10 +184,10 @@ main (int argc, char **argv)
     quit ("FT_New_Face: unknown file format");
   else if (err)
     quit ("FT_New_Face: unknown error");
-  err = FT_Set_Pixel_Sizes (face, 0, PIXEL_SIZE);
+  err = FT_Set_Pixel_Sizes (face, 0, pixel_size);
   if (err)
     quit ("FT_Set_Char_Size");
-
+  err = FT_Select_Charmap (face, FT_ENCODING_APPLE_ROMAN);
   memset (utog, 0, sizeof (utog));
   x0 = x1 = y0 = y1 = 0;
   for (i = 0; i < 0x10000; i++)
@@ -175,7 +200,6 @@ main (int argc, char **argv)
          Bitmap *bmp = bitmap + i;
          int bmpsize;
 
-         max_glyph_idx = i;
          bmp->advance = face->glyph->metrics.horiAdvance >> 6;
          bmp->left = face->glyph->bitmap_left;
          bmp->top = face->glyph->bitmap_top;
@@ -201,6 +225,8 @@ main (int argc, char **argv)
 
   for (i = 0; i < 0x10000; i++)
     {
+      if (i >= 0xD800 && i < 0xE000)
+       continue;
       gstring.glyphs[i & 0xFF].c = i;
       if ((i & 0xFF) == 0xFF)
        {
@@ -208,7 +234,8 @@ main (int argc, char **argv)
          for (j = 0; j < 0x100; j++)
            {
              utog[(i & 0xFF00) + j] = gstring.glyphs[j].glyph_id;
-             if (gstring.glyphs[j].glyph_id > 0)
+             if (gstring.glyphs[j].glyph_id > 0
+                 && gstring.glyphs[j].glyph_id < 0x10000)
                bitmap[gstring.glyphs[j].glyph_id].unicode = (i & 0xFF00) + j;
            }
        }
@@ -295,7 +322,7 @@ main (int argc, char **argv)
              goto finish;
            if (buf[0] == 'n' || buf[0] == ' ')
              {
-               if (first_idx + cols * rows <= max_glyph_idx)
+               if (first_idx + cols * rows < 0x10000)
                  {
                    first_idx += cols * rows;
                    update_mask |= UPDATE_BITMAP;
@@ -357,7 +384,7 @@ main (int argc, char **argv)
            }
        }
 
-      if (update_mask & UPDATE_RENDERING)      
+      if (otf && update_mask & UPDATE_RENDERING)      
        {
          x = margin + font_width * 9;
          y = margin + font->ascent;
@@ -392,19 +419,19 @@ main (int argc, char **argv)
                {
                case 1: case 2:
                  if (g->f.f1.format & OTF_XPlacement)
-                   xoff = PIXEL_SIZE * ((double) (g->f.f1.value->XPlacement)
+                   xoff = pixel_size * ((double) (g->f.f1.value->XPlacement)
                                         * 100 / otf->head->unitsPerEm);
                  if (g->f.f1.format & OTF_YPlacement)
-                   yoff = PIXEL_SIZE * ((double) (g->f.f1.value->YPlacement)
+                   yoff = pixel_size * ((double) (g->f.f1.value->YPlacement)
                                         * 100 / otf->head->unitsPerEm);
                  break;
 
                case 4:
-                 xoff = (PIXEL_SIZE
+                 xoff = (pixel_size
                          * ((double) (g->f.f4.base_anchor->XCoordinate
                                       - g->f.f4.mark_anchor->XCoordinate)
                             * 100 / otf->head->unitsPerEm));
-                 yoff = (PIXEL_SIZE
+                 yoff = (pixel_size
                          * ((double) (g->f.f4.base_anchor->YCoordinate
                                       - g->f.f4.mark_anchor->YCoordinate)
                             * 100 / otf->head->unitsPerEm));
@@ -431,10 +458,15 @@ main (int argc, char **argv)
          for (i = 0; i < rows; i++)
            for (j = 0; j < cols; j++)
              {
+               unsigned index = first_idx + i * cols + j;
+
                XClearArea (display, win, x + (max_glyph_width + 1) * j,
                            y + (max_glyph_height + 1) * i,
                            max_glyph_width, max_glyph_height, False);
-               draw_bitmap (first_idx + i * cols + j,
+               if (! raw_mode)
+                 index = FT_Get_Char_Index (face, (FT_ULong) index);
+
+               draw_bitmap (index,
                             x + (max_glyph_width + 1) * j - x0,
                             y + (max_glyph_height + 1) * i - y0);
              }