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