Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 1 | /************************************************ |
| 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 Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 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 Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 23 | #include "config.h" |
Alexandre Julliard | 49b7fdc | 2005-08-03 21:25:10 +0000 | [diff] [blame] | 24 | #include "wine/port.h" |
Patrik Stridvall | 9633632 | 1999-10-24 22:13:47 +0000 | [diff] [blame] | 25 | |
| 26 | #ifdef HAVE_SYS_PARAM_H |
| 27 | # include <sys/param.h> |
| 28 | #endif |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
Dmitry Timoshkov | c63d980 | 2002-08-17 18:28:43 +0000 | [diff] [blame] | 34 | #ifdef HAVE_UNISTD_H |
| 35 | # include <unistd.h> |
| 36 | #endif |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 37 | #include <fcntl.h> |
Dmitry Timoshkov | c63d980 | 2002-08-17 18:28:43 +0000 | [diff] [blame] | 38 | #ifdef HAVE_IO_H |
| 39 | # include <io.h> |
| 40 | #endif |
| 41 | |
Alexandre Julliard | 1f220db | 2009-08-29 21:06:19 +0200 | [diff] [blame] | 42 | #include "windef.h" |
| 43 | #include "wingdi.h" |
| 44 | |
| 45 | enum 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 | |
| 55 | typedef 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 | |
| 92 | typedef struct |
| 93 | { |
| 94 | WORD offset; |
| 95 | WORD length; |
| 96 | WORD flags; |
| 97 | WORD id; |
| 98 | WORD handle; |
| 99 | WORD usage; |
| 100 | } NE_NAMEINFO; |
| 101 | |
| 102 | typedef 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 | |
| 140 | typedef 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 | |
| 148 | typedef struct WinCharStruct |
| 149 | { |
| 150 | unsigned int charWidth; |
| 151 | long charOffset; |
| 152 | } WinCharS; |
| 153 | |
| 154 | typedef 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 Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 166 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 167 | #define FILE_ERROR 0 |
| 168 | #define FILE_DLL 1 |
| 169 | #define FILE_FNT 2 |
| 170 | |
| 171 | /* global options */ |
| 172 | |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 173 | static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer); |
| 174 | static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer); |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 175 | |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 176 | static char* g_lpstrFileName = NULL; |
| 177 | static char* g_lpstrCharSet = NULL; |
| 178 | static char* g_lpstrInputFile = NULL; |
| 179 | static int g_outputPoints = 0; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 180 | |
| 181 | /* info */ |
| 182 | |
Eric Pouech | 763aff6 | 2004-12-06 16:44:32 +0000 | [diff] [blame] | 183 | static void usage(void) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 184 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 185 | printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n"); |
| 186 | printf(" -c charset\tcharset name for OEM_CHARSET fonts\n"); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 187 | printf(" -f basename\tbasic output filename\n"); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 188 | printf(" -t \t\toutput files by point size instead of pixel height\n"); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 189 | 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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 194 | /* convert little-endian value to the local format */ |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 195 | |
Eric Pouech | 763aff6 | 2004-12-06 16:44:32 +0000 | [diff] [blame] | 196 | static int return_data_value(enum data_types dtype, void * pChr) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 197 | { |
| 198 | int ret_val = 0; |
| 199 | |
| 200 | switch(dtype) { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 201 | case (dfChar): |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 202 | ret_val = (int) *(unsigned char *)pChr; |
| 203 | break; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 204 | |
| 205 | case(dfShort): |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 206 | ret_val = *(unsigned char *)pChr; |
| 207 | ret_val += (*((unsigned char *)pChr + 1) << 8); |
| 208 | break; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 209 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 210 | 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 Julliard | a11d7b1 | 1998-03-01 20:05:02 +0000 | [diff] [blame] | 218 | case(dfString): |
| 219 | break; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 220 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 221 | return ret_val; |
| 222 | } |
| 223 | |
Eric Pouech | 763aff6 | 2004-12-06 16:44:32 +0000 | [diff] [blame] | 224 | static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 225 | { |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 226 | long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 227 | char* lpChar; |
| 228 | |
| 229 | if( !g_lpstrFileName ) |
| 230 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 231 | if( !l_nameoffset || |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 232 | l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 233 | return ERROR_DATA; |
| 234 | lpChar = (char*)(file_buffer + l_nameoffset); |
| 235 | } |
| 236 | else lpChar = g_lpstrFileName; |
| 237 | |
| 238 | strcpy( name, lpChar ); |
| 239 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 240 | while( (lpChar = strchr( name, ' ')) ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 241 | *lpChar = '-'; |
| 242 | |
| 243 | /* construct a filename from the font typeface, slant, weight, and size */ |
| 244 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 245 | if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" ); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 246 | else strcat(name, "_r" ); |
| 247 | |
| 248 | lpChar = name + strlen( name ); |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 249 | 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 Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* parse FONT resource and write .bdf file */ |
| 256 | |
Eric Pouech | 763aff6 | 2004-12-06 16:44:32 +0000 | [diff] [blame] | 257 | static int parse_fnt_data(unsigned char* file_buffer, int length) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 258 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 259 | fnt_fontS cpe_font_struct; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 260 | int ic=0, t; |
| 261 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 262 | memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS)); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 263 | |
| 264 | /* check font header */ |
| 265 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 266 | t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 267 | if( t != 0x300 && t != 0x200) return ERROR_VERSION; |
| 268 | |
Dmitry Timoshkov | c63d980 | 2002-08-17 18:28:43 +0000 | [diff] [blame] | 269 | 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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 276 | t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 277 | if( t > length ) return ERROR_SIZE; |
| 278 | else |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 279 | { |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 280 | /* set up the charWidth/charOffset structure pairs (dfCharTable)... */ |
| 281 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 282 | int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar), |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 283 | l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar); |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 284 | 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 Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 290 | |
| 291 | /* malloc size = (# chars) * sizeof(WinCharS) */ |
| 292 | |
Marcin Baczyński | c7183e8 | 2010-01-06 23:38:35 +0100 | [diff] [blame] | 293 | if((cpe_font_struct.dfCharTable = calloc(sizeof(WinCharS), l_len)) == NULL) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 294 | 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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 303 | if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 304 | cpe_font_struct.dfCharTable[ic].charOffset = |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 305 | return_data_value(dfShort, &file_buffer[l_ptr]); |
Dmitry Timoshkov | c63d980 | 2002-08-17 18:28:43 +0000 | [diff] [blame] | 306 | l_ptr += 2; /* bump by sizeof(short) */ |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 307 | } |
| 308 | else { /* Windows Version 3.0 type font */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 309 | cpe_font_struct.dfCharTable[ic].charOffset = |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 310 | 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 Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 320 | static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 321 | { |
| 322 | FILE* fp; |
| 323 | int ic; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 324 | 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 Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 326 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 327 | int l_len = l_lchar-l_fchar + 1, |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 328 | l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight); |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 329 | int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 330 | char l_filename[256]; |
| 331 | |
| 332 | if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) ) |
| 333 | return ic; |
| 334 | |
Francois Gouget | a70fbae | 2008-12-08 09:25:18 +0100 | [diff] [blame] | 335 | if((fp = fopen(l_filename, "w")) == NULL) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 336 | { |
| 337 | fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename); |
| 338 | return ERROR_FILE; |
| 339 | } |
| 340 | |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 341 | ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer); |
Marcus Meissner | a306253 | 2009-08-08 10:44:09 +0200 | [diff] [blame] | 342 | if (ic) { |
| 343 | fclose(fp); |
| 344 | return (ic); |
| 345 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 346 | |
| 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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 353 | l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 354 | |
Francois Gouget | 6724655 | 2009-03-08 23:51:25 +0100 | [diff] [blame] | 355 | fprintf(fp, "STARTCHAR %d\n", ic); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 356 | fprintf(fp, "ENCODING %d\n", l_fchar); |
Francois Gouget | 6724655 | 2009-03-08 23:51:25 +0100 | [diff] [blame] | 357 | fprintf(fp, "SWIDTH %d %d\n", |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 358 | cpe_font_struct->dfCharTable[ic].charWidth*1000, |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 359 | 0); |
| 360 | |
Francois Gouget | 6724655 | 2009-03-08 23:51:25 +0100 | [diff] [blame] | 361 | fprintf(fp, "DWIDTH %d %d\n", |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 362 | cpe_font_struct->dfCharTable[ic].charWidth, 0); |
| 363 | |
| 364 | fprintf(fp, "BBX %d %d %d %d\n", |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 365 | cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0, |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 366 | 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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 376 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 377 | case(1): /* 8-15 pixels wide font */ |
| 378 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 379 | fprintf(fp, "%02X%02X", |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 380 | (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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 387 | fprintf(fp, "%02X%02X%02X", |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 388 | 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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 397 | fprintf(fp, "%02X%02X%02X%02X", |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 398 | 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 | } |
| 426 | fprintf(fp, "ENDFONT\n"); |
| 427 | fclose(fp); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 432 | static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 433 | { |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 434 | int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar), |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 435 | l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 436 | int l_len = l_lchar - l_fchar + 1; |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 437 | long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace); |
| 438 | int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight); |
| 439 | int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 440 | |
| 441 | fprintf(fs, "STARTFONT 2.1\n"); |
| 442 | |
| 443 | /* Compose font name */ |
| 444 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 445 | if( l_nameoffset && |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 446 | l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 447 | { |
Gerald Pfeifer | 878df9a | 2010-04-23 20:42:32 +0200 | [diff] [blame] | 448 | int point_size; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 449 | char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar; |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 450 | short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 451 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 452 | while((lpChar = strchr(lpFace, '-')) ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 453 | *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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 469 | if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */ |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 470 | fputs("i-", fs); |
| 471 | else fputs("r-", fs); |
| 472 | |
| 473 | /* style */ |
| 474 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 475 | if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 476 | fputs("normal-sans-", fs); |
| 477 | else fputs("normal--", fs); /* still can be -sans */ |
| 478 | |
| 479 | /* y extents */ |
| 480 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 481 | point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 482 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 483 | fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size, |
Bill Medland | 4eb22c6 | 2001-07-18 20:00:44 +0000 | [diff] [blame] | 484 | return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes), |
| 485 | return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 486 | |
| 487 | /* spacing */ |
| 488 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 489 | if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs); |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 490 | else fputs("p-", fs); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 491 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 492 | /* average width */ |
| 493 | |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 494 | fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) ); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 495 | |
| 496 | /* charset */ |
| 497 | |
Stas Sergeev | 5c13c21 | 2000-07-10 13:04:08 +0000 | [diff] [blame] | 498 | if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet); |
| 499 | else |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 500 | switch( cpe_font_struct->hdr.fi.dfCharSet ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 501 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 502 | /* Microsoft just had to invent its own charsets! */ |
| 503 | |
Pablo Saratxaga | b13f23e | 1998-12-09 14:51:48 +0000 | [diff] [blame] | 504 | case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 505 | 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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 511 | case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 512 | case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 513 | case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break; |
| 514 | case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break; |
| 515 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 516 | default: |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 517 | case OEM_CHARSET: |
| 518 | fputs("Undefined charset, use -c option.\n", stderr); |
| 519 | return ERROR_DATA; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | else return ERROR_DATA; |
| 523 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 524 | fprintf(fs, "SIZE %d %d %d\n", |
Bill Medland | 4eb22c6 | 2001-07-18 20:00:44 +0000 | [diff] [blame] | 525 | return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ), |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 526 | return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes), |
| 527 | return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */ |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 528 | |
| 529 | fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n", |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 530 | return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth), |
| 531 | return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight), |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 532 | 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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 539 | 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 Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 541 | |
| 542 | fprintf(fs, "ENDPROPERTIES\n"); |
| 543 | |
| 544 | fprintf(fs, "CHARS %d\n", l_len); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | |
Eric Pouech | 763aff6 | 2004-12-06 16:44:32 +0000 | [diff] [blame] | 550 | static void parse_options(int argc, char **argv) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 551 | { |
| 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 Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 564 | case 7: |
| 565 | case 8: |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 566 | for( i = 1; i < argc - 1; i++ ) |
| 567 | { |
| 568 | if( argv[i][0] != '-' || |
| 569 | strlen(argv[i]) != 2 ) break; |
| 570 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 571 | if( argv[i][1] == 'c' ) |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 572 | g_lpstrCharSet = argv[i+1]; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 573 | else |
| 574 | if( argv[i][1] == 'f' ) |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 575 | g_lpstrFileName = argv[i+1]; |
| 576 | else |
| 577 | if( argv[i][1] == 't' ) |
| 578 | { |
| 579 | g_outputPoints = 1; |
| 580 | continue; |
| 581 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 582 | else |
| 583 | usage(); |
| 584 | |
| 585 | i++; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 586 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 587 | if( i == argc - 1 ) |
| 588 | { |
| 589 | g_lpstrInputFile = argv[i]; |
| 590 | break; |
| 591 | } |
| 592 | default: usage(); |
| 593 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 594 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* read file data and return file type */ |
| 598 | |
Eric Pouech | 763aff6 | 2004-12-06 16:44:32 +0000 | [diff] [blame] | 599 | static int get_resource_table(int fd, unsigned char** lpdata, int fsize) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 600 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 601 | IMAGE_DOS_HEADER mz_header; |
| 602 | IMAGE_OS2_HEADER ne_header; |
Dmitry Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 603 | long s, offset, size; |
| 604 | int retval; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 605 | |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 606 | lseek( fd, 0, SEEK_SET ); |
| 607 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 608 | if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 609 | return FILE_ERROR; |
| 610 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 611 | s = return_data_value(dfShort, &mz_header.e_magic); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 612 | |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 613 | if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */ |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 614 | { |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 615 | s = return_data_value(dfShort, &mz_header.e_lfanew); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 616 | 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 Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 623 | if( s == IMAGE_NT_SIGNATURE) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 624 | { |
| 625 | fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n"); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 626 | return FILE_ERROR; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 627 | } |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 628 | else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 629 | |
Alexandre Julliard | 180a088 | 2000-04-18 11:58:24 +0000 | [diff] [blame] | 630 | s = return_data_value(dfShort, &ne_header.ne_rsrctab); |
| 631 | size = return_data_value(dfShort, &ne_header.ne_restab); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 632 | |
| 633 | if( size > fsize ) return FILE_ERROR; |
| 634 | |
| 635 | size -= s; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 636 | offset = s + return_data_value(dfShort, &mz_header.e_lfanew); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 637 | |
| 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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 643 | size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 644 | |
| 645 | if( size != fsize ) return FILE_ERROR; |
| 646 | offset = 0; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 647 | retval = FILE_FNT; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 648 | } |
| 649 | else return FILE_ERROR; |
| 650 | |
Marcin Baczyński | c7183e8 | 2010-01-06 23:38:35 +0100 | [diff] [blame] | 651 | *lpdata = malloc(size); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 652 | |
| 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 | |
| 665 | int main(int argc, char **argv) |
| 666 | { |
| 667 | unsigned char* lpdata = NULL; |
| 668 | int fd; |
| 669 | |
| 670 | parse_options( argc, argv); |
| 671 | |
Marcus Meissner | a306253 | 2009-08-08 10:44:09 +0200 | [diff] [blame] | 672 | if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 673 | { |
| 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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 689 | while( (i = return_data_value(dfShort, &pTInfo->type_id)) ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 690 | { |
| 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 Timoshkov | fef7186 | 2000-07-25 12:25:40 +0000 | [diff] [blame] | 696 | break; /* found one */ |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 697 | } |
| 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 Timoshkov | c63d980 | 2002-08-17 18:28:43 +0000 | [diff] [blame] | 706 | int length; |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 707 | |
| 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éron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 712 | |
Marcin Baczyński | c7183e8 | 2010-01-06 23:38:35 +0100 | [diff] [blame] | 713 | if( !(lpfont = realloc( lpfont, length )) ) |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 714 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 715 | fprintf(stderr, "Memory allocation error.\n" ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 716 | exit(1); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | lseek( fd, offset, SEEK_SET ); |
| 720 | if( read(fd, lpfont, length) != length ) |
| 721 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 722 | fprintf(stderr, "Unable to read Windows DLL.\n" ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 723 | exit(1); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if( (i = parse_fnt_data( lpfont, length )) ) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 727 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 728 | fprintf(stderr, "Unable to parse font data: Error %d\n", i ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 729 | exit(1); |
| 730 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 731 | } |
| 732 | free(lpfont); free(lpdata); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 733 | exit(0); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 734 | } |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 735 | else |
| 736 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 737 | fprintf(stderr, "No fonts found.\n" ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 738 | exit(1); |
| 739 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 740 | free( lpdata ); |
| 741 | } |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 742 | else |
| 743 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 744 | fprintf(stderr, "Unable to read Windows DLL.\n" ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 745 | exit(1); |
| 746 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 747 | break; |
| 748 | |
| 749 | case FILE_FNT: |
| 750 | if( lpdata ) |
| 751 | { |
| 752 | if( (i = parse_fnt_data( lpdata, file_stat.st_size )) ) |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 753 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 754 | fprintf(stderr, "Unable to parse font data: Error %d\n", i ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 755 | exit(1); |
| 756 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 757 | free( lpdata ); |
| 758 | } |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 759 | else |
| 760 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 761 | fprintf(stderr, "Unable to read .FNT file.\n" ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 762 | exit(1); |
| 763 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 764 | break; |
| 765 | |
| 766 | case FILE_ERROR: |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 767 | fprintf(stderr, "Corrupt or invalid file.\n" ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 768 | exit(1); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 769 | } |
| 770 | close(fd); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 771 | exit(0); |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 772 | } |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 773 | else |
| 774 | { |
Alexandre Julliard | a5f1e9b | 2008-12-05 12:36:05 +0100 | [diff] [blame] | 775 | fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile ); |
Alexandre Julliard | 02e9008 | 1998-01-04 17:49:09 +0000 | [diff] [blame] | 776 | exit(1); |
| 777 | } |
Alexandre Julliard | 7e6ae4b | 1996-12-08 19:25:27 +0000 | [diff] [blame] | 778 | } |