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