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