blob: 5534ff9647d4784a0d61bbc291a2f9c767ab24b5 [file] [log] [blame]
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +00001/************************************************
2 *
3 * Extract fonts from .fnt or Windows DLL files
4 * and convert them to the .bdf format.
5 *
6 * Copyright 1994-1996 Kevin Carothers and Alex Korobka
7 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00008 * 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.
12 *
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.
17 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000021 */
22
Patrik Stridvall96336321999-10-24 22:13:47 +000023#include "config.h"
Alexandre Julliard49b7fdc2005-08-03 21:25:10 +000024#include "wine/port.h"
Patrik Stridvall96336321999-10-24 22:13:47 +000025
26#ifdef HAVE_SYS_PARAM_H
27# include <sys/param.h>
28#endif
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000029#include <sys/types.h>
30#include <sys/stat.h>
Dmitry Timoshkovfef71862000-07-25 12:25:40 +000031#include <stdio.h>
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000032#include <stdlib.h>
33#include <string.h>
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +000034#ifdef HAVE_UNISTD_H
35# include <unistd.h>
36#endif
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000037#include <fcntl.h>
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +000038#ifdef HAVE_IO_H
39# include <io.h>
40#endif
41
Alexandre Julliard1f220db2009-08-29 21:06:19 +020042#include "windef.h"
43#include "wingdi.h"
44
45enum data_types {dfChar, dfShort, dfLong, dfString};
46
47#define ERROR_DATA 1
48#define ERROR_VERSION 2
49#define ERROR_SIZE 3
50#define ERROR_MEMORY 4
51#define ERROR_FILE 5
52
53#include "pshpack1.h"
54
55typedef struct
56{
57 INT16 dfType;
58 INT16 dfPoints;
59 INT16 dfVertRes;
60 INT16 dfHorizRes;
61 INT16 dfAscent;
62 INT16 dfInternalLeading;
63 INT16 dfExternalLeading;
64 BYTE dfItalic;
65 BYTE dfUnderline;
66 BYTE dfStrikeOut;
67 INT16 dfWeight;
68 BYTE dfCharSet;
69 INT16 dfPixWidth;
70 INT16 dfPixHeight;
71 BYTE dfPitchAndFamily;
72 INT16 dfAvgWidth;
73 INT16 dfMaxWidth;
74 BYTE dfFirstChar;
75 BYTE dfLastChar;
76 BYTE dfDefaultChar;
77 BYTE dfBreakChar;
78 INT16 dfWidthBytes;
79 LONG dfDevice;
80 LONG dfFace;
81 LONG dfBitsPointer;
82 LONG dfBitsOffset;
83 BYTE dfReserved;
84 LONG dfFlags;
85 INT16 dfAspace;
86 INT16 dfBspace;
87 INT16 dfCspace;
88 LONG dfColorPointer;
89 LONG dfReserved1[4];
90} FONTINFO16;
91
92typedef struct
93{
94 WORD offset;
95 WORD length;
96 WORD flags;
97 WORD id;
98 WORD handle;
99 WORD usage;
100} NE_NAMEINFO;
101
102typedef struct
103{
104 WORD type_id;
105 WORD count;
106 DWORD resloader;
107} NE_TYPEINFO;
108
109#define NE_FFLAGS_SINGLEDATA 0x0001
110#define NE_FFLAGS_MULTIPLEDATA 0x0002
111#define NE_FFLAGS_WIN32 0x0010
112#define NE_FFLAGS_FRAMEBUF 0x0100
113#define NE_FFLAGS_CONSOLE 0x0200
114#define NE_FFLAGS_GUI 0x0300
115#define NE_FFLAGS_SELFLOAD 0x0800
116#define NE_FFLAGS_LINKERROR 0x2000
117#define NE_FFLAGS_CALLWEP 0x4000
118#define NE_FFLAGS_LIBMODULE 0x8000
119
120#define NE_OSFLAGS_WINDOWS 0x02
121
122#define NE_RSCTYPE_FONTDIR 0x8007
123#define NE_RSCTYPE_FONT 0x8008
124#define NE_RSCTYPE_SCALABLE_FONTPATH 0x80cc
125
126#define NE_SEGFLAGS_DATA 0x0001
127#define NE_SEGFLAGS_ALLOCATED 0x0002
128#define NE_SEGFLAGS_LOADED 0x0004
129#define NE_SEGFLAGS_ITERATED 0x0008
130#define NE_SEGFLAGS_MOVEABLE 0x0010
131#define NE_SEGFLAGS_SHAREABLE 0x0020
132#define NE_SEGFLAGS_PRELOAD 0x0040
133#define NE_SEGFLAGS_EXECUTEONLY 0x0080
134#define NE_SEGFLAGS_READONLY 0x0080
135#define NE_SEGFLAGS_RELOC_DATA 0x0100
136#define NE_SEGFLAGS_SELFLOAD 0x0800
137#define NE_SEGFLAGS_DISCARDABLE 0x1000
138#define NE_SEGFLAGS_32BIT 0x2000
139
140typedef struct tagFontHeader
141{
142 SHORT dfVersion; /* Version */
143 LONG dfSize; /* Total File Size */
144 char dfCopyright[60]; /* Copyright notice */
145 FONTINFO16 fi; /* FONTINFO structure */
146} fnt_hdrS;
147
148typedef struct WinCharStruct
149{
150 unsigned int charWidth;
151 long charOffset;
152} WinCharS;
153
154typedef struct fntFontStruct
155{
156 fnt_hdrS hdr;
157 WinCharS *dfCharTable;
158 unsigned char *dfDeviceP;
159 unsigned char *dfFaceP;
160 unsigned char *dfBitsPointerP;
161 unsigned char *dfBitsOffsetP;
162 short *dfColorTableP;
163} fnt_fontS;
164
165#include "poppack.h"
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000166
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000167#define FILE_ERROR 0
168#define FILE_DLL 1
169#define FILE_FNT 2
170
171/* global options */
172
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100173static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
174static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000175
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100176static char* g_lpstrFileName = NULL;
177static char* g_lpstrCharSet = NULL;
178static char* g_lpstrInputFile = NULL;
179static int g_outputPoints = 0;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000180
181/* info */
182
Eric Pouech763aff62004-12-06 16:44:32 +0000183static void usage(void)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000184{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000185 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
186 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000187 printf(" -f basename\tbasic output filename\n");
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000188 printf(" -t \t\toutput files by point size instead of pixel height\n");
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000189 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
190 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
191 exit(-1);
192}
193
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000194/* convert little-endian value to the local format */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000195
Eric Pouech763aff62004-12-06 16:44:32 +0000196static int return_data_value(enum data_types dtype, void * pChr)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000197{
198int ret_val = 0;
199
200 switch(dtype) {
Vincent Béron9a624912002-05-31 23:06:46 +0000201 case (dfChar):
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000202 ret_val = (int) *(unsigned char *)pChr;
203 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000204
205 case(dfShort):
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000206 ret_val = *(unsigned char *)pChr;
207 ret_val += (*((unsigned char *)pChr + 1) << 8);
208 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000209
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000210 case(dfLong): {
211 int i;
212
213 for(i=3; i >= 0; i--) {
214 ret_val += *((unsigned char *)pChr + i) << (8*i);
215 }
216 break;
217 }
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000218 case(dfString):
219 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000220 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000221 return ret_val;
222}
223
Eric Pouech763aff62004-12-06 16:44:32 +0000224static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000225{
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000226long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000227char* lpChar;
228
229 if( !g_lpstrFileName )
230 {
Vincent Béron9a624912002-05-31 23:06:46 +0000231 if( !l_nameoffset ||
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000232 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000233 return ERROR_DATA;
234 lpChar = (char*)(file_buffer + l_nameoffset);
235 }
236 else lpChar = g_lpstrFileName;
237
238 strcpy( name, lpChar );
239
Vincent Béron9a624912002-05-31 23:06:46 +0000240 while( (lpChar = strchr( name, ' ')) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000241 *lpChar = '-';
242
243 /* construct a filename from the font typeface, slant, weight, and size */
244
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000245 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000246 else strcat(name, "_r" );
247
248 lpChar = name + strlen( name );
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000249 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
250 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
251 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000252 return 0;
253}
254
255/* parse FONT resource and write .bdf file */
256
Eric Pouech763aff62004-12-06 16:44:32 +0000257static int parse_fnt_data(unsigned char* file_buffer, int length)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000258{
Vincent Béron9a624912002-05-31 23:06:46 +0000259 fnt_fontS cpe_font_struct;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000260 int ic=0, t;
261
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000262 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000263
264 /* check font header */
265
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000266 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000267 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
268
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000269 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
270 if (t & 1)
271 {
272 fprintf(stderr, "Vector fonts not supported\n");
273 return ERROR_DATA;
274 }
275
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000276 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000277 if( t > length ) return ERROR_SIZE;
278 else
Vincent Béron9a624912002-05-31 23:06:46 +0000279 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000280 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
281
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000282 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
Vincent Béron9a624912002-05-31 23:06:46 +0000283 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000284 int l_len = l_lchar - l_fchar + 1;
285 int l_ptr = sizeof(fnt_hdrS);
286
287 /* some fields were introduced for Windows 3.x fonts */
288 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
289 l_ptr -= 30;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000290
291 /* malloc size = (# chars) * sizeof(WinCharS) */
292
Vincent Béron9a624912002-05-31 23:06:46 +0000293 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000294 return ERROR_MEMORY;
295
296 /* NOW, convert them all to UNIX (lton) notation... */
297
298 for(ic=0; ic < l_len; ic++) {
299 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
300 l_ptr += 2; /* bump by sizeof(short) */
301
302
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000303 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
Vincent Béron9a624912002-05-31 23:06:46 +0000304 cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000305 return_data_value(dfShort, &file_buffer[l_ptr]);
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000306 l_ptr += 2; /* bump by sizeof(short) */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000307 }
308 else { /* Windows Version 3.0 type font */
Vincent Béron9a624912002-05-31 23:06:46 +0000309 cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000310 return_data_value(dfLong, &file_buffer[l_ptr]);
311 l_ptr += 4; /* bump by sizeof(long) */
312 }
313 }
314 t = dump_bdf(&cpe_font_struct, file_buffer);
315 free( cpe_font_struct.dfCharTable );
316 }
317 return t;
318}
319
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100320static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000321{
322 FILE* fp;
323 int ic;
Vincent Béron9a624912002-05-31 23:06:46 +0000324 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
325 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000326
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000327 int l_len = l_lchar-l_fchar + 1,
Vincent Béron9a624912002-05-31 23:06:46 +0000328 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000329 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000330 char l_filename[256];
331
332 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
333 return ic;
334
Francois Gougeta70fbae2008-12-08 09:25:18 +0100335 if((fp = fopen(l_filename, "w")) == NULL)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000336 {
337 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
338 return ERROR_FILE;
339 }
340
Alexandre Julliard02e90081998-01-04 17:49:09 +0000341 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
Marcus Meissnera3062532009-08-08 10:44:09 +0200342 if (ic) {
343 fclose(fp);
344 return (ic);
345 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000346
347 /* NOW, convert all chars to UNIX (lton) notation... */
348
349 for(ic=0; ic < l_len; ic++) {
350 int rowidx, l_span, /* how many char-cols wide is char? */
351 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
352
Vincent Béron9a624912002-05-31 23:06:46 +0000353 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000354
Francois Gouget67246552009-03-08 23:51:25 +0100355 fprintf(fp, "STARTCHAR %d\n", ic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000356 fprintf(fp, "ENCODING %d\n", l_fchar);
Francois Gouget67246552009-03-08 23:51:25 +0100357 fprintf(fp, "SWIDTH %d %d\n",
Vincent Béron9a624912002-05-31 23:06:46 +0000358 cpe_font_struct->dfCharTable[ic].charWidth*1000,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000359 0);
360
Francois Gouget67246552009-03-08 23:51:25 +0100361 fprintf(fp, "DWIDTH %d %d\n",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000362 cpe_font_struct->dfCharTable[ic].charWidth, 0);
363
364 fprintf(fp, "BBX %d %d %d %d\n",
Vincent Béron9a624912002-05-31 23:06:46 +0000365 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000366 l_ascent - l_hgt);
367
368 fprintf(fp, "BITMAP\n");
369 for(rowidx=0; rowidx < l_hgt; rowidx++) {
370 switch(l_span) {
371 case(0): /* 1-7 pixels wide font */
372 {
373 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
374 break;
375 }
Vincent Béron9a624912002-05-31 23:06:46 +0000376
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000377 case(1): /* 8-15 pixels wide font */
378 {
Vincent Béron9a624912002-05-31 23:06:46 +0000379 fprintf(fp, "%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000380 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
381 fprintf(fp, "\n");
382 break;
383 }
384
385 case(2): /* 16-23 pixels wide font */
386 {
Vincent Béron9a624912002-05-31 23:06:46 +0000387 fprintf(fp, "%02X%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000388 file_buffer[l_idx+rowidx],
389 file_buffer[l_idx+l_hgt+rowidx],
390 file_buffer[l_idx+(2*l_hgt)+rowidx]);
391 fprintf(fp, "\n");
392 break;
393 }
394
395 case(3): /* 24-31 pixels wide font */
396 {
Vincent Béron9a624912002-05-31 23:06:46 +0000397 fprintf(fp, "%02X%02X%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000398 file_buffer[l_idx+rowidx],
399 file_buffer[l_idx+l_hgt+rowidx],
400 file_buffer[l_idx+(2*l_hgt)+rowidx],
401 file_buffer[l_idx+(3*l_hgt)+rowidx]);
402 fprintf(fp, "\n");
403 break;
404 }
405 case(4): /* 32-39 */
406 {
407 fprintf(fp, "%02X%02X%02X%02X%02X",
408 file_buffer[l_idx+rowidx],
409 file_buffer[l_idx+l_hgt+rowidx],
410 file_buffer[l_idx+(2*l_hgt)+rowidx],
411 file_buffer[l_idx+(3*l_hgt)+rowidx],
412 file_buffer[l_idx+(4*l_hgt)+rowidx]);
413 fprintf(fp, "\n");
414 break;
415 }
416 default:
417 fclose(fp);
418 unlink(l_filename);
419 return ERROR_DATA;
420 }
421 }
422 fprintf(fp, "ENDCHAR\n");
423
424 l_fchar++; /* Go to next one */
425 }
426fprintf(fp, "ENDFONT\n");
427fclose(fp);
428return 0;
429}
430
431
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100432static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000433{
Vincent Béron9a624912002-05-31 23:06:46 +0000434int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000435 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000436int l_len = l_lchar - l_fchar + 1;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000437long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
438int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
439int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000440
441 fprintf(fs, "STARTFONT 2.1\n");
442
443 /* Compose font name */
444
Vincent Béron9a624912002-05-31 23:06:46 +0000445 if( l_nameoffset &&
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000446 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000447 {
448 int dpi, point_size;
449 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000450 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000451
Vincent Béron9a624912002-05-31 23:06:46 +0000452 while((lpChar = strchr(lpFace, '-')) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000453 *lpChar = ' ';
454
455 fprintf(fs, "FONT -windows-%s-", lpFace );
456
457 if( tmWeight == 0 ) /* weight */
458 fputs("medium-", fs);
459 else if( tmWeight <= FW_LIGHT )
460 fputs("light-", fs);
461 else if( tmWeight <= FW_MEDIUM )
462 fputs("medium-", fs);
463 else if( tmWeight <= FW_DEMIBOLD )
464 fputs("demibold-", fs);
465 else if( tmWeight <= FW_BOLD )
466 fputs("bold-", fs);
467 else fputs("black-", fs);
468
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000469 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000470 fputs("i-", fs);
471 else fputs("r-", fs);
472
473 /* style */
474
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000475 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000476 fputs("normal-sans-", fs);
477 else fputs("normal--", fs); /* still can be -sans */
478
479 /* y extents */
480
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000481 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000482 dpi = (l_cellheight * 720) / point_size;
483
Vincent Béron9a624912002-05-31 23:06:46 +0000484 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
Bill Medland4eb22c62001-07-18 20:00:44 +0000485 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
486 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000487
488 /* spacing */
489
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000490 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000491 else fputs("p-", fs);
Vincent Béron9a624912002-05-31 23:06:46 +0000492
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000493 /* average width */
494
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000495 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000496
497 /* charset */
498
Stas Sergeev5c13c212000-07-10 13:04:08 +0000499 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
500 else
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000501 switch( cpe_font_struct->hdr.fi.dfCharSet )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000502 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000503 /* Microsoft just had to invent its own charsets! */
504
Pablo Saratxagab13f23e1998-12-09 14:51:48 +0000505 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000506 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
507 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
508 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
509 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
510 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
511 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
Vincent Béron9a624912002-05-31 23:06:46 +0000512 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000513 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000514 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
515 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
516
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000517 default:
Vincent Béron9a624912002-05-31 23:06:46 +0000518 case OEM_CHARSET:
519 fputs("Undefined charset, use -c option.\n", stderr);
520 return ERROR_DATA;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000521 }
522 }
523 else return ERROR_DATA;
524
Vincent Béron9a624912002-05-31 23:06:46 +0000525 fprintf(fs, "SIZE %d %d %d\n",
Bill Medland4eb22c62001-07-18 20:00:44 +0000526 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000527 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
528 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000529
530 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000531 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
532 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000533 0, l_ascent - l_cellheight );
534
535 fprintf(fs, "STARTPROPERTIES 4\n");
536
537 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
538 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
539 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000540 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
541 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000542
543 fprintf(fs, "ENDPROPERTIES\n");
544
545 fprintf(fs, "CHARS %d\n", l_len);
546 return 0;
547}
548
549
550
Eric Pouech763aff62004-12-06 16:44:32 +0000551static void parse_options(int argc, char **argv)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000552{
553 int i;
554
555 switch( argc )
556 {
557 case 2:
558 g_lpstrInputFile = argv[1];
559 break;
560
561 case 3:
562 case 4:
563 case 5:
564 case 6:
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000565 case 7:
566 case 8:
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000567 for( i = 1; i < argc - 1; i++ )
568 {
569 if( argv[i][0] != '-' ||
570 strlen(argv[i]) != 2 ) break;
571
Vincent Béron9a624912002-05-31 23:06:46 +0000572 if( argv[i][1] == 'c' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000573 g_lpstrCharSet = argv[i+1];
Vincent Béron9a624912002-05-31 23:06:46 +0000574 else
575 if( argv[i][1] == 'f' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000576 g_lpstrFileName = argv[i+1];
577 else
578 if( argv[i][1] == 't' )
579 {
580 g_outputPoints = 1;
581 continue;
582 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000583 else
584 usage();
585
586 i++;
Vincent Béron9a624912002-05-31 23:06:46 +0000587 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000588 if( i == argc - 1 )
589 {
590 g_lpstrInputFile = argv[i];
591 break;
592 }
593 default: usage();
594 }
Vincent Béron9a624912002-05-31 23:06:46 +0000595
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000596}
597
598/* read file data and return file type */
599
Eric Pouech763aff62004-12-06 16:44:32 +0000600static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000601{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000602 IMAGE_DOS_HEADER mz_header;
603 IMAGE_OS2_HEADER ne_header;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000604 long s, offset, size;
605 int retval;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000606
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000607 lseek( fd, 0, SEEK_SET );
608
Vincent Béron9a624912002-05-31 23:06:46 +0000609 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000610 return FILE_ERROR;
611
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000612 s = return_data_value(dfShort, &mz_header.e_magic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000613
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000614 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000615 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000616 s = return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000617 lseek( fd, s, SEEK_SET );
618
619 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
620 return FILE_ERROR;
621
622 s = return_data_value(dfShort, &ne_header.ne_magic);
623
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000624 if( s == IMAGE_NT_SIGNATURE)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000625 {
626 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
Vincent Béron9a624912002-05-31 23:06:46 +0000627 return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000628 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000629 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000630
Alexandre Julliard180a0882000-04-18 11:58:24 +0000631 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
632 size = return_data_value(dfShort, &ne_header.ne_restab);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000633
634 if( size > fsize ) return FILE_ERROR;
635
636 size -= s;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000637 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000638
639 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
640 retval = FILE_DLL;
641 }
642 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
643 {
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000644 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000645
646 if( size != fsize ) return FILE_ERROR;
647 offset = 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000648 retval = FILE_FNT;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000649 }
650 else return FILE_ERROR;
651
652 *lpdata = (unsigned char*)malloc(size);
653
654 if( *lpdata )
655 {
656 lseek( fd, offset, SEEK_SET );
657 if( read(fd, *lpdata, size) != size )
658 { free( *lpdata ); *lpdata = NULL; }
659 }
660 return retval;
661}
662
663
664/* entry point */
665
666int main(int argc, char **argv)
667{
668 unsigned char* lpdata = NULL;
669 int fd;
670
671 parse_options( argc, argv);
672
Marcus Meissnera3062532009-08-08 10:44:09 +0200673 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000674 {
675 int i;
676 struct stat file_stat;
677
678 fstat( fd, &file_stat);
679 i = get_resource_table( fd, &lpdata, file_stat.st_size );
680
681 switch(i)
682 {
683 case FILE_DLL:
684 if( lpdata )
685 {
686 int j, count = 0;
687 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
688 NE_NAMEINFO* pFontStorage = NULL;
689
Vincent Béron9a624912002-05-31 23:06:46 +0000690 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000691 {
692 j = return_data_value(dfShort, &pTInfo->count);
693 if( i == NE_RSCTYPE_FONT )
694 {
695 count = j;
696 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000697 break; /* found one */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000698 }
699
700 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
701 }
702 if( pFontStorage && count )
703 {
704 unsigned short size_shift = return_data_value(dfShort, lpdata);
705 unsigned char* lpfont = NULL;
706 unsigned offset;
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000707 int length;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000708
709 for( j = 0; j < count; j++, pFontStorage++ )
710 {
711 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
712 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
Vincent Béron9a624912002-05-31 23:06:46 +0000713
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000714 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
715 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100716 fprintf(stderr, "Memory allocation error.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000717 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000718 }
719
720 lseek( fd, offset, SEEK_SET );
721 if( read(fd, lpfont, length) != length )
722 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100723 fprintf(stderr, "Unable to read Windows DLL.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000724 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000725 }
726
727 if( (i = parse_fnt_data( lpfont, length )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000728 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100729 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000730 exit(1);
731 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000732 }
733 free(lpfont); free(lpdata);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000734 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000735 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000736 else
737 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100738 fprintf(stderr, "No fonts found.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000739 exit(1);
740 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000741 free( lpdata );
742 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000743 else
744 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100745 fprintf(stderr, "Unable to read Windows DLL.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000746 exit(1);
747 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000748 break;
749
750 case FILE_FNT:
751 if( lpdata )
752 {
753 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000754 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100755 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000756 exit(1);
757 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000758 free( lpdata );
759 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000760 else
761 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100762 fprintf(stderr, "Unable to read .FNT file.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000763 exit(1);
764 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000765 break;
766
767 case FILE_ERROR:
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100768 fprintf(stderr, "Corrupt or invalid file.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000769 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000770 }
771 close(fd);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000772 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000773 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000774 else
775 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100776 fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000777 exit(1);
778 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000779}