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