blob: 4dc20e55844ce720ab4595253ef60bd1acdff01f [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
Marcin Baczyńskic7183e82010-01-06 23:38:35 +0100293 if((cpe_font_struct.dfCharTable = 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 {
Gerald Pfeifer878df9a2010-04-23 20:42:32 +0200448 int point_size;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000449 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
Vincent Béron9a624912002-05-31 23:06:46 +0000483 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
Bill Medland4eb22c62001-07-18 20:00:44 +0000484 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
485 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000486
487 /* spacing */
488
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000489 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000490 else fputs("p-", fs);
Vincent Béron9a624912002-05-31 23:06:46 +0000491
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000492 /* average width */
493
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000494 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000495
496 /* charset */
497
Stas Sergeev5c13c212000-07-10 13:04:08 +0000498 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
499 else
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000500 switch( cpe_font_struct->hdr.fi.dfCharSet )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000501 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000502 /* Microsoft just had to invent its own charsets! */
503
Pablo Saratxagab13f23e1998-12-09 14:51:48 +0000504 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000505 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
506 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
507 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
508 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
509 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
510 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
Vincent Béron9a624912002-05-31 23:06:46 +0000511 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000512 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000513 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
514 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
515
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000516 default:
Vincent Béron9a624912002-05-31 23:06:46 +0000517 case OEM_CHARSET:
518 fputs("Undefined charset, use -c option.\n", stderr);
519 return ERROR_DATA;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000520 }
521 }
522 else return ERROR_DATA;
523
Vincent Béron9a624912002-05-31 23:06:46 +0000524 fprintf(fs, "SIZE %d %d %d\n",
Bill Medland4eb22c62001-07-18 20:00:44 +0000525 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000526 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
527 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000528
529 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000530 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
531 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000532 0, l_ascent - l_cellheight );
533
534 fprintf(fs, "STARTPROPERTIES 4\n");
535
536 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
537 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
538 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000539 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
540 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000541
542 fprintf(fs, "ENDPROPERTIES\n");
543
544 fprintf(fs, "CHARS %d\n", l_len);
545 return 0;
546}
547
548
549
Eric Pouech763aff62004-12-06 16:44:32 +0000550static void parse_options(int argc, char **argv)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000551{
552 int i;
553
554 switch( argc )
555 {
556 case 2:
557 g_lpstrInputFile = argv[1];
558 break;
559
560 case 3:
561 case 4:
562 case 5:
563 case 6:
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000564 case 7:
565 case 8:
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000566 for( i = 1; i < argc - 1; i++ )
567 {
568 if( argv[i][0] != '-' ||
569 strlen(argv[i]) != 2 ) break;
570
Vincent Béron9a624912002-05-31 23:06:46 +0000571 if( argv[i][1] == 'c' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000572 g_lpstrCharSet = argv[i+1];
Vincent Béron9a624912002-05-31 23:06:46 +0000573 else
574 if( argv[i][1] == 'f' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000575 g_lpstrFileName = argv[i+1];
576 else
577 if( argv[i][1] == 't' )
578 {
579 g_outputPoints = 1;
580 continue;
581 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000582 else
583 usage();
584
585 i++;
Vincent Béron9a624912002-05-31 23:06:46 +0000586 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000587 if( i == argc - 1 )
588 {
589 g_lpstrInputFile = argv[i];
590 break;
591 }
592 default: usage();
593 }
Vincent Béron9a624912002-05-31 23:06:46 +0000594
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000595}
596
597/* read file data and return file type */
598
Eric Pouech763aff62004-12-06 16:44:32 +0000599static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000600{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000601 IMAGE_DOS_HEADER mz_header;
602 IMAGE_OS2_HEADER ne_header;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000603 long s, offset, size;
604 int retval;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000605
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000606 lseek( fd, 0, SEEK_SET );
607
Vincent Béron9a624912002-05-31 23:06:46 +0000608 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000609 return FILE_ERROR;
610
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000611 s = return_data_value(dfShort, &mz_header.e_magic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000612
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000613 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000614 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000615 s = return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000616 lseek( fd, s, SEEK_SET );
617
618 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
619 return FILE_ERROR;
620
621 s = return_data_value(dfShort, &ne_header.ne_magic);
622
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000623 if( s == IMAGE_NT_SIGNATURE)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000624 {
625 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
Vincent Béron9a624912002-05-31 23:06:46 +0000626 return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000627 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000628 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000629
Alexandre Julliard180a0882000-04-18 11:58:24 +0000630 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
631 size = return_data_value(dfShort, &ne_header.ne_restab);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000632
633 if( size > fsize ) return FILE_ERROR;
634
635 size -= s;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000636 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000637
638 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
639 retval = FILE_DLL;
640 }
641 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
642 {
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000643 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000644
645 if( size != fsize ) return FILE_ERROR;
646 offset = 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000647 retval = FILE_FNT;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000648 }
649 else return FILE_ERROR;
650
Marcin Baczyńskic7183e82010-01-06 23:38:35 +0100651 *lpdata = malloc(size);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000652
653 if( *lpdata )
654 {
655 lseek( fd, offset, SEEK_SET );
656 if( read(fd, *lpdata, size) != size )
657 { free( *lpdata ); *lpdata = NULL; }
658 }
659 return retval;
660}
661
662
663/* entry point */
664
665int main(int argc, char **argv)
666{
667 unsigned char* lpdata = NULL;
668 int fd;
669
670 parse_options( argc, argv);
671
Marcus Meissnera3062532009-08-08 10:44:09 +0200672 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000673 {
674 int i;
675 struct stat file_stat;
676
677 fstat( fd, &file_stat);
678 i = get_resource_table( fd, &lpdata, file_stat.st_size );
679
680 switch(i)
681 {
682 case FILE_DLL:
683 if( lpdata )
684 {
685 int j, count = 0;
686 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
687 NE_NAMEINFO* pFontStorage = NULL;
688
Vincent Béron9a624912002-05-31 23:06:46 +0000689 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000690 {
691 j = return_data_value(dfShort, &pTInfo->count);
692 if( i == NE_RSCTYPE_FONT )
693 {
694 count = j;
695 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000696 break; /* found one */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000697 }
698
699 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
700 }
701 if( pFontStorage && count )
702 {
703 unsigned short size_shift = return_data_value(dfShort, lpdata);
704 unsigned char* lpfont = NULL;
705 unsigned offset;
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000706 int length;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000707
708 for( j = 0; j < count; j++, pFontStorage++ )
709 {
710 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
711 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
Vincent Béron9a624912002-05-31 23:06:46 +0000712
Marcin Baczyńskic7183e82010-01-06 23:38:35 +0100713 if( !(lpfont = realloc( lpfont, length )) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000714 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100715 fprintf(stderr, "Memory allocation error.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000716 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000717 }
718
719 lseek( fd, offset, SEEK_SET );
720 if( read(fd, lpfont, length) != length )
721 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100722 fprintf(stderr, "Unable to read Windows DLL.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000723 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000724 }
725
726 if( (i = parse_fnt_data( lpfont, length )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000727 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100728 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000729 exit(1);
730 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000731 }
732 free(lpfont); free(lpdata);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000733 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000734 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000735 else
736 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100737 fprintf(stderr, "No fonts found.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000738 exit(1);
739 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000740 free( lpdata );
741 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000742 else
743 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100744 fprintf(stderr, "Unable to read Windows DLL.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000745 exit(1);
746 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000747 break;
748
749 case FILE_FNT:
750 if( lpdata )
751 {
752 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000753 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100754 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000755 exit(1);
756 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000757 free( lpdata );
758 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000759 else
760 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100761 fprintf(stderr, "Unable to read .FNT file.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000762 exit(1);
763 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000764 break;
765
766 case FILE_ERROR:
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100767 fprintf(stderr, "Corrupt or invalid file.\n" );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000768 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000769 }
770 close(fd);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000771 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000772 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000773 else
774 {
Alexandre Julliarda5f1e9b2008-12-05 12:36:05 +0100775 fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000776 exit(1);
777 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000778}