Pass all the d3d9 calls onto their wined3d equivalents.
[wine/multimedia.git] / tools / sfnt2fnt.c
blob1a1953d365aa26df4c51ac79b23f1b115d8c8dd1
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
29 #ifdef HAVE_FREETYPE
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
38 #include "wine/unicode.h"
39 #include "wine/wingdi16.h"
40 #include "wingdi.h"
42 #include "pshpack1.h"
44 typedef struct
46 WORD dfVersion;
47 DWORD dfSize;
48 char dfCopyright[60];
49 } FNT_HEADER;
51 typedef struct {
52 WORD width;
53 DWORD offset;
54 } CHAR_TABLE_ENTRY;
56 #include "poppack.h"
58 static void usage(char **argv)
60 fprintf(stderr, "%s foo.ttf ppem enc dpi def_char avg_width\n", argv[0]);
61 return;
64 static int lookup_charset(int enc)
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;
113 fprintf(stderr, "Unknown encoding %d - using OEM_CHARSET\n", enc);
115 return OEM_CHARSET;
118 static void fill_fontinfo(FT_Face face, int enc, FILE *fp, int dpi, unsigned char def_char, int avg_width)
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);
137 if(cptable->info.char_size != 1) {
138 fprintf(stderr, "Can't cope with double byte codepages\n");
139 exit(1);
142 ppem = face->size->metrics.y_ppem;
143 start = sizeof(FNT_HEADER) + sizeof(FONTINFO16);
145 if(FT_Load_Char(face, 0xc5, FT_LOAD_DEFAULT)) {
146 fprintf(stderr, "Can't find Aring\n");
147 exit(1);
149 ascent = face->glyph->metrics.height >> 6;
150 descent = ppem - ascent;
151 if(FT_Load_Char(face, 'M', FT_LOAD_DEFAULT)) {
152 fprintf(stderr, "Can't find M\n");
153 exit(1);
155 il = ascent - (face->glyph->metrics.height >> 6);
157 /* Hack: Courier has no internal leading, so we do likewise */
158 if(!strcmp(face->family_name, "Wine Courier"))
159 il = 0;
161 first_char = FT_Get_First_Char(face, &gi);
162 if(first_char == 0xd) /* fontforge's first glyph is 0xd, we'll catch this and skip it */
163 first_char = 32; /* FT_Get_Next_Char for some reason returns too high
164 number in this case */
166 dfCharTable = malloc((255 + 3) * sizeof(*dfCharTable));
167 memset(dfCharTable, 0, (255 + 3) * sizeof(*dfCharTable));
169 memset(&fi, 0, sizeof(fi));
170 fi.dfFirstChar = first_char;
171 fi.dfLastChar = 0xff;
172 start += ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar + 3 ) * sizeof(*dfCharTable);
174 num_names = FT_Get_Sfnt_Name_Count(face);
175 for(i = 0; i <num_names; i++) {
176 FT_Get_Sfnt_Name(face, i, &sfntname);
177 if(sfntname.platform_id == 1 && sfntname.encoding_id == 0 &&
178 sfntname.language_id == 0 && sfntname.name_id == 0) {
179 size_t len = min( sfntname.string_len, sizeof(hdr.dfCopyright)-1 );
180 memcpy(hdr.dfCopyright, sfntname.string, len);
181 hdr.dfCopyright[len] = 0;
185 os2 = FT_Get_Sfnt_Table(face, ft_sfnt_os2);
186 for(i = first_char; i < 0x100; i++) {
187 gi = FT_Get_Char_Index(face, cptable->sbcs.cp2uni[i]);
188 if(gi == 0)
189 fprintf(stderr, "Missing glyph for char %04x\n", cptable->sbcs.cp2uni[i]);
190 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
191 fprintf(stderr, "error loading char %d - bad news!\n", i);
192 continue;
194 dfCharTable[i].width = face->glyph->metrics.horiAdvance >> 6;
195 dfCharTable[i].offset = start + (width_bytes * ppem);
196 width_bytes += ((face->glyph->metrics.horiAdvance >> 6) + 7) >> 3;
197 if(max_width < (face->glyph->metrics.horiAdvance >> 6))
198 max_width = face->glyph->metrics.horiAdvance >> 6;
200 /* space */
201 space_size = (ppem + 3) / 4;
202 dfCharTable[i].width = space_size;
203 dfCharTable[i].offset = start + (width_bytes * ppem);
204 width_bytes += (space_size + 7) >> 3;
205 /* sentinel */
206 dfCharTable[++i].width = 0;
207 dfCharTable[i].offset = start + (width_bytes * ppem);
209 fi.dfType = 0;
210 fi.dfPoints = ((ppem - il) * 72 + dpi/2) / dpi;
211 fi.dfVertRes = dpi;
212 fi.dfHorizRes = dpi;
213 fi.dfAscent = ascent;
214 fi.dfInternalLeading = il;
215 fi.dfExternalLeading = 0;
216 fi.dfItalic = (face->style_flags & FT_STYLE_FLAG_ITALIC) ? 1 : 0;
217 fi.dfUnderline = 0;
218 fi.dfStrikeOut = 0;
219 fi.dfWeight = os2->usWeightClass;
220 fi.dfCharSet = lookup_charset(enc);
221 fi.dfPixWidth = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) ?
222 avg_width : 0;
223 fi.dfPixHeight = ppem;
224 fi.dfPitchAndFamily = FT_IS_FIXED_WIDTH(face) ? 0 : TMPF_FIXED_PITCH;
225 switch(os2->panose[PAN_FAMILYTYPE_INDEX]) {
226 case PAN_FAMILY_SCRIPT:
227 fi.dfPitchAndFamily |= FF_SCRIPT;
228 break;
229 case PAN_FAMILY_DECORATIVE:
230 case PAN_FAMILY_PICTORIAL:
231 fi.dfPitchAndFamily |= FF_DECORATIVE;
232 break;
233 case PAN_FAMILY_TEXT_DISPLAY:
234 if(fi.dfPitchAndFamily == 0) /* fixed */
235 fi.dfPitchAndFamily = FF_MODERN;
236 else {
237 switch(os2->panose[PAN_SERIFSTYLE_INDEX]) {
238 case PAN_SERIF_NORMAL_SANS:
239 case PAN_SERIF_OBTUSE_SANS:
240 case PAN_SERIF_PERP_SANS:
241 fi.dfPitchAndFamily |= FF_SWISS;
242 break;
243 default:
244 fi.dfPitchAndFamily |= FF_ROMAN;
247 break;
248 default:
249 fi.dfPitchAndFamily |= FF_DONTCARE;
252 fi.dfAvgWidth = avg_width;
253 fi.dfMaxWidth = max_width;
254 fi.dfDefaultChar = def_char - fi.dfFirstChar;
255 fi.dfBreakChar = ' ' - fi.dfFirstChar;
256 fi.dfWidthBytes = (width_bytes + 1) & ~1;
258 fi.dfFace = start + fi.dfWidthBytes * ppem;
259 fi.dfBitsOffset = start;
260 fi.dfFlags = 0x10; /* DFF_1COLOR */
261 fi.dfFlags |= FT_IS_FIXED_WIDTH(face) ? 1 : 2; /* DFF_FIXED : DFF_PROPORTIONAL */
263 hdr.dfVersion = 0x300;
264 hdr.dfSize = start + fi.dfWidthBytes * ppem + strlen(face->family_name) + 1;
265 fwrite(&hdr, sizeof(hdr), 1, fp);
266 fwrite(&fi, sizeof(fi), 1, fp);
267 fwrite(dfCharTable + fi.dfFirstChar, sizeof(*dfCharTable), ((unsigned char)fi.dfLastChar - (unsigned char)fi.dfFirstChar) + 3, fp);
269 for(i = first_char; i < 0x100; i++) {
270 if(FT_Load_Char(face, cptable->sbcs.cp2uni[i], FT_LOAD_DEFAULT)) {
271 continue;
273 assert(dfCharTable[i].width == face->glyph->metrics.horiAdvance >> 6);
275 for(x = 0; x < ((dfCharTable[i].width + 7) / 8); x++) {
276 for(y = 0; y < ppem; y++) {
277 if(y < ascent - face->glyph->bitmap_top ||
278 y >= face->glyph->bitmap.rows + ascent - face->glyph->bitmap_top) {
279 fputc('\0', fp);
280 continue;
282 x_off = face->glyph->bitmap_left / 8;
283 x_end = (face->glyph->bitmap_left + face->glyph->bitmap.width - 1) / 8;
284 if(x < x_off || x > x_end) {
285 fputc('\0', fp);
286 continue;
288 if(x == x_off)
289 left_byte = 0;
290 else
291 left_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off - 1];
293 /* On the last non-trival output byte (x == x_end) have we got one or two input bytes */
294 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)))
295 right_byte = 0;
296 else
297 right_byte = face->glyph->bitmap.buffer[(y - (ascent - face->glyph->bitmap_top)) * face->glyph->bitmap.pitch + x - x_off];
299 byte = (left_byte << (8 - (face->glyph->bitmap_left & 7))) & 0xff;
300 byte |= ((right_byte >> (face->glyph->bitmap_left & 7)) & 0xff);
301 fputc(byte, fp);
305 for(x = 0; x < (space_size + 7) / 8; x++) {
306 for(y = 0; y < ppem; y++)
307 fputc('\0', fp);
310 if(width_bytes & 1) {
311 for(y = 0; y < ppem; y++)
312 fputc('\0', fp);
314 fprintf(fp, "%s", face->family_name);
315 fputc('\0', fp);
320 int main(int argc, char **argv)
322 int ppem, enc;
323 FT_Face face;
324 FT_Library lib;
325 int dpi, avg_width;
326 unsigned int def_char;
327 FILE *fp;
328 char output[256];
329 char name[256];
330 char *cp;
331 if(argc != 7) {
332 usage(argv);
333 exit(0);
336 ppem = atoi(argv[2]);
337 enc = atoi(argv[3]);
338 dpi = atoi(argv[4]);
339 def_char = atoi(argv[5]);
340 avg_width = atoi(argv[6]);
342 if(FT_Init_FreeType(&lib)) {
343 fprintf(stderr, "ft init failure\n");
344 exit(1);
347 if(FT_New_Face(lib, argv[1], 0, &face)) {
348 fprintf(stderr, "Can't open face\n");
349 usage(argv);
350 exit(1);
353 if(FT_Set_Pixel_Sizes(face, ppem, ppem)) {
354 fprintf(stderr, "Can't set size\n");
355 usage(argv);
356 exit(1);
359 strcpy(name, face->family_name);
360 /* FIXME: should add a -o option instead */
361 for(cp = name; *cp; cp++)
363 if(*cp == ' ') *cp = '_';
364 else if (*cp >= 'A' && *cp <= 'Z') *cp += 'a' - 'A';
367 sprintf(output, "%s-%d-%d-%d.fnt", name, enc, dpi, ppem);
369 fp = fopen(output, "w");
371 fill_fontinfo(face, enc, fp, dpi, def_char, avg_width);
372 fclose(fp);
373 exit(0);
376 #else /* HAVE_FREETYPE */
378 int main(int argc, char **argv)
380 fprintf( stderr, "%s needs to be built with Freetype support\n", argv[0] );
381 exit(1);
384 #endif /* HAVE_FREETYPE */