Can we bundle everthing together?
[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 #include <stdlib.h>
27
28 #ifdef HAVE_FREETYPE
29
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
36
37 #include "wine/unicode.h"
38 #include "wingdi.h"
39
40 #pragma pack(1)
41
42 typedef struct
43 {
44     WORD dfVersion;
45     DWORD dfSize;
46     char dfCopyright[60];
47 } FNT_HEADER;
48
49 typedef struct
50 {
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;
86
87 typedef struct {
88     WORD width;
89     DWORD offset;
90 } CHAR_TABLE_ENTRY;
91
92
93 void usage(char **argv)
94 {
95     fprintf(stderr, "%s foo.ttf ppem enc dpi def_char avg_width\n", argv[0]);
96     return;
97 }
98
99 int lookup_charset(int enc)
100 {
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;
147     }
148     fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
149
150     return OEM_CHARSET;
151 }
152
153 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char, int avg_width)
154 {
155     int ascent, il, ppem, descent, 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);
172     }
173     if(cptable->info.char_size != 1) {
174         fprintf(stderr, "Can't cope with double byte codepages\n");
175         exit(0);
176     }
177
178     ppem = face->size->metrics.y_ppem;
179     start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
180
181     if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT)) {
182         fprintf(stderr, "Can't find Aring\n");
183         exit(0);
184     }
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);
190     }
191     il = ascent - (face->glyph->metrics.height >> 6);
192
193     /* Hack: Courier has no internal leading, so we do likewise */
194     if(!strcmp(face->family_name, "Wine Courier"))
195         il = 0;
196
197     first_char = FT_Get_First_Char(face, &gi);
198     if(first_char == 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
199         first_char = FT_Get_Next_Char(face, first_char, &gi);
200
201     dfCharTable = malloc((255 + 3) * sizeof(*dfCharTable));
202     memset(&fi, 0, sizeof(fi));
203     fi.dfFirstChar = first_char;
204     fi.dfLastChar = 0xff;
205     start += ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar + 3 ) * sizeof(*dfCharTable);
206
207     num_names = FT_Get_Sfnt_Name_Count(face);
208     for(i = 0; i <num_names; i++) {
209         FT_Get_Sfnt_Name(face, i, &sfntname);
210         strncpy(namebuf, sfntname.string, sfntname.string_len);
211         namebuf[sfntname.string_len] = '\0';
212         if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 && sfntname.language_id == 0 && sfntname.name_id == 0) {
213             strncpy(hdr.dfCopyright, namebuf, 60);
214             hdr.dfCopyright[59] = '\0';
215         }
216     }
217
218     os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
219     for(i = first_char; i < 0x100; i++) {
220         gi = FT_Get_Char_Index(face, cptable->sbcs.cp2uni[i]);
221         if(gi == 0)
222             fprintf(stderr, "Missing glyph for char %04x\n", cptable->sbcs.cp2uni[i]);
223         if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
224             fprintf(stderr, "error loading char %d - bad news!\n", i);
225             continue;
226         }
227         dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
228         dfCharTable[i].offset = start + (width_bytes * ppem);
229         width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
230         if(max_width < (face->glyph->metrics.horiAdvance >> 6))
231             max_width = face->glyph->metrics.horiAdvance >> 6;
232     }
233     /* space */
234     space_size = (ppem + 3) / 4;
235     dfCharTable[i].width = space_size;
236     dfCharTable[i].offset = start + (width_bytes * ppem);
237     width_bytes += (space_size + 7) >> 3;
238     /* sentinel */
239     dfCharTable[++i].width = 0;
240     dfCharTable[i].offset = start + (width_bytes * ppem);
241
242     fi.dfType = 0;
243     fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
244     fi.dfVertRes = dpi;
245     fi.dfHorizRes = dpi;
246     fi.dfAscent = ascent;
247     fi.dfInternalLeading = il;
248     fi.dfExternalLeading = 0;
249     fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
250     fi.dfUnderline = 0;
251     fi.dfStrikeOut = 0;
252     fi.dfWeight = os2->usWeightClass;
253     fi.dfCharSet = lookup_charset(enc);
254     fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
255         avg_width : 0;
256     fi.dfPixHeight = ppem;
257     fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
258     switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
259     case PAN_FAMILY_SCRIPT:
260         fi.dfPitchAndFamily |= FF_SCRIPT;
261         break;
262     case PAN_FAMILY_DECORATIVE:
263     case PAN_FAMILY_PICTORIAL:
264         fi.dfPitchAndFamily |= FF_DECORATIVE;
265         break;
266     case PAN_FAMILY_TEXT_DISPLAY:
267         if(fi.dfPitchAndFamily == 0) /* fixed */
268             fi.dfPitchAndFamily = FF_MODERN;
269         else {
270             switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
271             case PAN_SERIF_NORMAL_SANS:
272             case PAN_SERIF_OBTUSE_SANS:
273             case PAN_SERIF_PERP_SANS:
274                 fi.dfPitchAndFamily |= FF_SWISS;
275                 break;
276             default:
277                 fi.dfPitchAndFamily |= FF_ROMAN;
278             }
279         }
280         break;
281     default:
282         fi.dfPitchAndFamily |= FF_DONTCARE;
283     }
284
285     fi.dfAvgWidth = avg_width;
286     fi.dfMaxWidth = max_width;
287     fi.dfDefaultChar = def_char - fi.dfFirstChar;
288     fi.dfBreakChar = ' ' - fi.dfFirstChar;
289     fi.dfWidthBytes = (width_bytes + 1) & ~1;
290
291     fi.dfFace = start + fi.dfWidthBytes * ppem;
292     fi.dfBitsOffset = start;
293     fi.dfFlags = 0x10; /* DFF_1COLOR */
294     fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
295
296     hdr.dfVersion = 0x300;
297     hdr.dfSize = start + fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
298     fwrite(&hdr, sizeof(hdr), 1, fp);
299     fwrite(&fi, sizeof(fi), 1, fp);
300     fwrite(dfCharTable + fi.dfFirstChar, sizeof(*dfCharTable), ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar) + 3, fp);
301
302     for(i = first_char; i < 0x100; i++) {
303         if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
304             continue;
305         }
306         assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
307
308         for(x = 0; x < ((dfCharTable[i].width + 7) / 8); x++) {
309             for(y = 0; y < ppem; y++) {
310                 if(y < ascent - face->glyph->bitmap_top ||
311                    y >=  face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
312                     fputc('\0', fp);
313                     continue;
314                 }
315                 x_off = face->glyph->bitmap_left / 8;
316                 x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
317                 if(x < x_off || x > x_end) {
318                     fputc('\0', fp);
319                     continue;
320                 }
321                 if(x == x_off)
322                     left_byte = 0;
323                 else
324                     left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
325
326                 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
327                 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)))
328                     right_byte = 0;
329                 else
330                     right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
331
332                 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
333                 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
334                 fputc(byte, fp);
335             }
336         }
337     }
338     for(x = 0; x < (space_size + 7) / 8; x++) {
339         for(y = 0; y < ppem; y++)
340             fputc('\0', fp);
341     }
342
343     if(width_bytes & 1) {
344         for(y = 0; y < ppem; y++)
345             fputc('\0', fp);
346     }
347     fprintf(fp, "%s", face->family_name);
348     fputc('\0', fp);
349
350 }
351
352
353 int main(int argc, char **argv)
354 {
355     int ppem, enc;
356     FT_Face face;
357     FT_Library lib;
358     int dpi, avg_width;
359     unsigned int def_char;
360     FILE *fp;
361     char output[256];
362     char name[256];
363     char *cp;
364     if(argc != 7) {
365         usage(argv);
366         exit(0);
367     }
368
369     ppem = atoi(argv[2]);
370     enc = atoi(argv[3]);
371     dpi = atoi(argv[4]);
372     def_char = atoi(argv[5]);
373     avg_width = atoi(argv[6]);
374
375     if(FT_Init_FreeType(&lib)) {
376         fprintf(stderr, "ft init failure\n");
377         exit(0);
378     }
379
380     if(FT_New_Face(lib, argv[1], 0, &face)) {
381         fprintf(stderr, "Can't open face\n");
382         usage(argv);
383         exit(0);
384     }
385
386     if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
387         fprintf(stderr, "Can't set size\n");
388         usage(argv);
389         exit(0);
390     }
391
392     strcpy(name, face->family_name);
393     /* FIXME: should add a -o option instead */
394     for(cp = name; *cp; cp++)
395     {
396         if(*cp == ' ') *cp = '_';
397         else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
398     }
399
400     sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
401
402     fp = fopen(output, "w");
403
404     fill_fontinfo(face, enc, fp, dpi, def_char, avg_width);
405     fclose(fp);
406     exit(0);
407 }
408
409 #else /* HAVE_FREETYPE */
410
411 int main(int argc, char **argv)
412 {
413     fprintf( stderr, "%s needs to be built with Freetype support\n", argv[0] );
414     exit(1);
415 }
416
417 #endif /* HAVE_FREETYPE */