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