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
22 #include "wine/port.h"
36 #include "wine/winbase16.h"
37 #include "wine/wingdi16.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]);
78 #define __attribute__(X)
81 static void error(const char *s
, ...) __attribute__((format (printf
, 1, 2)));
83 static void error(const char *s
, ...)
87 fprintf(stderr
, "Error: ");
88 vfprintf(stderr
, s
, ap
);
89 fprintf(stderr
, "\n");
94 int main(int argc
, char **argv
)
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] = "";
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
;
112 IMAGE_OS2_HEADER NE_hdr
;
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");
126 fprintf(stderr
, "Can't open %s\n", argv
[i
+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
);
142 while((c
= fgetc(fp
)) != 0 && c
!= EOF
)
145 fprintf(stderr
, "%s %d pts %dx%d dpi\n", name
, pt
, dpi
[0], dpi
[1]);
147 /* fontdir entries for version 3 fonts are the same as for version 2 */
148 fontdir_len
+= 0x74 + strlen(name
) + 1;
150 sprintf(non_resident_name
, "FONTRES 100,%d,%d : %s %d", dpi
[0], dpi
[1], name
, pt
);
151 strcpy(resident_name
, name
);
153 sprintf(non_resident_name
+ strlen(non_resident_name
), ",%d", pt
);
157 strcat(non_resident_name
, " (VGA res)");
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
+
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;
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
;
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
);
197 signal( SIGHUP
, exit_on_signal
);
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
);
207 fwrite(&align
, sizeof(align
), 1, ofp
);
209 rc_type
.type_id
= NE_RSCTYPE_FONTDIR
;
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
;
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
;
236 fwrite(&rc_name
, sizeof(rc_name
), 1, ofp
);
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
);
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 */
262 pad
= ftell(ofp
) & 0xf;
265 for(i
= 0; i
< pad
; i
++)
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
);
283 while((c
= fgetc(fp
)) != 0 && c
!= EOF
)
286 fwrite(name
, strlen(name
) + 1, 1, ofp
);
290 pad
= ftell(ofp
) & 0xf;
293 for(i
= 0; i
< pad
; i
++)
296 for(res
= first_res
, i
= 0; i
< num_files
; i
++, res
++) {
297 fp
= fopen(argv
[i
+1], "rb");
300 nread
= read(fileno(fp
), buf
, sizeof(buf
));
302 fwrite(buf
, nread
, 1, ofp
);
305 pad
= file_lens
[i
] & 0xf;
308 for(j
= 0; j
< pad
; j
++)