blob: 4a6bd16ae3d5a4060b128cfe562b19924f81b8f8 [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"
24
25#ifdef HAVE_SYS_PARAM_H
26# include <sys/param.h>
27#endif
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000028#include <sys/types.h>
29#include <sys/stat.h>
Dmitry Timoshkovfef71862000-07-25 12:25:40 +000030#include <stdio.h>
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000031#include <stdlib.h>
32#include <string.h>
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +000033#ifdef HAVE_UNISTD_H
34# include <unistd.h>
35#endif
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000036#include <fcntl.h>
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +000037#ifdef HAVE_IO_H
38# include <io.h>
39#endif
40
41#ifndef O_BINARY
42# define O_BINARY 0
43#endif
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000044
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000045#include "fnt2bdf.h"
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000046
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000047#define FILE_ERROR 0
48#define FILE_DLL 1
49#define FILE_FNT 2
50
51/* global options */
52
Dmitry Timoshkovfef71862000-07-25 12:25:40 +000053int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
54int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
55
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000056char* g_lpstrFileName = NULL;
57char* g_lpstrCharSet = NULL;
58char* g_lpstrInputFile = NULL;
Alexandre Julliard23946ad1997-06-16 17:43:53 +000059int g_outputPoints = 0;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000060
Daniel Marmier9bb19ac2003-10-03 03:35:20 +000061static const char* errorDLLRead = "Unable to read Windows DLL.\n";
62static const char* errorFNTRead = "Unable to read .FNT file.\n";
63static const char* errorOpenFile = "Unable to open file.\n";
64static const char* errorMemory = "Memory allocation error.\n";
65static const char* errorFile = "Corrupt or invalid file.\n";
66static const char* errorFontData = "Unable to parse font data: Error ";
67static const char* errorEmpty = "No fonts found.\n";
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000068
69/* info */
70
Marcus Meissner7ed94411998-10-11 11:27:05 +000071void usage(void)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000072{
Alexandre Julliard23946ad1997-06-16 17:43:53 +000073 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
74 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000075 printf(" -f basename\tbasic output filename\n");
Alexandre Julliard23946ad1997-06-16 17:43:53 +000076 printf(" -t \t\toutput files by point size instead of pixel height\n");
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000077 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
78 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
79 exit(-1);
80}
81
Dmitry Timoshkovfef71862000-07-25 12:25:40 +000082/* convert little-endian value to the local format */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000083
84int return_data_value(enum data_types dtype, void * pChr)
85{
86int ret_val = 0;
87
88 switch(dtype) {
Vincent Béron9a624912002-05-31 23:06:46 +000089 case (dfChar):
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000090 ret_val = (int) *(unsigned char *)pChr;
91 break;
Vincent Béron9a624912002-05-31 23:06:46 +000092
93 case(dfShort):
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000094 ret_val = *(unsigned char *)pChr;
95 ret_val += (*((unsigned char *)pChr + 1) << 8);
96 break;
Vincent Béron9a624912002-05-31 23:06:46 +000097
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +000098 case(dfLong): {
99 int i;
100
101 for(i=3; i >= 0; i--) {
102 ret_val += *((unsigned char *)pChr + i) << (8*i);
103 }
104 break;
105 }
Alexandre Julliarda11d7b11998-03-01 20:05:02 +0000106 case(dfString):
107 break;
Vincent Béron9a624912002-05-31 23:06:46 +0000108 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000109 return ret_val;
110}
111
112int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
113{
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000114long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000115char* lpChar;
116
117 if( !g_lpstrFileName )
118 {
Vincent Béron9a624912002-05-31 23:06:46 +0000119 if( !l_nameoffset ||
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000120 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000121 return ERROR_DATA;
122 lpChar = (char*)(file_buffer + l_nameoffset);
123 }
124 else lpChar = g_lpstrFileName;
125
126 strcpy( name, lpChar );
127
Vincent Béron9a624912002-05-31 23:06:46 +0000128 while( (lpChar = strchr( name, ' ')) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000129 *lpChar = '-';
130
131 /* construct a filename from the font typeface, slant, weight, and size */
132
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000133 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000134 else strcat(name, "_r" );
135
136 lpChar = name + strlen( name );
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000137 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
138 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
139 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000140 return 0;
141}
142
143/* parse FONT resource and write .bdf file */
144
145int parse_fnt_data(unsigned char* file_buffer, int length)
146{
Vincent Béron9a624912002-05-31 23:06:46 +0000147 fnt_fontS cpe_font_struct;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000148 int ic=0, t;
149
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000150 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000151
152 /* check font header */
153
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000154 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000155 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
156
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000157 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
158 if (t & 1)
159 {
160 fprintf(stderr, "Vector fonts not supported\n");
161 return ERROR_DATA;
162 }
163
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000164 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000165 if( t > length ) return ERROR_SIZE;
166 else
Vincent Béron9a624912002-05-31 23:06:46 +0000167 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000168 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
169
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000170 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
Vincent Béron9a624912002-05-31 23:06:46 +0000171 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000172 int l_len = l_lchar - l_fchar + 1;
173 int l_ptr = sizeof(fnt_hdrS);
174
175 /* some fields were introduced for Windows 3.x fonts */
176 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
177 l_ptr -= 30;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000178
179 /* malloc size = (# chars) * sizeof(WinCharS) */
180
Vincent Béron9a624912002-05-31 23:06:46 +0000181 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000182 return ERROR_MEMORY;
183
184 /* NOW, convert them all to UNIX (lton) notation... */
185
186 for(ic=0; ic < l_len; ic++) {
187 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
188 l_ptr += 2; /* bump by sizeof(short) */
189
190
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000191 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
Vincent Béron9a624912002-05-31 23:06:46 +0000192 cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000193 return_data_value(dfShort, &file_buffer[l_ptr]);
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000194 l_ptr += 2; /* bump by sizeof(short) */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000195 }
196 else { /* Windows Version 3.0 type font */
Vincent Béron9a624912002-05-31 23:06:46 +0000197 cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000198 return_data_value(dfLong, &file_buffer[l_ptr]);
199 l_ptr += 4; /* bump by sizeof(long) */
200 }
201 }
202 t = dump_bdf(&cpe_font_struct, file_buffer);
203 free( cpe_font_struct.dfCharTable );
204 }
205 return t;
206}
207
208int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
209{
210 FILE* fp;
211 int ic;
Vincent Béron9a624912002-05-31 23:06:46 +0000212 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
213 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000214
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000215 int l_len = l_lchar-l_fchar + 1,
Vincent Béron9a624912002-05-31 23:06:46 +0000216 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000217 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000218 char l_filename[256];
219
220 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
221 return ic;
222
Vincent Béron9a624912002-05-31 23:06:46 +0000223 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000224 {
225 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
226 return ERROR_FILE;
227 }
228
Alexandre Julliard02e90081998-01-04 17:49:09 +0000229 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
230 if (ic) return (ic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000231
232 /* NOW, convert all chars to UNIX (lton) notation... */
233
234 for(ic=0; ic < l_len; ic++) {
235 int rowidx, l_span, /* how many char-cols wide is char? */
236 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
237
Vincent Béron9a624912002-05-31 23:06:46 +0000238 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000239
240 fprintf(fp, "STARTCHAR %d \n", ic);
241 fprintf(fp, "ENCODING %d\n", l_fchar);
Vincent Béron9a624912002-05-31 23:06:46 +0000242 fprintf(fp, "SWIDTH %d %d \n",
243 cpe_font_struct->dfCharTable[ic].charWidth*1000,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000244 0);
245
Vincent Béron9a624912002-05-31 23:06:46 +0000246 fprintf(fp, "DWIDTH %d %d \n",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000247 cpe_font_struct->dfCharTable[ic].charWidth, 0);
248
249 fprintf(fp, "BBX %d %d %d %d\n",
Vincent Béron9a624912002-05-31 23:06:46 +0000250 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000251 l_ascent - l_hgt);
252
253 fprintf(fp, "BITMAP\n");
254 for(rowidx=0; rowidx < l_hgt; rowidx++) {
255 switch(l_span) {
256 case(0): /* 1-7 pixels wide font */
257 {
258 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
259 break;
260 }
Vincent Béron9a624912002-05-31 23:06:46 +0000261
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000262 case(1): /* 8-15 pixels wide font */
263 {
Vincent Béron9a624912002-05-31 23:06:46 +0000264 fprintf(fp, "%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000265 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
266 fprintf(fp, "\n");
267 break;
268 }
269
270 case(2): /* 16-23 pixels wide font */
271 {
Vincent Béron9a624912002-05-31 23:06:46 +0000272 fprintf(fp, "%02X%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000273 file_buffer[l_idx+rowidx],
274 file_buffer[l_idx+l_hgt+rowidx],
275 file_buffer[l_idx+(2*l_hgt)+rowidx]);
276 fprintf(fp, "\n");
277 break;
278 }
279
280 case(3): /* 24-31 pixels wide font */
281 {
Vincent Béron9a624912002-05-31 23:06:46 +0000282 fprintf(fp, "%02X%02X%02X%02X",
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000283 file_buffer[l_idx+rowidx],
284 file_buffer[l_idx+l_hgt+rowidx],
285 file_buffer[l_idx+(2*l_hgt)+rowidx],
286 file_buffer[l_idx+(3*l_hgt)+rowidx]);
287 fprintf(fp, "\n");
288 break;
289 }
290 case(4): /* 32-39 */
291 {
292 fprintf(fp, "%02X%02X%02X%02X%02X",
293 file_buffer[l_idx+rowidx],
294 file_buffer[l_idx+l_hgt+rowidx],
295 file_buffer[l_idx+(2*l_hgt)+rowidx],
296 file_buffer[l_idx+(3*l_hgt)+rowidx],
297 file_buffer[l_idx+(4*l_hgt)+rowidx]);
298 fprintf(fp, "\n");
299 break;
300 }
301 default:
302 fclose(fp);
303 unlink(l_filename);
304 return ERROR_DATA;
305 }
306 }
307 fprintf(fp, "ENDCHAR\n");
308
309 l_fchar++; /* Go to next one */
310 }
311fprintf(fp, "ENDFONT\n");
312fclose(fp);
313return 0;
314}
315
316
317int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
318{
Vincent Béron9a624912002-05-31 23:06:46 +0000319int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000320 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000321int l_len = l_lchar - l_fchar + 1;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000322long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
323int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
324int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000325
326 fprintf(fs, "STARTFONT 2.1\n");
327
328 /* Compose font name */
329
Vincent Béron9a624912002-05-31 23:06:46 +0000330 if( l_nameoffset &&
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000331 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000332 {
333 int dpi, point_size;
334 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000335 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000336
Vincent Béron9a624912002-05-31 23:06:46 +0000337 while((lpChar = strchr(lpFace, '-')) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000338 *lpChar = ' ';
339
340 fprintf(fs, "FONT -windows-%s-", lpFace );
341
342 if( tmWeight == 0 ) /* weight */
343 fputs("medium-", fs);
344 else if( tmWeight <= FW_LIGHT )
345 fputs("light-", fs);
346 else if( tmWeight <= FW_MEDIUM )
347 fputs("medium-", fs);
348 else if( tmWeight <= FW_DEMIBOLD )
349 fputs("demibold-", fs);
350 else if( tmWeight <= FW_BOLD )
351 fputs("bold-", fs);
352 else fputs("black-", fs);
353
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000354 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000355 fputs("i-", fs);
356 else fputs("r-", fs);
357
358 /* style */
359
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000360 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000361 fputs("normal-sans-", fs);
362 else fputs("normal--", fs); /* still can be -sans */
363
364 /* y extents */
365
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000366 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000367 dpi = (l_cellheight * 720) / point_size;
368
Vincent Béron9a624912002-05-31 23:06:46 +0000369 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
Bill Medland4eb22c62001-07-18 20:00:44 +0000370 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
371 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000372
373 /* spacing */
374
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000375 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000376 else fputs("p-", fs);
Vincent Béron9a624912002-05-31 23:06:46 +0000377
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000378 /* average width */
379
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000380 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000381
382 /* charset */
383
Stas Sergeev5c13c212000-07-10 13:04:08 +0000384 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
385 else
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000386 switch( cpe_font_struct->hdr.fi.dfCharSet )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000387 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000388 /* Microsoft just had to invent its own charsets! */
389
Pablo Saratxagab13f23e1998-12-09 14:51:48 +0000390 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000391 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
392 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
393 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
394 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
395 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
396 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
Vincent Béron9a624912002-05-31 23:06:46 +0000397 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000398 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000399 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
400 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
401
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000402 default:
Vincent Béron9a624912002-05-31 23:06:46 +0000403 case OEM_CHARSET:
404 fputs("Undefined charset, use -c option.\n", stderr);
405 return ERROR_DATA;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000406 }
407 }
408 else return ERROR_DATA;
409
Vincent Béron9a624912002-05-31 23:06:46 +0000410 fprintf(fs, "SIZE %d %d %d\n",
Bill Medland4eb22c62001-07-18 20:00:44 +0000411 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000412 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
413 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000414
415 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000416 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
417 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000418 0, l_ascent - l_cellheight );
419
420 fprintf(fs, "STARTPROPERTIES 4\n");
421
422 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
423 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
424 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000425 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
426 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000427
428 fprintf(fs, "ENDPROPERTIES\n");
429
430 fprintf(fs, "CHARS %d\n", l_len);
431 return 0;
432}
433
434
435
436void parse_options(int argc, char **argv)
437{
438 int i;
439
440 switch( argc )
441 {
442 case 2:
443 g_lpstrInputFile = argv[1];
444 break;
445
446 case 3:
447 case 4:
448 case 5:
449 case 6:
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000450 case 7:
451 case 8:
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000452 for( i = 1; i < argc - 1; i++ )
453 {
454 if( argv[i][0] != '-' ||
455 strlen(argv[i]) != 2 ) break;
456
Vincent Béron9a624912002-05-31 23:06:46 +0000457 if( argv[i][1] == 'c' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000458 g_lpstrCharSet = argv[i+1];
Vincent Béron9a624912002-05-31 23:06:46 +0000459 else
460 if( argv[i][1] == 'f' )
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000461 g_lpstrFileName = argv[i+1];
462 else
463 if( argv[i][1] == 't' )
464 {
465 g_outputPoints = 1;
466 continue;
467 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000468 else
469 usage();
470
471 i++;
Vincent Béron9a624912002-05-31 23:06:46 +0000472 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000473 if( i == argc - 1 )
474 {
475 g_lpstrInputFile = argv[i];
476 break;
477 }
478 default: usage();
479 }
Vincent Béron9a624912002-05-31 23:06:46 +0000480
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000481}
482
483/* read file data and return file type */
484
485int get_resource_table(int fd, unsigned char** lpdata, int fsize)
486{
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000487 IMAGE_DOS_HEADER mz_header;
488 IMAGE_OS2_HEADER ne_header;
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000489 long s, offset, size;
490 int retval;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000491
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000492 lseek( fd, 0, SEEK_SET );
493
Vincent Béron9a624912002-05-31 23:06:46 +0000494 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000495 return FILE_ERROR;
496
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000497 s = return_data_value(dfShort, &mz_header.e_magic);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000498
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000499 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000500 {
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000501 s = return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000502 lseek( fd, s, SEEK_SET );
503
504 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
505 return FILE_ERROR;
506
507 s = return_data_value(dfShort, &ne_header.ne_magic);
508
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000509 if( s == IMAGE_NT_SIGNATURE)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000510 {
511 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
Vincent Béron9a624912002-05-31 23:06:46 +0000512 return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000513 }
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000514 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000515
Alexandre Julliard180a0882000-04-18 11:58:24 +0000516 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
517 size = return_data_value(dfShort, &ne_header.ne_restab);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000518
519 if( size > fsize ) return FILE_ERROR;
520
521 size -= s;
Alexandre Julliard23946ad1997-06-16 17:43:53 +0000522 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000523
524 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
525 retval = FILE_DLL;
526 }
527 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
528 {
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000529 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000530
531 if( size != fsize ) return FILE_ERROR;
532 offset = 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000533 retval = FILE_FNT;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000534 }
535 else return FILE_ERROR;
536
537 *lpdata = (unsigned char*)malloc(size);
538
539 if( *lpdata )
540 {
541 lseek( fd, offset, SEEK_SET );
542 if( read(fd, *lpdata, size) != size )
543 { free( *lpdata ); *lpdata = NULL; }
544 }
545 return retval;
546}
547
548
549/* entry point */
550
551int main(int argc, char **argv)
552{
553 unsigned char* lpdata = NULL;
554 int fd;
555
556 parse_options( argc, argv);
557
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000558 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000559 {
560 int i;
561 struct stat file_stat;
562
563 fstat( fd, &file_stat);
564 i = get_resource_table( fd, &lpdata, file_stat.st_size );
565
566 switch(i)
567 {
568 case FILE_DLL:
569 if( lpdata )
570 {
571 int j, count = 0;
572 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
573 NE_NAMEINFO* pFontStorage = NULL;
574
Vincent Béron9a624912002-05-31 23:06:46 +0000575 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000576 {
577 j = return_data_value(dfShort, &pTInfo->count);
578 if( i == NE_RSCTYPE_FONT )
579 {
580 count = j;
581 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
Dmitry Timoshkovfef71862000-07-25 12:25:40 +0000582 break; /* found one */
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000583 }
584
585 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
586 }
587 if( pFontStorage && count )
588 {
589 unsigned short size_shift = return_data_value(dfShort, lpdata);
590 unsigned char* lpfont = NULL;
591 unsigned offset;
Dmitry Timoshkovc63d9802002-08-17 18:28:43 +0000592 int length;
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000593
594 for( j = 0; j < count; j++, pFontStorage++ )
595 {
596 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
597 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
Vincent Béron9a624912002-05-31 23:06:46 +0000598
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000599 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
600 {
601 fprintf(stderr, errorMemory );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000602 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000603 }
604
605 lseek( fd, offset, SEEK_SET );
606 if( read(fd, lpfont, length) != length )
607 {
608 fprintf(stderr, errorDLLRead );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000609 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000610 }
611
612 if( (i = parse_fnt_data( lpfont, length )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000613 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000614 fprintf(stderr, "%s%d\n", errorFontData, i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000615 exit(1);
616 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000617 }
618 free(lpfont); free(lpdata);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000619 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000620 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000621 else
622 {
623 fprintf(stderr, errorEmpty );
624 exit(1);
625 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000626 free( lpdata );
627 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000628 else
629 {
630 fprintf(stderr, errorDLLRead);
631 exit(1);
632 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000633 break;
634
635 case FILE_FNT:
636 if( lpdata )
637 {
638 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
Alexandre Julliard02e90081998-01-04 17:49:09 +0000639 {
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000640 fprintf(stderr, "%s%d\n", errorFontData, i );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000641 exit(1);
642 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000643 free( lpdata );
644 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000645 else
646 {
647 fprintf(stderr, errorFNTRead);
648 exit(1);
649 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000650 break;
651
652 case FILE_ERROR:
653 fprintf(stderr, errorFile );
Alexandre Julliard02e90081998-01-04 17:49:09 +0000654 exit(1);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000655 }
656 close(fd);
Alexandre Julliard02e90081998-01-04 17:49:09 +0000657 exit(0);
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000658 }
Alexandre Julliard02e90081998-01-04 17:49:09 +0000659 else
660 {
661 fprintf(stderr, errorOpenFile );
662 exit(1);
663 }
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000664}