Avoid local WINAPI function pointers in _invoke.
[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 orientation */
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 "%%%%Orientation: %s\n"
41 "%%%%EndComments\n";
42
43 static char psbeginprolog[] =
44 "%%BeginProlog\n";
45
46 static char psendprolog[] =
47 "%%EndProlog\n";
48
49 static char psprolog[] =
50 "/tmpmtrx matrix def\n"
51 "/hatch {\n"
52 "  pathbbox\n"
53 "  /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
54 "  l cvi gap idiv gap mul\n"
55 "  gap\n"
56 "  r cvi gap idiv gap mul\n"
57 "  {t moveto 0 b t sub rlineto}\n"
58 "  for\n"
59 "} bind def\n"
60 "/B {pop pop pop pop} 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, *orient;
235     INPUTSLOT *slot;
236     PAGESIZE *page;
237     int llx, lly, urx, ury;
238
239     TRACE("'%s'\n", title);
240
241     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
242                              strlen(title) + 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
255     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
256         orient = "Landscape";
257     } else {
258         orient = "Portrait";
259     }
260
261     /* FIXME should do something better with BBox */
262
263     sprintf(buf, psheader, title, llx, lly, urx, ury, orient);
264
265     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
266                                                      strlen(buf) ) {
267         WARN("WriteSpool error\n");
268         HeapFree( PSDRV_Heap, 0, buf );
269         return 0;
270     }
271     HeapFree( PSDRV_Heap, 0, buf );
272
273     WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
274     WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
275     WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
276
277     WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
278
279     for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
280         if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
281             if(slot->InvocationString) {
282                 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
283                              slot->InvocationString);
284                 break;
285             }
286         }
287     }
288
289     for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
290         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
291             if(page->InvocationString) {
292                 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
293                              page->InvocationString);
294                 break;
295             }
296         }
297     }
298
299     WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
300
301
302     return 1;
303 }
304
305
306 INT PSDRV_WriteFooter( PSDRV_PDEVICE *physDev )
307 {
308     char *buf;
309
310     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
311     if(!buf) {
312         WARN("HeapAlloc failed\n");
313         return 0;
314     }
315
316     sprintf(buf, psfooter, physDev->job.PageNo);
317
318     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
319                                                      strlen(buf) ) {
320         WARN("WriteSpool error\n");
321         HeapFree( PSDRV_Heap, 0, buf );
322         return 0;
323     }
324     HeapFree( PSDRV_Heap, 0, buf );
325     return 1;
326 }
327
328
329
330 INT PSDRV_WriteEndPage( PSDRV_PDEVICE *physDev )
331 {
332     if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) !=
333                                                      sizeof(psendpage)-1 ) {
334         WARN("WriteSpool error\n");
335         return 0;
336     }
337     return 1;
338 }
339
340
341
342
343 INT PSDRV_WriteNewPage( PSDRV_PDEVICE *physDev )
344 {
345     char *buf;
346     char name[100];
347     signed int xtrans, ytrans, rotation;
348
349     sprintf(name, "%d", physDev->job.PageNo);
350
351     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
352     if(!buf) {
353         WARN("HeapAlloc failed\n");
354         return 0;
355     }
356
357     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
358         if(physDev->pi->ppd->LandscapeOrientation == -90) {
359             xtrans = physDev->ImageableArea.right;
360             ytrans = physDev->ImageableArea.top;
361             rotation = 90;
362         } else {
363             xtrans = physDev->ImageableArea.left;
364             ytrans = physDev->ImageableArea.bottom;
365             rotation = -90;
366         }
367     } else {
368         xtrans = physDev->ImageableArea.left;
369         ytrans = physDev->ImageableArea.top;
370         rotation = 0;
371     }
372
373     sprintf(buf, psnewpage, name, physDev->job.PageNo,
374             physDev->logPixelsX, physDev->logPixelsY,
375             xtrans, ytrans, rotation);
376
377     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
378                                                      strlen(buf) ) {
379         WARN("WriteSpool error\n");
380         HeapFree( PSDRV_Heap, 0, buf );
381         return 0;
382     }
383     HeapFree( PSDRV_Heap, 0, buf );
384     return 1;
385 }
386
387
388 BOOL PSDRV_WriteMoveTo(PSDRV_PDEVICE *physDev, INT x, INT y)
389 {
390     char buf[100];
391
392     sprintf(buf, psmoveto, x, y);
393     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
394 }
395
396 BOOL PSDRV_WriteLineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
397 {
398     char buf[100];
399
400     sprintf(buf, pslineto, x, y);
401     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
402 }
403
404
405 BOOL PSDRV_WriteStroke(PSDRV_PDEVICE *physDev)
406 {
407     return PSDRV_WriteSpool(physDev, psstroke, sizeof(psstroke)-1);
408 }
409
410
411
412 BOOL PSDRV_WriteRectangle(PSDRV_PDEVICE *physDev, INT x, INT y, INT width,
413                         INT height)
414 {
415     char buf[100];
416
417     sprintf(buf, psrectangle, x, y, width, height, -width);
418     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
419 }
420
421 BOOL PSDRV_WriteRRectangle(PSDRV_PDEVICE *physDev, INT x, INT y, INT width,
422       INT height)
423 {
424     char buf[100];
425
426     sprintf(buf, psrrectangle, x, y, width, height, -width);
427     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
428 }
429
430 BOOL PSDRV_WriteArc(PSDRV_PDEVICE *physDev, INT x, INT y, INT w, INT h, double ang1,
431                       double ang2)
432 {
433     char buf[256];
434
435     /* Make angles -ve and swap order because we're working with an upside
436        down y-axis */
437     sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
438     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
439 }
440
441
442 BOOL PSDRV_WriteSetFont(PSDRV_PDEVICE *physDev, const char *name, INT size, INT escapement)
443 {
444     char *buf;
445
446     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(pssetfont) +
447                              strlen(name) + 40);
448
449     if(!buf) {
450         WARN("HeapAlloc failed\n");
451         return FALSE;
452     }
453
454     sprintf(buf, pssetfont, name, size, -size, -escapement);
455
456     PSDRV_WriteSpool(physDev, buf, strlen(buf));
457     HeapFree(PSDRV_Heap, 0, buf);
458     return TRUE;
459 }
460
461 BOOL PSDRV_WriteSetColor(PSDRV_PDEVICE *physDev, PSCOLOR *color)
462 {
463     char buf[256];
464
465     PSDRV_CopyColor(&physDev->inkColor, color);
466     switch(color->type) {
467     case PSCOLOR_RGB:
468         sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
469                 color->value.rgb.b);
470         return PSDRV_WriteSpool(physDev, buf, strlen(buf));
471
472     case PSCOLOR_GRAY:
473         sprintf(buf, pssetgray, color->value.gray.i);
474         return PSDRV_WriteSpool(physDev, buf, strlen(buf));
475
476     default:
477         ERR("Unkonwn colour type %d\n", color->type);
478         break;
479     }
480
481     return FALSE;
482 }
483
484 BOOL PSDRV_WriteSetPen(PSDRV_PDEVICE *physDev)
485 {
486     char buf[256];
487
488     sprintf(buf, pssetlinewidth, physDev->pen.width);
489     PSDRV_WriteSpool(physDev, buf, strlen(buf));
490
491     if(physDev->pen.dash) {
492         sprintf(buf, pssetdash, physDev->pen.dash, 0);
493         PSDRV_WriteSpool(physDev, buf, strlen(buf));
494     }
495
496     return TRUE;
497 }
498
499 BOOL PSDRV_WriteGlyphShow(PSDRV_PDEVICE *physDev, LPCSTR g_name)
500 {
501     char    buf[128];
502     int     l;
503
504     l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
505
506     if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
507         WARN("Unusable glyph name '%s' - ignoring\n", g_name);
508         return FALSE;
509     }
510
511     PSDRV_WriteSpool(physDev, buf, l);
512     return TRUE;
513 }
514
515 BOOL PSDRV_WriteFill(PSDRV_PDEVICE *physDev)
516 {
517     return PSDRV_WriteSpool(physDev, psfill, sizeof(psfill)-1);
518 }
519
520 BOOL PSDRV_WriteEOFill(PSDRV_PDEVICE *physDev)
521 {
522     return PSDRV_WriteSpool(physDev, pseofill, sizeof(pseofill)-1);
523 }
524
525 BOOL PSDRV_WriteGSave(PSDRV_PDEVICE *physDev)
526 {
527     return PSDRV_WriteSpool(physDev, psgsave, sizeof(psgsave)-1);
528 }
529
530 BOOL PSDRV_WriteGRestore(PSDRV_PDEVICE *physDev)
531 {
532     return PSDRV_WriteSpool(physDev, psgrestore, sizeof(psgrestore)-1);
533 }
534
535 BOOL PSDRV_WriteNewPath(PSDRV_PDEVICE *physDev)
536 {
537     return PSDRV_WriteSpool(physDev, psnewpath, sizeof(psnewpath)-1);
538 }
539
540 BOOL PSDRV_WriteClosePath(PSDRV_PDEVICE *physDev)
541 {
542     return PSDRV_WriteSpool(physDev, psclosepath, sizeof(psclosepath)-1);
543 }
544
545 BOOL PSDRV_WriteClip(PSDRV_PDEVICE *physDev)
546 {
547     return PSDRV_WriteSpool(physDev, psclip, sizeof(psclip)-1);
548 }
549
550 BOOL PSDRV_WriteEOClip(PSDRV_PDEVICE *physDev)
551 {
552     return PSDRV_WriteSpool(physDev, pseoclip, sizeof(pseoclip)-1);
553 }
554
555 BOOL PSDRV_WriteInitClip(PSDRV_PDEVICE *physDev)
556 {
557     return PSDRV_WriteSpool(physDev, psinitclip, sizeof(psinitclip)-1);
558 }
559
560 BOOL PSDRV_WriteHatch(PSDRV_PDEVICE *physDev)
561 {
562     return PSDRV_WriteSpool(physDev, pshatch, sizeof(pshatch)-1);
563 }
564
565 BOOL PSDRV_WriteRotate(PSDRV_PDEVICE *physDev, float ang)
566 {
567     char buf[256];
568
569     sprintf(buf, psrotate, ang);
570     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
571 }
572
573 BOOL PSDRV_WriteIndexColorSpaceBegin(PSDRV_PDEVICE *physDev, int size)
574 {
575     char buf[256];
576     sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
577     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
578 }
579
580 BOOL PSDRV_WriteIndexColorSpaceEnd(PSDRV_PDEVICE *physDev)
581 {
582     char buf[] = ">\n] setcolorspace\n";
583     return PSDRV_WriteSpool(physDev, buf, sizeof(buf) - 1);
584 }
585
586 BOOL PSDRV_WriteRGB(PSDRV_PDEVICE *physDev, COLORREF *map, int number)
587 {
588     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
589     int i;
590
591     ptr = buf;
592     for(i = 0; i < number; i++) {
593         sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
594                 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
595                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
596         ptr += 7;
597     }
598     PSDRV_WriteSpool(physDev, buf, number * 7);
599     HeapFree(PSDRV_Heap, 0, buf);
600     return TRUE;
601 }
602
603
604 BOOL PSDRV_WriteImageDict(PSDRV_PDEVICE *physDev, WORD depth, INT xDst, INT yDst,
605                           INT widthDst, INT heightDst, INT widthSrc,
606                           INT heightSrc, char *bits)
607 {
608     char start[] = "%d %d translate\n%d %d scale\n<<\n"
609       " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
610       " /ImageMatrix [%d 0 0 %d 0 %d]\n";
611
612     char decode1[] = " /Decode [0 %d]\n";
613     char decode3[] = " /Decode [0 1 0 1 0 1]\n";
614
615     char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
616     char endbits[] = " /DataSource <%s>\n>> image\n";
617
618     char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
619
620     sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
621             (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
622
623     PSDRV_WriteSpool(physDev, buf, strlen(buf));
624
625     switch(depth) {
626     case 8:
627         sprintf(buf, decode1, 255);
628         break;
629
630     case 4:
631         sprintf(buf, decode1, 15);
632         break;
633
634     case 1:
635         sprintf(buf, decode1, 1);
636         break;
637
638     default:
639         strcpy(buf, decode3);
640         break;
641     }
642
643     PSDRV_WriteSpool(physDev, buf, strlen(buf));
644
645     if(!bits)
646         PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
647     else {
648         sprintf(buf, endbits, bits);
649         PSDRV_WriteSpool(physDev, buf, strlen(buf));
650     }
651
652     HeapFree(PSDRV_Heap, 0, buf);
653     return TRUE;
654 }
655
656
657 BOOL PSDRV_WriteBytes(PSDRV_PDEVICE *physDev, const BYTE *bytes, int number)
658 {
659     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
660     char *ptr;
661     int i;
662
663     ptr = buf;
664
665     for(i = 0; i < number; i++) {
666         sprintf(ptr, "%02x%c", bytes[i],
667                 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
668         ptr += 3;
669     }
670     PSDRV_WriteSpool(physDev, buf, number * 3);
671
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%c", r, g, b,
696                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
697         ptr += 7;
698     }
699     PSDRV_WriteSpool(physDev, buf, number * 7);
700
701     HeapFree(PSDRV_Heap, 0, buf);
702     return TRUE;
703 }
704
705 BOOL PSDRV_WriteDIBits24(PSDRV_PDEVICE *physDev, const BYTE *bits, int number)
706 {
707     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
708     char *ptr;
709     int i;
710
711     ptr = buf;
712
713     for(i = 0; i < number; i++) {
714         sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
715                 bits[i * 3],
716                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
717         ptr += 7;
718     }
719     PSDRV_WriteSpool(physDev, buf, number * 7);
720
721     HeapFree(PSDRV_Heap, 0, buf);
722     return TRUE;
723 }
724
725 BOOL PSDRV_WriteDIBits32(PSDRV_PDEVICE *physDev, const BYTE *bits, int number)
726 {
727     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
728     char *ptr;
729     int i;
730
731     ptr = buf;
732
733     for(i = 0; i < number; i++) {
734         sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
735                 bits[i * 4],
736                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
737         ptr += 7;
738     }
739     PSDRV_WriteSpool(physDev, buf, number * 7);
740
741     HeapFree(PSDRV_Heap, 0, buf);
742     return TRUE;
743 }
744
745 BOOL PSDRV_WriteArrayGet(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nIndex)
746 {
747     char buf[100];
748
749     sprintf(buf, psarrayget, pszArrayName, nIndex);
750     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
751 }
752
753 BOOL PSDRV_WriteArrayPut(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nIndex, LONG lObject)
754 {
755     char buf[100];
756
757     sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
758     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
759 }
760
761 BOOL PSDRV_WriteArrayDef(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nSize)
762 {
763     char buf[100];
764
765     sprintf(buf, psarraydef, pszArrayName, nSize);
766     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
767 }
768
769 BOOL PSDRV_WriteRectClip(PSDRV_PDEVICE *physDev, INT x, INT y, INT w, INT h)
770 {
771     char buf[100];
772
773     sprintf(buf, psrectclip, x, y, w, h);
774     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
775 }
776
777 BOOL PSDRV_WriteRectClip2(PSDRV_PDEVICE *physDev, CHAR *pszArrayName)
778 {
779     char buf[100];
780
781     sprintf(buf, psrectclip2, pszArrayName);
782     return PSDRV_WriteSpool(physDev, buf, strlen(buf));
783 }
784
785 BOOL PSDRV_WritePatternDict(PSDRV_PDEVICE *physDev, BITMAP *bm, BYTE *bits)
786 {
787     char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
788       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n";
789
790     char end[] = "  end\n }\n>>\n matrix makepattern setpattern\n";
791     char *buf, *ptr;
792     INT w, h, x, y;
793     COLORREF map[2];
794
795     w = bm->bmWidth & ~0x7;
796     h = bm->bmHeight & ~0x7;
797
798     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
799     sprintf(buf, start, w, h, w, h);
800     PSDRV_WriteSpool(physDev,  buf, strlen(buf));
801     PSDRV_WriteIndexColorSpaceBegin(physDev, 1);
802     map[0] = GetTextColor( physDev->hdc );
803     map[1] = GetBkColor( physDev->hdc );
804     PSDRV_WriteRGB(physDev, map, 2);
805     PSDRV_WriteIndexColorSpaceEnd(physDev);
806     ptr = buf;
807     for(y = h-1; y >= 0; y--) {
808         for(x = 0; x < w/8; x++) {
809             sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
810             ptr += 2;
811         }
812     }
813     PSDRV_WriteImageDict(physDev, 1, 0, 0, 8, 8, 8, 8, buf);
814     PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
815     HeapFree(PSDRV_Heap, 0, buf);
816     return TRUE;
817 }
818
819 BOOL PSDRV_WriteDIBPatternDict(PSDRV_PDEVICE *physDev, BITMAPINFO *bmi, UINT usage)
820 {
821     char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
822       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n";
823
824     char end[] = "  end\n }\n>>\n matrix makepattern setpattern\n";
825     char *buf, *ptr;
826     BYTE *bits;
827     INT w, h, x, y, colours;
828     COLORREF map[2];
829
830     if(bmi->bmiHeader.biBitCount != 1) {
831         FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
832         return FALSE;
833     }
834
835     bits = (char*)bmi + bmi->bmiHeader.biSize;
836     colours = bmi->bmiHeader.biClrUsed;
837     if(!colours && bmi->bmiHeader.biBitCount <= 8)
838         colours = 1 << bmi->bmiHeader.biBitCount;
839     bits += colours * ((usage == DIB_RGB_COLORS) ?
840                        sizeof(RGBQUAD) : sizeof(WORD));
841
842     w = bmi->bmiHeader.biWidth & ~0x7;
843     h = bmi->bmiHeader.biHeight & ~0x7;
844
845     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
846     sprintf(buf, start, w, h, w, h);
847     PSDRV_WriteSpool(physDev,  buf, strlen(buf));
848     PSDRV_WriteIndexColorSpaceBegin(physDev, 1);
849     map[0] = physDev->dc->textColor;
850     map[1] = physDev->dc->backgroundColor;
851     PSDRV_WriteRGB(physDev, map, 2);
852     PSDRV_WriteIndexColorSpaceEnd(physDev);
853     ptr = buf;
854     for(y = h-1; y >= 0; y--) {
855         for(x = 0; x < w/8; x++) {
856             sprintf(ptr, "%02x", *(bits + x/8 + y *
857                                    (bmi->bmiHeader.biWidth + 31) / 32 * 4));
858             ptr += 2;
859         }
860     }
861     PSDRV_WriteImageDict(physDev, 1, 0, 0, 8, 8, 8, 8, buf);
862     PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
863     HeapFree(PSDRV_Heap, 0, buf);
864     return TRUE;
865 }