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