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