*** empty log message ***
[m17n/libotf.git] / example / otftobdf.c
diff --git a/example/otftobdf.c b/example/otftobdf.c
new file mode 100644 (file)
index 0000000..af9a7fc
--- /dev/null
@@ -0,0 +1,136 @@
+#include <stdio.h>
+
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
+#define DEFAULT_PIXEL_SIZE 16
+
+FT_Face face;
+
+/* 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)
+
+void
+dump_header (FT_Face face, char *foundry, int nchars, int pixel_size)
+{
+  int width = ((face->bbox.xMax - face->bbox.xMin)
+              * pixel_size / face->units_per_EM);
+  int height = ((face->bbox.yMax - face->bbox.yMin)
+               * pixel_size / face->units_per_EM);
+  int x = face->bbox.xMin * pixel_size / face->units_per_EM;
+  int y = face->bbox.yMin * pixel_size / face->units_per_EM;
+
+  printf ("STARTFONT 2.1\n");
+  printf ("FONT -%s-%s-%s-R-Normal--%d-%d-72-72-C-%d-ISO10646-1\n",
+         foundry, face->family_name, face->style_name,
+         pixel_size, pixel_size * 10, pixel_size * 10);
+  printf ("SIZE %d 72 72\n", pixel_size);
+  printf ("FONTBOUNDINGBOX %d %d %d %d\n", width, height, x, y);
+  printf ("STARTPROPERTIES 2\n");
+  printf ("FONT_ASCENT %d\n", y + height);
+  printf ("FONT_DESCENT %d\n", -y);
+  printf ("ENDPROPERTIES 0\n");
+  printf ("CHARS %d\n", nchars);
+}
+
+void
+dump_tailer ()
+{
+  printf ("ENDFONT\n");
+}
+
+void
+dump_image (int pixel_size, int index, int code, int full)
+{
+  int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
+  int i,j;
+  unsigned char *buf;
+  FT_GlyphSlot glyph;
+  int dwidth, x, y;
+  
+  if (err)
+    return;
+  glyph = face->glyph;
+  if (glyph->bitmap.rows * glyph->bitmap.width == 0)
+    return;
+  printf ("STARTCHAR U+%04X\n", code);
+  printf ("ENCODING %d\n", code);
+  printf ("SWIDTH %d 0\n",
+         (int) (glyph->metrics.horiAdvance >> 6) * 1000 / pixel_size);
+  if (full)
+    {
+      dwidth = glyph->bitmap.width;
+      x = 0;
+    }
+  else
+    {
+      dwidth = glyph->metrics.horiAdvance >> 6;
+      x = glyph->metrics.horiBearingX >> 6;
+    }
+  y = (glyph->metrics.horiBearingY - glyph->metrics.height) >> 6;
+  printf ("DWIDTH %d 0\n", dwidth);
+  printf ("BBX %d %d %d %d\n", glyph->bitmap.width, glyph->bitmap.rows, x, y);
+  printf ("BITMAP\n");
+  buf = (unsigned char *) glyph->bitmap.buffer;
+  for (i = 0; i < glyph->bitmap.rows; i++)
+    {
+      for (j = 0; j < (glyph->bitmap.width + 7) / 8; j++)
+       printf ("%02X", buf[i * glyph->bitmap.pitch + j]);
+      printf ("\n");
+    }
+  printf ("ENDCHAR\n");
+}
+
+int
+main (int argc, char **argv)
+{
+  FT_Library library;
+  int err;
+  int i;
+  int pixel_size = DEFAULT_PIXEL_SIZE;
+  FT_UInt unicode_table[0x80];
+  int nchars;
+
+  if (argc != 2)
+    FATAL_ERROR ("%s\n", "Usage: otfimage [ X-OPTION ... ]  OTF-FILE");
+  
+  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)
+    FATAL_ERROR ("%s\n", "FT_New_Face: unknown file format");
+  else if (err)
+    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_Pixel_Sizes: error");
+
+  {
+    char *str = getenv ("PIXEL_SIZE");
+
+    if (str && (i = atoi (str)) > 0)
+      pixel_size = i;
+  }
+
+  for (i = nchars = 0; i < 0x10000; i++)
+    if (! FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
+       && face->glyph->bitmap.rows * face->glyph->bitmap.width)
+      nchars++;
+  for (i = 0x0D00; i < 0x0D80; i++)
+    if ((unicode_table[i - 0x0D00] = FT_Get_Char_Index (face, (FT_ULong) i)))
+      nchars++;
+
+  dump_header (face, "SuperSoft", nchars, pixel_size);
+  for (i = 0; i < 0x80; i++)
+    if (unicode_table[i])
+      dump_image (pixel_size, unicode_table[i], 0x0D00 + i, 1);
+  for (i = 0; i < 0x10000; i++)
+    dump_image (pixel_size, i, 0xE000 + i, 0);
+  dump_tailer ();
+
+  exit (0);
+}