Beginnings of an infrastructure to allow specifying the target CPU and
[wine/multimedia.git] / tools / sfnt2fnt.c
blobeee3de4469bbb1e4f9035d5a458f68ae758051c3
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 "wine/wingdi16.h"
39 #include "wingdi.h"
41 #include "pshpack1.h"
43 typedef struct
45 WORD dfVersion;
46 DWORD dfSize;
47 char dfCopyright[60];
48 } FNT_HEADER;
50 typedef struct {
51 WORD width;
52 DWORD offset;
53 } CHAR_TABLE_ENTRY;
55 #include "poppack.h"
57 static void usage(char **argv)
59 fprintf(stderr, "%s foo.ttf ppem enc dpi def_char avg_width\n", argv[0]);
60 return;
63 static int lookup_charset(int enc)
65 /* FIXME: make winelib app and use TranslateCharsetInfo */
66 switch(enc) {
67 case 1250:
68 return EE_CHARSET;
69 case 1251:
70 return RUSSIAN_CHARSET;
71 case 1252:
72 return ANSI_CHARSET;
73 case 1253:
74 return GREEK_CHARSET;
75 case 1254:
76 return TURKISH_CHARSET;
77 case 1255:
78 return HEBREW_CHARSET;
79 case 1256:
80 return ARABIC_CHARSET;
81 case 1257:
82 return BALTIC_CHARSET;
83 case 1258:
84 return VIETNAMESE_CHARSET;
85 case 437:
86 case 737:
87 case 775:
88 case 850:
89 case 852:
90 case 855:
91 case 857:
92 case 860:
93 case 861:
94 case 862:
95 case 863:
96 case 864:
97 case 865:
98 case 866:
99 case 869:
100 return OEM_CHARSET;
101 case 874:
102 return THAI_CHARSET;
103 case 932:
104 return SHIFTJIS_CHARSET;
105 case 936:
106 return GB2312_CHARSET;
107 case 949:
108 return HANGUL_CHARSET;
109 case 950:
110 return CHINESEBIG5_CHARSET;
112 fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
114 return OEM_CHARSET;
117 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char, int avg_width)
119 int ascent, il, ppem, descent, width_bytes = 0, space_size, max_width = 0;
120 FNT_HEADER hdr;
121 FONTINFO16 fi;
122 BYTE left_byte, right_byte, byte;
123 DWORD start;
124 CHAR_TABLE_ENTRY *dfCharTable;
125 int i, x, y, x_off, x_end, first_char;
126 FT_Int gi;
127 int num_names;
128 const union cptable *cptable;
129 FT_SfntName sfntname;
130 TT_OS2 *os2;
131 cptable = wine_cp_get_table(enc);
132 if(!cptable) {
133 fprintf(stderr, "Can't find codepage %d\n", enc);
134 exit(0);
136 if(cptable->info.char_size != 1) {
137 fprintf(stderr, "Can't cope with double byte codepages\n");
138 exit(0);
141 ppem = face->size->metrics.y_ppem;
142 start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
144 if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT)) {
145 fprintf(stderr, "Can't find Aring\n");
146 exit(0);
148 ascent = face->glyph->metrics.height >> 6;
149 descent = ppem - ascent;
150 if(FT_Load_Char(face, 'M', FT_LOAD_DEFAULT)) {
151 fprintf(stderr, "Can't find M\n");
152 exit(0);
154 il = ascent - (face->glyph->metrics.height >> 6);
156 /* Hack: Courier has no internal leading, so we do likewise */
157 if(!strcmp(face->family_name, "Wine Courier"))
158 il = 0;
160 first_char = FT_Get_First_Char(face, &gi);
161 if(first_char == 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
162 first_char = 32; /* FT_Get_Next_Char for some reason returns too high
163 number in this case */
165 dfCharTable = malloc((255 + 3) * sizeof(*dfCharTable));
166 memset(dfCharTable, 0, (255 + 3) * sizeof(*dfCharTable));
168 memset(&fi, 0, sizeof(fi));
169 fi.dfFirstChar = first_char;
170 fi.dfLastChar = 0xff;
171 start += ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar + 3 ) * sizeof(*dfCharTable);
173 num_names = FT_Get_Sfnt_Name_Count(face);
174 for(i = 0; i <num_names; i++) {
175 FT_Get_Sfnt_Name(face, i, &sfntname);
176 if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 &&
177 sfntname.language_id == 0 && sfntname.name_id == 0) {
178 size_t len = min( sfntname.string_len, sizeof(hdr.dfCopyright)-1 );
179 memcpy(hdr.dfCopyright, sfntname.string, len);
180 hdr.dfCopyright[len] = 0;
184 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
185 for(i = first_char; i < 0x100; i++) {
186 gi = FT_Get_Char_Index(face, cptable->sbcs.cp2uni[i]);
187 if(gi == 0)
188 fprintf(stderr, "Missing glyph for char %04x\n", cptable->sbcs.cp2uni[i]);
189 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
190 fprintf(stderr, "error loading char %d - bad news!\n", i);
191 continue;
193 dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
194 dfCharTable[i].offset = start + (width_bytes * ppem);
195 width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
196 if(max_width < (face->glyph->metrics.horiAdvance >> 6))
197 max_width = face->glyph->metrics.horiAdvance >> 6;
199 /* space */
200 space_size = (ppem + 3) / 4;
201 dfCharTable[i].width = space_size;
202 dfCharTable[i].offset = start + (width_bytes * ppem);
203 width_bytes += (space_size + 7) >> 3;
204 /* sentinel */
205 dfCharTable[++i].width = 0;
206 dfCharTable[i].offset = start + (width_bytes * ppem);
208 fi.dfType = 0;
209 fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
210 fi.dfVertRes = dpi;
211 fi.dfHorizRes = dpi;
212 fi.dfAscent = ascent;
213 fi.dfInternalLeading = il;
214 fi.dfExternalLeading = 0;
215 fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
216 fi.dfUnderline = 0;
217 fi.dfStrikeOut = 0;
218 fi.dfWeight = os2->usWeightClass;
219 fi.dfCharSet = lookup_charset(enc);
220 fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
221 avg_width : 0;
222 fi.dfPixHeight = ppem;
223 fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
224 switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
225 case PAN_FAMILY_SCRIPT:
226 fi.dfPitchAndFamily |= FF_SCRIPT;
227 break;
228 case PAN_FAMILY_DECORATIVE:
229 case PAN_FAMILY_PICTORIAL:
230 fi.dfPitchAndFamily |= FF_DECORATIVE;
231 break;
232 case PAN_FAMILY_TEXT_DISPLAY:
233 if(fi.dfPitchAndFamily == 0) /* fixed */
234 fi.dfPitchAndFamily = FF_MODERN;
235 else {
236 switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
237 case PAN_SERIF_NORMAL_SANS:
238 case PAN_SERIF_OBTUSE_SANS:
239 case PAN_SERIF_PERP_SANS:
240 fi.dfPitchAndFamily |= FF_SWISS;
241 break;
242 default:
243 fi.dfPitchAndFamily |= FF_ROMAN;
246 break;
247 default:
248 fi.dfPitchAndFamily |= FF_DONTCARE;
251 fi.dfAvgWidth = avg_width;
252 fi.dfMaxWidth = max_width;
253 fi.dfDefaultChar = def_char - fi.dfFirstChar;
254 fi.dfBreakChar = ' ' - fi.dfFirstChar;
255 fi.dfWidthBytes = (width_bytes + 1) & ~1;
257 fi.dfFace = start + fi.dfWidthBytes * ppem;
258 fi.dfBitsOffset = start;
259 fi.dfFlags = 0x10; /* DFF_1COLOR */
260 fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
262 hdr.dfVersion = 0x300;
263 hdr.dfSize = start + fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
264 fwrite(&hdr, sizeof(hdr), 1, fp);
265 fwrite(&fi, sizeof(fi), 1, fp);
266 fwrite(dfCharTable + fi.dfFirstChar, sizeof(*dfCharTable), ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar) + 3, fp);
268 for(i = first_char; i < 0x100; i++) {
269 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
270 continue;
272 assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
274 for(x = 0; x < ((dfCharTable[i].width + 7) / 8); x++) {
275 for(y = 0; y < ppem; y++) {
276 if(y < ascent - face->glyph->bitmap_top ||
277 y >= face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
278 fputc('\0', fp);
279 continue;
281 x_off = face->glyph->bitmap_left / 8;
282 x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
283 if(x < x_off || x > x_end) {
284 fputc('\0', fp);
285 continue;
287 if(x == x_off)
288 left_byte = 0;
289 else
290 left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
292 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
293 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)))
294 right_byte = 0;
295 else
296 right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
298 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
299 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
300 fputc(byte, fp);
304 for(x = 0; x < (space_size + 7) / 8; x++) {
305 for(y = 0; y < ppem; y++)
306 fputc('\0', fp);
309 if(width_bytes & 1) {
310 for(y = 0; y < ppem; y++)
311 fputc('\0', fp);
313 fprintf(fp, "%s", face->family_name);
314 fputc('\0', fp);
319 int main(int argc, char **argv)
321 int ppem, enc;
322 FT_Face face;
323 FT_Library lib;
324 int dpi, avg_width;
325 unsigned int def_char;
326 FILE *fp;
327 char output[256];
328 char name[256];
329 char *cp;
330 if(argc != 7) {
331 usage(argv);
332 exit(0);
335 ppem = atoi(argv[2]);
336 enc = atoi(argv[3]);
337 dpi = atoi(argv[4]);
338 def_char = atoi(argv[5]);
339 avg_width = atoi(argv[6]);
341 if(FT_Init_FreeType(&lib)) {
342 fprintf(stderr, "ft init failure\n");
343 exit(0);
346 if(FT_New_Face(lib, argv[1], 0, &face)) {
347 fprintf(stderr, "Can't open face\n");
348 usage(argv);
349 exit(0);
352 if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
353 fprintf(stderr, "Can't set size\n");
354 usage(argv);
355 exit(0);
358 strcpy(name, face->family_name);
359 /* FIXME: should add a -o option instead */
360 for(cp = name; *cp; cp++)
362 if(*cp == ' ') *cp = '_';
363 else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
366 sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
368 fp = fopen(output, "w");
370 fill_fontinfo(face, enc, fp, dpi, def_char, avg_width);
371 fclose(fp);
372 exit(0);
375 #else /* HAVE_FREETYPE */
377 int main(int argc, char **argv)
379 fprintf( stderr, "%s needs to be built with Freetype support\n", argv[0] );
380 exit(1);
383 #endif /* HAVE_FREETYPE */