comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / wineps.drv / ps.c
bloba01346e75adf580c30c110a582a6be7f642d80e1
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";
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 "%d %d %d %d %d %d 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 %d put\n";
212 static const char psarraydef[] =
213 "/%s %d array def\n";
215 static const char psenddocument[] =
216 "\n%%EndDocument\n";
218 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
220 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
221 int num, num_left = cch;
223 if(physDev->job.quiet) {
224 TRACE("ignoring output\n");
225 return 0;
228 if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
229 write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
230 physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
233 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
234 if( !PSDRV_StartPage(dev) )
235 return 0;
238 do {
239 num = min(num_left, 0x8000);
240 if(write_spool( dev, lpData, num ) != num)
241 return 0;
242 lpData += num;
243 num_left -= num;
244 } while(num_left);
246 return cch;
250 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
253 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
254 strlen(feature) + strlen(value));
256 sprintf(buf, psbeginfeature, feature, value);
257 write_spool( dev, buf, strlen(buf) );
258 write_spool( dev, invocation, strlen(invocation) );
259 write_spool( dev, psendfeature, strlen(psendfeature) );
261 HeapFree( GetProcessHeap(), 0, buf );
262 return 1;
265 /********************************************************
266 * escape_title
268 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
269 * as octal. If we've had to use an escape then surround the entire string
270 * in brackets. Truncate string to represent at most 0x80 characters.
273 static char *escape_title(LPCWSTR wstr)
275 char *ret, *cp, *str;
276 int i, extra = 0;
278 if(!wstr)
280 ret = HeapAlloc(GetProcessHeap(), 0, 1);
281 *ret = '\0';
282 return ret;
285 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
286 str = HeapAlloc( GetProcessHeap(), 0, i );
287 if (!str) return NULL;
288 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
290 for(i = 0; i < 0x80 && str[i]; i++)
292 if(!isprint(str[i]))
293 extra += 3;
296 if(!extra)
298 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
299 memcpy(ret, str, i);
300 ret[i] = '\0';
301 goto done;
304 extra += 2; /* two for the brackets */
305 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
306 *cp++ = '(';
307 for(i = 0; i < 0x80 && str[i]; i++)
309 if(!isprint(str[i]))
311 BYTE b = (BYTE)str[i];
312 *cp++ = '\\';
313 *cp++ = ((b >> 6) & 0x7) + '0';
314 *cp++ = ((b >> 3) & 0x7) + '0';
315 *cp++ = ((b) & 0x7) + '0';
317 else
318 *cp++ = str[i];
320 *cp++ = ')';
321 *cp = '\0';
323 done:
324 HeapFree( GetProcessHeap(), 0, str );
325 return ret;
328 struct ticket_info
330 PAGESIZE *page;
331 DUPLEX *duplex;
334 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
336 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
337 char buf[256];
338 int len;
340 if (info->page && info->page->InvocationString)
342 len = sizeof(media) + strlen( info->page->Name ) + 1;
343 if (len <= sizeof(buf))
345 memcpy( buf, media, sizeof(media) );
346 strcat( buf, info->page->Name );
347 strcat( buf, "\n");
348 write_spool( dev, buf, len - 1 );
350 else
351 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
354 if (info->duplex && info->duplex->InvocationString)
356 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
358 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
359 write_spool( dev, str, strlen( str ) );
363 if (physDev->Devmode->dmPublic.u1.s1.dmCopies > 1)
365 len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
366 physDev->Devmode->dmPublic.u1.s1.dmCopies );
367 if (len > 0 && len < sizeof(buf))
368 write_spool( dev, buf, len );
370 if (physDev->Devmode->dmPublic.dmFields & DM_COLLATE)
372 if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
373 write_spool( dev, cups_collate_false, sizeof(cups_collate_false) - 1 );
374 else if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
375 write_spool( dev, cups_collate_true, sizeof(cups_collate_true) - 1 );
380 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
382 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
383 char *buf, *escaped_title;
384 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
385 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
386 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
387 int llx, lly, urx, ury;
388 int ret, len;
389 const char * dmOrientation;
391 struct ticket_info ticket_info = { page, duplex };
393 TRACE("%s\n", debugstr_w(title));
395 len = strlen( psadobe );
396 ret = write_spool( dev, psadobe, len );
397 if (ret != len)
399 WARN("WriteSpool error\n");
400 return 0;
403 write_cups_job_ticket( dev, &ticket_info );
405 escaped_title = escape_title(title);
406 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
407 strlen(escaped_title) + 30 );
408 if(!buf) {
409 WARN("HeapAlloc failed\n");
410 HeapFree(GetProcessHeap(), 0, escaped_title);
411 return 0;
414 /* BBox co-ords are in default user co-ord system so urx < ury even in
415 landscape mode */
416 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
417 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
418 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
419 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
420 /* FIXME should do something better with BBox */
422 dmOrientation = (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
423 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
425 HeapFree(GetProcessHeap(), 0, escaped_title);
427 len = strlen( buf );
428 write_spool( dev, buf, len );
429 HeapFree( GetProcessHeap(), 0, buf );
431 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
432 write_spool( dev, psprolog, strlen(psprolog) );
433 write_spool( dev, psendprolog, strlen(psendprolog) );
434 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
436 if (slot && slot->InvocationString)
437 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
439 if (page && page->InvocationString)
440 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
442 if (duplex && duplex->InvocationString)
443 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
445 write_spool( dev, psendsetup, strlen(psendsetup) );
448 return 1;
452 INT PSDRV_WriteFooter( PHYSDEV dev )
454 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
455 char *buf;
456 int ret = 1;
458 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
459 if(!buf) {
460 WARN("HeapAlloc failed\n");
461 return 0;
464 sprintf(buf, psfooter, physDev->job.PageNo);
466 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
467 WARN("WriteSpool error\n");
468 ret = 0;
470 HeapFree( GetProcessHeap(), 0, buf );
471 return ret;
476 INT PSDRV_WriteEndPage( PHYSDEV dev )
478 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
479 WARN("WriteSpool error\n");
480 return 0;
482 return 1;
488 INT PSDRV_WriteNewPage( PHYSDEV dev )
490 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
491 char *buf;
492 char name[100];
493 signed int xtrans, ytrans, rotation;
494 int ret = 1;
496 sprintf(name, "%d", physDev->job.PageNo);
498 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
499 if(!buf) {
500 WARN("HeapAlloc failed\n");
501 return 0;
504 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
505 if(physDev->pi->ppd->LandscapeOrientation == -90) {
506 xtrans = physDev->ImageableArea.right;
507 ytrans = physDev->ImageableArea.top;
508 rotation = 90;
509 } else {
510 xtrans = physDev->ImageableArea.left;
511 ytrans = physDev->ImageableArea.bottom;
512 rotation = -90;
514 } else {
515 xtrans = physDev->ImageableArea.left;
516 ytrans = physDev->ImageableArea.top;
517 rotation = 0;
520 sprintf(buf, psnewpage, name, physDev->job.PageNo,
521 physDev->logPixelsX, physDev->logPixelsY,
522 xtrans, ytrans, rotation);
524 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
525 WARN("WriteSpool error\n");
526 ret = 0;
528 HeapFree( GetProcessHeap(), 0, buf );
529 return ret;
533 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
535 char buf[100];
537 sprintf(buf, psmoveto, x, y);
538 return PSDRV_WriteSpool(dev, buf, strlen(buf));
541 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
543 char buf[100];
545 sprintf(buf, pslineto, x, y);
546 return PSDRV_WriteSpool(dev, buf, strlen(buf));
550 BOOL PSDRV_WriteStroke(PHYSDEV dev)
552 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
557 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
558 INT height)
560 char buf[100];
562 sprintf(buf, psrectangle, x, y, width, height, -width);
563 return PSDRV_WriteSpool(dev, buf, strlen(buf));
566 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
567 double ang2)
569 char buf[256];
571 /* Make angles -ve and swap order because we're working with an upside
572 down y-axis */
573 push_lc_numeric("C");
574 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
575 pop_lc_numeric();
576 return PSDRV_WriteSpool(dev, buf, strlen(buf));
579 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
581 char buf[256];
583 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
584 return PSDRV_WriteSpool(dev, buf, strlen(buf));
587 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement, BOOL fake_italic)
589 char *buf;
591 buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
593 if(!buf) {
594 WARN("HeapAlloc failed\n");
595 return FALSE;
598 sprintf( buf, psfindfont, name );
599 PSDRV_WriteSpool( dev, buf, strlen(buf) );
601 if (fake_italic) PSDRV_WriteSpool( dev, psfakeitalic, sizeof(psfakeitalic) - 1 );
603 sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
604 PSDRV_WriteSpool( dev, buf, strlen(buf) );
606 if (fake_italic) PSDRV_WriteSpool( dev, psconcat, sizeof(psconcat) - 1 );
608 if (escapement)
610 sprintf( buf, psrotatefont, -escapement );
611 PSDRV_WriteSpool( dev, buf, strlen(buf) );
614 PSDRV_WriteSpool( dev, pssetfont, sizeof(pssetfont) - 1 );
615 HeapFree( GetProcessHeap(), 0, buf );
617 return TRUE;
620 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
622 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
623 char buf[256];
625 PSDRV_CopyColor(&physDev->inkColor, color);
626 switch(color->type) {
627 case PSCOLOR_RGB:
628 push_lc_numeric("C");
629 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
630 color->value.rgb.b);
631 pop_lc_numeric();
632 return PSDRV_WriteSpool(dev, buf, strlen(buf));
634 case PSCOLOR_GRAY:
635 push_lc_numeric("C");
636 sprintf(buf, pssetgray, color->value.gray.i);
637 pop_lc_numeric();
638 return PSDRV_WriteSpool(dev, buf, strlen(buf));
640 default:
641 ERR("Unknown colour type %d\n", color->type);
642 break;
645 return FALSE;
648 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
650 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
651 char buf[256];
652 DWORD i, pos;
654 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
655 PSDRV_WriteSpool(dev, buf, strlen(buf));
657 if (physDev->pen.dash_len)
659 for (i = pos = 0; i < physDev->pen.dash_len; i++)
660 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
661 buf[0] = '[';
662 sprintf(buf + pos, "] %u setdash\n", 0);
664 else
665 sprintf(buf, "[] %u setdash\n", 0);
667 PSDRV_WriteSpool(dev, buf, strlen(buf));
669 return TRUE;
672 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
674 char buf[128];
675 int l;
677 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
679 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
680 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
681 return FALSE;
684 PSDRV_WriteSpool(dev, buf, l);
685 return TRUE;
688 BOOL PSDRV_WriteFill(PHYSDEV dev)
690 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
693 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
695 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
698 BOOL PSDRV_WriteGSave(PHYSDEV dev)
700 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
703 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
705 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
708 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
710 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
713 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
715 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
718 BOOL PSDRV_WriteClip(PHYSDEV dev)
720 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
723 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
725 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
728 BOOL PSDRV_WriteHatch(PHYSDEV dev)
730 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
733 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
735 char buf[256];
737 push_lc_numeric("C");
738 sprintf(buf, psrotate, ang);
739 pop_lc_numeric();
740 return PSDRV_WriteSpool(dev, buf, strlen(buf));
743 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
745 char buf[256];
746 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
747 return PSDRV_WriteSpool(dev, buf, strlen(buf));
750 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
752 static const char buf[] = ">\n] setcolorspace\n";
753 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
756 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
758 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
759 int i;
761 ptr = buf;
762 for(i = 0; i < number; i++) {
763 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
764 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
765 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
766 ptr += 7;
768 PSDRV_WriteSpool(dev, buf, number * 7);
769 HeapFree( GetProcessHeap(), 0, buf );
770 return TRUE;
773 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
775 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
776 int i;
778 ptr = buf;
779 for(i = 0; i < number; i++, rgb++)
780 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
781 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
783 PSDRV_WriteSpool(dev, buf, ptr - buf);
784 HeapFree( GetProcessHeap(), 0, buf );
785 return TRUE;
788 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth, BOOL grayscale,
789 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
791 static const char start[] = "<<\n"
792 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
793 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
795 static const char decode1[] = " /Decode [0 %d]\n";
796 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
798 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
799 static const char endbits[] = " /DataSource <%s>\n>>\n";
800 char buf[1000];
802 if (top_down)
803 sprintf(buf, start, widthSrc, heightSrc,
804 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
805 else
806 sprintf(buf, start, widthSrc, heightSrc,
807 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
809 PSDRV_WriteSpool(dev, buf, strlen(buf));
811 switch(depth) {
812 case 8:
813 sprintf(buf, decode1, 255);
814 break;
816 case 4:
817 sprintf(buf, decode1, 15);
818 break;
820 case 1:
821 sprintf(buf, decode1, 1);
822 break;
824 default:
825 if (grayscale)
826 sprintf(buf, decode1, 1);
827 else
828 strcpy(buf, decode3);
829 break;
832 PSDRV_WriteSpool(dev, buf, strlen(buf));
834 if(!bits) {
835 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
836 } else {
837 sprintf(buf, endbits, bits);
838 PSDRV_WriteSpool(dev, buf, strlen(buf));
841 return TRUE;
844 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, BOOL grayscale, INT xDst, INT yDst,
845 INT widthDst, INT heightDst, INT widthSrc,
846 INT heightSrc, BOOL mask, BOOL top_down)
848 static const char start[] = "%d %d translate\n%d %d scale\n";
849 static const char image[] = "image\n";
850 static const char imagemask[] = "imagemask\n";
851 char buf[100];
853 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
854 PSDRV_WriteSpool(dev, buf, strlen(buf));
855 PSDRV_WriteImageDict(dev, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
856 if(mask)
857 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
858 else
859 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
860 return TRUE;
864 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
866 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
867 char *ptr;
868 unsigned int i;
870 ptr = buf;
872 for(i = 0; i < number; i++) {
873 sprintf(ptr, "%02x", bytes[i]);
874 ptr += 2;
875 if(((i & 0xf) == 0xf) || (i == number - 1)) {
876 strcpy(ptr, "\n");
877 ptr++;
880 PSDRV_WriteSpool(dev, buf, ptr - buf);
881 HeapFree( GetProcessHeap(), 0, buf );
882 return TRUE;
885 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
887 int num, num_left = number;
889 do {
890 num = min(num_left, 60);
891 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
892 PSDRV_WriteSpool(dev, "\n", 1);
893 data += num;
894 num_left -= num;
895 } while(num_left);
897 return TRUE;
900 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
902 char buf[100];
904 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
905 return PSDRV_WriteSpool(dev, buf, strlen(buf));
908 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
910 char buf[100];
912 sprintf(buf, psarraydef, pszArrayName, nSize);
913 return PSDRV_WriteSpool(dev, buf, strlen(buf));
916 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
918 char buf[100];
920 sprintf(buf, psrectclip, x, y, w, h);
921 return PSDRV_WriteSpool(dev, buf, strlen(buf));
924 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
926 char buf[100];
928 sprintf(buf, psrectclip2, pszArrayName);
929 return PSDRV_WriteSpool(dev, buf, strlen(buf));
932 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
934 static const char mypat[] = "/mypat\n";
935 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
936 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
937 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
938 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
939 char *buf, *ptr;
940 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
941 COLORREF map[2];
943 TRACE( "size %dx%dx%d\n",
944 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
946 if(bmi->bmiHeader.biBitCount != 1) {
947 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
948 return FALSE;
951 w = bmi->bmiHeader.biWidth & ~0x7;
952 h = abs_height & ~0x7;
954 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
955 ptr = buf;
956 for(y = h-1; y >= 0; y--) {
957 for(x = 0; x < w/8; x++) {
958 sprintf(ptr, "%02x", *(bits + x/8 + y *
959 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
960 ptr += 2;
963 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
964 PSDRV_WriteImageDict(dev, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
965 PSDRV_WriteSpool(dev, "def\n", 4);
967 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
968 map[0] = GetTextColor( dev->hdc );
969 map[1] = GetBkColor( dev->hdc );
970 PSDRV_WriteRGB(dev, map, 2);
971 PSDRV_WriteIndexColorSpaceEnd(dev);
973 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
974 w_mult = (physDev->logPixelsX + 150) / 300;
975 h_mult = (physDev->logPixelsY + 150) / 300;
976 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
977 PSDRV_WriteSpool(dev, buf, strlen(buf));
978 HeapFree( GetProcessHeap(), 0, buf );
979 return TRUE;