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