d3d9/tests: Accept AMD GPU sysmem sample failure in test_mipmap_upload.
[wine.git] / dlls / wineps.drv / ps.c
blobb0b27e62ac28057f4325e4e96cc137a4319346c2
1 /*
2 * PostScript output functions
4 * Copyright 1998 Huw D M Davies
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.
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.
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
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <locale.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31 #include "wingdi.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 static const char psadobe[] =
38 "%!PS-Adobe-3.0\n";
40 static const char media[] = "%cupsJobTicket: media=";
41 static const char cups_one_sided[] = "%cupsJobTicket: sides=one-sided\n";
42 static const char cups_two_sided_long[] = "%cupsJobTicket: sides=two-sided-long-edge\n";
43 static const char cups_two_sided_short[] = "%cupsJobTicket: sides=two-sided-short-edge\n";
44 static const char *cups_duplexes[3] =
46 cups_one_sided, /* DMDUP_SIMPLEX */
47 cups_two_sided_long, /* DMDUP_VERTICAL */
48 cups_two_sided_short /* DMDUP_HORIZONTAL */
50 static const char cups_collate_false[] = "%cupsJobTicket: collate=false\n";
51 static const char cups_collate_true[] = "%cupsJobTicket: collate=true\n";
52 static const char cups_ap_d_inputslot[] = "%cupsJobTicket: AP_D_InputSlot=\n"; /* intentionally empty value */
54 static const char psheader[] = /* title llx lly urx ury orientation */
55 "%%%%Creator: Wine PostScript Driver\n"
56 "%%%%Title: %s\n"
57 "%%%%BoundingBox: %d %d %d %d\n"
58 "%%%%Pages: (atend)\n"
59 "%%%%Orientation: %s\n"
60 "%%%%EndComments\n";
62 static const char psbeginprolog[] =
63 "%%BeginProlog\n";
65 static const char psendprolog[] =
66 "%%EndProlog\n";
68 static const char psprolog[] =
69 "/tmpmtrx matrix def\n"
70 "/hatch {\n"
71 " pathbbox\n"
72 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
73 " l cvi gap idiv gap mul\n"
74 " gap\n"
75 " r cvi gap idiv gap mul\n"
76 " {t moveto 0 b t sub rlineto}\n"
77 " for\n"
78 "} bind def\n"
79 "/B {pop pop pop pop} def\n"
80 "/N {newpath} def\n"
81 "/havetype42gdir {version cvi 2015 ge} bind def\n";
83 static const char psbeginsetup[] =
84 "%%BeginSetup\n";
86 static const char psendsetup[] =
87 "%%EndSetup\n";
89 static const char psbeginfeature[] = /* feature, value */
90 "mark {\n"
91 "%%%%BeginFeature: %s %s\n";
93 static const char psendfeature[] =
94 "\n%%EndFeature\n"
95 "} stopped cleartomark\n";
97 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
98 "%%%%Page: %s %d\n"
99 "%%%%BeginPageSetup\n"
100 "/pgsave save def\n"
101 "72 %d div 72 %d div scale\n"
102 "%d %d translate\n"
103 "1 -1 scale\n"
104 "%d rotate\n"
105 "%%%%EndPageSetup\n";
107 static const char psendpage[] =
108 "pgsave restore\n"
109 "showpage\n";
111 static const char psfooter[] = /* pages */
112 "%%%%Trailer\n"
113 "%%%%Pages: %d\n"
114 "%%%%EOF\n";
116 static const char psmoveto[] = /* x, y */
117 "%d %d moveto\n";
119 static const char pslineto[] = /* x, y */
120 "%d %d lineto\n";
122 static const char psstroke[] =
123 "stroke\n";
125 static const char psrectangle[] = /* x, y, width, height, -width */
126 "%d %d moveto\n"
127 "%d 0 rlineto\n"
128 "0 %d rlineto\n"
129 "%d 0 rlineto\n"
130 "closepath\n";
132 static const char psglyphshow[] = /* glyph name */
133 "/%s glyphshow\n";
135 static const char psfindfont[] = /* fontname */
136 "/%s findfont\n";
138 static const char psfakeitalic[] =
139 "[1 0 0.25 1 0 0]\n";
141 static const char pssizematrix[] =
142 "[%d %d %d %d 0 0]\n";
144 static const char psconcat[] =
145 "matrix concatmatrix\n";
147 static const char psrotatefont[] = /* escapement */
148 "%d 10 div matrix rotate\n"
149 "matrix concatmatrix\n";
151 static const char pssetfont[] =
152 "makefont setfont\n";
154 static const char pssetline[] = /* width, join, endcap */
155 "%d setlinewidth %u setlinejoin %u setlinecap\n";
157 static const char pssetgray[] = /* gray */
158 "%.2f setgray\n";
160 static const char pssetrgbcolor[] = /* r, g, b */
161 "%.2f %.2f %.2f setrgbcolor\n";
163 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
164 "tmpmtrx currentmatrix pop\n"
165 "%d %d translate\n"
166 "%d %d scale\n"
167 "0 0 0.5 %.1f %.1f arc\n"
168 "tmpmtrx setmatrix\n";
170 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
171 "%ld %ld %ld %ld %ld %ld curveto\n";
173 static const char psgsave[] =
174 "gsave\n";
176 static const char psgrestore[] =
177 "grestore\n";
179 static const char psfill[] =
180 "fill\n";
182 static const char pseofill[] =
183 "eofill\n";
185 static const char psnewpath[] =
186 "newpath\n";
188 static const char psclosepath[] =
189 "closepath\n";
191 static const char psclip[] =
192 "clip\n";
194 static const char pseoclip[] =
195 "eoclip\n";
197 static const char psrectclip[] =
198 "%d %d %d %d rectclip\n";
200 static const char psrectclip2[] =
201 "%s rectclip\n";
203 static const char pshatch[] =
204 "hatch\n";
206 static const char psrotate[] = /* ang */
207 "%.1f rotate\n";
209 static const char psarrayput[] =
210 "%s %d %ld put\n";
212 static const char psarraydef[] =
213 "/%s %d array def\n";
215 static const char psbegindocument[] =
216 "%%BeginDocument: Wine passthrough\n";
217 static const char psenddocument[] =
218 "\n%%EndDocument\n";
220 void passthrough_enter(PHYSDEV dev)
222 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
224 if (physDev->job.passthrough_state != passthrough_none) return;
226 write_spool(dev, psbegindocument, sizeof(psbegindocument) - 1);
227 physDev->job.passthrough_state = passthrough_active;
230 void passthrough_leave(PHYSDEV dev)
232 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
234 if (physDev->job.passthrough_state == passthrough_none) return;
236 write_spool(dev, psenddocument, sizeof(psenddocument) - 1);
237 physDev->job.passthrough_state = passthrough_none;
240 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
242 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
243 int num, num_left = cch;
245 if(physDev->job.quiet) {
246 TRACE("ignoring output\n");
247 return 0;
250 passthrough_leave(dev);
252 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
253 if( !PSDRV_StartPage(dev) )
254 return 0;
257 do {
258 num = min(num_left, 0x8000);
259 if(write_spool( dev, lpData, num ) != num)
260 return 0;
261 lpData += num;
262 num_left -= num;
263 } while(num_left);
265 return cch;
269 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
272 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
273 strlen(feature) + strlen(value));
275 sprintf(buf, psbeginfeature, feature, value);
276 write_spool( dev, buf, strlen(buf) );
277 write_spool( dev, invocation, strlen(invocation) );
278 write_spool( dev, psendfeature, strlen(psendfeature) );
280 HeapFree( GetProcessHeap(), 0, buf );
281 return 1;
284 /********************************************************
285 * escape_title
287 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
288 * as octal. If we've had to use an escape then surround the entire string
289 * in brackets. Truncate string to represent at most 0x80 characters.
292 static char *escape_title(LPCWSTR wstr)
294 char *ret, *cp, *str;
295 int i, extra = 0;
297 if(!wstr)
299 ret = HeapAlloc(GetProcessHeap(), 0, 1);
300 *ret = '\0';
301 return ret;
304 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
305 str = HeapAlloc( GetProcessHeap(), 0, i );
306 if (!str) return NULL;
307 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
309 for(i = 0; i < 0x80 && str[i]; i++)
311 if(!isprint(str[i]))
312 extra += 3;
315 if(!extra)
317 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
318 memcpy(ret, str, i);
319 ret[i] = '\0';
320 goto done;
323 extra += 2; /* two for the brackets */
324 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
325 *cp++ = '(';
326 for(i = 0; i < 0x80 && str[i]; i++)
328 if(!isprint(str[i]))
330 BYTE b = (BYTE)str[i];
331 *cp++ = '\\';
332 *cp++ = ((b >> 6) & 0x7) + '0';
333 *cp++ = ((b >> 3) & 0x7) + '0';
334 *cp++ = ((b) & 0x7) + '0';
336 else
337 *cp++ = str[i];
339 *cp++ = ')';
340 *cp = '\0';
342 done:
343 HeapFree( GetProcessHeap(), 0, str );
344 return ret;
347 struct ticket_info
349 PAGESIZE *page;
350 DUPLEX *duplex;
353 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
355 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
356 char buf[256];
357 int len;
359 if (info->page && info->page->InvocationString)
361 len = sizeof(media) + strlen( info->page->Name ) + 1;
362 if (len <= sizeof(buf))
364 memcpy( buf, media, sizeof(media) );
365 strcat( buf, info->page->Name );
366 strcat( buf, "\n");
367 write_spool( dev, buf, len - 1 );
369 else
370 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
373 if (info->duplex && info->duplex->InvocationString)
375 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
377 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
378 write_spool( dev, str, strlen( str ) );
382 if (physDev->Devmode->dmPublic.dmCopies > 1)
384 len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
385 physDev->Devmode->dmPublic.dmCopies );
386 if (len > 0 && len < sizeof(buf))
387 write_spool( dev, buf, len );
389 if (physDev->Devmode->dmPublic.dmFields & DM_COLLATE)
391 if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
392 write_spool( dev, cups_collate_false, sizeof(cups_collate_false) - 1 );
393 else if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
394 write_spool( dev, cups_collate_true, sizeof(cups_collate_true) - 1 );
398 if (!(physDev->Devmode->dmPublic.dmFields & DM_DEFAULTSOURCE) ||
399 physDev->Devmode->dmPublic.dmDefaultSource == DMBIN_AUTO)
400 write_spool( dev, cups_ap_d_inputslot, sizeof(cups_ap_d_inputslot) - 1 );
403 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
405 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
406 char *buf, *escaped_title;
407 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
408 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
409 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
410 int llx, lly, urx, ury;
411 int ret, len;
412 const char * dmOrientation;
414 struct ticket_info ticket_info = { page, duplex };
416 TRACE("%s\n", debugstr_w(title));
418 len = strlen( psadobe );
419 ret = write_spool( dev, psadobe, len );
420 if (ret != len)
422 WARN("WriteSpool error\n");
423 return 0;
426 write_cups_job_ticket( dev, &ticket_info );
428 escaped_title = escape_title(title);
429 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
430 strlen(escaped_title) + 30 );
431 if(!buf) {
432 WARN("HeapAlloc failed\n");
433 HeapFree(GetProcessHeap(), 0, escaped_title);
434 return 0;
437 /* BBox co-ords are in default user co-ord system so urx < ury even in
438 landscape mode */
439 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
440 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
441 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
442 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
443 /* FIXME should do something better with BBox */
445 dmOrientation = (physDev->Devmode->dmPublic.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
446 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
448 HeapFree(GetProcessHeap(), 0, escaped_title);
450 len = strlen( buf );
451 write_spool( dev, buf, len );
452 HeapFree( GetProcessHeap(), 0, buf );
454 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
455 write_spool( dev, psprolog, strlen(psprolog) );
456 write_spool( dev, psendprolog, strlen(psendprolog) );
457 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
459 if (slot && slot->InvocationString)
460 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
462 if (page && page->InvocationString)
463 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
465 if (duplex && duplex->InvocationString)
466 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
468 write_spool( dev, psendsetup, strlen(psendsetup) );
471 return 1;
475 INT PSDRV_WriteFooter( PHYSDEV dev )
477 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
478 char *buf;
479 int ret = 1;
481 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
482 if(!buf) {
483 WARN("HeapAlloc failed\n");
484 return 0;
487 sprintf(buf, psfooter, physDev->job.PageNo);
489 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
490 WARN("WriteSpool error\n");
491 ret = 0;
493 HeapFree( GetProcessHeap(), 0, buf );
494 return ret;
499 INT PSDRV_WriteEndPage( PHYSDEV dev )
501 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
502 WARN("WriteSpool error\n");
503 return 0;
505 return 1;
511 INT PSDRV_WriteNewPage( PHYSDEV dev )
513 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
514 char *buf;
515 char name[100];
516 signed int xtrans, ytrans, rotation;
517 int ret = 1;
519 sprintf(name, "%d", physDev->job.PageNo);
521 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
522 if(!buf) {
523 WARN("HeapAlloc failed\n");
524 return 0;
527 if(physDev->Devmode->dmPublic.dmOrientation == DMORIENT_LANDSCAPE) {
528 if(physDev->pi->ppd->LandscapeOrientation == -90) {
529 xtrans = physDev->ImageableArea.right;
530 ytrans = physDev->ImageableArea.top;
531 rotation = 90;
532 } else {
533 xtrans = physDev->ImageableArea.left;
534 ytrans = physDev->ImageableArea.bottom;
535 rotation = -90;
537 } else {
538 xtrans = physDev->ImageableArea.left;
539 ytrans = physDev->ImageableArea.top;
540 rotation = 0;
543 sprintf(buf, psnewpage, name, physDev->job.PageNo,
544 physDev->logPixelsX, physDev->logPixelsY,
545 xtrans, ytrans, rotation);
547 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
548 WARN("WriteSpool error\n");
549 ret = 0;
551 HeapFree( GetProcessHeap(), 0, buf );
552 return ret;
556 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
558 char buf[100];
560 sprintf(buf, psmoveto, x, y);
561 return PSDRV_WriteSpool(dev, buf, strlen(buf));
564 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
566 char buf[100];
568 sprintf(buf, pslineto, x, y);
569 return PSDRV_WriteSpool(dev, buf, strlen(buf));
573 BOOL PSDRV_WriteStroke(PHYSDEV dev)
575 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
580 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
581 INT height)
583 char buf[100];
585 sprintf(buf, psrectangle, x, y, width, height, -width);
586 return PSDRV_WriteSpool(dev, buf, strlen(buf));
589 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
590 double ang2)
592 char buf[256];
594 /* Make angles -ve and swap order because we're working with an upside
595 down y-axis */
596 push_lc_numeric("C");
597 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
598 pop_lc_numeric();
599 return PSDRV_WriteSpool(dev, buf, strlen(buf));
602 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
604 char buf[256];
606 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
607 return PSDRV_WriteSpool(dev, buf, strlen(buf));
610 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement, BOOL fake_italic)
612 char *buf;
614 buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
616 if(!buf) {
617 WARN("HeapAlloc failed\n");
618 return FALSE;
621 sprintf( buf, psfindfont, name );
622 PSDRV_WriteSpool( dev, buf, strlen(buf) );
624 if (fake_italic) PSDRV_WriteSpool( dev, psfakeitalic, sizeof(psfakeitalic) - 1 );
626 sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
627 PSDRV_WriteSpool( dev, buf, strlen(buf) );
629 if (fake_italic) PSDRV_WriteSpool( dev, psconcat, sizeof(psconcat) - 1 );
631 if (escapement)
633 sprintf( buf, psrotatefont, -escapement );
634 PSDRV_WriteSpool( dev, buf, strlen(buf) );
637 PSDRV_WriteSpool( dev, pssetfont, sizeof(pssetfont) - 1 );
638 HeapFree( GetProcessHeap(), 0, buf );
640 return TRUE;
643 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
645 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
646 char buf[256];
648 PSDRV_CopyColor(&physDev->inkColor, color);
649 switch(color->type) {
650 case PSCOLOR_RGB:
651 push_lc_numeric("C");
652 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
653 color->value.rgb.b);
654 pop_lc_numeric();
655 return PSDRV_WriteSpool(dev, buf, strlen(buf));
657 case PSCOLOR_GRAY:
658 push_lc_numeric("C");
659 sprintf(buf, pssetgray, color->value.gray.i);
660 pop_lc_numeric();
661 return PSDRV_WriteSpool(dev, buf, strlen(buf));
663 default:
664 ERR("Unknown colour type %d\n", color->type);
665 break;
668 return FALSE;
671 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
673 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
674 char buf[256];
675 DWORD i, pos;
677 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
678 PSDRV_WriteSpool(dev, buf, strlen(buf));
680 if (physDev->pen.dash_len)
682 for (i = pos = 0; i < physDev->pen.dash_len; i++)
683 pos += sprintf( buf + pos, " %lu", physDev->pen.dash[i] );
684 buf[0] = '[';
685 sprintf(buf + pos, "] %u setdash\n", 0);
687 else
688 sprintf(buf, "[] %u setdash\n", 0);
690 PSDRV_WriteSpool(dev, buf, strlen(buf));
692 return TRUE;
695 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
697 char buf[128];
698 int l;
700 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
702 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
703 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
704 return FALSE;
707 PSDRV_WriteSpool(dev, buf, l);
708 return TRUE;
711 BOOL PSDRV_WriteFill(PHYSDEV dev)
713 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
716 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
718 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
721 BOOL PSDRV_WriteGSave(PHYSDEV dev)
723 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
726 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
728 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
731 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
733 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
736 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
738 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
741 BOOL PSDRV_WriteClip(PHYSDEV dev)
743 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
746 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
748 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
751 BOOL PSDRV_WriteHatch(PHYSDEV dev)
753 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
756 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
758 char buf[256];
760 push_lc_numeric("C");
761 sprintf(buf, psrotate, ang);
762 pop_lc_numeric();
763 return PSDRV_WriteSpool(dev, buf, strlen(buf));
766 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
768 char buf[256];
769 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
770 return PSDRV_WriteSpool(dev, buf, strlen(buf));
773 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
775 static const char buf[] = ">\n] setcolorspace\n";
776 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
779 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
781 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
782 int i;
784 ptr = buf;
785 for(i = 0; i < number; i++) {
786 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
787 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
788 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
789 ptr += 7;
791 PSDRV_WriteSpool(dev, buf, number * 7);
792 HeapFree( GetProcessHeap(), 0, buf );
793 return TRUE;
796 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
798 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
799 int i;
801 ptr = buf;
802 for(i = 0; i < number; i++, rgb++)
803 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
804 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
806 PSDRV_WriteSpool(dev, buf, ptr - buf);
807 HeapFree( GetProcessHeap(), 0, buf );
808 return TRUE;
811 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth, BOOL grayscale,
812 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
814 static const char start[] = "<<\n"
815 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
816 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
818 static const char decode1[] = " /Decode [0 %d]\n";
819 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
821 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
822 static const char endbits[] = " /DataSource <%s>\n>>\n";
823 char buf[1000];
825 if (top_down)
826 sprintf(buf, start, widthSrc, heightSrc,
827 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
828 else
829 sprintf(buf, start, widthSrc, heightSrc,
830 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
832 PSDRV_WriteSpool(dev, buf, strlen(buf));
834 switch(depth) {
835 case 8:
836 sprintf(buf, decode1, 255);
837 break;
839 case 4:
840 sprintf(buf, decode1, 15);
841 break;
843 case 1:
844 sprintf(buf, decode1, 1);
845 break;
847 default:
848 if (grayscale)
849 sprintf(buf, decode1, 1);
850 else
851 strcpy(buf, decode3);
852 break;
855 PSDRV_WriteSpool(dev, buf, strlen(buf));
857 if(!bits) {
858 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
859 } else {
860 sprintf(buf, endbits, bits);
861 PSDRV_WriteSpool(dev, buf, strlen(buf));
864 return TRUE;
867 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, BOOL grayscale, INT xDst, INT yDst,
868 INT widthDst, INT heightDst, INT widthSrc,
869 INT heightSrc, BOOL mask, BOOL top_down)
871 static const char start[] = "%d %d translate\n%d %d scale\n";
872 static const char image[] = "image\n";
873 static const char imagemask[] = "imagemask\n";
874 char buf[100];
876 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
877 PSDRV_WriteSpool(dev, buf, strlen(buf));
878 PSDRV_WriteImageDict(dev, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
879 if(mask)
880 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
881 else
882 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
883 return TRUE;
887 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
889 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
890 char *ptr;
891 unsigned int i;
893 ptr = buf;
895 for(i = 0; i < number; i++) {
896 sprintf(ptr, "%02x", bytes[i]);
897 ptr += 2;
898 if(((i & 0xf) == 0xf) || (i == number - 1)) {
899 strcpy(ptr, "\n");
900 ptr++;
903 PSDRV_WriteSpool(dev, buf, ptr - buf);
904 HeapFree( GetProcessHeap(), 0, buf );
905 return TRUE;
908 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
910 int num, num_left = number;
912 do {
913 num = min(num_left, 60);
914 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
915 PSDRV_WriteSpool(dev, "\n", 1);
916 data += num;
917 num_left -= num;
918 } while(num_left);
920 return TRUE;
923 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
925 char buf[100];
927 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
928 return PSDRV_WriteSpool(dev, buf, strlen(buf));
931 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
933 char buf[100];
935 sprintf(buf, psarraydef, pszArrayName, nSize);
936 return PSDRV_WriteSpool(dev, buf, strlen(buf));
939 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
941 char buf[100];
943 sprintf(buf, psrectclip, x, y, w, h);
944 return PSDRV_WriteSpool(dev, buf, strlen(buf));
947 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
949 char buf[100];
951 sprintf(buf, psrectclip2, pszArrayName);
952 return PSDRV_WriteSpool(dev, buf, strlen(buf));
955 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
957 static const char mypat[] = "/mypat\n";
958 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
959 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
960 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
961 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
962 char *buf, *ptr;
963 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
964 COLORREF map[2];
966 TRACE( "size %ldx%ldx%d\n",
967 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
969 if(bmi->bmiHeader.biBitCount != 1) {
970 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
971 return FALSE;
974 w = bmi->bmiHeader.biWidth & ~0x7;
975 h = abs_height & ~0x7;
977 buf = HeapAlloc( GetProcessHeap(), 0, max(sizeof(do_pattern) + 100, 2 * w/8 * h + 1) );
978 ptr = buf;
979 for(y = h-1; y >= 0; y--) {
980 for(x = 0; x < w/8; x++) {
981 sprintf(ptr, "%02x", *(bits + x/8 + y *
982 ((bmi->bmiHeader.biWidth + 31) / 32) * 4));
983 ptr += 2;
986 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
987 PSDRV_WriteImageDict(dev, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
988 PSDRV_WriteSpool(dev, "def\n", 4);
990 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
991 map[0] = GetTextColor( dev->hdc );
992 map[1] = GetBkColor( dev->hdc );
993 PSDRV_WriteRGB(dev, map, 2);
994 PSDRV_WriteIndexColorSpaceEnd(dev);
996 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
997 w_mult = (physDev->logPixelsX + 150) / 300;
998 h_mult = (physDev->logPixelsY + 150) / 300;
999 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
1000 PSDRV_WriteSpool(dev, buf, strlen(buf));
1001 HeapFree( GetProcessHeap(), 0, buf );
1002 return TRUE;