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