Huw Davies | 00acb5f | 2004-08-17 22:33:14 +0000 | [diff] [blame] | 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 <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | unsigned char MZ_hdr[] = {'M', 'Z', 0x0d, 0x01, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, |
| 27 | 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 28 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 29 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, |
| 30 | 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 'T', 'h', |
| 31 | 'i', 's', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', ' ', 'c', 'a', 'n', 'n', 'o', |
| 32 | 't', ' ', 'b', 'e', ' ', 'r', 'u', 'n', ' ', 'i', 'n', ' ', 'D', 'O', 'S', ' ', |
| 33 | 'm', 'o', 'd', 'e', 0x0d, 0x0a, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 34 | }; |
| 35 | |
| 36 | unsigned char NE_hdr[] = {'N', 'E', 0x05, 0x01, 0xff, 0xff, 0x02, 0x00, 0x5c, 0x35, 0xe4, 0x41, 0x00, 0x83, 0x00, 0x00, |
| 37 | /* entry table tbd, 2 bytes long. */ |
| 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 39 | 0xff, 0xff, 0x40, 0x00, 0x40, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 40 | /* tbd bytes in non-resident name table, off of seg table 0x40, off of resource table 0x40, off of |
| 41 | resident name table (tbd) etc */ |
| 42 | 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04 |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | static void usage(char **argv) |
| 47 | { |
| 48 | fprintf(stderr, "%s fntfiles output.fon\n", argv[0]); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char **argv) |
| 53 | { |
| 54 | int num_files, i, j; |
| 55 | FILE *fp, *ofp; |
| 56 | long off; |
| 57 | char name[200]; |
| 58 | int c; |
| 59 | char *cp; |
| 60 | short pt, ver, dpi[2]; |
| 61 | int resource_table_len, non_resident_name_len, resident_name_len; |
| 62 | unsigned short resource_table_off, resident_name_off, module_ref_off, non_resident_name_off, fontdir_off, font_off; |
| 63 | char resident_name[200] = ""; |
| 64 | int fontdir_len = 2, fontdir_len_shift; |
| 65 | char non_resident_name[200] = ""; |
| 66 | int *file_lens, nread; |
| 67 | int first_res = 0x0050, pad, res; |
| 68 | char fnt_header[0x100]; |
| 69 | char buf[0x1000]; |
| 70 | |
| 71 | if(argc < 3) { |
| 72 | usage(argv); |
| 73 | exit(1); |
| 74 | } |
| 75 | |
| 76 | num_files = argc - 2; |
| 77 | file_lens = malloc(num_files * sizeof(int)); |
| 78 | for(i = 0; i < num_files; i++) { |
| 79 | fp = fopen(argv[i+1], "r"); |
| 80 | if(!fp) { |
| 81 | fprintf(stderr, "Can't open %s\n", argv[i+1]); |
| 82 | usage(argv); |
| 83 | exit(1); |
| 84 | } |
| 85 | fread(&ver, sizeof(short), 1, fp); |
| 86 | if(ver != 0x200 && ver != 0x300) { |
| 87 | fprintf(stderr, "invalid fnt file %s ver %d\n", argv[i+1], ver); |
| 88 | exit(1); |
| 89 | } |
| 90 | fread(file_lens + i, sizeof(int), 1, fp); |
| 91 | fseek(fp, 0x44, SEEK_SET); |
| 92 | fread(&pt, sizeof(short), 1, fp); |
| 93 | fread(dpi, sizeof(short), 2, fp); |
| 94 | fseek(fp, 0x69, SEEK_SET); |
| 95 | fread(&off, sizeof(long), 1, fp); |
| 96 | fseek(fp, off, SEEK_SET); |
| 97 | cp = name; |
| 98 | while((c = fgetc(fp)) != 0 && c != EOF) |
| 99 | *cp++ = c; |
| 100 | *cp = '\0'; |
| 101 | fprintf(stderr, "%s %d pts %dx%d dpi\n", name, pt, dpi[0], dpi[1]); |
| 102 | fclose(fp); |
| 103 | fontdir_len += 0x74 + strlen(name) + 1; /* FIXME does the fontdir entry for version 3 fonts differ from 2? */ |
| 104 | if(i == 0) { |
| 105 | sprintf(non_resident_name, "FONTRES 100,%d,%d : %s %d", dpi[0], dpi[1], name, pt); |
| 106 | strcpy(resident_name, name); |
| 107 | } else { |
| 108 | sprintf(non_resident_name + strlen(non_resident_name), ",%d", pt); |
| 109 | } |
| 110 | } |
| 111 | if(dpi[0] <= 108) |
| 112 | strcat(non_resident_name, " (VGA res)"); |
| 113 | else |
| 114 | strcat(non_resident_name, " (8514 res)"); |
| 115 | non_resident_name_len = strlen(non_resident_name) + 4; |
| 116 | |
| 117 | /* shift count + fontdir entry + num_files of font + nul type + \007FONTDIR */ |
| 118 | resource_table_len = 2 + 20 + 8 + 12 * num_files + 2 + 8; |
| 119 | resource_table_off = NE_hdr[0x24]; |
| 120 | resource_table_off |= (NE_hdr[0x25] << 8); |
| 121 | resident_name_off = resource_table_off + resource_table_len; |
| 122 | NE_hdr[0x20] = non_resident_name_len & 0xff; |
| 123 | NE_hdr[0x21] = (non_resident_name_len >> 8) & 0xff; |
| 124 | NE_hdr[0x26] = resident_name_off & 0xff; |
| 125 | NE_hdr[0x27] = (resident_name_off >> 8) & 0xff; |
| 126 | resident_name_len = strlen(resident_name) + 4; |
| 127 | module_ref_off = resident_name_off + resident_name_len; |
| 128 | NE_hdr[0x28] = module_ref_off & 0xff; |
| 129 | NE_hdr[0x29] = (module_ref_off >> 8) & 0xff; |
| 130 | NE_hdr[0x2a] = NE_hdr[0x28]; |
| 131 | NE_hdr[0x2b] = NE_hdr[0x29]; |
| 132 | NE_hdr[0x04] = NE_hdr[0x28]; |
| 133 | NE_hdr[0x05] = NE_hdr[0x29]; |
| 134 | non_resident_name_off = 0x80 + module_ref_off + 2; |
| 135 | NE_hdr[0x2c] = non_resident_name_off & 0xff; |
| 136 | NE_hdr[0x2d] = (non_resident_name_off >> 8) & 0xff; |
| 137 | |
| 138 | fontdir_off = (non_resident_name_off + non_resident_name_len + 15) & ~0xf; |
| 139 | font_off = (fontdir_off + fontdir_len + 15) & ~0x0f; |
| 140 | ofp = fopen(argv[argc - 1], "w"); |
| 141 | fwrite(MZ_hdr, sizeof(MZ_hdr), 1, ofp); |
| 142 | fwrite(NE_hdr, sizeof(NE_hdr), 1, ofp); |
| 143 | fputc(0x04, ofp); |
| 144 | fputc(0x00, ofp); /* shift of 4 */ |
| 145 | fputc(0x07, ofp); |
| 146 | fputc(0x80, ofp); /* type 0x8007 */ |
| 147 | fputc(0x01, ofp); |
| 148 | fputc(0x00, ofp); /* 1 off */ |
| 149 | fputc(0x00, ofp); fputc(0x00, ofp); fputc(0x00, ofp); fputc(0x00, ofp); |
| 150 | fontdir_off >>= 4; |
| 151 | fputc(fontdir_off & 0xff, ofp); |
| 152 | fputc((fontdir_off >> 8) & 0xff, ofp); |
| 153 | fontdir_len_shift = (fontdir_len + 15) >> 4; |
| 154 | fputc(fontdir_len_shift & 0xff, ofp); |
| 155 | fputc((fontdir_len_shift >> 8) & 0xff, ofp); |
| 156 | fputc(0x50, ofp); |
| 157 | fputc(0x0c, ofp); |
| 158 | fputc(0x7c, ofp); |
| 159 | fputc(0x00, ofp); |
| 160 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 161 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 162 | fputc(0x08, ofp); |
| 163 | fputc(0x80, ofp); /* type 0x8008 */ |
| 164 | fputc(num_files & 0xff, ofp); |
| 165 | fputc((num_files >> 8) & 0xff, ofp); /* num_files off */ |
| 166 | fputc(0x00, ofp); fputc(0x00, ofp); fputc(0x00, ofp); fputc(0x00, ofp); |
| 167 | for(res = first_res | 0x8000, i = 0; i < num_files; i++, res++) { |
| 168 | int len = (file_lens[i] + 15) & ~0xf; |
| 169 | fputc((font_off >> 4) & 0xff, ofp); |
| 170 | fputc((font_off >> 12) & 0xff, ofp); |
| 171 | fputc((len >> 4) & 0xff, ofp); |
| 172 | fputc((len >> 12) & 0xff, ofp); |
| 173 | font_off += len; |
| 174 | fputc(0x30, ofp); |
| 175 | fputc(0x1c, ofp); |
| 176 | fputc(res & 0xff, ofp); |
| 177 | fputc((res >> 8) & 0xff, ofp); |
| 178 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 179 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 180 | } |
| 181 | fputc(0x00, ofp); fputc(0x00, ofp); /* nul type */ |
| 182 | fputc(strlen("FONTDIR"), ofp); |
| 183 | fwrite("FONTDIR", strlen("FONTDIR"), 1, ofp); |
| 184 | fputc(strlen(resident_name), ofp); |
| 185 | fwrite(resident_name, strlen(resident_name), 1, ofp); |
| 186 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 187 | fputc(0x00, ofp); |
| 188 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 189 | fputc(strlen(non_resident_name), ofp); |
| 190 | fwrite(non_resident_name, strlen(non_resident_name), 1, ofp); |
| 191 | fputc(0x00, ofp); fputc(0x00, ofp); |
| 192 | fputc(0x00, ofp); |
| 193 | pad = (non_resident_name_off + non_resident_name_len) & 0xf; |
| 194 | if(pad != 0) |
| 195 | pad = 0x10 - pad; |
| 196 | for(i = 0; i < pad; i++) |
| 197 | fputc(0x00, ofp); |
| 198 | |
| 199 | /* FONTDIR resource */ |
| 200 | fputc(num_files & 0xff, ofp); |
| 201 | fputc((num_files >> 8) & 0xff, ofp); |
| 202 | |
| 203 | for(res = first_res, i = 0; i < num_files; i++, res++) { |
| 204 | fp = fopen(argv[i+1], "r"); |
| 205 | fputc(res & 0xff, ofp); |
| 206 | fputc((res >> 8) & 0xff, ofp); |
| 207 | fread(fnt_header, 0x72, 1, fp); |
| 208 | fwrite(fnt_header, 0x72, 1, ofp); |
| 209 | |
| 210 | fseek(fp, 0x69, SEEK_SET); |
| 211 | fread(&off, sizeof(long), 1, fp); |
| 212 | fseek(fp, off, SEEK_SET); |
| 213 | cp = name; |
| 214 | while((c = fgetc(fp)) != 0 && c != EOF) |
| 215 | *cp++ = c; |
| 216 | *cp = '\0'; |
| 217 | fwrite(name, strlen(name) + 1, 1, ofp); |
| 218 | fclose(fp); |
| 219 | } |
| 220 | pad = fontdir_len & 0xf; |
| 221 | if(pad != 0) |
| 222 | pad = 0x10 - pad; |
| 223 | for(i = 0; i < pad; i++) |
| 224 | fputc(0x00, ofp); |
| 225 | |
| 226 | for(res = first_res, i = 0; i < num_files; i++, res++) { |
| 227 | fp = fopen(argv[i+1], "r"); |
| 228 | while(1) { |
| 229 | nread = read(fileno(fp), buf, sizeof(buf)); |
| 230 | if(!nread) break; |
| 231 | fwrite(buf, nread, 1, ofp); |
| 232 | } |
| 233 | fclose(fp); |
| 234 | pad = file_lens[i] & 0xf; |
| 235 | if(pad != 0) |
| 236 | pad = 0x10 - pad; |
| 237 | for(j = 0; j < pad; j++) |
| 238 | fputc(0x00, ofp); |
| 239 | } |
| 240 | fclose(ofp); |
| 241 | |
| 242 | return 0; |
| 243 | } |