Switch IShellLink to use shlwapi.SHCreateStreamOnFileW.
[wine/multimedia.git] / tools / fnt2bdf.c
blobe03f556d50cce9cf41e1e843b36cde6fd586d5f1
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 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #ifdef HAVE_SYS_PARAM_H
26 # include <sys/param.h>
27 #endif
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <fcntl.h>
37 #ifdef HAVE_IO_H
38 # include <io.h>
39 #endif
41 #ifndef O_BINARY
42 # define O_BINARY 0
43 #endif
45 #include "fnt2bdf.h"
47 #define FILE_ERROR 0
48 #define FILE_DLL 1
49 #define FILE_FNT 2
51 /* global options */
53 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
54 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
56 char* g_lpstrFileName = NULL;
57 char* g_lpstrCharSet = NULL;
58 char* g_lpstrInputFile = NULL;
59 int g_outputPoints = 0;
61 static const char* errorDLLRead = "Unable to read Windows DLL.\n";
62 static const char* errorFNTRead = "Unable to read .FNT file.\n";
63 static const char* errorOpenFile = "Unable to open file.\n";
64 static const char* errorMemory = "Memory allocation error.\n";
65 static const char* errorFile = "Corrupt or invalid file.\n";
66 static const char* errorFontData = "Unable to parse font data: Error ";
67 static const char* errorEmpty = "No fonts found.\n";
69 /* info */
71 static void usage(void)
73 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
74 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
75 printf(" -f basename\tbasic output filename\n");
76 printf(" -t \t\toutput files by point size instead of pixel height\n");
77 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
78 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
79 exit(-1);
82 /* convert little-endian value to the local format */
84 static int return_data_value(enum data_types dtype, void * pChr)
86 int ret_val = 0;
88 switch(dtype) {
89 case (dfChar):
90 ret_val = (int) *(unsigned char *)pChr;
91 break;
93 case(dfShort):
94 ret_val = *(unsigned char *)pChr;
95 ret_val += (*((unsigned char *)pChr + 1) << 8);
96 break;
98 case(dfLong): {
99 int i;
101 for(i=3; i >= 0; i--) {
102 ret_val += *((unsigned char *)pChr + i) << (8*i);
104 break;
106 case(dfString):
107 break;
109 return ret_val;
112 static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
114 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
115 char* lpChar;
117 if( !g_lpstrFileName )
119 if( !l_nameoffset ||
120 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
121 return ERROR_DATA;
122 lpChar = (char*)(file_buffer + l_nameoffset);
124 else lpChar = g_lpstrFileName;
126 strcpy( name, lpChar );
128 while( (lpChar = strchr( name, ' ')) )
129 *lpChar = '-';
131 /* construct a filename from the font typeface, slant, weight, and size */
133 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
134 else strcat(name, "_r" );
136 lpChar = name + strlen( name );
137 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
138 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
139 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
140 return 0;
143 /* parse FONT resource and write .bdf file */
145 static int parse_fnt_data(unsigned char* file_buffer, int length)
147 fnt_fontS cpe_font_struct;
148 int ic=0, t;
150 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
152 /* check font header */
154 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
155 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
157 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
158 if (t & 1)
160 fprintf(stderr, "Vector fonts not supported\n");
161 return ERROR_DATA;
164 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
165 if( t > length ) return ERROR_SIZE;
166 else
168 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
170 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
171 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
172 int l_len = l_lchar - l_fchar + 1;
173 int l_ptr = sizeof(fnt_hdrS);
175 /* some fields were introduced for Windows 3.x fonts */
176 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
177 l_ptr -= 30;
179 /* malloc size = (# chars) * sizeof(WinCharS) */
181 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
182 return ERROR_MEMORY;
184 /* NOW, convert them all to UNIX (lton) notation... */
186 for(ic=0; ic < l_len; ic++) {
187 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
188 l_ptr += 2; /* bump by sizeof(short) */
191 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
192 cpe_font_struct.dfCharTable[ic].charOffset =
193 return_data_value(dfShort, &file_buffer[l_ptr]);
194 l_ptr += 2; /* bump by sizeof(short) */
196 else { /* Windows Version 3.0 type font */
197 cpe_font_struct.dfCharTable[ic].charOffset =
198 return_data_value(dfLong, &file_buffer[l_ptr]);
199 l_ptr += 4; /* bump by sizeof(long) */
202 t = dump_bdf(&cpe_font_struct, file_buffer);
203 free( cpe_font_struct.dfCharTable );
205 return t;
208 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
210 FILE* fp;
211 int ic;
212 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
213 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
215 int l_len = l_lchar-l_fchar + 1,
216 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
217 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
218 char l_filename[256];
220 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
221 return ic;
223 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
225 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
226 return ERROR_FILE;
229 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
230 if (ic) return (ic);
232 /* NOW, convert all chars to UNIX (lton) notation... */
234 for(ic=0; ic < l_len; ic++) {
235 int rowidx, l_span, /* how many char-cols wide is char? */
236 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
238 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
240 fprintf(fp, "STARTCHAR %d \n", ic);
241 fprintf(fp, "ENCODING %d\n", l_fchar);
242 fprintf(fp, "SWIDTH %d %d \n",
243 cpe_font_struct->dfCharTable[ic].charWidth*1000,
246 fprintf(fp, "DWIDTH %d %d \n",
247 cpe_font_struct->dfCharTable[ic].charWidth, 0);
249 fprintf(fp, "BBX %d %d %d %d\n",
250 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
251 l_ascent - l_hgt);
253 fprintf(fp, "BITMAP\n");
254 for(rowidx=0; rowidx < l_hgt; rowidx++) {
255 switch(l_span) {
256 case(0): /* 1-7 pixels wide font */
258 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
259 break;
262 case(1): /* 8-15 pixels wide font */
264 fprintf(fp, "%02X%02X",
265 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
266 fprintf(fp, "\n");
267 break;
270 case(2): /* 16-23 pixels wide font */
272 fprintf(fp, "%02X%02X%02X",
273 file_buffer[l_idx+rowidx],
274 file_buffer[l_idx+l_hgt+rowidx],
275 file_buffer[l_idx+(2*l_hgt)+rowidx]);
276 fprintf(fp, "\n");
277 break;
280 case(3): /* 24-31 pixels wide font */
282 fprintf(fp, "%02X%02X%02X%02X",
283 file_buffer[l_idx+rowidx],
284 file_buffer[l_idx+l_hgt+rowidx],
285 file_buffer[l_idx+(2*l_hgt)+rowidx],
286 file_buffer[l_idx+(3*l_hgt)+rowidx]);
287 fprintf(fp, "\n");
288 break;
290 case(4): /* 32-39 */
292 fprintf(fp, "%02X%02X%02X%02X%02X",
293 file_buffer[l_idx+rowidx],
294 file_buffer[l_idx+l_hgt+rowidx],
295 file_buffer[l_idx+(2*l_hgt)+rowidx],
296 file_buffer[l_idx+(3*l_hgt)+rowidx],
297 file_buffer[l_idx+(4*l_hgt)+rowidx]);
298 fprintf(fp, "\n");
299 break;
301 default:
302 fclose(fp);
303 unlink(l_filename);
304 return ERROR_DATA;
307 fprintf(fp, "ENDCHAR\n");
309 l_fchar++; /* Go to next one */
311 fprintf(fp, "ENDFONT\n");
312 fclose(fp);
313 return 0;
317 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
319 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
320 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
321 int l_len = l_lchar - l_fchar + 1;
322 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
323 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
324 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
326 fprintf(fs, "STARTFONT 2.1\n");
328 /* Compose font name */
330 if( l_nameoffset &&
331 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
333 int dpi, point_size;
334 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
335 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
337 while((lpChar = strchr(lpFace, '-')) )
338 *lpChar = ' ';
340 fprintf(fs, "FONT -windows-%s-", lpFace );
342 if( tmWeight == 0 ) /* weight */
343 fputs("medium-", fs);
344 else if( tmWeight <= FW_LIGHT )
345 fputs("light-", fs);
346 else if( tmWeight <= FW_MEDIUM )
347 fputs("medium-", fs);
348 else if( tmWeight <= FW_DEMIBOLD )
349 fputs("demibold-", fs);
350 else if( tmWeight <= FW_BOLD )
351 fputs("bold-", fs);
352 else fputs("black-", fs);
354 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
355 fputs("i-", fs);
356 else fputs("r-", fs);
358 /* style */
360 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
361 fputs("normal-sans-", fs);
362 else fputs("normal--", fs); /* still can be -sans */
364 /* y extents */
366 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
367 dpi = (l_cellheight * 720) / point_size;
369 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
370 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
371 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
373 /* spacing */
375 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
376 else fputs("p-", fs);
378 /* average width */
380 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
382 /* charset */
384 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
385 else
386 switch( cpe_font_struct->hdr.fi.dfCharSet )
388 /* Microsoft just had to invent its own charsets! */
390 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
391 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
392 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
393 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
394 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
395 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
396 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
397 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
398 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
399 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
400 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
402 default:
403 case OEM_CHARSET:
404 fputs("Undefined charset, use -c option.\n", stderr);
405 return ERROR_DATA;
408 else return ERROR_DATA;
410 fprintf(fs, "SIZE %d %d %d\n",
411 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
412 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
413 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
415 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
416 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
417 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
418 0, l_ascent - l_cellheight );
420 fprintf(fs, "STARTPROPERTIES 4\n");
422 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
423 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
424 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
425 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
426 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
428 fprintf(fs, "ENDPROPERTIES\n");
430 fprintf(fs, "CHARS %d\n", l_len);
431 return 0;
436 static void parse_options(int argc, char **argv)
438 int i;
440 switch( argc )
442 case 2:
443 g_lpstrInputFile = argv[1];
444 break;
446 case 3:
447 case 4:
448 case 5:
449 case 6:
450 case 7:
451 case 8:
452 for( i = 1; i < argc - 1; i++ )
454 if( argv[i][0] != '-' ||
455 strlen(argv[i]) != 2 ) break;
457 if( argv[i][1] == 'c' )
458 g_lpstrCharSet = argv[i+1];
459 else
460 if( argv[i][1] == 'f' )
461 g_lpstrFileName = argv[i+1];
462 else
463 if( argv[i][1] == 't' )
465 g_outputPoints = 1;
466 continue;
468 else
469 usage();
471 i++;
473 if( i == argc - 1 )
475 g_lpstrInputFile = argv[i];
476 break;
478 default: usage();
483 /* read file data and return file type */
485 static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
487 IMAGE_DOS_HEADER mz_header;
488 IMAGE_OS2_HEADER ne_header;
489 long s, offset, size;
490 int retval;
492 lseek( fd, 0, SEEK_SET );
494 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
495 return FILE_ERROR;
497 s = return_data_value(dfShort, &mz_header.e_magic);
499 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
501 s = return_data_value(dfShort, &mz_header.e_lfanew);
502 lseek( fd, s, SEEK_SET );
504 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
505 return FILE_ERROR;
507 s = return_data_value(dfShort, &ne_header.ne_magic);
509 if( s == IMAGE_NT_SIGNATURE)
511 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
512 return FILE_ERROR;
514 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
516 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
517 size = return_data_value(dfShort, &ne_header.ne_restab);
519 if( size > fsize ) return FILE_ERROR;
521 size -= s;
522 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
524 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
525 retval = FILE_DLL;
527 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
529 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
531 if( size != fsize ) return FILE_ERROR;
532 offset = 0;
533 retval = FILE_FNT;
535 else return FILE_ERROR;
537 *lpdata = (unsigned char*)malloc(size);
539 if( *lpdata )
541 lseek( fd, offset, SEEK_SET );
542 if( read(fd, *lpdata, size) != size )
543 { free( *lpdata ); *lpdata = NULL; }
545 return retval;
549 /* entry point */
551 int main(int argc, char **argv)
553 unsigned char* lpdata = NULL;
554 int fd;
556 parse_options( argc, argv);
558 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) )
560 int i;
561 struct stat file_stat;
563 fstat( fd, &file_stat);
564 i = get_resource_table( fd, &lpdata, file_stat.st_size );
566 switch(i)
568 case FILE_DLL:
569 if( lpdata )
571 int j, count = 0;
572 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
573 NE_NAMEINFO* pFontStorage = NULL;
575 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
577 j = return_data_value(dfShort, &pTInfo->count);
578 if( i == NE_RSCTYPE_FONT )
580 count = j;
581 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
582 break; /* found one */
585 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
587 if( pFontStorage && count )
589 unsigned short size_shift = return_data_value(dfShort, lpdata);
590 unsigned char* lpfont = NULL;
591 unsigned offset;
592 int length;
594 for( j = 0; j < count; j++, pFontStorage++ )
596 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
597 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
599 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
601 fprintf(stderr, errorMemory );
602 exit(1);
605 lseek( fd, offset, SEEK_SET );
606 if( read(fd, lpfont, length) != length )
608 fprintf(stderr, errorDLLRead );
609 exit(1);
612 if( (i = parse_fnt_data( lpfont, length )) )
614 fprintf(stderr, "%s%d\n", errorFontData, i );
615 exit(1);
618 free(lpfont); free(lpdata);
619 exit(0);
621 else
623 fprintf(stderr, errorEmpty );
624 exit(1);
626 free( lpdata );
628 else
630 fprintf(stderr, errorDLLRead);
631 exit(1);
633 break;
635 case FILE_FNT:
636 if( lpdata )
638 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
640 fprintf(stderr, "%s%d\n", errorFontData, i );
641 exit(1);
643 free( lpdata );
645 else
647 fprintf(stderr, errorFNTRead);
648 exit(1);
650 break;
652 case FILE_ERROR:
653 fprintf(stderr, errorFile );
654 exit(1);
656 close(fd);
657 exit(0);
659 else
661 fprintf(stderr, errorOpenFile );
662 exit(1);