bcrypt: Implement BCryptGenerateSymmetricKey and BCryptDestroyKey.
[wine.git] / dlls / wineps.drv / ps.c
blob972609dc2a87b1c58d8ff6d45ea1c599cfdb9a56
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 */
51 static const char cups_collate_false[] = "%cupsJobTicket: collate=false\n";
52 static const char cups_collate_true[] = "%cupsJobTicket: collate=true\n";
53 static const char cups_ap_d_inputslot[] = "%cupsJobTicket: AP_D_InputSlot=\n"; /* intentionally empty value */
55 static const char psheader[] = /* title llx lly urx ury orientation */
56 "%%%%Creator: Wine PostScript Driver\n"
57 "%%%%Title: %s\n"
58 "%%%%BoundingBox: %d %d %d %d\n"
59 "%%%%Pages: (atend)\n"
60 "%%%%Orientation: %s\n"
61 "%%%%EndComments\n";
63 static const char psbeginprolog[] =
64 "%%BeginProlog\n";
66 static const char psendprolog[] =
67 "%%EndProlog\n";
69 static const char psprolog[] =
70 "/tmpmtrx matrix def\n"
71 "/hatch {\n"
72 " pathbbox\n"
73 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
74 " l cvi gap idiv gap mul\n"
75 " gap\n"
76 " r cvi gap idiv gap mul\n"
77 " {t moveto 0 b t sub rlineto}\n"
78 " for\n"
79 "} bind def\n"
80 "/B {pop pop pop pop} def\n"
81 "/N {newpath} def\n"
82 "/havetype42gdir {version cvi 2015 ge} bind def\n";
84 static const char psbeginsetup[] =
85 "%%BeginSetup\n";
87 static const char psendsetup[] =
88 "%%EndSetup\n";
90 static const char psbeginfeature[] = /* feature, value */
91 "mark {\n"
92 "%%%%BeginFeature: %s %s\n";
94 static const char psendfeature[] =
95 "\n%%EndFeature\n"
96 "} stopped cleartomark\n";
98 static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
99 "%%%%Page: %s %d\n"
100 "%%%%BeginPageSetup\n"
101 "/pgsave save def\n"
102 "72 %d div 72 %d div scale\n"
103 "%d %d translate\n"
104 "1 -1 scale\n"
105 "%d rotate\n"
106 "%%%%EndPageSetup\n";
108 static const char psendpage[] =
109 "pgsave restore\n"
110 "showpage\n";
112 static const char psfooter[] = /* pages */
113 "%%%%Trailer\n"
114 "%%%%Pages: %d\n"
115 "%%%%EOF\n";
117 static const char psmoveto[] = /* x, y */
118 "%d %d moveto\n";
120 static const char pslineto[] = /* x, y */
121 "%d %d lineto\n";
123 static const char psstroke[] =
124 "stroke\n";
126 static const char psrectangle[] = /* x, y, width, height, -width */
127 "%d %d moveto\n"
128 "%d 0 rlineto\n"
129 "0 %d rlineto\n"
130 "%d 0 rlineto\n"
131 "closepath\n";
133 static const char psglyphshow[] = /* glyph name */
134 "/%s glyphshow\n";
136 static const char psfindfont[] = /* fontname */
137 "/%s findfont\n";
139 static const char psfakeitalic[] =
140 "[1 0 0.25 1 0 0]\n";
142 static const char pssizematrix[] =
143 "[%d %d %d %d 0 0]\n";
145 static const char psconcat[] =
146 "matrix concatmatrix\n";
148 static const char psrotatefont[] = /* escapement */
149 "%d 10 div matrix rotate\n"
150 "matrix concatmatrix\n";
152 static const char pssetfont[] =
153 "makefont setfont\n";
155 static const char pssetline[] = /* width, join, endcap */
156 "%d setlinewidth %u setlinejoin %u setlinecap\n";
158 static const char pssetgray[] = /* gray */
159 "%.2f setgray\n";
161 static const char pssetrgbcolor[] = /* r, g, b */
162 "%.2f %.2f %.2f setrgbcolor\n";
164 static const char psarc[] = /* x, y, w, h, ang1, ang2 */
165 "tmpmtrx currentmatrix pop\n"
166 "%d %d translate\n"
167 "%d %d scale\n"
168 "0 0 0.5 %.1f %.1f arc\n"
169 "tmpmtrx setmatrix\n";
171 static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
172 "%d %d %d %d %d %d curveto\n";
174 static const char psgsave[] =
175 "gsave\n";
177 static const char psgrestore[] =
178 "grestore\n";
180 static const char psfill[] =
181 "fill\n";
183 static const char pseofill[] =
184 "eofill\n";
186 static const char psnewpath[] =
187 "newpath\n";
189 static const char psclosepath[] =
190 "closepath\n";
192 static const char psclip[] =
193 "clip\n";
195 static const char pseoclip[] =
196 "eoclip\n";
198 static const char psrectclip[] =
199 "%d %d %d %d rectclip\n";
201 static const char psrectclip2[] =
202 "%s rectclip\n";
204 static const char pshatch[] =
205 "hatch\n";
207 static const char psrotate[] = /* ang */
208 "%.1f rotate\n";
210 static const char psarrayput[] =
211 "%s %d %d put\n";
213 static const char psarraydef[] =
214 "/%s %d array def\n";
216 static const char psenddocument[] =
217 "\n%%EndDocument\n";
219 DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
221 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
222 int num, num_left = cch;
224 if(physDev->job.quiet) {
225 TRACE("ignoring output\n");
226 return 0;
229 if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
230 write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
231 physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
234 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
235 if( !PSDRV_StartPage(dev) )
236 return 0;
239 do {
240 num = min(num_left, 0x8000);
241 if(write_spool( dev, lpData, num ) != num)
242 return 0;
243 lpData += num;
244 num_left -= num;
245 } while(num_left);
247 return cch;
251 static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
254 char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
255 strlen(feature) + strlen(value));
257 sprintf(buf, psbeginfeature, feature, value);
258 write_spool( dev, buf, strlen(buf) );
259 write_spool( dev, invocation, strlen(invocation) );
260 write_spool( dev, psendfeature, strlen(psendfeature) );
262 HeapFree( GetProcessHeap(), 0, buf );
263 return 1;
266 /********************************************************
267 * escape_title
269 * Helper for PSDRV_WriteHeader. Escape any non-printable characters
270 * as octal. If we've had to use an escape then surround the entire string
271 * in brackets. Truncate string to represent at most 0x80 characters.
274 static char *escape_title(LPCWSTR wstr)
276 char *ret, *cp, *str;
277 int i, extra = 0;
279 if(!wstr)
281 ret = HeapAlloc(GetProcessHeap(), 0, 1);
282 *ret = '\0';
283 return ret;
286 i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
287 str = HeapAlloc( GetProcessHeap(), 0, i );
288 if (!str) return NULL;
289 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );
291 for(i = 0; i < 0x80 && str[i]; i++)
293 if(!isprint(str[i]))
294 extra += 3;
297 if(!extra)
299 ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
300 memcpy(ret, str, i);
301 ret[i] = '\0';
302 goto done;
305 extra += 2; /* two for the brackets */
306 cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
307 *cp++ = '(';
308 for(i = 0; i < 0x80 && str[i]; i++)
310 if(!isprint(str[i]))
312 BYTE b = (BYTE)str[i];
313 *cp++ = '\\';
314 *cp++ = ((b >> 6) & 0x7) + '0';
315 *cp++ = ((b >> 3) & 0x7) + '0';
316 *cp++ = ((b) & 0x7) + '0';
318 else
319 *cp++ = str[i];
321 *cp++ = ')';
322 *cp = '\0';
324 done:
325 HeapFree( GetProcessHeap(), 0, str );
326 return ret;
329 struct ticket_info
331 PAGESIZE *page;
332 DUPLEX *duplex;
335 static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
337 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
338 char buf[256];
339 int len;
341 if (info->page && info->page->InvocationString)
343 len = sizeof(media) + strlen( info->page->Name ) + 1;
344 if (len <= sizeof(buf))
346 memcpy( buf, media, sizeof(media) );
347 strcat( buf, info->page->Name );
348 strcat( buf, "\n");
349 write_spool( dev, buf, len - 1 );
351 else
352 WARN( "paper name %s will be too long for DSC\n", info->page->Name );
355 if (info->duplex && info->duplex->InvocationString)
357 if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
359 const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
360 write_spool( dev, str, strlen( str ) );
364 if (physDev->Devmode->dmPublic.u1.s1.dmCopies > 1)
366 len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
367 physDev->Devmode->dmPublic.u1.s1.dmCopies );
368 if (len > 0 && len < sizeof(buf))
369 write_spool( dev, buf, len );
371 if (physDev->Devmode->dmPublic.dmFields & DM_COLLATE)
373 if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
374 write_spool( dev, cups_collate_false, sizeof(cups_collate_false) - 1 );
375 else if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
376 write_spool( dev, cups_collate_true, sizeof(cups_collate_true) - 1 );
380 if (!(physDev->Devmode->dmPublic.dmFields & DM_DEFAULTSOURCE) ||
381 physDev->Devmode->dmPublic.u1.s1.dmDefaultSource == DMBIN_AUTO)
382 write_spool( dev, cups_ap_d_inputslot, sizeof(cups_ap_d_inputslot) - 1 );
385 INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
387 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
388 char *buf, *escaped_title;
389 INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
390 PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
391 DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
392 int llx, lly, urx, ury;
393 int ret, len;
394 const char * dmOrientation;
396 struct ticket_info ticket_info = { page, duplex };
398 TRACE("%s\n", debugstr_w(title));
400 len = strlen( psadobe );
401 ret = write_spool( dev, psadobe, len );
402 if (ret != len)
404 WARN("WriteSpool error\n");
405 return 0;
408 write_cups_job_ticket( dev, &ticket_info );
410 escaped_title = escape_title(title);
411 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
412 strlen(escaped_title) + 30 );
413 if(!buf) {
414 WARN("HeapAlloc failed\n");
415 HeapFree(GetProcessHeap(), 0, escaped_title);
416 return 0;
419 /* BBox co-ords are in default user co-ord system so urx < ury even in
420 landscape mode */
421 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
422 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
423 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
424 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
425 /* FIXME should do something better with BBox */
427 dmOrientation = (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
428 sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
430 HeapFree(GetProcessHeap(), 0, escaped_title);
432 len = strlen( buf );
433 write_spool( dev, buf, len );
434 HeapFree( GetProcessHeap(), 0, buf );
436 write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
437 write_spool( dev, psprolog, strlen(psprolog) );
438 write_spool( dev, psendprolog, strlen(psendprolog) );
439 write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
441 if (slot && slot->InvocationString)
442 PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
444 if (page && page->InvocationString)
445 PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
447 if (duplex && duplex->InvocationString)
448 PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
450 write_spool( dev, psendsetup, strlen(psendsetup) );
453 return 1;
457 INT PSDRV_WriteFooter( PHYSDEV dev )
459 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
460 char *buf;
461 int ret = 1;
463 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
464 if(!buf) {
465 WARN("HeapAlloc failed\n");
466 return 0;
469 sprintf(buf, psfooter, physDev->job.PageNo);
471 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
472 WARN("WriteSpool error\n");
473 ret = 0;
475 HeapFree( GetProcessHeap(), 0, buf );
476 return ret;
481 INT PSDRV_WriteEndPage( PHYSDEV dev )
483 if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
484 WARN("WriteSpool error\n");
485 return 0;
487 return 1;
493 INT PSDRV_WriteNewPage( PHYSDEV dev )
495 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
496 char *buf;
497 char name[100];
498 signed int xtrans, ytrans, rotation;
499 int ret = 1;
501 sprintf(name, "%d", physDev->job.PageNo);
503 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
504 if(!buf) {
505 WARN("HeapAlloc failed\n");
506 return 0;
509 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
510 if(physDev->pi->ppd->LandscapeOrientation == -90) {
511 xtrans = physDev->ImageableArea.right;
512 ytrans = physDev->ImageableArea.top;
513 rotation = 90;
514 } else {
515 xtrans = physDev->ImageableArea.left;
516 ytrans = physDev->ImageableArea.bottom;
517 rotation = -90;
519 } else {
520 xtrans = physDev->ImageableArea.left;
521 ytrans = physDev->ImageableArea.top;
522 rotation = 0;
525 sprintf(buf, psnewpage, name, physDev->job.PageNo,
526 physDev->logPixelsX, physDev->logPixelsY,
527 xtrans, ytrans, rotation);
529 if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
530 WARN("WriteSpool error\n");
531 ret = 0;
533 HeapFree( GetProcessHeap(), 0, buf );
534 return ret;
538 BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
540 char buf[100];
542 sprintf(buf, psmoveto, x, y);
543 return PSDRV_WriteSpool(dev, buf, strlen(buf));
546 BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
548 char buf[100];
550 sprintf(buf, pslineto, x, y);
551 return PSDRV_WriteSpool(dev, buf, strlen(buf));
555 BOOL PSDRV_WriteStroke(PHYSDEV dev)
557 return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
562 BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
563 INT height)
565 char buf[100];
567 sprintf(buf, psrectangle, x, y, width, height, -width);
568 return PSDRV_WriteSpool(dev, buf, strlen(buf));
571 BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
572 double ang2)
574 char buf[256];
576 /* Make angles -ve and swap order because we're working with an upside
577 down y-axis */
578 push_lc_numeric("C");
579 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
580 pop_lc_numeric();
581 return PSDRV_WriteSpool(dev, buf, strlen(buf));
584 BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
586 char buf[256];
588 sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
589 return PSDRV_WriteSpool(dev, buf, strlen(buf));
592 BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement, BOOL fake_italic)
594 char *buf;
596 buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
598 if(!buf) {
599 WARN("HeapAlloc failed\n");
600 return FALSE;
603 sprintf( buf, psfindfont, name );
604 PSDRV_WriteSpool( dev, buf, strlen(buf) );
606 if (fake_italic) PSDRV_WriteSpool( dev, psfakeitalic, sizeof(psfakeitalic) - 1 );
608 sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
609 PSDRV_WriteSpool( dev, buf, strlen(buf) );
611 if (fake_italic) PSDRV_WriteSpool( dev, psconcat, sizeof(psconcat) - 1 );
613 if (escapement)
615 sprintf( buf, psrotatefont, -escapement );
616 PSDRV_WriteSpool( dev, buf, strlen(buf) );
619 PSDRV_WriteSpool( dev, pssetfont, sizeof(pssetfont) - 1 );
620 HeapFree( GetProcessHeap(), 0, buf );
622 return TRUE;
625 BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
627 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
628 char buf[256];
630 PSDRV_CopyColor(&physDev->inkColor, color);
631 switch(color->type) {
632 case PSCOLOR_RGB:
633 push_lc_numeric("C");
634 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
635 color->value.rgb.b);
636 pop_lc_numeric();
637 return PSDRV_WriteSpool(dev, buf, strlen(buf));
639 case PSCOLOR_GRAY:
640 push_lc_numeric("C");
641 sprintf(buf, pssetgray, color->value.gray.i);
642 pop_lc_numeric();
643 return PSDRV_WriteSpool(dev, buf, strlen(buf));
645 default:
646 ERR("Unknown colour type %d\n", color->type);
647 break;
650 return FALSE;
653 BOOL PSDRV_WriteSetPen(PHYSDEV dev)
655 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
656 char buf[256];
657 DWORD i, pos;
659 sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
660 PSDRV_WriteSpool(dev, buf, strlen(buf));
662 if (physDev->pen.dash_len)
664 for (i = pos = 0; i < physDev->pen.dash_len; i++)
665 pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
666 buf[0] = '[';
667 sprintf(buf + pos, "] %u setdash\n", 0);
669 else
670 sprintf(buf, "[] %u setdash\n", 0);
672 PSDRV_WriteSpool(dev, buf, strlen(buf));
674 return TRUE;
677 BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
679 char buf[128];
680 int l;
682 l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
684 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
685 WARN("Unusable glyph name '%s' - ignoring\n", g_name);
686 return FALSE;
689 PSDRV_WriteSpool(dev, buf, l);
690 return TRUE;
693 BOOL PSDRV_WriteFill(PHYSDEV dev)
695 return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
698 BOOL PSDRV_WriteEOFill(PHYSDEV dev)
700 return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
703 BOOL PSDRV_WriteGSave(PHYSDEV dev)
705 return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
708 BOOL PSDRV_WriteGRestore(PHYSDEV dev)
710 return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
713 BOOL PSDRV_WriteNewPath(PHYSDEV dev)
715 return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
718 BOOL PSDRV_WriteClosePath(PHYSDEV dev)
720 return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
723 BOOL PSDRV_WriteClip(PHYSDEV dev)
725 return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
728 BOOL PSDRV_WriteEOClip(PHYSDEV dev)
730 return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
733 BOOL PSDRV_WriteHatch(PHYSDEV dev)
735 return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
738 BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
740 char buf[256];
742 push_lc_numeric("C");
743 sprintf(buf, psrotate, ang);
744 pop_lc_numeric();
745 return PSDRV_WriteSpool(dev, buf, strlen(buf));
748 BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
750 char buf[256];
751 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
752 return PSDRV_WriteSpool(dev, buf, strlen(buf));
755 BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
757 static const char buf[] = ">\n] setcolorspace\n";
758 return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
761 static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
763 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
764 int i;
766 ptr = buf;
767 for(i = 0; i < number; i++) {
768 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
769 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
770 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
771 ptr += 7;
773 PSDRV_WriteSpool(dev, buf, number * 7);
774 HeapFree( GetProcessHeap(), 0, buf );
775 return TRUE;
778 BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
780 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
781 int i;
783 ptr = buf;
784 for(i = 0; i < number; i++, rgb++)
785 ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
786 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
788 PSDRV_WriteSpool(dev, buf, ptr - buf);
789 HeapFree( GetProcessHeap(), 0, buf );
790 return TRUE;
793 static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth, BOOL grayscale,
794 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
796 static const char start[] = "<<\n"
797 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
798 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
800 static const char decode1[] = " /Decode [0 %d]\n";
801 static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
803 static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
804 static const char endbits[] = " /DataSource <%s>\n>>\n";
805 char buf[1000];
807 if (top_down)
808 sprintf(buf, start, widthSrc, heightSrc,
809 (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
810 else
811 sprintf(buf, start, widthSrc, heightSrc,
812 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
814 PSDRV_WriteSpool(dev, buf, strlen(buf));
816 switch(depth) {
817 case 8:
818 sprintf(buf, decode1, 255);
819 break;
821 case 4:
822 sprintf(buf, decode1, 15);
823 break;
825 case 1:
826 sprintf(buf, decode1, 1);
827 break;
829 default:
830 if (grayscale)
831 sprintf(buf, decode1, 1);
832 else
833 strcpy(buf, decode3);
834 break;
837 PSDRV_WriteSpool(dev, buf, strlen(buf));
839 if(!bits) {
840 PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
841 } else {
842 sprintf(buf, endbits, bits);
843 PSDRV_WriteSpool(dev, buf, strlen(buf));
846 return TRUE;
849 BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, BOOL grayscale, INT xDst, INT yDst,
850 INT widthDst, INT heightDst, INT widthSrc,
851 INT heightSrc, BOOL mask, BOOL top_down)
853 static const char start[] = "%d %d translate\n%d %d scale\n";
854 static const char image[] = "image\n";
855 static const char imagemask[] = "imagemask\n";
856 char buf[100];
858 sprintf(buf, start, xDst, yDst, widthDst, heightDst);
859 PSDRV_WriteSpool(dev, buf, strlen(buf));
860 PSDRV_WriteImageDict(dev, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
861 if(mask)
862 PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
863 else
864 PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
865 return TRUE;
869 BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
871 char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
872 char *ptr;
873 unsigned int i;
875 ptr = buf;
877 for(i = 0; i < number; i++) {
878 sprintf(ptr, "%02x", bytes[i]);
879 ptr += 2;
880 if(((i & 0xf) == 0xf) || (i == number - 1)) {
881 strcpy(ptr, "\n");
882 ptr++;
885 PSDRV_WriteSpool(dev, buf, ptr - buf);
886 HeapFree( GetProcessHeap(), 0, buf );
887 return TRUE;
890 BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
892 int num, num_left = number;
894 do {
895 num = min(num_left, 60);
896 PSDRV_WriteSpool(dev, (LPCSTR)data, num);
897 PSDRV_WriteSpool(dev, "\n", 1);
898 data += num;
899 num_left -= num;
900 } while(num_left);
902 return TRUE;
905 BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
907 char buf[100];
909 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
910 return PSDRV_WriteSpool(dev, buf, strlen(buf));
913 BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
915 char buf[100];
917 sprintf(buf, psarraydef, pszArrayName, nSize);
918 return PSDRV_WriteSpool(dev, buf, strlen(buf));
921 BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
923 char buf[100];
925 sprintf(buf, psrectclip, x, y, w, h);
926 return PSDRV_WriteSpool(dev, buf, strlen(buf));
929 BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
931 char buf[100];
933 sprintf(buf, psrectclip2, pszArrayName);
934 return PSDRV_WriteSpool(dev, buf, strlen(buf));
937 BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
939 static const char mypat[] = "/mypat\n";
940 static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
941 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n 0 0 translate\n"
942 " %d %d scale\n mypat image\n end\n }\n>>\n matrix makepattern setpattern\n";
943 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
944 char *buf, *ptr;
945 INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
946 COLORREF map[2];
948 TRACE( "size %dx%dx%d\n",
949 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
951 if(bmi->bmiHeader.biBitCount != 1) {
952 FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
953 return FALSE;
956 w = bmi->bmiHeader.biWidth & ~0x7;
957 h = abs_height & ~0x7;
959 buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
960 ptr = buf;
961 for(y = h-1; y >= 0; y--) {
962 for(x = 0; x < w/8; x++) {
963 sprintf(ptr, "%02x", *(bits + x/8 + y *
964 (bmi->bmiHeader.biWidth + 31) / 32 * 4));
965 ptr += 2;
968 PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
969 PSDRV_WriteImageDict(dev, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
970 PSDRV_WriteSpool(dev, "def\n", 4);
972 PSDRV_WriteIndexColorSpaceBegin(dev, 1);
973 map[0] = GetTextColor( dev->hdc );
974 map[1] = GetBkColor( dev->hdc );
975 PSDRV_WriteRGB(dev, map, 2);
976 PSDRV_WriteIndexColorSpaceEnd(dev);
978 /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
979 w_mult = (physDev->logPixelsX + 150) / 300;
980 h_mult = (physDev->logPixelsY + 150) / 300;
981 sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
982 PSDRV_WriteSpool(dev, buf, strlen(buf));
983 HeapFree( GetProcessHeap(), 0, buf );
984 return TRUE;