quartz: Change VideoRenderer_Run to handle EndOfStream slightly better.
[wine] / dlls / wineps.drv / ps.c
1 /*
2  *      PostScript output functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <locale.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36
37 static const char psheader[] = /* title llx lly urx ury */
38 "%%!PS-Adobe-3.0\n"
39 "%%%%Creator: Wine PostScript Driver\n"
40 "%%%%Title: %s\n"
41 "%%%%BoundingBox: %d %d %d %d\n"
42 "%%%%Pages: (atend)\n"
43 "%%%%EndComments\n";
44
45 static const char psbeginprolog[] =
46 "%%BeginProlog\n";
47
48 static const char psendprolog[] =
49 "%%EndProlog\n";
50
51 static const char psprolog[] =
52 "/tmpmtrx matrix def\n"
53 "/hatch {\n"
54 "  pathbbox\n"
55 "  /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
56 "  l cvi gap idiv gap mul\n"
57 "  gap\n"
58 "  r cvi gap idiv gap mul\n"
59 "  {t moveto 0 b t sub rlineto}\n"
60 "  for\n"
61 "} bind def\n"
62 "/B {pop pop pop pop} def\n"
63 "/N {newpath} def\n"
64 "/havetype42gdir {version cvi 2015 ge} bind def\n";
65
66 static const char psbeginsetup[] =
67 "%%BeginSetup\n";
68
69 static const char psendsetup[] =
70 "%%EndSetup\n";
71
72 static const char psbeginfeature[] = /* feature, value */
73 "mark {\n"
74 "%%%%BeginFeature: %s %s\n";
75
76 static const char psendfeature[] =
77 "\n%%EndFeature\n"
78 "} stopped cleartomark\n";
79
80 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
81 "%%%%Page: %s %d\n"
82 "%%%%BeginPageSetup\n"
83 "/pgsave save def\n"
84 "72 %d div 72 %d div scale\n"
85 "%d %d translate\n"
86 "1 -1 scale\n"
87 "%d rotate\n"
88 "%%%%EndPageSetup\n";
89
90 static const char psendpage[] =
91 "pgsave restore\n"
92 "showpage\n";
93
94 static const char psfooter[] = /* pages */
95 "%%%%Trailer\n"
96 "%%%%Pages: %d\n"
97 "%%%%EOF\n";
98
99 static const char psmoveto[] = /* x, y */
100 "%d %d moveto\n";
101
102 static const char pslineto[] = /* x, y */
103 "%d %d lineto\n";
104
105 static const char psstroke[] =
106 "stroke\n";
107
108 static const char psrectangle[] = /* x, y, width, height, -width */
109 "%d %d moveto\n"
110 "%d 0 rlineto\n"
111 "0 %d rlineto\n"
112 "%d 0 rlineto\n"
113 "closepath\n";
114
115 static const char psglyphshow[] = /* glyph name */
116 "/%s glyphshow\n";
117
118 static const char pssetfont[] = /* fontname, xx_scale, xy_scale, yx_scale, yy_scale, escapement */
119 "/%s findfont\n"
120 "[%d %d %d %d 0 0]\n"
121 "%d 10 div matrix rotate\n"
122 "matrix concatmatrix\n"
123 "makefont setfont\n";
124
125 static const char pssetline[] = /* width, join, endcap */
126 "%d setlinewidth %u setlinejoin %u setlinecap\n";
127
128 static const char pssetdash[] = /* dash, offset */
129 "[%s] %d setdash\n";
130
131 static const char pssetgray[] = /* gray */
132 "%.2f setgray\n";
133
134 static const char pssetrgbcolor[] = /* r, g, b */
135 "%.2f %.2f %.2f setrgbcolor\n";
136
137 static const 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 const char psgsave[] =
145 "gsave\n";
146
147 static const char psgrestore[] =
148 "grestore\n";
149
150 static const char psfill[] =
151 "fill\n";
152
153 static const char pseofill[] =
154 "eofill\n";
155
156 static const char psnewpath[] =
157 "newpath\n";
158
159 static const char psclosepath[] =
160 "closepath\n";
161
162 static const char psclip[] =
163 "clip\n";
164
165 static const char pseoclip[] =
166 "eoclip\n";
167
168 static const char psrectclip[] =
169 "%d %d %d %d rectclip\n";
170
171 static const char psrectclip2[] =
172 "%s rectclip\n";
173
174 static const char pshatch[] =
175 "hatch\n";
176
177 static const char psrotate[] = /* ang */
178 "%.1f rotate\n";
179
180 static const char psarrayput[] =
181 "%s %d %d put\n";
182
183 static const char psarraydef[] =
184 "/%s %d array def\n";
185
186 static const char psenddocument[] =
187 "\n%%EndDocument\n";
188
189 DWORD PSDRV_WriteSpool(PSDRV_PDEVICE *physDev, LPCSTR lpData, DWORD cch)
190 {
191     int num, num_left = cch;
192
193     if(physDev->job.quiet) {
194         TRACE("ignoring output\n");
195         return 0;
196     }
197
198     if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
199         write_spool( physDev, psenddocument, sizeof(psenddocument)-1 );
200         physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
201     }
202
203     if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
204         if( !PSDRV_StartPage(physDev) )
205             return 0;
206     }
207
208     do {
209         num = min(num_left, 0x8000);
210         if(write_spool( physDev, lpData, num ) != num)
211             return 0;
212         lpData += num;
213         num_left -= num;
214     } while(num_left);
215
216     return cch;
217 }
218
219
220 static INT PSDRV_WriteFeature(PSDRV_PDEVICE *physDev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
221 {
222
223     char *buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
224                            strlen(feature) + strlen(value));
225
226     sprintf(buf, psbeginfeature, feature, value);
227     write_spool( physDev, buf, strlen(buf) );
228     write_spool( physDev, invocation, strlen(invocation) );
229     write_spool( physDev, psendfeature, strlen(psendfeature) );
230
231     HeapFree( PSDRV_Heap, 0, buf );
232     return 1;
233 }
234
235 /********************************************************
236  *         escape_title
237  *
238  * Helper for PSDRV_WriteHeader.  Escape any non-printable characters
239  * as octal.  If we've had to use an escape then surround the entire string
240  * in brackets.  Truncate string to represent at most 0x80 characters.
241  *
242  */
243 static char *escape_title(LPCSTR str)
244 {
245     char *ret, *cp;
246     int i, extra = 0;
247
248     if(!str)
249     {
250         ret = HeapAlloc(GetProcessHeap(), 0, 1);
251         *ret = '\0';
252         return ret;
253     }
254
255     for(i = 0; i < 0x80 && str[i]; i++)
256     {
257         if(!isprint(str[i]))
258            extra += 3;
259     }
260
261     if(!extra)
262     {
263         ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
264         memcpy(ret, str, i);
265         ret[i] = '\0';
266         return ret;
267     }
268
269     extra += 2; /* two for the brackets */
270     cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
271     *cp++ = '(';
272     for(i = 0; i < 0x80 && str[i]; i++)
273     {
274         if(!isprint(str[i]))
275         {
276             BYTE b = (BYTE)str[i];
277             *cp++ = '\\';
278             *cp++ = ((b >> 6) & 0x7) + '0';
279             *cp++ = ((b >> 3) & 0x7) + '0';
280             *cp++ = ((b)      & 0x7) + '0';
281         }
282         else
283             *cp++ = str[i];
284     }
285     *cp++ = ')';
286     *cp = '\0';
287     return ret;
288 }
289        
290
291 INT PSDRV_WriteHeader( PSDRV_PDEVICE *physDev, LPCSTR title )
292 {
293     char *buf, *escaped_title;
294     INPUTSLOT *slot;
295     PAGESIZE *page;
296     DUPLEX *duplex;
297     int win_duplex;
298     int llx, lly, urx, ury;
299
300     TRACE("%s\n", debugstr_a(title));
301
302     escaped_title = escape_title(title);
303     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
304                      strlen(escaped_title) + 30 );
305     if(!buf) {
306         WARN("HeapAlloc failed\n");
307         return 0;
308     }
309
310     /* BBox co-ords are in default user co-ord system so urx < ury even in
311        landscape mode */
312     llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
313     lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
314     urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
315     ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
316     /* FIXME should do something better with BBox */
317
318     sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
319
320     HeapFree(GetProcessHeap(), 0, escaped_title);
321     if( write_spool( physDev, buf, strlen(buf) ) != strlen(buf) ) {
322         WARN("WriteSpool error\n");
323         HeapFree( PSDRV_Heap, 0, buf );
324         return 0;
325     }
326     HeapFree( PSDRV_Heap, 0, buf );
327
328     write_spool( physDev, psbeginprolog, strlen(psbeginprolog) );
329     write_spool( physDev, psprolog, strlen(psprolog) );
330     write_spool( physDev, psendprolog, strlen(psendprolog) );
331     write_spool( physDev, psbeginsetup, strlen(psbeginsetup) );
332
333     if(physDev->Devmode->dmPublic.u1.s1.dmCopies > 1) {
334         char copies_buf[100];
335         sprintf(copies_buf, "mark {\n << /NumCopies %d >> setpagedevice\n} stopped cleartomark\n", physDev->Devmode->dmPublic.u1.s1.dmCopies);
336         write_spool(physDev, copies_buf, strlen(copies_buf));
337     }
338
339     for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
340         if(slot->WinBin == physDev->Devmode->dmPublic.u1.s1.dmDefaultSource) {
341             if(slot->InvocationString) {
342                 PSDRV_WriteFeature(physDev, "*InputSlot", slot->Name,
343                              slot->InvocationString);
344                 break;
345             }
346         }
347     }
348
349     LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
350         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
351             if(page->InvocationString) {
352                 PSDRV_WriteFeature(physDev, "*PageSize", page->Name,
353                              page->InvocationString);
354                 break;
355             }
356         }
357     }
358
359     win_duplex = physDev->Devmode->dmPublic.dmFields & DM_DUPLEX ?
360         physDev->Devmode->dmPublic.dmDuplex : 0;
361     for(duplex = physDev->pi->ppd->Duplexes; duplex; duplex = duplex->next) {
362         if(duplex->WinDuplex == win_duplex) {
363             if(duplex->InvocationString) {
364                 PSDRV_WriteFeature(physDev, "*Duplex", duplex->Name,
365                              duplex->InvocationString);
366                 break;
367             }
368         }
369     }
370
371     write_spool( physDev, psendsetup, strlen(psendsetup) );
372
373
374     return 1;
375 }
376
377
378 INT PSDRV_WriteFooter( PSDRV_PDEVICE *physDev )
379 {
380     char *buf;
381
382     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
383     if(!buf) {
384         WARN("HeapAlloc failed\n");
385         return 0;
386     }
387
388     sprintf(buf, psfooter, physDev->job.PageNo);
389
390     if( write_spool( physDev, buf, strlen(buf) ) != strlen(buf) ) {
391         WARN("WriteSpool error\n");
392         HeapFree( PSDRV_Heap, 0, buf );
393         return 0;
394     }
395     HeapFree( PSDRV_Heap, 0, buf );
396     return 1;
397 }
398
399
400
401 INT PSDRV_WriteEndPage( PSDRV_PDEVICE *physDev )
402 {
403     if( write_spool( physDev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
404         WARN("WriteSpool error\n");
405         return 0;
406     }
407     return 1;
408 }
409
410
411
412
413 INT PSDRV_WriteNewPage( PSDRV_PDEVICE *physDev )
414 {
415     char *buf;
416     char name[100];
417     signed int xtrans, ytrans, rotation;
418
419     sprintf(name, "%d", physDev->job.PageNo);
420
421     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
422     if(!buf) {
423         WARN("HeapAlloc failed\n");
424         return 0;
425     }
426
427     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
428         if(physDev->pi->ppd->LandscapeOrientation == -90) {
429             xtrans = physDev->ImageableArea.right;
430             ytrans = physDev->ImageableArea.top;
431             rotation = 90;
432         } else {
433             xtrans = physDev->ImageableArea.left;
434             ytrans = physDev->ImageableArea.bottom;
435             rotation = -90;
436         }
437     } else {
438         xtrans = physDev->ImageableArea.left;
439         ytrans = physDev->ImageableArea.top;
440         rotation = 0;
441     }
442
443     sprintf(buf, psnewpage, name, physDev->job.PageNo,
444             physDev->logPixelsX, physDev->logPixelsY,
445             xtrans, ytrans, rotation);
446
447     if( write_spool( physDev, buf, strlen(buf) ) != strlen(buf) ) {
448         WARN("WriteSpool error\n");
449         HeapFree( PSDRV_Heap, 0, buf );
450         return 0;
451     }
452     HeapFree( PSDRV_Heap, 0, buf );
453     return 1;
454 }
455
456
457 BOOL PSDRV_WriteMoveTo(PSDRV_PDEVICE *physDev, INT x, INT y)
458 {
459     char buf[100];
460
461     sprintf(buf, psmoveto, x, y);
462     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
463 }
464
465 BOOL PSDRV_WriteLineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
466 {
467     char buf[100];
468
469     sprintf(buf, pslineto, x, y);
470     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
471 }
472
473
474 BOOL PSDRV_WriteStroke(PSDRV_PDEVICE *physDev)
475 {
476     return PSDRV_WriteSpool(physDev, psstroke, sizeof(psstroke)-1);
477 }
478
479
480
481 BOOL PSDRV_WriteRectangle(PSDRV_PDEVICE *physDev, INT x, INT y, INT width,
482                         INT height)
483 {
484     char buf[100];
485
486     sprintf(buf, psrectangle, x, y, width, height, -width);
487     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
488 }
489
490 BOOL PSDRV_WriteArc(PSDRV_PDEVICE *physDev, INT x, INT y, INT w, INT h, double ang1,
491                       double ang2)
492 {
493     char buf[256];
494
495     /* Make angles -ve and swap order because we're working with an upside
496        down y-axis */
497     push_lc_numeric("C");
498     sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
499     pop_lc_numeric();
500     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
501 }
502
503
504 BOOL PSDRV_WriteSetFont(PSDRV_PDEVICE *physDev, const char *name, matrix size, INT escapement)
505 {
506     char *buf;
507
508     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(pssetfont) +
509                              strlen(name) + 40);
510
511     if(!buf) {
512         WARN("HeapAlloc failed\n");
513         return FALSE;
514     }
515
516     sprintf(buf, pssetfont, name, size.xx, size.xy, size.yx, size.yy, -escapement);
517
518     PSDRV_WriteSpool(physDev, buf, strlen(buf));
519     HeapFree(PSDRV_Heap, 0, buf);
520     return TRUE;
521 }
522
523 BOOL PSDRV_WriteSetColor(PSDRV_PDEVICE *physDev, PSCOLOR *color)
524 {
525     char buf[256];
526
527     PSDRV_CopyColor(&physDev->inkColor, color);
528     switch(color->type) {
529     case PSCOLOR_RGB:
530         push_lc_numeric("C");
531         sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
532                 color->value.rgb.b);
533         pop_lc_numeric();
534         return PSDRV_WriteSpool(physDev, buf, strlen(buf));
535
536     case PSCOLOR_GRAY:
537         push_lc_numeric("C");
538         sprintf(buf, pssetgray, color->value.gray.i);
539         pop_lc_numeric();
540         return PSDRV_WriteSpool(physDev, buf, strlen(buf));
541
542     default:
543         ERR("Unkonwn colour type %d\n", color->type);
544         break;
545     }
546
547     return FALSE;
548 }
549
550 BOOL PSDRV_WriteSetPen(PSDRV_PDEVICE *physDev)
551 {
552     char buf[256];
553
554     sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
555     PSDRV_WriteSpool(physDev, buf, strlen(buf));
556
557     if(physDev->pen.dash) {
558         sprintf(buf, pssetdash, physDev->pen.dash, 0);
559     }
560     else
561         sprintf(buf, pssetdash, "", 0);
562    
563    PSDRV_WriteSpool(physDev, buf, strlen(buf));
564         
565    return TRUE;
566 }
567
568 BOOL PSDRV_WriteGlyphShow(PSDRV_PDEVICE *physDev, LPCSTR g_name)
569 {
570     char    buf[128];
571     int     l;
572
573     l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
574
575     if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
576         WARN("Unusable glyph name '%s' - ignoring\n", g_name);
577         return FALSE;
578     }
579
580     PSDRV_WriteSpool(physDev, buf, l);
581     return TRUE;
582 }
583
584 BOOL PSDRV_WriteFill(PSDRV_PDEVICE *physDev)
585 {
586     return PSDRV_WriteSpool(physDev, psfill, sizeof(psfill)-1);
587 }
588
589 BOOL PSDRV_WriteEOFill(PSDRV_PDEVICE *physDev)
590 {
591     return PSDRV_WriteSpool(physDev, pseofill, sizeof(pseofill)-1);
592 }
593
594 BOOL PSDRV_WriteGSave(PSDRV_PDEVICE *physDev)
595 {
596     return PSDRV_WriteSpool(physDev, psgsave, sizeof(psgsave)-1);
597 }
598
599 BOOL PSDRV_WriteGRestore(PSDRV_PDEVICE *physDev)
600 {
601     return PSDRV_WriteSpool(physDev, psgrestore, sizeof(psgrestore)-1);
602 }
603
604 BOOL PSDRV_WriteNewPath(PSDRV_PDEVICE *physDev)
605 {
606     return PSDRV_WriteSpool(physDev, psnewpath, sizeof(psnewpath)-1);
607 }
608
609 BOOL PSDRV_WriteClosePath(PSDRV_PDEVICE *physDev)
610 {
611     return PSDRV_WriteSpool(physDev, psclosepath, sizeof(psclosepath)-1);
612 }
613
614 BOOL PSDRV_WriteClip(PSDRV_PDEVICE *physDev)
615 {
616     return PSDRV_WriteSpool(physDev, psclip, sizeof(psclip)-1);
617 }
618
619 BOOL PSDRV_WriteEOClip(PSDRV_PDEVICE *physDev)
620 {
621     return PSDRV_WriteSpool(physDev, pseoclip, sizeof(pseoclip)-1);
622 }
623
624 BOOL PSDRV_WriteHatch(PSDRV_PDEVICE *physDev)
625 {
626     return PSDRV_WriteSpool(physDev, pshatch, sizeof(pshatch)-1);
627 }
628
629 BOOL PSDRV_WriteRotate(PSDRV_PDEVICE *physDev, float ang)
630 {
631     char buf[256];
632
633     push_lc_numeric("C");
634     sprintf(buf, psrotate, ang);
635     pop_lc_numeric();
636     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
637 }
638
639 BOOL PSDRV_WriteIndexColorSpaceBegin(PSDRV_PDEVICE *physDev, int size)
640 {
641     char buf[256];
642     sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
643     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
644 }
645
646 BOOL PSDRV_WriteIndexColorSpaceEnd(PSDRV_PDEVICE *physDev)
647 {
648     static const char buf[] = ">\n] setcolorspace\n";
649     return PSDRV_WriteSpool(physDev, buf, sizeof(buf) - 1);
650 }
651
652 BOOL PSDRV_WriteRGB(PSDRV_PDEVICE *physDev, COLORREF *map, int number)
653 {
654     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
655     int i;
656
657     ptr = buf;
658     for(i = 0; i < number; i++) {
659         sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
660                 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
661                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
662         ptr += 7;
663     }
664     PSDRV_WriteSpool(physDev, buf, number * 7);
665     HeapFree(PSDRV_Heap, 0, buf);
666     return TRUE;
667 }
668
669 static BOOL PSDRV_WriteImageDict(PSDRV_PDEVICE *physDev, WORD depth,
670                                  INT widthSrc, INT heightSrc, char *bits)
671 {
672     static const char start[] = "<<\n"
673       " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
674       " /ImageMatrix [%d 0 0 %d 0 %d]\n";
675
676     static const char decode1[] = " /Decode [0 %d]\n";
677     static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
678
679     static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
680     static const char endbits[] = " /DataSource <%s>\n>>\n";
681
682     char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
683
684     sprintf(buf, start, widthSrc, heightSrc,
685             (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
686
687     PSDRV_WriteSpool(physDev, buf, strlen(buf));
688
689     switch(depth) {
690     case 8:
691         sprintf(buf, decode1, 255);
692         break;
693
694     case 4:
695         sprintf(buf, decode1, 15);
696         break;
697
698     case 1:
699         sprintf(buf, decode1, 1);
700         break;
701
702     default:
703         strcpy(buf, decode3);
704         break;
705     }
706
707     PSDRV_WriteSpool(physDev, buf, strlen(buf));
708
709     if(!bits) {
710         PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
711     } else {
712         sprintf(buf, endbits, bits);
713         PSDRV_WriteSpool(physDev, buf, strlen(buf));
714     }
715
716     HeapFree(PSDRV_Heap, 0, buf);
717     return TRUE;
718 }
719
720 BOOL PSDRV_WriteImage(PSDRV_PDEVICE *physDev, WORD depth, INT xDst, INT yDst,
721                       INT widthDst, INT heightDst, INT widthSrc,
722                       INT heightSrc, BOOL mask)
723 {
724     static const char start[] = "%d %d translate\n%d %d scale\n";
725     static const char image[] = "image\n";
726     static const char imagemask[] = "imagemask\n";
727     char buf[100];
728
729     sprintf(buf, start, xDst, yDst, widthDst, heightDst);
730     PSDRV_WriteSpool(physDev, buf, strlen(buf));
731     PSDRV_WriteImageDict(physDev, depth, widthSrc, heightSrc, NULL);
732     if(mask)
733         PSDRV_WriteSpool(physDev, imagemask, sizeof(imagemask) - 1);
734     else
735         PSDRV_WriteSpool(physDev, image, sizeof(image) - 1);
736     return TRUE;
737 }
738
739
740 BOOL PSDRV_WriteBytes(PSDRV_PDEVICE *physDev, const BYTE *bytes, DWORD number)
741 {
742     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
743     char *ptr;
744     unsigned int i;
745
746     ptr = buf;
747
748     for(i = 0; i < number; i++) {
749         sprintf(ptr, "%02x", bytes[i]);
750         ptr += 2;
751         if(((i & 0xf) == 0xf) || (i == number - 1)) {
752             strcpy(ptr, "\n");
753             ptr++;
754         }
755     }
756     PSDRV_WriteSpool(physDev, buf, ptr - buf);
757     HeapFree(PSDRV_Heap, 0, buf);
758     return TRUE;
759 }
760
761 BOOL PSDRV_WriteData(PSDRV_PDEVICE *physDev, const BYTE *data, DWORD number)
762 {
763     int num, num_left = number;
764
765     do {
766         num = min(num_left, 60);
767         PSDRV_WriteSpool(physDev, (LPCSTR)data, num);
768         PSDRV_WriteSpool(physDev, "\n", 1);
769         data += num;
770         num_left -= num;
771     } while(num_left);
772
773     return TRUE;
774 }
775
776 BOOL PSDRV_WriteArrayPut(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nIndex, LONG lObject)
777 {
778     char buf[100];
779
780     sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
781     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
782 }
783
784 BOOL PSDRV_WriteArrayDef(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nSize)
785 {
786     char buf[100];
787
788     sprintf(buf, psarraydef, pszArrayName, nSize);
789     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
790 }
791
792 BOOL PSDRV_WriteRectClip(PSDRV_PDEVICE *physDev, INT x, INT y, INT w, INT h)
793 {
794     char buf[100];
795
796     sprintf(buf, psrectclip, x, y, w, h);
797     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
798 }
799
800 BOOL PSDRV_WriteRectClip2(PSDRV_PDEVICE *physDev, CHAR *pszArrayName)
801 {
802     char buf[100];
803
804     sprintf(buf, psrectclip2, pszArrayName);
805     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
806 }
807
808 BOOL PSDRV_WritePatternDict(PSDRV_PDEVICE *physDev, BITMAP *bm, BYTE *bits)
809 {
810     static const char mypat[] = "/mypat\n";
811     static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
812       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n  0 0 translate\n"
813       "  %d %d scale\n  mypat image\n  end\n }\n>>\n matrix makepattern setpattern\n";
814
815     char *buf, *ptr;
816     INT w, h, x, y, w_mult, h_mult;
817     COLORREF map[2];
818
819     w = bm->bmWidth & ~0x7;
820     h = bm->bmHeight & ~0x7;
821
822     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(do_pattern) + 100);
823     ptr = buf;
824     for(y = h-1; y >= 0; y--) {
825         for(x = 0; x < w/8; x++) {
826             sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
827             ptr += 2;
828         }
829     }
830     PSDRV_WriteSpool(physDev, mypat, sizeof(mypat) - 1);
831     PSDRV_WriteImageDict(physDev, 1, 8, 8, buf);
832     PSDRV_WriteSpool(physDev, "def\n", 4);
833
834     PSDRV_WriteIndexColorSpaceBegin(physDev, 1);
835     map[0] = GetTextColor( physDev->hdc );
836     map[1] = GetBkColor( physDev->hdc );
837     PSDRV_WriteRGB(physDev, map, 2);
838     PSDRV_WriteIndexColorSpaceEnd(physDev);
839
840     /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
841     w_mult = (physDev->logPixelsX + 150) / 300;
842     h_mult = (physDev->logPixelsY + 150) / 300;
843     sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
844     PSDRV_WriteSpool(physDev,  buf, strlen(buf));
845
846     HeapFree(PSDRV_Heap, 0, buf);
847     return TRUE;
848 }
849
850 BOOL PSDRV_WriteDIBPatternDict(PSDRV_PDEVICE *physDev, BITMAPINFO *bmi, UINT usage)
851 {
852     static const char mypat[] = "/mypat\n";
853     static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
854       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n  0 0 translate\n"
855       "  %d %d scale\n  mypat image\n  end\n }\n>>\n matrix makepattern setpattern\n";
856     char *buf, *ptr;
857     BYTE *bits;
858     INT w, h, x, y, colours, w_mult, h_mult;
859     COLORREF map[2];
860
861     if(bmi->bmiHeader.biBitCount != 1) {
862         FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
863         return FALSE;
864     }
865
866     bits = (LPBYTE)bmi + bmi->bmiHeader.biSize;
867     colours = bmi->bmiHeader.biClrUsed;
868     if (colours > 256) colours = 256;
869     if(!colours && bmi->bmiHeader.biBitCount <= 8)
870         colours = 1 << bmi->bmiHeader.biBitCount;
871     bits += colours * ((usage == DIB_RGB_COLORS) ?
872                        sizeof(RGBQUAD) : sizeof(WORD));
873
874     w = bmi->bmiHeader.biWidth & ~0x7;
875     h = bmi->bmiHeader.biHeight & ~0x7;
876
877     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(do_pattern) + 100);
878     ptr = buf;
879     for(y = h-1; y >= 0; y--) {
880         for(x = 0; x < w/8; x++) {
881             sprintf(ptr, "%02x", *(bits + x/8 + y *
882                                    (bmi->bmiHeader.biWidth + 31) / 32 * 4));
883             ptr += 2;
884         }
885     }
886     PSDRV_WriteSpool(physDev, mypat, sizeof(mypat) - 1);
887     PSDRV_WriteImageDict(physDev, 1, 8, 8, buf);
888     PSDRV_WriteSpool(physDev, "def\n", 4);
889
890     PSDRV_WriteIndexColorSpaceBegin(physDev, 1);
891     map[0] = GetTextColor( physDev->hdc );
892     map[1] = GetBkColor( physDev->hdc );
893     PSDRV_WriteRGB(physDev, map, 2);
894     PSDRV_WriteIndexColorSpaceEnd(physDev);
895
896     /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
897     w_mult = (physDev->logPixelsX + 150) / 300;
898     h_mult = (physDev->logPixelsY + 150) / 300;
899     sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
900     PSDRV_WriteSpool(physDev,  buf, strlen(buf));
901     HeapFree(PSDRV_Heap, 0, buf);
902     return TRUE;
903 }