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