*** empty log message ***
[m17n/libotf.git] / example / otftobdf.c
1 #include <stdio.h>
2
3 #include <ft2build.h>
4 #include FT_FREETYPE_H
5
6 #define DEFAULT_PIXEL_SIZE 16
7
8 FT_Face face;
9
10 /* Format MSG by FMT and print the result to the stderr, and exit.  */
11
12 #define FATAL_ERROR(fmt, arg)   \
13   do {                          \
14     fprintf (stderr, fmt, arg); \
15     exit (1);                   \
16   } while (0)
17
18 void
19 dump_header (FT_Face face, char *foundry, int nchars, int pixel_size)
20 {
21   int width = ((face->bbox.xMax - face->bbox.xMin)
22                * pixel_size / face->units_per_EM);
23   int height = ((face->bbox.yMax - face->bbox.yMin)
24                 * pixel_size / face->units_per_EM);
25   int x = face->bbox.xMin * pixel_size / face->units_per_EM;
26   int y = face->bbox.yMin * pixel_size / face->units_per_EM;
27
28   printf ("STARTFONT 2.1\n");
29   printf ("FONT -%s-%s-%s-R-Normal--%d-%d-72-72-C-%d-ISO10646-1\n",
30           foundry, face->family_name, face->style_name,
31           pixel_size, pixel_size * 10, pixel_size * 10);
32   printf ("SIZE %d 72 72\n", pixel_size);
33   printf ("FONTBOUNDINGBOX %d %d %d %d\n", width, height, x, y);
34   printf ("STARTPROPERTIES 2\n");
35   printf ("FONT_ASCENT %d\n", y + height);
36   printf ("FONT_DESCENT %d\n", -y);
37   printf ("ENDPROPERTIES 0\n");
38   printf ("CHARS %d\n", nchars);
39 }
40
41 void
42 dump_tailer ()
43 {
44   printf ("ENDFONT\n");
45 }
46
47 void
48 dump_image (int pixel_size, int index, int code, int full)
49 {
50   int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
51   int i,j;
52   unsigned char *buf;
53   FT_GlyphSlot glyph;
54   int dwidth, x, y;
55   
56   if (err)
57     return;
58   glyph = face->glyph;
59   if (glyph->bitmap.rows * glyph->bitmap.width == 0)
60     return;
61   printf ("STARTCHAR U+%04X\n", code);
62   printf ("ENCODING %d\n", code);
63   printf ("SWIDTH %d 0\n",
64           (int) (glyph->metrics.horiAdvance >> 6) * 1000 / pixel_size);
65   if (full)
66     {
67       dwidth = glyph->bitmap.width;
68       x = 0;
69     }
70   else
71     {
72       dwidth = glyph->metrics.horiAdvance >> 6;
73       x = glyph->metrics.horiBearingX >> 6;
74     }
75   y = (glyph->metrics.horiBearingY - glyph->metrics.height) >> 6;
76   printf ("DWIDTH %d 0\n", dwidth);
77   printf ("BBX %d %d %d %d\n", glyph->bitmap.width, glyph->bitmap.rows, x, y);
78   printf ("BITMAP\n");
79   buf = (unsigned char *) glyph->bitmap.buffer;
80   for (i = 0; i < glyph->bitmap.rows; i++)
81     {
82       for (j = 0; j < (glyph->bitmap.width + 7) / 8; j++)
83         printf ("%02X", buf[i * glyph->bitmap.pitch + j]);
84       printf ("\n");
85     }
86   printf ("ENDCHAR\n");
87 }
88
89 int
90 main (int argc, char **argv)
91 {
92   FT_Library library;
93   int err;
94   int i;
95   int pixel_size = DEFAULT_PIXEL_SIZE;
96   FT_UInt unicode_table[0x10000];
97   int nchars;
98
99   if (argc != 2)
100     FATAL_ERROR ("%s\n", "Usage: otfimage [ X-OPTION ... ]  OTF-FILE");
101   
102   if ((err = FT_Init_FreeType (&library)))
103     FATAL_ERROR ("%s\n", "FT_Init_FreeType: error");
104   err = FT_New_Face (library, argv[1], 0, &face);
105   if (err == FT_Err_Unknown_File_Format)
106     FATAL_ERROR ("%s\n", "FT_New_Face: unknown file format");
107   else if (err)
108     FATAL_ERROR ("%s\n", "FT_New_Face: unknown error");
109   {
110     char *str = getenv ("PIXEL_SIZE");
111
112     if (str && (i = atoi (str)) > 0)
113       pixel_size = i;
114   }
115
116   if ((err = FT_Set_Pixel_Sizes (face, 0, pixel_size)))
117     FATAL_ERROR ("%s\n", "FT_Set_Pixel_Sizes: error");
118
119   for (i = nchars = 0; i < 0x10000; i++)
120     if (! FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
121         && face->glyph->bitmap.rows * face->glyph->bitmap.width)
122       nchars++;
123   for (i = 0; i < 0x10000; i++)
124     if ((unicode_table[i] = FT_Get_Char_Index (face, (FT_ULong) i)))
125       {
126         if (! FT_Load_Glyph (face, unicode_table[i],
127                              FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
128             && face->glyph->bitmap.rows * face->glyph->bitmap.width)
129           nchars++;
130         else
131           unicode_table[i] = 0;
132       }
133
134   dump_header (face, "SuperSoft", nchars, pixel_size);
135   for (i = 0; i < 0x10000; i++)
136     if (unicode_table[i])
137       dump_image (pixel_size, unicode_table[i], i, 1);
138   for (i = 0; i < 0x10000; i++)
139     dump_image (pixel_size, i, 0xE000 + i, 0);
140   dump_tailer ();
141
142   exit (0);
143 }