bcrypt: Implement BCryptGenerateSymmetricKey and BCryptDestroyKey.
[wine.git] / dlls / wineps.drv / ppd.c
blobf8de57bf3fe7f61548ff3c039631da4c0febae20
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 "config.h"
23 #include "wine/port.h"
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <locale.h>
30 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wine/debug.h"
34 #include "psdrv.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 typedef struct {
39 char *key;
40 char *option;
41 char *opttrans;
42 char *value;
43 char *valtrans;
44 } PPDTuple;
46 struct map_context
48 const char *ptr, *pos, *end;
51 /* map of page names in ppd file to Windows paper constants */
53 static const struct {
54 const char *PSName;
55 WORD WinPage;
56 } PageTrans[] = {
57 {"10x11", DMPAPER_10X11},
58 {"10x14", DMPAPER_10X14},
59 {"11x17", DMPAPER_11X17}, /* not in Adobe PPD file spec */
60 {"12x11", DMPAPER_12X11},
61 {"15x11", DMPAPER_15X11},
62 {"9x11", DMPAPER_9X11},
63 {"A2", DMPAPER_A2},
64 {"A3", DMPAPER_A3},
65 {"A3.Transverse", DMPAPER_A3_TRANSVERSE},
66 {"A3Extra", DMPAPER_A3_EXTRA},
67 {"A3Extra.Transverse", DMPAPER_A3_EXTRA_TRANSVERSE},
68 {"A3Rotated", DMPAPER_A3_ROTATED},
69 {"A4", DMPAPER_A4},
70 {"A4.Transverse", DMPAPER_A4_TRANSVERSE},
71 {"A4Extra", DMPAPER_A4_EXTRA},
72 {"A4Plus", DMPAPER_A4_PLUS},
73 {"A4Rotated", DMPAPER_A4_ROTATED},
74 {"A4Small", DMPAPER_A4SMALL},
75 {"A5", DMPAPER_A5},
76 {"A5.Transverse", DMPAPER_A5_TRANSVERSE},
77 {"A5Extra", DMPAPER_A5_EXTRA},
78 {"A5Rotated", DMPAPER_A5_ROTATED},
79 {"A6", DMPAPER_A6},
80 {"A6Rotated", DMPAPER_A6_ROTATED},
81 {"ARCHC", DMPAPER_CSHEET},
82 {"ARCHD", DMPAPER_DSHEET},
83 {"ARCHE", DMPAPER_ESHEET},
84 {"B4", DMPAPER_B4},
85 {"B4Rotated", DMPAPER_B4_JIS_ROTATED},
86 {"B5", DMPAPER_B5},
87 {"B5.Transverse", DMPAPER_B5_TRANSVERSE},
88 {"B5Rotated", DMPAPER_B5_JIS_ROTATED},
89 {"B6", DMPAPER_B6_JIS},
90 {"B6Rotated", DMPAPER_B6_JIS_ROTATED},
91 {"C4", DMPAPER_ENV_C4}, /* use EnvC4 */
92 {"C5", DMPAPER_ENV_C5}, /* use EnvC5 */
93 {"C6", DMPAPER_ENV_C6}, /* use EnvC6 */
94 {"Comm10", DMPAPER_ENV_10}, /* use Env10 */
95 {"DL", DMPAPER_ENV_DL}, /* use EnvDL */
96 {"DoublePostcard", DMPAPER_DBL_JAPANESE_POSTCARD},
97 {"DoublePostcardRotated", DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED},
98 {"Env10", DMPAPER_ENV_10},
99 {"Env11", DMPAPER_ENV_11},
100 {"Env12", DMPAPER_ENV_12},
101 {"Env14", DMPAPER_ENV_14},
102 {"Env9", DMPAPER_ENV_9},
103 {"EnvC3", DMPAPER_ENV_C3},
104 {"EnvC4", DMPAPER_ENV_C4},
105 {"EnvC5", DMPAPER_ENV_C5},
106 {"EnvC6", DMPAPER_ENV_C6},
107 {"EnvC65", DMPAPER_ENV_C65},
108 {"EnvChou3", DMPAPER_JENV_CHOU3},
109 {"EnvChou3Rotated", DMPAPER_JENV_CHOU3_ROTATED},
110 {"EnvChou4", DMPAPER_JENV_CHOU4},
111 {"EnvChou4Rotated", DMPAPER_JENV_CHOU4_ROTATED},
112 {"EnvDL", DMPAPER_ENV_DL},
113 {"EnvISOB4", DMPAPER_ENV_B4},
114 {"EnvISOB5", DMPAPER_ENV_B5},
115 {"EnvISOB6", DMPAPER_ENV_B6},
116 {"EnvInvite", DMPAPER_ENV_INVITE},
117 {"EnvItalian", DMPAPER_ENV_ITALY},
118 {"EnvKaku2", DMPAPER_JENV_KAKU2},
119 {"EnvKaku2Rotated", DMPAPER_JENV_KAKU2_ROTATED},
120 {"EnvKaku3", DMPAPER_JENV_KAKU3},
121 {"EnvKaku3Rotated", DMPAPER_JENV_KAKU3_ROTATED},
122 {"EnvMonarch", DMPAPER_ENV_MONARCH},
123 {"EnvPRC1", DMPAPER_PENV_1},
124 {"EnvPRC10", DMPAPER_PENV_10},
125 {"EnvPRC10Rotated", DMPAPER_PENV_10_ROTATED},
126 {"EnvPRC1Rotated", DMPAPER_PENV_1_ROTATED},
127 {"EnvPRC2", DMPAPER_PENV_2},
128 {"EnvPRC2Rotated", DMPAPER_PENV_2_ROTATED},
129 {"EnvPRC3", DMPAPER_PENV_3},
130 {"EnvPRC3Rotated", DMPAPER_PENV_3_ROTATED},
131 {"EnvPRC4", DMPAPER_PENV_4},
132 {"EnvPRC4Rotated", DMPAPER_PENV_4_ROTATED},
133 {"EnvPRC5", DMPAPER_PENV_5},
134 {"EnvPRC5Rotated", DMPAPER_PENV_5_ROTATED},
135 {"EnvPRC6", DMPAPER_PENV_6},
136 {"EnvPRC6Rotated", DMPAPER_PENV_6_ROTATED},
137 {"EnvPRC7", DMPAPER_PENV_7},
138 {"EnvPRC7Rotated", DMPAPER_PENV_7_ROTATED},
139 {"EnvPRC8", DMPAPER_PENV_8},
140 {"EnvPRC8Rotated", DMPAPER_PENV_8_ROTATED},
141 {"EnvPRC9", DMPAPER_PENV_9},
142 {"EnvPRC9Rotated", DMPAPER_PENV_9_ROTATED},
143 {"EnvPersonal", DMPAPER_ENV_PERSONAL},
144 {"EnvYou4", DMPAPER_JENV_YOU4},
145 {"EnvYou4Rotated", DMPAPER_JENV_YOU4_ROTATED},
146 {"Executive", DMPAPER_EXECUTIVE},
147 {"FanFoldGerman", DMPAPER_FANFOLD_STD_GERMAN},
148 {"FanFoldGermanLegal", DMPAPER_FANFOLD_LGL_GERMAN},
149 {"FanFoldUS", DMPAPER_FANFOLD_US},
150 {"Folio", DMPAPER_FOLIO},
151 {"ISOB4", DMPAPER_ISO_B4},
152 {"ISOB5Extra", DMPAPER_B5_EXTRA},
153 {"Ledger", DMPAPER_LEDGER},
154 {"Legal", DMPAPER_LEGAL},
155 {"LegalExtra", DMPAPER_LEGAL_EXTRA},
156 {"Letter", DMPAPER_LETTER},
157 {"Letter.Transverse", DMPAPER_LETTER_TRANSVERSE},
158 {"LetterExtra", DMPAPER_LETTER_EXTRA},
159 {"LetterExtra.Transverse", DMPAPER_LETTER_EXTRA_TRANSVERSE},
160 {"LetterPlus", DMPAPER_LETTER_PLUS},
161 {"LetterRotated", DMPAPER_LETTER_ROTATED},
162 {"LetterSmall", DMPAPER_LETTERSMALL},
163 {"Monarch", DMPAPER_ENV_MONARCH}, /* use EnvMonarch */
164 {"Note", DMPAPER_NOTE},
165 {"PRC16K", DMPAPER_P16K},
166 {"PRC16KRotated", DMPAPER_P16K_ROTATED},
167 {"PRC32K", DMPAPER_P32K},
168 {"PRC32KBig", DMPAPER_P32KBIG},
169 {"PRC32KBigRotated", DMPAPER_P32KBIG_ROTATED},
170 {"PRC32KRotated", DMPAPER_P32K_ROTATED},
171 {"Postcard", DMPAPER_JAPANESE_POSTCARD},
172 {"PostcardRotated", DMPAPER_JAPANESE_POSTCARD_ROTATED},
173 {"Quarto", DMPAPER_QUARTO},
174 {"Statement", DMPAPER_STATEMENT},
175 {"SuperA", DMPAPER_A_PLUS},
176 {"SuperB", DMPAPER_B_PLUS},
177 {"Tabloid", DMPAPER_TABLOID},
178 {"TabloidExtra", DMPAPER_TABLOID_EXTRA},
179 {NULL, 0}
182 /***********************************************************************
184 * PSDRV_PPDDecodeHex
186 * Copies str into a newly allocated string from the process heap substituting
187 * hex strings enclosed in '<' and '>' for their byte codes.
190 static char *PSDRV_PPDDecodeHex(char *str)
192 char *buf, *in, *out;
193 BOOL inhex = FALSE;
195 buf = HeapAlloc(PSDRV_Heap, 0, strlen(str) + 1);
196 if(!buf)
197 return NULL;
199 for(in = str, out = buf; *in; in++) {
200 if(!inhex) {
201 if(*in != '<')
202 *out++ = *in;
203 else
204 inhex = TRUE;
205 } else {
206 if(*in == '>') {
207 inhex = FALSE;
208 continue;
210 else if(isspace(*in))
211 continue;
212 else {
213 int i;
214 if(!isxdigit(*in) || !isxdigit(*(in + 1))) {
215 ERR("Invalid hex char in hex string\n");
216 HeapFree(PSDRV_Heap, 0, buf);
217 return NULL;
219 *out = 0;
220 for(i = 0; i < 2; i++) {
221 if(isdigit(*(in + i)))
222 *out |= (*(in + i) - '0') << ((1-i) * 4);
223 else
224 *out |= (toupper(*(in + i)) - 'A' + 10) << ((1-i) * 4);
226 out++;
227 in++;
231 *out = '\0';
232 return buf;
236 /***********************************************************************
238 * PSDRV_PPDGetTransValue
241 static BOOL PSDRV_PPDGetTransValue(const char *start, PPDTuple *tuple)
243 char *buf;
244 const char *end;
246 end = strpbrk(start, "\r\n");
247 if(end == start) return FALSE;
248 if(!end) end = start + strlen(start);
249 buf = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
250 memcpy(buf, start, end - start);
251 *(buf + (end - start)) = '\0';
252 tuple->valtrans = PSDRV_PPDDecodeHex(buf);
253 HeapFree( PSDRV_Heap, 0, buf );
254 return TRUE;
257 static BOOL get_line( char *buf, int size, struct map_context *ctx )
259 int i;
260 if (ctx->pos > ctx->end) return FALSE;
262 for (i = 0; i < size - 1; i++)
264 if (ctx->pos > ctx->end) break;
265 buf[i] = *ctx->pos++;
267 /* \r\n -> \n */
268 if (buf[i] == '\r' && ctx->pos <= ctx->end && *ctx->pos == '\n')
270 ctx->pos++;
271 buf[i] = '\n';
274 if (buf[i] == '\n' || buf[i] == '\r')
276 i++;
277 break;
280 buf[i] = '\0';
281 return TRUE;
284 /***********************************************************************
286 * PSDRV_PPDGetInvocationValue
288 * Passed string that should be surrounded by `"'s, return string alloced
289 * from process heap.
291 static BOOL PSDRV_PPDGetInvocationValue(struct map_context *ctx, PPDTuple *tuple)
293 const char *start;
294 char *buf, line[257];
296 assert( *ctx->pos == '"' );
298 ctx->pos++;
299 for (start = ctx->pos; ctx->pos <= ctx->end; ctx->pos++)
300 if (*ctx->pos == '"') break;
301 if (ctx->pos > ctx->end) return FALSE;
302 ctx->pos++;
304 buf = HeapAlloc( PSDRV_Heap, 0, ctx->pos - start );
305 memcpy( buf, start, ctx->pos - start - 1 );
306 buf[ctx->pos - start - 1] = '\0';
307 tuple->value = buf;
309 if (get_line( line, sizeof(line), ctx ))
311 start = strchr( line, '/' );
312 if (start) return PSDRV_PPDGetTransValue( start + 1, tuple );
314 return TRUE;
318 /***********************************************************************
320 * PSDRV_PPDGetQuotedValue
322 * Passed string that should be surrounded by `"'s. Expand <xx> as hex
323 * return string alloced from process heap.
325 static BOOL PSDRV_PPDGetQuotedValue(struct map_context *ctx, PPDTuple *tuple)
327 char *buf;
329 if(!PSDRV_PPDGetInvocationValue(ctx, tuple))
330 return FALSE;
331 buf = PSDRV_PPDDecodeHex(tuple->value);
332 HeapFree(PSDRV_Heap, 0, tuple->value);
333 tuple->value = buf;
334 return TRUE;
338 /***********************************************************************
340 * PSDRV_PPDGetStringValue
342 * Just strip leading white space.
344 static BOOL PSDRV_PPDGetStringValue(char *str, PPDTuple *tuple)
346 char *start = str, *end;
348 while(*start != '\0' && isspace(*start))
349 start++;
351 end = strpbrk(start, "/\r\n");
352 if(!end) end = start + strlen(start);
353 tuple->value = HeapAlloc( PSDRV_Heap, 0, (end - start) + 1 );
354 memcpy(tuple->value, start, end - start);
355 *(tuple->value + (end - start)) = '\0';
356 if(*end == '/')
357 PSDRV_PPDGetTransValue(end + 1, tuple);
358 return TRUE;
362 /***********************************************************************
364 * PSDRV_PPDSymbolValue
366 * Not implemented yet.
368 static BOOL PSDRV_PPDGetSymbolValue(char *pos, PPDTuple *tuple)
370 FIXME("Stub\n");
371 return FALSE;
375 /*********************************************************************
377 * PSDRV_PPDGetNextTuple
379 * Gets the next Keyword Option Value tuple from the file. Allocs space off
380 * the process heap which should be free()ed by the caller if not needed.
382 static BOOL PSDRV_PPDGetNextTuple(struct map_context *ctx, PPDTuple *tuple)
384 char line[257], *opt, *cp, *trans, *endkey;
385 BOOL gotoption;
386 struct map_context save;
388 start:
390 gotoption = TRUE;
391 opt = NULL;
392 memset(tuple, 0, sizeof(*tuple));
394 do {
395 save = *ctx;
396 if(!get_line(line, sizeof(line), ctx))
397 return FALSE;
398 if(line[0] == '*' && line[1] != '%' && strncmp(line, "*End", 4))
399 break;
400 } while(1);
402 cp = line + strlen(line) - 1;
403 if (*cp != '\n' && *cp != '\r')
405 ERR("Line too long.\n");
406 goto start;
409 for(cp = line; !isspace(*cp) && *cp != ':'; cp++)
412 endkey = cp;
413 while (isspace(*cp)) cp++;
414 if (*cp == ':') /* <key>: */
415 gotoption = FALSE;
416 else /* <key> <option> */
417 opt = cp;
419 tuple->key = HeapAlloc( PSDRV_Heap, 0, endkey - line + 1 );
420 if(!tuple->key) return FALSE;
422 memcpy(tuple->key, line, endkey - line);
423 tuple->key[endkey - line] = '\0';
425 if(gotoption) { /* opt points to 1st non-space character of the option */
426 cp = strpbrk(opt, ":/");
427 if(!cp) {
428 ERR("Error in line '%s'?\n", line);
429 HeapFree(GetProcessHeap(), 0, tuple->key);
430 goto start;
432 tuple->option = HeapAlloc( PSDRV_Heap, 0, cp - opt + 1 );
433 if(!tuple->option) return FALSE;
434 memcpy(tuple->option, opt, cp - opt);
435 tuple->option[cp - opt] = '\0';
436 if(*cp == '/') {
437 char *buf;
438 trans = cp + 1;
439 cp = strchr(trans, ':');
440 if(!cp) {
441 ERR("Error in line '%s'?\n", line);
442 HeapFree(GetProcessHeap(), 0, tuple->option);
443 HeapFree(GetProcessHeap(), 0, tuple->key);
444 goto start;
446 buf = HeapAlloc( PSDRV_Heap, 0, cp - trans + 1 );
447 if(!buf) return FALSE;
448 memcpy(buf, trans, cp - trans);
449 buf[cp - trans] = '\0';
450 tuple->opttrans = PSDRV_PPDDecodeHex(buf);
451 HeapFree( PSDRV_Heap, 0, buf );
455 /* cp should point to a ':', so we increment past it */
456 cp++;
458 while(isspace(*cp))
459 cp++;
461 switch(*cp) {
462 case '"':
463 /* update the context pos so that it points to the opening quote */
464 ctx->pos = save.pos + (cp - line);
465 if( (!gotoption && strncmp(tuple->key, "*?", 2) ) ||
466 !strncmp(tuple->key, "*JCL", 4))
467 PSDRV_PPDGetQuotedValue(ctx, tuple);
468 else
469 PSDRV_PPDGetInvocationValue(ctx, tuple);
470 break;
472 case '^':
473 PSDRV_PPDGetSymbolValue(cp, tuple);
474 break;
476 default:
477 PSDRV_PPDGetStringValue(cp, tuple);
479 return TRUE;
482 /*********************************************************************
483 * get_pagesize
485 * Searches ppd PageSize list to return entry matching name or optionally creates new
486 * entry which is appended to the list if name is not found.
488 static PAGESIZE *get_pagesize( PPD *ppd, char *name, BOOL create )
490 PAGESIZE *page;
492 LIST_FOR_EACH_ENTRY(page, &ppd->PageSizes, PAGESIZE, entry)
494 if(!strcmp(page->Name, name))
495 return page;
498 if (!create) return NULL;
500 page = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*page) );
501 list_add_tail(&ppd->PageSizes, &page->entry);
502 return page;
505 static DUPLEX *get_duplex( PPD *ppd, const char *name )
507 DUPLEX *duplex;
509 LIST_FOR_EACH_ENTRY( duplex, &ppd->Duplexes, DUPLEX, entry )
511 if (!strcmp( duplex->Name, name ))
512 return duplex;
515 return NULL;
518 /**********************************************************************
520 * PSDRV_PPDGetWord
522 * Returns ptr alloced from heap to first word in str. Strips leading spaces.
523 * Puts ptr to next word in next
525 static char *PSDRV_PPDGetWord(char *str, char **next)
527 char *start, *end, *ret;
529 start = str;
530 while(start && *start && isspace(*start))
531 start++;
532 if(!start || !*start) return FALSE;
534 end = start;
535 while(*end && !isspace(*end))
536 end++;
538 ret = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
539 memcpy(ret, start, end - start );
540 *(ret + (end - start)) = '\0';
542 while(*end && isspace(*end))
543 end++;
544 if(*end)
545 *next = end;
546 else
547 *next = NULL;
549 return ret;
552 /************************************************************
553 * parse_resolution
555 * Helper to extract x and y resolutions from a resolution entry.
556 * Returns TRUE on successful parsing, otherwise FALSE.
558 * The ppd spec says that entries can either be:
559 * "nnndpi"
560 * or
561 * "nnnxmmmdpi"
562 * in the former case return sz.cx == cx.cy == nnn.
564 * There are broken ppd files out there (notably from Samsung) that
565 * use the form "nnnmmmdpi", so we have to work harder to spot these.
566 * We use a transition from a zero to a non-zero digit as separator
567 * (and fail if we find more than one of these). We also don't bother
568 * trying to parse "dpi" in case that's missing.
570 static BOOL parse_resolution(const char *str, SIZE *sz)
572 int tmp[2];
573 int *cur;
574 BOOL had_zero;
575 const char *c;
577 if(sscanf(str, "%dx%d", tmp, tmp + 1) == 2)
579 sz->cx = tmp[0];
580 sz->cy = tmp[1];
581 return TRUE;
584 tmp[0] = 0;
585 tmp[1] = -1;
586 cur = tmp;
587 had_zero = FALSE;
588 for(c = str; isdigit(*c); c++)
590 if(!had_zero || *c == '0')
592 *cur *= 10;
593 *cur += *c - '0';
594 if(*c == '0')
595 had_zero = TRUE;
597 else if(cur != tmp)
598 return FALSE;
599 else
601 cur++;
602 *cur = *c - '0';
603 had_zero = FALSE;
606 if(tmp[0] == 0) return FALSE;
608 sz->cx = tmp[0];
609 if(tmp[1] != -1)
610 sz->cy = tmp[1];
611 else
612 sz->cy = sz->cx;
613 return TRUE;
616 /*******************************************************************************
617 * PSDRV_AddSlot
620 static BOOL PSDRV_AddSlot(PPD *ppd, LPCSTR szName, LPCSTR szFullName,
621 LPSTR szInvocationString, WORD wWinBin)
623 INPUTSLOT *slot = HeapAlloc( PSDRV_Heap, 0, sizeof(INPUTSLOT) );
624 if (!slot) return FALSE;
626 slot->Name = szName;
627 slot->FullName = szFullName;
628 slot->InvocationString = szInvocationString;
629 slot->WinBin = wWinBin;
631 list_add_tail( &ppd->InputSlots, &slot->entry );
632 return TRUE;
635 static char *get_ppd_override( HANDLE printer, const char *value )
637 DWORD err, type, needed;
638 char *data;
640 err = GetPrinterDataExA( printer, "PPD Overrides", value, &type, NULL, 0, &needed );
641 if (err != ERROR_MORE_DATA || type != REG_SZ || needed == 0) return NULL;
643 data = HeapAlloc( PSDRV_Heap, 0, needed );
644 if (data)
646 GetPrinterDataExA( printer, "PPD Overrides", value, &type, (BYTE*)data, needed, &needed );
647 TRACE( "got override %s: %s\n", value, data );
649 return data;
652 static BOOL map_file( const WCHAR *filename, struct map_context *c )
654 HANDLE file, mapping;
655 LARGE_INTEGER size;
657 file = CreateFileW( filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
658 if (file == INVALID_HANDLE_VALUE) return FALSE;
660 if (!GetFileSizeEx( file, &size ) || size.u.HighPart)
662 CloseHandle( file );
663 return FALSE;
666 mapping = CreateFileMappingW( file, NULL, PAGE_READONLY, 0, 0, NULL );
667 CloseHandle( file );
668 if (!mapping) return FALSE;
670 c->pos = c->ptr = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
671 c->end = c->ptr + size.u.LowPart - 1;
672 CloseHandle( mapping );
673 return TRUE;
676 static void unmap_file( struct map_context *c )
678 UnmapViewOfFile( c->ptr );
681 /***********************************************************************
683 * PSDRV_ParsePPD
687 PPD *PSDRV_ParsePPD( const WCHAR *fname, HANDLE printer )
689 PPD *ppd;
690 PPDTuple tuple;
691 char *default_pagesize = NULL, *default_duplex = NULL;
692 char *def_pagesize_override = NULL, *def_duplex_override = NULL;
693 PAGESIZE *page, *page_cursor2;
694 struct map_context c;
695 WORD UserPageType = DMPAPER_USER;
696 WORD UserBinType = DMBIN_USER;
698 TRACE("file %s\n", debugstr_w(fname));
700 if (!map_file( fname, &c ))
702 WARN("Couldn't open ppd file %s\n", debugstr_w(fname));
703 return NULL;
706 ppd = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*ppd));
707 if(!ppd) {
708 ERR("Unable to allocate memory for ppd\n");
709 unmap_file( &c );
710 return NULL;
713 ppd->ColorDevice = CD_NotSpecified;
715 list_init( &ppd->Resolutions );
716 list_init( &ppd->InstalledFonts );
717 list_init( &ppd->PageSizes );
718 list_init( &ppd->Constraints );
719 list_init( &ppd->InputSlots );
720 list_init( &ppd->Duplexes );
722 /* Some gimp-print ppd files don't contain a DefaultResolution line
723 so default to 300 */
724 ppd->DefaultResolution = 300;
727 * The Windows PostScript drivers create the following "virtual bin" for
728 * every PostScript printer
730 if (!PSDRV_AddSlot( ppd, NULL, "Automatically Select", NULL, DMBIN_FORMSOURCE ))
732 HeapFree (PSDRV_Heap, 0, ppd);
733 unmap_file( &c );
734 return NULL;
737 while (PSDRV_PPDGetNextTuple( &c, &tuple ))
739 if(!strcmp("*NickName", tuple.key)) {
740 ppd->NickName = tuple.value;
741 tuple.value = NULL;
742 TRACE("NickName = '%s'\n", ppd->NickName);
745 else if(!strcmp("*LanguageLevel", tuple.key)) {
746 sscanf(tuple.value, "%d", &(ppd->LanguageLevel));
747 TRACE("LanguageLevel = %d\n", ppd->LanguageLevel);
750 else if(!strcmp("*ColorDevice", tuple.key)) {
751 if(!strcasecmp(tuple.value, "true"))
752 ppd->ColorDevice = CD_True;
753 else
754 ppd->ColorDevice = CD_False;
755 TRACE("ColorDevice = %s\n", ppd->ColorDevice == CD_True ? "True" : "False");
758 else if((!strcmp("*DefaultResolution", tuple.key)) ||
759 (!strcmp("*DefaultJCLResolution", tuple.key))) {
760 SIZE sz;
761 if(parse_resolution(tuple.value, &sz))
763 TRACE("DefaultResolution %dx%d\n", sz.cx, sz.cy);
764 ppd->DefaultResolution = sz.cx;
766 else
767 WARN("failed to parse DefaultResolution %s\n", debugstr_a(tuple.value));
770 else if(!strcmp("*Resolution", tuple.key))
772 SIZE sz;
773 if (parse_resolution(tuple.option, &sz))
775 RESOLUTION *res;
777 TRACE("Resolution %dx%d, invocation %s\n", sz.cx, sz.cy, tuple.value);
779 res = HeapAlloc( GetProcessHeap(), 0, sizeof(*res) );
780 res->resx = sz.cx;
781 res->resy = sz.cy;
782 res->InvocationString = tuple.value;
783 tuple.value = NULL;
784 list_add_tail( &ppd->Resolutions, &res->entry );
786 else
787 WARN("failed to parse Resolution %s\n", debugstr_a(tuple.option));
790 else if(!strcmp("*Font", tuple.key))
792 FONTNAME *fn = HeapAlloc( PSDRV_Heap, 0, sizeof(*fn) );
793 fn->Name = tuple.option;
794 tuple.option = NULL;
795 list_add_tail( &ppd->InstalledFonts, &fn->entry );
798 else if(!strcmp("*DefaultFont", tuple.key)) {
799 ppd->DefaultFont = tuple.value;
800 tuple.value = NULL;
803 else if(!strcmp("*JCLBegin", tuple.key)) {
804 ppd->JCLBegin = tuple.value;
805 tuple.value = NULL;
808 else if(!strcmp("*JCLToPSInterpreter", tuple.key)) {
809 ppd->JCLToPSInterpreter = tuple.value;
810 tuple.value = NULL;
813 else if(!strcmp("*JCLEnd", tuple.key)) {
814 ppd->JCLEnd = tuple.value;
815 tuple.value = NULL;
818 else if(!strcmp("*PageSize", tuple.key))
820 page = get_pagesize( ppd, tuple.option, TRUE );
822 if(!page->Name) {
823 page->Name = tuple.option;
824 tuple.option = NULL;
826 if(!page->FullName) {
827 if(tuple.opttrans) {
828 page->FullName = tuple.opttrans;
829 tuple.opttrans = NULL;
830 } else
832 page->FullName = HeapAlloc( PSDRV_Heap, 0, strlen(page->Name)+1 );
833 strcpy( page->FullName, page->Name );
836 if(!page->InvocationString) {
837 page->InvocationString = tuple.value;
838 tuple.value = NULL;
840 if (!page->WinPage)
842 int i;
843 for (i = 0; PageTrans[i].PSName; i++)
845 if (!strcmp( PageTrans[i].PSName, page->Name ))
847 page->WinPage = PageTrans[i].WinPage;
848 break;
851 if (!page->WinPage)
853 TRACE( "Can't find Windows page type for %s - using %u\n", debugstr_a(page->Name), UserPageType );
854 page->WinPage = UserPageType++;
859 else if(!strcmp("*DefaultPageSize", tuple.key))
861 if (!default_pagesize)
863 default_pagesize = tuple.value;
864 tuple.value = NULL;
868 else if(!strcmp("*ImageableArea", tuple.key)) {
869 page = get_pagesize( ppd, tuple.option, TRUE );
871 if(!page->Name) {
872 page->Name = tuple.option;
873 tuple.option = NULL;
875 if(!page->FullName) {
876 page->FullName = tuple.opttrans;
877 tuple.opttrans = NULL;
880 #define PIA page->ImageableArea
881 if(!PIA) {
882 PIA = HeapAlloc( PSDRV_Heap, 0, sizeof(*PIA) );
883 push_lc_numeric("C");
884 sscanf(tuple.value, "%f%f%f%f", &PIA->llx, &PIA->lly,
885 &PIA->urx, &PIA->ury);
886 pop_lc_numeric();
888 #undef PIA
891 else if(!strcmp("*PaperDimension", tuple.key)) {
892 page = get_pagesize( ppd, tuple.option, TRUE );
894 if(!page->Name) {
895 page->Name = tuple.option;
896 tuple.option = NULL;
898 if(!page->FullName) {
899 page->FullName = tuple.opttrans;
900 tuple.opttrans = NULL;
903 #define PD page->PaperDimension
904 if(!PD) {
905 PD = HeapAlloc( PSDRV_Heap, 0, sizeof(*PD) );
906 push_lc_numeric("C");
907 sscanf(tuple.value, "%f%f", &PD->x, &PD->y);
908 pop_lc_numeric();
910 #undef PD
913 else if(!strcmp("*LandscapeOrientation", tuple.key)) {
914 if(!strcmp(tuple.value, "Plus90"))
915 ppd->LandscapeOrientation = 90;
916 else if(!strcmp(tuple.value, "Minus90"))
917 ppd->LandscapeOrientation = -90;
919 /* anything else, namely 'any', leaves value at 0 */
921 TRACE("LandscapeOrientation = %d\n",
922 ppd->LandscapeOrientation);
925 else if(!strcmp("*UIConstraints", tuple.key))
927 char *start;
928 CONSTRAINT *con = HeapAlloc( PSDRV_Heap, 0, sizeof(*con) );
930 start = tuple.value;
931 con->Feature1 = PSDRV_PPDGetWord(start, &start);
932 con->Value1 = PSDRV_PPDGetWord(start, &start);
933 con->Feature2 = PSDRV_PPDGetWord(start, &start);
934 con->Value2 = PSDRV_PPDGetWord(start, &start);
936 list_add_tail( &ppd->Constraints, &con->entry );
939 else if (!strcmp("*InputSlot", tuple.key))
942 if (!tuple.opttrans)
943 tuple.opttrans = tuple.option;
945 TRACE("Using Windows bin type %u for '%s'\n", UserBinType,
946 tuple.option);
948 /* FIXME - should check for failure */
949 PSDRV_AddSlot(ppd, tuple.option, tuple.opttrans, tuple.value,
950 UserBinType++);
952 tuple.option = tuple.opttrans = tuple.value = NULL;
956 * Windows treats "manual feed" as another paper source. Most PPD
957 * files, however, treat it as a separate option which is either on or
958 * off.
960 else if (!strcmp("*ManualFeed", tuple.key) && tuple.option &&
961 !strcmp ("True", tuple.option))
963 /* FIXME - should check for failure */
964 PSDRV_AddSlot(ppd, "Manual Feed", "Manual Feed", tuple.value, DMBIN_MANUAL);
965 tuple.value = NULL;
968 else if(!strcmp("*TTRasterizer", tuple.key)) {
969 if(!strcasecmp("None", tuple.value))
970 ppd->TTRasterizer = RO_None;
971 else if(!strcasecmp("Accept68K", tuple.value))
972 ppd->TTRasterizer = RO_Accept68K;
973 else if(!strcasecmp("Type42", tuple.value))
974 ppd->TTRasterizer = RO_Type42;
975 else if(!strcasecmp("TrueImage", tuple.value))
976 ppd->TTRasterizer = RO_TrueImage;
977 else {
978 FIXME("Unknown option %s for *TTRasterizer\n",
979 tuple.value);
980 ppd->TTRasterizer = RO_None;
982 TRACE("*TTRasterizer = %d\n", ppd->TTRasterizer);
985 else if(!strcmp("*Duplex", tuple.key))
987 DUPLEX *duplex = HeapAlloc( GetProcessHeap(), 0, sizeof(*duplex) );
988 duplex->Name = tuple.option;
989 duplex->FullName = tuple.opttrans;
990 duplex->InvocationString = tuple.value;
991 if(!strcasecmp("None", tuple.option) || !strcasecmp("False", tuple.option)
992 || !strcasecmp("Simplex", tuple.option))
993 duplex->WinDuplex = DMDUP_SIMPLEX;
994 else if(!strcasecmp("DuplexNoTumble", tuple.option))
995 duplex->WinDuplex = DMDUP_VERTICAL;
996 else if(!strcasecmp("DuplexTumble", tuple.option))
997 duplex->WinDuplex = DMDUP_HORIZONTAL;
998 else if(!strcasecmp("Notcapable", tuple.option))
999 duplex->WinDuplex = 0;
1000 else {
1001 FIXME("Unknown option %s for *Duplex defaulting to simplex\n", tuple.option);
1002 duplex->WinDuplex = DMDUP_SIMPLEX;
1004 tuple.option = tuple.opttrans = tuple.value = NULL;
1005 list_add_tail( &ppd->Duplexes, &duplex->entry );
1008 else if (!strcmp("*DefaultDuplex", tuple.key))
1010 if (!default_duplex)
1012 default_duplex = tuple.value;
1013 tuple.value = NULL;
1017 HeapFree(PSDRV_Heap, 0, tuple.key);
1018 HeapFree(PSDRV_Heap, 0, tuple.option);
1019 HeapFree(PSDRV_Heap, 0, tuple.value);
1020 HeapFree(PSDRV_Heap, 0, tuple.opttrans);
1021 HeapFree(PSDRV_Heap, 0, tuple.valtrans);
1025 /* Remove any partial page size entries, that is any without a PageSize or a PaperDimension (we can
1026 cope with a missing ImageableArea). */
1027 LIST_FOR_EACH_ENTRY_SAFE(page, page_cursor2, &ppd->PageSizes, PAGESIZE, entry)
1029 if(!page->InvocationString || !page->PaperDimension)
1031 WARN("Removing page %s since it has a missing %s entry\n", debugstr_a(page->FullName),
1032 page->InvocationString ? "PaperDimension" : "InvocationString");
1033 HeapFree(PSDRV_Heap, 0, page->Name);
1034 HeapFree(PSDRV_Heap, 0, page->FullName);
1035 HeapFree(PSDRV_Heap, 0, page->InvocationString);
1036 HeapFree(PSDRV_Heap, 0, page->ImageableArea);
1037 HeapFree(PSDRV_Heap, 0, page->PaperDimension);
1038 list_remove(&page->entry);
1042 ppd->DefaultPageSize = NULL;
1043 def_pagesize_override = get_ppd_override( printer, "DefaultPageSize" );
1044 if (def_pagesize_override)
1045 ppd->DefaultPageSize = get_pagesize( ppd, def_pagesize_override, FALSE );
1046 if (!ppd->DefaultPageSize && default_pagesize)
1047 ppd->DefaultPageSize = get_pagesize( ppd, default_pagesize, FALSE );
1049 if (!ppd->DefaultPageSize)
1051 struct list *head = list_head( &ppd->PageSizes );
1052 if (head) ppd->DefaultPageSize = LIST_ENTRY( head, PAGESIZE, entry );
1053 TRACE("Setting DefaultPageSize to first in list\n");
1055 TRACE( "DefaultPageSize: %s\n", ppd->DefaultPageSize ? ppd->DefaultPageSize->Name : "<not set>" );
1057 HeapFree( PSDRV_Heap, 0, def_pagesize_override );
1058 HeapFree( PSDRV_Heap, 0, default_pagesize );
1060 ppd->DefaultDuplex = NULL;
1061 def_duplex_override = get_ppd_override( printer, "DefaultDuplex" );
1062 if (def_duplex_override)
1063 ppd->DefaultDuplex = get_duplex( ppd, def_duplex_override );
1064 if (!ppd->DefaultDuplex && default_duplex)
1065 ppd->DefaultDuplex = get_duplex( ppd, default_duplex );
1067 if (!ppd->DefaultDuplex)
1069 struct list *head = list_head( &ppd->Duplexes );
1070 if (head) ppd->DefaultDuplex = LIST_ENTRY( head, DUPLEX, entry );
1071 TRACE("Setting DefaultDuplex to first in list\n");
1073 TRACE( "DefaultDuplex: %s\n", ppd->DefaultDuplex ? ppd->DefaultDuplex->Name : "<not set>" );
1075 HeapFree( PSDRV_Heap, 0, def_duplex_override );
1076 HeapFree( PSDRV_Heap, 0, default_duplex );
1080 FONTNAME *fn;
1081 PAGESIZE *page;
1082 CONSTRAINT *con;
1083 INPUTSLOT *slot;
1085 LIST_FOR_EACH_ENTRY( fn, &ppd->InstalledFonts, FONTNAME, entry )
1086 TRACE("'%s'\n", fn->Name);
1088 LIST_FOR_EACH_ENTRY(page, &ppd->PageSizes, PAGESIZE, entry) {
1089 TRACE("'%s' aka '%s' (%d) invoked by '%s'\n", page->Name,
1090 page->FullName, page->WinPage, page->InvocationString);
1091 if(page->ImageableArea)
1092 TRACE("Area = %.2f,%.2f - %.2f, %.2f\n",
1093 page->ImageableArea->llx, page->ImageableArea->lly,
1094 page->ImageableArea->urx, page->ImageableArea->ury);
1095 if(page->PaperDimension)
1096 TRACE("Dimension = %.2f x %.2f\n",
1097 page->PaperDimension->x, page->PaperDimension->y);
1100 LIST_FOR_EACH_ENTRY( con, &ppd->Constraints, CONSTRAINT, entry )
1101 TRACE("CONSTRAINTS@ %s %s %s %s\n", con->Feature1,
1102 con->Value1, con->Feature2, con->Value2);
1104 LIST_FOR_EACH_ENTRY( slot, &ppd->InputSlots, INPUTSLOT, entry )
1105 TRACE("INPUTSLOTS '%s' Name '%s' (%d) Invocation '%s'\n",
1106 debugstr_a(slot->Name), slot->FullName, slot->WinBin,
1107 debugstr_a(slot->InvocationString));
1110 unmap_file( &c );
1111 return ppd;