blob: 3f0ab595e4179390ad4e5e6bb46cd95d897b7558 [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
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 Julliard7e6ae4b1996-12-08 19:25:27 +000042#include "fnt2bdf.h"
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000043
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000044#define FILE_ERROR 0
45#define FILE_DLL 1
46#define FILE_FNT 2
47
48/* global options */
49
Dmitry Timoshkovfef71862000-07-25 12:25:40 +000050int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
51int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
52
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000053char* g_lpstrFileName = NULL;
54char* g_lpstrCharSet = NULL;
55char* g_lpstrInputFile = NULL;
Alexandre Julliard23946ad1997-06-16 17:43:53 +000056int g_outputPoints = 0;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000057
Daniel Marmier9bb19ac2003-10-03 03:35:20 +000058static const char* errorDLLRead = "Unable to read Windows DLL.\n";
59static const char* errorFNTRead = "Unable to read .FNT file.\n";
60static const char* errorOpenFile = "Unable to open file.\n";
61static const char* errorMemory = "Memory allocation error.\n";
62static const char* errorFile = "Corrupt or invalid file.\n";
63static const char* errorFontData = "Unable to parse font data: Error ";
64static const char* errorEmpty = "No fonts found.\n";
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000065
66/* info */
67
Eric Pouech763aff62004-12-06 16:44:32 +000068static void usage(void)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000069{
Alexandre Julliard23946ad1997-06-16 17:43:53 +000070 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
71 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000072 printf(" -f basename\tbasic output filename\n");
Alexandre Julliard23946ad1997-06-16 17:43:53 +000073 printf(" -t \t\toutput files by point size instead of pixel height\n");
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000074 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);
77}
78
Dmitry Timoshkovfef71862000-07-25 12:25:40 +000079/* convert little-endian value to the local format */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000080
Eric Pouech763aff62004-12-06 16:44:32 +000081static int return_data_value(enum data_types dtype, void * pChr)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000082{
83int ret_val = 0;
84
85 switch(dtype) {
Vincent Béron9a624912002-05-31 23:06:46 +000086 case (dfChar):
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000087 ret_val = (int) *(unsigned char *)pChr;
88 break;
Vincent Béron9a624912002-05-31 23:06:46 +000089
90 case(dfShort):
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000091 ret_val = *(unsigned char *)pChr;
92 ret_val += (*((unsigned char *)pChr + 1) << 8);
93 break;
Vincent Béron9a624912002-05-31 23:06:46 +000094
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000095 case(dfLong): {
96 int i;
97
98 for(i=3; i >= 0; i--) {
99 ret_val += *((unsigned char *)pChr + i) << (8*i);
100 }
101 break;
102 }
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000103 case(dfString):
104 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000105 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000106 return ret_val;
107}
108
Eric Pouech763aff62004-12-06 16:44:32 +0000109static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000110{
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000111long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000112char* lpChar;
113
114 if( !g_lpstrFileName )
115 {
Vincent Béron9a624912002-05-31 23:06:46 +0000116 if( !l_nameoffset ||
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000117 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000118 return ERROR_DATA;
119 lpChar = (char*)(file_buffer + l_nameoffset);
120 }
121 else lpChar = g_lpstrFileName;
122
123 strcpy( name, lpChar );
124
Vincent Béron9a624912002-05-31 23:06:46 +0000125 while( (lpChar = strchr( name, ' ')) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000126 *lpChar = '-';
127
128 /* construct a filename from the font typeface, slant, weight, and size */
129
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000130 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000131 else strcat(name, "_r" );
132
133 lpChar = name + strlen( name );
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000134 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) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000137 return 0;
138}
139
140/* parse FONT resource and write .bdf file */
141
Eric Pouech763aff62004-12-06 16:44:32 +0000142static int parse_fnt_data(unsigned char* file_buffer, int length)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000143{
Vincent Béron9a624912002-05-31 23:06:46 +0000144 fnt_fontS cpe_font_struct;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000145 int ic=0, t;
146
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000147 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000148
149 /* check font header */
150
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000151 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000152 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
153
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000154 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
155 if (t & 1)
156 {
157 fprintf(stderr, "Vector fonts not supported\n");
158 return ERROR_DATA;
159 }
160
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000161 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000162 if( t > length ) return ERROR_SIZE;
163 else
Vincent Béron9a624912002-05-31 23:06:46 +0000164 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000165 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
166
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000167 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
Vincent Béron9a624912002-05-31 23:06:46 +0000168 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000169 int l_len = l_lchar - l_fchar + 1;
170 int l_ptr = sizeof(fnt_hdrS);
171
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;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000175
176 /* malloc size = (# chars) * sizeof(WinCharS) */
177
Vincent Béron9a624912002-05-31 23:06:46 +0000178 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000179 return ERROR_MEMORY;
180
181 /* NOW, convert them all to UNIX (lton) notation... */
182
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) */
186
187
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000188 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
Vincent Béron9a624912002-05-31 23:06:46 +0000189 cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000190 return_data_value(dfShort, &file_buffer[l_ptr]);
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000191 l_ptr += 2; /* bump by sizeof(short) */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000192 }
193 else { /* Windows Version 3.0 type font */
Vincent Béron9a624912002-05-31 23:06:46 +0000194 cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000195 return_data_value(dfLong, &file_buffer[l_ptr]);
196 l_ptr += 4; /* bump by sizeof(long) */
197 }
198 }
199 t = dump_bdf(&cpe_font_struct, file_buffer);
200 free( cpe_font_struct.dfCharTable );
201 }
202 return t;
203}
204
205int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
206{
207 FILE* fp;
208 int ic;
Vincent Béron9a624912002-05-31 23:06:46 +0000209 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);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000211
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000212 int l_len = l_lchar-l_fchar + 1,
Vincent Béron9a624912002-05-31 23:06:46 +0000213 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000214 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000215 char l_filename[256];
216
217 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
218 return ic;
219
Vincent Béron9a624912002-05-31 23:06:46 +0000220 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000221 {
222 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
223 return ERROR_FILE;
224 }
225
Alexandre Julliard02e90081998-01-04 17:49:09 +0000226 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
227 if (ic) return (ic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000228
229 /* NOW, convert all chars to UNIX (lton) notation... */
230
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;
234
Vincent Béron9a624912002-05-31 23:06:46 +0000235 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000236
237 fprintf(fp, "STARTCHAR %d \n", ic);
238 fprintf(fp, "ENCODING %d\n", l_fchar);
Vincent Béron9a624912002-05-31 23:06:46 +0000239 fprintf(fp, "SWIDTH %d %d \n",
240 cpe_font_struct->dfCharTable[ic].charWidth*1000,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000241 0);
242
Vincent Béron9a624912002-05-31 23:06:46 +0000243 fprintf(fp, "DWIDTH %d %d \n",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000244 cpe_font_struct->dfCharTable[ic].charWidth, 0);
245
246 fprintf(fp, "BBX %d %d %d %d\n",
Vincent Béron9a624912002-05-31 23:06:46 +0000247 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000248 l_ascent - l_hgt);
249
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 */
254 {
255 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
256 break;
257 }
Vincent Béron9a624912002-05-31 23:06:46 +0000258
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000259 case(1): /* 8-15 pixels wide font */
260 {
Vincent Béron9a624912002-05-31 23:06:46 +0000261 fprintf(fp, "%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000262 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
263 fprintf(fp, "\n");
264 break;
265 }
266
267 case(2): /* 16-23 pixels wide font */
268 {
Vincent Béron9a624912002-05-31 23:06:46 +0000269 fprintf(fp, "%02X%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000270 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;
275 }
276
277 case(3): /* 24-31 pixels wide font */
278 {
Vincent Béron9a624912002-05-31 23:06:46 +0000279 fprintf(fp, "%02X%02X%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000280 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;
286 }
287 case(4): /* 32-39 */
288 {
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;
297 }
298 default:
299 fclose(fp);
300 unlink(l_filename);
301 return ERROR_DATA;
302 }
303 }
304 fprintf(fp, "ENDCHAR\n");
305
306 l_fchar++; /* Go to next one */
307 }
308fprintf(fp, "ENDFONT\n");
309fclose(fp);
310return 0;
311}
312
313
314int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
315{
Vincent Béron9a624912002-05-31 23:06:46 +0000316int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000317 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000318int l_len = l_lchar - l_fchar + 1;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000319long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
320int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
321int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000322
323 fprintf(fs, "STARTFONT 2.1\n");
324
325 /* Compose font name */
326
Vincent Béron9a624912002-05-31 23:06:46 +0000327 if( l_nameoffset &&
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000328 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000329 {
330 int dpi, point_size;
331 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000332 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000333
Vincent Béron9a624912002-05-31 23:06:46 +0000334 while((lpChar = strchr(lpFace, '-')) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000335 *lpChar = ' ';
336
337 fprintf(fs, "FONT -windows-%s-", lpFace );
338
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);
350
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000351 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000352 fputs("i-", fs);
353 else fputs("r-", fs);
354
355 /* style */
356
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000357 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000358 fputs("normal-sans-", fs);
359 else fputs("normal--", fs); /* still can be -sans */
360
361 /* y extents */
362
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000363 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000364 dpi = (l_cellheight * 720) / point_size;
365
Vincent Béron9a624912002-05-31 23:06:46 +0000366 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
Bill Medland4eb22c62001-07-18 20:00:44 +0000367 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
368 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000369
370 /* spacing */
371
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000372 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000373 else fputs("p-", fs);
Vincent Béron9a624912002-05-31 23:06:46 +0000374
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000375 /* average width */
376
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000377 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000378
379 /* charset */
380
Stas Sergeev5c13c212000-07-10 13:04:08 +0000381 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
382 else
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000383 switch( cpe_font_struct->hdr.fi.dfCharSet )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000384 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000385 /* Microsoft just had to invent its own charsets! */
386
Pablo Saratxagab13f23e1998-12-09 14:51:48 +0000387 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000388 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;
Vincent Béron9a624912002-05-31 23:06:46 +0000394 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000395 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000396 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
397 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
398
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000399 default:
Vincent Béron9a624912002-05-31 23:06:46 +0000400 case OEM_CHARSET:
401 fputs("Undefined charset, use -c option.\n", stderr);
402 return ERROR_DATA;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000403 }
404 }
405 else return ERROR_DATA;
406
Vincent Béron9a624912002-05-31 23:06:46 +0000407 fprintf(fs, "SIZE %d %d %d\n",
Bill Medland4eb22c62001-07-18 20:00:44 +0000408 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000409 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
410 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000411
412 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000413 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
414 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000415 0, l_ascent - l_cellheight );
416
417 fprintf(fs, "STARTPROPERTIES 4\n");
418
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 -
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000422 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));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000424
425 fprintf(fs, "ENDPROPERTIES\n");
426
427 fprintf(fs, "CHARS %d\n", l_len);
428 return 0;
429}
430
431
432
Eric Pouech763aff62004-12-06 16:44:32 +0000433static void parse_options(int argc, char **argv)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000434{
435 int i;
436
437 switch( argc )
438 {
439 case 2:
440 g_lpstrInputFile = argv[1];
441 break;
442
443 case 3:
444 case 4:
445 case 5:
446 case 6:
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000447 case 7:
448 case 8:
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000449 for( i = 1; i < argc - 1; i++ )
450 {
451 if( argv[i][0] != '-' ||
452 strlen(argv[i]) != 2 ) break;
453
Vincent Béron9a624912002-05-31 23:06:46 +0000454 if( argv[i][1] == 'c' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000455 g_lpstrCharSet = argv[i+1];
Vincent Béron9a624912002-05-31 23:06:46 +0000456 else
457 if( argv[i][1] == 'f' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000458 g_lpstrFileName = argv[i+1];
459 else
460 if( argv[i][1] == 't' )
461 {
462 g_outputPoints = 1;
463 continue;
464 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000465 else
466 usage();
467
468 i++;
Vincent Béron9a624912002-05-31 23:06:46 +0000469 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000470 if( i == argc - 1 )
471 {
472 g_lpstrInputFile = argv[i];
473 break;
474 }
475 default: usage();
476 }
Vincent Béron9a624912002-05-31 23:06:46 +0000477
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000478}
479
480/* read file data and return file type */
481
Eric Pouech763aff62004-12-06 16:44:32 +0000482static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000483{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000484 IMAGE_DOS_HEADER mz_header;
485 IMAGE_OS2_HEADER ne_header;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000486 long s, offset, size;
487 int retval;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000488
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000489 lseek( fd, 0, SEEK_SET );
490
Vincent Béron9a624912002-05-31 23:06:46 +0000491 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000492 return FILE_ERROR;
493
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000494 s = return_data_value(dfShort, &mz_header.e_magic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000495
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000496 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000497 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000498 s = return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000499 lseek( fd, s, SEEK_SET );
500
501 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
502 return FILE_ERROR;
503
504 s = return_data_value(dfShort, &ne_header.ne_magic);
505
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000506 if( s == IMAGE_NT_SIGNATURE)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000507 {
508 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
Vincent Béron9a624912002-05-31 23:06:46 +0000509 return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000510 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000511 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000512
Alexandre Julliard180a0882000-04-18 11:58:24 +0000513 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
514 size = return_data_value(dfShort, &ne_header.ne_restab);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000515
516 if( size > fsize ) return FILE_ERROR;
517
518 size -= s;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000519 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000520
521 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
522 retval = FILE_DLL;
523 }
524 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
525 {
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000526 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000527
528 if( size != fsize ) return FILE_ERROR;
529 offset = 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000530 retval = FILE_FNT;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000531 }
532 else return FILE_ERROR;
533
534 *lpdata = (unsigned char*)malloc(size);
535
536 if( *lpdata )
537 {
538 lseek( fd, offset, SEEK_SET );
539 if( read(fd, *lpdata, size) != size )
540 { free( *lpdata ); *lpdata = NULL; }
541 }
542 return retval;
543}
544
545
546/* entry point */
547
548int main(int argc, char **argv)
549{
550 unsigned char* lpdata = NULL;
551 int fd;
552
553 parse_options( argc, argv);
554
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000555 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000556 {
557 int i;
558 struct stat file_stat;
559
560 fstat( fd, &file_stat);
561 i = get_resource_table( fd, &lpdata, file_stat.st_size );
562
563 switch(i)
564 {
565 case FILE_DLL:
566 if( lpdata )
567 {
568 int j, count = 0;
569 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
570 NE_NAMEINFO* pFontStorage = NULL;
571
Vincent Béron9a624912002-05-31 23:06:46 +0000572 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000573 {
574 j = return_data_value(dfShort, &pTInfo->count);
575 if( i == NE_RSCTYPE_FONT )
576 {
577 count = j;
578 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000579 break; /* found one */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000580 }
581
582 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
583 }
584 if( pFontStorage && count )
585 {
586 unsigned short size_shift = return_data_value(dfShort, lpdata);
587 unsigned char* lpfont = NULL;
588 unsigned offset;
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000589 int length;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000590
591 for( j = 0; j < count; j++, pFontStorage++ )
592 {
593 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
594 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
Vincent Béron9a624912002-05-31 23:06:46 +0000595
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000596 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
597 {
598 fprintf(stderr, errorMemory );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000599 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000600 }
601
602 lseek( fd, offset, SEEK_SET );
603 if( read(fd, lpfont, length) != length )
604 {
605 fprintf(stderr, errorDLLRead );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000606 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000607 }
608
609 if( (i = parse_fnt_data( lpfont, length )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000610 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000611 fprintf(stderr, "%s%d\n", errorFontData, i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000612 exit(1);
613 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000614 }
615 free(lpfont); free(lpdata);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000616 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000617 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000618 else
619 {
620 fprintf(stderr, errorEmpty );
621 exit(1);
622 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000623 free( lpdata );
624 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000625 else
626 {
627 fprintf(stderr, errorDLLRead);
628 exit(1);
629 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000630 break;
631
632 case FILE_FNT:
633 if( lpdata )
634 {
635 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000636 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000637 fprintf(stderr, "%s%d\n", errorFontData, i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000638 exit(1);
639 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000640 free( lpdata );
641 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000642 else
643 {
644 fprintf(stderr, errorFNTRead);
645 exit(1);
646 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000647 break;
648
649 case FILE_ERROR:
650 fprintf(stderr, errorFile );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000651 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000652 }
653 close(fd);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000654 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000655 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000656 else
657 {
658 fprintf(stderr, errorOpenFile );
659 exit(1);
660 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000661}