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
29 #ifdef HAVE_FT2BUILD_H
32 #include FT_FREETYPE_H
33 #include FT_SFNT_NAMES_H
34 #include FT_TRUETYPE_TABLES_H
36 #include "wine/unicode.h"
55 INT16 dfInternalLeading;
56 INT16 dfExternalLeading;
64 CHAR dfPitchAndFamily;
77 /* Fields, introduced for Windows 3.x fonts */
84 } FONTINFO16, *LPFONTINFO16;
92 void usage(char **argv)
94 fprintf(stderr, "%s foo.ttf ppem enc dpi def_char\n", argv[0]);
98 int lookup_charset(int enc)
100 /* FIXME: make winelib app and use TranslateCharsetInfo */
105 return RUSSIAN_CHARSET;
109 return GREEK_CHARSET;
111 return TURKISH_CHARSET;
113 return HEBREW_CHARSET;
115 return ARABIC_CHARSET;
117 return BALTIC_CHARSET;
119 return VIETNAMESE_CHARSET;
139 return SHIFTJIS_CHARSET;
141 return GB2312_CHARSET;
143 return HANGUL_CHARSET;
145 return CHINESEBIG5_CHARSET;
147 fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
152 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char)
154 int ascent, il, ppem, descent, avg_width, width_bytes = 0, space_size, max_width = 0;
157 BYTE left_byte, right_byte, byte;
159 CHAR_TABLE_ENTRY *dfCharTable;
160 int i, x, y, x_off, x_end, first_char;
163 const union cptable *cptable;
164 FT_SfntName sfntname;
167 cptable = wine_cp_get_table(enc);
169 fprintf(stderr, "Can't find codepage %d\n", enc);
172 if(cptable->info.char_size != 1) {
173 fprintf(stderr, "Can't cope with double byte codepages\n");
177 ppem = face->size->metrics.y_ppem;
178 start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
180 if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT)) {
181 fprintf(stderr, "Can't find Aring\n");
184 ascent = face->glyph->metrics.height >> 6;
185 descent = ppem - ascent;
186 if(FT_Load_Char(face, 'M', FT_LOAD_DEFAULT)) {
187 fprintf(stderr, "Can't find M\n");
190 il = ascent - (face->glyph->metrics.height >> 6);
192 /* Hack: Courier has no internal leading, so we do likewise */
193 if(!strcmp(face->family_name, "Wine Courier"))
196 if(FT_Load_Char(face, 'X', FT_LOAD_DEFAULT)) {
197 fprintf(stderr, "Can't find X\n");
200 avg_width = face->glyph->metrics.horiAdvance >> 6;
202 first_char = FT_Get_First_Char(face, &gi);
203 if(first_char == 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
204 first_char = FT_Get_Next_Char(face, first_char, &gi);
206 dfCharTable = malloc((255 + 3) * sizeof(*dfCharTable));
207 memset(&fi, 0, sizeof(fi));
208 fi.dfFirstChar = first_char;
209 fi.dfLastChar = 0xff;
210 start += ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar + 3 ) * sizeof(*dfCharTable);
212 num_names = FT_Get_Sfnt_Name_Count(face);
213 for(i = 0; i <num_names; i++) {
214 FT_Get_Sfnt_Name(face, i, &sfntname);
215 strncpy(namebuf, sfntname.string, sfntname.string_len);
216 namebuf[sfntname.string_len] = '\0';
217 if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 && sfntname.language_id == 0 && sfntname.name_id == 0) {
218 strncpy(hdr.dfCopyright, namebuf, 60);
219 hdr.dfCopyright[59] = '\0';
223 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
224 for(i = first_char; i < 0x100; i++) {
225 gi = FT_Get_Char_Index(face, cptable->sbcs.cp2uni[i]);
227 fprintf(stderr, "Missing glyph for char %04x\n", cptable->sbcs.cp2uni[i]);
228 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
229 fprintf(stderr, "error loading char %d - bad news!\n", i);
232 dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
233 dfCharTable[i].offset = start + (width_bytes * ppem);
234 width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
235 if(max_width < (face->glyph->metrics.horiAdvance >> 6))
236 max_width = face->glyph->metrics.horiAdvance >> 6;
239 space_size = (ppem + 3) / 4;
240 dfCharTable[i].width = space_size;
241 dfCharTable[i].offset = start + (width_bytes * ppem);
242 width_bytes += (space_size + 7) >> 3;
244 dfCharTable[++i].width = 0;
245 dfCharTable[i].offset = start + (width_bytes * ppem);
248 fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
251 fi.dfAscent = ascent;
252 fi.dfInternalLeading = il;
253 fi.dfExternalLeading = 0;
254 fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
257 fi.dfWeight = os2->usWeightClass;
258 fi.dfCharSet = lookup_charset(enc);
259 fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
261 fi.dfPixHeight = ppem;
262 fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
263 switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
264 case PAN_FAMILY_SCRIPT:
265 fi.dfPitchAndFamily |= FF_SCRIPT;
267 case PAN_FAMILY_DECORATIVE:
268 case PAN_FAMILY_PICTORIAL:
269 fi.dfPitchAndFamily |= FF_DECORATIVE;
271 case PAN_FAMILY_TEXT_DISPLAY:
272 if(fi.dfPitchAndFamily == 0) /* fixed */
273 fi.dfPitchAndFamily = FF_MODERN;
275 switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
276 case PAN_SERIF_NORMAL_SANS:
277 case PAN_SERIF_OBTUSE_SANS:
278 case PAN_SERIF_PERP_SANS:
279 fi.dfPitchAndFamily |= FF_SWISS;
282 fi.dfPitchAndFamily |= FF_ROMAN;
287 fi.dfPitchAndFamily |= FF_DONTCARE;
290 fi.dfAvgWidth = avg_width;
291 fi.dfMaxWidth = max_width;
292 fi.dfDefaultChar = def_char - fi.dfFirstChar;
293 fi.dfBreakChar = ' ' - fi.dfFirstChar;
294 fi.dfWidthBytes = (width_bytes + 1) & ~1;
296 fi.dfFace = start + fi.dfWidthBytes * ppem;
297 fi.dfBitsOffset = start;
298 fi.dfFlags = 0x10; /* DFF_1COLOR */
299 fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
301 hdr.dfVersion = 0x300;
302 hdr.dfSize = start + fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
303 fwrite(&hdr, sizeof(hdr), 1, fp);
304 fwrite(&fi, sizeof(fi), 1, fp);
305 fwrite(dfCharTable + fi.dfFirstChar, sizeof(*dfCharTable), ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar) + 3, fp);
307 for(i = first_char; i < 0x100; i++) {
308 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
311 assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
313 for(x = 0; x < ((dfCharTable[i].width + 7) / 8); x++) {
314 for(y = 0; y < ppem; y++) {
315 if(y < ascent - face->glyph->bitmap_top ||
316 y >= face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
320 x_off = face->glyph->bitmap_left / 8;
321 x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
322 if(x < x_off || x > x_end) {
329 left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
331 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
332 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)))
335 right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
337 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
338 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
343 for(x = 0; x < (space_size + 7) / 8; x++) {
344 for(y = 0; y < ppem; y++)
348 if(width_bytes & 1) {
349 for(y = 0; y < ppem; y++)
352 fprintf(fp, "%s", face->family_name);
358 int main(int argc, char **argv)
364 unsigned int def_char;
374 ppem = atoi(argv[2]);
377 def_char = atoi(argv[5]);
378 if(FT_Init_FreeType(&lib)) {
379 fprintf(stderr, "ft init failure\n");
383 if(FT_New_Face(lib, argv[1], 0, &face)) {
384 fprintf(stderr, "Can't open face\n");
389 if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
390 fprintf(stderr, "Can't set size\n");
395 strcpy(name, face->family_name);
396 /* FIXME: should add a -o option instead */
397 for(cp = name; *cp; cp++)
399 if(*cp == ' ') *cp = '_';
400 else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
403 sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
405 fp = fopen(output, "w");
407 fill_fontinfo(face, enc, fp, dpi, def_char);
412 #else /* HAVE_FREETYPE */
414 int main(int argc, char **argv)
416 fprintf( stderr, "%s needs to be built with Freetype support\n", argv[0] );
420 #endif /* HAVE_FREETYPE */