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