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