Release 9.12.
[wine.git] / dlls / wineps.drv / ppd.c
blobd116f8ebbc0f5e48cc73860bd6789b7bb9f9caed
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 "winnls.h"
32 #include "wine/debug.h"
33 #include "psdrv.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 typedef struct {
38 char *key;
39 char *option;
40 char *opttrans;
41 char *value;
42 char *valtrans;
43 } PPDTuple;
45 struct map_context
47 const char *ptr, *pos, *end;
50 /* map of page names in ppd file to Windows paper constants */
52 static const struct {
53 const char *PSName;
54 WORD WinPage;
55 } PageTrans[] = {
56 {"10x11", DMPAPER_10X11},
57 {"10x14", DMPAPER_10X14},
58 {"11x17", DMPAPER_11X17}, /* not in Adobe PPD file spec */
59 {"12x11", DMPAPER_12X11},
60 {"15x11", DMPAPER_15X11},
61 {"9x11", DMPAPER_9X11},
62 {"A2", DMPAPER_A2},
63 {"A3", DMPAPER_A3},
64 {"A3.Transverse", DMPAPER_A3_TRANSVERSE},
65 {"A3Extra", DMPAPER_A3_EXTRA},
66 {"A3Extra.Transverse", DMPAPER_A3_EXTRA_TRANSVERSE},
67 {"A3Rotated", DMPAPER_A3_ROTATED},
68 {"A4", DMPAPER_A4},
69 {"A4.Transverse", DMPAPER_A4_TRANSVERSE},
70 {"A4Extra", DMPAPER_A4_EXTRA},
71 {"A4Plus", DMPAPER_A4_PLUS},
72 {"A4Rotated", DMPAPER_A4_ROTATED},
73 {"A4Small", DMPAPER_A4SMALL},
74 {"A5", DMPAPER_A5},
75 {"A5.Transverse", DMPAPER_A5_TRANSVERSE},
76 {"A5Extra", DMPAPER_A5_EXTRA},
77 {"A5Rotated", DMPAPER_A5_ROTATED},
78 {"A6", DMPAPER_A6},
79 {"A6Rotated", DMPAPER_A6_ROTATED},
80 {"ARCHC", DMPAPER_CSHEET},
81 {"ARCHD", DMPAPER_DSHEET},
82 {"ARCHE", DMPAPER_ESHEET},
83 {"B4", DMPAPER_B4},
84 {"B4Rotated", DMPAPER_B4_JIS_ROTATED},
85 {"B5", DMPAPER_B5},
86 {"B5.Transverse", DMPAPER_B5_TRANSVERSE},
87 {"B5Rotated", DMPAPER_B5_JIS_ROTATED},
88 {"B6", DMPAPER_B6_JIS},
89 {"B6Rotated", DMPAPER_B6_JIS_ROTATED},
90 {"C4", DMPAPER_ENV_C4}, /* use EnvC4 */
91 {"C5", DMPAPER_ENV_C5}, /* use EnvC5 */
92 {"C6", DMPAPER_ENV_C6}, /* use EnvC6 */
93 {"Comm10", DMPAPER_ENV_10}, /* use Env10 */
94 {"DL", DMPAPER_ENV_DL}, /* use EnvDL */
95 {"DoublePostcard", DMPAPER_DBL_JAPANESE_POSTCARD},
96 {"DoublePostcardRotated", DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED},
97 {"Env10", DMPAPER_ENV_10},
98 {"Env11", DMPAPER_ENV_11},
99 {"Env12", DMPAPER_ENV_12},
100 {"Env14", DMPAPER_ENV_14},
101 {"Env9", DMPAPER_ENV_9},
102 {"EnvC3", DMPAPER_ENV_C3},
103 {"EnvC4", DMPAPER_ENV_C4},
104 {"EnvC5", DMPAPER_ENV_C5},
105 {"EnvC6", DMPAPER_ENV_C6},
106 {"EnvC65", DMPAPER_ENV_C65},
107 {"EnvChou3", DMPAPER_JENV_CHOU3},
108 {"EnvChou3Rotated", DMPAPER_JENV_CHOU3_ROTATED},
109 {"EnvChou4", DMPAPER_JENV_CHOU4},
110 {"EnvChou4Rotated", DMPAPER_JENV_CHOU4_ROTATED},
111 {"EnvDL", DMPAPER_ENV_DL},
112 {"EnvISOB4", DMPAPER_ENV_B4},
113 {"EnvISOB5", DMPAPER_ENV_B5},
114 {"EnvISOB6", DMPAPER_ENV_B6},
115 {"EnvInvite", DMPAPER_ENV_INVITE},
116 {"EnvItalian", DMPAPER_ENV_ITALY},
117 {"EnvKaku2", DMPAPER_JENV_KAKU2},
118 {"EnvKaku2Rotated", DMPAPER_JENV_KAKU2_ROTATED},
119 {"EnvKaku3", DMPAPER_JENV_KAKU3},
120 {"EnvKaku3Rotated", DMPAPER_JENV_KAKU3_ROTATED},
121 {"EnvMonarch", DMPAPER_ENV_MONARCH},
122 {"EnvPRC1", DMPAPER_PENV_1},
123 {"EnvPRC10", DMPAPER_PENV_10},
124 {"EnvPRC10Rotated", DMPAPER_PENV_10_ROTATED},
125 {"EnvPRC1Rotated", DMPAPER_PENV_1_ROTATED},
126 {"EnvPRC2", DMPAPER_PENV_2},
127 {"EnvPRC2Rotated", DMPAPER_PENV_2_ROTATED},
128 {"EnvPRC3", DMPAPER_PENV_3},
129 {"EnvPRC3Rotated", DMPAPER_PENV_3_ROTATED},
130 {"EnvPRC4", DMPAPER_PENV_4},
131 {"EnvPRC4Rotated", DMPAPER_PENV_4_ROTATED},
132 {"EnvPRC5", DMPAPER_PENV_5},
133 {"EnvPRC5Rotated", DMPAPER_PENV_5_ROTATED},
134 {"EnvPRC6", DMPAPER_PENV_6},
135 {"EnvPRC6Rotated", DMPAPER_PENV_6_ROTATED},
136 {"EnvPRC7", DMPAPER_PENV_7},
137 {"EnvPRC7Rotated", DMPAPER_PENV_7_ROTATED},
138 {"EnvPRC8", DMPAPER_PENV_8},
139 {"EnvPRC8Rotated", DMPAPER_PENV_8_ROTATED},
140 {"EnvPRC9", DMPAPER_PENV_9},
141 {"EnvPRC9Rotated", DMPAPER_PENV_9_ROTATED},
142 {"EnvPersonal", DMPAPER_ENV_PERSONAL},
143 {"EnvYou4", DMPAPER_JENV_YOU4},
144 {"EnvYou4Rotated", DMPAPER_JENV_YOU4_ROTATED},
145 {"Executive", DMPAPER_EXECUTIVE},
146 {"FanFoldGerman", DMPAPER_FANFOLD_STD_GERMAN},
147 {"FanFoldGermanLegal", DMPAPER_FANFOLD_LGL_GERMAN},
148 {"FanFoldUS", DMPAPER_FANFOLD_US},
149 {"Folio", DMPAPER_FOLIO},
150 {"ISOB4", DMPAPER_ISO_B4},
151 {"ISOB5Extra", DMPAPER_B5_EXTRA},
152 {"Ledger", DMPAPER_LEDGER},
153 {"Legal", DMPAPER_LEGAL},
154 {"LegalExtra", DMPAPER_LEGAL_EXTRA},
155 {"Letter", DMPAPER_LETTER},
156 {"Letter.Transverse", DMPAPER_LETTER_TRANSVERSE},
157 {"LetterExtra", DMPAPER_LETTER_EXTRA},
158 {"LetterExtra.Transverse", DMPAPER_LETTER_EXTRA_TRANSVERSE},
159 {"LetterPlus", DMPAPER_LETTER_PLUS},
160 {"LetterRotated", DMPAPER_LETTER_ROTATED},
161 {"LetterSmall", DMPAPER_LETTERSMALL},
162 {"Monarch", DMPAPER_ENV_MONARCH}, /* use EnvMonarch */
163 {"Note", DMPAPER_NOTE},
164 {"PRC16K", DMPAPER_P16K},
165 {"PRC16KRotated", DMPAPER_P16K_ROTATED},
166 {"PRC32K", DMPAPER_P32K},
167 {"PRC32KBig", DMPAPER_P32KBIG},
168 {"PRC32KBigRotated", DMPAPER_P32KBIG_ROTATED},
169 {"PRC32KRotated", DMPAPER_P32K_ROTATED},
170 {"Postcard", DMPAPER_JAPANESE_POSTCARD},
171 {"PostcardRotated", DMPAPER_JAPANESE_POSTCARD_ROTATED},
172 {"Quarto", DMPAPER_QUARTO},
173 {"Statement", DMPAPER_STATEMENT},
174 {"SuperA", DMPAPER_A_PLUS},
175 {"SuperB", DMPAPER_B_PLUS},
176 {"Tabloid", DMPAPER_TABLOID},
177 {"TabloidExtra", DMPAPER_TABLOID_EXTRA},
178 {NULL, 0}
181 /***********************************************************************
183 * PSDRV_PPDDecodeHex
185 * Copies str into a newly allocated string from the process heap substituting
186 * hex strings enclosed in '<' and '>' for their byte codes.
189 static char *PSDRV_PPDDecodeHex(char *str)
191 char *buf, *in, *out;
192 BOOL inhex = FALSE;
194 buf = HeapAlloc(PSDRV_Heap, 0, strlen(str) + 1);
195 if(!buf)
196 return NULL;
198 for(in = str, out = buf; *in; in++) {
199 if(!inhex) {
200 if(*in != '<')
201 *out++ = *in;
202 else
203 inhex = TRUE;
204 } else {
205 if(*in == '>') {
206 inhex = FALSE;
207 continue;
209 else if(isspace(*in))
210 continue;
211 else {
212 int i;
213 if(!isxdigit(*in) || !isxdigit(*(in + 1))) {
214 ERR("Invalid hex char in hex string\n");
215 HeapFree(PSDRV_Heap, 0, buf);
216 return NULL;
218 *out = 0;
219 for(i = 0; i < 2; i++) {
220 if(isdigit(*(in + i)))
221 *out |= (*(in + i) - '0') << ((1-i) * 4);
222 else
223 *out |= (toupper(*(in + i)) - 'A' + 10) << ((1-i) * 4);
225 out++;
226 in++;
230 *out = '\0';
231 return buf;
235 /***********************************************************************
237 * PSDRV_PPDGetTransValue
240 static BOOL PSDRV_PPDGetTransValue(const char *start, PPDTuple *tuple)
242 char *buf;
243 const char *end;
245 end = strpbrk(start, "\r\n");
246 if(end == start) return FALSE;
247 if(!end) end = start + strlen(start);
248 buf = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
249 memcpy(buf, start, end - start);
250 *(buf + (end - start)) = '\0';
251 tuple->valtrans = PSDRV_PPDDecodeHex(buf);
252 HeapFree( PSDRV_Heap, 0, buf );
253 return TRUE;
256 static BOOL get_line( char *buf, int size, struct map_context *ctx )
258 int i;
259 if (ctx->pos > ctx->end) return FALSE;
261 for (i = 0; i < size - 1; i++)
263 if (ctx->pos > ctx->end) break;
264 buf[i] = *ctx->pos++;
266 /* \r\n -> \n */
267 if (buf[i] == '\r' && ctx->pos <= ctx->end && *ctx->pos == '\n')
269 ctx->pos++;
270 buf[i] = '\n';
273 if (buf[i] == '\n' || buf[i] == '\r')
275 i++;
276 break;
279 buf[i] = '\0';
280 return TRUE;
283 /***********************************************************************
285 * PSDRV_PPDGetInvocationValue
287 * Passed string that should be surrounded by `"'s, return string allocated
288 * from process heap.
290 static BOOL PSDRV_PPDGetInvocationValue(struct map_context *ctx, PPDTuple *tuple)
292 const char *start;
293 char *buf, line[257];
295 assert( *ctx->pos == '"' );
297 ctx->pos++;
298 for (start = ctx->pos; ctx->pos <= ctx->end; ctx->pos++)
299 if (*ctx->pos == '"') break;
300 if (ctx->pos > ctx->end) return FALSE;
301 ctx->pos++;
303 buf = HeapAlloc( PSDRV_Heap, 0, ctx->pos - start );
304 memcpy( buf, start, ctx->pos - start - 1 );
305 buf[ctx->pos - start - 1] = '\0';
306 tuple->value = buf;
308 if (get_line( line, sizeof(line), ctx ))
310 start = strchr( line, '/' );
311 if (start) return PSDRV_PPDGetTransValue( start + 1, tuple );
313 return TRUE;
317 /***********************************************************************
319 * PSDRV_PPDGetQuotedValue
321 * Passed string that should be surrounded by `"'s. Expand <xx> as hex
322 * return string allocated from process heap.
324 static BOOL PSDRV_PPDGetQuotedValue(struct map_context *ctx, PPDTuple *tuple)
326 char *buf;
328 if(!PSDRV_PPDGetInvocationValue(ctx, tuple))
329 return FALSE;
330 buf = PSDRV_PPDDecodeHex(tuple->value);
331 HeapFree(PSDRV_Heap, 0, tuple->value);
332 tuple->value = buf;
333 return TRUE;
337 /***********************************************************************
339 * PSDRV_PPDGetStringValue
341 * Just strip leading white space.
343 static BOOL PSDRV_PPDGetStringValue(char *str, PPDTuple *tuple)
345 char *start = str, *end;
347 while(*start != '\0' && isspace(*start))
348 start++;
350 end = strpbrk(start, "/\r\n");
351 if(!end) end = start + strlen(start);
352 tuple->value = HeapAlloc( PSDRV_Heap, 0, (end - start) + 1 );
353 memcpy(tuple->value, start, end - start);
354 *(tuple->value + (end - start)) = '\0';
355 if(*end == '/')
356 PSDRV_PPDGetTransValue(end + 1, tuple);
357 return TRUE;
361 /***********************************************************************
363 * PSDRV_PPDSymbolValue
365 * Not implemented yet.
367 static BOOL PSDRV_PPDGetSymbolValue(char *pos, PPDTuple *tuple)
369 FIXME("Stub\n");
370 return FALSE;
374 /*********************************************************************
376 * PSDRV_PPDGetNextTuple
378 * Gets the next Keyword Option Value tuple from the file. Allocs space off
379 * the process heap which should be free()ed by the caller if not needed.
381 static BOOL PSDRV_PPDGetNextTuple(struct map_context *ctx, PPDTuple *tuple)
383 char line[257], *opt, *cp, *trans, *endkey;
384 BOOL gotoption;
385 struct map_context save;
387 start:
389 gotoption = TRUE;
390 opt = NULL;
391 memset(tuple, 0, sizeof(*tuple));
393 do {
394 save = *ctx;
395 if(!get_line(line, sizeof(line), ctx))
396 return FALSE;
397 if(line[0] == '*' && line[1] != '%' && strncmp(line, "*End", 4))
398 break;
399 } while(1);
401 cp = line + strlen(line) - 1;
402 if (*cp != '\n' && *cp != '\r')
404 ERR("Line too long.\n");
405 goto start;
408 for(cp = line; !isspace(*cp) && *cp != ':'; cp++)
411 endkey = cp;
412 while (isspace(*cp)) cp++;
413 if (*cp == ':') /* <key>: */
414 gotoption = FALSE;
415 else /* <key> <option> */
416 opt = cp;
418 tuple->key = HeapAlloc( PSDRV_Heap, 0, endkey - line + 1 );
419 if(!tuple->key) return FALSE;
421 memcpy(tuple->key, line, endkey - line);
422 tuple->key[endkey - line] = '\0';
424 if(gotoption) { /* opt points to 1st non-space character of the option */
425 cp = strpbrk(opt, ":/");
426 if(!cp) {
427 ERR("Error in line '%s'?\n", line);
428 HeapFree(GetProcessHeap(), 0, tuple->key);
429 goto start;
431 tuple->option = HeapAlloc( PSDRV_Heap, 0, cp - opt + 1 );
432 if(!tuple->option) return FALSE;
433 memcpy(tuple->option, opt, cp - opt);
434 tuple->option[cp - opt] = '\0';
435 if(*cp == '/') {
436 char *buf;
437 trans = cp + 1;
438 cp = strchr(trans, ':');
439 if(!cp) {
440 ERR("Error in line '%s'?\n", line);
441 HeapFree(GetProcessHeap(), 0, tuple->option);
442 HeapFree(GetProcessHeap(), 0, tuple->key);
443 goto start;
445 buf = HeapAlloc( PSDRV_Heap, 0, cp - trans + 1 );
446 if(!buf) return FALSE;
447 memcpy(buf, trans, cp - trans);
448 buf[cp - trans] = '\0';
449 tuple->opttrans = PSDRV_PPDDecodeHex(buf);
450 HeapFree( PSDRV_Heap, 0, buf );
454 /* cp should point to a ':', so we increment past it */
455 cp++;
457 while(isspace(*cp))
458 cp++;
460 switch(*cp) {
461 case '"':
462 /* update the context pos so that it points to the opening quote */
463 ctx->pos = save.pos + (cp - line);
464 if( (!gotoption && strncmp(tuple->key, "*?", 2) ) ||
465 !strncmp(tuple->key, "*JCL", 4))
466 PSDRV_PPDGetQuotedValue(ctx, tuple);
467 else
468 PSDRV_PPDGetInvocationValue(ctx, tuple);
469 break;
471 case '^':
472 PSDRV_PPDGetSymbolValue(cp, tuple);
473 break;
475 default:
476 PSDRV_PPDGetStringValue(cp, tuple);
478 return TRUE;
481 /*********************************************************************
482 * get_pagesize
484 * Searches ppd PageSize list to return entry matching name or optionally creates new
485 * entry which is appended to the list if name is not found.
487 static PAGESIZE *get_pagesize( PPD *ppd, char *name, BOOL create )
489 PAGESIZE *page;
491 LIST_FOR_EACH_ENTRY(page, &ppd->PageSizes, PAGESIZE, entry)
493 if(!strcmp(page->Name, name))
494 return page;
497 if (!create) return NULL;
499 page = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*page) );
500 list_add_tail(&ppd->PageSizes, &page->entry);
501 return page;
504 static DUPLEX *get_duplex( PPD *ppd, const char *name )
506 DUPLEX *duplex;
508 LIST_FOR_EACH_ENTRY( duplex, &ppd->Duplexes, DUPLEX, entry )
510 if (!strcmp( duplex->Name, name ))
511 return duplex;
514 return NULL;
517 /**********************************************************************
519 * PSDRV_PPDGetWord
521 * Returns ptr allocated from heap to first word in str. Strips leading spaces.
522 * Puts ptr to next word in next
524 static char *PSDRV_PPDGetWord(char *str, char **next)
526 char *start, *end, *ret;
528 start = str;
529 while(start && *start && isspace(*start))
530 start++;
531 if(!start || !*start) return FALSE;
533 end = start;
534 while(*end && !isspace(*end))
535 end++;
537 ret = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
538 memcpy(ret, start, end - start );
539 *(ret + (end - start)) = '\0';
541 while(*end && isspace(*end))
542 end++;
543 if(*end)
544 *next = end;
545 else
546 *next = NULL;
548 return ret;
551 /************************************************************
552 * parse_resolution
554 * Helper to extract x and y resolutions from a resolution entry.
555 * Returns TRUE on successful parsing, otherwise FALSE.
557 * The ppd spec says that entries can either be:
558 * "nnndpi"
559 * or
560 * "nnnxmmmdpi"
561 * in the former case return sz.cx == cx.cy == nnn.
563 * There are broken ppd files out there (notably from Samsung) that
564 * use the form "nnnmmmdpi", so we have to work harder to spot these.
565 * We use a transition from a zero to a non-zero digit as separator
566 * (and fail if we find more than one of these). We also don't bother
567 * trying to parse "dpi" in case that's missing.
569 static BOOL parse_resolution(const char *str, SIZE *sz)
571 int tmp[2];
572 int *cur;
573 BOOL had_zero;
574 const char *c;
576 if(sscanf(str, "%dx%d", tmp, tmp + 1) == 2)
578 sz->cx = tmp[0];
579 sz->cy = tmp[1];
580 return TRUE;
583 tmp[0] = 0;
584 tmp[1] = -1;
585 cur = tmp;
586 had_zero = FALSE;
587 for(c = str; isdigit(*c); c++)
589 if(!had_zero || *c == '0')
591 *cur *= 10;
592 *cur += *c - '0';
593 if(*c == '0')
594 had_zero = TRUE;
596 else if(cur != tmp)
597 return FALSE;
598 else
600 cur++;
601 *cur = *c - '0';
602 had_zero = FALSE;
605 if(tmp[0] == 0) return FALSE;
607 sz->cx = tmp[0];
608 if(tmp[1] != -1)
609 sz->cy = tmp[1];
610 else
611 sz->cy = sz->cx;
612 return TRUE;
615 /*******************************************************************************
616 * PSDRV_AddSlot
619 static BOOL PSDRV_AddSlot(PPD *ppd, LPCSTR szName, LPCSTR szFullName,
620 LPSTR szInvocationString, WORD wWinBin)
622 INPUTSLOT *slot = HeapAlloc( PSDRV_Heap, 0, sizeof(INPUTSLOT) );
623 if (!slot) return FALSE;
625 slot->Name = szName;
626 slot->FullName = szFullName;
627 slot->InvocationString = szInvocationString;
628 slot->WinBin = wWinBin;
630 list_add_tail( &ppd->InputSlots, &slot->entry );
631 return TRUE;
634 static char *get_ppd_override( HANDLE printer, const char *value )
636 DWORD err, type, needed;
637 char *data;
639 err = GetPrinterDataExA( printer, "PPD Overrides", value, &type, NULL, 0, &needed );
640 if (err != ERROR_MORE_DATA || type != REG_SZ || needed == 0) return NULL;
642 data = HeapAlloc( PSDRV_Heap, 0, needed );
643 if (data)
645 GetPrinterDataExA( printer, "PPD Overrides", value, &type, (BYTE*)data, needed, &needed );
646 TRACE( "got override %s: %s\n", value, data );
648 return data;
651 static BOOL map_file( const WCHAR *filename, struct map_context *c )
653 HANDLE file, mapping;
654 LARGE_INTEGER size;
656 file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
657 if (file == INVALID_HANDLE_VALUE) return FALSE;
659 if (!GetFileSizeEx( file, &size ) || size.u.HighPart)
661 CloseHandle( file );
662 return FALSE;
665 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
666 CloseHandle( file );
667 if (!mapping) return FALSE;
669 c->pos = c->ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
670 c->end = c->ptr + size.u.LowPart - 1;
671 CloseHandle( mapping );
672 return TRUE;
675 static void unmap_file( struct map_context *c )
677 UnmapViewOfFile( c->ptr );
680 /***********************************************************************
682 * PSDRV_ParsePPD
686 PPD *PSDRV_ParsePPD( const WCHAR *fname, HANDLE printer )
688 PPD *ppd;
689 PPDTuple tuple;
690 char *default_pagesize = NULL, *default_duplex = NULL;
691 char *def_pagesize_override = NULL, *def_duplex_override = NULL;
692 PAGESIZE *page, *page_cursor2;
693 struct map_context c;
694 WORD UserPageType = DMPAPER_USER;
695 WORD UserBinType = DMBIN_USER;
697 TRACE("file %s\n", debugstr_w(fname));
699 if (!map_file( fname, &c ))
701 WARN("Couldn't open ppd file %s\n", debugstr_w(fname));
702 return NULL;
705 ppd = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*ppd));
706 if(!ppd) {
707 ERR("Unable to allocate memory for ppd\n");
708 unmap_file( &c );
709 return NULL;
712 ppd->ColorDevice = CD_NotSpecified;
714 list_init( &ppd->Resolutions );
715 list_init( &ppd->InstalledFonts );
716 list_init( &ppd->PageSizes );
717 list_init( &ppd->Constraints );
718 list_init( &ppd->InputSlots );
719 list_init( &ppd->Duplexes );
721 /* Some gimp-print ppd files don't contain a DefaultResolution line
722 so default to 300 */
723 ppd->DefaultResolution = 300;
726 * The Windows PostScript drivers create the following "virtual bin" for
727 * every PostScript printer
729 if (!PSDRV_AddSlot( ppd, NULL, "Automatically Select", NULL, DMBIN_FORMSOURCE ))
731 HeapFree (PSDRV_Heap, 0, ppd);
732 unmap_file( &c );
733 return NULL;
736 while (PSDRV_PPDGetNextTuple( &c, &tuple ))
738 if(!strcmp("*NickName", tuple.key)) {
739 ppd->NickName = tuple.value;
740 tuple.value = NULL;
741 TRACE("NickName = '%s'\n", ppd->NickName);
744 else if(!strcmp("*LanguageLevel", tuple.key)) {
745 sscanf(tuple.value, "%d", &(ppd->LanguageLevel));
746 TRACE("LanguageLevel = %d\n", ppd->LanguageLevel);
749 else if(!strcmp("*ColorDevice", tuple.key)) {
750 if(!stricmp(tuple.value, "true"))
751 ppd->ColorDevice = CD_True;
752 else
753 ppd->ColorDevice = CD_False;
754 TRACE("ColorDevice = %s\n", ppd->ColorDevice == CD_True ? "True" : "False");
757 else if((!strcmp("*DefaultResolution", tuple.key)) ||
758 (!strcmp("*DefaultJCLResolution", tuple.key))) {
759 SIZE sz;
760 if(parse_resolution(tuple.value, &sz))
762 TRACE("DefaultResolution %ldx%ld\n", sz.cx, sz.cy);
763 ppd->DefaultResolution = sz.cx;
765 else
766 WARN("failed to parse DefaultResolution %s\n", debugstr_a(tuple.value));
769 else if(!strcmp("*Resolution", tuple.key))
771 SIZE sz;
772 if (parse_resolution(tuple.option, &sz))
774 RESOLUTION *res;
776 TRACE("Resolution %ldx%ld, invocation %s\n", sz.cx, sz.cy, tuple.value);
778 res = HeapAlloc( GetProcessHeap(), 0, sizeof(*res) );
779 res->resx = sz.cx;
780 res->resy = sz.cy;
781 res->InvocationString = tuple.value;
782 tuple.value = NULL;
783 list_add_tail( &ppd->Resolutions, &res->entry );
785 else
786 WARN("failed to parse Resolution %s\n", debugstr_a(tuple.option));
789 else if(!strcmp("*Font", tuple.key))
791 FONTNAME *fn = HeapAlloc( PSDRV_Heap, 0, sizeof(*fn) );
792 fn->Name = tuple.option;
793 tuple.option = NULL;
794 list_add_tail( &ppd->InstalledFonts, &fn->entry );
797 else if(!strcmp("*DefaultFont", tuple.key)) {
798 ppd->DefaultFont = tuple.value;
799 tuple.value = NULL;
802 else if(!strcmp("*JCLBegin", tuple.key)) {
803 ppd->JCLBegin = tuple.value;
804 tuple.value = NULL;
807 else if(!strcmp("*JCLToPSInterpreter", tuple.key)) {
808 ppd->JCLToPSInterpreter = tuple.value;
809 tuple.value = NULL;
812 else if(!strcmp("*JCLEnd", tuple.key)) {
813 ppd->JCLEnd = tuple.value;
814 tuple.value = NULL;
817 else if(!strcmp("*PageSize", tuple.key))
819 page = get_pagesize( ppd, tuple.option, TRUE );
821 if(!page->Name) {
822 page->Name = tuple.option;
823 tuple.option = NULL;
825 if(!page->FullName) {
826 int len;
828 if(tuple.opttrans) {
829 len = MultiByteToWideChar( CP_ACP, 0, tuple.opttrans, -1, NULL, 0 );
830 page->FullName = HeapAlloc( PSDRV_Heap, 0, len * sizeof(WCHAR) );
831 MultiByteToWideChar( CP_ACP, 0, tuple.opttrans, -1, page->FullName, len );
832 } else
834 len = MultiByteToWideChar( CP_ACP, 0, page->Name, -1, NULL, 0 );
835 page->FullName = HeapAlloc( PSDRV_Heap, 0, len * sizeof(WCHAR) );
836 MultiByteToWideChar( CP_ACP, 0, page->Name, -1, page->FullName, len );
839 if(!page->InvocationString) {
840 page->InvocationString = tuple.value;
841 tuple.value = NULL;
843 if (!page->WinPage)
845 int i;
846 for (i = 0; PageTrans[i].PSName; i++)
848 if (!strcmp( PageTrans[i].PSName, page->Name ))
850 page->WinPage = PageTrans[i].WinPage;
851 break;
854 if (!page->WinPage)
856 TRACE( "Can't find Windows page type for %s - using %u\n", debugstr_a(page->Name), UserPageType );
857 page->WinPage = UserPageType++;
862 else if(!strcmp("*DefaultPageSize", tuple.key))
864 if (!default_pagesize)
866 default_pagesize = tuple.value;
867 tuple.value = NULL;
871 else if(!strcmp("*ImageableArea", tuple.key)) {
872 page = get_pagesize( ppd, tuple.option, TRUE );
874 if(!page->Name) {
875 page->Name = tuple.option;
876 tuple.option = NULL;
878 if(!page->FullName) {
879 int len = MultiByteToWideChar( CP_ACP, 0, tuple.opttrans, -1, NULL, 0 );
880 page->FullName = HeapAlloc( PSDRV_Heap, 0, len * sizeof(WCHAR) );
881 MultiByteToWideChar( CP_ACP, 0, tuple.opttrans, -1, page->FullName, len );
884 #define PIA page->ImageableArea
885 if(!PIA) {
886 PIA = HeapAlloc( PSDRV_Heap, 0, sizeof(*PIA) );
887 push_lc_numeric("C");
888 sscanf(tuple.value, "%f%f%f%f", &PIA->llx, &PIA->lly,
889 &PIA->urx, &PIA->ury);
890 pop_lc_numeric();
892 #undef PIA
895 else if(!strcmp("*PaperDimension", tuple.key)) {
896 page = get_pagesize( ppd, tuple.option, TRUE );
898 if(!page->Name) {
899 page->Name = tuple.option;
900 tuple.option = NULL;
902 if(!page->FullName) {
903 int len = MultiByteToWideChar( CP_ACP, 0, tuple.opttrans, -1, NULL, 0 );
904 page->FullName = HeapAlloc( PSDRV_Heap, 0, len * sizeof(WCHAR) );
905 MultiByteToWideChar( CP_ACP, 0, tuple.opttrans, -1, page->FullName, len );
908 #define PD page->PaperDimension
909 if(!PD) {
910 PD = HeapAlloc( PSDRV_Heap, 0, sizeof(*PD) );
911 push_lc_numeric("C");
912 sscanf(tuple.value, "%f%f", &PD->x, &PD->y);
913 pop_lc_numeric();
915 #undef PD
918 else if(!strcmp("*LandscapeOrientation", tuple.key)) {
919 if(!strcmp(tuple.value, "Plus90"))
920 ppd->LandscapeOrientation = 90;
921 else if(!strcmp(tuple.value, "Minus90"))
922 ppd->LandscapeOrientation = -90;
924 /* anything else, namely 'any', leaves value at 0 */
926 TRACE("LandscapeOrientation = %d\n",
927 ppd->LandscapeOrientation);
930 else if(!strcmp("*UIConstraints", tuple.key))
932 char *start;
933 CONSTRAINT *con = HeapAlloc( PSDRV_Heap, 0, sizeof(*con) );
935 start = tuple.value;
936 con->Feature1 = PSDRV_PPDGetWord(start, &start);
937 con->Value1 = PSDRV_PPDGetWord(start, &start);
938 con->Feature2 = PSDRV_PPDGetWord(start, &start);
939 con->Value2 = PSDRV_PPDGetWord(start, &start);
941 list_add_tail( &ppd->Constraints, &con->entry );
944 else if (!strcmp("*InputSlot", tuple.key))
947 if (!tuple.opttrans)
948 tuple.opttrans = tuple.option;
950 TRACE("Using Windows bin type %u for '%s'\n", UserBinType,
951 tuple.option);
953 /* FIXME - should check for failure */
954 PSDRV_AddSlot(ppd, tuple.option, tuple.opttrans, tuple.value,
955 UserBinType++);
957 tuple.option = tuple.opttrans = tuple.value = NULL;
961 * Windows treats "manual feed" as another paper source. Most PPD
962 * files, however, treat it as a separate option which is either on or
963 * off.
965 else if (!strcmp("*ManualFeed", tuple.key) && tuple.option &&
966 !strcmp ("True", tuple.option))
968 /* FIXME - should check for failure */
969 PSDRV_AddSlot(ppd, "Manual Feed", "Manual Feed", tuple.value, DMBIN_MANUAL);
970 tuple.value = NULL;
973 else if(!strcmp("*TTRasterizer", tuple.key)) {
974 if(!stricmp("None", tuple.value))
975 ppd->TTRasterizer = RO_None;
976 else if(!stricmp("Accept68K", tuple.value))
977 ppd->TTRasterizer = RO_Accept68K;
978 else if(!stricmp("Type42", tuple.value))
979 ppd->TTRasterizer = RO_Type42;
980 else if(!stricmp("TrueImage", tuple.value))
981 ppd->TTRasterizer = RO_TrueImage;
982 else {
983 FIXME("Unknown option %s for *TTRasterizer\n",
984 tuple.value);
985 ppd->TTRasterizer = RO_None;
987 TRACE("*TTRasterizer = %d\n", ppd->TTRasterizer);
990 else if(!strcmp("*Duplex", tuple.key))
992 DUPLEX *duplex = HeapAlloc( GetProcessHeap(), 0, sizeof(*duplex) );
993 duplex->Name = tuple.option;
994 duplex->FullName = tuple.opttrans;
995 duplex->InvocationString = tuple.value;
996 if(!stricmp("None", tuple.option) || !stricmp("False", tuple.option)
997 || !stricmp("Simplex", tuple.option))
998 duplex->WinDuplex = DMDUP_SIMPLEX;
999 else if(!stricmp("DuplexNoTumble", tuple.option))
1000 duplex->WinDuplex = DMDUP_VERTICAL;
1001 else if(!stricmp("DuplexTumble", tuple.option))
1002 duplex->WinDuplex = DMDUP_HORIZONTAL;
1003 else if(!stricmp("Notcapable", tuple.option))
1004 duplex->WinDuplex = 0;
1005 else {
1006 FIXME("Unknown option %s for *Duplex defaulting to simplex\n", tuple.option);
1007 duplex->WinDuplex = DMDUP_SIMPLEX;
1009 tuple.option = tuple.opttrans = tuple.value = NULL;
1010 list_add_tail( &ppd->Duplexes, &duplex->entry );
1013 else if (!strcmp("*DefaultDuplex", tuple.key))
1015 if (!default_duplex)
1017 default_duplex = tuple.value;
1018 tuple.value = NULL;
1022 HeapFree(PSDRV_Heap, 0, tuple.key);
1023 HeapFree(PSDRV_Heap, 0, tuple.option);
1024 HeapFree(PSDRV_Heap, 0, tuple.value);
1025 HeapFree(PSDRV_Heap, 0, tuple.opttrans);
1026 HeapFree(PSDRV_Heap, 0, tuple.valtrans);
1030 /* Remove any partial page size entries, that is any without a PageSize or a PaperDimension (we can
1031 cope with a missing ImageableArea). */
1032 LIST_FOR_EACH_ENTRY_SAFE(page, page_cursor2, &ppd->PageSizes, PAGESIZE, entry)
1034 if(!page->InvocationString || !page->PaperDimension)
1036 WARN("Removing page %s since it has a missing %s entry\n", debugstr_w(page->FullName),
1037 page->InvocationString ? "PaperDimension" : "InvocationString");
1038 HeapFree(PSDRV_Heap, 0, page->Name);
1039 HeapFree(PSDRV_Heap, 0, page->FullName);
1040 HeapFree(PSDRV_Heap, 0, page->InvocationString);
1041 HeapFree(PSDRV_Heap, 0, page->ImageableArea);
1042 HeapFree(PSDRV_Heap, 0, page->PaperDimension);
1043 list_remove(&page->entry);
1047 ppd->DefaultPageSize = NULL;
1048 def_pagesize_override = get_ppd_override( printer, "DefaultPageSize" );
1049 if (def_pagesize_override)
1050 ppd->DefaultPageSize = get_pagesize( ppd, def_pagesize_override, FALSE );
1051 if (!ppd->DefaultPageSize && default_pagesize)
1052 ppd->DefaultPageSize = get_pagesize( ppd, default_pagesize, FALSE );
1054 if (!ppd->DefaultPageSize)
1056 struct list *head = list_head( &ppd->PageSizes );
1057 if (head) ppd->DefaultPageSize = LIST_ENTRY( head, PAGESIZE, entry );
1058 TRACE("Setting DefaultPageSize to first in list\n");
1060 TRACE( "DefaultPageSize: %s\n", ppd->DefaultPageSize ? ppd->DefaultPageSize->Name : "<not set>" );
1062 HeapFree( PSDRV_Heap, 0, def_pagesize_override );
1063 HeapFree( PSDRV_Heap, 0, default_pagesize );
1065 ppd->DefaultDuplex = NULL;
1066 def_duplex_override = get_ppd_override( printer, "DefaultDuplex" );
1067 if (def_duplex_override)
1068 ppd->DefaultDuplex = get_duplex( ppd, def_duplex_override );
1069 if (!ppd->DefaultDuplex && default_duplex)
1070 ppd->DefaultDuplex = get_duplex( ppd, default_duplex );
1072 if (!ppd->DefaultDuplex)
1074 struct list *head = list_head( &ppd->Duplexes );
1075 if (head) ppd->DefaultDuplex = LIST_ENTRY( head, DUPLEX, entry );
1076 TRACE("Setting DefaultDuplex to first in list\n");
1078 TRACE( "DefaultDuplex: %s\n", ppd->DefaultDuplex ? ppd->DefaultDuplex->Name : "<not set>" );
1080 HeapFree( PSDRV_Heap, 0, def_duplex_override );
1081 HeapFree( PSDRV_Heap, 0, default_duplex );
1085 FONTNAME *fn;
1086 PAGESIZE *page;
1087 CONSTRAINT *con;
1088 INPUTSLOT *slot;
1090 LIST_FOR_EACH_ENTRY( fn, &ppd->InstalledFonts, FONTNAME, entry )
1091 TRACE("'%s'\n", fn->Name);
1093 LIST_FOR_EACH_ENTRY(page, &ppd->PageSizes, PAGESIZE, entry) {
1094 TRACE("'%s' aka %s (%d) invoked by '%s'\n", page->Name,
1095 debugstr_w(page->FullName), page->WinPage, page->InvocationString);
1096 if(page->ImageableArea)
1097 TRACE("Area = %.2f,%.2f - %.2f, %.2f\n",
1098 page->ImageableArea->llx, page->ImageableArea->lly,
1099 page->ImageableArea->urx, page->ImageableArea->ury);
1100 if(page->PaperDimension)
1101 TRACE("Dimension = %.2f x %.2f\n",
1102 page->PaperDimension->x, page->PaperDimension->y);
1105 LIST_FOR_EACH_ENTRY( con, &ppd->Constraints, CONSTRAINT, entry )
1106 TRACE("CONSTRAINTS@ %s %s %s %s\n", con->Feature1,
1107 con->Value1, con->Feature2, con->Value2);
1109 LIST_FOR_EACH_ENTRY( slot, &ppd->InputSlots, INPUTSLOT, entry )
1110 TRACE("INPUTSLOTS '%s' Name '%s' (%d) Invocation '%s'\n",
1111 debugstr_a(slot->Name), slot->FullName, slot->WinBin,
1112 debugstr_a(slot->InvocationString));
1115 unmap_file( &c );
1116 return ppd;