2 * sfnttofnt. Bitmap only ttf to Window fnt file converter
4 * Copyright 2004 Huw Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #ifdef HAVE_FT2BUILD_H
34 #include FT_FREETYPE_H
35 #include FT_SFNT_NAMES_H
36 #include FT_TRUETYPE_TABLES_H
38 #include "wine/unicode.h"
39 #include "wine/wingdi16.h"
58 static void usage(char **argv)
60 fprintf(stderr, "%s foo.ttf ppem enc dpi def_char avg_width\n", argv[0]);
65 #define __attribute__(X)
68 static void error(const char *s, ...) __attribute__((format (printf, 1, 2)));
70 static void error(const char *s, ...)
74 fprintf(stderr, "Error: ");
75 vfprintf(stderr, s, ap);
80 static int lookup_charset(int enc)
82 /* FIXME: make winelib app and use TranslateCharsetInfo */
87 return RUSSIAN_CHARSET;
93 return TURKISH_CHARSET;
95 return HEBREW_CHARSET;
97 return ARABIC_CHARSET;
99 return BALTIC_CHARSET;
101 return VIETNAMESE_CHARSET;
121 return SHIFTJIS_CHARSET;
123 return GB2312_CHARSET;
125 return HANGUL_CHARSET;
127 return CHINESEBIG5_CHARSET;
129 fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
134 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char, int avg_width)
136 int ascent, il, ppem, descent, width_bytes = 0, space_size, max_width = 0;
139 BYTE left_byte, right_byte, byte;
141 CHAR_TABLE_ENTRY *dfCharTable;
142 int i, x, y, x_off, x_end, first_char;
145 const union cptable *cptable;
146 FT_SfntName sfntname;
148 cptable = wine_cp_get_table(enc);
150 error("Can't find codepage %d\n", enc);
152 if(cptable->info.char_size != 1) {
153 /* for double byte charsets we actually want to use cp1252 */
154 cptable = wine_cp_get_table(1252);
156 error("Can't find codepage 1252\n");
159 ppem = face->size->metrics.y_ppem;
160 start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
162 if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT))
163 error("Can't find Aring\n");
164 ascent = face->glyph->metrics.height >> 6;
165 descent = ppem - ascent;
166 if(FT_Load_Char(face, 'M', FT_LOAD_DEFAULT))
167 error("Can't find M\n");
168 il = ascent - (face->glyph->metrics.height >> 6);
170 /* Hack: Courier has no internal leading, nor do any Chinese fonts */
171 if(!strcmp(face->family_name, "Courier") || enc == 936 || enc == 950)
174 first_char = FT_Get_First_Char(face, &gi);
175 if(first_char == 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
176 first_char = 32; /* FT_Get_Next_Char for some reason returns too high
177 number in this case */
179 dfCharTable = malloc((255 + 3) * sizeof(*dfCharTable));
180 memset(dfCharTable, 0, (255 + 3) * sizeof(*dfCharTable));
182 memset(&fi, 0, sizeof(fi));
183 fi.dfFirstChar = first_char;
184 fi.dfLastChar = 0xff;
185 start += ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar + 3 ) * sizeof(*dfCharTable);
187 num_names = FT_Get_Sfnt_Name_Count(face);
188 for(i = 0; i <num_names; i++) {
189 FT_Get_Sfnt_Name(face, i, &sfntname);
190 if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 &&
191 sfntname.language_id == 0 && sfntname.name_id == 0) {
192 size_t len = min( sfntname.string_len, sizeof(hdr.dfCopyright)-1 );
193 memcpy(hdr.dfCopyright, sfntname.string, len);
194 hdr.dfCopyright[len] = 0;
198 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
199 for(i = first_char; i < 0x100; i++) {
200 gi = FT_Get_Char_Index(face, cptable->sbcs.cp2uni[i]);
202 fprintf(stderr, "Missing glyph for char %04x\n", cptable->sbcs.cp2uni[i]);
203 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
204 fprintf(stderr, "error loading char %d - bad news!\n", i);
207 dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
208 dfCharTable[i].offset = start + (width_bytes * ppem);
209 width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
210 if(max_width < (face->glyph->metrics.horiAdvance >> 6))
211 max_width = face->glyph->metrics.horiAdvance >> 6;
214 space_size = (ppem + 3) / 4;
215 dfCharTable[i].width = space_size;
216 dfCharTable[i].offset = start + (width_bytes * ppem);
217 width_bytes += (space_size + 7) >> 3;
219 dfCharTable[++i].width = 0;
220 dfCharTable[i].offset = start + (width_bytes * ppem);
223 fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
226 fi.dfAscent = ascent;
227 fi.dfInternalLeading = il;
228 fi.dfExternalLeading = 0;
229 fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
232 fi.dfWeight = os2->usWeightClass;
233 fi.dfCharSet = lookup_charset(enc);
234 fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
236 fi.dfPixHeight = ppem;
237 fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
238 switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
239 case PAN_FAMILY_SCRIPT:
240 fi.dfPitchAndFamily |= FF_SCRIPT;
242 case PAN_FAMILY_DECORATIVE:
243 case PAN_FAMILY_PICTORIAL:
244 fi.dfPitchAndFamily |= FF_DECORATIVE;
246 case PAN_FAMILY_TEXT_DISPLAY:
247 if(fi.dfPitchAndFamily == 0) /* fixed */
248 fi.dfPitchAndFamily = FF_MODERN;
250 switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
251 case PAN_SERIF_NORMAL_SANS:
252 case PAN_SERIF_OBTUSE_SANS:
253 case PAN_SERIF_PERP_SANS:
254 fi.dfPitchAndFamily |= FF_SWISS;
257 fi.dfPitchAndFamily |= FF_ROMAN;
262 fi.dfPitchAndFamily |= FF_DONTCARE;
265 fi.dfAvgWidth = avg_width;
266 fi.dfMaxWidth = max_width;
267 fi.dfDefaultChar = def_char - fi.dfFirstChar;
268 fi.dfBreakChar = ' ' - fi.dfFirstChar;
269 fi.dfWidthBytes = (width_bytes + 1) & ~1;
271 fi.dfFace = start + fi.dfWidthBytes * ppem;
272 fi.dfBitsOffset = start;
273 fi.dfFlags = 0x10; /* DFF_1COLOR */
274 fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
276 hdr.dfVersion = 0x300;
277 hdr.dfSize = start + fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
278 fwrite(&hdr, sizeof(hdr), 1, fp);
279 fwrite(&fi, sizeof(fi), 1, fp);
280 fwrite(dfCharTable + fi.dfFirstChar, sizeof(*dfCharTable), ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar) + 3, fp);
282 for(i = first_char; i < 0x100; i++) {
283 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
286 assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
288 for(x = 0; x < ((dfCharTable[i].width + 7) / 8); x++) {
289 for(y = 0; y < ppem; y++) {
290 if(y < ascent - face->glyph->bitmap_top ||
291 y >= face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
295 x_off = face->glyph->bitmap_left / 8;
296 x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
297 if(x < x_off || x > x_end) {
304 left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
306 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
307 if(x == x_end && (face->glyph->bitmap_left % 8 != 0) && ((face->glyph->bitmap.width % 8 == 0) || (x != (((face->glyph->bitmap.width) & ~0x7) + face->glyph->bitmap_left) / 8)))
310 right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
312 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
313 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
318 for(x = 0; x < (space_size + 7) / 8; x++) {
319 for(y = 0; y < ppem; y++)
323 if(width_bytes & 1) {
324 for(y = 0; y < ppem; y++)
327 fprintf(fp, "%s", face->family_name);
333 int main(int argc, char **argv)
339 unsigned int def_char;
349 ppem = atoi(argv[2]);
352 def_char = atoi(argv[5]);
353 avg_width = atoi(argv[6]);
355 if(FT_Init_FreeType(&lib))
356 error("ft init failure\n");
358 if(FT_New_Face(lib, argv[1], 0, &face)) {
359 fprintf(stderr, "Can't open face\n");
364 if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
365 fprintf(stderr, "Can't set size\n");
370 strcpy(name, face->family_name);
371 /* FIXME: should add a -o option instead */
372 for(cp = name; *cp; cp++)
374 if(*cp == ' ') *cp = '_';
375 else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
378 sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
380 fp = fopen(output, "w");
382 fill_fontinfo(face, enc, fp, dpi, def_char, avg_width);
387 #else /* HAVE_FREETYPE */
389 int main(int argc, char **argv)
391 fprintf( stderr, "%s needs to be built with FreeType support\n", argv[0] );
395 #endif /* HAVE_FREETYPE */