wineps: Fix top-down dib pattern brushes.
[wine/multimedia.git] / dlls / wineps.drv / ps.c
bloba1d8432a9ad6811affaceb8adf9516b41c8fabdd
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 psheader[] = /* title llx lly urx ury */
39 "%%!PS-Adobe-3.0\n"
40 "%%%%Creator: Wine PostScript Driver\n"
41 "%%%%Title: %s\n"
42 "%%%%BoundingBox: %d %d %d %d\n"
43 "%%%%Pages: (atend)\n"
44 "%%%%EndComments\n";
46 static const char psbeginprolog[] =
47 "%%BeginProlog\n";
49 static const char psendprolog[] =
50 "%%EndProlog\n";
52 static const char psprolog[] =
53 "/tmpmtrx matrix def\n"
54 "/hatch {\n"
55 " pathbbox\n"
56 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
57 " l cvi gap idiv gap mul\n"
58 " gap\n"
59 " r cvi gap idiv gap mul\n"
60 " {t moveto 0 b t sub rlineto}\n"
61 " for\n"
62 "} bind def\n"
63 "/B {pop pop pop pop} def\n"
64 "/N {newpath} def\n"
65 "/havetype42gdir {version cvi 2015 ge} bind def\n";
67 static const char psbeginsetup[] =
68 "%%BeginSetup\n";
70 static const char psendsetup[] =
71 "%%EndSetup\n";
73 static const char psbeginfeature[] = /* feature, value */
74 "mark {\n"
75 "%%%%BeginFeature: %s %s\n";
77 static const char psendfeature[] =
78 "\n%%EndFeature\n"
79 "} stopped cleartomark\n";
81 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
82 "%%%%Page: %s %d\n"
83 "%%%%BeginPageSetup\n"
84 "/pgsave save def\n"
85 "72 %d div 72 %d div scale\n"
86 "%d %d translate\n"
87 "1 -1 scale\n"
88 "%d rotate\n"
89 "%%%%EndPageSetup\n";
91 static const char psendpage[] =
92 "pgsave restore\n"
93 "showpage\n";
95 static const char psfooter[] = /* pages */
96 "%%%%Trailer\n"
97 "%%%%Pages: %d\n"
98 "%%%%EOF\n";
100 static const char psmoveto[] = /* x, y */
101 "%d %d moveto\n";
103 static const char pslineto[] = /* x, y */
104 "%d %d lineto\n";
106 static const char psstroke[] =
107 "stroke\n";
109 static const char psrectangle[] = /* x, y, width, height, -width */
110 "%d %d moveto\n"
111 "%d 0 rlineto\n"
112 "0 %d rlineto\n"
113 "%d 0 rlineto\n"
114 "closepath\n";
116 static const char psglyphshow[] = /* glyph name */
117 "/%s glyphshow\n";
119 static const char pssetfont[] = /* fontname, xx_scale, xy_scale, yx_scale, yy_scale, escapement */
120 "/%s findfont\n"
121 "[%d %d %d %d 0 0]\n"
122 "%d 10 div matrix rotate\n"
123 "matrix concatmatrix\n"
124 "makefont setfont\n";
126 static const char pssetline[] = /* width, join, endcap */
127 "%d setlinewidth %u setlinejoin %u setlinecap\n";
129 static const char pssetgray[] = /* gray */
130 "%.2f setgray\n";
132 static const char pssetrgbcolor[] = /* r, g, b */
133 "%.2f %.2f %.2f setrgbcolor\n";
135 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
136 "tmpmtrx currentmatrix pop\n"
137 "%d %d translate\n"
138 "%d %d scale\n"
139 "0 0 0.5 %.1f %.1f arc\n"
140 "tmpmtrx setmatrix\n";
142 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
143 "%d %d %d %d %d %d curveto\n";
145 static const char psgsave[] =
146 "gsave\n";
148 static const char psgrestore[] =
149 "grestore\n";
151 static const char psfill[] =
152 "fill\n";
154 static const char pseofill[] =
155 "eofill\n";
157 static const char psnewpath[] =
158 "newpath\n";
160 static const char psclosepath[] =
161 "closepath\n";
163 static const char psclip[] =
164 "clip\n";
166 static const char pseoclip[] =
167 "eoclip\n";
169 static const char psrectclip[] =
170 "%d %d %d %d rectclip\n";
172 static const char psrectclip2[] =
173 "%s rectclip\n";
175 static const char pshatch[] =
176 "hatch\n";
178 static const char psrotate[] = /* ang */
179 "%.1f rotate\n";
181 static const char psarrayput[] =
182 "%s %d %d put\n";
184 static const char psarraydef[] =
185 "/%s %d array def\n";
187 static const char psenddocument[] =
188 "\n%%EndDocument\n";
190 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
192 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
193 int num, num_left = cch;
195 if(physDev->job.quiet) {
196 TRACE("ignoring output\n");
197 return 0;
200 if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
201 write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
202 physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
205 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
206 if( !PSDRV_StartPage(dev) )
207 return 0;
210 do {
211 num = min(num_left, 0x8000);
212 if(write_spool( dev, lpData, num ) != num)
213 return 0;
214 lpData += num;
215 num_left -= num;
216 } while(num_left);
218 return cch;
222 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
225 char *buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
226 strlen(feature) + strlen(value));
228 sprintf(buf, psbeginfeature, feature, value);
229 write_spool( dev, buf, strlen(buf) );
230 write_spool( dev, invocation, strlen(invocation) );
231 write_spool( dev, psendfeature, strlen(psendfeature) );
233 HeapFree( PSDRV_Heap, 0, buf );
234 return 1;
237 /********************************************************
238 * escape_title
240 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
241 * as octal. If we've had to use an escape then surround the entire string
242 * in brackets. Truncate string to represent at most 0x80 characters.
245 static char *escape_title(LPCSTR str)
247 char *ret, *cp;
248 int i, extra = 0;
250 if(!str)
252 ret = HeapAlloc(GetProcessHeap(), 0, 1);
253 *ret = '\0';
254 return ret;
257 for(i = 0; i < 0x80 && str[i]; i++)
259 if(!isprint(str[i]))
260 extra += 3;
263 if(!extra)
265 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
266 memcpy(ret, str, i);
267 ret[i] = '\0';
268 return ret;
271 extra += 2; /* two for the brackets */
272 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
273 *cp++ = '(';
274 for(i = 0; i < 0x80 && str[i]; i++)
276 if(!isprint(str[i]))
278 BYTE b = (BYTE)str[i];
279 *cp++ = '\\';
280 *cp++ = ((b >> 6) & 0x7) + '0';
281 *cp++ = ((b >> 3) & 0x7) + '0';
282 *cp++ = ((b) & 0x7) + '0';
284 else
285 *cp++ = str[i];
287 *cp++ = ')';
288 *cp = '\0';
289 return ret;
293 INT PSDRV_WriteHeader( PHYSDEV dev, LPCSTR title )
295 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
296 char *buf, *escaped_title;
297 INPUTSLOT *slot;
298 PAGESIZE *page;
299 DUPLEX *duplex;
300 int win_duplex;
301 int llx, lly, urx, ury;
303 TRACE("%s\n", debugstr_a(title));
305 escaped_title = escape_title(title);
306 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
307 strlen(escaped_title) + 30 );
308 if(!buf) {
309 WARN("HeapAlloc failed\n");
310 return 0;
313 /* BBox co-ords are in default user co-ord system so urx < ury even in
314 landscape mode */
315 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
316 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
317 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
318 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
319 /* FIXME should do something better with BBox */
321 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
323 HeapFree(GetProcessHeap(), 0, escaped_title);
324 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
325 WARN("WriteSpool error\n");
326 HeapFree( PSDRV_Heap, 0, buf );
327 return 0;
329 HeapFree( PSDRV_Heap, 0, buf );
331 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
332 write_spool( dev, psprolog, strlen(psprolog) );
333 write_spool( dev, psendprolog, strlen(psendprolog) );
334 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
336 if(physDev->Devmode->dmPublic.u1.s1.dmCopies > 1) {
337 char copies_buf[100];
338 sprintf(copies_buf, "mark {\n << /NumCopies %d >> setpagedevice\n} stopped cleartomark\n", physDev->Devmode->dmPublic.u1.s1.dmCopies);
339 write_spool(dev, copies_buf, strlen(copies_buf));
342 for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
343 if(slot->WinBin == physDev->Devmode->dmPublic.u1.s1.dmDefaultSource) {
344 if(slot->InvocationString) {
345 PSDRV_WriteFeature(dev, "*InputSlot", slot->Name,
346 slot->InvocationString);
347 break;
352 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
353 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
354 if(page->InvocationString) {
355 PSDRV_WriteFeature(dev, "*PageSize", page->Name,
356 page->InvocationString);
357 break;
362 win_duplex = physDev->Devmode->dmPublic.dmFields & DM_DUPLEX ?
363 physDev->Devmode->dmPublic.dmDuplex : 0;
364 for(duplex = physDev->pi->ppd->Duplexes; duplex; duplex = duplex->next) {
365 if(duplex->WinDuplex == win_duplex) {
366 if(duplex->InvocationString) {
367 PSDRV_WriteFeature(dev, "*Duplex", duplex->Name,
368 duplex->InvocationString);
369 break;
374 write_spool( dev, psendsetup, strlen(psendsetup) );
377 return 1;
381 INT PSDRV_WriteFooter( PHYSDEV dev )
383 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
384 char *buf;
386 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
387 if(!buf) {
388 WARN("HeapAlloc failed\n");
389 return 0;
392 sprintf(buf, psfooter, physDev->job.PageNo);
394 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
395 WARN("WriteSpool error\n");
396 HeapFree( PSDRV_Heap, 0, buf );
397 return 0;
399 HeapFree( PSDRV_Heap, 0, buf );
400 return 1;
405 INT PSDRV_WriteEndPage( PHYSDEV dev )
407 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
408 WARN("WriteSpool error\n");
409 return 0;
411 return 1;
417 INT PSDRV_WriteNewPage( PHYSDEV dev )
419 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
420 char *buf;
421 char name[100];
422 signed int xtrans, ytrans, rotation;
424 sprintf(name, "%d", physDev->job.PageNo);
426 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
427 if(!buf) {
428 WARN("HeapAlloc failed\n");
429 return 0;
432 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
433 if(physDev->pi->ppd->LandscapeOrientation == -90) {
434 xtrans = physDev->ImageableArea.right;
435 ytrans = physDev->ImageableArea.top;
436 rotation = 90;
437 } else {
438 xtrans = physDev->ImageableArea.left;
439 ytrans = physDev->ImageableArea.bottom;
440 rotation = -90;
442 } else {
443 xtrans = physDev->ImageableArea.left;
444 ytrans = physDev->ImageableArea.top;
445 rotation = 0;
448 sprintf(buf, psnewpage, name, physDev->job.PageNo,
449 physDev->logPixelsX, physDev->logPixelsY,
450 xtrans, ytrans, rotation);
452 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
453 WARN("WriteSpool error\n");
454 HeapFree( PSDRV_Heap, 0, buf );
455 return 0;
457 HeapFree( PSDRV_Heap, 0, buf );
458 return 1;
462 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
464 char buf[100];
466 sprintf(buf, psmoveto, x, y);
467 return PSDRV_WriteSpool(dev, buf, strlen(buf));
470 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
472 char buf[100];
474 sprintf(buf, pslineto, x, y);
475 return PSDRV_WriteSpool(dev, buf, strlen(buf));
479 BOOL PSDRV_WriteStroke(PHYSDEV dev)
481 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
486 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
487 INT height)
489 char buf[100];
491 sprintf(buf, psrectangle, x, y, width, height, -width);
492 return PSDRV_WriteSpool(dev, buf, strlen(buf));
495 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
496 double ang2)
498 char buf[256];
500 /* Make angles -ve and swap order because we're working with an upside
501 down y-axis */
502 push_lc_numeric("C");
503 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
504 pop_lc_numeric();
505 return PSDRV_WriteSpool(dev, buf, strlen(buf));
508 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
510 char buf[256];
512 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
513 return PSDRV_WriteSpool(dev, buf, strlen(buf));
517 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement)
519 char *buf;
521 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(pssetfont) +
522 strlen(name) + 40);
524 if(!buf) {
525 WARN("HeapAlloc failed\n");
526 return FALSE;
529 sprintf(buf, pssetfont, name, size.xx, size.xy, size.yx, size.yy, -escapement);
531 PSDRV_WriteSpool(dev, buf, strlen(buf));
532 HeapFree(PSDRV_Heap, 0, buf);
533 return TRUE;
536 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
538 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
539 char buf[256];
541 PSDRV_CopyColor(&physDev->inkColor, color);
542 switch(color->type) {
543 case PSCOLOR_RGB:
544 push_lc_numeric("C");
545 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
546 color->value.rgb.b);
547 pop_lc_numeric();
548 return PSDRV_WriteSpool(dev, buf, strlen(buf));
550 case PSCOLOR_GRAY:
551 push_lc_numeric("C");
552 sprintf(buf, pssetgray, color->value.gray.i);
553 pop_lc_numeric();
554 return PSDRV_WriteSpool(dev, buf, strlen(buf));
556 default:
557 ERR("Unkonwn colour type %d\n", color->type);
558 break;
561 return FALSE;
564 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
566 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
567 char buf[256];
568 DWORD i, pos;
570 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
571 PSDRV_WriteSpool(dev, buf, strlen(buf));
573 if (physDev->pen.dash_len)
575 for (i = pos = 0; i < physDev->pen.dash_len; i++)
576 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
577 buf[0] = '[';
578 sprintf(buf + pos, "] %u setdash\n", 0);
580 else
581 sprintf(buf, "[] %u setdash\n", 0);
583 PSDRV_WriteSpool(dev, buf, strlen(buf));
585 return TRUE;
588 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
590 char buf[128];
591 int l;
593 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
595 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
596 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
597 return FALSE;
600 PSDRV_WriteSpool(dev, buf, l);
601 return TRUE;
604 BOOL PSDRV_WriteFill(PHYSDEV dev)
606 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
609 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
611 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
614 BOOL PSDRV_WriteGSave(PHYSDEV dev)
616 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
619 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
621 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
624 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
626 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
629 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
631 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
634 BOOL PSDRV_WriteClip(PHYSDEV dev)
636 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
639 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
641 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
644 BOOL PSDRV_WriteHatch(PHYSDEV dev)
646 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
649 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
651 char buf[256];
653 push_lc_numeric("C");
654 sprintf(buf, psrotate, ang);
655 pop_lc_numeric();
656 return PSDRV_WriteSpool(dev, buf, strlen(buf));
659 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
661 char buf[256];
662 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
663 return PSDRV_WriteSpool(dev, buf, strlen(buf));
666 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
668 static const char buf[] = ">\n] setcolorspace\n";
669 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
672 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
674 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
675 int i;
677 ptr = buf;
678 for(i = 0; i < number; i++) {
679 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
680 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
681 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
682 ptr += 7;
684 PSDRV_WriteSpool(dev, buf, number * 7);
685 HeapFree(PSDRV_Heap, 0, buf);
686 return TRUE;
689 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
691 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
692 int i;
694 ptr = buf;
695 for(i = 0; i < number; i++, rgb++)
696 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
697 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
699 PSDRV_WriteSpool(dev, buf, ptr - buf);
700 HeapFree(PSDRV_Heap, 0, buf);
701 return TRUE;
704 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth,
705 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
707 static const char start[] = "<<\n"
708 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
709 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
711 static const char decode1[] = " /Decode [0 %d]\n";
712 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
714 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
715 static const char endbits[] = " /DataSource <%s>\n>>\n";
717 char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
719 if (top_down)
720 sprintf(buf, start, widthSrc, heightSrc,
721 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
722 else
723 sprintf(buf, start, widthSrc, heightSrc,
724 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
726 PSDRV_WriteSpool(dev, buf, strlen(buf));
728 switch(depth) {
729 case 8:
730 sprintf(buf, decode1, 255);
731 break;
733 case 4:
734 sprintf(buf, decode1, 15);
735 break;
737 case 1:
738 sprintf(buf, decode1, 1);
739 break;
741 default:
742 strcpy(buf, decode3);
743 break;
746 PSDRV_WriteSpool(dev, buf, strlen(buf));
748 if(!bits) {
749 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
750 } else {
751 sprintf(buf, endbits, bits);
752 PSDRV_WriteSpool(dev, buf, strlen(buf));
755 HeapFree(PSDRV_Heap, 0, buf);
756 return TRUE;
759 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, INT xDst, INT yDst,
760 INT widthDst, INT heightDst, INT widthSrc,
761 INT heightSrc, BOOL mask, BOOL top_down)
763 static const char start[] = "%d %d translate\n%d %d scale\n";
764 static const char image[] = "image\n";
765 static const char imagemask[] = "imagemask\n";
766 char buf[100];
768 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
769 PSDRV_WriteSpool(dev, buf, strlen(buf));
770 PSDRV_WriteImageDict(dev, depth, widthSrc, heightSrc, NULL, top_down);
771 if(mask)
772 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
773 else
774 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
775 return TRUE;
779 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
781 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
782 char *ptr;
783 unsigned int i;
785 ptr = buf;
787 for(i = 0; i < number; i++) {
788 sprintf(ptr, "%02x", bytes[i]);
789 ptr += 2;
790 if(((i & 0xf) == 0xf) || (i == number - 1)) {
791 strcpy(ptr, "\n");
792 ptr++;
795 PSDRV_WriteSpool(dev, buf, ptr - buf);
796 HeapFree(PSDRV_Heap, 0, buf);
797 return TRUE;
800 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
802 int num, num_left = number;
804 do {
805 num = min(num_left, 60);
806 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
807 PSDRV_WriteSpool(dev, "\n", 1);
808 data += num;
809 num_left -= num;
810 } while(num_left);
812 return TRUE;
815 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
817 char buf[100];
819 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
820 return PSDRV_WriteSpool(dev, buf, strlen(buf));
823 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
825 char buf[100];
827 sprintf(buf, psarraydef, pszArrayName, nSize);
828 return PSDRV_WriteSpool(dev, buf, strlen(buf));
831 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
833 char buf[100];
835 sprintf(buf, psrectclip, x, y, w, h);
836 return PSDRV_WriteSpool(dev, buf, strlen(buf));
839 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
841 char buf[100];
843 sprintf(buf, psrectclip2, pszArrayName);
844 return PSDRV_WriteSpool(dev, buf, strlen(buf));
847 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
849 static const char mypat[] = "/mypat\n";
850 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
851 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
852 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
853 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
854 char *buf, *ptr;
855 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
856 COLORREF map[2];
858 TRACE( "size %dx%dx%d\n",
859 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
861 if(bmi->bmiHeader.biBitCount != 1) {
862 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
863 return FALSE;
866 w = bmi->bmiHeader.biWidth & ~0x7;
867 h = abs_height & ~0x7;
869 buf = HeapAlloc(PSDRV_Heap, 0, sizeof(do_pattern) + 100);
870 ptr = buf;
871 for(y = h-1; y >= 0; y--) {
872 for(x = 0; x < w/8; x++) {
873 sprintf(ptr, "%02x", *(bits + x/8 + y *
874 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
875 ptr += 2;
878 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
879 PSDRV_WriteImageDict(dev, 1, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
880 PSDRV_WriteSpool(dev, "def\n", 4);
882 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
883 map[0] = GetTextColor( dev->hdc );
884 map[1] = GetBkColor( dev->hdc );
885 PSDRV_WriteRGB(dev, map, 2);
886 PSDRV_WriteIndexColorSpaceEnd(dev);
888 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
889 w_mult = (physDev->logPixelsX + 150) / 300;
890 h_mult = (physDev->logPixelsY + 150) / 300;
891 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
892 PSDRV_WriteSpool(dev, buf, strlen(buf));
893 HeapFree(PSDRV_Heap, 0, buf);
894 return TRUE;