shell32: Remove casts to the type of the casted expression.
[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(PHYSDEV dev, LPCSTR lpData, DWORD cch)
190 {
191     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
192     int num, num_left = cch;
193
194     if(physDev->job.quiet) {
195         TRACE("ignoring output\n");
196         return 0;
197     }
198
199     if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
200         write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
201         physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
202     }
203
204     if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
205         if( !PSDRV_StartPage(dev) )
206             return 0;
207     }
208
209     do {
210         num = min(num_left, 0x8000);
211         if(write_spool( dev, lpData, num ) != num)
212             return 0;
213         lpData += num;
214         num_left -= num;
215     } while(num_left);
216
217     return cch;
218 }
219
220
221 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
222 {
223
224     char *buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
225                            strlen(feature) + strlen(value));
226
227     sprintf(buf, psbeginfeature, feature, value);
228     write_spool( dev, buf, strlen(buf) );
229     write_spool( dev, invocation, strlen(invocation) );
230     write_spool( dev, psendfeature, strlen(psendfeature) );
231
232     HeapFree( PSDRV_Heap, 0, buf );
233     return 1;
234 }
235
236 /********************************************************
237  *         escape_title
238  *
239  * Helper for PSDRV_WriteHeader.  Escape any non-printable characters
240  * as octal.  If we've had to use an escape then surround the entire string
241  * in brackets.  Truncate string to represent at most 0x80 characters.
242  *
243  */
244 static char *escape_title(LPCSTR str)
245 {
246     char *ret, *cp;
247     int i, extra = 0;
248
249     if(!str)
250     {
251         ret = HeapAlloc(GetProcessHeap(), 0, 1);
252         *ret = '\0';
253         return ret;
254     }
255
256     for(i = 0; i < 0x80 && str[i]; i++)
257     {
258         if(!isprint(str[i]))
259            extra += 3;
260     }
261
262     if(!extra)
263     {
264         ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
265         memcpy(ret, str, i);
266         ret[i] = '\0';
267         return ret;
268     }
269
270     extra += 2; /* two for the brackets */
271     cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
272     *cp++ = '(';
273     for(i = 0; i < 0x80 && str[i]; i++)
274     {
275         if(!isprint(str[i]))
276         {
277             BYTE b = (BYTE)str[i];
278             *cp++ = '\\';
279             *cp++ = ((b >> 6) & 0x7) + '0';
280             *cp++ = ((b >> 3) & 0x7) + '0';
281             *cp++ = ((b)      & 0x7) + '0';
282         }
283         else
284             *cp++ = str[i];
285     }
286     *cp++ = ')';
287     *cp = '\0';
288     return ret;
289 }
290
291
292 INT PSDRV_WriteHeader( PHYSDEV dev, LPCSTR title )
293 {
294     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
295     char *buf, *escaped_title;
296     INPUTSLOT *slot;
297     PAGESIZE *page;
298     DUPLEX *duplex;
299     int win_duplex;
300     int llx, lly, urx, ury;
301
302     TRACE("%s\n", debugstr_a(title));
303
304     escaped_title = escape_title(title);
305     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
306                      strlen(escaped_title) + 30 );
307     if(!buf) {
308         WARN("HeapAlloc failed\n");
309         return 0;
310     }
311
312     /* BBox co-ords are in default user co-ord system so urx < ury even in
313        landscape mode */
314     llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
315     lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
316     urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
317     ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
318     /* FIXME should do something better with BBox */
319
320     sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
321
322     HeapFree(GetProcessHeap(), 0, escaped_title);
323     if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
324         WARN("WriteSpool error\n");
325         HeapFree( PSDRV_Heap, 0, buf );
326         return 0;
327     }
328     HeapFree( PSDRV_Heap, 0, buf );
329
330     write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
331     write_spool( dev, psprolog, strlen(psprolog) );
332     write_spool( dev, psendprolog, strlen(psendprolog) );
333     write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
334
335     if(physDev->Devmode->dmPublic.u1.s1.dmCopies > 1) {
336         char copies_buf[100];
337         sprintf(copies_buf, "mark {\n << /NumCopies %d >> setpagedevice\n} stopped cleartomark\n", physDev->Devmode->dmPublic.u1.s1.dmCopies);
338         write_spool(dev, copies_buf, strlen(copies_buf));
339     }
340
341     for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
342         if(slot->WinBin == physDev->Devmode->dmPublic.u1.s1.dmDefaultSource) {
343             if(slot->InvocationString) {
344                 PSDRV_WriteFeature(dev, "*InputSlot", slot->Name,
345                              slot->InvocationString);
346                 break;
347             }
348         }
349     }
350
351     LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
352         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
353             if(page->InvocationString) {
354                 PSDRV_WriteFeature(dev, "*PageSize", page->Name,
355                              page->InvocationString);
356                 break;
357             }
358         }
359     }
360
361     win_duplex = physDev->Devmode->dmPublic.dmFields & DM_DUPLEX ?
362         physDev->Devmode->dmPublic.dmDuplex : 0;
363     for(duplex = physDev->pi->ppd->Duplexes; duplex; duplex = duplex->next) {
364         if(duplex->WinDuplex == win_duplex) {
365             if(duplex->InvocationString) {
366                 PSDRV_WriteFeature(dev, "*Duplex", duplex->Name,
367                              duplex->InvocationString);
368                 break;
369             }
370         }
371     }
372
373     write_spool( dev, psendsetup, strlen(psendsetup) );
374
375
376     return 1;
377 }
378
379
380 INT PSDRV_WriteFooter( PHYSDEV dev )
381 {
382     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
383     char *buf;
384
385     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
386     if(!buf) {
387         WARN("HeapAlloc failed\n");
388         return 0;
389     }
390
391     sprintf(buf, psfooter, physDev->job.PageNo);
392
393     if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
394         WARN("WriteSpool error\n");
395         HeapFree( PSDRV_Heap, 0, buf );
396         return 0;
397     }
398     HeapFree( PSDRV_Heap, 0, buf );
399     return 1;
400 }
401
402
403
404 INT PSDRV_WriteEndPage( PHYSDEV dev )
405 {
406     if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
407         WARN("WriteSpool error\n");
408         return 0;
409     }
410     return 1;
411 }
412
413
414
415
416 INT PSDRV_WriteNewPage( PHYSDEV dev )
417 {
418     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
419     char *buf;
420     char name[100];
421     signed int xtrans, ytrans, rotation;
422
423     sprintf(name, "%d", physDev->job.PageNo);
424
425     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
426     if(!buf) {
427         WARN("HeapAlloc failed\n");
428         return 0;
429     }
430
431     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
432         if(physDev->pi->ppd->LandscapeOrientation == -90) {
433             xtrans = physDev->ImageableArea.right;
434             ytrans = physDev->ImageableArea.top;
435             rotation = 90;
436         } else {
437             xtrans = physDev->ImageableArea.left;
438             ytrans = physDev->ImageableArea.bottom;
439             rotation = -90;
440         }
441     } else {
442         xtrans = physDev->ImageableArea.left;
443         ytrans = physDev->ImageableArea.top;
444         rotation = 0;
445     }
446
447     sprintf(buf, psnewpage, name, physDev->job.PageNo,
448             physDev->logPixelsX, physDev->logPixelsY,
449             xtrans, ytrans, rotation);
450
451     if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
452         WARN("WriteSpool error\n");
453         HeapFree( PSDRV_Heap, 0, buf );
454         return 0;
455     }
456     HeapFree( PSDRV_Heap, 0, buf );
457     return 1;
458 }
459
460
461 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
462 {
463     char buf[100];
464
465     sprintf(buf, psmoveto, x, y);
466     return PSDRV_WriteSpool(dev, buf, strlen(buf));
467 }
468
469 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
470 {
471     char buf[100];
472
473     sprintf(buf, pslineto, x, y);
474     return PSDRV_WriteSpool(dev, buf, strlen(buf));
475 }
476
477
478 BOOL PSDRV_WriteStroke(PHYSDEV dev)
479 {
480     return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
481 }
482
483
484
485 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
486                         INT height)
487 {
488     char buf[100];
489
490     sprintf(buf, psrectangle, x, y, width, height, -width);
491     return PSDRV_WriteSpool(dev, buf, strlen(buf));
492 }
493
494 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
495                       double ang2)
496 {
497     char buf[256];
498
499     /* Make angles -ve and swap order because we're working with an upside
500        down y-axis */
501     push_lc_numeric("C");
502     sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
503     pop_lc_numeric();
504     return PSDRV_WriteSpool(dev, buf, strlen(buf));
505 }
506
507
508 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement)
509 {
510     char *buf;
511
512     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(pssetfont) +
513                              strlen(name) + 40);
514
515     if(!buf) {
516         WARN("HeapAlloc failed\n");
517         return FALSE;
518     }
519
520     sprintf(buf, pssetfont, name, size.xx, size.xy, size.yx, size.yy, -escapement);
521
522     PSDRV_WriteSpool(dev, buf, strlen(buf));
523     HeapFree(PSDRV_Heap, 0, buf);
524     return TRUE;
525 }
526
527 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
528 {
529     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
530     char buf[256];
531
532     PSDRV_CopyColor(&physDev->inkColor, color);
533     switch(color->type) {
534     case PSCOLOR_RGB:
535         push_lc_numeric("C");
536         sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
537                 color->value.rgb.b);
538         pop_lc_numeric();
539         return PSDRV_WriteSpool(dev, buf, strlen(buf));
540
541     case PSCOLOR_GRAY:
542         push_lc_numeric("C");
543         sprintf(buf, pssetgray, color->value.gray.i);
544         pop_lc_numeric();
545         return PSDRV_WriteSpool(dev, buf, strlen(buf));
546
547     default:
548         ERR("Unkonwn colour type %d\n", color->type);
549         break;
550     }
551
552     return FALSE;
553 }
554
555 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
556 {
557     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
558     char buf[256];
559
560     sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
561     PSDRV_WriteSpool(dev, buf, strlen(buf));
562
563     if(physDev->pen.dash) {
564         sprintf(buf, pssetdash, physDev->pen.dash, 0);
565     }
566     else
567         sprintf(buf, pssetdash, "", 0);
568    
569    PSDRV_WriteSpool(dev, buf, strlen(buf));
570         
571    return TRUE;
572 }
573
574 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
575 {
576     char    buf[128];
577     int     l;
578
579     l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
580
581     if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
582         WARN("Unusable glyph name '%s' - ignoring\n", g_name);
583         return FALSE;
584     }
585
586     PSDRV_WriteSpool(dev, buf, l);
587     return TRUE;
588 }
589
590 BOOL PSDRV_WriteFill(PHYSDEV dev)
591 {
592     return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
593 }
594
595 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
596 {
597     return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
598 }
599
600 BOOL PSDRV_WriteGSave(PHYSDEV dev)
601 {
602     return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
603 }
604
605 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
606 {
607     return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
608 }
609
610 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
611 {
612     return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
613 }
614
615 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
616 {
617     return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
618 }
619
620 BOOL PSDRV_WriteClip(PHYSDEV dev)
621 {
622     return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
623 }
624
625 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
626 {
627     return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
628 }
629
630 BOOL PSDRV_WriteHatch(PHYSDEV dev)
631 {
632     return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
633 }
634
635 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
636 {
637     char buf[256];
638
639     push_lc_numeric("C");
640     sprintf(buf, psrotate, ang);
641     pop_lc_numeric();
642     return PSDRV_WriteSpool(dev, buf, strlen(buf));
643 }
644
645 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
646 {
647     char buf[256];
648     sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
649     return PSDRV_WriteSpool(dev, buf, strlen(buf));
650 }
651
652 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
653 {
654     static const char buf[] = ">\n] setcolorspace\n";
655     return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
656 }
657
658 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
659 {
660     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
661     int i;
662
663     ptr = buf;
664     for(i = 0; i < number; i++) {
665         sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
666                 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
667                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
668         ptr += 7;
669     }
670     PSDRV_WriteSpool(dev, buf, number * 7);
671     HeapFree(PSDRV_Heap, 0, buf);
672     return TRUE;
673 }
674
675 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
676 {
677     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
678     int i;
679
680     ptr = buf;
681     for(i = 0; i < number; i++)
682         ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
683                        ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
684
685     PSDRV_WriteSpool(dev, buf, ptr - buf);
686     HeapFree(PSDRV_Heap, 0, buf);
687     return TRUE;
688 }
689
690 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth,
691                                  INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
692 {
693     static const char start[] = "<<\n"
694       " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
695       " /ImageMatrix [%d 0 0 %d 0 %d]\n";
696
697     static const char decode1[] = " /Decode [0 %d]\n";
698     static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
699
700     static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
701     static const char endbits[] = " /DataSource <%s>\n>>\n";
702
703     char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
704
705     if (top_down)
706         sprintf(buf, start, widthSrc, heightSrc,
707                 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
708     else
709         sprintf(buf, start, widthSrc, heightSrc,
710                 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
711
712     PSDRV_WriteSpool(dev, buf, strlen(buf));
713
714     switch(depth) {
715     case 8:
716         sprintf(buf, decode1, 255);
717         break;
718
719     case 4:
720         sprintf(buf, decode1, 15);
721         break;
722
723     case 1:
724         sprintf(buf, decode1, 1);
725         break;
726
727     default:
728         strcpy(buf, decode3);
729         break;
730     }
731
732     PSDRV_WriteSpool(dev, buf, strlen(buf));
733
734     if(!bits) {
735         PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
736     } else {
737         sprintf(buf, endbits, bits);
738         PSDRV_WriteSpool(dev, buf, strlen(buf));
739     }
740
741     HeapFree(PSDRV_Heap, 0, buf);
742     return TRUE;
743 }
744
745 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, INT xDst, INT yDst,
746                       INT widthDst, INT heightDst, INT widthSrc,
747                       INT heightSrc, BOOL mask, BOOL top_down)
748 {
749     static const char start[] = "%d %d translate\n%d %d scale\n";
750     static const char image[] = "image\n";
751     static const char imagemask[] = "imagemask\n";
752     char buf[100];
753
754     sprintf(buf, start, xDst, yDst, widthDst, heightDst);
755     PSDRV_WriteSpool(dev, buf, strlen(buf));
756     PSDRV_WriteImageDict(dev, depth, widthSrc, heightSrc, NULL, top_down);
757     if(mask)
758         PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
759     else
760         PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
761     return TRUE;
762 }
763
764
765 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
766 {
767     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
768     char *ptr;
769     unsigned int i;
770
771     ptr = buf;
772
773     for(i = 0; i < number; i++) {
774         sprintf(ptr, "%02x", bytes[i]);
775         ptr += 2;
776         if(((i & 0xf) == 0xf) || (i == number - 1)) {
777             strcpy(ptr, "\n");
778             ptr++;
779         }
780     }
781     PSDRV_WriteSpool(dev, buf, ptr - buf);
782     HeapFree(PSDRV_Heap, 0, buf);
783     return TRUE;
784 }
785
786 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
787 {
788     int num, num_left = number;
789
790     do {
791         num = min(num_left, 60);
792         PSDRV_WriteSpool(dev, (LPCSTR)data, num);
793         PSDRV_WriteSpool(dev, "\n", 1);
794         data += num;
795         num_left -= num;
796     } while(num_left);
797
798     return TRUE;
799 }
800
801 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
802 {
803     char buf[100];
804
805     sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
806     return PSDRV_WriteSpool(dev, buf, strlen(buf));
807 }
808
809 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
810 {
811     char buf[100];
812
813     sprintf(buf, psarraydef, pszArrayName, nSize);
814     return PSDRV_WriteSpool(dev, buf, strlen(buf));
815 }
816
817 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
818 {
819     char buf[100];
820
821     sprintf(buf, psrectclip, x, y, w, h);
822     return PSDRV_WriteSpool(dev, buf, strlen(buf));
823 }
824
825 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
826 {
827     char buf[100];
828
829     sprintf(buf, psrectclip2, pszArrayName);
830     return PSDRV_WriteSpool(dev, buf, strlen(buf));
831 }
832
833 BOOL PSDRV_WritePatternDict(PHYSDEV dev, BITMAP *bm, BYTE *bits)
834 {
835     static const char mypat[] = "/mypat\n";
836     static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
837       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n  0 0 translate\n"
838       "  %d %d scale\n  mypat image\n  end\n }\n>>\n matrix makepattern setpattern\n";
839
840     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
841     char *buf, *ptr;
842     INT w, h, x, y, w_mult, h_mult;
843     COLORREF map[2];
844
845     w = bm->bmWidth & ~0x7;
846     h = bm->bmHeight & ~0x7;
847
848     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(do_pattern) + 100);
849     ptr = buf;
850     for(y = h-1; y >= 0; y--) {
851         for(x = 0; x < w/8; x++) {
852             sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
853             ptr += 2;
854         }
855     }
856     PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
857     PSDRV_WriteImageDict(dev, 1, 8, 8, buf, FALSE);
858     PSDRV_WriteSpool(dev, "def\n", 4);
859
860     PSDRV_WriteIndexColorSpaceBegin(dev, 1);
861     map[0] = GetTextColor( dev->hdc );
862     map[1] = GetBkColor( dev->hdc );
863     PSDRV_WriteRGB(dev, map, 2);
864     PSDRV_WriteIndexColorSpaceEnd(dev);
865
866     /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
867     w_mult = (physDev->logPixelsX + 150) / 300;
868     h_mult = (physDev->logPixelsY + 150) / 300;
869     sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
870     PSDRV_WriteSpool(dev,  buf, strlen(buf));
871
872     HeapFree(PSDRV_Heap, 0, buf);
873     return TRUE;
874 }
875
876 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, BITMAPINFO *bmi, UINT usage)
877 {
878     static const char mypat[] = "/mypat\n";
879     static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
880       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n  0 0 translate\n"
881       "  %d %d scale\n  mypat image\n  end\n }\n>>\n matrix makepattern setpattern\n";
882     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
883     char *buf, *ptr;
884     BYTE *bits;
885     INT w, h, x, y, colours, w_mult, h_mult;
886     COLORREF map[2];
887
888     if(bmi->bmiHeader.biBitCount != 1) {
889         FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
890         return FALSE;
891     }
892
893     bits = (LPBYTE)bmi + bmi->bmiHeader.biSize;
894     colours = bmi->bmiHeader.biClrUsed;
895     if (colours > 256) colours = 256;
896     if(!colours && bmi->bmiHeader.biBitCount <= 8)
897         colours = 1 << bmi->bmiHeader.biBitCount;
898     bits += colours * ((usage == DIB_RGB_COLORS) ?
899                        sizeof(RGBQUAD) : sizeof(WORD));
900
901     w = bmi->bmiHeader.biWidth & ~0x7;
902     h = bmi->bmiHeader.biHeight & ~0x7;
903
904     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(do_pattern) + 100);
905     ptr = buf;
906     for(y = h-1; y >= 0; y--) {
907         for(x = 0; x < w/8; x++) {
908             sprintf(ptr, "%02x", *(bits + x/8 + y *
909                                    (bmi->bmiHeader.biWidth + 31) / 32 * 4));
910             ptr += 2;
911         }
912     }
913     PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
914     PSDRV_WriteImageDict(dev, 1, 8, 8, buf, FALSE);
915     PSDRV_WriteSpool(dev, "def\n", 4);
916
917     PSDRV_WriteIndexColorSpaceBegin(dev, 1);
918     map[0] = GetTextColor( dev->hdc );
919     map[1] = GetBkColor( dev->hdc );
920     PSDRV_WriteRGB(dev, map, 2);
921     PSDRV_WriteIndexColorSpaceEnd(dev);
922
923     /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
924     w_mult = (physDev->logPixelsX + 150) / 300;
925     h_mult = (physDev->logPixelsY + 150) / 300;
926     sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
927     PSDRV_WriteSpool(dev,  buf, strlen(buf));
928     HeapFree(PSDRV_Heap, 0, buf);
929     return TRUE;
930 }