- Add support for TBSTYLE_EX_MIXEDBUTTONS.
[wine] / dlls / wineps / ppd.c
1 /*      PostScript Printer Description (PPD) file parser
2  *
3  *      See  http://www.adobe.com/supportservice/devrelations/PDFS/TN/5003.PPD_Spec_v4.3.pdf
4  *
5  *      Copyright 1998  Huw D M Davies
6  *
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.
11  *
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.
16  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wine/debug.h"
29 #include "psdrv.h"
30 #include "winspool.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
33
34 typedef struct {
35 char    *key;
36 char    *option;
37 char    *opttrans;
38 char    *value;
39 char    *valtrans;
40 } PPDTuple;
41
42
43 /* map of page names in ppd file to Windows paper constants */
44
45 static const struct {
46   const char *PSName;
47   WORD WinPage;
48 } PageTrans[] = {
49   {"10x11",                   DMPAPER_10X11},
50   {"10x14",                   DMPAPER_10X14},
51   {"11x17",                   DMPAPER_11X17},   /* not in Adobe PPD file spec */
52   {"12x11",                   DMPAPER_12X11},
53   {"15x11",                   DMPAPER_15X11},
54   {"9x11",                    DMPAPER_9X11},
55   {"A2",                      DMPAPER_A2},
56   {"A3",                      DMPAPER_A3},
57   {"A3.Transverse",           DMPAPER_A3_TRANSVERSE},
58   {"A3Extra",                 DMPAPER_A3_EXTRA},
59   {"A3Extra.Transverse",      DMPAPER_A3_EXTRA_TRANSVERSE},
60   {"A3Rotated",               DMPAPER_A3_ROTATED},
61   {"A4",                      DMPAPER_A4},
62   {"A4.Transverse",           DMPAPER_A4_TRANSVERSE},
63   {"A4Extra",                 DMPAPER_A4_EXTRA},
64   {"A4Plus",                  DMPAPER_A4_PLUS},
65   {"A4Rotated",               DMPAPER_A4_ROTATED},
66   {"A4Small",                 DMPAPER_A4SMALL},
67   {"A5",                      DMPAPER_A5},
68   {"A5.Transverse",           DMPAPER_A5_TRANSVERSE},
69   {"A5Extra",                 DMPAPER_A5_EXTRA},
70   {"A5Rotated",               DMPAPER_A5_ROTATED},
71   {"A6",                      DMPAPER_A6},
72   {"A6Rotated",               DMPAPER_A6_ROTATED},
73   {"ARCHC",                   DMPAPER_CSHEET},
74   {"ARCHD",                   DMPAPER_DSHEET},
75   {"ARCHE",                   DMPAPER_ESHEET},
76   {"B4",                      DMPAPER_B4},
77   {"B4Rotated",               DMPAPER_B4_JIS_ROTATED},
78   {"B5",                      DMPAPER_B5},
79   {"B5.Transverse",           DMPAPER_B5_TRANSVERSE},
80   {"B5Rotated",               DMPAPER_B5_JIS_ROTATED},
81   {"B6",                      DMPAPER_B6_JIS},
82   {"B6Rotated",               DMPAPER_B6_JIS_ROTATED},
83   {"C4",                      DMPAPER_ENV_C4},          /*  use EnvC4 */
84   {"C5",                      DMPAPER_ENV_C5},          /*  use EnvC5 */
85   {"C6",                      DMPAPER_ENV_C6},          /*  use EnvC6 */
86   {"Comm10",                  DMPAPER_ENV_10},          /*  use Env10 */
87   {"DL",                      DMPAPER_ENV_DL},          /*  use EnvDL */
88   {"DoublePostcard",          DMPAPER_DBL_JAPANESE_POSTCARD},
89   {"DoublePostcardRotated",   DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED},
90   {"Env10",                   DMPAPER_ENV_10},
91   {"Env11",                   DMPAPER_ENV_11},
92   {"Env12",                   DMPAPER_ENV_12},
93   {"Env14",                   DMPAPER_ENV_14},
94   {"Env9",                    DMPAPER_ENV_9},
95   {"EnvC3",                   DMPAPER_ENV_C3},
96   {"EnvC4",                   DMPAPER_ENV_C4},
97   {"EnvC5",                   DMPAPER_ENV_C5},
98   {"EnvC6",                   DMPAPER_ENV_C6},
99   {"EnvC65",                  DMPAPER_ENV_C65},
100   {"EnvChou3",                DMPAPER_JENV_CHOU3},
101   {"EnvChou3Rotated",         DMPAPER_JENV_CHOU3_ROTATED},
102   {"EnvChou4",                DMPAPER_JENV_CHOU4},
103   {"EnvChou4Rotated",         DMPAPER_JENV_CHOU4_ROTATED},
104   {"EnvDL",                   DMPAPER_ENV_DL},
105   {"EnvISOB4",                DMPAPER_ENV_B4},
106   {"EnvISOB5",                DMPAPER_ENV_B5},
107   {"EnvISOB6",                DMPAPER_ENV_B6},
108   {"EnvInvite",               DMPAPER_ENV_INVITE},
109   {"EnvItalian",              DMPAPER_ENV_ITALY},
110   {"EnvKaku2",                DMPAPER_JENV_KAKU2},
111   {"EnvKaku2Rotated",         DMPAPER_JENV_KAKU2_ROTATED},
112   {"EnvKaku3",                DMPAPER_JENV_KAKU3},
113   {"EnvKaku3Rotated",         DMPAPER_JENV_KAKU3_ROTATED},
114   {"EnvMonarch",              DMPAPER_ENV_MONARCH},
115   {"EnvPRC1",                 DMPAPER_PENV_1},
116   {"EnvPRC10",                DMPAPER_PENV_10},
117   {"EnvPRC10Rotated",         DMPAPER_PENV_10_ROTATED},
118   {"EnvPRC1Rotated",          DMPAPER_PENV_1_ROTATED},
119   {"EnvPRC2",                 DMPAPER_PENV_2},
120   {"EnvPRC2Rotated",          DMPAPER_PENV_2_ROTATED},
121   {"EnvPRC3",                 DMPAPER_PENV_3},
122   {"EnvPRC3Rotated",          DMPAPER_PENV_3_ROTATED},
123   {"EnvPRC4",                 DMPAPER_PENV_4},
124   {"EnvPRC4Rotated",          DMPAPER_PENV_4_ROTATED},
125   {"EnvPRC5",                 DMPAPER_PENV_5},
126   {"EnvPRC5Rotated",          DMPAPER_PENV_5_ROTATED},
127   {"EnvPRC6",                 DMPAPER_PENV_6},
128   {"EnvPRC6Rotated",          DMPAPER_PENV_6_ROTATED},
129   {"EnvPRC7",                 DMPAPER_PENV_7},
130   {"EnvPRC7Rotated",          DMPAPER_PENV_7_ROTATED},
131   {"EnvPRC8",                 DMPAPER_PENV_8},
132   {"EnvPRC8Rotated",          DMPAPER_PENV_8_ROTATED},
133   {"EnvPRC9",                 DMPAPER_PENV_9},
134   {"EnvPRC9Rotated",          DMPAPER_PENV_9_ROTATED},
135   {"EnvPersonal",             DMPAPER_ENV_PERSONAL},
136   {"EnvYou4",                 DMPAPER_JENV_YOU4},
137   {"EnvYou4Rotated",          DMPAPER_JENV_YOU4_ROTATED},
138   {"Executive",               DMPAPER_EXECUTIVE},
139   {"FanFoldGerman",           DMPAPER_FANFOLD_STD_GERMAN},
140   {"FanFoldGermanLegal",      DMPAPER_FANFOLD_LGL_GERMAN},
141   {"FanFoldUS",               DMPAPER_FANFOLD_US},
142   {"Folio",                   DMPAPER_FOLIO},
143   {"ISOB4",                   DMPAPER_ISO_B4},
144   {"ISOB5Extra",              DMPAPER_B5_EXTRA},
145   {"Ledger",                  DMPAPER_LEDGER},
146   {"Legal",                   DMPAPER_LEGAL},
147   {"LegalExtra",              DMPAPER_LEGAL_EXTRA},
148   {"Letter",                  DMPAPER_LETTER},
149   {"Letter.Transverse",       DMPAPER_LETTER_TRANSVERSE},
150   {"LetterExtra",             DMPAPER_LETTER_EXTRA},
151   {"LetterExtra.Transverse",  DMPAPER_LETTER_EXTRA_TRANSVERSE},
152   {"LetterPlus",              DMPAPER_LETTER_PLUS},
153   {"LetterRotated",           DMPAPER_LETTER_ROTATED},
154   {"LetterSmall",             DMPAPER_LETTERSMALL},
155   {"Monarch",                 DMPAPER_ENV_MONARCH},     /* use EnvMonarch */
156   {"Note",                    DMPAPER_NOTE},
157   {"PRC16K",                  DMPAPER_P16K},
158   {"PRC16KRotated",           DMPAPER_P16K_ROTATED},
159   {"PRC32K",                  DMPAPER_P32K},
160   {"PRC32KBig",               DMPAPER_P32KBIG},
161   {"PRC32KBigRotated",        DMPAPER_P32KBIG_ROTATED},
162   {"PRC32KRotated",           DMPAPER_P32K_ROTATED},
163   {"Postcard",                DMPAPER_JAPANESE_POSTCARD},
164   {"PostcardRotated",         DMPAPER_JAPANESE_POSTCARD_ROTATED},
165   {"Quarto",                  DMPAPER_QUARTO},
166   {"Statement",               DMPAPER_STATEMENT},
167   {"SuperA",                  DMPAPER_A_PLUS},
168   {"SuperB",                  DMPAPER_B_PLUS},
169   {"Tabloid",                 DMPAPER_TABLOID},
170   {"TabloidExtra",            DMPAPER_TABLOID_EXTRA},
171   {NULL,                      0}
172 };
173
174 static WORD UserPageType = DMPAPER_USER;
175 static WORD UserBinType = DMBIN_USER;
176
177 /***********************************************************************
178  *
179  *              PSDRV_PPDDecodeHex
180  *
181  * Copies str into a newly allocated string from the process heap subsituting
182  * hex strings enclosed in '<' and '>' for their byte codes.
183  *
184  */
185 static char *PSDRV_PPDDecodeHex(char *str)
186 {
187     char *buf, *in, *out;
188     BOOL inhex = FALSE;
189
190     buf = HeapAlloc(PSDRV_Heap, 0, strlen(str) + 1);
191     if(!buf)
192         return NULL;
193
194     for(in = str, out = buf; *in; in++) {
195         if(!inhex) {
196             if(*in != '<')
197                 *out++ = *in;
198             else
199                 inhex = TRUE;
200         } else {
201             if(*in == '>') {
202                 inhex = FALSE;
203                 continue;
204             }
205             else if(isspace(*in))
206                 continue;
207             else {
208                 int i;
209                 if(!isxdigit(*in) || !isxdigit(*(in + 1))) {
210                     ERR("Invalid hex char in hex string\n");
211                     HeapFree(PSDRV_Heap, 0, buf);
212                     return NULL;
213                 }
214                 *out = 0;
215                 for(i = 0; i < 2; i++) {
216                     if(isdigit(*(in + i)))
217                         *out |= (*(in + i) - '0') << ((1-i) * 4);
218                     else
219                         *out |= (toupper(*(in + i)) - 'A' + 10) << ((1-i) * 4);
220                 }
221                 out++;
222                 in++;
223             }
224         }
225     }
226     *out = '\0';
227     return buf;
228 }
229
230
231 /***********************************************************************
232  *
233  *              PSDRV_PPDGetTransValue
234  *
235  */
236 static BOOL PSDRV_PPDGetTransValue(char *start, PPDTuple *tuple)
237 {
238     char *buf, *end;
239
240     end = strpbrk(start, "\r\n");
241     if(end == start) return FALSE;
242     if(!end) end = start + strlen(start);
243     buf = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
244     memcpy(buf, start, end - start);
245     *(buf + (end - start)) = '\0';
246     tuple->valtrans = PSDRV_PPDDecodeHex(buf);
247     HeapFree( PSDRV_Heap, 0, buf );
248     return TRUE;
249 }
250
251
252 /***********************************************************************
253  *
254  *              PSDRV_PPDGetInvocationValue
255  *
256  * Passed string that should be surrounded by `"'s, return string alloced
257  * from process heap.
258  */
259 static BOOL PSDRV_PPDGetInvocationValue(FILE *fp, char *pos, PPDTuple *tuple)
260 {
261     char *start, *end, *buf;
262     char line[257];
263     int len;
264
265     start = pos + 1;
266     buf = HeapAlloc( PSDRV_Heap, 0, strlen(start) + 1 );
267     len = 0;
268     do {
269         end = strchr(start, '"');
270         if(end) {
271             buf = HeapReAlloc( PSDRV_Heap, 0, buf,
272                                len + (end - start) + 1 );
273             memcpy(buf + len, start, end - start);
274             *(buf + len + (end - start)) = '\0';
275             tuple->value = buf;
276             start = strchr(end, '/');
277             if(start)
278                 return PSDRV_PPDGetTransValue(start + 1, tuple);
279             return TRUE;
280         } else {
281             int sl = strlen(start);
282             buf = HeapReAlloc( PSDRV_Heap, 0, buf, len + sl + 1 );
283             strcpy(buf + len, start);
284             len += sl;
285         }
286     } while( fgets((start = line), sizeof(line), fp) );
287
288     tuple->value = NULL;
289     HeapFree( PSDRV_Heap, 0, buf );
290     return FALSE;
291 }
292
293
294 /***********************************************************************
295  *
296  *              PSDRV_PPDGetQuotedValue
297  *
298  * Passed string that should be surrounded by `"'s. Expand <xx> as hex
299  * return string alloced from process heap.
300  */
301 static BOOL PSDRV_PPDGetQuotedValue(FILE *fp, char *pos, PPDTuple *tuple)
302 {
303     char *buf;
304
305     if(!PSDRV_PPDGetInvocationValue(fp, pos, tuple))
306         return FALSE;
307     buf = PSDRV_PPDDecodeHex(tuple->value);
308     HeapFree(PSDRV_Heap, 0, tuple->value);
309     tuple->value = buf;
310     return TRUE;
311 }
312
313
314 /***********************************************************************
315  *
316  *              PSDRV_PPDGetStringValue
317  *
318  * Just strip leading white space.
319  */
320 static BOOL PSDRV_PPDGetStringValue(char *str, PPDTuple *tuple)
321 {
322     char *start = str, *end;
323
324     while(*start != '\0' && isspace(*start))
325         start++;
326
327     end = strpbrk(start, "/\r\n");
328     if(!end) end = start + strlen(start);
329     tuple->value = HeapAlloc( PSDRV_Heap, 0, (end - start) + 1 );
330     memcpy(tuple->value, start, end - start);
331     *(tuple->value + (end - start)) = '\0';
332     if(*end == '/')
333         PSDRV_PPDGetTransValue(end + 1, tuple);
334     return TRUE;
335 }
336
337
338 /***********************************************************************
339  *
340  *              PSDRV_PPDSymbolValue
341  *
342  * Not implemented yet.
343  */
344 static BOOL PSDRV_PPDGetSymbolValue(char *pos, PPDTuple *tuple)
345 {
346     FIXME("Stub\n");
347     return FALSE;
348 }
349
350
351 /*********************************************************************
352  *
353  *              PSDRV_PPDGetNextTuple
354  *
355  * Gets the next Keyword Option Value tuple from the file. Allocs space off
356  * the process heap which should be free()ed by the caller if not needed.
357  */
358 static BOOL PSDRV_PPDGetNextTuple(FILE *fp, PPDTuple *tuple)
359 {
360     char line[257], *opt, *cp, *trans, *endkey;
361     BOOL gotoption;
362
363  start:
364
365     gotoption = TRUE;
366     opt = NULL;
367     memset(tuple, 0, sizeof(*tuple));
368
369     do {
370         if(!fgets(line, sizeof(line), fp))
371             return FALSE;
372         if(line[0] == '*' && line[1] != '%' && strncmp(line, "*End", 4))
373             break;
374     } while(1);
375
376     if(line[strlen(line)-1] != '\n') {
377         ERR("Line too long.\n");
378         goto start;
379     }
380
381     for(cp = line; !isspace(*cp) && *cp != ':'; cp++)
382         ;
383
384     endkey = cp;
385     if(*cp == ':') { /* <key>: */
386         gotoption = FALSE;
387     } else {
388         while(isspace(*cp))
389             cp++;
390         if(*cp == ':') { /* <key>  : */
391             gotoption = FALSE;
392         } else { /* <key> <option> */
393             opt = cp;
394         }
395     }
396
397     tuple->key = HeapAlloc( PSDRV_Heap, 0, endkey - line + 1 );
398     if(!tuple->key) return FALSE;
399
400     memcpy(tuple->key, line, endkey - line);
401     tuple->key[endkey - line] = '\0';
402
403     if(gotoption) { /* opt points to 1st non-space character of the option */
404         cp = strpbrk(opt, ":/");
405         if(!cp) {
406             ERR("Error in line '%s'?\n", line);
407             HeapFree(GetProcessHeap(), 0, tuple->key);
408             goto start;
409         }
410         tuple->option = HeapAlloc( PSDRV_Heap, 0, cp - opt + 1 );
411         if(!tuple->option) return FALSE;
412         memcpy(tuple->option, opt, cp - opt);
413         tuple->option[cp - opt] = '\0';
414         if(*cp == '/') {
415             char *buf;
416             trans = cp + 1;
417             cp = strchr(trans, ':');
418             if(!cp) {
419                 ERR("Error in line '%s'?\n", line);
420                 HeapFree(GetProcessHeap(), 0, tuple->option);
421                 HeapFree(GetProcessHeap(), 0, tuple->key);
422                 goto start;
423             }
424             buf = HeapAlloc( PSDRV_Heap, 0, cp - trans + 1 );
425             if(!buf) return FALSE;
426             memcpy(buf, trans, cp - trans);
427             buf[cp - trans] = '\0';
428             tuple->opttrans = PSDRV_PPDDecodeHex(buf);
429             HeapFree( PSDRV_Heap, 0, buf );
430         }
431     }
432
433     /* cp should point to a ':', so we increment past it */
434         cp++;
435
436     while(isspace(*cp))
437         cp++;
438
439     switch(*cp) {
440     case '"':
441         if( (!gotoption && strncmp(tuple->key, "*?", 2) ) ||
442              !strncmp(tuple->key, "*JCL", 4))
443             PSDRV_PPDGetQuotedValue(fp, cp, tuple);
444         else
445             PSDRV_PPDGetInvocationValue(fp, cp, tuple);
446         break;
447
448     case '^':
449         PSDRV_PPDGetSymbolValue(cp, tuple);
450         break;
451
452     default:
453         PSDRV_PPDGetStringValue(cp, tuple);
454     }
455     return TRUE;
456 }
457
458 /*********************************************************************
459  *
460  *              PSDRV_PPDGetPageSizeInfo
461  *
462  * Searches ppd PageSize list to return entry matching name or creates new
463  * entry which is appended to the list if name is not found.
464  *
465  */
466 PAGESIZE *PSDRV_PPDGetPageSizeInfo(PPD *ppd, char *name)
467 {
468     PAGESIZE *page = ppd->PageSizes, *lastpage;
469
470     if(!page) {
471        page = ppd->PageSizes = HeapAlloc( PSDRV_Heap,
472                                             HEAP_ZERO_MEMORY, sizeof(*page) );
473        return page;
474     } else {
475         for( ; page; page = page->next) {
476             if(!strcmp(page->Name, name))
477                  return page;
478             lastpage = page;
479         }
480
481         lastpage->next = HeapAlloc( PSDRV_Heap,
482                                            HEAP_ZERO_MEMORY, sizeof(*page) );
483         return lastpage->next;
484     }
485 }
486
487 /**********************************************************************
488  *
489  *              PSDRV_PPDGetWord
490  *
491  * Returns ptr alloced from heap to first word in str. Strips leading spaces.
492  * Puts ptr to next word in next
493  */
494 static char *PSDRV_PPDGetWord(char *str, char **next)
495 {
496     char *start, *end, *ret;
497
498     start = str;
499     while(start && *start && isspace(*start))
500         start++;
501     if(!start || !*start) return FALSE;
502
503     end = start;
504     while(*end && !isspace(*end))
505         end++;
506
507     ret = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
508     memcpy(ret, start, end - start );
509     *(ret + (end - start)) = '\0';
510
511     while(*end && isspace(*end))
512         end++;
513     if(*end)
514         *next = end;
515     else
516         *next = NULL;
517
518     return ret;
519 }
520
521 /*******************************************************************************
522  *      PSDRV_AddSlot
523  *
524  */
525 static INT PSDRV_AddSlot(PPD *ppd, LPSTR szName, LPSTR szFullName,
526         LPSTR szInvocationString, WORD wWinBin)
527 {
528     INPUTSLOT   *slot, **insert = &ppd->InputSlots;
529
530     while (*insert)
531         insert = &((*insert)->next);
532
533     slot = *insert = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(INPUTSLOT));
534     if (!slot)
535     {
536         ERR("Failed to allocate %i bytes of memory\n", sizeof(INPUTSLOT));
537         return 1;
538     }
539
540     slot->Name = szName;
541     slot->FullName = szFullName;
542     slot->InvocationString = szInvocationString;
543     slot->WinBin = wWinBin;
544
545     return 0;
546 }
547
548 /***********************************************************************
549  *
550  *              PSDRV_ParsePPD
551  *
552  *
553  */
554 PPD *PSDRV_ParsePPD(char *fname)
555 {
556     FILE *fp;
557     PPD *ppd;
558     PPDTuple tuple;
559     char *default_pagesize = NULL, *default_duplex = NULL;
560
561     TRACE("file '%s'\n", fname);
562
563     if((fp = fopen(fname, "r")) == NULL) {
564         WARN("Couldn't open ppd file '%s'\n", fname);
565         return NULL;
566     }
567
568     ppd = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*ppd));
569     if(!ppd) {
570         ERR("Unable to allocate memory for ppd\n");
571         fclose(fp);
572         return NULL;
573     }
574
575     /*
576      *  The Windows PostScript drivers create the following "virtual bin" for
577      *  every PostScript printer
578      */
579     if (PSDRV_AddSlot(ppd, NULL, "Automatically Select", NULL,
580             DMBIN_FORMSOURCE))
581     {
582         HeapFree (PSDRV_Heap, 0, ppd);
583         fclose(fp);
584         return NULL;
585     }
586
587     while( PSDRV_PPDGetNextTuple(fp, &tuple)) {
588
589         if(!strcmp("*NickName", tuple.key)) {
590             ppd->NickName = tuple.value;
591             tuple.value = NULL;
592             TRACE("NickName = '%s'\n", ppd->NickName);
593         }
594
595         else if(!strcmp("*LanguageLevel", tuple.key)) {
596             sscanf(tuple.value, "%d", &(ppd->LanguageLevel));
597             TRACE("LanguageLevel = %d\n", ppd->LanguageLevel);
598         }
599
600         else if(!strcmp("*ColorDevice", tuple.key)) {
601             if(!strcasecmp(tuple.value, "true"))
602                 ppd->ColorDevice = TRUE;
603             TRACE("ColorDevice = %d\n", (int)ppd->ColorDevice);
604         }
605
606         else if((!strcmp("*DefaultResolution", tuple.key)) ||
607                 (!strcmp("*DefaultJCLResolution", tuple.key))) {
608             sscanf(tuple.value, "%d", &(ppd->DefaultResolution));
609             TRACE("DefaultResolution = %d\n", ppd->DefaultResolution);
610         }
611
612         else if(!strcmp("*Font", tuple.key)) {
613             FONTNAME *fn;
614
615             for(fn = ppd->InstalledFonts; fn && fn->next; fn = fn->next)
616                 ;
617             if(!fn) {
618                 ppd->InstalledFonts = HeapAlloc(PSDRV_Heap,
619                                                HEAP_ZERO_MEMORY, sizeof(*fn));
620                 fn = ppd->InstalledFonts;
621             } else {
622                fn->next = HeapAlloc(PSDRV_Heap,
623                                                HEAP_ZERO_MEMORY, sizeof(*fn));
624                fn = fn->next;
625             }
626             fn->Name = tuple.option;
627             tuple.option = NULL;
628         }
629
630         else if(!strcmp("*DefaultFont", tuple.key)) {
631             ppd->DefaultFont = tuple.value;
632             tuple.value = NULL;
633         }
634
635         else if(!strcmp("*JCLBegin", tuple.key)) {
636             ppd->JCLBegin = tuple.value;
637             tuple.value = NULL;
638         }
639
640         else if(!strcmp("*JCLToPSInterpreter", tuple.key)) {
641             ppd->JCLToPSInterpreter = tuple.value;
642             tuple.value = NULL;
643         }
644
645         else if(!strcmp("*JCLEnd", tuple.key)) {
646             ppd->JCLEnd = tuple.value;
647             tuple.value = NULL;
648         }
649
650         else if(!strcmp("*PageSize", tuple.key)) {
651             PAGESIZE *page;
652             page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
653
654             if(!page->Name) {
655                 int i;
656
657                 page->Name = tuple.option;
658                 tuple.option = NULL;
659
660                 for(i = 0; PageTrans[i].PSName; i++) {
661                     if(!strcmp(PageTrans[i].PSName, page->Name)) { /* case ? */
662                         page->WinPage = PageTrans[i].WinPage;
663                         break;
664                     }
665                 }
666                 if(!page->WinPage) {
667                     TRACE("Can't find Windows page type for '%s' - using %u\n",
668                           page->Name, UserPageType);
669                     page->WinPage = UserPageType++;
670                 }
671             }
672             if(!page->FullName) {
673                 if(tuple.opttrans) {
674                     page->FullName = tuple.opttrans;
675                     tuple.opttrans = NULL;
676                 } else
677                 {
678                     page->FullName = HeapAlloc( PSDRV_Heap, 0, strlen(page->Name)+1 );
679                     strcpy( page->FullName, page->Name );
680                 }
681             }
682             if(!page->InvocationString) {
683                 page->InvocationString = tuple.value;
684                 tuple.value = NULL;
685             }
686         }
687
688         else if(!strcmp("*DefaultPageSize", tuple.key)) {
689             if(default_pagesize) {
690                 WARN("Already set default pagesize\n");
691             } else {
692                 default_pagesize = tuple.value;
693                 tuple.value = NULL;
694            }
695         }
696
697         else if(!strcmp("*ImageableArea", tuple.key)) {
698             PAGESIZE *page;
699             page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
700
701             if(!page->Name) {
702                 page->Name = tuple.option;
703                 tuple.option = NULL;
704             }
705             if(!page->FullName) {
706                 page->FullName = tuple.opttrans;
707                 tuple.opttrans = NULL;
708             }
709
710 #define PIA page->ImageableArea
711             if(!PIA) {
712                 PIA = HeapAlloc( PSDRV_Heap, 0, sizeof(*PIA) );
713                 sscanf(tuple.value, "%f%f%f%f", &PIA->llx, &PIA->lly,
714                                                 &PIA->urx, &PIA->ury);
715             }
716 #undef PIA
717         }
718
719
720         else if(!strcmp("*PaperDimension", tuple.key)) {
721             PAGESIZE *page;
722             page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
723
724             if(!page->Name) {
725                 page->Name = tuple.option;
726                 tuple.option = NULL;
727             }
728             if(!page->FullName) {
729                 page->FullName = tuple.opttrans;
730                 tuple.opttrans = NULL;
731             }
732
733 #define PD page->PaperDimension
734             if(!PD) {
735                 PD = HeapAlloc( PSDRV_Heap, 0, sizeof(*PD) );
736                 sscanf(tuple.value, "%f%f", &PD->x, &PD->y);
737             }
738 #undef PD
739         }
740
741         else if(!strcmp("*LandscapeOrientation", tuple.key)) {
742             if(!strcmp(tuple.value, "Plus90"))
743                 ppd->LandscapeOrientation = 90;
744             else if(!strcmp(tuple.value, "Minus90"))
745                 ppd->LandscapeOrientation = -90;
746
747             /* anything else, namely 'any', leaves value at 0 */
748
749             TRACE("LandscapeOrientation = %d\n",
750                   ppd->LandscapeOrientation);
751         }
752
753         else if(!strcmp("*UIConstraints", tuple.key)) {
754             char *start;
755             CONSTRAINT *con, **insert = &ppd->Constraints;
756
757             while(*insert)
758                 insert = &((*insert)->next);
759
760             con = *insert = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
761                                        sizeof(*con) );
762
763             start = tuple.value;
764
765             con->Feature1 = PSDRV_PPDGetWord(start, &start);
766             con->Value1 = PSDRV_PPDGetWord(start, &start);
767             con->Feature2 = PSDRV_PPDGetWord(start, &start);
768             con->Value2 = PSDRV_PPDGetWord(start, &start);
769         }
770
771         else if (!strcmp("*InputSlot", tuple.key))
772         {
773
774             if (!tuple.opttrans)
775                 tuple.opttrans = tuple.option;
776
777             TRACE("Using Windows bin type %u for '%s'\n", UserBinType,
778                     tuple.option);
779
780             /* FIXME - should check for failure */
781             PSDRV_AddSlot(ppd, tuple.option, tuple.opttrans, tuple.value,
782                     UserBinType++);
783
784             tuple.option = tuple.opttrans = tuple.value = NULL;
785         }
786
787         /*
788          *  Windows treats "manual feed" as another paper source.  Most PPD
789          *  files, however, treat it as a separate option which is either on or
790          *  off.
791          */
792         else if (!strcmp("*ManualFeed", tuple.key) && tuple.option &&
793                 !strcmp ("True", tuple.option))
794         {
795             /* FIXME - should check for failure */
796             PSDRV_AddSlot(ppd, "Manual Feed", "Manual Feed", tuple.value, DMBIN_MANUAL);
797             tuple.value = NULL;
798         }
799
800         else if(!strcmp("*TTRasterizer", tuple.key)) {
801             if(!strcasecmp("None", tuple.value))
802                 ppd->TTRasterizer = RO_None;
803             else if(!strcasecmp("Accept68K", tuple.value))
804                 ppd->TTRasterizer = RO_Accept68K;
805             else if(!strcasecmp("Type42", tuple.value))
806                 ppd->TTRasterizer = RO_Type42;
807             else if(!strcasecmp("TrueImage", tuple.value))
808                 ppd->TTRasterizer = RO_TrueImage;
809             else {
810                 FIXME("Unknown option %s for *TTRasterizer\n",
811                       tuple.value);
812                 ppd->TTRasterizer = RO_None;
813             }
814             TRACE("*TTRasterizer = %d\n", ppd->TTRasterizer);
815         }
816
817         else if(!strcmp("*Duplex", tuple.key)) {
818             DUPLEX **duplex;
819             for(duplex = &ppd->Duplexes; *duplex; duplex = &(*duplex)->next)
820                 ;
821             *duplex = HeapAlloc(GetProcessHeap(), 0, sizeof(**duplex));
822             (*duplex)->Name = tuple.option;
823             (*duplex)->FullName = tuple.opttrans;
824             (*duplex)->InvocationString = tuple.value;
825             (*duplex)->next = NULL;
826             if(!strcasecmp("None", tuple.option) || !strcasecmp("False", tuple.option)
827                || !strcasecmp("Simplex", tuple.option))
828                 (*duplex)->WinDuplex = DMDUP_SIMPLEX;
829             else if(!strcasecmp("DuplexNoTumble", tuple.option))
830                 (*duplex)->WinDuplex = DMDUP_VERTICAL;
831             else if(!strcasecmp("DuplexTumble", tuple.option))
832                 (*duplex)->WinDuplex = DMDUP_HORIZONTAL;
833             else if(!strcasecmp("Notcapable", tuple.option))
834                 (*duplex)->WinDuplex = 0;
835             else {
836                 FIXME("Unknown option %s for *Duplex defaulting to simplex\n", tuple.option);
837                 (*duplex)->WinDuplex = DMDUP_SIMPLEX;
838             }
839             tuple.option = tuple.opttrans = tuple.value = NULL;
840         }
841
842         else if(!strcmp("*DefaultDuplex", tuple.key)) {
843             if(default_duplex) {
844                 WARN("Already set default duplex\n");
845             } else {
846                 default_duplex = tuple.value;
847                 tuple.value = NULL;
848            }
849         }
850
851         if(tuple.key) HeapFree(PSDRV_Heap, 0, tuple.key);
852         if(tuple.option) HeapFree(PSDRV_Heap, 0, tuple.option);
853         if(tuple.value) HeapFree(PSDRV_Heap, 0, tuple.value);
854         if(tuple.opttrans) HeapFree(PSDRV_Heap, 0, tuple.opttrans);
855         if(tuple.valtrans) HeapFree(PSDRV_Heap, 0, tuple.valtrans);
856
857     }
858
859
860     ppd->DefaultPageSize = NULL;
861     if(default_pagesize) {
862         PAGESIZE *page;
863         for(page = ppd->PageSizes; page; page = page->next) {
864             if(!strcmp(page->Name, default_pagesize)) {
865                 ppd->DefaultPageSize = page;
866                 TRACE("DefaultPageSize: %s\n", page->Name);
867                 break;
868             }
869         }
870         HeapFree(PSDRV_Heap, 0, default_pagesize);
871     }
872     if(!ppd->DefaultPageSize) {
873         ppd->DefaultPageSize = ppd->PageSizes;
874         TRACE("Setting DefaultPageSize to first in list\n");
875     }
876
877     ppd->DefaultDuplex = NULL;
878     if(default_duplex) {
879         DUPLEX *duplex;
880         for(duplex = ppd->Duplexes; duplex; duplex = duplex->next) {
881             if(!strcmp(duplex->Name, default_duplex)) {
882                 ppd->DefaultDuplex = duplex;
883                 TRACE("DefaultDuplex: %s\n", duplex->Name);
884                 break;
885             }
886         }
887         HeapFree(PSDRV_Heap, 0, default_duplex);
888     }
889     if(!ppd->DefaultDuplex) {
890         ppd->DefaultDuplex = ppd->Duplexes;
891         TRACE("Setting DefaultDuplex to first in list\n");
892     }
893
894
895     {
896         FONTNAME *fn;
897         PAGESIZE *page;
898         CONSTRAINT *con;
899         INPUTSLOT *slot;
900         OPTION *option;
901         OPTIONENTRY *optionEntry;
902
903         for(fn = ppd->InstalledFonts; fn; fn = fn->next)
904             TRACE("'%s'\n", fn->Name);
905
906         for(page = ppd->PageSizes; page; page = page->next) {
907             TRACE("'%s' aka '%s' (%d) invoked by '%s'\n", page->Name,
908               page->FullName, page->WinPage, page->InvocationString);
909             if(page->ImageableArea)
910                 TRACE("Area = %.2f,%.2f - %.2f, %.2f\n",
911                       page->ImageableArea->llx, page->ImageableArea->lly,
912                       page->ImageableArea->urx, page->ImageableArea->ury);
913             if(page->PaperDimension)
914                 TRACE("Dimension = %.2f x %.2f\n",
915                       page->PaperDimension->x, page->PaperDimension->y);
916         }
917
918         for(con = ppd->Constraints; con; con = con->next)
919             TRACE("CONSTRAINTS@ %s %s %s %s\n", con->Feature1,
920                   con->Value1, con->Feature2, con->Value2);
921
922         for(option = ppd->InstalledOptions; option; option = option->next) {
923             TRACE("OPTION: %s %s %s\n", option->OptionName,
924                   option->FullName, option->DefaultOption);
925             for(optionEntry = option->Options; optionEntry;
926                 optionEntry = optionEntry->next)
927                 TRACE("\tOPTIONENTRY: %s %s %s\n", optionEntry->Name,
928                       optionEntry->FullName, optionEntry->InvocationString);
929         }
930
931         for(slot = ppd->InputSlots; slot; slot = slot->next)
932             TRACE("INPUTSLOTS '%s' Name '%s' (%d) Invocation '%s'\n",
933                   slot->Name, slot->FullName, slot->WinBin,
934                   slot->InvocationString);
935     }
936
937     return ppd;
938 }