Use a better location than HKCU\Wine for saving the temporary
[wine] / tools / fnt2fon.c
1 /*
2  * fnttofon.  Combine several fnt files in one fon file
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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <fcntl.h>
30 #ifdef HAVE_IO_H
31 # include <io.h>
32 #endif
33
34 #include "wine/winbase16.h"
35 #include "wine/wingdi16.h"
36
37 #include "pshpack1.h"
38 struct _fnt_header
39 {
40     short dfVersion;
41     long dfSize;
42     char dfCopyright[60];
43     FONTINFO16 fi;
44 };
45 #include "poppack.h"
46
47 static const BYTE MZ_hdr[] = {'M',  'Z',  0x0d, 0x01, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
48                  0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
51                  0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 'T',  'h',
52                  'i',  's',  ' ',  'P',  'r',  'o',  'g',  'r',  'a',  'm',  ' ',  'c',  'a',  'n',  'n',  'o',
53                  't',  ' ',  'b',  'e',  ' ',  'r',  'u',  'n',  ' ',  'i',  'n',  ' ',  'D',  'O',  'S',  ' ',
54                  'm',  'o',  'd',  'e',  0x0d, 0x0a, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
55 };
56
57 static void usage(char **argv)
58 {
59     fprintf(stderr, "%s fntfiles output.fon\n", argv[0]);
60     return;
61 }
62
63 int main(int argc, char **argv)
64 {
65     int i, j;
66     FILE *fp, *ofp;
67     long off;
68     char name[200];
69     int c;
70     char *cp;
71     short pt, ver, dpi[2], align, num_files;
72     int resource_table_len, non_resident_name_len, resident_name_len;
73     unsigned short resource_table_off, resident_name_off, module_ref_off, non_resident_name_off, fontdir_off, font_off;
74     char resident_name[200] = "";
75     int fontdir_len = 2;
76     char non_resident_name[200] = "";
77     int *file_lens, nread;
78     unsigned short first_res = 0x0050, pad, res;
79     struct _fnt_header *fnt_header;
80     char buf[0x1000];
81     IMAGE_OS2_HEADER NE_hdr;
82     NE_TYPEINFO rc_type;
83     NE_NAMEINFO rc_name;
84
85     if(argc < 3) {
86         usage(argv);
87         exit(1);
88     }
89
90     num_files = argc - 2;
91     file_lens = malloc(num_files * sizeof(int));
92     for(i = 0; i < num_files; i++) {
93         fp = fopen(argv[i+1], "rb");
94         if(!fp) {
95             fprintf(stderr, "Can't open %s\n", argv[i+1]);
96             usage(argv);
97             exit(1);
98         }
99         fread(&ver, sizeof(short), 1, fp);
100         if(ver != 0x200 && ver != 0x300) {
101             fprintf(stderr, "invalid fnt file %s ver %d\n", argv[i+1], ver);
102             exit(1);
103         }
104         fread(file_lens + i, sizeof(int), 1, fp);
105         fseek(fp, 0x44, SEEK_SET);
106         fread(&pt, sizeof(short), 1, fp);
107         fread(dpi, sizeof(short), 2, fp);
108         fseek(fp, 0x69, SEEK_SET);
109         fread(&off, sizeof(long), 1, fp);
110         fseek(fp, off, SEEK_SET);
111         cp = name;
112         while((c = fgetc(fp)) != 0 && c != EOF)
113             *cp++ = c;
114         *cp = '\0';
115         fprintf(stderr, "%s %d pts %dx%d dpi\n", name, pt, dpi[0], dpi[1]);
116         fclose(fp);
117         /* fontdir entries for version 3 fonts are the same as for version 2 */
118         fontdir_len += 0x74 + strlen(name) + 1;
119         if(i == 0) {
120             sprintf(non_resident_name, "FONTRES 100,%d,%d : %s %d", dpi[0], dpi[1], name, pt);
121             strcpy(resident_name, name);
122         } else {
123             sprintf(non_resident_name + strlen(non_resident_name), ",%d", pt);
124         }
125     }
126     if(dpi[0] <= 108)
127         strcat(non_resident_name, " (VGA res)");
128     else
129         strcat(non_resident_name, " (8514 res)");
130     non_resident_name_len = strlen(non_resident_name) + 4;
131
132     /* shift count + fontdir entry + num_files of font + nul type + \007FONTDIR */
133     resource_table_len = sizeof(align) + sizeof("FONTDIR") +
134                          sizeof(NE_TYPEINFO) + sizeof(NE_NAMEINFO) +
135                          sizeof(NE_TYPEINFO) + sizeof(NE_NAMEINFO) * num_files +
136                          sizeof(NE_TYPEINFO);
137     resource_table_off = sizeof(NE_hdr);
138     resident_name_off = resource_table_off + resource_table_len;
139     resident_name_len = strlen(resident_name) + 4;
140     module_ref_off = resident_name_off + resident_name_len;
141     non_resident_name_off = sizeof(MZ_hdr) + module_ref_off + sizeof(align);
142
143     memset(&NE_hdr, 0, sizeof(NE_hdr));
144     NE_hdr.ne_magic = 0x454e;
145     NE_hdr.ne_ver = 5;
146     NE_hdr.ne_rev = 1;
147     NE_hdr.ne_flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_GUI;
148     NE_hdr.ne_cbnrestab = non_resident_name_len;
149     NE_hdr.ne_segtab = sizeof(NE_hdr);
150     NE_hdr.ne_rsrctab = sizeof(NE_hdr);
151     NE_hdr.ne_restab = resident_name_off;
152     NE_hdr.ne_modtab = module_ref_off;
153     NE_hdr.ne_imptab = module_ref_off;
154     NE_hdr.ne_enttab = NE_hdr.ne_modtab;
155     NE_hdr.ne_nrestab = non_resident_name_off;
156     NE_hdr.ne_align = 4;
157     NE_hdr.ne_exetyp = NE_OSFLAGS_WINDOWS;
158     NE_hdr.ne_expver = 0x400;
159
160     fontdir_off = (non_resident_name_off + non_resident_name_len + 15) & ~0xf;
161     font_off = (fontdir_off + fontdir_len + 15) & ~0x0f;
162
163     ofp = fopen(argv[argc - 1], "wb");
164
165     fwrite(MZ_hdr, sizeof(MZ_hdr), 1, ofp);
166     fwrite(&NE_hdr, sizeof(NE_hdr), 1, ofp);
167
168     align = 4;
169     fwrite(&align, sizeof(align), 1, ofp);
170
171     rc_type.type_id = NE_RSCTYPE_FONTDIR;
172     rc_type.count = 1;
173     rc_type.resloader = 0;
174     fwrite(&rc_type, sizeof(rc_type), 1, ofp);
175
176     rc_name.offset = fontdir_off >> 4;
177     rc_name.length = (fontdir_len + 15) >> 4;
178     rc_name.flags = NE_SEGFLAGS_MOVEABLE | NE_SEGFLAGS_PRELOAD;
179     rc_name.id = resident_name_off - sizeof("FONTDIR") - NE_hdr.ne_rsrctab;
180     rc_name.handle = 0;
181     rc_name.usage = 0;
182     fwrite(&rc_name, sizeof(rc_name), 1, ofp);
183
184     rc_type.type_id = NE_RSCTYPE_FONT;
185     rc_type.count = num_files;
186     rc_type.resloader = 0;
187     fwrite(&rc_type, sizeof(rc_type), 1, ofp);
188
189     for(res = first_res | 0x8000, i = 0; i < num_files; i++, res++) {
190         int len = (file_lens[i] + 15) & ~0xf;
191
192         rc_name.offset = font_off >> 4;
193         rc_name.length = len >> 4;
194         rc_name.flags = NE_SEGFLAGS_MOVEABLE | NE_SEGFLAGS_SHAREABLE | NE_SEGFLAGS_DISCARDABLE;
195         rc_name.id = res;
196         rc_name.handle = 0;
197         rc_name.usage = 0;
198         fwrite(&rc_name, sizeof(rc_name), 1, ofp);
199
200         font_off += len;
201     }
202
203     /* empty type info */
204     memset(&rc_type, 0, sizeof(rc_type));
205     fwrite(&rc_type, sizeof(rc_type), 1, ofp);
206
207     fputc(strlen("FONTDIR"), ofp);
208     fwrite("FONTDIR", strlen("FONTDIR"), 1, ofp);
209     fputc(strlen(resident_name), ofp);
210     fwrite(resident_name, strlen(resident_name), 1, ofp);
211
212     fputc(0x00, ofp);    fputc(0x00, ofp);
213     fputc(0x00, ofp);
214     fputc(0x00, ofp);    fputc(0x00, ofp);
215
216     fputc(strlen(non_resident_name), ofp);
217     fwrite(non_resident_name, strlen(non_resident_name), 1, ofp);
218     fputc(0x00, ofp); /* terminator */
219
220     /* empty ne_modtab and ne_imptab */
221     fputc(0x00, ofp);
222     fputc(0x00, ofp);
223
224     pad = ftell(ofp) & 0xf;
225     if(pad != 0)
226         pad = 0x10 - pad;
227     for(i = 0; i < pad; i++)
228         fputc(0x00, ofp);
229
230     /* FONTDIR resource */
231     fwrite(&num_files, sizeof(num_files), 1, ofp);
232     
233     for(res = first_res, i = 0; i < num_files; i++, res++) {
234         fp = fopen(argv[i+1], "rb");
235
236         fwrite(&res, sizeof(res), 1, ofp);
237         fread(buf, 0x72, 1, fp);
238
239         fnt_header = (struct _fnt_header *)buf;
240         fseek(fp, fnt_header->fi.dfFace, SEEK_SET);
241         fnt_header->fi.dfBitsOffset = 0;
242         fwrite(buf, 0x72, 1, ofp);
243
244         cp = name;
245         while((c = fgetc(fp)) != 0 && c != EOF)
246             *cp++ = c;
247         *cp = '\0';
248         fwrite(name, strlen(name) + 1, 1, ofp);
249         fclose(fp);
250     }
251
252     pad = ftell(ofp) & 0xf;
253     if(pad != 0)
254         pad = 0x10 - pad;
255     for(i = 0; i < pad; i++)
256         fputc(0x00, ofp);
257
258     for(res = first_res, i = 0; i < num_files; i++, res++) {
259         fp = fopen(argv[i+1], "rb");
260
261         while(1) {
262             nread = read(fileno(fp), buf, sizeof(buf));
263             if(!nread) break;
264             fwrite(buf, nread, 1, ofp);
265         }
266         fclose(fp);
267         pad = file_lens[i] & 0xf;
268         if(pad != 0)
269             pad = 0x10 - pad;
270         for(j = 0; j < pad; j++)
271             fputc(0x00, ofp);
272     }
273     fclose(ofp);
274
275     return 0;
276 }