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