Moved HDROP to shellapi.h.
[wine] / dlls / wineps / ps.c
1 /*
2  *      PostScript output functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include "psdrv.h"
12 #include "winspool.h"
13 #include "debugtools.h"
14
15 DEFAULT_DEBUG_CHANNEL(psdrv);
16
17 static char psheader[] = /* title llx lly urx ury orientation */
18 "%%!PS-Adobe-3.0\n"
19 "%%%%Creator: Wine PostScript Driver\n"
20 "%%%%Title: %s\n"
21 "%%%%BoundingBox: %d %d %d %d\n"
22 "%%%%Pages: (atend)\n"
23 "%%%%Orientation: %s\n"
24 "%%%%EndComments\n";
25
26 static char psbeginprolog[] = 
27 "%%BeginProlog\n";
28
29 static char psendprolog[] =
30 "%%EndProlog\n";
31
32 static char psvectorstart[] =
33 "/ANSIEncoding [\n";
34
35 static char psvectorend[] =
36 "] def\n";
37
38 static char psprolog[] = /* output ANSIEncoding vector first */
39 "/reencodefont {\n"
40 "  findfont\n"
41 "  dup length dict begin\n"
42 "  {1 index /FID ne {def} {pop pop} ifelse} forall\n"
43 "  /Encoding ANSIEncoding def\n"
44 "  currentdict\n"
45 "  end\n"
46 "  definefont pop\n"
47 "} bind def\n"
48 "/tmpmtrx matrix def\n"
49 "/hatch {\n"
50 "  pathbbox\n"
51 "  /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
52 "  l cvi gap idiv gap mul\n"
53 "  gap\n"
54 "  r cvi gap idiv gap mul\n"
55 "  {t moveto 0 b t sub rlineto}\n"
56 "  for\n"
57 "} bind def\n";
58
59 static char psbeginsetup[] =
60 "%%BeginSetup\n";
61
62 static char psendsetup[] =
63 "%%EndSetup\n";
64
65 static char psbeginfeature[] = /* feature, value */
66 "mark {\n"
67 "%%%%BeginFeature: %s %s\n";
68
69 static char psendfeature[] =
70 "\n%%EndFeature\n"
71 "} stopped cleartomark\n";
72
73 static char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
74 "%%%%Page: %s %d\n"
75 "%%%%BeginPageSetup\n"
76 "/pgsave save def\n"
77 "72 %d div 72 %d div scale\n"
78 "%d %d translate\n"
79 "1 -1 scale\n"
80 "%d rotate\n"
81 "%%%%EndPageSetup\n";
82
83 static char psendpage[] =
84 "pgsave restore\n"
85 "showpage\n";
86
87 static char psfooter[] = /* pages */
88 "%%%%Trailer\n"
89 "%%%%Pages: %d\n"
90 "%%%%EOF\n";
91
92 static char psmoveto[] = /* x, y */
93 "%d %d moveto\n";
94
95 static char pslineto[] = /* x, y */
96 "%d %d lineto\n";
97
98 static char psstroke[] = 
99 "stroke\n";
100
101 static char psrectangle[] = /* x, y, width, height, -width */
102 "%d %d moveto\n"
103 "%d 0 rlineto\n"
104 "0 %d rlineto\n"
105 "%d 0 rlineto\n"
106 "closepath\n";
107
108 static char psrrectangle[] = /* x, y, width, height, -width */
109 "%d %d rmoveto\n"
110 "%d 0 rlineto\n"
111 "0 %d rlineto\n"
112 "%d 0 rlineto\n"
113 "closepath\n";
114
115 static char psshow[] = /* string */
116 "(%s) show\n";
117
118 static char pssetfont[] = /* fontname, xscale, yscale, ascent, escapement */
119 "/%s findfont\n"
120 "[%d 0 0 %d 0 0]\n"
121 "%d 10 div matrix rotate\n"
122 "matrix concatmatrix\n"
123 "makefont setfont\n";
124
125 static char pssetlinewidth[] = /* width */
126 "%d setlinewidth\n";
127
128 static char pssetdash[] = /* dash, offset */
129 "[%s] %d setdash\n";
130
131 static char pssetgray[] = /* gray */
132 "%.2f setgray\n";
133
134 static char pssetrgbcolor[] = /* r, g, b */
135 "%.2f %.2f %.2f setrgbcolor\n";
136
137 static char psarc[] = /* x, y, w, h, ang1, ang2 */
138 "tmpmtrx currentmatrix pop\n"
139 "%d %d translate\n"
140 "%d %d scale\n"
141 "0 0 0.5 %.1f %.1f arc\n"
142 "tmpmtrx setmatrix\n";
143
144 static char psgsave[] =
145 "gsave\n";
146
147 static char psgrestore[] =
148 "grestore\n";
149
150 static char psfill[] =
151 "fill\n";
152
153 static char pseofill[] =
154 "eofill\n";
155
156 static char psnewpath[] =
157 "newpath\n";
158
159 static char psclosepath[] =
160 "closepath\n";
161
162 static char psclip[] =
163 "clip\n";
164
165 static char psinitclip[] =
166 "initclip\n";
167
168 static char pseoclip[] =
169 "eoclip\n";
170
171 static char psrectclip[] =
172 "%d %d %d %d rectclip\n"; 
173
174 static char psrectclip2[] =
175 "%s rectclip\n"; 
176
177 static char pshatch[] =
178 "hatch\n";
179
180 static char psrotate[] = /* ang */
181 "%.1f rotate\n";
182
183 static char psarrayget[] = 
184 "%s %d get\n";
185
186 static char psarrayput[] = 
187 "%s %d %ld put\n";
188
189 static char psarraydef[] = 
190 "/%s %d array def\n";
191
192 char *PSDRV_ANSIVector[256] = {
193 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x00 */
194 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
195 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x10 */
196 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
197 "space",        "exclam",       "quotedbl",     "numbersign", /* 0x20 */
198 "dollar",       "percent",      "ampersand",    "quotesingle",
199 "parenleft",    "parenright",   "asterisk",     "plus",
200 "comma",        "hyphen",       "period",       "slash",
201 "zero",         "one",          "two",          "three", /* 0x30 */
202 "four",         "five",         "six",          "seven",
203 "eight",        "nine",         "colon",        "semicolon",
204 "less",         "equal",        "greater",      "question",
205 "at",           "A",            "B",            "C", /* 0x40 */
206 "D",            "E",            "F",            "G",
207 "H",            "I",            "J",            "K",
208 "L",            "M",            "N",            "O",
209 "P",            "Q",            "R",            "S", /* 0x50 */
210 "T",            "U",            "V",            "W",
211 "X",            "Y",            "Z",            "bracketleft",
212 "backslash",    "bracketright", "asciicircum",  "underscore",
213 "grave",        "a",            "b",            "c", /* 0x60 */
214 "d",            "e",            "f",            "g",
215 "h",            "i",            "j",            "k",
216 "l",            "m",            "n",            "o",
217 "p",            "q",            "r",            "s", /* 0x70 */
218 "t",            "u",            "v",            "w",
219 "x",            "y",            "z",            "braceleft",
220 "bar",          "braceright",   "asciitilde",   NULL,
221 NULL,           NULL,           NULL,           NULL, /* 0x80 */
222 NULL,           NULL,           NULL,           NULL,
223 NULL,           NULL,           NULL,           NULL,
224 NULL,           NULL,           NULL,           NULL,
225 NULL,           "quoteleft",    "quoteright",   "quotedblleft", /* 0x90 */
226 "quotedblright","bullet",       "endash",       "emdash",
227 NULL,           NULL,           NULL,           NULL,
228 NULL,           NULL,           NULL,           NULL,
229 "space",        "exclamdown",   "cent",         "sterling", /* 0xa0 */
230 "currency",     "yen",          "brokenbar",    "section",
231 "dieresis",     "copyright",    "ordfeminine",  "guillemotleft",
232 "logicalnot",   "hyphen",       "registered",   "macron",
233 "degree",       "plusminus",    "twosuperior",  "threesuperior", /* 0xb0 */
234 "acute",        "mu",           "paragraph",    "periodcentered",
235 "cedilla",      "onesuperior",  "ordmasculine", "guillemotright",
236 "onequarter",   "onehalf",      "threequarters","questiondown",
237 "Agrave",       "Aacute",       "Acircumflex",  "Atilde", /* 0xc0 */
238 "Adieresis",    "Aring",        "AE",           "Ccedilla",
239 "Egrave",       "Eacute",       "Ecircumflex",  "Edieresis",
240 "Igrave",       "Iacute",       "Icircumflex",  "Idieresis",
241 "Eth",          "Ntilde",       "Ograve",       "Oacute", /* 0xd0 */
242 "Ocircumflex",  "Otilde",       "Odieresis",    "multiply",
243 "Oslash",       "Ugrave",       "Uacute",       "Ucircumflex",
244 "Udieresis",    "Yacute",       "Thorn",        "germandbls",
245 "agrave",       "aacute",       "acircumflex",  "atilde", /* 0xe0 */
246 "adieresis",    "aring",        "ae",           "ccedilla",
247 "egrave",       "eacute",       "ecircumflex",  "edieresis",
248 "igrave",       "iacute",       "icircumflex",  "idieresis",
249 "eth",          "ntilde",       "ograve",       "oacute", /* 0xf0 */
250 "ocircumflex",  "otilde",       "odieresis",    "divide",
251 "oslash",       "ugrave",       "uacute",       "ucircumflex",
252 "udieresis",    "yacute",       "thorn",        "ydieresis"
253 };
254
255
256 char psreencodefont[] = /* newfontname basefontname*/
257 "/%s /%s reencodefont\n";
258
259
260 int PSDRV_WriteSpool(DC *dc, LPSTR lpData, WORD cch)
261 {
262     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
263
264     if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
265         if( !PSDRV_StartPage(dc) )
266             return FALSE;
267     }
268     return WriteSpool16( physDev->job.hJob, lpData, cch );
269 }
270
271
272 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
273                          char *invocation)
274 {
275
276     char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
277                              strlen(feature) + strlen(value));
278
279
280     sprintf(buf, psbeginfeature, feature, value);
281     WriteSpool16( hJob, buf, strlen(buf) );
282
283     WriteSpool16( hJob, invocation, strlen(invocation) );
284
285     WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
286     
287     HeapFree( PSDRV_Heap, 0, buf );
288     return 1;
289 }
290
291
292
293 INT PSDRV_WriteHeader( DC *dc, LPCSTR title )
294 {
295     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
296     char *buf, *orient, vectbuf[256];
297     INPUTSLOT *slot;
298     PAGESIZE *page;
299     int urx, ury, i, j;
300
301     TRACE("'%s'\n", title);
302
303     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
304                              strlen(title) + 30 );
305     if(!buf) {
306         WARN("HeapAlloc failed\n");
307         return 0;
308     }
309
310     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
311       /* BBox co-ords are in default user co-ord system so urx < ury even in
312          landscape mode */
313         urx = (int) (dc->devCaps->vertSize * 72.0 / 25.4);
314         ury = (int) (dc->devCaps->horzSize * 72.0 / 25.4);
315         orient = "Landscape";
316     } else {
317         urx = (int) (dc->devCaps->horzSize * 72.0 / 25.4);
318         ury = (int) (dc->devCaps->vertSize * 72.0 / 25.4);
319         orient = "Portrait";
320     }
321
322     /* FIXME should do something better with BBox */
323
324     sprintf(buf, psheader, title, 0, 0, urx, ury, orient);              
325
326     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
327                                                      strlen(buf) ) {
328         WARN("WriteSpool error\n");
329         HeapFree( PSDRV_Heap, 0, buf );
330         return 0;
331     }
332     HeapFree( PSDRV_Heap, 0, buf );
333
334     WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
335     WriteSpool16( physDev->job.hJob, psvectorstart, strlen(psvectorstart) );
336     
337     for(i = 0; i < 256; i += 8) {
338         vectbuf[0] = '\0';
339         for(j = 0; j < 8; j++) {
340             strcat(vectbuf, "/");
341             if(PSDRV_ANSIVector[i+j]) {
342                 strcat(vectbuf, PSDRV_ANSIVector[i+j]);
343                 strcat(vectbuf, " ");
344             } else {
345                 strcat(vectbuf, ".notdef ");
346             }
347         }
348         strcat(vectbuf, "\n");
349         WriteSpool16( physDev->job.hJob, vectbuf, strlen(vectbuf) );
350     }
351
352     WriteSpool16( physDev->job.hJob, psvectorend, strlen(psvectorend) );
353     WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
354     WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
355
356
357     WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
358
359     for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
360         if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
361             if(slot->InvocationString) {
362                 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
363                              slot->InvocationString);
364                 break;
365             }
366         }
367     }
368
369     for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
370         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
371             if(page->InvocationString) {
372                 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
373                              page->InvocationString);
374                 break;
375             }
376         }
377     }
378
379     WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
380
381
382     return 1;
383 }
384
385
386 INT PSDRV_WriteFooter( DC *dc )
387 {
388     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
389     char *buf;
390
391     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
392     if(!buf) {
393         WARN("HeapAlloc failed\n");
394         return 0;
395     }
396
397     sprintf(buf, psfooter, physDev->job.PageNo);
398
399     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
400                                                      strlen(buf) ) {
401         WARN("WriteSpool error\n");
402         HeapFree( PSDRV_Heap, 0, buf );
403         return 0;
404     }
405     HeapFree( PSDRV_Heap, 0, buf );
406     return 1;
407 }
408
409
410
411 INT PSDRV_WriteEndPage( DC *dc )
412 {
413     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
414
415     if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) != 
416                                                      sizeof(psendpage)-1 ) {
417         WARN("WriteSpool error\n");
418         return 0;
419     }
420     return 1;
421 }
422
423
424
425
426 INT PSDRV_WriteNewPage( DC *dc )
427 {
428     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
429     char *buf;
430     char name[100];
431     signed int xtrans, ytrans, rotation;
432
433     sprintf(name, "%d", physDev->job.PageNo);
434
435     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
436     if(!buf) {
437         WARN("HeapAlloc failed\n");
438         return 0;
439     }
440
441     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
442         if(physDev->pi->ppd->LandscapeOrientation == -90) {
443             xtrans = dc->devCaps->vertRes;
444             ytrans = dc->devCaps->horzRes;
445             rotation = 90;
446         } else {
447             xtrans = ytrans = 0;
448             rotation = -90;
449         }
450     } else {
451         xtrans = 0;
452         ytrans = dc->devCaps->vertRes;
453         rotation = 0;
454     }
455
456     sprintf(buf, psnewpage, name, physDev->job.PageNo,
457             dc->devCaps->logPixelsX, dc->devCaps->logPixelsY,
458             xtrans, ytrans, rotation);
459
460     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
461                                                      strlen(buf) ) {
462         WARN("WriteSpool error\n");
463         HeapFree( PSDRV_Heap, 0, buf );
464         return 0;
465     }
466     HeapFree( PSDRV_Heap, 0, buf );
467     return 1;
468 }
469
470
471 BOOL PSDRV_WriteMoveTo(DC *dc, INT x, INT y)
472 {
473     char buf[100];
474
475     sprintf(buf, psmoveto, x, y);
476     return PSDRV_WriteSpool(dc, buf, strlen(buf));
477 }
478
479 BOOL PSDRV_WriteLineTo(DC *dc, INT x, INT y)
480 {
481     char buf[100];
482
483     sprintf(buf, pslineto, x, y);
484     return PSDRV_WriteSpool(dc, buf, strlen(buf));
485 }
486
487
488 BOOL PSDRV_WriteStroke(DC *dc)
489 {
490     return PSDRV_WriteSpool(dc, psstroke, sizeof(psstroke)-1);
491 }
492
493
494
495 BOOL PSDRV_WriteRectangle(DC *dc, INT x, INT y, INT width, 
496                         INT height)
497 {
498     char buf[100];
499
500     sprintf(buf, psrectangle, x, y, width, height, -width);
501     return PSDRV_WriteSpool(dc, buf, strlen(buf));
502 }
503
504 BOOL PSDRV_WriteRRectangle(DC *dc, INT x, INT y, INT width,
505       INT height)
506 {
507     char buf[100];
508
509     sprintf(buf, psrrectangle, x, y, width, height, -width);
510     return PSDRV_WriteSpool(dc, buf, strlen(buf));
511 }
512
513 BOOL PSDRV_WriteArc(DC *dc, INT x, INT y, INT w, INT h, double ang1,
514                       double ang2)
515 {
516     char buf[256];
517
518     /* Make angles -ve and swap order because we're working with an upside
519        down y-axis */
520     sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
521     return PSDRV_WriteSpool(dc, buf, strlen(buf));
522 }
523
524 static char encodingext[] = "-ANSI";
525
526 BOOL PSDRV_WriteSetFont(DC *dc, BOOL UseANSI)
527 {
528     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
529     char *buf, *newbuf;
530
531     buf = (char *)HeapAlloc( PSDRV_Heap, 0,
532              sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
533
534     if(!buf) {
535         WARN("HeapAlloc failed\n");
536         return FALSE;
537     }
538
539     newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
540               strlen(physDev->font.afm->FontName) + sizeof(encodingext));
541
542     if(!newbuf) {
543         WARN("HeapAlloc failed\n");
544         HeapFree(PSDRV_Heap, 0, buf);
545         return FALSE;
546     }
547
548     if(UseANSI)
549         sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
550     else
551         strcpy(newbuf, physDev->font.afm->FontName);
552
553     sprintf(buf, pssetfont, newbuf, 
554                 physDev->font.size, -physDev->font.size,
555                 -physDev->font.escapement);
556
557     PSDRV_WriteSpool(dc, buf, strlen(buf));
558     HeapFree(PSDRV_Heap, 0, buf);
559     return TRUE;
560 }    
561
562 BOOL PSDRV_WriteSetColor(DC *dc, PSCOLOR *color)
563 {
564     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
565     char buf[256];
566
567     PSDRV_CopyColor(&physDev->inkColor, color);
568     switch(color->type) {
569     case PSCOLOR_RGB:
570         sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
571                 color->value.rgb.b);
572         return PSDRV_WriteSpool(dc, buf, strlen(buf));
573
574     case PSCOLOR_GRAY:  
575         sprintf(buf, pssetgray, color->value.gray.i);
576         return PSDRV_WriteSpool(dc, buf, strlen(buf));
577         
578     default:
579         ERR("Unkonwn colour type %d\n", color->type);
580         break;
581     }
582
583     return FALSE;
584 }
585
586 BOOL PSDRV_WriteSetPen(DC *dc)
587 {
588     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
589     char buf[256];
590
591     sprintf(buf, pssetlinewidth, physDev->pen.width);
592     PSDRV_WriteSpool(dc, buf, strlen(buf));
593
594     if(physDev->pen.dash) {
595         sprintf(buf, pssetdash, physDev->pen.dash, 0);
596         PSDRV_WriteSpool(dc, buf, strlen(buf));
597     }
598
599     return TRUE;
600 }
601
602 BOOL PSDRV_WriteReencodeFont(DC *dc)
603 {
604     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
605     char *buf, *newbuf;
606  
607     buf = (char *)HeapAlloc( PSDRV_Heap, 0,
608              sizeof(psreencodefont) + 2 * strlen(physDev->font.afm->FontName) 
609                              + sizeof(encodingext));
610
611     if(!buf) {
612         WARN("HeapAlloc failed\n");
613         return FALSE;
614     }
615
616     newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
617               strlen(physDev->font.afm->FontName) + sizeof(encodingext));
618
619     if(!newbuf) {
620         WARN("HeapAlloc failed\n");
621         HeapFree(PSDRV_Heap, 0, buf);
622         return FALSE;
623     }
624
625     sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
626     sprintf(buf, psreencodefont, newbuf, physDev->font.afm->FontName);
627
628     PSDRV_WriteSpool(dc, buf, strlen(buf));
629
630     HeapFree(PSDRV_Heap, 0, newbuf);
631     HeapFree(PSDRV_Heap, 0, buf);
632     return TRUE;
633 }    
634
635 BOOL PSDRV_WriteShow(DC *dc, LPCWSTR str, INT count)
636 {
637     char *buf, *buf1;
638     INT buflen = count + 10, i, done;
639
640     buf = (char *)HeapAlloc( PSDRV_Heap, 0, buflen );
641     
642     for(i = done = 0; i < count; i++) {
643         char c = PSDRV_UnicodeToANSI(str[i]);
644         if(!isprint(c)) {
645             if(done + 4 >= buflen)
646                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
647             sprintf(buf + done, "\\%03o", (int)(unsigned char)c);
648             done += 4;
649         } else if(c == '\\' || c == '(' || c == ')' ) {
650             if(done + 2 >= buflen)
651                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
652             buf[done++] = '\\';
653             buf[done++] = c;
654         } else {
655             if(done + 1 >= buflen)
656                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
657             buf[done++] = c;
658         }
659     }
660     buf[done] = '\0';
661
662     buf1 = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psshow) + done);
663
664     sprintf(buf1, psshow, buf);
665
666     PSDRV_WriteSpool(dc, buf1, strlen(buf1));
667     HeapFree(PSDRV_Heap, 0, buf);
668     HeapFree(PSDRV_Heap, 0, buf1);
669
670     return TRUE;
671 }    
672
673 BOOL PSDRV_WriteFill(DC *dc)
674 {
675     return PSDRV_WriteSpool(dc, psfill, sizeof(psfill)-1);
676 }
677
678 BOOL PSDRV_WriteEOFill(DC *dc)
679 {
680     return PSDRV_WriteSpool(dc, pseofill, sizeof(pseofill)-1);
681 }
682
683 BOOL PSDRV_WriteGSave(DC *dc)
684 {
685     return PSDRV_WriteSpool(dc, psgsave, sizeof(psgsave)-1);
686 }
687
688 BOOL PSDRV_WriteGRestore(DC *dc)
689 {
690     return PSDRV_WriteSpool(dc, psgrestore, sizeof(psgrestore)-1);
691 }
692
693 BOOL PSDRV_WriteNewPath(DC *dc)
694 {
695     return PSDRV_WriteSpool(dc, psnewpath, sizeof(psnewpath)-1);
696 }
697
698 BOOL PSDRV_WriteClosePath(DC *dc)
699 {
700     return PSDRV_WriteSpool(dc, psclosepath, sizeof(psclosepath)-1);
701 }
702
703 BOOL PSDRV_WriteClip(DC *dc)
704 {
705     return PSDRV_WriteSpool(dc, psclip, sizeof(psclip)-1);
706 }
707
708 BOOL PSDRV_WriteEOClip(DC *dc)
709 {
710     return PSDRV_WriteSpool(dc, pseoclip, sizeof(pseoclip)-1);
711 }
712
713 BOOL PSDRV_WriteInitClip(DC *dc)
714 {
715     return PSDRV_WriteSpool(dc, psinitclip, sizeof(psinitclip)-1);
716 }
717
718 BOOL PSDRV_WriteHatch(DC *dc)
719 {
720     return PSDRV_WriteSpool(dc, pshatch, sizeof(pshatch)-1);
721 }
722
723 BOOL PSDRV_WriteRotate(DC *dc, float ang)
724 {
725     char buf[256];
726
727     sprintf(buf, psrotate, ang);
728     return PSDRV_WriteSpool(dc, buf, strlen(buf));
729 }
730
731 BOOL PSDRV_WriteIndexColorSpaceBegin(DC *dc, int size)
732 {
733     char buf[256];
734     sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
735     return PSDRV_WriteSpool(dc, buf, strlen(buf));
736 }
737
738 BOOL PSDRV_WriteIndexColorSpaceEnd(DC *dc)
739 {
740     char buf[] = ">\n] setcolorspace\n";
741     return PSDRV_WriteSpool(dc, buf, sizeof(buf) - 1);
742
743
744 BOOL PSDRV_WriteRGB(DC *dc, COLORREF *map, int number)
745 {
746     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
747     int i;
748
749     ptr = buf;
750     for(i = 0; i < number; i++) {
751         sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]), 
752                 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
753                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
754         ptr += 7;
755     }
756     PSDRV_WriteSpool(dc, buf, number * 7);
757     HeapFree(PSDRV_Heap, 0, buf);
758     return TRUE;
759 }
760
761
762 BOOL PSDRV_WriteImageDict(DC *dc, WORD depth, INT xDst, INT yDst,
763                           INT widthDst, INT heightDst, INT widthSrc,
764                           INT heightSrc, char *bits)
765 {
766     char start[] = "%d %d translate\n%d %d scale\n<<\n"
767       " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
768       " /ImageMatrix [%d 0 0 %d 0 %d]\n";
769
770     char decode1[] = " /Decode [0 %d]\n";
771     char decode3[] = " /Decode [0 1 0 1 0 1]\n";
772
773     char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
774     char endbits[] = " /DataSource <%s>\n>> image\n";
775
776     char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
777
778     sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
779             (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
780
781     PSDRV_WriteSpool(dc, buf, strlen(buf));
782
783     switch(depth) {
784     case 8:
785         sprintf(buf, decode1, 255);
786         break;
787
788     case 4:
789         sprintf(buf, decode1, 15);
790         break;
791
792     case 1:
793         sprintf(buf, decode1, 1);
794         break;
795
796     default:
797         strcpy(buf, decode3);
798         break;
799     }
800
801     PSDRV_WriteSpool(dc, buf, strlen(buf));
802
803     if(!bits)
804         PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
805     else {
806         sprintf(buf, endbits, bits);
807         PSDRV_WriteSpool(dc, buf, strlen(buf));
808     }
809
810     HeapFree(PSDRV_Heap, 0, buf);
811     return TRUE;
812 }
813
814
815 BOOL PSDRV_WriteBytes(DC *dc, const BYTE *bytes, int number)
816 {
817     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
818     char *ptr;
819     int i;
820     
821     ptr = buf;
822     
823     for(i = 0; i < number; i++) {
824         sprintf(ptr, "%02x%c", bytes[i],
825                 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
826         ptr += 3;
827     }
828     PSDRV_WriteSpool(dc, buf, number * 3);
829
830     HeapFree(PSDRV_Heap, 0, buf);
831     return TRUE;
832 }
833
834 BOOL PSDRV_WriteDIBits16(DC *dc, const WORD *words, int number)
835 {
836     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
837     char *ptr;
838     int i;
839     
840     ptr = buf;
841     
842     for(i = 0; i < number; i++) {
843         int r, g, b;
844
845         /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
846
847         r = words[i] >> 10 & 0x1f;
848         r = r << 3 | r >> 2;
849         g = words[i] >> 5 & 0x1f;
850         g = g << 3 | g >> 2;
851         b = words[i] & 0x1f;
852         b = b << 3 | b >> 2;
853         sprintf(ptr, "%02x%02x%02x%c", r, g, b,
854                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
855         ptr += 7;
856     }
857     PSDRV_WriteSpool(dc, buf, number * 7);
858
859     HeapFree(PSDRV_Heap, 0, buf);
860     return TRUE;
861 }
862
863 BOOL PSDRV_WriteDIBits24(DC *dc, const BYTE *bits, int number)
864 {
865     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
866     char *ptr;
867     int i;
868     
869     ptr = buf;
870     
871     for(i = 0; i < number; i++) {
872         sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
873                 bits[i * 3],
874                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
875         ptr += 7;
876     }
877     PSDRV_WriteSpool(dc, buf, number * 7);
878
879     HeapFree(PSDRV_Heap, 0, buf);
880     return TRUE;
881 }
882
883 BOOL PSDRV_WriteDIBits32(DC *dc, const BYTE *bits, int number)
884 {
885     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
886     char *ptr;
887     int i;
888     
889     ptr = buf;
890     
891     for(i = 0; i < number; i++) {
892         sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
893                 bits[i * 4],
894                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
895         ptr += 7;
896     }
897     PSDRV_WriteSpool(dc, buf, number * 7);
898
899     HeapFree(PSDRV_Heap, 0, buf);
900     return TRUE;
901 }
902
903 BOOL PSDRV_WriteArrayGet(DC *dc, CHAR *pszArrayName, INT nIndex)
904 {
905     char buf[100];
906
907     sprintf(buf, psarrayget, pszArrayName, nIndex);
908     return PSDRV_WriteSpool(dc, buf, strlen(buf));
909 }
910
911 BOOL PSDRV_WriteArrayPut(DC *dc, CHAR *pszArrayName, INT nIndex, LONG lObject)
912 {
913     char buf[100];
914
915     sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
916     return PSDRV_WriteSpool(dc, buf, strlen(buf));
917 }
918
919 BOOL PSDRV_WriteArrayDef(DC *dc, CHAR *pszArrayName, INT nSize)
920 {
921     char buf[100];
922
923     sprintf(buf, psarraydef, pszArrayName, nSize);
924     return PSDRV_WriteSpool(dc, buf, strlen(buf));
925 }
926
927 BOOL PSDRV_WriteRectClip(DC *dc, INT x, INT y, INT w, INT h)
928 {
929     char buf[100];
930
931     sprintf(buf, psrectclip, x, y, w, h);
932     return PSDRV_WriteSpool(dc, buf, strlen(buf));
933 }
934
935 BOOL PSDRV_WriteRectClip2(DC *dc, CHAR *pszArrayName)
936 {
937     char buf[100];
938
939     sprintf(buf, psrectclip2, pszArrayName);
940     return PSDRV_WriteSpool(dc, buf, strlen(buf));
941 }
942
943 BOOL PSDRV_WritePatternDict(DC *dc, BITMAP *bm, BYTE *bits)
944 {
945     char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
946       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n";
947
948     char end[] = "  end\n }\n>>\n matrix makepattern setpattern\n";
949     char *buf, *ptr;
950     INT w, h, x, y;
951     COLORREF map[2];
952
953     w = bm->bmWidth & ~0x7;
954     h = bm->bmHeight & ~0x7;
955
956     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
957     sprintf(buf, start, w, h, w, h);
958     PSDRV_WriteSpool(dc,  buf, strlen(buf));
959     PSDRV_WriteIndexColorSpaceBegin(dc, 1);
960     map[0] = dc->textColor;
961     map[1] = dc->backgroundColor;
962     PSDRV_WriteRGB(dc, map, 2);
963     PSDRV_WriteIndexColorSpaceEnd(dc);
964     ptr = buf;
965     for(y = h-1; y >= 0; y--) {
966         for(x = 0; x < w/8; x++) {
967             sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
968             ptr += 2;
969         }
970     }
971     PSDRV_WriteImageDict(dc, 1, 0, 0, 8, 8, 8, 8, buf);
972     PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
973     HeapFree(PSDRV_Heap, 0, buf);
974     return TRUE;
975 }
976
977
978
979