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