wineps: Store the document title as a unicode string.
[wine/multimedia.git] / dlls / wineps.drv / ps.c
blob8b0d576bd542b1bee0ee1272f59ccec0e6d6aeb8
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 <string.h>
24 #include <stdarg.h>
25 #include <locale.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 static const char psheader[] = /* title llx lly urx ury */
38 "%%!PS-Adobe-3.0\n"
39 "%%%%Creator: Wine PostScript Driver\n"
40 "%%%%Title: %s\n"
41 "%%%%BoundingBox: %d %d %d %d\n"
42 "%%%%Pages: (atend)\n"
43 "%%%%EndComments\n";
45 static const char psbeginprolog[] =
46 "%%BeginProlog\n";
48 static const char psendprolog[] =
49 "%%EndProlog\n";
51 static const char psprolog[] =
52 "/tmpmtrx matrix def\n"
53 "/hatch {\n"
54 " pathbbox\n"
55 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
56 " l cvi gap idiv gap mul\n"
57 " gap\n"
58 " r cvi gap idiv gap mul\n"
59 " {t moveto 0 b t sub rlineto}\n"
60 " for\n"
61 "} bind def\n"
62 "/B {pop pop pop pop} def\n"
63 "/N {newpath} def\n"
64 "/havetype42gdir {version cvi 2015 ge} bind def\n";
66 static const char psbeginsetup[] =
67 "%%BeginSetup\n";
69 static const char psendsetup[] =
70 "%%EndSetup\n";
72 static const char psbeginfeature[] = /* feature, value */
73 "mark {\n"
74 "%%%%BeginFeature: %s %s\n";
76 static const char psendfeature[] =
77 "\n%%EndFeature\n"
78 "} stopped cleartomark\n";
80 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
81 "%%%%Page: %s %d\n"
82 "%%%%BeginPageSetup\n"
83 "/pgsave save def\n"
84 "72 %d div 72 %d div scale\n"
85 "%d %d translate\n"
86 "1 -1 scale\n"
87 "%d rotate\n"
88 "%%%%EndPageSetup\n";
90 static const char psendpage[] =
91 "pgsave restore\n"
92 "showpage\n";
94 static const char psfooter[] = /* pages */
95 "%%%%Trailer\n"
96 "%%%%Pages: %d\n"
97 "%%%%EOF\n";
99 static const char psmoveto[] = /* x, y */
100 "%d %d moveto\n";
102 static const char pslineto[] = /* x, y */
103 "%d %d lineto\n";
105 static const char psstroke[] =
106 "stroke\n";
108 static const char psrectangle[] = /* x, y, width, height, -width */
109 "%d %d moveto\n"
110 "%d 0 rlineto\n"
111 "0 %d rlineto\n"
112 "%d 0 rlineto\n"
113 "closepath\n";
115 static const char psglyphshow[] = /* glyph name */
116 "/%s glyphshow\n";
118 static const char pssetfont[] = /* fontname, xx_scale, xy_scale, yx_scale, yy_scale, escapement */
119 "/%s findfont\n"
120 "[%d %d %d %d 0 0]\n"
121 "%d 10 div matrix rotate\n"
122 "matrix concatmatrix\n"
123 "makefont setfont\n";
125 static const char pssetline[] = /* width, join, endcap */
126 "%d setlinewidth %u setlinejoin %u setlinecap\n";
128 static const char pssetgray[] = /* gray */
129 "%.2f setgray\n";
131 static const char pssetrgbcolor[] = /* r, g, b */
132 "%.2f %.2f %.2f setrgbcolor\n";
134 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
135 "tmpmtrx currentmatrix pop\n"
136 "%d %d translate\n"
137 "%d %d scale\n"
138 "0 0 0.5 %.1f %.1f arc\n"
139 "tmpmtrx setmatrix\n";
141 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
142 "%d %d %d %d %d %d curveto\n";
144 static const char psgsave[] =
145 "gsave\n";
147 static const char psgrestore[] =
148 "grestore\n";
150 static const char psfill[] =
151 "fill\n";
153 static const char pseofill[] =
154 "eofill\n";
156 static const char psnewpath[] =
157 "newpath\n";
159 static const char psclosepath[] =
160 "closepath\n";
162 static const char psclip[] =
163 "clip\n";
165 static const char pseoclip[] =
166 "eoclip\n";
168 static const char psrectclip[] =
169 "%d %d %d %d rectclip\n";
171 static const char psrectclip2[] =
172 "%s rectclip\n";
174 static const char pshatch[] =
175 "hatch\n";
177 static const char psrotate[] = /* ang */
178 "%.1f rotate\n";
180 static const char psarrayput[] =
181 "%s %d %d put\n";
183 static const char psarraydef[] =
184 "/%s %d array def\n";
186 static const char psenddocument[] =
187 "\n%%EndDocument\n";
189 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
191 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
192 int num, num_left = cch;
194 if(physDev->job.quiet) {
195 TRACE("ignoring output\n");
196 return 0;
199 if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
200 write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
201 physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
204 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
205 if( !PSDRV_StartPage(dev) )
206 return 0;
209 do {
210 num = min(num_left, 0x8000);
211 if(write_spool( dev, lpData, num ) != num)
212 return 0;
213 lpData += num;
214 num_left -= num;
215 } while(num_left);
217 return cch;
221 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
224 char *buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
225 strlen(feature) + strlen(value));
227 sprintf(buf, psbeginfeature, feature, value);
228 write_spool( dev, buf, strlen(buf) );
229 write_spool( dev, invocation, strlen(invocation) );
230 write_spool( dev, psendfeature, strlen(psendfeature) );
232 HeapFree( PSDRV_Heap, 0, buf );
233 return 1;
236 /********************************************************
237 * escape_title
239 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
240 * as octal. If we've had to use an escape then surround the entire string
241 * in brackets. Truncate string to represent at most 0x80 characters.
244 static char *escape_title(LPCWSTR wstr)
246 char *ret, *cp, *str;
247 int i, extra = 0;
249 if(!wstr)
251 ret = HeapAlloc(GetProcessHeap(), 0, 1);
252 *ret = '\0';
253 return ret;
256 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
257 str = HeapAlloc( GetProcessHeap(), 0, i );
258 if (!str) return NULL;
259 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
261 for(i = 0; i < 0x80 && str[i]; i++)
263 if(!isprint(str[i]))
264 extra += 3;
267 if(!extra)
269 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
270 memcpy(ret, str, i);
271 ret[i] = '\0';
272 goto done;
275 extra += 2; /* two for the brackets */
276 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
277 *cp++ = '(';
278 for(i = 0; i < 0x80 && str[i]; i++)
280 if(!isprint(str[i]))
282 BYTE b = (BYTE)str[i];
283 *cp++ = '\\';
284 *cp++ = ((b >> 6) & 0x7) + '0';
285 *cp++ = ((b >> 3) & 0x7) + '0';
286 *cp++ = ((b) & 0x7) + '0';
288 else
289 *cp++ = str[i];
291 *cp++ = ')';
292 *cp = '\0';
294 done:
295 HeapFree( GetProcessHeap(), 0, str );
296 return ret;
300 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
302 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
303 char *buf, *escaped_title;
304 INPUTSLOT *slot;
305 PAGESIZE *page;
306 DUPLEX *duplex;
307 int win_duplex;
308 int llx, lly, urx, ury;
310 TRACE("%s\n", debugstr_w(title));
312 escaped_title = escape_title(title);
313 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
314 strlen(escaped_title) + 30 );
315 if(!buf) {
316 WARN("HeapAlloc failed\n");
317 return 0;
320 /* BBox co-ords are in default user co-ord system so urx < ury even in
321 landscape mode */
322 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
323 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
324 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
325 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
326 /* FIXME should do something better with BBox */
328 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
330 HeapFree(GetProcessHeap(), 0, escaped_title);
331 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
332 WARN("WriteSpool error\n");
333 HeapFree( PSDRV_Heap, 0, buf );
334 return 0;
336 HeapFree( PSDRV_Heap, 0, buf );
338 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
339 write_spool( dev, psprolog, strlen(psprolog) );
340 write_spool( dev, psendprolog, strlen(psendprolog) );
341 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
343 if(physDev->Devmode->dmPublic.u1.s1.dmCopies > 1) {
344 char copies_buf[100];
345 sprintf(copies_buf, "mark {\n << /NumCopies %d >> setpagedevice\n} stopped cleartomark\n", physDev->Devmode->dmPublic.u1.s1.dmCopies);
346 write_spool(dev, copies_buf, strlen(copies_buf));
349 for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
350 if(slot->WinBin == physDev->Devmode->dmPublic.u1.s1.dmDefaultSource) {
351 if(slot->InvocationString) {
352 PSDRV_WriteFeature(dev, "*InputSlot", slot->Name,
353 slot->InvocationString);
354 break;
359 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
360 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
361 if(page->InvocationString) {
362 PSDRV_WriteFeature(dev, "*PageSize", page->Name,
363 page->InvocationString);
364 break;
369 win_duplex = physDev->Devmode->dmPublic.dmFields & DM_DUPLEX ?
370 physDev->Devmode->dmPublic.dmDuplex : 0;
371 for(duplex = physDev->pi->ppd->Duplexes; duplex; duplex = duplex->next) {
372 if(duplex->WinDuplex == win_duplex) {
373 if(duplex->InvocationString) {
374 PSDRV_WriteFeature(dev, "*Duplex", duplex->Name,
375 duplex->InvocationString);
376 break;
381 write_spool( dev, psendsetup, strlen(psendsetup) );
384 return 1;
388 INT PSDRV_WriteFooter( PHYSDEV dev )
390 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
391 char *buf;
393 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
394 if(!buf) {
395 WARN("HeapAlloc failed\n");
396 return 0;
399 sprintf(buf, psfooter, physDev->job.PageNo);
401 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
402 WARN("WriteSpool error\n");
403 HeapFree( PSDRV_Heap, 0, buf );
404 return 0;
406 HeapFree( PSDRV_Heap, 0, buf );
407 return 1;
412 INT PSDRV_WriteEndPage( PHYSDEV dev )
414 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
415 WARN("WriteSpool error\n");
416 return 0;
418 return 1;
424 INT PSDRV_WriteNewPage( PHYSDEV dev )
426 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
427 char *buf;
428 char name[100];
429 signed int xtrans, ytrans, rotation;
431 sprintf(name, "%d", physDev->job.PageNo);
433 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
434 if(!buf) {
435 WARN("HeapAlloc failed\n");
436 return 0;
439 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
440 if(physDev->pi->ppd->LandscapeOrientation == -90) {
441 xtrans = physDev->ImageableArea.right;
442 ytrans = physDev->ImageableArea.top;
443 rotation = 90;
444 } else {
445 xtrans = physDev->ImageableArea.left;
446 ytrans = physDev->ImageableArea.bottom;
447 rotation = -90;
449 } else {
450 xtrans = physDev->ImageableArea.left;
451 ytrans = physDev->ImageableArea.top;
452 rotation = 0;
455 sprintf(buf, psnewpage, name, physDev->job.PageNo,
456 physDev->logPixelsX, physDev->logPixelsY,
457 xtrans, ytrans, rotation);
459 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
460 WARN("WriteSpool error\n");
461 HeapFree( PSDRV_Heap, 0, buf );
462 return 0;
464 HeapFree( PSDRV_Heap, 0, buf );
465 return 1;
469 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
471 char buf[100];
473 sprintf(buf, psmoveto, x, y);
474 return PSDRV_WriteSpool(dev, buf, strlen(buf));
477 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
479 char buf[100];
481 sprintf(buf, pslineto, x, y);
482 return PSDRV_WriteSpool(dev, buf, strlen(buf));
486 BOOL PSDRV_WriteStroke(PHYSDEV dev)
488 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
493 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
494 INT height)
496 char buf[100];
498 sprintf(buf, psrectangle, x, y, width, height, -width);
499 return PSDRV_WriteSpool(dev, buf, strlen(buf));
502 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
503 double ang2)
505 char buf[256];
507 /* Make angles -ve and swap order because we're working with an upside
508 down y-axis */
509 push_lc_numeric("C");
510 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
511 pop_lc_numeric();
512 return PSDRV_WriteSpool(dev, buf, strlen(buf));
515 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
517 char buf[256];
519 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
520 return PSDRV_WriteSpool(dev, buf, strlen(buf));
524 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement)
526 char *buf;
528 buf = HeapAlloc( PSDRV_Heap, 0, sizeof(pssetfont) +
529 strlen(name) + 40);
531 if(!buf) {
532 WARN("HeapAlloc failed\n");
533 return FALSE;
536 sprintf(buf, pssetfont, name, size.xx, size.xy, size.yx, size.yy, -escapement);
538 PSDRV_WriteSpool(dev, buf, strlen(buf));
539 HeapFree(PSDRV_Heap, 0, buf);
540 return TRUE;
543 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
545 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
546 char buf[256];
548 PSDRV_CopyColor(&physDev->inkColor, color);
549 switch(color->type) {
550 case PSCOLOR_RGB:
551 push_lc_numeric("C");
552 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
553 color->value.rgb.b);
554 pop_lc_numeric();
555 return PSDRV_WriteSpool(dev, buf, strlen(buf));
557 case PSCOLOR_GRAY:
558 push_lc_numeric("C");
559 sprintf(buf, pssetgray, color->value.gray.i);
560 pop_lc_numeric();
561 return PSDRV_WriteSpool(dev, buf, strlen(buf));
563 default:
564 ERR("Unkonwn colour type %d\n", color->type);
565 break;
568 return FALSE;
571 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
573 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
574 char buf[256];
575 DWORD i, pos;
577 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
578 PSDRV_WriteSpool(dev, buf, strlen(buf));
580 if (physDev->pen.dash_len)
582 for (i = pos = 0; i < physDev->pen.dash_len; i++)
583 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
584 buf[0] = '[';
585 sprintf(buf + pos, "] %u setdash\n", 0);
587 else
588 sprintf(buf, "[] %u setdash\n", 0);
590 PSDRV_WriteSpool(dev, buf, strlen(buf));
592 return TRUE;
595 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
597 char buf[128];
598 int l;
600 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
602 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
603 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
604 return FALSE;
607 PSDRV_WriteSpool(dev, buf, l);
608 return TRUE;
611 BOOL PSDRV_WriteFill(PHYSDEV dev)
613 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
616 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
618 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
621 BOOL PSDRV_WriteGSave(PHYSDEV dev)
623 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
626 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
628 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
631 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
633 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
636 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
638 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
641 BOOL PSDRV_WriteClip(PHYSDEV dev)
643 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
646 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
648 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
651 BOOL PSDRV_WriteHatch(PHYSDEV dev)
653 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
656 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
658 char buf[256];
660 push_lc_numeric("C");
661 sprintf(buf, psrotate, ang);
662 pop_lc_numeric();
663 return PSDRV_WriteSpool(dev, buf, strlen(buf));
666 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
668 char buf[256];
669 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
670 return PSDRV_WriteSpool(dev, buf, strlen(buf));
673 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
675 static const char buf[] = ">\n] setcolorspace\n";
676 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
679 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
681 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
682 int i;
684 ptr = buf;
685 for(i = 0; i < number; i++) {
686 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
687 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
688 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
689 ptr += 7;
691 PSDRV_WriteSpool(dev, buf, number * 7);
692 HeapFree(PSDRV_Heap, 0, buf);
693 return TRUE;
696 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
698 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
699 int i;
701 ptr = buf;
702 for(i = 0; i < number; i++, rgb++)
703 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
704 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
706 PSDRV_WriteSpool(dev, buf, ptr - buf);
707 HeapFree(PSDRV_Heap, 0, buf);
708 return TRUE;
711 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth,
712 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
714 static const char start[] = "<<\n"
715 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
716 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
718 static const char decode1[] = " /Decode [0 %d]\n";
719 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
721 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
722 static const char endbits[] = " /DataSource <%s>\n>>\n";
724 char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
726 if (top_down)
727 sprintf(buf, start, widthSrc, heightSrc,
728 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
729 else
730 sprintf(buf, start, widthSrc, heightSrc,
731 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
733 PSDRV_WriteSpool(dev, buf, strlen(buf));
735 switch(depth) {
736 case 8:
737 sprintf(buf, decode1, 255);
738 break;
740 case 4:
741 sprintf(buf, decode1, 15);
742 break;
744 case 1:
745 sprintf(buf, decode1, 1);
746 break;
748 default:
749 strcpy(buf, decode3);
750 break;
753 PSDRV_WriteSpool(dev, buf, strlen(buf));
755 if(!bits) {
756 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
757 } else {
758 sprintf(buf, endbits, bits);
759 PSDRV_WriteSpool(dev, buf, strlen(buf));
762 HeapFree(PSDRV_Heap, 0, buf);
763 return TRUE;
766 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, INT xDst, INT yDst,
767 INT widthDst, INT heightDst, INT widthSrc,
768 INT heightSrc, BOOL mask, BOOL top_down)
770 static const char start[] = "%d %d translate\n%d %d scale\n";
771 static const char image[] = "image\n";
772 static const char imagemask[] = "imagemask\n";
773 char buf[100];
775 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
776 PSDRV_WriteSpool(dev, buf, strlen(buf));
777 PSDRV_WriteImageDict(dev, depth, widthSrc, heightSrc, NULL, top_down);
778 if(mask)
779 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
780 else
781 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
782 return TRUE;
786 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
788 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
789 char *ptr;
790 unsigned int i;
792 ptr = buf;
794 for(i = 0; i < number; i++) {
795 sprintf(ptr, "%02x", bytes[i]);
796 ptr += 2;
797 if(((i & 0xf) == 0xf) || (i == number - 1)) {
798 strcpy(ptr, "\n");
799 ptr++;
802 PSDRV_WriteSpool(dev, buf, ptr - buf);
803 HeapFree(PSDRV_Heap, 0, buf);
804 return TRUE;
807 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
809 int num, num_left = number;
811 do {
812 num = min(num_left, 60);
813 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
814 PSDRV_WriteSpool(dev, "\n", 1);
815 data += num;
816 num_left -= num;
817 } while(num_left);
819 return TRUE;
822 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
824 char buf[100];
826 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
827 return PSDRV_WriteSpool(dev, buf, strlen(buf));
830 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
832 char buf[100];
834 sprintf(buf, psarraydef, pszArrayName, nSize);
835 return PSDRV_WriteSpool(dev, buf, strlen(buf));
838 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
840 char buf[100];
842 sprintf(buf, psrectclip, x, y, w, h);
843 return PSDRV_WriteSpool(dev, buf, strlen(buf));
846 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
848 char buf[100];
850 sprintf(buf, psrectclip2, pszArrayName);
851 return PSDRV_WriteSpool(dev, buf, strlen(buf));
854 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
856 static const char mypat[] = "/mypat\n";
857 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
858 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
859 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
860 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
861 char *buf, *ptr;
862 INT w, h, x, y, w_mult, h_mult;
863 COLORREF map[2];
865 TRACE( "size %dx%dx%d\n",
866 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
868 if(bmi->bmiHeader.biBitCount != 1) {
869 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
870 return FALSE;
873 w = bmi->bmiHeader.biWidth & ~0x7;
874 h = bmi->bmiHeader.biHeight & ~0x7;
876 buf = HeapAlloc(PSDRV_Heap, 0, sizeof(do_pattern) + 100);
877 ptr = buf;
878 for(y = h-1; y >= 0; y--) {
879 for(x = 0; x < w/8; x++) {
880 sprintf(ptr, "%02x", *(bits + x/8 + y *
881 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
882 ptr += 2;
885 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
886 PSDRV_WriteImageDict(dev, 1, 8, 8, buf, FALSE);
887 PSDRV_WriteSpool(dev, "def\n", 4);
889 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
890 map[0] = GetTextColor( dev->hdc );
891 map[1] = GetBkColor( dev->hdc );
892 PSDRV_WriteRGB(dev, map, 2);
893 PSDRV_WriteIndexColorSpaceEnd(dev);
895 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
896 w_mult = (physDev->logPixelsX + 150) / 300;
897 h_mult = (physDev->logPixelsY + 150) / 300;
898 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
899 PSDRV_WriteSpool(dev, buf, strlen(buf));
900 HeapFree(PSDRV_Heap, 0, buf);
901 return TRUE;