Define COBJMACROS. Fixes compilation on Windows.
[wine] / tools / sfnt2fnt.c
1 /*
2  * sfnttofnt.  Bitmap only ttf to Window fnt file converter
3  *
4  * Copyright 2004 Huw Davies
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdio.h>
26
27 #ifdef HAVE_FREETYPE
28
29 #ifdef HAVE_FT2BUILD_H
30 #include <ft2build.h>
31 #endif
32 #include FT_FREETYPE_H
33 #include FT_SFNT_NAMES_H
34 #include FT_TRUETYPE_TABLES_H
35
36 #include "wine/unicode.h"
37 #include "wingdi.h"
38
39 #pragma pack(1)
40
41 typedef struct
42 {
43     WORD dfVersion;
44     DWORD dfSize;
45     char dfCopyright[60];
46 } FNT_HEADER;
47
48 typedef struct
49 {
50     INT16 dfType;
51     INT16 dfPoints;
52     INT16 dfVertRes;
53     INT16 dfHorizRes;
54     INT16 dfAscent;
55     INT16 dfInternalLeading;
56     INT16 dfExternalLeading;
57     CHAR  dfItalic;
58     CHAR  dfUnderline;
59     CHAR  dfStrikeOut;
60     INT16 dfWeight;
61     BYTE  dfCharSet;
62     INT16 dfPixWidth;
63     INT16 dfPixHeight;
64     CHAR  dfPitchAndFamily;
65     INT16 dfAvgWidth;
66     INT16 dfMaxWidth;
67     CHAR  dfFirstChar;
68     CHAR  dfLastChar;
69     CHAR  dfDefaultChar;
70     CHAR  dfBreakChar;
71     INT16 dfWidthBytes;
72     LONG  dfDevice;
73     LONG  dfFace;
74     LONG  dfBitsPointer;
75     LONG  dfBitsOffset;
76     CHAR  dfReserved;
77     /* Fields, introduced for Windows 3.x fonts */
78     LONG  dfFlags;
79     INT16 dfAspace;
80     INT16 dfBspace;
81     INT16 dfCspace;
82     LONG  dfColorPointer;
83     LONG  dfReserved1[4];
84 } FONTINFO16, *LPFONTINFO16;
85
86 typedef struct {
87     WORD width;
88     DWORD offset;
89 } CHAR_TABLE_ENTRY;
90
91
92 void usage(char **argv)
93 {
94     fprintf(stderr, "%s foo.ttf ppem enc dpi def_char\n", argv[0]);
95     return;
96 }
97
98 int lookup_charset(int enc)
99 {
100     /* FIXME: make winelib app and use TranslateCharsetInfo */
101     switch(enc) {
102     case 1250:
103         return EE_CHARSET;
104     case 1251:
105         return RUSSIAN_CHARSET;
106     case 1252:
107         return ANSI_CHARSET;
108     case 1253:
109         return GREEK_CHARSET;
110     case 1254:
111         return TURKISH_CHARSET;
112     case 1255:
113         return HEBREW_CHARSET;
114     case 1256:
115         return ARABIC_CHARSET;
116     case 1257:
117         return BALTIC_CHARSET;
118     case 1258:
119         return VIETNAMESE_CHARSET;
120     case 437:
121     case 737:
122     case 775:
123     case 850:
124     case 852:
125     case 855:
126     case 857:
127     case 860:
128     case 861:
129     case 862:
130     case 863:
131     case 864:
132     case 865:
133     case 866:
134     case 869:
135         return OEM_CHARSET;
136     case 874:
137         return THAI_CHARSET;
138     case 932:
139         return SHIFTJIS_CHARSET;
140     case 936:
141         return GB2312_CHARSET;
142     case 949:
143         return HANGUL_CHARSET;
144     case 950:
145         return CHINESEBIG5_CHARSET;
146     }
147     fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
148
149     return OEM_CHARSET;
150 }
151
152 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char)
153 {
154     int ascent, il, ppem, descent, avg_width, width_bytes = 0, space_size, max_width = 0;
155     FNT_HEADER hdr;
156     FONTINFO16 fi;
157     BYTE left_byte, right_byte, byte;
158     DWORD start;
159     CHAR_TABLE_ENTRY *dfCharTable;
160     int i, x, y, x_off, x_end, first_char;
161     FT_Int gi;
162     int num_names;
163     const union cptable *cptable;
164     FT_SfntName sfntname;
165     char namebuf[4096];
166     TT_OS2 *os2;
167     cptable = wine_cp_get_table(enc);
168     if(!cptable) {
169         fprintf(stderr, "Can't find codepage %d\n", enc);
170         exit(0);
171     }
172     if(cptable->info.char_size != 1) {
173         fprintf(stderr, "Can't cope with double byte codepages\n");
174         exit(0);
175     }
176
177     ppem = face->size->metrics.y_ppem;
178     start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
179
180     if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT)) {
181         fprintf(stderr, "Can't find Aring\n");
182         exit(0);
183     }
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");
188         exit(0);
189     }
190     il = ascent - (face->glyph->metrics.height >> 6);
191
192     /* Hack: Courier has no internal leading, so we do likewise */
193     if(!strcmp(face->family_name, "Wine Courier"))
194         il = 0;
195
196     if(FT_Load_Char(face, 'X', FT_LOAD_DEFAULT)) {
197         fprintf(stderr, "Can't find X\n");
198         exit(0);
199     }
200     avg_width = face->glyph->metrics.horiAdvance >> 6;
201
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);
205
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);
211
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';
220         }
221     }
222
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]);
226         if(gi == 0)
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);
230             continue;
231         }
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;
237     }
238     /* space */
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;
243     /* sentinel */
244     dfCharTable[++i].width = 0;
245     dfCharTable[i].offset = start + (width_bytes * ppem);
246
247     fi.dfType = 0;
248     fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
249     fi.dfVertRes = dpi;
250     fi.dfHorizRes = 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;
255     fi.dfUnderline = 0;
256     fi.dfStrikeOut = 0;
257     fi.dfWeight = os2->usWeightClass;
258     fi.dfCharSet = lookup_charset(enc);
259     fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
260         avg_width : 0;
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;
266         break;
267     case PAN_FAMILY_DECORATIVE:
268     case PAN_FAMILY_PICTORIAL:
269         fi.dfPitchAndFamily |= FF_DECORATIVE;
270         break;
271     case PAN_FAMILY_TEXT_DISPLAY:
272         if(fi.dfPitchAndFamily == 0) /* fixed */
273             fi.dfPitchAndFamily = FF_MODERN;
274         else {
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;
280                 break;
281             default:
282                 fi.dfPitchAndFamily |= FF_ROMAN;
283             }
284         }
285         break;
286     default:
287         fi.dfPitchAndFamily |= FF_DONTCARE;
288     }
289
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;
295
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 */
300
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);
306
307     for(i = first_char; i < 0x100; i++) {
308         if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
309             continue;
310         }
311         assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
312
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) {
317                     fputc('\0', fp);
318                     continue;
319                 }
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) {
323                     fputc('\0', fp);
324                     continue;
325                 }
326                 if(x == x_off)
327                     left_byte = 0;
328                 else
329                     left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
330
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)))
333                     right_byte = 0;
334                 else
335                     right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
336
337                 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
338                 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
339                 fputc(byte, fp);
340             }
341         }
342     }
343     for(x = 0; x < (space_size + 7) / 8; x++) {
344         for(y = 0; y < ppem; y++)
345             fputc('\0', fp);
346     }
347
348     if(width_bytes & 1) {
349         for(y = 0; y < ppem; y++)
350             fputc('\0', fp);
351     }
352     fprintf(fp, "%s", face->family_name);
353     fputc('\0', fp);
354
355 }
356
357
358 int main(int argc, char **argv)
359 {
360     int ppem, enc;
361     FT_Face face;
362     FT_Library lib;
363     int dpi;
364     unsigned int def_char;
365     FILE *fp;
366     char output[256];
367     char name[256];
368     char *cp;
369     if(argc != 6) {
370         usage(argv);
371         exit(0);
372     }
373
374     ppem = atoi(argv[2]);
375     enc = atoi(argv[3]);
376     dpi = atoi(argv[4]);
377     def_char = atoi(argv[5]);
378     if(FT_Init_FreeType(&lib)) {
379         fprintf(stderr, "ft init failure\n");
380         exit(0);
381     }
382
383     if(FT_New_Face(lib, argv[1], 0, &face)) {
384         fprintf(stderr, "Can't open face\n");
385         usage(argv);
386         exit(0);
387     }
388
389     if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
390         fprintf(stderr, "Can't set size\n");
391         usage(argv);
392         exit(0);
393     }
394
395     strcpy(name, face->family_name);
396     /* FIXME: should add a -o option instead */
397     for(cp = name; *cp; cp++)
398     {
399         if(*cp == ' ') *cp = '_';
400         else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
401     }
402
403     sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
404
405     fp = fopen(output, "w");
406
407     fill_fontinfo(face, enc, fp, dpi, def_char);
408     fclose(fp);
409     exit(0);
410 }
411
412 #else /* HAVE_FREETYPE */
413
414 int main(int argc, char **argv)
415 {
416     fprintf( stderr, "%s needs to be built with Freetype support\n", argv[0] );
417     exit(1);
418 }
419
420 #endif /* HAVE_FREETYPE */