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