*** empty log message ***
[m17n/libotf.git] / example / otftobdf.c
1 /* otftobdf.c -- Generate BDF font from OpenType font.
2
3 Copyright (C) 2003, 2004
4   National Institute of Advanced Industrial Science and Technology (AIST)
5   Registration Number H15PRO167
6
7 This file is part of libotf.
8
9 Libotf is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 Libotf is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library, in a file named COPYING; if not,
21 write to the Free Software Foundation, Inc., 59 Temple Place, Suite
22 330, Boston, MA 02111-1307, USA.  */
23
24 #include <stdio.h>
25
26 #include <ft2build.h>
27 #include FT_FREETYPE_H
28
29 #define DEFAULT_PIXEL_SIZE 16
30
31 FT_Face face;
32
33 /* Format MSG by FMT and print the result to the stderr, and exit.  */
34
35 #define FATAL_ERROR(fmt, arg)   \
36   do {                          \
37     fprintf (stderr, fmt, arg); \
38     exit (1);                   \
39   } while (0)
40
41 void
42 dump_header (FT_Face face, char *foundry, int nchars, int pixel_size)
43 {
44   int width = ((face->bbox.xMax - face->bbox.xMin)
45                * pixel_size / face->units_per_EM);
46   int height = ((face->bbox.yMax - face->bbox.yMin)
47                 * pixel_size / face->units_per_EM);
48   int x = face->bbox.xMin * pixel_size / face->units_per_EM;
49   int y = face->bbox.yMin * pixel_size / face->units_per_EM;
50
51   printf ("STARTFONT 2.1\n");
52   printf ("FONT -%s-%s-%s-R-Normal--%d-%d-72-72-C-%d-ISO10646-1\n",
53           foundry, face->family_name, face->style_name,
54           pixel_size, pixel_size * 10, pixel_size * 10);
55   printf ("SIZE %d 72 72\n", pixel_size);
56   printf ("FONTBOUNDINGBOX %d %d %d %d\n", width, height, x, y);
57   printf ("STARTPROPERTIES 2\n");
58   printf ("FONT_ASCENT %d\n", y + height);
59   printf ("FONT_DESCENT %d\n", -y);
60   printf ("ENDPROPERTIES 0\n");
61   printf ("CHARS %d\n", nchars);
62 }
63
64 void
65 dump_tailer ()
66 {
67   printf ("ENDFONT\n");
68 }
69
70 void
71 dump_image (int pixel_size, int index, int code, int full)
72 {
73   int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
74   int i,j;
75   unsigned char *buf;
76   FT_GlyphSlot glyph;
77   int dwidth, x, y;
78   
79   if (err)
80     return;
81   glyph = face->glyph;
82   if (glyph->bitmap.rows * glyph->bitmap.width == 0)
83     return;
84   printf ("STARTCHAR U+%04X\n", code);
85   printf ("ENCODING %d\n", code);
86   printf ("SWIDTH %d 0\n",
87           (int) (glyph->metrics.horiAdvance >> 6) * 1000 / pixel_size);
88   if (full)
89     {
90       dwidth = glyph->bitmap.width;
91       x = 0;
92     }
93   else
94     {
95       dwidth = glyph->metrics.horiAdvance >> 6;
96       x = glyph->metrics.horiBearingX >> 6;
97     }
98   y = (glyph->metrics.horiBearingY - glyph->metrics.height) >> 6;
99   printf ("DWIDTH %d 0\n", dwidth);
100   printf ("BBX %d %d %d %d\n", glyph->bitmap.width, glyph->bitmap.rows, x, y);
101   printf ("BITMAP\n");
102   buf = (unsigned char *) glyph->bitmap.buffer;
103   for (i = 0; i < glyph->bitmap.rows; i++)
104     {
105       for (j = 0; j < (glyph->bitmap.width + 7) / 8; j++)
106         printf ("%02X", buf[i * glyph->bitmap.pitch + j]);
107       printf ("\n");
108     }
109   printf ("ENDCHAR\n");
110 }
111
112 int
113 main (int argc, char **argv)
114 {
115   FT_Library library;
116   int err;
117   int i;
118   int pixel_size = DEFAULT_PIXEL_SIZE;
119   FT_UInt unicode_table[0x10000];
120   int nchars;
121
122   if (argc != 2)
123     FATAL_ERROR ("%s\n", "Usage: otfimage [ X-OPTION ... ]  OTF-FILE");
124   
125   if ((err = FT_Init_FreeType (&library)))
126     FATAL_ERROR ("%s\n", "FT_Init_FreeType: error");
127   err = FT_New_Face (library, argv[1], 0, &face);
128   if (err == FT_Err_Unknown_File_Format)
129     FATAL_ERROR ("%s\n", "FT_New_Face: unknown file format");
130   else if (err)
131     FATAL_ERROR ("%s\n", "FT_New_Face: unknown error");
132   {
133     char *str = getenv ("PIXEL_SIZE");
134
135     if (str && (i = atoi (str)) > 0)
136       pixel_size = i;
137   }
138
139   if ((err = FT_Set_Pixel_Sizes (face, 0, pixel_size)))
140     FATAL_ERROR ("%s\n", "FT_Set_Pixel_Sizes: error");
141
142   /*
143   for (i = nchars = 0; i < 0x10000; i++)
144     if (! FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
145         && face->glyph->bitmap.rows * face->glyph->bitmap.width)
146       nchars++;
147   */
148   for (i = 0; i < 0x10000; i++)
149     if ((unicode_table[i] = FT_Get_Char_Index (face, (FT_ULong) i)))
150       {
151         if (! FT_Load_Glyph (face, unicode_table[i],
152                              FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
153             && face->glyph->bitmap.rows * face->glyph->bitmap.width)
154           nchars++;
155         else
156           unicode_table[i] = 0;
157       }
158
159   dump_header (face, "SuperSoft", nchars, pixel_size);
160   for (i = 0; i < 0x10000; i++)
161     if (unicode_table[i])
162       dump_image (pixel_size, unicode_table[i], i, 1);
163   /*
164   for (i = 0; i < 0x10000; i++)
165     dump_image (pixel_size, i, 0xE000 + i, 0);
166   */
167   dump_tailer ();
168
169   exit (0);
170 }