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