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