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