ntoskrnl.exe/tests: Add some IOCTL_HID_WRITE_REPORT tests.
[wine.git] / dlls / wineps.drv / ps.c
blob60737d67bda44b3524c9a8d2279afbeff57bfc56
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 "winnls.h"
33 #include "wingdi.h"
34 #include "psdrv.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
39 static const char psadobe[] =
40 "%!PS-Adobe-3.0\n";
42 static const char media[] = "%cupsJobTicket: media=";
43 static const char cups_one_sided[] = "%cupsJobTicket: sides=one-sided\n";
44 static const char cups_two_sided_long[] = "%cupsJobTicket: sides=two-sided-long-edge\n";
45 static const char cups_two_sided_short[] = "%cupsJobTicket: sides=two-sided-short-edge\n";
46 static const char *cups_duplexes[3] =
48 cups_one_sided, /* DMDUP_SIMPLEX */
49 cups_two_sided_long, /* DMDUP_VERTICAL */
50 cups_two_sided_short /* DMDUP_HORIZONTAL */
52 static const char cups_collate_false[] = "%cupsJobTicket: collate=false\n";
53 static const char cups_collate_true[] = "%cupsJobTicket: collate=true\n";
54 static const char cups_ap_d_inputslot[] = "%cupsJobTicket: AP_D_InputSlot=\n"; /* intentionally empty value */
56 static const char psheader[] = /* title llx lly urx ury orientation */
57 "%%%%Creator: Wine PostScript Driver\n"
58 "%%%%Title: %s\n"
59 "%%%%BoundingBox: %d %d %d %d\n"
60 "%%%%Pages: (atend)\n"
61 "%%%%Orientation: %s\n"
62 "%%%%EndComments\n";
64 static const char psbeginprolog[] =
65 "%%BeginProlog\n";
67 static const char psendprolog[] =
68 "%%EndProlog\n";
70 static const char psprolog[] =
71 "/tmpmtrx matrix def\n"
72 "/hatch {\n"
73 " pathbbox\n"
74 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
75 " l cvi gap idiv gap mul\n"
76 " gap\n"
77 " r cvi gap idiv gap mul\n"
78 " {t moveto 0 b t sub rlineto}\n"
79 " for\n"
80 "} bind def\n"
81 "/B {pop pop pop pop} def\n"
82 "/N {newpath} def\n"
83 "/havetype42gdir {version cvi 2015 ge} bind def\n";
85 static const char psbeginsetup[] =
86 "%%BeginSetup\n";
88 static const char psendsetup[] =
89 "%%EndSetup\n";
91 static const char psbeginfeature[] = /* feature, value */
92 "mark {\n"
93 "%%%%BeginFeature: %s %s\n";
95 static const char psendfeature[] =
96 "\n%%EndFeature\n"
97 "} stopped cleartomark\n";
99 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
100 "%%%%Page: %s %d\n"
101 "%%%%BeginPageSetup\n"
102 "/pgsave save def\n"
103 "72 %d div 72 %d div scale\n"
104 "%d %d translate\n"
105 "1 -1 scale\n"
106 "%d rotate\n"
107 "%%%%EndPageSetup\n";
109 static const char psendpage[] =
110 "pgsave restore\n"
111 "showpage\n";
113 static const char psfooter[] = /* pages */
114 "%%%%Trailer\n"
115 "%%%%Pages: %d\n"
116 "%%%%EOF\n";
118 static const char psmoveto[] = /* x, y */
119 "%d %d moveto\n";
121 static const char pslineto[] = /* x, y */
122 "%d %d lineto\n";
124 static const char psstroke[] =
125 "stroke\n";
127 static const char psrectangle[] = /* x, y, width, height, -width */
128 "%d %d moveto\n"
129 "%d 0 rlineto\n"
130 "0 %d rlineto\n"
131 "%d 0 rlineto\n"
132 "closepath\n";
134 static const char psglyphshow[] = /* glyph name */
135 "/%s glyphshow\n";
137 static const char psfindfont[] = /* fontname */
138 "/%s findfont\n";
140 static const char psfakeitalic[] =
141 "[1 0 0.25 1 0 0]\n";
143 static const char pssizematrix[] =
144 "[%d %d %d %d 0 0]\n";
146 static const char psconcat[] =
147 "matrix concatmatrix\n";
149 static const char psrotatefont[] = /* escapement */
150 "%d 10 div matrix rotate\n"
151 "matrix concatmatrix\n";
153 static const char pssetfont[] =
154 "makefont setfont\n";
156 static const char pssetline[] = /* width, join, endcap */
157 "%d setlinewidth %u setlinejoin %u setlinecap\n";
159 static const char pssetgray[] = /* gray */
160 "%.2f setgray\n";
162 static const char pssetrgbcolor[] = /* r, g, b */
163 "%.2f %.2f %.2f setrgbcolor\n";
165 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
166 "tmpmtrx currentmatrix pop\n"
167 "%d %d translate\n"
168 "%d %d scale\n"
169 "0 0 0.5 %.1f %.1f arc\n"
170 "tmpmtrx setmatrix\n";
172 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
173 "%d %d %d %d %d %d curveto\n";
175 static const char psgsave[] =
176 "gsave\n";
178 static const char psgrestore[] =
179 "grestore\n";
181 static const char psfill[] =
182 "fill\n";
184 static const char pseofill[] =
185 "eofill\n";
187 static const char psnewpath[] =
188 "newpath\n";
190 static const char psclosepath[] =
191 "closepath\n";
193 static const char psclip[] =
194 "clip\n";
196 static const char pseoclip[] =
197 "eoclip\n";
199 static const char psrectclip[] =
200 "%d %d %d %d rectclip\n";
202 static const char psrectclip2[] =
203 "%s rectclip\n";
205 static const char pshatch[] =
206 "hatch\n";
208 static const char psrotate[] = /* ang */
209 "%.1f rotate\n";
211 static const char psarrayput[] =
212 "%s %d %d put\n";
214 static const char psarraydef[] =
215 "/%s %d array def\n";
217 static const char psbegindocument[] =
218 "%%BeginDocument: Wine passthrough\n";
219 static const char psenddocument[] =
220 "\n%%EndDocument\n";
222 void passthrough_enter(PHYSDEV dev)
224 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
226 if (physDev->job.passthrough_state != passthrough_none) return;
228 write_spool(dev, psbegindocument, sizeof(psbegindocument) - 1);
229 physDev->job.passthrough_state = passthrough_active;
232 void passthrough_leave(PHYSDEV dev)
234 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
236 if (physDev->job.passthrough_state == passthrough_none) return;
238 write_spool(dev, psenddocument, sizeof(psenddocument) - 1);
239 physDev->job.passthrough_state = passthrough_none;
242 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
244 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
245 int num, num_left = cch;
247 if(physDev->job.quiet) {
248 TRACE("ignoring output\n");
249 return 0;
252 passthrough_leave(dev);
254 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
255 if( !PSDRV_StartPage(dev) )
256 return 0;
259 do {
260 num = min(num_left, 0x8000);
261 if(write_spool( dev, lpData, num ) != num)
262 return 0;
263 lpData += num;
264 num_left -= num;
265 } while(num_left);
267 return cch;
271 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
274 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
275 strlen(feature) + strlen(value));
277 sprintf(buf, psbeginfeature, feature, value);
278 write_spool( dev, buf, strlen(buf) );
279 write_spool( dev, invocation, strlen(invocation) );
280 write_spool( dev, psendfeature, strlen(psendfeature) );
282 HeapFree( GetProcessHeap(), 0, buf );
283 return 1;
286 /********************************************************
287 * escape_title
289 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
290 * as octal. If we've had to use an escape then surround the entire string
291 * in brackets. Truncate string to represent at most 0x80 characters.
294 static char *escape_title(LPCWSTR wstr)
296 char *ret, *cp, *str;
297 int i, extra = 0;
299 if(!wstr)
301 ret = HeapAlloc(GetProcessHeap(), 0, 1);
302 *ret = '\0';
303 return ret;
306 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
307 str = HeapAlloc( GetProcessHeap(), 0, i );
308 if (!str) return NULL;
309 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
311 for(i = 0; i < 0x80 && str[i]; i++)
313 if(!isprint(str[i]))
314 extra += 3;
317 if(!extra)
319 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
320 memcpy(ret, str, i);
321 ret[i] = '\0';
322 goto done;
325 extra += 2; /* two for the brackets */
326 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
327 *cp++ = '(';
328 for(i = 0; i < 0x80 && str[i]; i++)
330 if(!isprint(str[i]))
332 BYTE b = (BYTE)str[i];
333 *cp++ = '\\';
334 *cp++ = ((b >> 6) & 0x7) + '0';
335 *cp++ = ((b >> 3) & 0x7) + '0';
336 *cp++ = ((b) & 0x7) + '0';
338 else
339 *cp++ = str[i];
341 *cp++ = ')';
342 *cp = '\0';
344 done:
345 HeapFree( GetProcessHeap(), 0, str );
346 return ret;
349 struct ticket_info
351 PAGESIZE *page;
352 DUPLEX *duplex;
355 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
357 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
358 char buf[256];
359 int len;
361 if (info->page && info->page->InvocationString)
363 len = sizeof(media) + strlen( info->page->Name ) + 1;
364 if (len <= sizeof(buf))
366 memcpy( buf, media, sizeof(media) );
367 strcat( buf, info->page->Name );
368 strcat( buf, "\n");
369 write_spool( dev, buf, len - 1 );
371 else
372 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
375 if (info->duplex && info->duplex->InvocationString)
377 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
379 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
380 write_spool( dev, str, strlen( str ) );
384 if (physDev->Devmode->dmPublic.u1.s1.dmCopies > 1)
386 len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
387 physDev->Devmode->dmPublic.u1.s1.dmCopies );
388 if (len > 0 && len < sizeof(buf))
389 write_spool( dev, buf, len );
391 if (physDev->Devmode->dmPublic.dmFields & DM_COLLATE)
393 if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
394 write_spool( dev, cups_collate_false, sizeof(cups_collate_false) - 1 );
395 else if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
396 write_spool( dev, cups_collate_true, sizeof(cups_collate_true) - 1 );
400 if (!(physDev->Devmode->dmPublic.dmFields & DM_DEFAULTSOURCE) ||
401 physDev->Devmode->dmPublic.u1.s1.dmDefaultSource == DMBIN_AUTO)
402 write_spool( dev, cups_ap_d_inputslot, sizeof(cups_ap_d_inputslot) - 1 );
405 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
407 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
408 char *buf, *escaped_title;
409 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
410 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
411 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
412 int llx, lly, urx, ury;
413 int ret, len;
414 const char * dmOrientation;
416 struct ticket_info ticket_info = { page, duplex };
418 TRACE("%s\n", debugstr_w(title));
420 len = strlen( psadobe );
421 ret = write_spool( dev, psadobe, len );
422 if (ret != len)
424 WARN("WriteSpool error\n");
425 return 0;
428 write_cups_job_ticket( dev, &ticket_info );
430 escaped_title = escape_title(title);
431 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
432 strlen(escaped_title) + 30 );
433 if(!buf) {
434 WARN("HeapAlloc failed\n");
435 HeapFree(GetProcessHeap(), 0, escaped_title);
436 return 0;
439 /* BBox co-ords are in default user co-ord system so urx < ury even in
440 landscape mode */
441 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
442 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
443 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
444 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
445 /* FIXME should do something better with BBox */
447 dmOrientation = (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
448 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
450 HeapFree(GetProcessHeap(), 0, escaped_title);
452 len = strlen( buf );
453 write_spool( dev, buf, len );
454 HeapFree( GetProcessHeap(), 0, buf );
456 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
457 write_spool( dev, psprolog, strlen(psprolog) );
458 write_spool( dev, psendprolog, strlen(psendprolog) );
459 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
461 if (slot && slot->InvocationString)
462 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
464 if (page && page->InvocationString)
465 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
467 if (duplex && duplex->InvocationString)
468 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
470 write_spool( dev, psendsetup, strlen(psendsetup) );
473 return 1;
477 INT PSDRV_WriteFooter( PHYSDEV dev )
479 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
480 char *buf;
481 int ret = 1;
483 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
484 if(!buf) {
485 WARN("HeapAlloc failed\n");
486 return 0;
489 sprintf(buf, psfooter, physDev->job.PageNo);
491 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
492 WARN("WriteSpool error\n");
493 ret = 0;
495 HeapFree( GetProcessHeap(), 0, buf );
496 return ret;
501 INT PSDRV_WriteEndPage( PHYSDEV dev )
503 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
504 WARN("WriteSpool error\n");
505 return 0;
507 return 1;
513 INT PSDRV_WriteNewPage( PHYSDEV dev )
515 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
516 char *buf;
517 char name[100];
518 signed int xtrans, ytrans, rotation;
519 int ret = 1;
521 sprintf(name, "%d", physDev->job.PageNo);
523 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
524 if(!buf) {
525 WARN("HeapAlloc failed\n");
526 return 0;
529 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
530 if(physDev->pi->ppd->LandscapeOrientation == -90) {
531 xtrans = physDev->ImageableArea.right;
532 ytrans = physDev->ImageableArea.top;
533 rotation = 90;
534 } else {
535 xtrans = physDev->ImageableArea.left;
536 ytrans = physDev->ImageableArea.bottom;
537 rotation = -90;
539 } else {
540 xtrans = physDev->ImageableArea.left;
541 ytrans = physDev->ImageableArea.top;
542 rotation = 0;
545 sprintf(buf, psnewpage, name, physDev->job.PageNo,
546 physDev->logPixelsX, physDev->logPixelsY,
547 xtrans, ytrans, rotation);
549 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
550 WARN("WriteSpool error\n");
551 ret = 0;
553 HeapFree( GetProcessHeap(), 0, buf );
554 return ret;
558 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
560 char buf[100];
562 sprintf(buf, psmoveto, x, y);
563 return PSDRV_WriteSpool(dev, buf, strlen(buf));
566 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
568 char buf[100];
570 sprintf(buf, pslineto, x, y);
571 return PSDRV_WriteSpool(dev, buf, strlen(buf));
575 BOOL PSDRV_WriteStroke(PHYSDEV dev)
577 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
582 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
583 INT height)
585 char buf[100];
587 sprintf(buf, psrectangle, x, y, width, height, -width);
588 return PSDRV_WriteSpool(dev, buf, strlen(buf));
591 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
592 double ang2)
594 char buf[256];
596 /* Make angles -ve and swap order because we're working with an upside
597 down y-axis */
598 push_lc_numeric("C");
599 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
600 pop_lc_numeric();
601 return PSDRV_WriteSpool(dev, buf, strlen(buf));
604 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
606 char buf[256];
608 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
609 return PSDRV_WriteSpool(dev, buf, strlen(buf));
612 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement, BOOL fake_italic)
614 char *buf;
616 buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
618 if(!buf) {
619 WARN("HeapAlloc failed\n");
620 return FALSE;
623 sprintf( buf, psfindfont, name );
624 PSDRV_WriteSpool( dev, buf, strlen(buf) );
626 if (fake_italic) PSDRV_WriteSpool( dev, psfakeitalic, sizeof(psfakeitalic) - 1 );
628 sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
629 PSDRV_WriteSpool( dev, buf, strlen(buf) );
631 if (fake_italic) PSDRV_WriteSpool( dev, psconcat, sizeof(psconcat) - 1 );
633 if (escapement)
635 sprintf( buf, psrotatefont, -escapement );
636 PSDRV_WriteSpool( dev, buf, strlen(buf) );
639 PSDRV_WriteSpool( dev, pssetfont, sizeof(pssetfont) - 1 );
640 HeapFree( GetProcessHeap(), 0, buf );
642 return TRUE;
645 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
647 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
648 char buf[256];
650 PSDRV_CopyColor(&physDev->inkColor, color);
651 switch(color->type) {
652 case PSCOLOR_RGB:
653 push_lc_numeric("C");
654 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
655 color->value.rgb.b);
656 pop_lc_numeric();
657 return PSDRV_WriteSpool(dev, buf, strlen(buf));
659 case PSCOLOR_GRAY:
660 push_lc_numeric("C");
661 sprintf(buf, pssetgray, color->value.gray.i);
662 pop_lc_numeric();
663 return PSDRV_WriteSpool(dev, buf, strlen(buf));
665 default:
666 ERR("Unknown colour type %d\n", color->type);
667 break;
670 return FALSE;
673 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
675 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
676 char buf[256];
677 DWORD i, pos;
679 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
680 PSDRV_WriteSpool(dev, buf, strlen(buf));
682 if (physDev->pen.dash_len)
684 for (i = pos = 0; i < physDev->pen.dash_len; i++)
685 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
686 buf[0] = '[';
687 sprintf(buf + pos, "] %u setdash\n", 0);
689 else
690 sprintf(buf, "[] %u setdash\n", 0);
692 PSDRV_WriteSpool(dev, buf, strlen(buf));
694 return TRUE;
697 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
699 char buf[128];
700 int l;
702 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
704 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
705 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
706 return FALSE;
709 PSDRV_WriteSpool(dev, buf, l);
710 return TRUE;
713 BOOL PSDRV_WriteFill(PHYSDEV dev)
715 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
718 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
720 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
723 BOOL PSDRV_WriteGSave(PHYSDEV dev)
725 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
728 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
730 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
733 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
735 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
738 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
740 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
743 BOOL PSDRV_WriteClip(PHYSDEV dev)
745 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
748 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
750 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
753 BOOL PSDRV_WriteHatch(PHYSDEV dev)
755 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
758 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
760 char buf[256];
762 push_lc_numeric("C");
763 sprintf(buf, psrotate, ang);
764 pop_lc_numeric();
765 return PSDRV_WriteSpool(dev, buf, strlen(buf));
768 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
770 char buf[256];
771 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
772 return PSDRV_WriteSpool(dev, buf, strlen(buf));
775 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
777 static const char buf[] = ">\n] setcolorspace\n";
778 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
781 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
783 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
784 int i;
786 ptr = buf;
787 for(i = 0; i < number; i++) {
788 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
789 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
790 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
791 ptr += 7;
793 PSDRV_WriteSpool(dev, buf, number * 7);
794 HeapFree( GetProcessHeap(), 0, buf );
795 return TRUE;
798 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
800 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
801 int i;
803 ptr = buf;
804 for(i = 0; i < number; i++, rgb++)
805 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
806 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
808 PSDRV_WriteSpool(dev, buf, ptr - buf);
809 HeapFree( GetProcessHeap(), 0, buf );
810 return TRUE;
813 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth, BOOL grayscale,
814 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
816 static const char start[] = "<<\n"
817 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
818 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
820 static const char decode1[] = " /Decode [0 %d]\n";
821 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
823 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
824 static const char endbits[] = " /DataSource <%s>\n>>\n";
825 char buf[1000];
827 if (top_down)
828 sprintf(buf, start, widthSrc, heightSrc,
829 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
830 else
831 sprintf(buf, start, widthSrc, heightSrc,
832 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
834 PSDRV_WriteSpool(dev, buf, strlen(buf));
836 switch(depth) {
837 case 8:
838 sprintf(buf, decode1, 255);
839 break;
841 case 4:
842 sprintf(buf, decode1, 15);
843 break;
845 case 1:
846 sprintf(buf, decode1, 1);
847 break;
849 default:
850 if (grayscale)
851 sprintf(buf, decode1, 1);
852 else
853 strcpy(buf, decode3);
854 break;
857 PSDRV_WriteSpool(dev, buf, strlen(buf));
859 if(!bits) {
860 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
861 } else {
862 sprintf(buf, endbits, bits);
863 PSDRV_WriteSpool(dev, buf, strlen(buf));
866 return TRUE;
869 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, BOOL grayscale, INT xDst, INT yDst,
870 INT widthDst, INT heightDst, INT widthSrc,
871 INT heightSrc, BOOL mask, BOOL top_down)
873 static const char start[] = "%d %d translate\n%d %d scale\n";
874 static const char image[] = "image\n";
875 static const char imagemask[] = "imagemask\n";
876 char buf[100];
878 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
879 PSDRV_WriteSpool(dev, buf, strlen(buf));
880 PSDRV_WriteImageDict(dev, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
881 if(mask)
882 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
883 else
884 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
885 return TRUE;
889 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
891 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
892 char *ptr;
893 unsigned int i;
895 ptr = buf;
897 for(i = 0; i < number; i++) {
898 sprintf(ptr, "%02x", bytes[i]);
899 ptr += 2;
900 if(((i & 0xf) == 0xf) || (i == number - 1)) {
901 strcpy(ptr, "\n");
902 ptr++;
905 PSDRV_WriteSpool(dev, buf, ptr - buf);
906 HeapFree( GetProcessHeap(), 0, buf );
907 return TRUE;
910 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
912 int num, num_left = number;
914 do {
915 num = min(num_left, 60);
916 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
917 PSDRV_WriteSpool(dev, "\n", 1);
918 data += num;
919 num_left -= num;
920 } while(num_left);
922 return TRUE;
925 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
927 char buf[100];
929 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
930 return PSDRV_WriteSpool(dev, buf, strlen(buf));
933 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
935 char buf[100];
937 sprintf(buf, psarraydef, pszArrayName, nSize);
938 return PSDRV_WriteSpool(dev, buf, strlen(buf));
941 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
943 char buf[100];
945 sprintf(buf, psrectclip, x, y, w, h);
946 return PSDRV_WriteSpool(dev, buf, strlen(buf));
949 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
951 char buf[100];
953 sprintf(buf, psrectclip2, pszArrayName);
954 return PSDRV_WriteSpool(dev, buf, strlen(buf));
957 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
959 static const char mypat[] = "/mypat\n";
960 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
961 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
962 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
963 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
964 char *buf, *ptr;
965 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
966 COLORREF map[2];
968 TRACE( "size %dx%dx%d\n",
969 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
971 if(bmi->bmiHeader.biBitCount != 1) {
972 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
973 return FALSE;
976 w = bmi->bmiHeader.biWidth & ~0x7;
977 h = abs_height & ~0x7;
979 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
980 ptr = buf;
981 for(y = h-1; y >= 0; y--) {
982 for(x = 0; x < w/8; x++) {
983 sprintf(ptr, "%02x", *(bits + x/8 + y *
984 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
985 ptr += 2;
988 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
989 PSDRV_WriteImageDict(dev, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
990 PSDRV_WriteSpool(dev, "def\n", 4);
992 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
993 map[0] = GetTextColor( dev->hdc );
994 map[1] = GetBkColor( dev->hdc );
995 PSDRV_WriteRGB(dev, map, 2);
996 PSDRV_WriteIndexColorSpaceEnd(dev);
998 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
999 w_mult = (physDev->logPixelsX + 150) / 300;
1000 h_mult = (physDev->logPixelsY + 150) / 300;
1001 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
1002 PSDRV_WriteSpool(dev, buf, strlen(buf));
1003 HeapFree( GetProcessHeap(), 0, buf );
1004 return TRUE;