- Bugfix: Styles were evaluated as expressions. The NOT in combination
[wine.git] / tools / fnt2bdf.c
blob0f1691994d5cb479590ae190cd53d04f5243b46a
1 /************************************************
3 * Extract fonts from .fnt or Windows DLL files
4 * and convert them to the .bdf format.
6 * Copyright 1994-1996 Kevin Carothers and Alex Korobka
8 */
10 #include "config.h"
12 #ifdef HAVE_SYS_PARAM_H
13 # include <sys/param.h>
14 #endif
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <fcntl.h>
22 #include "winuser.h"
23 #include "fnt2bdf.h"
24 #include "neexe.h"
25 #include "module.h"
27 #define MAP_BEG 118
29 extern char* g_lpstrFileName;
30 extern char* g_lpstrCharSet;
32 #define FILE_ERROR 0
33 #define FILE_DLL 1
34 #define FILE_FNT 2
36 /* global options */
38 char* g_lpstrFileName = NULL;
39 char* g_lpstrCharSet = NULL;
40 char* g_lpstrInputFile = NULL;
41 int g_outputPoints = 0;
43 static char* errorDLLRead = "Unable to read Windows DLL.\n";
44 static char* errorFNTRead = "Unable to read .FNT file.\n";
45 static char* errorOpenFile = "Unable to open file.\n";
46 static char* errorMemory = "Memory allocation error.\n";
47 static char* errorFile = "Corrupt or invalid file.\n";
48 static char* errorFontData = "Unable to parse font data: Error ";
49 static char* errorEmpty = "No fonts found.\n";
51 /* info */
53 void usage(void)
55 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
56 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
57 printf(" -f basename\tbasic output filename\n");
58 printf(" -t \t\toutput files by point size instead of pixel height\n");
59 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
60 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
61 exit(-1);
64 /* convert big-endian value to the local format */
66 int return_data_value(enum data_types dtype, void * pChr)
68 int ret_val = 0;
70 switch(dtype) {
71 case (dfChar):
72 ret_val = (int) *(unsigned char *)pChr;
73 break;
75 case(dfShort):
76 ret_val = *(unsigned char *)pChr;
77 ret_val += (*((unsigned char *)pChr + 1) << 8);
78 break;
80 case(dfLong): {
81 int i;
83 for(i=3; i >= 0; i--) {
84 ret_val += *((unsigned char *)pChr + i) << (8*i);
86 break;
88 case(dfString):
89 break;
91 return ret_val;
94 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
96 int l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
97 char* lpChar;
99 if( !g_lpstrFileName )
101 if( !l_nameoffset ||
102 l_nameoffset > return_data_value(dfLong, cpe_font_struct->hdr.dfSize) + 1 )
103 return ERROR_DATA;
104 lpChar = (char*)(file_buffer + l_nameoffset);
106 else lpChar = g_lpstrFileName;
108 strcpy( name, lpChar );
110 while( (lpChar = strchr( name, ' ')) )
111 *lpChar = '-';
113 /* construct a filename from the font typeface, slant, weight, and size */
115 if( cpe_font_struct->hdr.dfItalic[0] ) strcat(name, "_i" );
116 else strcat(name, "_r" );
118 lpChar = name + strlen( name );
119 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, cpe_font_struct->hdr.dfWeight),
120 (g_outputPoints) ? return_data_value(dfShort, cpe_font_struct->hdr.dfPoints)
121 : return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight) );
122 return 0;
125 /* parse FONT resource and write .bdf file */
127 int parse_fnt_data(unsigned char* file_buffer, int length)
129 fnt_fontS cpe_font_struct;
130 int ic=0, t;
132 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
134 /* check font header */
136 t = return_data_value(dfShort, cpe_font_struct.hdr.dfVersion);
137 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
139 t = return_data_value(dfLong, cpe_font_struct.hdr.dfSize);
140 if( t > length ) return ERROR_SIZE;
141 else
143 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
145 int l_fchar = return_data_value(dfChar, cpe_font_struct.hdr.dfFirstChar),
146 l_lchar = return_data_value(dfChar, cpe_font_struct.hdr.dfLastChar);
147 int l_len = l_lchar - l_fchar + 1, l_ptr = MAP_BEG;
149 /* malloc size = (# chars) * sizeof(WinCharS) */
151 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
152 return ERROR_MEMORY;
154 /* NOW, convert them all to UNIX (lton) notation... */
156 for(ic=0; ic < l_len; ic++) {
157 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
158 l_ptr += 2; /* bump by sizeof(short) */
161 if( return_data_value(dfShort, cpe_font_struct.hdr.dfVersion) == 0x200) {
162 cpe_font_struct.dfCharTable[ic].charOffset =
163 return_data_value(dfShort, &file_buffer[l_ptr]);
164 l_ptr += 2; /* bump by sizeof(long) */
166 else { /* Windows Version 3.0 type font */
167 cpe_font_struct.dfCharTable[ic].charOffset =
168 return_data_value(dfLong, &file_buffer[l_ptr]);
169 l_ptr += 4; /* bump by sizeof(long) */
172 t = dump_bdf(&cpe_font_struct, file_buffer);
173 free( cpe_font_struct.dfCharTable );
175 return t;
178 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
180 FILE* fp;
181 int ic;
182 int l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar),
183 l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
185 int l_len = l_lchar-l_fchar + 1,
186 l_hgt = return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight);
187 int l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
188 char l_filename[256];
190 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
191 return ic;
193 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
195 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
196 return ERROR_FILE;
199 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
200 if (ic) return (ic);
202 /* NOW, convert all chars to UNIX (lton) notation... */
204 for(ic=0; ic < l_len; ic++) {
205 int rowidx, l_span, /* how many char-cols wide is char? */
206 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
208 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
210 fprintf(fp, "STARTCHAR %d \n", ic);
211 fprintf(fp, "ENCODING %d\n", l_fchar);
212 fprintf(fp, "SWIDTH %d %d \n",
213 cpe_font_struct->dfCharTable[ic].charWidth*1000,
216 fprintf(fp, "DWIDTH %d %d \n",
217 cpe_font_struct->dfCharTable[ic].charWidth, 0);
219 fprintf(fp, "BBX %d %d %d %d\n",
220 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
221 l_ascent - l_hgt);
223 fprintf(fp, "BITMAP\n");
224 for(rowidx=0; rowidx < l_hgt; rowidx++) {
225 switch(l_span) {
226 case(0): /* 1-7 pixels wide font */
228 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
229 break;
232 case(1): /* 8-15 pixels wide font */
234 fprintf(fp, "%02X%02X",
235 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
236 fprintf(fp, "\n");
237 break;
240 case(2): /* 16-23 pixels wide font */
242 fprintf(fp, "%02X%02X%02X",
243 file_buffer[l_idx+rowidx],
244 file_buffer[l_idx+l_hgt+rowidx],
245 file_buffer[l_idx+(2*l_hgt)+rowidx]);
246 fprintf(fp, "\n");
247 break;
250 case(3): /* 24-31 pixels wide font */
252 fprintf(fp, "%02X%02X%02X%02X",
253 file_buffer[l_idx+rowidx],
254 file_buffer[l_idx+l_hgt+rowidx],
255 file_buffer[l_idx+(2*l_hgt)+rowidx],
256 file_buffer[l_idx+(3*l_hgt)+rowidx]);
257 fprintf(fp, "\n");
258 break;
260 case(4): /* 32-39 */
262 fprintf(fp, "%02X%02X%02X%02X%02X",
263 file_buffer[l_idx+rowidx],
264 file_buffer[l_idx+l_hgt+rowidx],
265 file_buffer[l_idx+(2*l_hgt)+rowidx],
266 file_buffer[l_idx+(3*l_hgt)+rowidx],
267 file_buffer[l_idx+(4*l_hgt)+rowidx]);
268 fprintf(fp, "\n");
269 break;
271 default:
272 fclose(fp);
273 unlink(l_filename);
274 return ERROR_DATA;
277 fprintf(fp, "ENDCHAR\n");
279 l_fchar++; /* Go to next one */
281 fprintf(fp, "ENDFONT\n");
282 fclose(fp);
283 return 0;
287 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
289 int l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar),
290 l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
291 int l_len = l_lchar - l_fchar + 1;
292 int l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
293 int l_cellheight = return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight);
294 int l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
296 fprintf(fs, "STARTFONT 2.1\n");
298 /* Compose font name */
300 if( l_nameoffset &&
301 l_nameoffset < return_data_value(dfLong, cpe_font_struct->hdr.dfSize) )
303 int dpi, point_size;
304 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
305 short tmWeight = return_data_value(dfShort, cpe_font_struct->hdr.dfWeight);
307 while((lpChar = strchr(lpFace, '-')) )
308 *lpChar = ' ';
310 fprintf(fs, "FONT -windows-%s-", lpFace );
312 if( tmWeight == 0 ) /* weight */
313 fputs("medium-", fs);
314 else if( tmWeight <= FW_LIGHT )
315 fputs("light-", fs);
316 else if( tmWeight <= FW_MEDIUM )
317 fputs("medium-", fs);
318 else if( tmWeight <= FW_DEMIBOLD )
319 fputs("demibold-", fs);
320 else if( tmWeight <= FW_BOLD )
321 fputs("bold-", fs);
322 else fputs("black-", fs);
324 if( cpe_font_struct->hdr.dfItalic[0] ) /* slant */
325 fputs("i-", fs);
326 else fputs("r-", fs);
328 /* style */
330 if( (cpe_font_struct->hdr.dfPitchAndFamily[0] & 0xF0) == FF_SWISS )
331 fputs("normal-sans-", fs);
332 else fputs("normal--", fs); /* still can be -sans */
334 /* y extents */
336 point_size = 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfPoints );
337 dpi = (l_cellheight * 720) / point_size;
339 fprintf(fs, "%d-%d-%d-%d-", l_cellheight, 10*l_cellheight, 72, 72);
340 /* point_size, dpi, dpi); */
342 /* spacing */
344 if( return_data_value(dfShort, cpe_font_struct->hdr.dfPixWidth) ) fputs("c-", fs);
345 else fputs("p-", fs);
347 /* average width */
349 fprintf( fs, "%d-", 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfAvgWidth) );
351 /* charset */
353 switch( cpe_font_struct->hdr.dfCharSet[0] )
355 /* Microsoft just had to invent its own charsets! */
357 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
358 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
359 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
360 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
361 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
362 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
363 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
364 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
365 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
366 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
367 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
369 default:
370 case OEM_CHARSET:
371 if( !g_lpstrCharSet )
373 fputs("Undefined charset, use -c option.\n", stderr);
374 return ERROR_DATA;
376 fprintf(fs, "%s\n", g_lpstrCharSet);
379 else return ERROR_DATA;
381 fprintf(fs, "SIZE %d %d %d\n",
382 l_cellheight,
383 return_data_value(dfShort, cpe_font_struct->hdr.dfHorizRes),
384 return_data_value(dfShort, cpe_font_struct->hdr.dfVertRes)); /* dfVertRes[2] */
386 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
387 return_data_value(dfShort, cpe_font_struct->hdr.dfMaxWidth),
388 return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight),
389 0, l_ascent - l_cellheight );
391 fprintf(fs, "STARTPROPERTIES 4\n");
393 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
394 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
395 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
396 return_data_value(dfShort, cpe_font_struct->hdr.dfInternalLeading));
397 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, cpe_font_struct->hdr.dfDefaultChar));
399 fprintf(fs, "ENDPROPERTIES\n");
401 fprintf(fs, "CHARS %d\n", l_len);
402 return 0;
407 void parse_options(int argc, char **argv)
409 int i;
411 switch( argc )
413 case 2:
414 g_lpstrInputFile = argv[1];
415 break;
417 case 3:
418 case 4:
419 case 5:
420 case 6:
421 case 7:
422 case 8:
423 for( i = 1; i < argc - 1; i++ )
425 if( argv[i][0] != '-' ||
426 strlen(argv[i]) != 2 ) break;
428 if( argv[i][1] == 'c' )
429 g_lpstrCharSet = argv[i+1];
430 else
431 if( argv[i][1] == 'f' )
432 g_lpstrFileName = argv[i+1];
433 else
434 if( argv[i][1] == 't' )
436 g_outputPoints = 1;
437 continue;
439 else
440 usage();
442 i++;
444 if( i == argc - 1 )
446 g_lpstrInputFile = argv[i];
447 break;
449 default: usage();
454 /* read file data and return file type */
456 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
458 IMAGE_DOS_HEADER mz_header;
459 IMAGE_OS2_HEADER ne_header;
460 short s, offset, size, retval;
463 lseek( fd, 0, SEEK_SET );
465 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
466 return FILE_ERROR;
468 s = return_data_value(dfShort, &mz_header.e_magic);
470 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
472 s = return_data_value(dfShort, &mz_header.e_lfanew);
473 lseek( fd, s, SEEK_SET );
475 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
476 return FILE_ERROR;
478 s = return_data_value(dfShort, &ne_header.ne_magic);
480 if( s == IMAGE_NT_SIGNATURE)
482 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
483 return FILE_ERROR;
485 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
487 s = return_data_value(dfShort, &ne_header.resource_tab_offset);
488 size = return_data_value(dfShort, &ne_header.rname_tab_offset);
490 if( size > fsize ) return FILE_ERROR;
492 size -= s;
493 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
495 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
496 retval = FILE_DLL;
498 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
500 size = return_data_value(dfLong, (char*)&mz_header+2);
502 if( size != fsize ) return FILE_ERROR;
503 offset = 0;
504 retval = FILE_FNT;
506 else return FILE_ERROR;
508 *lpdata = (unsigned char*)malloc(size);
510 if( *lpdata )
512 lseek( fd, offset, SEEK_SET );
513 if( read(fd, *lpdata, size) != size )
514 { free( *lpdata ); *lpdata = NULL; }
516 return retval;
520 /* entry point */
522 int main(int argc, char **argv)
524 unsigned char* lpdata = NULL;
525 int fd;
527 parse_options( argc, argv);
529 if( (fd = open( g_lpstrInputFile, O_RDONLY)) )
531 int i;
532 struct stat file_stat;
534 fstat( fd, &file_stat);
535 i = get_resource_table( fd, &lpdata, file_stat.st_size );
537 switch(i)
539 case FILE_DLL:
540 if( lpdata )
542 int j, count = 0;
543 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
544 NE_NAMEINFO* pFontStorage = NULL;
546 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
548 j = return_data_value(dfShort, &pTInfo->count);
549 if( i == NE_RSCTYPE_FONT )
551 count = j;
552 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
555 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
557 if( pFontStorage && count )
559 unsigned short size_shift = return_data_value(dfShort, lpdata);
560 unsigned char* lpfont = NULL;
561 unsigned offset;
562 unsigned length;
564 for( j = 0; j < count; j++, pFontStorage++ )
566 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
567 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
569 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
571 fprintf(stderr, errorMemory );
572 exit(1);
575 lseek( fd, offset, SEEK_SET );
576 if( read(fd, lpfont, length) != length )
578 fprintf(stderr, errorDLLRead );
579 exit(1);
582 if( (i = parse_fnt_data( lpfont, length )) )
584 fprintf(stderr, "%s%d\n", errorFontData, i );
585 exit(1);
588 free(lpfont); free(lpdata);
589 exit(0);
591 else
593 fprintf(stderr, errorEmpty );
594 exit(1);
596 free( lpdata );
598 else
600 fprintf(stderr, errorDLLRead);
601 exit(1);
603 break;
605 case FILE_FNT:
606 if( lpdata )
608 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
610 fprintf(stderr, "%s%d\n", errorFontData, i );
611 exit(1);
613 free( lpdata );
615 else
617 fprintf(stderr, errorFNTRead);
618 exit(1);
620 break;
622 case FILE_ERROR:
623 fprintf(stderr, errorFile );
624 exit(1);
626 close(fd);
627 exit(0);
629 else
631 fprintf(stderr, errorOpenFile );
632 exit(1);