po: Update Japanese translation.
[wine/multimedia.git] / dlls / wineps.drv / ps.c
blob2e7597a7fe2b1ebaef7ca676186ba2da2256cf75
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 */
52 static const char psheader[] = /* title llx lly urx ury */
53 "%%%%Creator: Wine PostScript Driver\n"
54 "%%%%Title: %s\n"
55 "%%%%BoundingBox: %d %d %d %d\n"
56 "%%%%Pages: (atend)\n"
57 "%%%%EndComments\n";
59 static const char psbeginprolog[] =
60 "%%BeginProlog\n";
62 static const char psendprolog[] =
63 "%%EndProlog\n";
65 static const char psprolog[] =
66 "/tmpmtrx matrix def\n"
67 "/hatch {\n"
68 " pathbbox\n"
69 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
70 " l cvi gap idiv gap mul\n"
71 " gap\n"
72 " r cvi gap idiv gap mul\n"
73 " {t moveto 0 b t sub rlineto}\n"
74 " for\n"
75 "} bind def\n"
76 "/B {pop pop pop pop} def\n"
77 "/N {newpath} def\n"
78 "/havetype42gdir {version cvi 2015 ge} bind def\n";
80 static const char psbeginsetup[] =
81 "%%BeginSetup\n";
83 static const char psendsetup[] =
84 "%%EndSetup\n";
86 static const char psbeginfeature[] = /* feature, value */
87 "mark {\n"
88 "%%%%BeginFeature: %s %s\n";
90 static const char psendfeature[] =
91 "\n%%EndFeature\n"
92 "} stopped cleartomark\n";
94 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
95 "%%%%Page: %s %d\n"
96 "%%%%BeginPageSetup\n"
97 "/pgsave save def\n"
98 "72 %d div 72 %d div scale\n"
99 "%d %d translate\n"
100 "1 -1 scale\n"
101 "%d rotate\n"
102 "%%%%EndPageSetup\n";
104 static const char psendpage[] =
105 "pgsave restore\n"
106 "showpage\n";
108 static const char psfooter[] = /* pages */
109 "%%%%Trailer\n"
110 "%%%%Pages: %d\n"
111 "%%%%EOF\n";
113 static const char psmoveto[] = /* x, y */
114 "%d %d moveto\n";
116 static const char pslineto[] = /* x, y */
117 "%d %d lineto\n";
119 static const char psstroke[] =
120 "stroke\n";
122 static const char psrectangle[] = /* x, y, width, height, -width */
123 "%d %d moveto\n"
124 "%d 0 rlineto\n"
125 "0 %d rlineto\n"
126 "%d 0 rlineto\n"
127 "closepath\n";
129 static const char psglyphshow[] = /* glyph name */
130 "/%s glyphshow\n";
132 static const char pssetfont[] = /* fontname, xx_scale, xy_scale, yx_scale, yy_scale, escapement */
133 "/%s findfont\n"
134 "[%d %d %d %d 0 0]\n"
135 "%d 10 div matrix rotate\n"
136 "matrix concatmatrix\n"
137 "makefont setfont\n";
139 static const char pssetline[] = /* width, join, endcap */
140 "%d setlinewidth %u setlinejoin %u setlinecap\n";
142 static const char pssetgray[] = /* gray */
143 "%.2f setgray\n";
145 static const char pssetrgbcolor[] = /* r, g, b */
146 "%.2f %.2f %.2f setrgbcolor\n";
148 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
149 "tmpmtrx currentmatrix pop\n"
150 "%d %d translate\n"
151 "%d %d scale\n"
152 "0 0 0.5 %.1f %.1f arc\n"
153 "tmpmtrx setmatrix\n";
155 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
156 "%d %d %d %d %d %d curveto\n";
158 static const char psgsave[] =
159 "gsave\n";
161 static const char psgrestore[] =
162 "grestore\n";
164 static const char psfill[] =
165 "fill\n";
167 static const char pseofill[] =
168 "eofill\n";
170 static const char psnewpath[] =
171 "newpath\n";
173 static const char psclosepath[] =
174 "closepath\n";
176 static const char psclip[] =
177 "clip\n";
179 static const char pseoclip[] =
180 "eoclip\n";
182 static const char psrectclip[] =
183 "%d %d %d %d rectclip\n";
185 static const char psrectclip2[] =
186 "%s rectclip\n";
188 static const char pshatch[] =
189 "hatch\n";
191 static const char psrotate[] = /* ang */
192 "%.1f rotate\n";
194 static const char psarrayput[] =
195 "%s %d %d put\n";
197 static const char psarraydef[] =
198 "/%s %d array def\n";
200 static const char psenddocument[] =
201 "\n%%EndDocument\n";
203 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
205 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
206 int num, num_left = cch;
208 if(physDev->job.quiet) {
209 TRACE("ignoring output\n");
210 return 0;
213 if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
214 write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
215 physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
218 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
219 if( !PSDRV_StartPage(dev) )
220 return 0;
223 do {
224 num = min(num_left, 0x8000);
225 if(write_spool( dev, lpData, num ) != num)
226 return 0;
227 lpData += num;
228 num_left -= num;
229 } while(num_left);
231 return cch;
235 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
238 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
239 strlen(feature) + strlen(value));
241 sprintf(buf, psbeginfeature, feature, value);
242 write_spool( dev, buf, strlen(buf) );
243 write_spool( dev, invocation, strlen(invocation) );
244 write_spool( dev, psendfeature, strlen(psendfeature) );
246 HeapFree( GetProcessHeap(), 0, buf );
247 return 1;
250 /********************************************************
251 * escape_title
253 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
254 * as octal. If we've had to use an escape then surround the entire string
255 * in brackets. Truncate string to represent at most 0x80 characters.
258 static char *escape_title(LPCWSTR wstr)
260 char *ret, *cp, *str;
261 int i, extra = 0;
263 if(!wstr)
265 ret = HeapAlloc(GetProcessHeap(), 0, 1);
266 *ret = '\0';
267 return ret;
270 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
271 str = HeapAlloc( GetProcessHeap(), 0, i );
272 if (!str) return NULL;
273 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
275 for(i = 0; i < 0x80 && str[i]; i++)
277 if(!isprint(str[i]))
278 extra += 3;
281 if(!extra)
283 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
284 memcpy(ret, str, i);
285 ret[i] = '\0';
286 goto done;
289 extra += 2; /* two for the brackets */
290 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
291 *cp++ = '(';
292 for(i = 0; i < 0x80 && str[i]; i++)
294 if(!isprint(str[i]))
296 BYTE b = (BYTE)str[i];
297 *cp++ = '\\';
298 *cp++ = ((b >> 6) & 0x7) + '0';
299 *cp++ = ((b >> 3) & 0x7) + '0';
300 *cp++ = ((b) & 0x7) + '0';
302 else
303 *cp++ = str[i];
305 *cp++ = ')';
306 *cp = '\0';
308 done:
309 HeapFree( GetProcessHeap(), 0, str );
310 return ret;
313 struct ticket_info
315 PAGESIZE *page;
316 DUPLEX *duplex;
319 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
321 char buf[256];
322 int len;
324 if (info->page && info->page->InvocationString)
326 len = sizeof(media) + strlen( info->page->Name ) + 1;
327 if (len <= sizeof(buf))
329 memcpy( buf, media, sizeof(media) );
330 strcat( buf, info->page->Name );
331 strcat( buf, "\n");
332 write_spool( dev, buf, len - 1 );
334 else
335 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
338 if (info->duplex && info->duplex->InvocationString)
340 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
342 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
343 write_spool( dev, str, strlen( str ) );
348 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
350 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
351 char *buf, *escaped_title;
352 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
353 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
354 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
355 int llx, lly, urx, ury;
356 int ret, len;
357 struct ticket_info ticket_info = { page, duplex };
359 TRACE("%s\n", debugstr_w(title));
361 len = strlen( psadobe );
362 ret = write_spool( dev, psadobe, len );
363 if (ret != len)
365 WARN("WriteSpool error\n");
366 return 0;
369 write_cups_job_ticket( dev, &ticket_info );
371 escaped_title = escape_title(title);
372 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
373 strlen(escaped_title) + 30 );
374 if(!buf) {
375 WARN("HeapAlloc failed\n");
376 return 0;
379 /* BBox co-ords are in default user co-ord system so urx < ury even in
380 landscape mode */
381 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
382 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
383 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
384 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
385 /* FIXME should do something better with BBox */
387 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
389 HeapFree(GetProcessHeap(), 0, escaped_title);
391 len = strlen( buf );
392 write_spool( dev, buf, len );
393 HeapFree( GetProcessHeap(), 0, buf );
395 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
396 write_spool( dev, psprolog, strlen(psprolog) );
397 write_spool( dev, psendprolog, strlen(psendprolog) );
398 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
400 if(physDev->Devmode->dmPublic.u1.s1.dmCopies > 1) {
401 char copies_buf[100];
402 sprintf(copies_buf, "mark {\n << /NumCopies %d >> setpagedevice\n} stopped cleartomark\n", physDev->Devmode->dmPublic.u1.s1.dmCopies);
403 write_spool(dev, copies_buf, strlen(copies_buf));
406 if (slot && slot->InvocationString)
407 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
409 if (page && page->InvocationString)
410 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
412 if (duplex && duplex->InvocationString)
413 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
415 write_spool( dev, psendsetup, strlen(psendsetup) );
418 return 1;
422 INT PSDRV_WriteFooter( PHYSDEV dev )
424 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
425 char *buf;
426 int ret = 1;
428 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
429 if(!buf) {
430 WARN("HeapAlloc failed\n");
431 return 0;
434 sprintf(buf, psfooter, physDev->job.PageNo);
436 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
437 WARN("WriteSpool error\n");
438 ret = 0;
440 HeapFree( GetProcessHeap(), 0, buf );
441 return ret;
446 INT PSDRV_WriteEndPage( PHYSDEV dev )
448 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
449 WARN("WriteSpool error\n");
450 return 0;
452 return 1;
458 INT PSDRV_WriteNewPage( PHYSDEV dev )
460 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
461 char *buf;
462 char name[100];
463 signed int xtrans, ytrans, rotation;
464 int ret = 1;
466 sprintf(name, "%d", physDev->job.PageNo);
468 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
469 if(!buf) {
470 WARN("HeapAlloc failed\n");
471 return 0;
474 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
475 if(physDev->pi->ppd->LandscapeOrientation == -90) {
476 xtrans = physDev->ImageableArea.right;
477 ytrans = physDev->ImageableArea.top;
478 rotation = 90;
479 } else {
480 xtrans = physDev->ImageableArea.left;
481 ytrans = physDev->ImageableArea.bottom;
482 rotation = -90;
484 } else {
485 xtrans = physDev->ImageableArea.left;
486 ytrans = physDev->ImageableArea.top;
487 rotation = 0;
490 sprintf(buf, psnewpage, name, physDev->job.PageNo,
491 physDev->logPixelsX, physDev->logPixelsY,
492 xtrans, ytrans, rotation);
494 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
495 WARN("WriteSpool error\n");
496 ret = 0;
498 HeapFree( GetProcessHeap(), 0, buf );
499 return ret;
503 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
505 char buf[100];
507 sprintf(buf, psmoveto, x, y);
508 return PSDRV_WriteSpool(dev, buf, strlen(buf));
511 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
513 char buf[100];
515 sprintf(buf, pslineto, x, y);
516 return PSDRV_WriteSpool(dev, buf, strlen(buf));
520 BOOL PSDRV_WriteStroke(PHYSDEV dev)
522 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
527 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
528 INT height)
530 char buf[100];
532 sprintf(buf, psrectangle, x, y, width, height, -width);
533 return PSDRV_WriteSpool(dev, buf, strlen(buf));
536 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
537 double ang2)
539 char buf[256];
541 /* Make angles -ve and swap order because we're working with an upside
542 down y-axis */
543 push_lc_numeric("C");
544 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
545 pop_lc_numeric();
546 return PSDRV_WriteSpool(dev, buf, strlen(buf));
549 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
551 char buf[256];
553 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
554 return PSDRV_WriteSpool(dev, buf, strlen(buf));
558 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement)
560 char *buf;
562 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(pssetfont) + strlen(name) + 40 );
564 if(!buf) {
565 WARN("HeapAlloc failed\n");
566 return FALSE;
569 sprintf(buf, pssetfont, name, size.xx, size.xy, size.yx, size.yy, -escapement);
571 PSDRV_WriteSpool(dev, buf, strlen(buf));
572 HeapFree( GetProcessHeap(), 0, buf );
573 return TRUE;
576 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
578 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
579 char buf[256];
581 PSDRV_CopyColor(&physDev->inkColor, color);
582 switch(color->type) {
583 case PSCOLOR_RGB:
584 push_lc_numeric("C");
585 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
586 color->value.rgb.b);
587 pop_lc_numeric();
588 return PSDRV_WriteSpool(dev, buf, strlen(buf));
590 case PSCOLOR_GRAY:
591 push_lc_numeric("C");
592 sprintf(buf, pssetgray, color->value.gray.i);
593 pop_lc_numeric();
594 return PSDRV_WriteSpool(dev, buf, strlen(buf));
596 default:
597 ERR("Unknown colour type %d\n", color->type);
598 break;
601 return FALSE;
604 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
606 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
607 char buf[256];
608 DWORD i, pos;
610 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
611 PSDRV_WriteSpool(dev, buf, strlen(buf));
613 if (physDev->pen.dash_len)
615 for (i = pos = 0; i < physDev->pen.dash_len; i++)
616 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
617 buf[0] = '[';
618 sprintf(buf + pos, "] %u setdash\n", 0);
620 else
621 sprintf(buf, "[] %u setdash\n", 0);
623 PSDRV_WriteSpool(dev, buf, strlen(buf));
625 return TRUE;
628 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
630 char buf[128];
631 int l;
633 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
635 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
636 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
637 return FALSE;
640 PSDRV_WriteSpool(dev, buf, l);
641 return TRUE;
644 BOOL PSDRV_WriteFill(PHYSDEV dev)
646 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
649 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
651 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
654 BOOL PSDRV_WriteGSave(PHYSDEV dev)
656 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
659 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
661 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
664 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
666 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
669 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
671 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
674 BOOL PSDRV_WriteClip(PHYSDEV dev)
676 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
679 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
681 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
684 BOOL PSDRV_WriteHatch(PHYSDEV dev)
686 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
689 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
691 char buf[256];
693 push_lc_numeric("C");
694 sprintf(buf, psrotate, ang);
695 pop_lc_numeric();
696 return PSDRV_WriteSpool(dev, buf, strlen(buf));
699 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
701 char buf[256];
702 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
703 return PSDRV_WriteSpool(dev, buf, strlen(buf));
706 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
708 static const char buf[] = ">\n] setcolorspace\n";
709 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
712 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
714 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
715 int i;
717 ptr = buf;
718 for(i = 0; i < number; i++) {
719 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
720 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
721 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
722 ptr += 7;
724 PSDRV_WriteSpool(dev, buf, number * 7);
725 HeapFree( GetProcessHeap(), 0, buf );
726 return TRUE;
729 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
731 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
732 int i;
734 ptr = buf;
735 for(i = 0; i < number; i++, rgb++)
736 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
737 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
739 PSDRV_WriteSpool(dev, buf, ptr - buf);
740 HeapFree( GetProcessHeap(), 0, buf );
741 return TRUE;
744 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth,
745 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
747 static const char start[] = "<<\n"
748 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
749 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
751 static const char decode1[] = " /Decode [0 %d]\n";
752 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
754 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
755 static const char endbits[] = " /DataSource <%s>\n>>\n";
756 char buf[1000];
758 if (top_down)
759 sprintf(buf, start, widthSrc, heightSrc,
760 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
761 else
762 sprintf(buf, start, widthSrc, heightSrc,
763 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
765 PSDRV_WriteSpool(dev, buf, strlen(buf));
767 switch(depth) {
768 case 8:
769 sprintf(buf, decode1, 255);
770 break;
772 case 4:
773 sprintf(buf, decode1, 15);
774 break;
776 case 1:
777 sprintf(buf, decode1, 1);
778 break;
780 default:
781 strcpy(buf, decode3);
782 break;
785 PSDRV_WriteSpool(dev, buf, strlen(buf));
787 if(!bits) {
788 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
789 } else {
790 sprintf(buf, endbits, bits);
791 PSDRV_WriteSpool(dev, buf, strlen(buf));
794 return TRUE;
797 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, INT xDst, INT yDst,
798 INT widthDst, INT heightDst, INT widthSrc,
799 INT heightSrc, BOOL mask, BOOL top_down)
801 static const char start[] = "%d %d translate\n%d %d scale\n";
802 static const char image[] = "image\n";
803 static const char imagemask[] = "imagemask\n";
804 char buf[100];
806 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
807 PSDRV_WriteSpool(dev, buf, strlen(buf));
808 PSDRV_WriteImageDict(dev, depth, widthSrc, heightSrc, NULL, top_down);
809 if(mask)
810 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
811 else
812 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
813 return TRUE;
817 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
819 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
820 char *ptr;
821 unsigned int i;
823 ptr = buf;
825 for(i = 0; i < number; i++) {
826 sprintf(ptr, "%02x", bytes[i]);
827 ptr += 2;
828 if(((i & 0xf) == 0xf) || (i == number - 1)) {
829 strcpy(ptr, "\n");
830 ptr++;
833 PSDRV_WriteSpool(dev, buf, ptr - buf);
834 HeapFree( GetProcessHeap(), 0, buf );
835 return TRUE;
838 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
840 int num, num_left = number;
842 do {
843 num = min(num_left, 60);
844 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
845 PSDRV_WriteSpool(dev, "\n", 1);
846 data += num;
847 num_left -= num;
848 } while(num_left);
850 return TRUE;
853 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
855 char buf[100];
857 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
858 return PSDRV_WriteSpool(dev, buf, strlen(buf));
861 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
863 char buf[100];
865 sprintf(buf, psarraydef, pszArrayName, nSize);
866 return PSDRV_WriteSpool(dev, buf, strlen(buf));
869 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
871 char buf[100];
873 sprintf(buf, psrectclip, x, y, w, h);
874 return PSDRV_WriteSpool(dev, buf, strlen(buf));
877 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
879 char buf[100];
881 sprintf(buf, psrectclip2, pszArrayName);
882 return PSDRV_WriteSpool(dev, buf, strlen(buf));
885 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
887 static const char mypat[] = "/mypat\n";
888 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
889 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
890 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
891 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
892 char *buf, *ptr;
893 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
894 COLORREF map[2];
896 TRACE( "size %dx%dx%d\n",
897 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
899 if(bmi->bmiHeader.biBitCount != 1) {
900 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
901 return FALSE;
904 w = bmi->bmiHeader.biWidth & ~0x7;
905 h = abs_height & ~0x7;
907 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
908 ptr = buf;
909 for(y = h-1; y >= 0; y--) {
910 for(x = 0; x < w/8; x++) {
911 sprintf(ptr, "%02x", *(bits + x/8 + y *
912 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
913 ptr += 2;
916 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
917 PSDRV_WriteImageDict(dev, 1, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
918 PSDRV_WriteSpool(dev, "def\n", 4);
920 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
921 map[0] = GetTextColor( dev->hdc );
922 map[1] = GetBkColor( dev->hdc );
923 PSDRV_WriteRGB(dev, map, 2);
924 PSDRV_WriteIndexColorSpaceEnd(dev);
926 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
927 w_mult = (physDev->logPixelsX + 150) / 300;
928 h_mult = (physDev->logPixelsY + 150) / 300;
929 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
930 PSDRV_WriteSpool(dev, buf, strlen(buf));
931 HeapFree( GetProcessHeap(), 0, buf );
932 return TRUE;