Release 20040914.
[wine/multimedia.git] / tools / sfnt2fnt.c
blob0ffe7e3a9b3c29036688e284eef1d22ca779600c
1 /*
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
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #ifdef HAVE_FREETYPE
30 #ifdef HAVE_FT2BUILD_H
31 #include <ft2build.h>
32 #endif
33 #include FT_FREETYPE_H
34 #include FT_SFNT_NAMES_H
35 #include FT_TRUETYPE_TABLES_H
37 #include "wine/unicode.h"
38 #include "wingdi.h"
40 #pragma pack(1)
42 typedef struct
44 WORD dfVersion;
45 DWORD dfSize;
46 char dfCopyright[60];
47 } FNT_HEADER;
49 typedef struct
51 INT16 dfType;
52 INT16 dfPoints;
53 INT16 dfVertRes;
54 INT16 dfHorizRes;
55 INT16 dfAscent;
56 INT16 dfInternalLeading;
57 INT16 dfExternalLeading;
58 CHAR dfItalic;
59 CHAR dfUnderline;
60 CHAR dfStrikeOut;
61 INT16 dfWeight;
62 BYTE dfCharSet;
63 INT16 dfPixWidth;
64 INT16 dfPixHeight;
65 CHAR dfPitchAndFamily;
66 INT16 dfAvgWidth;
67 INT16 dfMaxWidth;
68 CHAR dfFirstChar;
69 CHAR dfLastChar;
70 CHAR dfDefaultChar;
71 CHAR dfBreakChar;
72 INT16 dfWidthBytes;
73 LONG dfDevice;
74 LONG dfFace;
75 LONG dfBitsPointer;
76 LONG dfBitsOffset;
77 CHAR dfReserved;
78 /* Fields, introduced for Windows 3.x fonts */
79 LONG dfFlags;
80 INT16 dfAspace;
81 INT16 dfBspace;
82 INT16 dfCspace;
83 LONG dfColorPointer;
84 LONG dfReserved1[4];
85 } FONTINFO16, *LPFONTINFO16;
87 typedef struct {
88 WORD width;
89 DWORD offset;
90 } CHAR_TABLE_ENTRY;
93 void usage(char **argv)
95 fprintf(stderr, "%s foo.ttf ppem enc dpi def_char\n", argv[0]);
96 return;
99 int lookup_charset(int enc)
101 /* FIXME: make winelib app and use TranslateCharsetInfo */
102 switch(enc) {
103 case 1250:
104 return EE_CHARSET;
105 case 1251:
106 return RUSSIAN_CHARSET;
107 case 1252:
108 return ANSI_CHARSET;
109 case 1253:
110 return GREEK_CHARSET;
111 case 1254:
112 return TURKISH_CHARSET;
113 case 1255:
114 return HEBREW_CHARSET;
115 case 1256:
116 return ARABIC_CHARSET;
117 case 1257:
118 return BALTIC_CHARSET;
119 case 1258:
120 return VIETNAMESE_CHARSET;
121 case 437:
122 case 737:
123 case 775:
124 case 850:
125 case 852:
126 case 855:
127 case 857:
128 case 860:
129 case 861:
130 case 862:
131 case 863:
132 case 864:
133 case 865:
134 case 866:
135 case 869:
136 return OEM_CHARSET;
137 case 874:
138 return THAI_CHARSET;
139 case 932:
140 return SHIFTJIS_CHARSET;
141 case 936:
142 return GB2312_CHARSET;
143 case 949:
144 return HANGUL_CHARSET;
145 case 950:
146 return CHINESEBIG5_CHARSET;
148 fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
150 return OEM_CHARSET;
153 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char)
155 int ascent, il, ppem, descent, avg_width, width_bytes = 0, space_size, max_width = 0;
156 FNT_HEADER hdr;
157 FONTINFO16 fi;
158 BYTE left_byte, right_byte, byte;
159 DWORD start;
160 CHAR_TABLE_ENTRY *dfCharTable;
161 int i, x, y, x_off, x_end, first_char;
162 FT_Int gi;
163 int num_names;
164 const union cptable *cptable;
165 FT_SfntName sfntname;
166 char namebuf[4096];
167 TT_OS2 *os2;
168 cptable = wine_cp_get_table(enc);
169 if(!cptable) {
170 fprintf(stderr, "Can't find codepage %d\n", enc);
171 exit(0);
173 if(cptable->info.char_size != 1) {
174 fprintf(stderr, "Can't cope with double byte codepages\n");
175 exit(0);
178 ppem = face->size->metrics.y_ppem;
179 start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
181 if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT)) {
182 fprintf(stderr, "Can't find Aring\n");
183 exit(0);
185 ascent = face->glyph->metrics.height >> 6;
186 descent = ppem - ascent;
187 if(FT_Load_Char(face, 'M', FT_LOAD_DEFAULT)) {
188 fprintf(stderr, "Can't find M\n");
189 exit(0);
191 il = ascent - (face->glyph->metrics.height >> 6);
193 /* Hack: Courier has no internal leading, so we do likewise */
194 if(!strcmp(face->family_name, "Wine Courier"))
195 il = 0;
197 if(FT_Load_Char(face, 'X', FT_LOAD_DEFAULT)) {
198 fprintf(stderr, "Can't find X\n");
199 exit(0);
201 avg_width = face->glyph->metrics.horiAdvance >> 6;
203 first_char = FT_Get_First_Char(face, &gi);
204 if(first_char == 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
205 first_char = FT_Get_Next_Char(face, first_char, &gi);
207 dfCharTable = malloc((255 + 3) * sizeof(*dfCharTable));
208 memset(&fi, 0, sizeof(fi));
209 fi.dfFirstChar = first_char;
210 fi.dfLastChar = 0xff;
211 start += ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar + 3 ) * sizeof(*dfCharTable);
213 num_names = FT_Get_Sfnt_Name_Count(face);
214 for(i = 0; i <num_names; i++) {
215 FT_Get_Sfnt_Name(face, i, &sfntname);
216 strncpy(namebuf, sfntname.string, sfntname.string_len);
217 namebuf[sfntname.string_len] = '\0';
218 if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 && sfntname.language_id == 0 && sfntname.name_id == 0) {
219 strncpy(hdr.dfCopyright, namebuf, 60);
220 hdr.dfCopyright[59] = '\0';
224 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
225 for(i = first_char; i < 0x100; i++) {
226 gi = FT_Get_Char_Index(face, cptable->sbcs.cp2uni[i]);
227 if(gi == 0)
228 fprintf(stderr, "Missing glyph for char %04x\n", cptable->sbcs.cp2uni[i]);
229 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
230 fprintf(stderr, "error loading char %d - bad news!\n", i);
231 continue;
233 dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
234 dfCharTable[i].offset = start + (width_bytes * ppem);
235 width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
236 if(max_width < (face->glyph->metrics.horiAdvance >> 6))
237 max_width = face->glyph->metrics.horiAdvance >> 6;
239 /* space */
240 space_size = (ppem + 3) / 4;
241 dfCharTable[i].width = space_size;
242 dfCharTable[i].offset = start + (width_bytes * ppem);
243 width_bytes += (space_size + 7) >> 3;
244 /* sentinel */
245 dfCharTable[++i].width = 0;
246 dfCharTable[i].offset = start + (width_bytes * ppem);
248 fi.dfType = 0;
249 fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
250 fi.dfVertRes = dpi;
251 fi.dfHorizRes = dpi;
252 fi.dfAscent = ascent;
253 fi.dfInternalLeading = il;
254 fi.dfExternalLeading = 0;
255 fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
256 fi.dfUnderline = 0;
257 fi.dfStrikeOut = 0;
258 fi.dfWeight = os2->usWeightClass;
259 fi.dfCharSet = lookup_charset(enc);
260 fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
261 avg_width : 0;
262 fi.dfPixHeight = ppem;
263 fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
264 switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
265 case PAN_FAMILY_SCRIPT:
266 fi.dfPitchAndFamily |= FF_SCRIPT;
267 break;
268 case PAN_FAMILY_DECORATIVE:
269 case PAN_FAMILY_PICTORIAL:
270 fi.dfPitchAndFamily |= FF_DECORATIVE;
271 break;
272 case PAN_FAMILY_TEXT_DISPLAY:
273 if(fi.dfPitchAndFamily == 0) /* fixed */
274 fi.dfPitchAndFamily = FF_MODERN;
275 else {
276 switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
277 case PAN_SERIF_NORMAL_SANS:
278 case PAN_SERIF_OBTUSE_SANS:
279 case PAN_SERIF_PERP_SANS:
280 fi.dfPitchAndFamily |= FF_SWISS;
281 break;
282 default:
283 fi.dfPitchAndFamily |= FF_ROMAN;
286 break;
287 default:
288 fi.dfPitchAndFamily |= FF_DONTCARE;
291 fi.dfAvgWidth = avg_width;
292 fi.dfMaxWidth = max_width;
293 fi.dfDefaultChar = def_char - fi.dfFirstChar;
294 fi.dfBreakChar = ' ' - fi.dfFirstChar;
295 fi.dfWidthBytes = (width_bytes + 1) & ~1;
297 fi.dfFace = start + fi.dfWidthBytes * ppem;
298 fi.dfBitsOffset = start;
299 fi.dfFlags = 0x10; /* DFF_1COLOR */
300 fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
302 hdr.dfVersion = 0x300;
303 hdr.dfSize = start + fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
304 fwrite(&hdr, sizeof(hdr), 1, fp);
305 fwrite(&fi, sizeof(fi), 1, fp);
306 fwrite(dfCharTable + fi.dfFirstChar, sizeof(*dfCharTable), ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar) + 3, fp);
308 for(i = first_char; i < 0x100; i++) {
309 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
310 continue;
312 assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
314 for(x = 0; x < ((dfCharTable[i].width + 7) / 8); x++) {
315 for(y = 0; y < ppem; y++) {
316 if(y < ascent - face->glyph->bitmap_top ||
317 y >= face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
318 fputc('\0', fp);
319 continue;
321 x_off = face->glyph->bitmap_left / 8;
322 x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
323 if(x < x_off || x > x_end) {
324 fputc('\0', fp);
325 continue;
327 if(x == x_off)
328 left_byte = 0;
329 else
330 left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
332 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
333 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)))
334 right_byte = 0;
335 else
336 right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
338 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
339 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
340 fputc(byte, fp);
344 for(x = 0; x < (space_size + 7) / 8; x++) {
345 for(y = 0; y < ppem; y++)
346 fputc('\0', fp);
349 if(width_bytes & 1) {
350 for(y = 0; y < ppem; y++)
351 fputc('\0', fp);
353 fprintf(fp, "%s", face->family_name);
354 fputc('\0', fp);
359 int main(int argc, char **argv)
361 int ppem, enc;
362 FT_Face face;
363 FT_Library lib;
364 int dpi;
365 unsigned int def_char;
366 FILE *fp;
367 char output[256];
368 char name[256];
369 char *cp;
370 if(argc != 6) {
371 usage(argv);
372 exit(0);
375 ppem = atoi(argv[2]);
376 enc = atoi(argv[3]);
377 dpi = atoi(argv[4]);
378 def_char = atoi(argv[5]);
379 if(FT_Init_FreeType(&lib)) {
380 fprintf(stderr, "ft init failure\n");
381 exit(0);
384 if(FT_New_Face(lib, argv[1], 0, &face)) {
385 fprintf(stderr, "Can't open face\n");
386 usage(argv);
387 exit(0);
390 if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
391 fprintf(stderr, "Can't set size\n");
392 usage(argv);
393 exit(0);
396 strcpy(name, face->family_name);
397 /* FIXME: should add a -o option instead */
398 for(cp = name; *cp; cp++)
400 if(*cp == ' ') *cp = '_';
401 else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
404 sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
406 fp = fopen(output, "w");
408 fill_fontinfo(face, enc, fp, dpi, def_char);
409 fclose(fp);
410 exit(0);
413 #else /* HAVE_FREETYPE */
415 int main(int argc, char **argv)
417 fprintf( stderr, "%s needs to be built with Freetype support\n", argv[0] );
418 exit(1);
421 #endif /* HAVE_FREETYPE */