kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / wineps.drv / ps.c
blob4fd3c767b91835ff5f534517468a8b03be8158e5
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 #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"
36 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 static const char psadobe[] =
39 "%!PS-Adobe-3.0\n";
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] =
47 cups_one_sided, /* DMDUP_SIMPLEX */
48 cups_two_sided_long, /* DMDUP_VERTICAL */
49 cups_two_sided_short /* DMDUP_HORIZONTAL */
51 static const char cups_collate_false[] = "%cupsJobTicket: collate=false\n";
52 static const char cups_collate_true[] = "%cupsJobTicket: collate=true\n";
53 static const char cups_ap_d_inputslot[] = "%cupsJobTicket: AP_D_InputSlot=\n"; /* intentionally empty value */
55 static const char psheader[] = /* title llx lly urx ury orientation */
56 "%%%%Creator: Wine PostScript Driver\n"
57 "%%%%Title: %s\n"
58 "%%%%BoundingBox: %d %d %d %d\n"
59 "%%%%Pages: (atend)\n"
60 "%%%%Orientation: %s\n"
61 "%%%%EndComments\n";
63 static const char psbeginprolog[] =
64 "%%BeginProlog\n";
66 static const char psendprolog[] =
67 "%%EndProlog\n";
69 static const char psprolog[] =
70 "/tmpmtrx matrix def\n"
71 "/hatch {\n"
72 " pathbbox\n"
73 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
74 " l cvi gap idiv gap mul\n"
75 " gap\n"
76 " r cvi gap idiv gap mul\n"
77 " {t moveto 0 b t sub rlineto}\n"
78 " for\n"
79 "} bind def\n"
80 "/B {pop pop pop pop} def\n"
81 "/N {newpath} def\n"
82 "/havetype42gdir {version cvi 2015 ge} bind def\n";
84 static const char psbeginsetup[] =
85 "%%BeginSetup\n";
87 static const char psendsetup[] =
88 "%%EndSetup\n";
90 static const char psbeginfeature[] = /* feature, value */
91 "mark {\n"
92 "%%%%BeginFeature: %s %s\n";
94 static const char psendfeature[] =
95 "\n%%EndFeature\n"
96 "} stopped cleartomark\n";
98 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
99 "%%%%Page: %s %d\n"
100 "%%%%BeginPageSetup\n"
101 "/pgsave save def\n"
102 "72 %d div 72 %d div scale\n"
103 "%d %d translate\n"
104 "1 -1 scale\n"
105 "%d rotate\n"
106 "%%%%EndPageSetup\n";
108 static const char psendpage[] =
109 "pgsave restore\n"
110 "showpage\n";
112 static const char psfooter[] = /* pages */
113 "%%%%Trailer\n"
114 "%%%%Pages: %d\n"
115 "%%%%EOF\n";
117 static const char psmoveto[] = /* x, y */
118 "%d %d moveto\n";
120 static const char pslineto[] = /* x, y */
121 "%d %d lineto\n";
123 static const char psstroke[] =
124 "stroke\n";
126 static const char psrectangle[] = /* x, y, width, height, -width */
127 "%d %d moveto\n"
128 "%d 0 rlineto\n"
129 "0 %d rlineto\n"
130 "%d 0 rlineto\n"
131 "closepath\n";
133 static const char psglyphshow[] = /* glyph name */
134 "/%s glyphshow\n";
136 static const char psfindfont[] = /* fontname */
137 "/%s findfont\n";
139 static const char psfakeitalic[] =
140 "[1 0 0.25 1 0 0]\n";
142 static const char pssizematrix[] =
143 "[%d %d %d %d 0 0]\n";
145 static const char psconcat[] =
146 "matrix concatmatrix\n";
148 static const char psrotatefont[] = /* escapement */
149 "%d 10 div matrix rotate\n"
150 "matrix concatmatrix\n";
152 static const char pssetfont[] =
153 "makefont setfont\n";
155 static const char pssetline[] = /* width, join, endcap */
156 "%d setlinewidth %u setlinejoin %u setlinecap\n";
158 static const char pssetgray[] = /* gray */
159 "%.2f setgray\n";
161 static const char pssetrgbcolor[] = /* r, g, b */
162 "%.2f %.2f %.2f setrgbcolor\n";
164 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
165 "tmpmtrx currentmatrix pop\n"
166 "%d %d translate\n"
167 "%d %d scale\n"
168 "0 0 0.5 %.1f %.1f arc\n"
169 "tmpmtrx setmatrix\n";
171 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
172 "%d %d %d %d %d %d curveto\n";
174 static const char psgsave[] =
175 "gsave\n";
177 static const char psgrestore[] =
178 "grestore\n";
180 static const char psfill[] =
181 "fill\n";
183 static const char pseofill[] =
184 "eofill\n";
186 static const char psnewpath[] =
187 "newpath\n";
189 static const char psclosepath[] =
190 "closepath\n";
192 static const char psclip[] =
193 "clip\n";
195 static const char pseoclip[] =
196 "eoclip\n";
198 static const char psrectclip[] =
199 "%d %d %d %d rectclip\n";
201 static const char psrectclip2[] =
202 "%s rectclip\n";
204 static const char pshatch[] =
205 "hatch\n";
207 static const char psrotate[] = /* ang */
208 "%.1f rotate\n";
210 static const char psarrayput[] =
211 "%s %d %d put\n";
213 static const char psarraydef[] =
214 "/%s %d array def\n";
216 static const char psbegindocument[] =
217 "%%BeginDocument: Wine passthrough\n";
218 static const char psenddocument[] =
219 "\n%%EndDocument\n";
221 void passthrough_enter(PHYSDEV dev)
223 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
225 if (physDev->job.passthrough_state != passthrough_none) return;
227 write_spool(dev, psbegindocument, sizeof(psbegindocument) - 1);
228 physDev->job.passthrough_state = passthrough_active;
231 void passthrough_leave(PHYSDEV dev)
233 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
235 if (physDev->job.passthrough_state == passthrough_none) return;
237 write_spool(dev, psenddocument, sizeof(psenddocument) - 1);
238 physDev->job.passthrough_state = passthrough_none;
241 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
243 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
244 int num, num_left = cch;
246 if(physDev->job.quiet) {
247 TRACE("ignoring output\n");
248 return 0;
251 passthrough_leave(dev);
253 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
254 if( !PSDRV_StartPage(dev) )
255 return 0;
258 do {
259 num = min(num_left, 0x8000);
260 if(write_spool( dev, lpData, num ) != num)
261 return 0;
262 lpData += num;
263 num_left -= num;
264 } while(num_left);
266 return cch;
270 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
273 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
274 strlen(feature) + strlen(value));
276 sprintf(buf, psbeginfeature, feature, value);
277 write_spool( dev, buf, strlen(buf) );
278 write_spool( dev, invocation, strlen(invocation) );
279 write_spool( dev, psendfeature, strlen(psendfeature) );
281 HeapFree( GetProcessHeap(), 0, buf );
282 return 1;
285 /********************************************************
286 * escape_title
288 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
289 * as octal. If we've had to use an escape then surround the entire string
290 * in brackets. Truncate string to represent at most 0x80 characters.
293 static char *escape_title(LPCWSTR wstr)
295 char *ret, *cp, *str;
296 int i, extra = 0;
298 if(!wstr)
300 ret = HeapAlloc(GetProcessHeap(), 0, 1);
301 *ret = '\0';
302 return ret;
305 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
306 str = HeapAlloc( GetProcessHeap(), 0, i );
307 if (!str) return NULL;
308 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
310 for(i = 0; i < 0x80 && str[i]; i++)
312 if(!isprint(str[i]))
313 extra += 3;
316 if(!extra)
318 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
319 memcpy(ret, str, i);
320 ret[i] = '\0';
321 goto done;
324 extra += 2; /* two for the brackets */
325 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
326 *cp++ = '(';
327 for(i = 0; i < 0x80 && str[i]; i++)
329 if(!isprint(str[i]))
331 BYTE b = (BYTE)str[i];
332 *cp++ = '\\';
333 *cp++ = ((b >> 6) & 0x7) + '0';
334 *cp++ = ((b >> 3) & 0x7) + '0';
335 *cp++ = ((b) & 0x7) + '0';
337 else
338 *cp++ = str[i];
340 *cp++ = ')';
341 *cp = '\0';
343 done:
344 HeapFree( GetProcessHeap(), 0, str );
345 return ret;
348 struct ticket_info
350 PAGESIZE *page;
351 DUPLEX *duplex;
354 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
356 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
357 char buf[256];
358 int len;
360 if (info->page && info->page->InvocationString)
362 len = sizeof(media) + strlen( info->page->Name ) + 1;
363 if (len <= sizeof(buf))
365 memcpy( buf, media, sizeof(media) );
366 strcat( buf, info->page->Name );
367 strcat( buf, "\n");
368 write_spool( dev, buf, len - 1 );
370 else
371 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
374 if (info->duplex && info->duplex->InvocationString)
376 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
378 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
379 write_spool( dev, str, strlen( str ) );
383 if (physDev->Devmode->dmPublic.u1.s1.dmCopies > 1)
385 len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
386 physDev->Devmode->dmPublic.u1.s1.dmCopies );
387 if (len > 0 && len < sizeof(buf))
388 write_spool( dev, buf, len );
390 if (physDev->Devmode->dmPublic.dmFields & DM_COLLATE)
392 if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
393 write_spool( dev, cups_collate_false, sizeof(cups_collate_false) - 1 );
394 else if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
395 write_spool( dev, cups_collate_true, sizeof(cups_collate_true) - 1 );
399 if (!(physDev->Devmode->dmPublic.dmFields & DM_DEFAULTSOURCE) ||
400 physDev->Devmode->dmPublic.u1.s1.dmDefaultSource == DMBIN_AUTO)
401 write_spool( dev, cups_ap_d_inputslot, sizeof(cups_ap_d_inputslot) - 1 );
404 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
406 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
407 char *buf, *escaped_title;
408 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
409 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
410 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
411 int llx, lly, urx, ury;
412 int ret, len;
413 const char * dmOrientation;
415 struct ticket_info ticket_info = { page, duplex };
417 TRACE("%s\n", debugstr_w(title));
419 len = strlen( psadobe );
420 ret = write_spool( dev, psadobe, len );
421 if (ret != len)
423 WARN("WriteSpool error\n");
424 return 0;
427 write_cups_job_ticket( dev, &ticket_info );
429 escaped_title = escape_title(title);
430 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
431 strlen(escaped_title) + 30 );
432 if(!buf) {
433 WARN("HeapAlloc failed\n");
434 HeapFree(GetProcessHeap(), 0, escaped_title);
435 return 0;
438 /* BBox co-ords are in default user co-ord system so urx < ury even in
439 landscape mode */
440 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
441 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
442 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
443 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
444 /* FIXME should do something better with BBox */
446 dmOrientation = (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
447 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
449 HeapFree(GetProcessHeap(), 0, escaped_title);
451 len = strlen( buf );
452 write_spool( dev, buf, len );
453 HeapFree( GetProcessHeap(), 0, buf );
455 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
456 write_spool( dev, psprolog, strlen(psprolog) );
457 write_spool( dev, psendprolog, strlen(psendprolog) );
458 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
460 if (slot && slot->InvocationString)
461 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
463 if (page && page->InvocationString)
464 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
466 if (duplex && duplex->InvocationString)
467 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
469 write_spool( dev, psendsetup, strlen(psendsetup) );
472 return 1;
476 INT PSDRV_WriteFooter( PHYSDEV dev )
478 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
479 char *buf;
480 int ret = 1;
482 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
483 if(!buf) {
484 WARN("HeapAlloc failed\n");
485 return 0;
488 sprintf(buf, psfooter, physDev->job.PageNo);
490 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
491 WARN("WriteSpool error\n");
492 ret = 0;
494 HeapFree( GetProcessHeap(), 0, buf );
495 return ret;
500 INT PSDRV_WriteEndPage( PHYSDEV dev )
502 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
503 WARN("WriteSpool error\n");
504 return 0;
506 return 1;
512 INT PSDRV_WriteNewPage( PHYSDEV dev )
514 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
515 char *buf;
516 char name[100];
517 signed int xtrans, ytrans, rotation;
518 int ret = 1;
520 sprintf(name, "%d", physDev->job.PageNo);
522 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
523 if(!buf) {
524 WARN("HeapAlloc failed\n");
525 return 0;
528 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
529 if(physDev->pi->ppd->LandscapeOrientation == -90) {
530 xtrans = physDev->ImageableArea.right;
531 ytrans = physDev->ImageableArea.top;
532 rotation = 90;
533 } else {
534 xtrans = physDev->ImageableArea.left;
535 ytrans = physDev->ImageableArea.bottom;
536 rotation = -90;
538 } else {
539 xtrans = physDev->ImageableArea.left;
540 ytrans = physDev->ImageableArea.top;
541 rotation = 0;
544 sprintf(buf, psnewpage, name, physDev->job.PageNo,
545 physDev->logPixelsX, physDev->logPixelsY,
546 xtrans, ytrans, rotation);
548 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
549 WARN("WriteSpool error\n");
550 ret = 0;
552 HeapFree( GetProcessHeap(), 0, buf );
553 return ret;
557 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
559 char buf[100];
561 sprintf(buf, psmoveto, x, y);
562 return PSDRV_WriteSpool(dev, buf, strlen(buf));
565 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
567 char buf[100];
569 sprintf(buf, pslineto, x, y);
570 return PSDRV_WriteSpool(dev, buf, strlen(buf));
574 BOOL PSDRV_WriteStroke(PHYSDEV dev)
576 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
581 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
582 INT height)
584 char buf[100];
586 sprintf(buf, psrectangle, x, y, width, height, -width);
587 return PSDRV_WriteSpool(dev, buf, strlen(buf));
590 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
591 double ang2)
593 char buf[256];
595 /* Make angles -ve and swap order because we're working with an upside
596 down y-axis */
597 push_lc_numeric("C");
598 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
599 pop_lc_numeric();
600 return PSDRV_WriteSpool(dev, buf, strlen(buf));
603 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
605 char buf[256];
607 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
608 return PSDRV_WriteSpool(dev, buf, strlen(buf));
611 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement, BOOL fake_italic)
613 char *buf;
615 buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
617 if(!buf) {
618 WARN("HeapAlloc failed\n");
619 return FALSE;
622 sprintf( buf, psfindfont, name );
623 PSDRV_WriteSpool( dev, buf, strlen(buf) );
625 if (fake_italic) PSDRV_WriteSpool( dev, psfakeitalic, sizeof(psfakeitalic) - 1 );
627 sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
628 PSDRV_WriteSpool( dev, buf, strlen(buf) );
630 if (fake_italic) PSDRV_WriteSpool( dev, psconcat, sizeof(psconcat) - 1 );
632 if (escapement)
634 sprintf( buf, psrotatefont, -escapement );
635 PSDRV_WriteSpool( dev, buf, strlen(buf) );
638 PSDRV_WriteSpool( dev, pssetfont, sizeof(pssetfont) - 1 );
639 HeapFree( GetProcessHeap(), 0, buf );
641 return TRUE;
644 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
646 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
647 char buf[256];
649 PSDRV_CopyColor(&physDev->inkColor, color);
650 switch(color->type) {
651 case PSCOLOR_RGB:
652 push_lc_numeric("C");
653 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
654 color->value.rgb.b);
655 pop_lc_numeric();
656 return PSDRV_WriteSpool(dev, buf, strlen(buf));
658 case PSCOLOR_GRAY:
659 push_lc_numeric("C");
660 sprintf(buf, pssetgray, color->value.gray.i);
661 pop_lc_numeric();
662 return PSDRV_WriteSpool(dev, buf, strlen(buf));
664 default:
665 ERR("Unknown colour type %d\n", color->type);
666 break;
669 return FALSE;
672 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
674 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
675 char buf[256];
676 DWORD i, pos;
678 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
679 PSDRV_WriteSpool(dev, buf, strlen(buf));
681 if (physDev->pen.dash_len)
683 for (i = pos = 0; i < physDev->pen.dash_len; i++)
684 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
685 buf[0] = '[';
686 sprintf(buf + pos, "] %u setdash\n", 0);
688 else
689 sprintf(buf, "[] %u setdash\n", 0);
691 PSDRV_WriteSpool(dev, buf, strlen(buf));
693 return TRUE;
696 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
698 char buf[128];
699 int l;
701 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
703 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
704 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
705 return FALSE;
708 PSDRV_WriteSpool(dev, buf, l);
709 return TRUE;
712 BOOL PSDRV_WriteFill(PHYSDEV dev)
714 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
717 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
719 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
722 BOOL PSDRV_WriteGSave(PHYSDEV dev)
724 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
727 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
729 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
732 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
734 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
737 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
739 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
742 BOOL PSDRV_WriteClip(PHYSDEV dev)
744 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
747 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
749 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
752 BOOL PSDRV_WriteHatch(PHYSDEV dev)
754 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
757 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
759 char buf[256];
761 push_lc_numeric("C");
762 sprintf(buf, psrotate, ang);
763 pop_lc_numeric();
764 return PSDRV_WriteSpool(dev, buf, strlen(buf));
767 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
769 char buf[256];
770 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
771 return PSDRV_WriteSpool(dev, buf, strlen(buf));
774 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
776 static const char buf[] = ">\n] setcolorspace\n";
777 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
780 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
782 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
783 int i;
785 ptr = buf;
786 for(i = 0; i < number; i++) {
787 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
788 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
789 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
790 ptr += 7;
792 PSDRV_WriteSpool(dev, buf, number * 7);
793 HeapFree( GetProcessHeap(), 0, buf );
794 return TRUE;
797 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
799 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
800 int i;
802 ptr = buf;
803 for(i = 0; i < number; i++, rgb++)
804 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
805 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
807 PSDRV_WriteSpool(dev, buf, ptr - buf);
808 HeapFree( GetProcessHeap(), 0, buf );
809 return TRUE;
812 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth, BOOL grayscale,
813 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
815 static const char start[] = "<<\n"
816 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
817 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
819 static const char decode1[] = " /Decode [0 %d]\n";
820 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
822 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
823 static const char endbits[] = " /DataSource <%s>\n>>\n";
824 char buf[1000];
826 if (top_down)
827 sprintf(buf, start, widthSrc, heightSrc,
828 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
829 else
830 sprintf(buf, start, widthSrc, heightSrc,
831 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
833 PSDRV_WriteSpool(dev, buf, strlen(buf));
835 switch(depth) {
836 case 8:
837 sprintf(buf, decode1, 255);
838 break;
840 case 4:
841 sprintf(buf, decode1, 15);
842 break;
844 case 1:
845 sprintf(buf, decode1, 1);
846 break;
848 default:
849 if (grayscale)
850 sprintf(buf, decode1, 1);
851 else
852 strcpy(buf, decode3);
853 break;
856 PSDRV_WriteSpool(dev, buf, strlen(buf));
858 if(!bits) {
859 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
860 } else {
861 sprintf(buf, endbits, bits);
862 PSDRV_WriteSpool(dev, buf, strlen(buf));
865 return TRUE;
868 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, BOOL grayscale, INT xDst, INT yDst,
869 INT widthDst, INT heightDst, INT widthSrc,
870 INT heightSrc, BOOL mask, BOOL top_down)
872 static const char start[] = "%d %d translate\n%d %d scale\n";
873 static const char image[] = "image\n";
874 static const char imagemask[] = "imagemask\n";
875 char buf[100];
877 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
878 PSDRV_WriteSpool(dev, buf, strlen(buf));
879 PSDRV_WriteImageDict(dev, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
880 if(mask)
881 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
882 else
883 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
884 return TRUE;
888 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
890 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
891 char *ptr;
892 unsigned int i;
894 ptr = buf;
896 for(i = 0; i < number; i++) {
897 sprintf(ptr, "%02x", bytes[i]);
898 ptr += 2;
899 if(((i & 0xf) == 0xf) || (i == number - 1)) {
900 strcpy(ptr, "\n");
901 ptr++;
904 PSDRV_WriteSpool(dev, buf, ptr - buf);
905 HeapFree( GetProcessHeap(), 0, buf );
906 return TRUE;
909 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
911 int num, num_left = number;
913 do {
914 num = min(num_left, 60);
915 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
916 PSDRV_WriteSpool(dev, "\n", 1);
917 data += num;
918 num_left -= num;
919 } while(num_left);
921 return TRUE;
924 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
926 char buf[100];
928 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
929 return PSDRV_WriteSpool(dev, buf, strlen(buf));
932 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
934 char buf[100];
936 sprintf(buf, psarraydef, pszArrayName, nSize);
937 return PSDRV_WriteSpool(dev, buf, strlen(buf));
940 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
942 char buf[100];
944 sprintf(buf, psrectclip, x, y, w, h);
945 return PSDRV_WriteSpool(dev, buf, strlen(buf));
948 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
950 char buf[100];
952 sprintf(buf, psrectclip2, pszArrayName);
953 return PSDRV_WriteSpool(dev, buf, strlen(buf));
956 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
958 static const char mypat[] = "/mypat\n";
959 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
960 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
961 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
962 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
963 char *buf, *ptr;
964 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
965 COLORREF map[2];
967 TRACE( "size %dx%dx%d\n",
968 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
970 if(bmi->bmiHeader.biBitCount != 1) {
971 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
972 return FALSE;
975 w = bmi->bmiHeader.biWidth & ~0x7;
976 h = abs_height & ~0x7;
978 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
979 ptr = buf;
980 for(y = h-1; y >= 0; y--) {
981 for(x = 0; x < w/8; x++) {
982 sprintf(ptr, "%02x", *(bits + x/8 + y *
983 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
984 ptr += 2;
987 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
988 PSDRV_WriteImageDict(dev, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
989 PSDRV_WriteSpool(dev, "def\n", 4);
991 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
992 map[0] = GetTextColor( dev->hdc );
993 map[1] = GetBkColor( dev->hdc );
994 PSDRV_WriteRGB(dev, map, 2);
995 PSDRV_WriteIndexColorSpaceEnd(dev);
997 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
998 w_mult = (physDev->logPixelsX + 150) / 300;
999 h_mult = (physDev->logPixelsY + 150) / 300;
1000 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
1001 PSDRV_WriteSpool(dev, buf, strlen(buf));
1002 HeapFree( GetProcessHeap(), 0, buf );
1003 return TRUE;