wined3d: Don't check for a "representative" in the MarkStateDirty() functions.
[wine/hacks.git] / tools / fnt2bdf.c
blobf9f6647354a39ef0736324aa7248cd7d2ea036b2
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 static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
51 static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
53 static char* g_lpstrFileName = NULL;
54 static char* g_lpstrCharSet = NULL;
55 static char* g_lpstrInputFile = NULL;
56 static int g_outputPoints = 0;
58 /* info */
60 static void usage(void)
62 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
63 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
64 printf(" -f basename\tbasic output filename\n");
65 printf(" -t \t\toutput files by point size instead of pixel height\n");
66 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
67 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
68 exit(-1);
71 /* convert little-endian value to the local format */
73 static int return_data_value(enum data_types dtype, void * pChr)
75 int ret_val = 0;
77 switch(dtype) {
78 case (dfChar):
79 ret_val = (int) *(unsigned char *)pChr;
80 break;
82 case(dfShort):
83 ret_val = *(unsigned char *)pChr;
84 ret_val += (*((unsigned char *)pChr + 1) << 8);
85 break;
87 case(dfLong): {
88 int i;
90 for(i=3; i >= 0; i--) {
91 ret_val += *((unsigned char *)pChr + i) << (8*i);
93 break;
95 case(dfString):
96 break;
98 return ret_val;
101 static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
103 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
104 char* lpChar;
106 if( !g_lpstrFileName )
108 if( !l_nameoffset ||
109 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
110 return ERROR_DATA;
111 lpChar = (char*)(file_buffer + l_nameoffset);
113 else lpChar = g_lpstrFileName;
115 strcpy( name, lpChar );
117 while( (lpChar = strchr( name, ' ')) )
118 *lpChar = '-';
120 /* construct a filename from the font typeface, slant, weight, and size */
122 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
123 else strcat(name, "_r" );
125 lpChar = name + strlen( name );
126 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
127 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
128 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
129 return 0;
132 /* parse FONT resource and write .bdf file */
134 static int parse_fnt_data(unsigned char* file_buffer, int length)
136 fnt_fontS cpe_font_struct;
137 int ic=0, t;
139 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
141 /* check font header */
143 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
144 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
146 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
147 if (t & 1)
149 fprintf(stderr, "Vector fonts not supported\n");
150 return ERROR_DATA;
153 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
154 if( t > length ) return ERROR_SIZE;
155 else
157 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
159 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
160 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
161 int l_len = l_lchar - l_fchar + 1;
162 int l_ptr = sizeof(fnt_hdrS);
164 /* some fields were introduced for Windows 3.x fonts */
165 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
166 l_ptr -= 30;
168 /* malloc size = (# chars) * sizeof(WinCharS) */
170 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
171 return ERROR_MEMORY;
173 /* NOW, convert them all to UNIX (lton) notation... */
175 for(ic=0; ic < l_len; ic++) {
176 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
177 l_ptr += 2; /* bump by sizeof(short) */
180 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
181 cpe_font_struct.dfCharTable[ic].charOffset =
182 return_data_value(dfShort, &file_buffer[l_ptr]);
183 l_ptr += 2; /* bump by sizeof(short) */
185 else { /* Windows Version 3.0 type font */
186 cpe_font_struct.dfCharTable[ic].charOffset =
187 return_data_value(dfLong, &file_buffer[l_ptr]);
188 l_ptr += 4; /* bump by sizeof(long) */
191 t = dump_bdf(&cpe_font_struct, file_buffer);
192 free( cpe_font_struct.dfCharTable );
194 return t;
197 static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
199 FILE* fp;
200 int ic;
201 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
202 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
204 int l_len = l_lchar-l_fchar + 1,
205 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
206 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
207 char l_filename[256];
209 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
210 return ic;
212 if((fp = fopen(l_filename, "w")) == NULL)
214 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
215 return ERROR_FILE;
218 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
219 if (ic) {
220 fclose(fp);
221 return (ic);
224 /* NOW, convert all chars to UNIX (lton) notation... */
226 for(ic=0; ic < l_len; ic++) {
227 int rowidx, l_span, /* how many char-cols wide is char? */
228 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
230 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
232 fprintf(fp, "STARTCHAR %d\n", ic);
233 fprintf(fp, "ENCODING %d\n", l_fchar);
234 fprintf(fp, "SWIDTH %d %d\n",
235 cpe_font_struct->dfCharTable[ic].charWidth*1000,
238 fprintf(fp, "DWIDTH %d %d\n",
239 cpe_font_struct->dfCharTable[ic].charWidth, 0);
241 fprintf(fp, "BBX %d %d %d %d\n",
242 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
243 l_ascent - l_hgt);
245 fprintf(fp, "BITMAP\n");
246 for(rowidx=0; rowidx < l_hgt; rowidx++) {
247 switch(l_span) {
248 case(0): /* 1-7 pixels wide font */
250 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
251 break;
254 case(1): /* 8-15 pixels wide font */
256 fprintf(fp, "%02X%02X",
257 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
258 fprintf(fp, "\n");
259 break;
262 case(2): /* 16-23 pixels wide font */
264 fprintf(fp, "%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 fprintf(fp, "\n");
269 break;
272 case(3): /* 24-31 pixels wide font */
274 fprintf(fp, "%02X%02X%02X%02X",
275 file_buffer[l_idx+rowidx],
276 file_buffer[l_idx+l_hgt+rowidx],
277 file_buffer[l_idx+(2*l_hgt)+rowidx],
278 file_buffer[l_idx+(3*l_hgt)+rowidx]);
279 fprintf(fp, "\n");
280 break;
282 case(4): /* 32-39 */
284 fprintf(fp, "%02X%02X%02X%02X%02X",
285 file_buffer[l_idx+rowidx],
286 file_buffer[l_idx+l_hgt+rowidx],
287 file_buffer[l_idx+(2*l_hgt)+rowidx],
288 file_buffer[l_idx+(3*l_hgt)+rowidx],
289 file_buffer[l_idx+(4*l_hgt)+rowidx]);
290 fprintf(fp, "\n");
291 break;
293 default:
294 fclose(fp);
295 unlink(l_filename);
296 return ERROR_DATA;
299 fprintf(fp, "ENDCHAR\n");
301 l_fchar++; /* Go to next one */
303 fprintf(fp, "ENDFONT\n");
304 fclose(fp);
305 return 0;
309 static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
311 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
312 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
313 int l_len = l_lchar - l_fchar + 1;
314 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
315 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
316 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
318 fprintf(fs, "STARTFONT 2.1\n");
320 /* Compose font name */
322 if( l_nameoffset &&
323 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
325 int dpi, point_size;
326 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
327 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
329 while((lpChar = strchr(lpFace, '-')) )
330 *lpChar = ' ';
332 fprintf(fs, "FONT -windows-%s-", lpFace );
334 if( tmWeight == 0 ) /* weight */
335 fputs("medium-", fs);
336 else if( tmWeight <= FW_LIGHT )
337 fputs("light-", fs);
338 else if( tmWeight <= FW_MEDIUM )
339 fputs("medium-", fs);
340 else if( tmWeight <= FW_DEMIBOLD )
341 fputs("demibold-", fs);
342 else if( tmWeight <= FW_BOLD )
343 fputs("bold-", fs);
344 else fputs("black-", fs);
346 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
347 fputs("i-", fs);
348 else fputs("r-", fs);
350 /* style */
352 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
353 fputs("normal-sans-", fs);
354 else fputs("normal--", fs); /* still can be -sans */
356 /* y extents */
358 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
359 dpi = (l_cellheight * 720) / point_size;
361 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
362 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
363 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
365 /* spacing */
367 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
368 else fputs("p-", fs);
370 /* average width */
372 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
374 /* charset */
376 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
377 else
378 switch( cpe_font_struct->hdr.fi.dfCharSet )
380 /* Microsoft just had to invent its own charsets! */
382 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
383 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
384 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
385 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
386 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
387 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
388 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
389 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
390 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
391 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
392 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
394 default:
395 case OEM_CHARSET:
396 fputs("Undefined charset, use -c option.\n", stderr);
397 return ERROR_DATA;
400 else return ERROR_DATA;
402 fprintf(fs, "SIZE %d %d %d\n",
403 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
404 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
405 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
407 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
408 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
409 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
410 0, l_ascent - l_cellheight );
412 fprintf(fs, "STARTPROPERTIES 4\n");
414 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
415 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
416 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
417 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
418 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
420 fprintf(fs, "ENDPROPERTIES\n");
422 fprintf(fs, "CHARS %d\n", l_len);
423 return 0;
428 static void parse_options(int argc, char **argv)
430 int i;
432 switch( argc )
434 case 2:
435 g_lpstrInputFile = argv[1];
436 break;
438 case 3:
439 case 4:
440 case 5:
441 case 6:
442 case 7:
443 case 8:
444 for( i = 1; i < argc - 1; i++ )
446 if( argv[i][0] != '-' ||
447 strlen(argv[i]) != 2 ) break;
449 if( argv[i][1] == 'c' )
450 g_lpstrCharSet = argv[i+1];
451 else
452 if( argv[i][1] == 'f' )
453 g_lpstrFileName = argv[i+1];
454 else
455 if( argv[i][1] == 't' )
457 g_outputPoints = 1;
458 continue;
460 else
461 usage();
463 i++;
465 if( i == argc - 1 )
467 g_lpstrInputFile = argv[i];
468 break;
470 default: usage();
475 /* read file data and return file type */
477 static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
479 IMAGE_DOS_HEADER mz_header;
480 IMAGE_OS2_HEADER ne_header;
481 long s, offset, size;
482 int retval;
484 lseek( fd, 0, SEEK_SET );
486 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
487 return FILE_ERROR;
489 s = return_data_value(dfShort, &mz_header.e_magic);
491 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
493 s = return_data_value(dfShort, &mz_header.e_lfanew);
494 lseek( fd, s, SEEK_SET );
496 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
497 return FILE_ERROR;
499 s = return_data_value(dfShort, &ne_header.ne_magic);
501 if( s == IMAGE_NT_SIGNATURE)
503 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
504 return FILE_ERROR;
506 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
508 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
509 size = return_data_value(dfShort, &ne_header.ne_restab);
511 if( size > fsize ) return FILE_ERROR;
513 size -= s;
514 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
516 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
517 retval = FILE_DLL;
519 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
521 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
523 if( size != fsize ) return FILE_ERROR;
524 offset = 0;
525 retval = FILE_FNT;
527 else return FILE_ERROR;
529 *lpdata = (unsigned char*)malloc(size);
531 if( *lpdata )
533 lseek( fd, offset, SEEK_SET );
534 if( read(fd, *lpdata, size) != size )
535 { free( *lpdata ); *lpdata = NULL; }
537 return retval;
541 /* entry point */
543 int main(int argc, char **argv)
545 unsigned char* lpdata = NULL;
546 int fd;
548 parse_options( argc, argv);
550 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
552 int i;
553 struct stat file_stat;
555 fstat( fd, &file_stat);
556 i = get_resource_table( fd, &lpdata, file_stat.st_size );
558 switch(i)
560 case FILE_DLL:
561 if( lpdata )
563 int j, count = 0;
564 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
565 NE_NAMEINFO* pFontStorage = NULL;
567 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
569 j = return_data_value(dfShort, &pTInfo->count);
570 if( i == NE_RSCTYPE_FONT )
572 count = j;
573 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
574 break; /* found one */
577 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
579 if( pFontStorage && count )
581 unsigned short size_shift = return_data_value(dfShort, lpdata);
582 unsigned char* lpfont = NULL;
583 unsigned offset;
584 int length;
586 for( j = 0; j < count; j++, pFontStorage++ )
588 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
589 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
591 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
593 fprintf(stderr, "Memory allocation error.\n" );
594 exit(1);
597 lseek( fd, offset, SEEK_SET );
598 if( read(fd, lpfont, length) != length )
600 fprintf(stderr, "Unable to read Windows DLL.\n" );
601 exit(1);
604 if( (i = parse_fnt_data( lpfont, length )) )
606 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
607 exit(1);
610 free(lpfont); free(lpdata);
611 exit(0);
613 else
615 fprintf(stderr, "No fonts found.\n" );
616 exit(1);
618 free( lpdata );
620 else
622 fprintf(stderr, "Unable to read Windows DLL.\n" );
623 exit(1);
625 break;
627 case FILE_FNT:
628 if( lpdata )
630 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
632 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
633 exit(1);
635 free( lpdata );
637 else
639 fprintf(stderr, "Unable to read .FNT file.\n" );
640 exit(1);
642 break;
644 case FILE_ERROR:
645 fprintf(stderr, "Corrupt or invalid file.\n" );
646 exit(1);
648 close(fd);
649 exit(0);
651 else
653 fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );
654 exit(1);