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); \
42 dump_header (FT_Face face, char *foundry, int nchars, int pixel_size)
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;
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);
71 dump_image (int pixel_size, int index, int code, int full)
73 int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
82 if (glyph->bitmap.rows * glyph->bitmap.width == 0)
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);
90 dwidth = glyph->bitmap.width;
95 dwidth = glyph->metrics.horiAdvance >> 6;
96 x = glyph->metrics.horiBearingX >> 6;
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);
102 buf = (unsigned char *) glyph->bitmap.buffer;
103 for (i = 0; i < glyph->bitmap.rows; i++)
105 for (j = 0; j < (glyph->bitmap.width + 7) / 8; j++)
106 printf ("%02X", buf[i * glyph->bitmap.pitch + j]);
109 printf ("ENDCHAR\n");
113 main (int argc, char **argv)
118 int pixel_size = DEFAULT_PIXEL_SIZE;
119 FT_UInt unicode_table[0x10000];
123 FATAL_ERROR ("%s\n", "Usage: otfimage [ X-OPTION ... ] OTF-FILE");
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");
131 FATAL_ERROR ("%s\n", "FT_New_Face: unknown error");
133 char *str = getenv ("PIXEL_SIZE");
135 if (str && (i = atoi (str)) > 0)
139 if ((err = FT_Set_Pixel_Sizes (face, 0, pixel_size)))
140 FATAL_ERROR ("%s\n", "FT_Set_Pixel_Sizes: error");
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)
148 for (i = 0; i < 0x10000; i++)
149 if ((unicode_table[i] = FT_Get_Char_Index (face, (FT_ULong) i)))
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)
156 unicode_table[i] = 0;
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);
164 for (i = 0; i < 0x10000; i++)
165 dump_image (pixel_size, i, 0xE000 + i, 0);