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