mapi32: Add support to MAPISendMailW for ANSI fallback.
[wine/multimedia.git] / dlls / wineps.drv / ps.c
blob0312b36b7f4b6e34796a7fc1f42a570cdf0403a6
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 psadobe[] =
38 "%!PS-Adobe-3.0\n";
40 static const char media[] = "%cupsJobTicket: media=";
41 static const char cups_one_sided[] = "%cupsJobTicket: sides=one-sided\n";
42 static const char cups_two_sided_long[] = "%cupsJobTicket: sides=two-sided-long-edge\n";
43 static const char cups_two_sided_short[] = "%cupsJobTicket: sides=two-sided-short-edge\n";
44 static const char *cups_duplexes[3] =
46 cups_one_sided, /* DMDUP_SIMPLEX */
47 cups_two_sided_long, /* DMDUP_VERTICAL */
48 cups_two_sided_short /* DMDUP_HORIZONTAL */
51 static const char psheader[] = /* title llx lly urx ury */
52 "%%%%Creator: Wine PostScript Driver\n"
53 "%%%%Title: %s\n"
54 "%%%%BoundingBox: %d %d %d %d\n"
55 "%%%%Pages: (atend)\n"
56 "%%%%EndComments\n";
58 static const char psbeginprolog[] =
59 "%%BeginProlog\n";
61 static const char psendprolog[] =
62 "%%EndProlog\n";
64 static const char psprolog[] =
65 "/tmpmtrx matrix def\n"
66 "/hatch {\n"
67 " pathbbox\n"
68 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
69 " l cvi gap idiv gap mul\n"
70 " gap\n"
71 " r cvi gap idiv gap mul\n"
72 " {t moveto 0 b t sub rlineto}\n"
73 " for\n"
74 "} bind def\n"
75 "/B {pop pop pop pop} def\n"
76 "/N {newpath} def\n"
77 "/havetype42gdir {version cvi 2015 ge} bind def\n";
79 static const char psbeginsetup[] =
80 "%%BeginSetup\n";
82 static const char psendsetup[] =
83 "%%EndSetup\n";
85 static const char psbeginfeature[] = /* feature, value */
86 "mark {\n"
87 "%%%%BeginFeature: %s %s\n";
89 static const char psendfeature[] =
90 "\n%%EndFeature\n"
91 "} stopped cleartomark\n";
93 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
94 "%%%%Page: %s %d\n"
95 "%%%%BeginPageSetup\n"
96 "/pgsave save def\n"
97 "72 %d div 72 %d div scale\n"
98 "%d %d translate\n"
99 "1 -1 scale\n"
100 "%d rotate\n"
101 "%%%%EndPageSetup\n";
103 static const char psendpage[] =
104 "pgsave restore\n"
105 "showpage\n";
107 static const char psfooter[] = /* pages */
108 "%%%%Trailer\n"
109 "%%%%Pages: %d\n"
110 "%%%%EOF\n";
112 static const char psmoveto[] = /* x, y */
113 "%d %d moveto\n";
115 static const char pslineto[] = /* x, y */
116 "%d %d lineto\n";
118 static const char psstroke[] =
119 "stroke\n";
121 static const char psrectangle[] = /* x, y, width, height, -width */
122 "%d %d moveto\n"
123 "%d 0 rlineto\n"
124 "0 %d rlineto\n"
125 "%d 0 rlineto\n"
126 "closepath\n";
128 static const char psglyphshow[] = /* glyph name */
129 "/%s glyphshow\n";
131 static const char pssetfont[] = /* fontname, xx_scale, xy_scale, yx_scale, yy_scale, escapement */
132 "/%s findfont\n"
133 "[%d %d %d %d 0 0]\n"
134 "%d 10 div matrix rotate\n"
135 "matrix concatmatrix\n"
136 "makefont setfont\n";
138 static const char pssetline[] = /* width, join, endcap */
139 "%d setlinewidth %u setlinejoin %u setlinecap\n";
141 static const char pssetgray[] = /* gray */
142 "%.2f setgray\n";
144 static const char pssetrgbcolor[] = /* r, g, b */
145 "%.2f %.2f %.2f setrgbcolor\n";
147 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
148 "tmpmtrx currentmatrix pop\n"
149 "%d %d translate\n"
150 "%d %d scale\n"
151 "0 0 0.5 %.1f %.1f arc\n"
152 "tmpmtrx setmatrix\n";
154 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
155 "%d %d %d %d %d %d curveto\n";
157 static const char psgsave[] =
158 "gsave\n";
160 static const char psgrestore[] =
161 "grestore\n";
163 static const char psfill[] =
164 "fill\n";
166 static const char pseofill[] =
167 "eofill\n";
169 static const char psnewpath[] =
170 "newpath\n";
172 static const char psclosepath[] =
173 "closepath\n";
175 static const char psclip[] =
176 "clip\n";
178 static const char pseoclip[] =
179 "eoclip\n";
181 static const char psrectclip[] =
182 "%d %d %d %d rectclip\n";
184 static const char psrectclip2[] =
185 "%s rectclip\n";
187 static const char pshatch[] =
188 "hatch\n";
190 static const char psrotate[] = /* ang */
191 "%.1f rotate\n";
193 static const char psarrayput[] =
194 "%s %d %d put\n";
196 static const char psarraydef[] =
197 "/%s %d array def\n";
199 static const char psenddocument[] =
200 "\n%%EndDocument\n";
202 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
204 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
205 int num, num_left = cch;
207 if(physDev->job.quiet) {
208 TRACE("ignoring output\n");
209 return 0;
212 if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
213 write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
214 physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
217 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
218 if( !PSDRV_StartPage(dev) )
219 return 0;
222 do {
223 num = min(num_left, 0x8000);
224 if(write_spool( dev, lpData, num ) != num)
225 return 0;
226 lpData += num;
227 num_left -= num;
228 } while(num_left);
230 return cch;
234 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
237 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
238 strlen(feature) + strlen(value));
240 sprintf(buf, psbeginfeature, feature, value);
241 write_spool( dev, buf, strlen(buf) );
242 write_spool( dev, invocation, strlen(invocation) );
243 write_spool( dev, psendfeature, strlen(psendfeature) );
245 HeapFree( GetProcessHeap(), 0, buf );
246 return 1;
249 /********************************************************
250 * escape_title
252 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
253 * as octal. If we've had to use an escape then surround the entire string
254 * in brackets. Truncate string to represent at most 0x80 characters.
257 static char *escape_title(LPCWSTR wstr)
259 char *ret, *cp, *str;
260 int i, extra = 0;
262 if(!wstr)
264 ret = HeapAlloc(GetProcessHeap(), 0, 1);
265 *ret = '\0';
266 return ret;
269 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
270 str = HeapAlloc( GetProcessHeap(), 0, i );
271 if (!str) return NULL;
272 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
274 for(i = 0; i < 0x80 && str[i]; i++)
276 if(!isprint(str[i]))
277 extra += 3;
280 if(!extra)
282 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
283 memcpy(ret, str, i);
284 ret[i] = '\0';
285 goto done;
288 extra += 2; /* two for the brackets */
289 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
290 *cp++ = '(';
291 for(i = 0; i < 0x80 && str[i]; i++)
293 if(!isprint(str[i]))
295 BYTE b = (BYTE)str[i];
296 *cp++ = '\\';
297 *cp++ = ((b >> 6) & 0x7) + '0';
298 *cp++ = ((b >> 3) & 0x7) + '0';
299 *cp++ = ((b) & 0x7) + '0';
301 else
302 *cp++ = str[i];
304 *cp++ = ')';
305 *cp = '\0';
307 done:
308 HeapFree( GetProcessHeap(), 0, str );
309 return ret;
312 struct ticket_info
314 PAGESIZE *page;
315 DUPLEX *duplex;
318 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
320 char buf[256];
321 int len;
323 if (info->page && info->page->InvocationString)
325 len = sizeof(media) + strlen( info->page->Name ) + 1;
326 if (len <= sizeof(buf))
328 memcpy( buf, media, sizeof(media) );
329 strcat( buf, info->page->Name );
330 strcat( buf, "\n");
331 write_spool( dev, buf, len - 1 );
333 else
334 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
337 if (info->duplex && info->duplex->InvocationString)
339 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
341 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
342 write_spool( dev, str, strlen( str ) );
347 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
349 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
350 char *buf, *escaped_title;
351 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
352 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
353 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
354 int llx, lly, urx, ury;
355 int ret, len;
356 struct ticket_info ticket_info = { page, duplex };
358 TRACE("%s\n", debugstr_w(title));
360 len = strlen( psadobe );
361 ret = write_spool( dev, psadobe, len );
362 if (ret != len)
364 WARN("WriteSpool error\n");
365 return 0;
368 write_cups_job_ticket( dev, &ticket_info );
370 escaped_title = escape_title(title);
371 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
372 strlen(escaped_title) + 30 );
373 if(!buf) {
374 WARN("HeapAlloc failed\n");
375 return 0;
378 /* BBox co-ords are in default user co-ord system so urx < ury even in
379 landscape mode */
380 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
381 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
382 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
383 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
384 /* FIXME should do something better with BBox */
386 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
388 HeapFree(GetProcessHeap(), 0, escaped_title);
390 len = strlen( buf );
391 write_spool( dev, buf, len );
392 HeapFree( GetProcessHeap(), 0, buf );
394 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
395 write_spool( dev, psprolog, strlen(psprolog) );
396 write_spool( dev, psendprolog, strlen(psendprolog) );
397 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
399 if(physDev->Devmode->dmPublic.u1.s1.dmCopies > 1) {
400 char copies_buf[100];
401 sprintf(copies_buf, "mark {\n << /NumCopies %d >> setpagedevice\n} stopped cleartomark\n", physDev->Devmode->dmPublic.u1.s1.dmCopies);
402 write_spool(dev, copies_buf, strlen(copies_buf));
405 if (slot && slot->InvocationString)
406 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
408 if (page && page->InvocationString)
409 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
411 if (duplex && duplex->InvocationString)
412 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
414 write_spool( dev, psendsetup, strlen(psendsetup) );
417 return 1;
421 INT PSDRV_WriteFooter( PHYSDEV dev )
423 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
424 char *buf;
425 int ret = 1;
427 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
428 if(!buf) {
429 WARN("HeapAlloc failed\n");
430 return 0;
433 sprintf(buf, psfooter, physDev->job.PageNo);
435 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
436 WARN("WriteSpool error\n");
437 ret = 0;
439 HeapFree( GetProcessHeap(), 0, buf );
440 return ret;
445 INT PSDRV_WriteEndPage( PHYSDEV dev )
447 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
448 WARN("WriteSpool error\n");
449 return 0;
451 return 1;
457 INT PSDRV_WriteNewPage( PHYSDEV dev )
459 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
460 char *buf;
461 char name[100];
462 signed int xtrans, ytrans, rotation;
463 int ret = 1;
465 sprintf(name, "%d", physDev->job.PageNo);
467 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
468 if(!buf) {
469 WARN("HeapAlloc failed\n");
470 return 0;
473 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
474 if(physDev->pi->ppd->LandscapeOrientation == -90) {
475 xtrans = physDev->ImageableArea.right;
476 ytrans = physDev->ImageableArea.top;
477 rotation = 90;
478 } else {
479 xtrans = physDev->ImageableArea.left;
480 ytrans = physDev->ImageableArea.bottom;
481 rotation = -90;
483 } else {
484 xtrans = physDev->ImageableArea.left;
485 ytrans = physDev->ImageableArea.top;
486 rotation = 0;
489 sprintf(buf, psnewpage, name, physDev->job.PageNo,
490 physDev->logPixelsX, physDev->logPixelsY,
491 xtrans, ytrans, rotation);
493 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
494 WARN("WriteSpool error\n");
495 ret = 0;
497 HeapFree( GetProcessHeap(), 0, buf );
498 return ret;
502 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
504 char buf[100];
506 sprintf(buf, psmoveto, x, y);
507 return PSDRV_WriteSpool(dev, buf, strlen(buf));
510 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
512 char buf[100];
514 sprintf(buf, pslineto, x, y);
515 return PSDRV_WriteSpool(dev, buf, strlen(buf));
519 BOOL PSDRV_WriteStroke(PHYSDEV dev)
521 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
526 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
527 INT height)
529 char buf[100];
531 sprintf(buf, psrectangle, x, y, width, height, -width);
532 return PSDRV_WriteSpool(dev, buf, strlen(buf));
535 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
536 double ang2)
538 char buf[256];
540 /* Make angles -ve and swap order because we're working with an upside
541 down y-axis */
542 push_lc_numeric("C");
543 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
544 pop_lc_numeric();
545 return PSDRV_WriteSpool(dev, buf, strlen(buf));
548 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
550 char buf[256];
552 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
553 return PSDRV_WriteSpool(dev, buf, strlen(buf));
557 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement)
559 char *buf;
561 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(pssetfont) + strlen(name) + 40 );
563 if(!buf) {
564 WARN("HeapAlloc failed\n");
565 return FALSE;
568 sprintf(buf, pssetfont, name, size.xx, size.xy, size.yx, size.yy, -escapement);
570 PSDRV_WriteSpool(dev, buf, strlen(buf));
571 HeapFree( GetProcessHeap(), 0, buf );
572 return TRUE;
575 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
577 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
578 char buf[256];
580 PSDRV_CopyColor(&physDev->inkColor, color);
581 switch(color->type) {
582 case PSCOLOR_RGB:
583 push_lc_numeric("C");
584 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
585 color->value.rgb.b);
586 pop_lc_numeric();
587 return PSDRV_WriteSpool(dev, buf, strlen(buf));
589 case PSCOLOR_GRAY:
590 push_lc_numeric("C");
591 sprintf(buf, pssetgray, color->value.gray.i);
592 pop_lc_numeric();
593 return PSDRV_WriteSpool(dev, buf, strlen(buf));
595 default:
596 ERR("Unkonwn colour type %d\n", color->type);
597 break;
600 return FALSE;
603 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
605 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
606 char buf[256];
607 DWORD i, pos;
609 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
610 PSDRV_WriteSpool(dev, buf, strlen(buf));
612 if (physDev->pen.dash_len)
614 for (i = pos = 0; i < physDev->pen.dash_len; i++)
615 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
616 buf[0] = '[';
617 sprintf(buf + pos, "] %u setdash\n", 0);
619 else
620 sprintf(buf, "[] %u setdash\n", 0);
622 PSDRV_WriteSpool(dev, buf, strlen(buf));
624 return TRUE;
627 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
629 char buf[128];
630 int l;
632 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
634 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
635 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
636 return FALSE;
639 PSDRV_WriteSpool(dev, buf, l);
640 return TRUE;
643 BOOL PSDRV_WriteFill(PHYSDEV dev)
645 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
648 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
650 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
653 BOOL PSDRV_WriteGSave(PHYSDEV dev)
655 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
658 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
660 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
663 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
665 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
668 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
670 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
673 BOOL PSDRV_WriteClip(PHYSDEV dev)
675 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
678 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
680 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
683 BOOL PSDRV_WriteHatch(PHYSDEV dev)
685 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
688 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
690 char buf[256];
692 push_lc_numeric("C");
693 sprintf(buf, psrotate, ang);
694 pop_lc_numeric();
695 return PSDRV_WriteSpool(dev, buf, strlen(buf));
698 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
700 char buf[256];
701 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
702 return PSDRV_WriteSpool(dev, buf, strlen(buf));
705 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
707 static const char buf[] = ">\n] setcolorspace\n";
708 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
711 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
713 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
714 int i;
716 ptr = buf;
717 for(i = 0; i < number; i++) {
718 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
719 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
720 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
721 ptr += 7;
723 PSDRV_WriteSpool(dev, buf, number * 7);
724 HeapFree( GetProcessHeap(), 0, buf );
725 return TRUE;
728 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
730 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
731 int i;
733 ptr = buf;
734 for(i = 0; i < number; i++, rgb++)
735 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
736 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
738 PSDRV_WriteSpool(dev, buf, ptr - buf);
739 HeapFree( GetProcessHeap(), 0, buf );
740 return TRUE;
743 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth,
744 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
746 static const char start[] = "<<\n"
747 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
748 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
750 static const char decode1[] = " /Decode [0 %d]\n";
751 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
753 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
754 static const char endbits[] = " /DataSource <%s>\n>>\n";
755 char buf[1000];
757 if (top_down)
758 sprintf(buf, start, widthSrc, heightSrc,
759 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
760 else
761 sprintf(buf, start, widthSrc, heightSrc,
762 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
764 PSDRV_WriteSpool(dev, buf, strlen(buf));
766 switch(depth) {
767 case 8:
768 sprintf(buf, decode1, 255);
769 break;
771 case 4:
772 sprintf(buf, decode1, 15);
773 break;
775 case 1:
776 sprintf(buf, decode1, 1);
777 break;
779 default:
780 strcpy(buf, decode3);
781 break;
784 PSDRV_WriteSpool(dev, buf, strlen(buf));
786 if(!bits) {
787 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
788 } else {
789 sprintf(buf, endbits, bits);
790 PSDRV_WriteSpool(dev, buf, strlen(buf));
793 return TRUE;
796 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, INT xDst, INT yDst,
797 INT widthDst, INT heightDst, INT widthSrc,
798 INT heightSrc, BOOL mask, BOOL top_down)
800 static const char start[] = "%d %d translate\n%d %d scale\n";
801 static const char image[] = "image\n";
802 static const char imagemask[] = "imagemask\n";
803 char buf[100];
805 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
806 PSDRV_WriteSpool(dev, buf, strlen(buf));
807 PSDRV_WriteImageDict(dev, depth, widthSrc, heightSrc, NULL, top_down);
808 if(mask)
809 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
810 else
811 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
812 return TRUE;
816 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
818 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
819 char *ptr;
820 unsigned int i;
822 ptr = buf;
824 for(i = 0; i < number; i++) {
825 sprintf(ptr, "%02x", bytes[i]);
826 ptr += 2;
827 if(((i & 0xf) == 0xf) || (i == number - 1)) {
828 strcpy(ptr, "\n");
829 ptr++;
832 PSDRV_WriteSpool(dev, buf, ptr - buf);
833 HeapFree( GetProcessHeap(), 0, buf );
834 return TRUE;
837 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
839 int num, num_left = number;
841 do {
842 num = min(num_left, 60);
843 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
844 PSDRV_WriteSpool(dev, "\n", 1);
845 data += num;
846 num_left -= num;
847 } while(num_left);
849 return TRUE;
852 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
854 char buf[100];
856 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
857 return PSDRV_WriteSpool(dev, buf, strlen(buf));
860 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
862 char buf[100];
864 sprintf(buf, psarraydef, pszArrayName, nSize);
865 return PSDRV_WriteSpool(dev, buf, strlen(buf));
868 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
870 char buf[100];
872 sprintf(buf, psrectclip, x, y, w, h);
873 return PSDRV_WriteSpool(dev, buf, strlen(buf));
876 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
878 char buf[100];
880 sprintf(buf, psrectclip2, pszArrayName);
881 return PSDRV_WriteSpool(dev, buf, strlen(buf));
884 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
886 static const char mypat[] = "/mypat\n";
887 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
888 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
889 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
890 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
891 char *buf, *ptr;
892 INT w, h, x, y, w_mult, h_mult;
893 COLORREF map[2];
895 TRACE( "size %dx%dx%d\n",
896 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
898 if(bmi->bmiHeader.biBitCount != 1) {
899 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
900 return FALSE;
903 w = bmi->bmiHeader.biWidth & ~0x7;
904 h = bmi->bmiHeader.biHeight & ~0x7;
906 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
907 ptr = buf;
908 for(y = h-1; y >= 0; y--) {
909 for(x = 0; x < w/8; x++) {
910 sprintf(ptr, "%02x", *(bits + x/8 + y *
911 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
912 ptr += 2;
915 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
916 PSDRV_WriteImageDict(dev, 1, 8, 8, buf, FALSE);
917 PSDRV_WriteSpool(dev, "def\n", 4);
919 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
920 map[0] = GetTextColor( dev->hdc );
921 map[1] = GetBkColor( dev->hdc );
922 PSDRV_WriteRGB(dev, map, 2);
923 PSDRV_WriteIndexColorSpaceEnd(dev);
925 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
926 w_mult = (physDev->logPixelsX + 150) / 300;
927 h_mult = (physDev->logPixelsY + 150) / 300;
928 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
929 PSDRV_WriteSpool(dev, buf, strlen(buf));
930 HeapFree( GetProcessHeap(), 0, buf );
931 return TRUE;