1 /* otftobdf.c -- Generate BDF font from OpenType font.
3 Copyright (C) 2003, 2004
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H15PRO167
7 This file is part of libotf.
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.
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.
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. */
27 #include FT_FREETYPE_H
29 #define DEFAULT_PIXEL_SIZE 16
33 /* Format MSG by FMT and print the result to the stderr, and exit. */
35 #define FATAL_ERROR(fmt, arg) \
37 fprintf (stderr, fmt, arg); \
44 dump_header (FT_Face face, char *foundry, int nchars, int pixel_size)
46 int width = ((face->bbox.xMax - face->bbox.xMin)
47 * pixel_size / face->units_per_EM);
48 int height = ((face->bbox.yMax - face->bbox.yMin)
49 * pixel_size / face->units_per_EM);
50 int x = face->bbox.xMin * pixel_size / face->units_per_EM;
51 int y = face->bbox.yMin * pixel_size / face->units_per_EM;
53 printf ("STARTFONT 2.1\n");
54 printf ("FONT -%s-%s-%s-R-Normal--%d-%d-72-72-C-%d-%s\n",
55 foundry, face->family_name, face->style_name,
56 pixel_size, pixel_size * 10, pixel_size * 10, registry);
57 printf ("SIZE %d 72 72\n", pixel_size);
58 printf ("FONTBOUNDINGBOX %d %d %d %d\n", width, height, x, y);
59 printf ("STARTPROPERTIES 2\n");
60 printf ("FONT_ASCENT %d\n", y + height);
61 printf ("FONT_DESCENT %d\n", -y);
62 printf ("ENDPROPERTIES 0\n");
63 printf ("CHARS %d\n", nchars);
73 dump_image (int pixel_size, int index, int code, int full)
75 int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
84 if (glyph->bitmap.rows * glyph->bitmap.width == 0)
86 printf ("STARTCHAR U+%04X\n", code);
87 printf ("ENCODING %d\n", code);
88 printf ("SWIDTH %d 0\n",
89 (int) (glyph->metrics.horiAdvance >> 6) * 1000 / pixel_size);
92 dwidth = glyph->bitmap.width;
97 dwidth = glyph->metrics.horiAdvance >> 6;
98 x = glyph->metrics.horiBearingX >> 6;
100 y = (glyph->metrics.horiBearingY - glyph->metrics.height) >> 6;
101 printf ("DWIDTH %d 0\n", dwidth);
102 printf ("BBX %d %d %d %d\n", glyph->bitmap.width, glyph->bitmap.rows, x, y);
104 buf = (unsigned char *) glyph->bitmap.buffer;
105 for (i = 0; i < glyph->bitmap.rows; i++)
107 for (j = 0; j < (glyph->bitmap.width + 7) / 8; j++)
108 printf ("%02X", buf[i * glyph->bitmap.pitch + j]);
111 printf ("ENDCHAR\n");
116 main (int argc, char **argv)
121 int pixel_size = DEFAULT_PIXEL_SIZE;
122 FT_UInt code_table[0x10000];
125 int platform_id, encoding_id;
128 FATAL_ERROR ("Usage: %s ENCODING OTF-FILE\n", argv[0]);
129 if (sscanf (argv[1], "%d-%d", &platform_id, &encoding_id) != 2)
137 if ((err = FT_Init_FreeType (&library)))
138 FATAL_ERROR ("%s\n", "FT_Init_FreeType: error");
139 err = FT_New_Face (library, filename, 0, &face);
140 if (err == FT_Err_Unknown_File_Format)
141 FATAL_ERROR ("%s\n", "FT_New_Face: unknown file format");
143 FATAL_ERROR ("%s\n", "FT_New_Face: unknown error");
144 if (platform_id >= 0)
146 for (i = 0; i < face->num_charmaps; i++)
147 if (face->charmaps[i]->platform_id == platform_id
148 && face->charmaps[i]->encoding_id == encoding_id)
150 if (i == face->num_charmaps)
151 FATAL_ERROR ("Unknown ENCODING: %s\n", argv[1]);
152 FT_Set_Charmap (face, face->charmaps[i]);
153 if (platform_id == 0)
155 if (encoding_id == 3)
156 registry = "iso10646-1";
157 else if (face->charmaps[i]->encoding_id == 4)
158 registry = "iso10646-full";
160 else if (face->charmaps[i]->platform_id == 3)
162 if (face->charmaps[i]->encoding_id == 1)
163 registry = "iso10646-1";
164 else if (face->charmaps[i]->encoding_id == 10)
165 registry = "iso10646-full";
169 registry = alloca (256);
170 sprintf (registry, "%d-%d", platform_id, encoding_id);
175 registry = "raw-glyph";
179 char *str = getenv ("PIXEL_SIZE");
181 if (str && (i = atoi (str)) > 0)
185 if ((err = FT_Set_Pixel_Sizes (face, 0, pixel_size)))
186 FATAL_ERROR ("%s\n", "FT_Set_Pixel_Sizes: error");
189 for (i = nchars = 0; i < 0x10000; i++)
190 if (! FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
191 && face->glyph->bitmap.rows * face->glyph->bitmap.width)
194 for (i = 0; i < 0x10000; i++)
196 if (platform_id >= 0)
198 code_table[i] = FT_Get_Char_Index (face, (FT_ULong) i);
209 if (! FT_Load_Glyph (face, code_table[i],
210 FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
211 && face->glyph->bitmap.rows * face->glyph->bitmap.width)
217 dump_header (face, "unknown", nchars, pixel_size);
218 for (i = 0; i < 0x10000; i++)
219 if (code_table[i] >= 0)
220 dump_image (pixel_size, code_table[i], i, 1);