ntdll: Reimplement DbgPrint* using DBG_PRINTEXCEPTION_C.
[wine.git] / dlls / wineps.drv / ppd.c
blob03267c4370234ca0ed3845b054044e516c37376f
1 /* PostScript Printer Description (PPD) file parser
3 * See http://www.adobe.com/supportservice/devrelations/PDFS/TN/5003.PPD_Spec_v4.3.pdf
5 * Copyright 1998 Huw D M Davies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <string.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <locale.h>
27 #include <assert.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "psdrv.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36 typedef struct {
37 char *key;
38 char *option;
39 char *opttrans;
40 char *value;
41 char *valtrans;
42 } PPDTuple;
44 struct map_context
46 const char *ptr, *pos, *end;
49 /* map of page names in ppd file to Windows paper constants */
51 static const struct {
52 const char *PSName;
53 WORD WinPage;
54 } PageTrans[] = {
55 {"10x11", DMPAPER_10X11},
56 {"10x14", DMPAPER_10X14},
57 {"11x17", DMPAPER_11X17}, /* not in Adobe PPD file spec */
58 {"12x11", DMPAPER_12X11},
59 {"15x11", DMPAPER_15X11},
60 {"9x11", DMPAPER_9X11},
61 {"A2", DMPAPER_A2},
62 {"A3", DMPAPER_A3},
63 {"A3.Transverse", DMPAPER_A3_TRANSVERSE},
64 {"A3Extra", DMPAPER_A3_EXTRA},
65 {"A3Extra.Transverse", DMPAPER_A3_EXTRA_TRANSVERSE},
66 {"A3Rotated", DMPAPER_A3_ROTATED},
67 {"A4", DMPAPER_A4},
68 {"A4.Transverse", DMPAPER_A4_TRANSVERSE},
69 {"A4Extra", DMPAPER_A4_EXTRA},
70 {"A4Plus", DMPAPER_A4_PLUS},
71 {"A4Rotated", DMPAPER_A4_ROTATED},
72 {"A4Small", DMPAPER_A4SMALL},
73 {"A5", DMPAPER_A5},
74 {"A5.Transverse", DMPAPER_A5_TRANSVERSE},
75 {"A5Extra", DMPAPER_A5_EXTRA},
76 {"A5Rotated", DMPAPER_A5_ROTATED},
77 {"A6", DMPAPER_A6},
78 {"A6Rotated", DMPAPER_A6_ROTATED},
79 {"ARCHC", DMPAPER_CSHEET},
80 {"ARCHD", DMPAPER_DSHEET},
81 {"ARCHE", DMPAPER_ESHEET},
82 {"B4", DMPAPER_B4},
83 {"B4Rotated", DMPAPER_B4_JIS_ROTATED},
84 {"B5", DMPAPER_B5},
85 {"B5.Transverse", DMPAPER_B5_TRANSVERSE},
86 {"B5Rotated", DMPAPER_B5_JIS_ROTATED},
87 {"B6", DMPAPER_B6_JIS},
88 {"B6Rotated", DMPAPER_B6_JIS_ROTATED},
89 {"C4", DMPAPER_ENV_C4}, /* use EnvC4 */
90 {"C5", DMPAPER_ENV_C5}, /* use EnvC5 */
91 {"C6", DMPAPER_ENV_C6}, /* use EnvC6 */
92 {"Comm10", DMPAPER_ENV_10}, /* use Env10 */
93 {"DL", DMPAPER_ENV_DL}, /* use EnvDL */
94 {"DoublePostcard", DMPAPER_DBL_JAPANESE_POSTCARD},
95 {"DoublePostcardRotated", DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED},
96 {"Env10", DMPAPER_ENV_10},
97 {"Env11", DMPAPER_ENV_11},
98 {"Env12", DMPAPER_ENV_12},
99 {"Env14", DMPAPER_ENV_14},
100 {"Env9", DMPAPER_ENV_9},
101 {"EnvC3", DMPAPER_ENV_C3},
102 {"EnvC4", DMPAPER_ENV_C4},
103 {"EnvC5", DMPAPER_ENV_C5},
104 {"EnvC6", DMPAPER_ENV_C6},
105 {"EnvC65", DMPAPER_ENV_C65},
106 {"EnvChou3", DMPAPER_JENV_CHOU3},
107 {"EnvChou3Rotated", DMPAPER_JENV_CHOU3_ROTATED},
108 {"EnvChou4", DMPAPER_JENV_CHOU4},
109 {"EnvChou4Rotated", DMPAPER_JENV_CHOU4_ROTATED},
110 {"EnvDL", DMPAPER_ENV_DL},
111 {"EnvISOB4", DMPAPER_ENV_B4},
112 {"EnvISOB5", DMPAPER_ENV_B5},
113 {"EnvISOB6", DMPAPER_ENV_B6},
114 {"EnvInvite", DMPAPER_ENV_INVITE},
115 {"EnvItalian", DMPAPER_ENV_ITALY},
116 {"EnvKaku2", DMPAPER_JENV_KAKU2},
117 {"EnvKaku2Rotated", DMPAPER_JENV_KAKU2_ROTATED},
118 {"EnvKaku3", DMPAPER_JENV_KAKU3},
119 {"EnvKaku3Rotated", DMPAPER_JENV_KAKU3_ROTATED},
120 {"EnvMonarch", DMPAPER_ENV_MONARCH},
121 {"EnvPRC1", DMPAPER_PENV_1},
122 {"EnvPRC10", DMPAPER_PENV_10},
123 {"EnvPRC10Rotated", DMPAPER_PENV_10_ROTATED},
124 {"EnvPRC1Rotated", DMPAPER_PENV_1_ROTATED},
125 {"EnvPRC2", DMPAPER_PENV_2},
126 {"EnvPRC2Rotated", DMPAPER_PENV_2_ROTATED},
127 {"EnvPRC3", DMPAPER_PENV_3},
128 {"EnvPRC3Rotated", DMPAPER_PENV_3_ROTATED},
129 {"EnvPRC4", DMPAPER_PENV_4},
130 {"EnvPRC4Rotated", DMPAPER_PENV_4_ROTATED},
131 {"EnvPRC5", DMPAPER_PENV_5},
132 {"EnvPRC5Rotated", DMPAPER_PENV_5_ROTATED},
133 {"EnvPRC6", DMPAPER_PENV_6},
134 {"EnvPRC6Rotated", DMPAPER_PENV_6_ROTATED},
135 {"EnvPRC7", DMPAPER_PENV_7},
136 {"EnvPRC7Rotated", DMPAPER_PENV_7_ROTATED},
137 {"EnvPRC8", DMPAPER_PENV_8},
138 {"EnvPRC8Rotated", DMPAPER_PENV_8_ROTATED},
139 {"EnvPRC9", DMPAPER_PENV_9},
140 {"EnvPRC9Rotated", DMPAPER_PENV_9_ROTATED},
141 {"EnvPersonal", DMPAPER_ENV_PERSONAL},
142 {"EnvYou4", DMPAPER_JENV_YOU4},
143 {"EnvYou4Rotated", DMPAPER_JENV_YOU4_ROTATED},
144 {"Executive", DMPAPER_EXECUTIVE},
145 {"FanFoldGerman", DMPAPER_FANFOLD_STD_GERMAN},
146 {"FanFoldGermanLegal", DMPAPER_FANFOLD_LGL_GERMAN},
147 {"FanFoldUS", DMPAPER_FANFOLD_US},
148 {"Folio", DMPAPER_FOLIO},
149 {"ISOB4", DMPAPER_ISO_B4},
150 {"ISOB5Extra", DMPAPER_B5_EXTRA},
151 {"Ledger", DMPAPER_LEDGER},
152 {"Legal", DMPAPER_LEGAL},
153 {"LegalExtra", DMPAPER_LEGAL_EXTRA},
154 {"Letter", DMPAPER_LETTER},
155 {"Letter.Transverse", DMPAPER_LETTER_TRANSVERSE},
156 {"LetterExtra", DMPAPER_LETTER_EXTRA},
157 {"LetterExtra.Transverse", DMPAPER_LETTER_EXTRA_TRANSVERSE},
158 {"LetterPlus", DMPAPER_LETTER_PLUS},
159 {"LetterRotated", DMPAPER_LETTER_ROTATED},
160 {"LetterSmall", DMPAPER_LETTERSMALL},
161 {"Monarch", DMPAPER_ENV_MONARCH}, /* use EnvMonarch */
162 {"Note", DMPAPER_NOTE},
163 {"PRC16K", DMPAPER_P16K},
164 {"PRC16KRotated", DMPAPER_P16K_ROTATED},
165 {"PRC32K", DMPAPER_P32K},
166 {"PRC32KBig", DMPAPER_P32KBIG},
167 {"PRC32KBigRotated", DMPAPER_P32KBIG_ROTATED},
168 {"PRC32KRotated", DMPAPER_P32K_ROTATED},
169 {"Postcard", DMPAPER_JAPANESE_POSTCARD},
170 {"PostcardRotated", DMPAPER_JAPANESE_POSTCARD_ROTATED},
171 {"Quarto", DMPAPER_QUARTO},
172 {"Statement", DMPAPER_STATEMENT},
173 {"SuperA", DMPAPER_A_PLUS},
174 {"SuperB", DMPAPER_B_PLUS},
175 {"Tabloid", DMPAPER_TABLOID},
176 {"TabloidExtra", DMPAPER_TABLOID_EXTRA},
177 {NULL, 0}
180 /***********************************************************************
182 * PSDRV_PPDDecodeHex
184 * Copies str into a newly allocated string from the process heap substituting
185 * hex strings enclosed in '<' and '>' for their byte codes.
188 static char *PSDRV_PPDDecodeHex(char *str)
190 char *buf, *in, *out;
191 BOOL inhex = FALSE;
193 buf = HeapAlloc(PSDRV_Heap, 0, strlen(str) + 1);
194 if(!buf)
195 return NULL;
197 for(in = str, out = buf; *in; in++) {
198 if(!inhex) {
199 if(*in != '<')
200 *out++ = *in;
201 else
202 inhex = TRUE;
203 } else {
204 if(*in == '>') {
205 inhex = FALSE;
206 continue;
208 else if(isspace(*in))
209 continue;
210 else {
211 int i;
212 if(!isxdigit(*in) || !isxdigit(*(in + 1))) {
213 ERR("Invalid hex char in hex string\n");
214 HeapFree(PSDRV_Heap, 0, buf);
215 return NULL;
217 *out = 0;
218 for(i = 0; i < 2; i++) {
219 if(isdigit(*(in + i)))
220 *out |= (*(in + i) - '0') << ((1-i) * 4);
221 else
222 *out |= (toupper(*(in + i)) - 'A' + 10) << ((1-i) * 4);
224 out++;
225 in++;
229 *out = '\0';
230 return buf;
234 /***********************************************************************
236 * PSDRV_PPDGetTransValue
239 static BOOL PSDRV_PPDGetTransValue(const char *start, PPDTuple *tuple)
241 char *buf;
242 const char *end;
244 end = strpbrk(start, "\r\n");
245 if(end == start) return FALSE;
246 if(!end) end = start + strlen(start);
247 buf = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
248 memcpy(buf, start, end - start);
249 *(buf + (end - start)) = '\0';
250 tuple->valtrans = PSDRV_PPDDecodeHex(buf);
251 HeapFree( PSDRV_Heap, 0, buf );
252 return TRUE;
255 static BOOL get_line( char *buf, int size, struct map_context *ctx )
257 int i;
258 if (ctx->pos > ctx->end) return FALSE;
260 for (i = 0; i < size - 1; i++)
262 if (ctx->pos > ctx->end) break;
263 buf[i] = *ctx->pos++;
265 /* \r\n -> \n */
266 if (buf[i] == '\r' && ctx->pos <= ctx->end && *ctx->pos == '\n')
268 ctx->pos++;
269 buf[i] = '\n';
272 if (buf[i] == '\n' || buf[i] == '\r')
274 i++;
275 break;
278 buf[i] = '\0';
279 return TRUE;
282 /***********************************************************************
284 * PSDRV_PPDGetInvocationValue
286 * Passed string that should be surrounded by `"'s, return string allocated
287 * from process heap.
289 static BOOL PSDRV_PPDGetInvocationValue(struct map_context *ctx, PPDTuple *tuple)
291 const char *start;
292 char *buf, line[257];
294 assert( *ctx->pos == '"' );
296 ctx->pos++;
297 for (start = ctx->pos; ctx->pos <= ctx->end; ctx->pos++)
298 if (*ctx->pos == '"') break;
299 if (ctx->pos > ctx->end) return FALSE;
300 ctx->pos++;
302 buf = HeapAlloc( PSDRV_Heap, 0, ctx->pos - start );
303 memcpy( buf, start, ctx->pos - start - 1 );
304 buf[ctx->pos - start - 1] = '\0';
305 tuple->value = buf;
307 if (get_line( line, sizeof(line), ctx ))
309 start = strchr( line, '/' );
310 if (start) return PSDRV_PPDGetTransValue( start + 1, tuple );
312 return TRUE;
316 /***********************************************************************
318 * PSDRV_PPDGetQuotedValue
320 * Passed string that should be surrounded by `"'s. Expand <xx> as hex
321 * return string allocated from process heap.
323 static BOOL PSDRV_PPDGetQuotedValue(struct map_context *ctx, PPDTuple *tuple)
325 char *buf;
327 if(!PSDRV_PPDGetInvocationValue(ctx, tuple))
328 return FALSE;
329 buf = PSDRV_PPDDecodeHex(tuple->value);
330 HeapFree(PSDRV_Heap, 0, tuple->value);
331 tuple->value = buf;
332 return TRUE;
336 /***********************************************************************
338 * PSDRV_PPDGetStringValue
340 * Just strip leading white space.
342 static BOOL PSDRV_PPDGetStringValue(char *str, PPDTuple *tuple)
344 char *start = str, *end;
346 while(*start != '\0' && isspace(*start))
347 start++;
349 end = strpbrk(start, "/\r\n");
350 if(!end) end = start + strlen(start);
351 tuple->value = HeapAlloc( PSDRV_Heap, 0, (end - start) + 1 );
352 memcpy(tuple->value, start, end - start);
353 *(tuple->value + (end - start)) = '\0';
354 if(*end == '/')
355 PSDRV_PPDGetTransValue(end + 1, tuple);
356 return TRUE;
360 /***********************************************************************
362 * PSDRV_PPDSymbolValue
364 * Not implemented yet.
366 static BOOL PSDRV_PPDGetSymbolValue(char *pos, PPDTuple *tuple)
368 FIXME("Stub\n");
369 return FALSE;
373 /*********************************************************************
375 * PSDRV_PPDGetNextTuple
377 * Gets the next Keyword Option Value tuple from the file. Allocs space off
378 * the process heap which should be free()ed by the caller if not needed.
380 static BOOL PSDRV_PPDGetNextTuple(struct map_context *ctx, PPDTuple *tuple)
382 char line[257], *opt, *cp, *trans, *endkey;
383 BOOL gotoption;
384 struct map_context save;
386 start:
388 gotoption = TRUE;
389 opt = NULL;
390 memset(tuple, 0, sizeof(*tuple));
392 do {
393 save = *ctx;
394 if(!get_line(line, sizeof(line), ctx))
395 return FALSE;
396 if(line[0] == '*' && line[1] != '%' && strncmp(line, "*End", 4))
397 break;
398 } while(1);
400 cp = line + strlen(line) - 1;
401 if (*cp != '\n' && *cp != '\r')
403 ERR("Line too long.\n");
404 goto start;
407 for(cp = line; !isspace(*cp) && *cp != ':'; cp++)
410 endkey = cp;
411 while (isspace(*cp)) cp++;
412 if (*cp == ':') /* <key>: */
413 gotoption = FALSE;
414 else /* <key> <option> */
415 opt = cp;
417 tuple->key = HeapAlloc( PSDRV_Heap, 0, endkey - line + 1 );
418 if(!tuple->key) return FALSE;
420 memcpy(tuple->key, line, endkey - line);
421 tuple->key[endkey - line] = '\0';
423 if(gotoption) { /* opt points to 1st non-space character of the option */
424 cp = strpbrk(opt, ":/");
425 if(!cp) {
426 ERR("Error in line '%s'?\n", line);
427 HeapFree(GetProcessHeap(), 0, tuple->key);
428 goto start;
430 tuple->option = HeapAlloc( PSDRV_Heap, 0, cp - opt + 1 );
431 if(!tuple->option) return FALSE;
432 memcpy(tuple->option, opt, cp - opt);
433 tuple->option[cp - opt] = '\0';
434 if(*cp == '/') {
435 char *buf;
436 trans = cp + 1;
437 cp = strchr(trans, ':');
438 if(!cp) {
439 ERR("Error in line '%s'?\n", line);
440 HeapFree(GetProcessHeap(), 0, tuple->option);
441 HeapFree(GetProcessHeap(), 0, tuple->key);
442 goto start;
444 buf = HeapAlloc( PSDRV_Heap, 0, cp - trans + 1 );
445 if(!buf) return FALSE;
446 memcpy(buf, trans, cp - trans);
447 buf[cp - trans] = '\0';
448 tuple->opttrans = PSDRV_PPDDecodeHex(buf);
449 HeapFree( PSDRV_Heap, 0, buf );
453 /* cp should point to a ':', so we increment past it */
454 cp++;
456 while(isspace(*cp))
457 cp++;
459 switch(*cp) {
460 case '"':
461 /* update the context pos so that it points to the opening quote */
462 ctx->pos = save.pos + (cp - line);
463 if( (!gotoption && strncmp(tuple->key, "*?", 2) ) ||
464 !strncmp(tuple->key, "*JCL", 4))
465 PSDRV_PPDGetQuotedValue(ctx, tuple);
466 else
467 PSDRV_PPDGetInvocationValue(ctx, tuple);
468 break;
470 case '^':
471 PSDRV_PPDGetSymbolValue(cp, tuple);
472 break;
474 default:
475 PSDRV_PPDGetStringValue(cp, tuple);
477 return TRUE;
480 /*********************************************************************
481 * get_pagesize
483 * Searches ppd PageSize list to return entry matching name or optionally creates new
484 * entry which is appended to the list if name is not found.
486 static PAGESIZE *get_pagesize( PPD *ppd, char *name, BOOL create )
488 PAGESIZE *page;
490 LIST_FOR_EACH_ENTRY(page, &ppd->PageSizes, PAGESIZE, entry)
492 if(!strcmp(page->Name, name))
493 return page;
496 if (!create) return NULL;
498 page = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*page) );
499 list_add_tail(&ppd->PageSizes, &page->entry);
500 return page;
503 static DUPLEX *get_duplex( PPD *ppd, const char *name )
505 DUPLEX *duplex;
507 LIST_FOR_EACH_ENTRY( duplex, &ppd->Duplexes, DUPLEX, entry )
509 if (!strcmp( duplex->Name, name ))
510 return duplex;
513 return NULL;
516 /**********************************************************************
518 * PSDRV_PPDGetWord
520 * Returns ptr allocated from heap to first word in str. Strips leading spaces.
521 * Puts ptr to next word in next
523 static char *PSDRV_PPDGetWord(char *str, char **next)
525 char *start, *end, *ret;
527 start = str;
528 while(start && *start && isspace(*start))
529 start++;
530 if(!start || !*start) return FALSE;
532 end = start;
533 while(*end && !isspace(*end))
534 end++;
536 ret = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
537 memcpy(ret, start, end - start );
538 *(ret + (end - start)) = '\0';
540 while(*end && isspace(*end))
541 end++;
542 if(*end)
543 *next = end;
544 else
545 *next = NULL;
547 return ret;
550 /************************************************************
551 * parse_resolution
553 * Helper to extract x and y resolutions from a resolution entry.
554 * Returns TRUE on successful parsing, otherwise FALSE.
556 * The ppd spec says that entries can either be:
557 * "nnndpi"
558 * or
559 * "nnnxmmmdpi"
560 * in the former case return sz.cx == cx.cy == nnn.
562 * There are broken ppd files out there (notably from Samsung) that
563 * use the form "nnnmmmdpi", so we have to work harder to spot these.
564 * We use a transition from a zero to a non-zero digit as separator
565 * (and fail if we find more than one of these). We also don't bother
566 * trying to parse "dpi" in case that's missing.
568 static BOOL parse_resolution(const char *str, SIZE *sz)
570 int tmp[2];
571 int *cur;
572 BOOL had_zero;
573 const char *c;
575 if(sscanf(str, "%dx%d", tmp, tmp + 1) == 2)
577 sz->cx = tmp[0];
578 sz->cy = tmp[1];
579 return TRUE;
582 tmp[0] = 0;
583 tmp[1] = -1;
584 cur = tmp;
585 had_zero = FALSE;
586 for(c = str; isdigit(*c); c++)
588 if(!had_zero || *c == '0')
590 *cur *= 10;
591 *cur += *c - '0';
592 if(*c == '0')
593 had_zero = TRUE;
595 else if(cur != tmp)
596 return FALSE;
597 else
599 cur++;
600 *cur = *c - '0';
601 had_zero = FALSE;
604 if(tmp[0] == 0) return FALSE;
606 sz->cx = tmp[0];
607 if(tmp[1] != -1)
608 sz->cy = tmp[1];
609 else
610 sz->cy = sz->cx;
611 return TRUE;
614 /*******************************************************************************
615 * PSDRV_AddSlot
618 static BOOL PSDRV_AddSlot(PPD *ppd, LPCSTR szName, LPCSTR szFullName,
619 LPSTR szInvocationString, WORD wWinBin)
621 INPUTSLOT *slot = HeapAlloc( PSDRV_Heap, 0, sizeof(INPUTSLOT) );
622 if (!slot) return FALSE;
624 slot->Name = szName;
625 slot->FullName = szFullName;
626 slot->InvocationString = szInvocationString;
627 slot->WinBin = wWinBin;
629 list_add_tail( &ppd->InputSlots, &slot->entry );
630 return TRUE;
633 static char *get_ppd_override( HANDLE printer, const char *value )
635 DWORD err, type, needed;
636 char *data;
638 err = GetPrinterDataExA( printer, "PPD Overrides", value, &type, NULL, 0, &needed );
639 if (err != ERROR_MORE_DATA || type != REG_SZ || needed == 0) return NULL;
641 data = HeapAlloc( PSDRV_Heap, 0, needed );
642 if (data)
644 GetPrinterDataExA( printer, "PPD Overrides", value, &type, (BYTE*)data, needed, &needed );
645 TRACE( "got override %s: %s\n", value, data );
647 return data;
650 static BOOL map_file( const WCHAR *filename, struct map_context *c )
652 HANDLE file, mapping;
653 LARGE_INTEGER size;
655 file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
656 if (file == INVALID_HANDLE_VALUE) return FALSE;
658 if (!GetFileSizeEx( file, &size ) || size.u.HighPart)
660 CloseHandle( file );
661 return FALSE;
664 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
665 CloseHandle( file );
666 if (!mapping) return FALSE;
668 c->pos = c->ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
669 c->end = c->ptr + size.u.LowPart - 1;
670 CloseHandle( mapping );
671 return TRUE;
674 static void unmap_file( struct map_context *c )
676 UnmapViewOfFile( c->ptr );
679 /***********************************************************************
681 * PSDRV_ParsePPD
685 PPD *PSDRV_ParsePPD( const WCHAR *fname, HANDLE printer )
687 PPD *ppd;
688 PPDTuple tuple;
689 char *default_pagesize = NULL, *default_duplex = NULL;
690 char *def_pagesize_override = NULL, *def_duplex_override = NULL;
691 PAGESIZE *page, *page_cursor2;
692 struct map_context c;
693 WORD UserPageType = DMPAPER_USER;
694 WORD UserBinType = DMBIN_USER;
696 TRACE("file %s\n", debugstr_w(fname));
698 if (!map_file( fname, &c ))
700 WARN("Couldn't open ppd file %s\n", debugstr_w(fname));
701 return NULL;
704 ppd = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*ppd));
705 if(!ppd) {
706 ERR("Unable to allocate memory for ppd\n");
707 unmap_file( &c );
708 return NULL;
711 ppd->ColorDevice = CD_NotSpecified;
713 list_init( &ppd->Resolutions );
714 list_init( &ppd->InstalledFonts );
715 list_init( &ppd->PageSizes );
716 list_init( &ppd->Constraints );
717 list_init( &ppd->InputSlots );
718 list_init( &ppd->Duplexes );
720 /* Some gimp-print ppd files don't contain a DefaultResolution line
721 so default to 300 */
722 ppd->DefaultResolution = 300;
725 * The Windows PostScript drivers create the following "virtual bin" for
726 * every PostScript printer
728 if (!PSDRV_AddSlot( ppd, NULL, "Automatically Select", NULL, DMBIN_FORMSOURCE ))
730 HeapFree (PSDRV_Heap, 0, ppd);
731 unmap_file( &c );
732 return NULL;
735 while (PSDRV_PPDGetNextTuple( &c, &tuple ))
737 if(!strcmp("*NickName", tuple.key)) {
738 ppd->NickName = tuple.value;
739 tuple.value = NULL;
740 TRACE("NickName = '%s'\n", ppd->NickName);
743 else if(!strcmp("*LanguageLevel", tuple.key)) {
744 sscanf(tuple.value, "%d", &(ppd->LanguageLevel));
745 TRACE("LanguageLevel = %d\n", ppd->LanguageLevel);
748 else if(!strcmp("*ColorDevice", tuple.key)) {
749 if(!stricmp(tuple.value, "true"))
750 ppd->ColorDevice = CD_True;
751 else
752 ppd->ColorDevice = CD_False;
753 TRACE("ColorDevice = %s\n", ppd->ColorDevice == CD_True ? "True" : "False");
756 else if((!strcmp("*DefaultResolution", tuple.key)) ||
757 (!strcmp("*DefaultJCLResolution", tuple.key))) {
758 SIZE sz;
759 if(parse_resolution(tuple.value, &sz))
761 TRACE("DefaultResolution %dx%d\n", sz.cx, sz.cy);
762 ppd->DefaultResolution = sz.cx;
764 else
765 WARN("failed to parse DefaultResolution %s\n", debugstr_a(tuple.value));
768 else if(!strcmp("*Resolution", tuple.key))
770 SIZE sz;
771 if (parse_resolution(tuple.option, &sz))
773 RESOLUTION *res;
775 TRACE("Resolution %dx%d, invocation %s\n", sz.cx, sz.cy, tuple.value);
777 res = HeapAlloc( GetProcessHeap(), 0, sizeof(*res) );
778 res->resx = sz.cx;
779 res->resy = sz.cy;
780 res->InvocationString = tuple.value;
781 tuple.value = NULL;
782 list_add_tail( &ppd->Resolutions, &res->entry );
784 else
785 WARN("failed to parse Resolution %s\n", debugstr_a(tuple.option));
788 else if(!strcmp("*Font", tuple.key))
790 FONTNAME *fn = HeapAlloc( PSDRV_Heap, 0, sizeof(*fn) );
791 fn->Name = tuple.option;
792 tuple.option = NULL;
793 list_add_tail( &ppd->InstalledFonts, &fn->entry );
796 else if(!strcmp("*DefaultFont", tuple.key)) {
797 ppd->DefaultFont = tuple.value;
798 tuple.value = NULL;
801 else if(!strcmp("*JCLBegin", tuple.key)) {
802 ppd->JCLBegin = tuple.value;
803 tuple.value = NULL;
806 else if(!strcmp("*JCLToPSInterpreter", tuple.key)) {
807 ppd->JCLToPSInterpreter = tuple.value;
808 tuple.value = NULL;
811 else if(!strcmp("*JCLEnd", tuple.key)) {
812 ppd->JCLEnd = tuple.value;
813 tuple.value = NULL;
816 else if(!strcmp("*PageSize", tuple.key))
818 page = get_pagesize( ppd, tuple.option, TRUE );
820 if(!page->Name) {
821 page->Name = tuple.option;
822 tuple.option = NULL;
824 if(!page->FullName) {
825 if(tuple.opttrans) {
826 page->FullName = tuple.opttrans;
827 tuple.opttrans = NULL;
828 } else
830 page->FullName = HeapAlloc( PSDRV_Heap, 0, strlen(page->Name)+1 );
831 strcpy( page->FullName, page->Name );
834 if(!page->InvocationString) {
835 page->InvocationString = tuple.value;
836 tuple.value = NULL;
838 if (!page->WinPage)
840 int i;
841 for (i = 0; PageTrans[i].PSName; i++)
843 if (!strcmp( PageTrans[i].PSName, page->Name ))
845 page->WinPage = PageTrans[i].WinPage;
846 break;
849 if (!page->WinPage)
851 TRACE( "Can't find Windows page type for %s - using %u\n", debugstr_a(page->Name), UserPageType );
852 page->WinPage = UserPageType++;
857 else if(!strcmp("*DefaultPageSize", tuple.key))
859 if (!default_pagesize)
861 default_pagesize = tuple.value;
862 tuple.value = NULL;
866 else if(!strcmp("*ImageableArea", tuple.key)) {
867 page = get_pagesize( ppd, tuple.option, TRUE );
869 if(!page->Name) {
870 page->Name = tuple.option;
871 tuple.option = NULL;
873 if(!page->FullName) {
874 page->FullName = tuple.opttrans;
875 tuple.opttrans = NULL;
878 #define PIA page->ImageableArea
879 if(!PIA) {
880 PIA = HeapAlloc( PSDRV_Heap, 0, sizeof(*PIA) );
881 push_lc_numeric("C");
882 sscanf(tuple.value, "%f%f%f%f", &PIA->llx, &PIA->lly,
883 &PIA->urx, &PIA->ury);
884 pop_lc_numeric();
886 #undef PIA
889 else if(!strcmp("*PaperDimension", tuple.key)) {
890 page = get_pagesize( ppd, tuple.option, TRUE );
892 if(!page->Name) {
893 page->Name = tuple.option;
894 tuple.option = NULL;
896 if(!page->FullName) {
897 page->FullName = tuple.opttrans;
898 tuple.opttrans = NULL;
901 #define PD page->PaperDimension
902 if(!PD) {
903 PD = HeapAlloc( PSDRV_Heap, 0, sizeof(*PD) );
904 push_lc_numeric("C");
905 sscanf(tuple.value, "%f%f", &PD->x, &PD->y);
906 pop_lc_numeric();
908 #undef PD
911 else if(!strcmp("*LandscapeOrientation", tuple.key)) {
912 if(!strcmp(tuple.value, "Plus90"))
913 ppd->LandscapeOrientation = 90;
914 else if(!strcmp(tuple.value, "Minus90"))
915 ppd->LandscapeOrientation = -90;
917 /* anything else, namely 'any', leaves value at 0 */
919 TRACE("LandscapeOrientation = %d\n",
920 ppd->LandscapeOrientation);
923 else if(!strcmp("*UIConstraints", tuple.key))
925 char *start;
926 CONSTRAINT *con = HeapAlloc( PSDRV_Heap, 0, sizeof(*con) );
928 start = tuple.value;
929 con->Feature1 = PSDRV_PPDGetWord(start, &start);
930 con->Value1 = PSDRV_PPDGetWord(start, &start);
931 con->Feature2 = PSDRV_PPDGetWord(start, &start);
932 con->Value2 = PSDRV_PPDGetWord(start, &start);
934 list_add_tail( &ppd->Constraints, &con->entry );
937 else if (!strcmp("*InputSlot", tuple.key))
940 if (!tuple.opttrans)
941 tuple.opttrans = tuple.option;
943 TRACE("Using Windows bin type %u for '%s'\n", UserBinType,
944 tuple.option);
946 /* FIXME - should check for failure */
947 PSDRV_AddSlot(ppd, tuple.option, tuple.opttrans, tuple.value,
948 UserBinType++);
950 tuple.option = tuple.opttrans = tuple.value = NULL;
954 * Windows treats "manual feed" as another paper source. Most PPD
955 * files, however, treat it as a separate option which is either on or
956 * off.
958 else if (!strcmp("*ManualFeed", tuple.key) && tuple.option &&
959 !strcmp ("True", tuple.option))
961 /* FIXME - should check for failure */
962 PSDRV_AddSlot(ppd, "Manual Feed", "Manual Feed", tuple.value, DMBIN_MANUAL);
963 tuple.value = NULL;
966 else if(!strcmp("*TTRasterizer", tuple.key)) {
967 if(!stricmp("None", tuple.value))
968 ppd->TTRasterizer = RO_None;
969 else if(!stricmp("Accept68K", tuple.value))
970 ppd->TTRasterizer = RO_Accept68K;
971 else if(!stricmp("Type42", tuple.value))
972 ppd->TTRasterizer = RO_Type42;
973 else if(!stricmp("TrueImage", tuple.value))
974 ppd->TTRasterizer = RO_TrueImage;
975 else {
976 FIXME("Unknown option %s for *TTRasterizer\n",
977 tuple.value);
978 ppd->TTRasterizer = RO_None;
980 TRACE("*TTRasterizer = %d\n", ppd->TTRasterizer);
983 else if(!strcmp("*Duplex", tuple.key))
985 DUPLEX *duplex = HeapAlloc( GetProcessHeap(), 0, sizeof(*duplex) );
986 duplex->Name = tuple.option;
987 duplex->FullName = tuple.opttrans;
988 duplex->InvocationString = tuple.value;
989 if(!stricmp("None", tuple.option) || !stricmp("False", tuple.option)
990 || !stricmp("Simplex", tuple.option))
991 duplex->WinDuplex = DMDUP_SIMPLEX;
992 else if(!stricmp("DuplexNoTumble", tuple.option))
993 duplex->WinDuplex = DMDUP_VERTICAL;
994 else if(!stricmp("DuplexTumble", tuple.option))
995 duplex->WinDuplex = DMDUP_HORIZONTAL;
996 else if(!stricmp("Notcapable", tuple.option))
997 duplex->WinDuplex = 0;
998 else {
999 FIXME("Unknown option %s for *Duplex defaulting to simplex\n", tuple.option);
1000 duplex->WinDuplex = DMDUP_SIMPLEX;
1002 tuple.option = tuple.opttrans = tuple.value = NULL;
1003 list_add_tail( &ppd->Duplexes, &duplex->entry );
1006 else if (!strcmp("*DefaultDuplex", tuple.key))
1008 if (!default_duplex)
1010 default_duplex = tuple.value;
1011 tuple.value = NULL;
1015 HeapFree(PSDRV_Heap, 0, tuple.key);
1016 HeapFree(PSDRV_Heap, 0, tuple.option);
1017 HeapFree(PSDRV_Heap, 0, tuple.value);
1018 HeapFree(PSDRV_Heap, 0, tuple.opttrans);
1019 HeapFree(PSDRV_Heap, 0, tuple.valtrans);
1023 /* Remove any partial page size entries, that is any without a PageSize or a PaperDimension (we can
1024 cope with a missing ImageableArea). */
1025 LIST_FOR_EACH_ENTRY_SAFE(page, page_cursor2, &ppd->PageSizes, PAGESIZE, entry)
1027 if(!page->InvocationString || !page->PaperDimension)
1029 WARN("Removing page %s since it has a missing %s entry\n", debugstr_a(page->FullName),
1030 page->InvocationString ? "PaperDimension" : "InvocationString");
1031 HeapFree(PSDRV_Heap, 0, page->Name);
1032 HeapFree(PSDRV_Heap, 0, page->FullName);
1033 HeapFree(PSDRV_Heap, 0, page->InvocationString);
1034 HeapFree(PSDRV_Heap, 0, page->ImageableArea);
1035 HeapFree(PSDRV_Heap, 0, page->PaperDimension);
1036 list_remove(&page->entry);
1040 ppd->DefaultPageSize = NULL;
1041 def_pagesize_override = get_ppd_override( printer, "DefaultPageSize" );
1042 if (def_pagesize_override)
1043 ppd->DefaultPageSize = get_pagesize( ppd, def_pagesize_override, FALSE );
1044 if (!ppd->DefaultPageSize && default_pagesize)
1045 ppd->DefaultPageSize = get_pagesize( ppd, default_pagesize, FALSE );
1047 if (!ppd->DefaultPageSize)
1049 struct list *head = list_head( &ppd->PageSizes );
1050 if (head) ppd->DefaultPageSize = LIST_ENTRY( head, PAGESIZE, entry );
1051 TRACE("Setting DefaultPageSize to first in list\n");
1053 TRACE( "DefaultPageSize: %s\n", ppd->DefaultPageSize ? ppd->DefaultPageSize->Name : "<not set>" );
1055 HeapFree( PSDRV_Heap, 0, def_pagesize_override );
1056 HeapFree( PSDRV_Heap, 0, default_pagesize );
1058 ppd->DefaultDuplex = NULL;
1059 def_duplex_override = get_ppd_override( printer, "DefaultDuplex" );
1060 if (def_duplex_override)
1061 ppd->DefaultDuplex = get_duplex( ppd, def_duplex_override );
1062 if (!ppd->DefaultDuplex && default_duplex)
1063 ppd->DefaultDuplex = get_duplex( ppd, default_duplex );
1065 if (!ppd->DefaultDuplex)
1067 struct list *head = list_head( &ppd->Duplexes );
1068 if (head) ppd->DefaultDuplex = LIST_ENTRY( head, DUPLEX, entry );
1069 TRACE("Setting DefaultDuplex to first in list\n");
1071 TRACE( "DefaultDuplex: %s\n", ppd->DefaultDuplex ? ppd->DefaultDuplex->Name : "<not set>" );
1073 HeapFree( PSDRV_Heap, 0, def_duplex_override );
1074 HeapFree( PSDRV_Heap, 0, default_duplex );
1078 FONTNAME *fn;
1079 PAGESIZE *page;
1080 CONSTRAINT *con;
1081 INPUTSLOT *slot;
1083 LIST_FOR_EACH_ENTRY( fn, &ppd->InstalledFonts, FONTNAME, entry )
1084 TRACE("'%s'\n", fn->Name);
1086 LIST_FOR_EACH_ENTRY(page, &ppd->PageSizes, PAGESIZE, entry) {
1087 TRACE("'%s' aka '%s' (%d) invoked by '%s'\n", page->Name,
1088 page->FullName, page->WinPage, page->InvocationString);
1089 if(page->ImageableArea)
1090 TRACE("Area = %.2f,%.2f - %.2f, %.2f\n",
1091 page->ImageableArea->llx, page->ImageableArea->lly,
1092 page->ImageableArea->urx, page->ImageableArea->ury);
1093 if(page->PaperDimension)
1094 TRACE("Dimension = %.2f x %.2f\n",
1095 page->PaperDimension->x, page->PaperDimension->y);
1098 LIST_FOR_EACH_ENTRY( con, &ppd->Constraints, CONSTRAINT, entry )
1099 TRACE("CONSTRAINTS@ %s %s %s %s\n", con->Feature1,
1100 con->Value1, con->Feature2, con->Value2);
1102 LIST_FOR_EACH_ENTRY( slot, &ppd->InputSlots, INPUTSLOT, entry )
1103 TRACE("INPUTSLOTS '%s' Name '%s' (%d) Invocation '%s'\n",
1104 debugstr_a(slot->Name), slot->FullName, slot->WinBin,
1105 debugstr_a(slot->InvocationString));
1108 unmap_file( &c );
1109 return ppd;