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