winmm: Avoid explicitly casting the pointer returned from Heap(Re)Alloc.
[wine.git] / dlls / wineps.drv / ps.c
bloba8050abe19b8ff9c630418639ed725b0d6b000ef
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 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.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 */
50 static const char cups_collate_false[] = "%cupsJobTicket: collate=false\n";
51 static const char cups_collate_true[] = "%cupsJobTicket: collate=true\n";
52 static const char cups_ap_d_inputslot[] = "%cupsJobTicket: AP_D_InputSlot=\n"; /* intentionally empty value */
54 static const char psheader[] = /* title llx lly urx ury orientation */
55 "%%%%Creator: Wine PostScript Driver\n"
56 "%%%%Title: %s\n"
57 "%%%%BoundingBox: %d %d %d %d\n"
58 "%%%%Pages: (atend)\n"
59 "%%%%Orientation: %s\n"
60 "%%%%EndComments\n";
62 static const char psbeginprolog[] =
63 "%%BeginProlog\n";
65 static const char psendprolog[] =
66 "%%EndProlog\n";
68 static const char psprolog[] =
69 "/tmpmtrx matrix def\n"
70 "/hatch {\n"
71 " pathbbox\n"
72 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
73 " l cvi gap idiv gap mul\n"
74 " gap\n"
75 " r cvi gap idiv gap mul\n"
76 " {t moveto 0 b t sub rlineto}\n"
77 " for\n"
78 "} bind def\n"
79 "/B {pop pop pop pop} def\n"
80 "/N {newpath} def\n"
81 "/havetype42gdir {version cvi 2015 ge} bind def\n";
83 static const char psbeginsetup[] =
84 "%%BeginSetup\n";
86 static const char psendsetup[] =
87 "%%EndSetup\n";
89 static const char psbeginfeature[] = /* feature, value */
90 "mark {\n"
91 "%%%%BeginFeature: %s %s\n";
93 static const char psendfeature[] =
94 "\n%%EndFeature\n"
95 "} stopped cleartomark\n";
97 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
98 "%%%%Page: %s %d\n"
99 "%%%%BeginPageSetup\n"
100 "/pgsave save def\n"
101 "72 %d div 72 %d div scale\n"
102 "%d %d translate\n"
103 "1 -1 scale\n"
104 "%d rotate\n"
105 "%%%%EndPageSetup\n";
107 static const char psendpage[] =
108 "pgsave restore\n"
109 "showpage\n";
111 static const char psfooter[] = /* pages */
112 "%%%%Trailer\n"
113 "%%%%Pages: %d\n"
114 "%%%%EOF\n";
116 static const char psmoveto[] = /* x, y */
117 "%d %d moveto\n";
119 static const char pslineto[] = /* x, y */
120 "%d %d lineto\n";
122 static const char psstroke[] =
123 "stroke\n";
125 static const char psrectangle[] = /* x, y, width, height, -width */
126 "%d %d moveto\n"
127 "%d 0 rlineto\n"
128 "0 %d rlineto\n"
129 "%d 0 rlineto\n"
130 "closepath\n";
132 static const char psglyphshow[] = /* glyph name */
133 "/%s glyphshow\n";
135 static const char psfindfont[] = /* fontname */
136 "/%s findfont\n";
138 static const char psfakeitalic[] =
139 "[1 0 0.25 1 0 0]\n";
141 static const char pssizematrix[] =
142 "[%d %d %d %d 0 0]\n";
144 static const char psconcat[] =
145 "matrix concatmatrix\n";
147 static const char psrotatefont[] = /* escapement */
148 "%d 10 div matrix rotate\n"
149 "matrix concatmatrix\n";
151 static const char pssetfont[] =
152 "makefont setfont\n";
154 static const char pssetline[] = /* width, join, endcap */
155 "%d setlinewidth %u setlinejoin %u setlinecap\n";
157 static const char pssetgray[] = /* gray */
158 "%.2f setgray\n";
160 static const char pssetrgbcolor[] = /* r, g, b */
161 "%.2f %.2f %.2f setrgbcolor\n";
163 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
164 "tmpmtrx currentmatrix pop\n"
165 "%d %d translate\n"
166 "%d %d scale\n"
167 "0 0 0.5 %.1f %.1f arc\n"
168 "tmpmtrx setmatrix\n";
170 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
171 "%ld %ld %ld %ld %ld %ld curveto\n";
173 static const char psgsave[] =
174 "gsave\n";
176 static const char psgrestore[] =
177 "grestore\n";
179 static const char psfill[] =
180 "fill\n";
182 static const char pseofill[] =
183 "eofill\n";
185 static const char psnewpath[] =
186 "newpath\n";
188 static const char psclosepath[] =
189 "closepath\n";
191 static const char psclip[] =
192 "clip\n";
194 static const char pseoclip[] =
195 "eoclip\n";
197 static const char psrectclip[] =
198 "%d %d %d %d rectclip\n";
200 static const char psrectclip2[] =
201 "%s rectclip\n";
203 static const char pshatch[] =
204 "hatch\n";
206 static const char psrotate[] = /* ang */
207 "%.1f rotate\n";
209 static const char psarrayput[] =
210 "%s %d %ld put\n";
212 static const char psarraydef[] =
213 "/%s %d array def\n";
215 static const char psbegindocument[] =
216 "%%BeginDocument: Wine passthrough\n";
217 static const char psenddocument[] =
218 "\n%%EndDocument\n";
220 void passthrough_enter(print_ctx *ctx)
222 if (ctx->job.passthrough_state != passthrough_none) return;
224 write_spool(ctx, psbegindocument, sizeof(psbegindocument) - 1);
225 ctx->job.passthrough_state = passthrough_active;
228 void passthrough_leave(print_ctx *ctx)
230 if (ctx->job.passthrough_state == passthrough_none) return;
232 write_spool(ctx, psenddocument, sizeof(psenddocument) - 1);
233 ctx->job.passthrough_state = passthrough_none;
236 DWORD PSDRV_WriteSpool(print_ctx *ctx, LPCSTR lpData, DWORD cch)
238 int num, num_left = cch;
240 if(ctx->job.quiet) {
241 TRACE("ignoring output\n");
242 return 0;
245 passthrough_leave(ctx);
247 if(ctx->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
248 if( !PSDRV_StartPage(ctx) )
249 return 0;
252 do {
253 num = min(num_left, 0x8000);
254 if(write_spool( ctx, lpData, num ) != num)
255 return 0;
256 lpData += num;
257 num_left -= num;
258 } while(num_left);
260 return cch;
264 static INT PSDRV_WriteFeature(print_ctx *ctx, LPCSTR feature, LPCSTR value, LPCSTR invocation)
267 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
268 strlen(feature) + strlen(value));
270 sprintf(buf, psbeginfeature, feature, value);
271 write_spool( ctx, buf, strlen(buf) );
272 write_spool( ctx, invocation, strlen(invocation) );
273 write_spool( ctx, psendfeature, strlen(psendfeature) );
275 HeapFree( GetProcessHeap(), 0, buf );
276 return 1;
279 /********************************************************
280 * escape_title
282 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
283 * as octal. If we've had to use an escape then surround the entire string
284 * in brackets. Truncate string to represent at most 0x80 characters.
287 static char *escape_title(LPCWSTR wstr)
289 char *ret, *cp, *str;
290 int i, extra = 0;
292 if(!wstr)
294 ret = HeapAlloc(GetProcessHeap(), 0, 1);
295 *ret = '\0';
296 return ret;
299 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
300 str = HeapAlloc( GetProcessHeap(), 0, i );
301 if (!str) return NULL;
302 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
304 for(i = 0; i < 0x80 && str[i]; i++)
306 if(!isprint(str[i]))
307 extra += 3;
310 if(!extra)
312 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
313 memcpy(ret, str, i);
314 ret[i] = '\0';
315 goto done;
318 extra += 2; /* two for the brackets */
319 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
320 *cp++ = '(';
321 for(i = 0; i < 0x80 && str[i]; i++)
323 if(!isprint(str[i]))
325 BYTE b = (BYTE)str[i];
326 *cp++ = '\\';
327 *cp++ = ((b >> 6) & 0x7) + '0';
328 *cp++ = ((b >> 3) & 0x7) + '0';
329 *cp++ = ((b) & 0x7) + '0';
331 else
332 *cp++ = str[i];
334 *cp++ = ')';
335 *cp = '\0';
337 done:
338 HeapFree( GetProcessHeap(), 0, str );
339 return ret;
342 struct ticket_info
344 PAGESIZE *page;
345 DUPLEX *duplex;
348 static void write_cups_job_ticket( print_ctx *ctx, const struct ticket_info *info )
350 char buf[256];
351 int len;
353 if (info->page && info->page->InvocationString)
355 len = sizeof(media) + strlen( info->page->Name ) + 1;
356 if (len <= sizeof(buf))
358 memcpy( buf, media, sizeof(media) );
359 strcat( buf, info->page->Name );
360 strcat( buf, "\n");
361 write_spool( ctx, buf, len - 1 );
363 else
364 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
367 if (info->duplex && info->duplex->InvocationString)
369 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
371 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
372 write_spool( ctx, str, strlen( str ) );
376 if (ctx->Devmode->dmPublic.dmCopies > 1)
378 len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
379 ctx->Devmode->dmPublic.dmCopies );
380 if (len > 0 && len < sizeof(buf))
381 write_spool( ctx, buf, len );
383 if (ctx->Devmode->dmPublic.dmFields & DM_COLLATE)
385 if (ctx->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
386 write_spool( ctx, cups_collate_false, sizeof(cups_collate_false) - 1 );
387 else if (ctx->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
388 write_spool( ctx, cups_collate_true, sizeof(cups_collate_true) - 1 );
392 if (!(ctx->Devmode->dmPublic.dmFields & DM_DEFAULTSOURCE) ||
393 ctx->Devmode->dmPublic.dmDefaultSource == DMBIN_AUTO)
394 write_spool( ctx, cups_ap_d_inputslot, sizeof(cups_ap_d_inputslot) - 1 );
397 INT PSDRV_WriteHeader( print_ctx *ctx, LPCWSTR title )
399 char *buf, *escaped_title;
400 INPUTSLOT *slot = find_slot( ctx->pi->ppd, &ctx->Devmode->dmPublic );
401 PAGESIZE *page = find_pagesize( ctx->pi->ppd, &ctx->Devmode->dmPublic );
402 DUPLEX *duplex = find_duplex( ctx->pi->ppd, &ctx->Devmode->dmPublic );
403 int llx, lly, urx, ury;
404 int ret, len;
405 const char * dmOrientation;
407 struct ticket_info ticket_info = { page, duplex };
409 TRACE("%s\n", debugstr_w(title));
411 len = strlen( psadobe );
412 ret = write_spool( ctx, psadobe, len );
413 if (ret != len)
415 WARN("WriteSpool error\n");
416 return 0;
419 write_cups_job_ticket( ctx, &ticket_info );
421 escaped_title = escape_title(title);
422 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
423 strlen(escaped_title) + 30 );
424 if(!buf) {
425 WARN("HeapAlloc failed\n");
426 HeapFree(GetProcessHeap(), 0, escaped_title);
427 return 0;
430 /* BBox co-ords are in default user co-ord system so urx < ury even in
431 landscape mode */
432 if ((ctx->Devmode->dmPublic.dmFields & DM_PAPERSIZE) && page)
434 if (page->ImageableArea)
436 llx = page->ImageableArea->llx;
437 lly = page->ImageableArea->lly;
438 urx = page->ImageableArea->urx;
439 ury = page->ImageableArea->ury;
441 else
443 llx = lly = 0;
444 urx = page->PaperDimension->x;
445 ury = page->PaperDimension->y;
448 else if ((ctx->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
449 (ctx->Devmode->dmPublic.dmFields & DM_PAPERWIDTH))
451 /* Devmode sizes in 1/10 mm */
452 llx = lly = 0;
453 urx = ctx->Devmode->dmPublic.dmPaperWidth * 72.0 / 254.0;
454 ury = ctx->Devmode->dmPublic.dmPaperLength * 72.0 / 254.0;
456 else
458 llx = lly = urx = ury = 0;
460 /* FIXME should do something better with BBox */
462 dmOrientation = (ctx->Devmode->dmPublic.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
463 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
465 HeapFree(GetProcessHeap(), 0, escaped_title);
467 len = strlen( buf );
468 write_spool( ctx, buf, len );
469 HeapFree( GetProcessHeap(), 0, buf );
471 write_spool( ctx, psbeginprolog, strlen(psbeginprolog) );
472 write_spool( ctx, psprolog, strlen(psprolog) );
473 write_spool( ctx, psendprolog, strlen(psendprolog) );
474 write_spool( ctx, psbeginsetup, strlen(psbeginsetup) );
476 if (slot && slot->InvocationString)
477 PSDRV_WriteFeature( ctx, "*InputSlot", slot->Name, slot->InvocationString );
479 if (page && page->InvocationString)
480 PSDRV_WriteFeature( ctx, "*PageSize", page->Name, page->InvocationString );
482 if (duplex && duplex->InvocationString)
483 PSDRV_WriteFeature( ctx, "*Duplex", duplex->Name, duplex->InvocationString );
485 write_spool( ctx, psendsetup, strlen(psendsetup) );
488 return 1;
492 INT PSDRV_WriteFooter( print_ctx *ctx )
494 char *buf;
495 int ret = 1;
497 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
498 if(!buf) {
499 WARN("HeapAlloc failed\n");
500 return 0;
503 sprintf(buf, psfooter, ctx->job.PageNo);
505 if( write_spool( ctx, buf, strlen(buf) ) != strlen(buf) ) {
506 WARN("WriteSpool error\n");
507 ret = 0;
509 HeapFree( GetProcessHeap(), 0, buf );
510 return ret;
515 INT PSDRV_WriteEndPage( print_ctx *ctx )
517 if( write_spool( ctx, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
518 WARN("WriteSpool error\n");
519 return 0;
521 return 1;
527 INT PSDRV_WriteNewPage( print_ctx *ctx )
529 char *buf;
530 char name[100];
531 signed int xtrans, ytrans, rotation;
532 int ret = 1;
534 sprintf(name, "%d", ctx->job.PageNo);
536 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
537 if(!buf) {
538 WARN("HeapAlloc failed\n");
539 return 0;
542 if(ctx->Devmode->dmPublic.dmOrientation == DMORIENT_LANDSCAPE) {
543 if(ctx->pi->ppd->LandscapeOrientation == -90) {
544 xtrans = GetDeviceCaps(ctx->hdc, PHYSICALHEIGHT) -
545 GetDeviceCaps(ctx->hdc, PHYSICALOFFSETY);
546 ytrans = GetDeviceCaps(ctx->hdc, PHYSICALWIDTH) -
547 GetDeviceCaps(ctx->hdc, PHYSICALOFFSETX);
548 rotation = 90;
549 } else {
550 xtrans = GetDeviceCaps(ctx->hdc, PHYSICALOFFSETY);
551 ytrans = GetDeviceCaps(ctx->hdc, PHYSICALOFFSETX);
552 rotation = -90;
554 } else {
555 xtrans = GetDeviceCaps(ctx->hdc, PHYSICALOFFSETX);
556 ytrans = GetDeviceCaps(ctx->hdc, PHYSICALHEIGHT) -
557 GetDeviceCaps(ctx->hdc, PHYSICALOFFSETY);
558 rotation = 0;
561 sprintf(buf, psnewpage, name, ctx->job.PageNo,
562 GetDeviceCaps(ctx->hdc, ASPECTX), GetDeviceCaps(ctx->hdc, ASPECTY),
563 xtrans, ytrans, rotation);
565 if( write_spool( ctx, buf, strlen(buf) ) != strlen(buf) ) {
566 WARN("WriteSpool error\n");
567 ret = 0;
569 HeapFree( GetProcessHeap(), 0, buf );
570 return ret;
574 BOOL PSDRV_WriteMoveTo(print_ctx *ctx, INT x, INT y)
576 char buf[100];
578 sprintf(buf, psmoveto, x, y);
579 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
582 BOOL PSDRV_WriteLineTo(print_ctx *ctx, INT x, INT y)
584 char buf[100];
586 sprintf(buf, pslineto, x, y);
587 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
591 BOOL PSDRV_WriteStroke(print_ctx *ctx)
593 return PSDRV_WriteSpool(ctx, psstroke, sizeof(psstroke)-1);
598 BOOL PSDRV_WriteRectangle(print_ctx *ctx, INT x, INT y, INT width,
599 INT height)
601 char buf[100];
603 sprintf(buf, psrectangle, x, y, width, height, -width);
604 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
607 BOOL PSDRV_WriteArc(print_ctx *ctx, INT x, INT y, INT w, INT h, double ang1,
608 double ang2)
610 char buf[256];
612 /* Make angles -ve and swap order because we're working with an upside
613 down y-axis */
614 push_lc_numeric("C");
615 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
616 pop_lc_numeric();
617 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
620 BOOL PSDRV_WriteCurveTo(print_ctx *ctx, POINT pts[3])
622 char buf[256];
624 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
625 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
628 BOOL PSDRV_WriteSetFont(print_ctx *ctx, const char *name, matrix size, INT escapement, BOOL fake_italic)
630 char *buf;
632 buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
634 if(!buf) {
635 WARN("HeapAlloc failed\n");
636 return FALSE;
639 sprintf( buf, psfindfont, name );
640 PSDRV_WriteSpool( ctx, buf, strlen(buf) );
642 if (fake_italic) PSDRV_WriteSpool( ctx, psfakeitalic, sizeof(psfakeitalic) - 1 );
644 sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
645 PSDRV_WriteSpool( ctx, buf, strlen(buf) );
647 if (fake_italic) PSDRV_WriteSpool( ctx, psconcat, sizeof(psconcat) - 1 );
649 if (escapement)
651 sprintf( buf, psrotatefont, -escapement );
652 PSDRV_WriteSpool( ctx, buf, strlen(buf) );
655 PSDRV_WriteSpool( ctx, pssetfont, sizeof(pssetfont) - 1 );
656 HeapFree( GetProcessHeap(), 0, buf );
658 return TRUE;
661 BOOL PSDRV_WriteSetColor(print_ctx *ctx, PSCOLOR *color)
663 char buf[256];
665 switch(color->type) {
666 case PSCOLOR_RGB:
667 push_lc_numeric("C");
668 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
669 color->value.rgb.b);
670 pop_lc_numeric();
671 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
673 case PSCOLOR_GRAY:
674 push_lc_numeric("C");
675 sprintf(buf, pssetgray, color->value.gray.i);
676 pop_lc_numeric();
677 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
679 default:
680 ERR("Unknown colour type %d\n", color->type);
681 break;
684 return FALSE;
687 BOOL PSDRV_WriteSetPen(print_ctx *ctx)
689 char buf[256];
690 DWORD i, pos;
692 sprintf(buf, pssetline, ctx->pen.width, ctx->pen.join, ctx->pen.endcap);
693 PSDRV_WriteSpool(ctx, buf, strlen(buf));
695 if (ctx->pen.dash_len)
697 for (i = pos = 0; i < ctx->pen.dash_len; i++)
698 pos += sprintf( buf + pos, " %lu", ctx->pen.dash[i] );
699 buf[0] = '[';
700 sprintf(buf + pos, "] %u setdash\n", 0);
702 else
703 sprintf(buf, "[] %u setdash\n", 0);
705 PSDRV_WriteSpool(ctx, buf, strlen(buf));
707 return TRUE;
710 BOOL PSDRV_WriteGlyphShow(print_ctx *ctx, LPCSTR g_name)
712 char buf[128];
713 int l;
715 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
717 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
718 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
719 return FALSE;
722 PSDRV_WriteSpool(ctx, buf, l);
723 return TRUE;
726 BOOL PSDRV_WriteFill(print_ctx *ctx)
728 return PSDRV_WriteSpool(ctx, psfill, sizeof(psfill)-1);
731 BOOL PSDRV_WriteEOFill(print_ctx *ctx)
733 return PSDRV_WriteSpool(ctx, pseofill, sizeof(pseofill)-1);
736 BOOL PSDRV_WriteGSave(print_ctx *ctx)
738 return PSDRV_WriteSpool(ctx, psgsave, sizeof(psgsave)-1);
741 BOOL PSDRV_WriteGRestore(print_ctx *ctx)
743 return PSDRV_WriteSpool(ctx, psgrestore, sizeof(psgrestore)-1);
746 BOOL PSDRV_WriteNewPath(print_ctx *ctx)
748 return PSDRV_WriteSpool(ctx, psnewpath, sizeof(psnewpath)-1);
751 BOOL PSDRV_WriteClosePath(print_ctx *ctx)
753 return PSDRV_WriteSpool(ctx, psclosepath, sizeof(psclosepath)-1);
756 BOOL PSDRV_WriteClip(print_ctx *ctx)
758 return PSDRV_WriteSpool(ctx, psclip, sizeof(psclip)-1);
761 BOOL PSDRV_WriteEOClip(print_ctx *ctx)
763 return PSDRV_WriteSpool(ctx, pseoclip, sizeof(pseoclip)-1);
766 BOOL PSDRV_WriteHatch(print_ctx *ctx)
768 return PSDRV_WriteSpool(ctx, pshatch, sizeof(pshatch)-1);
771 BOOL PSDRV_WriteRotate(print_ctx *ctx, float ang)
773 char buf[256];
775 push_lc_numeric("C");
776 sprintf(buf, psrotate, ang);
777 pop_lc_numeric();
778 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
781 BOOL PSDRV_WriteIndexColorSpaceBegin(print_ctx *ctx, int size)
783 char buf[256];
784 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
785 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
788 BOOL PSDRV_WriteIndexColorSpaceEnd(print_ctx *ctx)
790 static const char buf[] = ">\n] setcolorspace\n";
791 return PSDRV_WriteSpool(ctx, buf, sizeof(buf) - 1);
794 static BOOL PSDRV_WriteRGB(print_ctx *ctx, COLORREF *map, int number)
796 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
797 int i;
799 ptr = buf;
800 for(i = 0; i < number; i++) {
801 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
802 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
803 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
804 ptr += 7;
806 PSDRV_WriteSpool(ctx, buf, number * 7);
807 HeapFree( GetProcessHeap(), 0, buf );
808 return TRUE;
811 BOOL PSDRV_WriteRGBQUAD(print_ctx *ctx, const RGBQUAD *rgb, int number)
813 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
814 int i;
816 ptr = buf;
817 for(i = 0; i < number; i++, rgb++)
818 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
819 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
821 PSDRV_WriteSpool(ctx, buf, ptr - buf);
822 HeapFree( GetProcessHeap(), 0, buf );
823 return TRUE;
826 static BOOL PSDRV_WriteImageDict(print_ctx *ctx, WORD depth, BOOL grayscale,
827 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
829 static const char start[] = "<<\n"
830 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
831 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
833 static const char decode1[] = " /Decode [0 %d]\n";
834 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
836 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
837 static const char endbits[] = " /DataSource <%s>\n>>\n";
838 char buf[1000];
840 if (top_down)
841 sprintf(buf, start, widthSrc, heightSrc,
842 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
843 else
844 sprintf(buf, start, widthSrc, heightSrc,
845 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
847 PSDRV_WriteSpool(ctx, buf, strlen(buf));
849 switch(depth) {
850 case 8:
851 sprintf(buf, decode1, 255);
852 break;
854 case 4:
855 sprintf(buf, decode1, 15);
856 break;
858 case 1:
859 sprintf(buf, decode1, 1);
860 break;
862 default:
863 if (grayscale)
864 sprintf(buf, decode1, 1);
865 else
866 strcpy(buf, decode3);
867 break;
870 PSDRV_WriteSpool(ctx, buf, strlen(buf));
872 if(!bits) {
873 PSDRV_WriteSpool(ctx, end, sizeof(end) - 1);
874 } else {
875 sprintf(buf, endbits, bits);
876 PSDRV_WriteSpool(ctx, buf, strlen(buf));
879 return TRUE;
882 BOOL PSDRV_WriteImage(print_ctx *ctx, WORD depth, BOOL grayscale, INT xDst, INT yDst,
883 INT widthDst, INT heightDst, INT widthSrc,
884 INT heightSrc, BOOL mask, BOOL top_down)
886 static const char start[] = "%d %d translate\n%d %d scale\n";
887 static const char image[] = "image\n";
888 static const char imagemask[] = "imagemask\n";
889 char buf[100];
891 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
892 PSDRV_WriteSpool(ctx, buf, strlen(buf));
893 PSDRV_WriteImageDict(ctx, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
894 if(mask)
895 PSDRV_WriteSpool(ctx, imagemask, sizeof(imagemask) - 1);
896 else
897 PSDRV_WriteSpool(ctx, image, sizeof(image) - 1);
898 return TRUE;
902 BOOL PSDRV_WriteBytes(print_ctx *ctx, const BYTE *bytes, DWORD number)
904 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
905 char *ptr;
906 unsigned int i;
908 ptr = buf;
910 for(i = 0; i < number; i++) {
911 sprintf(ptr, "%02x", bytes[i]);
912 ptr += 2;
913 if(((i & 0xf) == 0xf) || (i == number - 1)) {
914 strcpy(ptr, "\n");
915 ptr++;
918 PSDRV_WriteSpool(ctx, buf, ptr - buf);
919 HeapFree( GetProcessHeap(), 0, buf );
920 return TRUE;
923 BOOL PSDRV_WriteData(print_ctx *ctx, const BYTE *data, DWORD number)
925 int num, num_left = number;
927 do {
928 num = min(num_left, 60);
929 PSDRV_WriteSpool(ctx, (LPCSTR)data, num);
930 PSDRV_WriteSpool(ctx, "\n", 1);
931 data += num;
932 num_left -= num;
933 } while(num_left);
935 return TRUE;
938 BOOL PSDRV_WriteArrayPut(print_ctx *ctx, CHAR *pszArrayName, INT nIndex, LONG lObject)
940 char buf[100];
942 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
943 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
946 BOOL PSDRV_WriteArrayDef(print_ctx *ctx, CHAR *pszArrayName, INT nSize)
948 char buf[100];
950 sprintf(buf, psarraydef, pszArrayName, nSize);
951 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
954 BOOL PSDRV_WriteRectClip(print_ctx *ctx, INT x, INT y, INT w, INT h)
956 char buf[100];
958 sprintf(buf, psrectclip, x, y, w, h);
959 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
962 BOOL PSDRV_WriteRectClip2(print_ctx *ctx, CHAR *pszArrayName)
964 char buf[100];
966 sprintf(buf, psrectclip2, pszArrayName);
967 return PSDRV_WriteSpool(ctx, buf, strlen(buf));
970 BOOL PSDRV_WriteDIBPatternDict(print_ctx *ctx, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
972 static const char mypat[] = "/mypat\n";
973 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
974 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
975 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
976 char *buf, *ptr;
977 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
978 COLORREF map[2];
980 TRACE( "size %ldx%ldx%d\n",
981 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
983 if(bmi->bmiHeader.biBitCount != 1) {
984 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
985 return FALSE;
988 w = bmi->bmiHeader.biWidth & ~0x7;
989 h = abs_height & ~0x7;
991 buf = HeapAlloc( GetProcessHeap(), 0, max(sizeof(do_pattern) + 100, 2 * w/8 * h + 1) );
992 ptr = buf;
993 for(y = h-1; y >= 0; y--) {
994 for(x = 0; x < w/8; x++) {
995 sprintf(ptr, "%02x", *(bits + x/8 + y *
996 ((bmi->bmiHeader.biWidth + 31) / 32) * 4));
997 ptr += 2;
1000 PSDRV_WriteSpool(ctx, mypat, sizeof(mypat) - 1);
1001 PSDRV_WriteImageDict(ctx, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
1002 PSDRV_WriteSpool(ctx, "def\n", 4);
1004 PSDRV_WriteIndexColorSpaceBegin(ctx, 1);
1005 map[0] = GetTextColor( ctx->hdc );
1006 map[1] = GetBkColor( ctx->hdc );
1007 PSDRV_WriteRGB(ctx, map, 2);
1008 PSDRV_WriteIndexColorSpaceEnd(ctx);
1010 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
1011 w_mult = (GetDeviceCaps(ctx->hdc, ASPECTX) + 150) / 300;
1012 h_mult = (GetDeviceCaps(ctx->hdc, ASPECTY) + 150) / 300;
1013 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
1014 PSDRV_WriteSpool(ctx, buf, strlen(buf));
1015 HeapFree( GetProcessHeap(), 0, buf );
1016 return TRUE;