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