Update nItemWidth in LISTVIEW_SetItem() if LVS_LIST or LVS_SMALLICON
[wine/multimedia.git] / tools / fnt2bdf.c
blob13811d8f96b9f697ef1250c1163cd5e33315d2a7
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 "windef.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "fnt2bdf.h"
26 #include "neexe.h"
27 #include "module.h"
29 #define MAP_BEG 118
31 extern char* g_lpstrFileName;
32 extern char* g_lpstrCharSet;
34 #define FILE_ERROR 0
35 #define FILE_DLL 1
36 #define FILE_FNT 2
38 /* global options */
40 char* g_lpstrFileName = NULL;
41 char* g_lpstrCharSet = NULL;
42 char* g_lpstrInputFile = NULL;
43 int g_outputPoints = 0;
45 static char* errorDLLRead = "Unable to read Windows DLL.\n";
46 static char* errorFNTRead = "Unable to read .FNT file.\n";
47 static char* errorOpenFile = "Unable to open file.\n";
48 static char* errorMemory = "Memory allocation error.\n";
49 static char* errorFile = "Corrupt or invalid file.\n";
50 static char* errorFontData = "Unable to parse font data: Error ";
51 static char* errorEmpty = "No fonts found.\n";
53 /* info */
55 void usage(void)
57 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
58 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
59 printf(" -f basename\tbasic output filename\n");
60 printf(" -t \t\toutput files by point size instead of pixel height\n");
61 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
62 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
63 exit(-1);
66 /* convert big-endian value to the local format */
68 int return_data_value(enum data_types dtype, void * pChr)
70 int ret_val = 0;
72 switch(dtype) {
73 case (dfChar):
74 ret_val = (int) *(unsigned char *)pChr;
75 break;
77 case(dfShort):
78 ret_val = *(unsigned char *)pChr;
79 ret_val += (*((unsigned char *)pChr + 1) << 8);
80 break;
82 case(dfLong): {
83 int i;
85 for(i=3; i >= 0; i--) {
86 ret_val += *((unsigned char *)pChr + i) << (8*i);
88 break;
90 case(dfString):
91 break;
93 return ret_val;
96 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
98 int l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
99 char* lpChar;
101 if( !g_lpstrFileName )
103 if( !l_nameoffset ||
104 l_nameoffset > return_data_value(dfLong, cpe_font_struct->hdr.dfSize) + 1 )
105 return ERROR_DATA;
106 lpChar = (char*)(file_buffer + l_nameoffset);
108 else lpChar = g_lpstrFileName;
110 strcpy( name, lpChar );
112 while( (lpChar = strchr( name, ' ')) )
113 *lpChar = '-';
115 /* construct a filename from the font typeface, slant, weight, and size */
117 if( cpe_font_struct->hdr.dfItalic[0] ) strcat(name, "_i" );
118 else strcat(name, "_r" );
120 lpChar = name + strlen( name );
121 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, cpe_font_struct->hdr.dfWeight),
122 (g_outputPoints) ? return_data_value(dfShort, cpe_font_struct->hdr.dfPoints)
123 : return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight) );
124 return 0;
127 /* parse FONT resource and write .bdf file */
129 int parse_fnt_data(unsigned char* file_buffer, int length)
131 fnt_fontS cpe_font_struct;
132 int ic=0, t;
134 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
136 /* check font header */
138 t = return_data_value(dfShort, cpe_font_struct.hdr.dfVersion);
139 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
141 t = return_data_value(dfLong, cpe_font_struct.hdr.dfSize);
142 if( t > length ) return ERROR_SIZE;
143 else
145 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
147 int l_fchar = return_data_value(dfChar, cpe_font_struct.hdr.dfFirstChar),
148 l_lchar = return_data_value(dfChar, cpe_font_struct.hdr.dfLastChar);
149 int l_len = l_lchar - l_fchar + 1, l_ptr = MAP_BEG;
151 /* malloc size = (# chars) * sizeof(WinCharS) */
153 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
154 return ERROR_MEMORY;
156 /* NOW, convert them all to UNIX (lton) notation... */
158 for(ic=0; ic < l_len; ic++) {
159 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
160 l_ptr += 2; /* bump by sizeof(short) */
163 if( return_data_value(dfShort, cpe_font_struct.hdr.dfVersion) == 0x200) {
164 cpe_font_struct.dfCharTable[ic].charOffset =
165 return_data_value(dfShort, &file_buffer[l_ptr]);
166 l_ptr += 2; /* bump by sizeof(long) */
168 else { /* Windows Version 3.0 type font */
169 cpe_font_struct.dfCharTable[ic].charOffset =
170 return_data_value(dfLong, &file_buffer[l_ptr]);
171 l_ptr += 4; /* bump by sizeof(long) */
174 t = dump_bdf(&cpe_font_struct, file_buffer);
175 free( cpe_font_struct.dfCharTable );
177 return t;
180 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
182 FILE* fp;
183 int ic;
184 int l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar),
185 l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
187 int l_len = l_lchar-l_fchar + 1,
188 l_hgt = return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight);
189 int l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
190 char l_filename[256];
192 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
193 return ic;
195 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
197 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
198 return ERROR_FILE;
201 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
202 if (ic) return (ic);
204 /* NOW, convert all chars to UNIX (lton) notation... */
206 for(ic=0; ic < l_len; ic++) {
207 int rowidx, l_span, /* how many char-cols wide is char? */
208 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
210 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
212 fprintf(fp, "STARTCHAR %d \n", ic);
213 fprintf(fp, "ENCODING %d\n", l_fchar);
214 fprintf(fp, "SWIDTH %d %d \n",
215 cpe_font_struct->dfCharTable[ic].charWidth*1000,
218 fprintf(fp, "DWIDTH %d %d \n",
219 cpe_font_struct->dfCharTable[ic].charWidth, 0);
221 fprintf(fp, "BBX %d %d %d %d\n",
222 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
223 l_ascent - l_hgt);
225 fprintf(fp, "BITMAP\n");
226 for(rowidx=0; rowidx < l_hgt; rowidx++) {
227 switch(l_span) {
228 case(0): /* 1-7 pixels wide font */
230 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
231 break;
234 case(1): /* 8-15 pixels wide font */
236 fprintf(fp, "%02X%02X",
237 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
238 fprintf(fp, "\n");
239 break;
242 case(2): /* 16-23 pixels wide font */
244 fprintf(fp, "%02X%02X%02X",
245 file_buffer[l_idx+rowidx],
246 file_buffer[l_idx+l_hgt+rowidx],
247 file_buffer[l_idx+(2*l_hgt)+rowidx]);
248 fprintf(fp, "\n");
249 break;
252 case(3): /* 24-31 pixels wide font */
254 fprintf(fp, "%02X%02X%02X%02X",
255 file_buffer[l_idx+rowidx],
256 file_buffer[l_idx+l_hgt+rowidx],
257 file_buffer[l_idx+(2*l_hgt)+rowidx],
258 file_buffer[l_idx+(3*l_hgt)+rowidx]);
259 fprintf(fp, "\n");
260 break;
262 case(4): /* 32-39 */
264 fprintf(fp, "%02X%02X%02X%02X%02X",
265 file_buffer[l_idx+rowidx],
266 file_buffer[l_idx+l_hgt+rowidx],
267 file_buffer[l_idx+(2*l_hgt)+rowidx],
268 file_buffer[l_idx+(3*l_hgt)+rowidx],
269 file_buffer[l_idx+(4*l_hgt)+rowidx]);
270 fprintf(fp, "\n");
271 break;
273 default:
274 fclose(fp);
275 unlink(l_filename);
276 return ERROR_DATA;
279 fprintf(fp, "ENDCHAR\n");
281 l_fchar++; /* Go to next one */
283 fprintf(fp, "ENDFONT\n");
284 fclose(fp);
285 return 0;
289 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
291 int l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar),
292 l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
293 int l_len = l_lchar - l_fchar + 1;
294 int l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
295 int l_cellheight = return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight);
296 int l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
298 fprintf(fs, "STARTFONT 2.1\n");
300 /* Compose font name */
302 if( l_nameoffset &&
303 l_nameoffset < return_data_value(dfLong, cpe_font_struct->hdr.dfSize) )
305 int dpi, point_size;
306 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
307 short tmWeight = return_data_value(dfShort, cpe_font_struct->hdr.dfWeight);
309 while((lpChar = strchr(lpFace, '-')) )
310 *lpChar = ' ';
312 fprintf(fs, "FONT -windows-%s-", lpFace );
314 if( tmWeight == 0 ) /* weight */
315 fputs("medium-", fs);
316 else if( tmWeight <= FW_LIGHT )
317 fputs("light-", fs);
318 else if( tmWeight <= FW_MEDIUM )
319 fputs("medium-", fs);
320 else if( tmWeight <= FW_DEMIBOLD )
321 fputs("demibold-", fs);
322 else if( tmWeight <= FW_BOLD )
323 fputs("bold-", fs);
324 else fputs("black-", fs);
326 if( cpe_font_struct->hdr.dfItalic[0] ) /* slant */
327 fputs("i-", fs);
328 else fputs("r-", fs);
330 /* style */
332 if( (cpe_font_struct->hdr.dfPitchAndFamily[0] & 0xF0) == FF_SWISS )
333 fputs("normal-sans-", fs);
334 else fputs("normal--", fs); /* still can be -sans */
336 /* y extents */
338 point_size = 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfPoints );
339 dpi = (l_cellheight * 720) / point_size;
341 fprintf(fs, "%d-%d-%d-%d-", l_cellheight, 10*l_cellheight, 72, 72);
342 /* point_size, dpi, dpi); */
344 /* spacing */
346 if( return_data_value(dfShort, cpe_font_struct->hdr.dfPixWidth) ) fputs("c-", fs);
347 else fputs("p-", fs);
349 /* average width */
351 fprintf( fs, "%d-", 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfAvgWidth) );
353 /* charset */
355 switch( cpe_font_struct->hdr.dfCharSet[0] )
357 /* Microsoft just had to invent its own charsets! */
359 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
360 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
361 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
362 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
363 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
364 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
365 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
366 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
367 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
368 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
369 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
371 default:
372 case OEM_CHARSET:
373 if( !g_lpstrCharSet )
375 fputs("Undefined charset, use -c option.\n", stderr);
376 return ERROR_DATA;
378 fprintf(fs, "%s\n", g_lpstrCharSet);
381 else return ERROR_DATA;
383 fprintf(fs, "SIZE %d %d %d\n",
384 l_cellheight,
385 return_data_value(dfShort, cpe_font_struct->hdr.dfHorizRes),
386 return_data_value(dfShort, cpe_font_struct->hdr.dfVertRes)); /* dfVertRes[2] */
388 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
389 return_data_value(dfShort, cpe_font_struct->hdr.dfMaxWidth),
390 return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight),
391 0, l_ascent - l_cellheight );
393 fprintf(fs, "STARTPROPERTIES 4\n");
395 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
396 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
397 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
398 return_data_value(dfShort, cpe_font_struct->hdr.dfInternalLeading));
399 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, cpe_font_struct->hdr.dfDefaultChar));
401 fprintf(fs, "ENDPROPERTIES\n");
403 fprintf(fs, "CHARS %d\n", l_len);
404 return 0;
409 void parse_options(int argc, char **argv)
411 int i;
413 switch( argc )
415 case 2:
416 g_lpstrInputFile = argv[1];
417 break;
419 case 3:
420 case 4:
421 case 5:
422 case 6:
423 case 7:
424 case 8:
425 for( i = 1; i < argc - 1; i++ )
427 if( argv[i][0] != '-' ||
428 strlen(argv[i]) != 2 ) break;
430 if( argv[i][1] == 'c' )
431 g_lpstrCharSet = argv[i+1];
432 else
433 if( argv[i][1] == 'f' )
434 g_lpstrFileName = argv[i+1];
435 else
436 if( argv[i][1] == 't' )
438 g_outputPoints = 1;
439 continue;
441 else
442 usage();
444 i++;
446 if( i == argc - 1 )
448 g_lpstrInputFile = argv[i];
449 break;
451 default: usage();
456 /* read file data and return file type */
458 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
460 IMAGE_DOS_HEADER mz_header;
461 IMAGE_OS2_HEADER ne_header;
462 short s, offset, size, retval;
465 lseek( fd, 0, SEEK_SET );
467 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
468 return FILE_ERROR;
470 s = return_data_value(dfShort, &mz_header.e_magic);
472 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
474 s = return_data_value(dfShort, &mz_header.e_lfanew);
475 lseek( fd, s, SEEK_SET );
477 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
478 return FILE_ERROR;
480 s = return_data_value(dfShort, &ne_header.ne_magic);
482 if( s == IMAGE_NT_SIGNATURE)
484 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
485 return FILE_ERROR;
487 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
489 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
490 size = return_data_value(dfShort, &ne_header.ne_restab);
492 if( size > fsize ) return FILE_ERROR;
494 size -= s;
495 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
497 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
498 retval = FILE_DLL;
500 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
502 size = return_data_value(dfLong, (char*)&mz_header+2);
504 if( size != fsize ) return FILE_ERROR;
505 offset = 0;
506 retval = FILE_FNT;
508 else return FILE_ERROR;
510 *lpdata = (unsigned char*)malloc(size);
512 if( *lpdata )
514 lseek( fd, offset, SEEK_SET );
515 if( read(fd, *lpdata, size) != size )
516 { free( *lpdata ); *lpdata = NULL; }
518 return retval;
522 /* entry point */
524 int main(int argc, char **argv)
526 unsigned char* lpdata = NULL;
527 int fd;
529 parse_options( argc, argv);
531 if( (fd = open( g_lpstrInputFile, O_RDONLY)) )
533 int i;
534 struct stat file_stat;
536 fstat( fd, &file_stat);
537 i = get_resource_table( fd, &lpdata, file_stat.st_size );
539 switch(i)
541 case FILE_DLL:
542 if( lpdata )
544 int j, count = 0;
545 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
546 NE_NAMEINFO* pFontStorage = NULL;
548 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
550 j = return_data_value(dfShort, &pTInfo->count);
551 if( i == NE_RSCTYPE_FONT )
553 count = j;
554 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
557 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
559 if( pFontStorage && count )
561 unsigned short size_shift = return_data_value(dfShort, lpdata);
562 unsigned char* lpfont = NULL;
563 unsigned offset;
564 unsigned length;
566 for( j = 0; j < count; j++, pFontStorage++ )
568 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
569 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
571 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
573 fprintf(stderr, errorMemory );
574 exit(1);
577 lseek( fd, offset, SEEK_SET );
578 if( read(fd, lpfont, length) != length )
580 fprintf(stderr, errorDLLRead );
581 exit(1);
584 if( (i = parse_fnt_data( lpfont, length )) )
586 fprintf(stderr, "%s%d\n", errorFontData, i );
587 exit(1);
590 free(lpfont); free(lpdata);
591 exit(0);
593 else
595 fprintf(stderr, errorEmpty );
596 exit(1);
598 free( lpdata );
600 else
602 fprintf(stderr, errorDLLRead);
603 exit(1);
605 break;
607 case FILE_FNT:
608 if( lpdata )
610 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
612 fprintf(stderr, "%s%d\n", errorFontData, i );
613 exit(1);
615 free( lpdata );
617 else
619 fprintf(stderr, errorFNTRead);
620 exit(1);
622 break;
624 case FILE_ERROR:
625 fprintf(stderr, errorFile );
626 exit(1);
628 close(fd);
629 exit(0);
631 else
633 fprintf(stderr, errorOpenFile );
634 exit(1);