wined3d: Do not copy the saved states structure into the primary stateblock.
[wine.git] / tools / fnt2bdf.c
blob1a14d6cdf76d46a3e1ec444d2c86459ff9db4e16
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 #endif
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_IO_H
39 # include <io.h>
40 #endif
42 #include "fnt2bdf.h"
44 #define FILE_ERROR 0
45 #define FILE_DLL 1
46 #define FILE_FNT 2
48 /* global options */
50 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
51 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
53 char* g_lpstrFileName = NULL;
54 char* g_lpstrCharSet = NULL;
55 char* g_lpstrInputFile = NULL;
56 int g_outputPoints = 0;
58 static const char* errorDLLRead = "Unable to read Windows DLL.\n";
59 static const char* errorFNTRead = "Unable to read .FNT file.\n";
60 static const char* errorOpenFile = "Unable to open file.\n";
61 static const char* errorMemory = "Memory allocation error.\n";
62 static const char* errorFile = "Corrupt or invalid file.\n";
63 static const char* errorFontData = "Unable to parse font data: Error ";
64 static const char* errorEmpty = "No fonts found.\n";
66 /* info */
68 static void usage(void)
70 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
71 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
72 printf(" -f basename\tbasic output filename\n");
73 printf(" -t \t\toutput files by point size instead of pixel height\n");
74 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
75 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
76 exit(-1);
79 /* convert little-endian value to the local format */
81 static int return_data_value(enum data_types dtype, void * pChr)
83 int ret_val = 0;
85 switch(dtype) {
86 case (dfChar):
87 ret_val = (int) *(unsigned char *)pChr;
88 break;
90 case(dfShort):
91 ret_val = *(unsigned char *)pChr;
92 ret_val += (*((unsigned char *)pChr + 1) << 8);
93 break;
95 case(dfLong): {
96 int i;
98 for(i=3; i >= 0; i--) {
99 ret_val += *((unsigned char *)pChr + i) << (8*i);
101 break;
103 case(dfString):
104 break;
106 return ret_val;
109 static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
111 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
112 char* lpChar;
114 if( !g_lpstrFileName )
116 if( !l_nameoffset ||
117 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
118 return ERROR_DATA;
119 lpChar = (char*)(file_buffer + l_nameoffset);
121 else lpChar = g_lpstrFileName;
123 strcpy( name, lpChar );
125 while( (lpChar = strchr( name, ' ')) )
126 *lpChar = '-';
128 /* construct a filename from the font typeface, slant, weight, and size */
130 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
131 else strcat(name, "_r" );
133 lpChar = name + strlen( name );
134 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
135 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
136 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
137 return 0;
140 /* parse FONT resource and write .bdf file */
142 static int parse_fnt_data(unsigned char* file_buffer, int length)
144 fnt_fontS cpe_font_struct;
145 int ic=0, t;
147 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
149 /* check font header */
151 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
152 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
154 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
155 if (t & 1)
157 fprintf(stderr, "Vector fonts not supported\n");
158 return ERROR_DATA;
161 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
162 if( t > length ) return ERROR_SIZE;
163 else
165 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
167 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
168 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
169 int l_len = l_lchar - l_fchar + 1;
170 int l_ptr = sizeof(fnt_hdrS);
172 /* some fields were introduced for Windows 3.x fonts */
173 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
174 l_ptr -= 30;
176 /* malloc size = (# chars) * sizeof(WinCharS) */
178 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
179 return ERROR_MEMORY;
181 /* NOW, convert them all to UNIX (lton) notation... */
183 for(ic=0; ic < l_len; ic++) {
184 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
185 l_ptr += 2; /* bump by sizeof(short) */
188 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
189 cpe_font_struct.dfCharTable[ic].charOffset =
190 return_data_value(dfShort, &file_buffer[l_ptr]);
191 l_ptr += 2; /* bump by sizeof(short) */
193 else { /* Windows Version 3.0 type font */
194 cpe_font_struct.dfCharTable[ic].charOffset =
195 return_data_value(dfLong, &file_buffer[l_ptr]);
196 l_ptr += 4; /* bump by sizeof(long) */
199 t = dump_bdf(&cpe_font_struct, file_buffer);
200 free( cpe_font_struct.dfCharTable );
202 return t;
205 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
207 FILE* fp;
208 int ic;
209 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
210 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
212 int l_len = l_lchar-l_fchar + 1,
213 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
214 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
215 char l_filename[256];
217 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
218 return ic;
220 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
222 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
223 return ERROR_FILE;
226 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
227 if (ic) return (ic);
229 /* NOW, convert all chars to UNIX (lton) notation... */
231 for(ic=0; ic < l_len; ic++) {
232 int rowidx, l_span, /* how many char-cols wide is char? */
233 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
235 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
237 fprintf(fp, "STARTCHAR %d \n", ic);
238 fprintf(fp, "ENCODING %d\n", l_fchar);
239 fprintf(fp, "SWIDTH %d %d \n",
240 cpe_font_struct->dfCharTable[ic].charWidth*1000,
243 fprintf(fp, "DWIDTH %d %d \n",
244 cpe_font_struct->dfCharTable[ic].charWidth, 0);
246 fprintf(fp, "BBX %d %d %d %d\n",
247 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
248 l_ascent - l_hgt);
250 fprintf(fp, "BITMAP\n");
251 for(rowidx=0; rowidx < l_hgt; rowidx++) {
252 switch(l_span) {
253 case(0): /* 1-7 pixels wide font */
255 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
256 break;
259 case(1): /* 8-15 pixels wide font */
261 fprintf(fp, "%02X%02X",
262 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
263 fprintf(fp, "\n");
264 break;
267 case(2): /* 16-23 pixels wide font */
269 fprintf(fp, "%02X%02X%02X",
270 file_buffer[l_idx+rowidx],
271 file_buffer[l_idx+l_hgt+rowidx],
272 file_buffer[l_idx+(2*l_hgt)+rowidx]);
273 fprintf(fp, "\n");
274 break;
277 case(3): /* 24-31 pixels wide font */
279 fprintf(fp, "%02X%02X%02X%02X",
280 file_buffer[l_idx+rowidx],
281 file_buffer[l_idx+l_hgt+rowidx],
282 file_buffer[l_idx+(2*l_hgt)+rowidx],
283 file_buffer[l_idx+(3*l_hgt)+rowidx]);
284 fprintf(fp, "\n");
285 break;
287 case(4): /* 32-39 */
289 fprintf(fp, "%02X%02X%02X%02X%02X",
290 file_buffer[l_idx+rowidx],
291 file_buffer[l_idx+l_hgt+rowidx],
292 file_buffer[l_idx+(2*l_hgt)+rowidx],
293 file_buffer[l_idx+(3*l_hgt)+rowidx],
294 file_buffer[l_idx+(4*l_hgt)+rowidx]);
295 fprintf(fp, "\n");
296 break;
298 default:
299 fclose(fp);
300 unlink(l_filename);
301 return ERROR_DATA;
304 fprintf(fp, "ENDCHAR\n");
306 l_fchar++; /* Go to next one */
308 fprintf(fp, "ENDFONT\n");
309 fclose(fp);
310 return 0;
314 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
316 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
317 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
318 int l_len = l_lchar - l_fchar + 1;
319 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
320 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
321 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
323 fprintf(fs, "STARTFONT 2.1\n");
325 /* Compose font name */
327 if( l_nameoffset &&
328 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
330 int dpi, point_size;
331 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
332 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
334 while((lpChar = strchr(lpFace, '-')) )
335 *lpChar = ' ';
337 fprintf(fs, "FONT -windows-%s-", lpFace );
339 if( tmWeight == 0 ) /* weight */
340 fputs("medium-", fs);
341 else if( tmWeight <= FW_LIGHT )
342 fputs("light-", fs);
343 else if( tmWeight <= FW_MEDIUM )
344 fputs("medium-", fs);
345 else if( tmWeight <= FW_DEMIBOLD )
346 fputs("demibold-", fs);
347 else if( tmWeight <= FW_BOLD )
348 fputs("bold-", fs);
349 else fputs("black-", fs);
351 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
352 fputs("i-", fs);
353 else fputs("r-", fs);
355 /* style */
357 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
358 fputs("normal-sans-", fs);
359 else fputs("normal--", fs); /* still can be -sans */
361 /* y extents */
363 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
364 dpi = (l_cellheight * 720) / point_size;
366 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
367 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
368 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
370 /* spacing */
372 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
373 else fputs("p-", fs);
375 /* average width */
377 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
379 /* charset */
381 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
382 else
383 switch( cpe_font_struct->hdr.fi.dfCharSet )
385 /* Microsoft just had to invent its own charsets! */
387 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
388 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
389 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
390 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
391 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
392 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
393 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
394 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
395 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
396 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
397 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
399 default:
400 case OEM_CHARSET:
401 fputs("Undefined charset, use -c option.\n", stderr);
402 return ERROR_DATA;
405 else return ERROR_DATA;
407 fprintf(fs, "SIZE %d %d %d\n",
408 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
409 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
410 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
412 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
413 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
414 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
415 0, l_ascent - l_cellheight );
417 fprintf(fs, "STARTPROPERTIES 4\n");
419 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
420 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
421 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
422 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
423 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
425 fprintf(fs, "ENDPROPERTIES\n");
427 fprintf(fs, "CHARS %d\n", l_len);
428 return 0;
433 static void parse_options(int argc, char **argv)
435 int i;
437 switch( argc )
439 case 2:
440 g_lpstrInputFile = argv[1];
441 break;
443 case 3:
444 case 4:
445 case 5:
446 case 6:
447 case 7:
448 case 8:
449 for( i = 1; i < argc - 1; i++ )
451 if( argv[i][0] != '-' ||
452 strlen(argv[i]) != 2 ) break;
454 if( argv[i][1] == 'c' )
455 g_lpstrCharSet = argv[i+1];
456 else
457 if( argv[i][1] == 'f' )
458 g_lpstrFileName = argv[i+1];
459 else
460 if( argv[i][1] == 't' )
462 g_outputPoints = 1;
463 continue;
465 else
466 usage();
468 i++;
470 if( i == argc - 1 )
472 g_lpstrInputFile = argv[i];
473 break;
475 default: usage();
480 /* read file data and return file type */
482 static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
484 IMAGE_DOS_HEADER mz_header;
485 IMAGE_OS2_HEADER ne_header;
486 long s, offset, size;
487 int retval;
489 lseek( fd, 0, SEEK_SET );
491 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
492 return FILE_ERROR;
494 s = return_data_value(dfShort, &mz_header.e_magic);
496 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
498 s = return_data_value(dfShort, &mz_header.e_lfanew);
499 lseek( fd, s, SEEK_SET );
501 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
502 return FILE_ERROR;
504 s = return_data_value(dfShort, &ne_header.ne_magic);
506 if( s == IMAGE_NT_SIGNATURE)
508 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
509 return FILE_ERROR;
511 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
513 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
514 size = return_data_value(dfShort, &ne_header.ne_restab);
516 if( size > fsize ) return FILE_ERROR;
518 size -= s;
519 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
521 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
522 retval = FILE_DLL;
524 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
526 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
528 if( size != fsize ) return FILE_ERROR;
529 offset = 0;
530 retval = FILE_FNT;
532 else return FILE_ERROR;
534 *lpdata = (unsigned char*)malloc(size);
536 if( *lpdata )
538 lseek( fd, offset, SEEK_SET );
539 if( read(fd, *lpdata, size) != size )
540 { free( *lpdata ); *lpdata = NULL; }
542 return retval;
546 /* entry point */
548 int main(int argc, char **argv)
550 unsigned char* lpdata = NULL;
551 int fd;
553 parse_options( argc, argv);
555 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) )
557 int i;
558 struct stat file_stat;
560 fstat( fd, &file_stat);
561 i = get_resource_table( fd, &lpdata, file_stat.st_size );
563 switch(i)
565 case FILE_DLL:
566 if( lpdata )
568 int j, count = 0;
569 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
570 NE_NAMEINFO* pFontStorage = NULL;
572 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
574 j = return_data_value(dfShort, &pTInfo->count);
575 if( i == NE_RSCTYPE_FONT )
577 count = j;
578 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
579 break; /* found one */
582 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
584 if( pFontStorage && count )
586 unsigned short size_shift = return_data_value(dfShort, lpdata);
587 unsigned char* lpfont = NULL;
588 unsigned offset;
589 int length;
591 for( j = 0; j < count; j++, pFontStorage++ )
593 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
594 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
596 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
598 fprintf(stderr, errorMemory );
599 exit(1);
602 lseek( fd, offset, SEEK_SET );
603 if( read(fd, lpfont, length) != length )
605 fprintf(stderr, errorDLLRead );
606 exit(1);
609 if( (i = parse_fnt_data( lpfont, length )) )
611 fprintf(stderr, "%s%d\n", errorFontData, i );
612 exit(1);
615 free(lpfont); free(lpdata);
616 exit(0);
618 else
620 fprintf(stderr, errorEmpty );
621 exit(1);
623 free( lpdata );
625 else
627 fprintf(stderr, errorDLLRead);
628 exit(1);
630 break;
632 case FILE_FNT:
633 if( lpdata )
635 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
637 fprintf(stderr, "%s%d\n", errorFontData, i );
638 exit(1);
640 free( lpdata );
642 else
644 fprintf(stderr, errorFNTRead);
645 exit(1);
647 break;
649 case FILE_ERROR:
650 fprintf(stderr, errorFile );
651 exit(1);
653 close(fd);
654 exit(0);
656 else
658 fprintf(stderr, errorOpenFile );
659 exit(1);