PSDRV_StretchDIBits should use logical co-ords.
[wine] / graphics / psdrv / ps.c
1 /*
2  *      PostScript output functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  */
7
8 #include <ctype.h>
9 #include <string.h>
10 #include "psdrv.h"
11 #include "winspool.h"
12 #include "debug.h"
13
14 DEFAULT_DEBUG_CHANNEL(psdrv)
15
16 static char psheader[] = /* title llx lly urx ury orientation */
17 "%%!PS-Adobe-3.0\n"
18 "%%%%Creator: Wine PostScript Driver\n"
19 "%%%%Title: %s\n"
20 "%%%%BoundingBox: %d %d %d %d\n"
21 "%%%%Pages: (atend)\n"
22 "%%%%Orientation: %s\n"
23 "%%%%EndComments\n";
24
25 static char psbeginprolog[] = 
26 "%%BeginProlog\n";
27
28 static char psendprolog[] =
29 "%%EndProlog\n";
30
31 static char psvectorstart[] =
32 "/ANSIEncoding [\n";
33
34 static char psvectorend[] =
35 "] def\n";
36
37 static char psprolog[] = /* output ANSIEncoding vector first */
38 "/reencodefont {\n"
39 "  findfont\n"
40 "  dup length dict begin\n"
41 "  {1 index /FID ne {def} {pop pop} ifelse} forall\n"
42 "  /Encoding ANSIEncoding def\n"
43 "  currentdict\n"
44 "  end\n"
45 "  definefont pop\n"
46 "} bind def\n"
47 "/tmpmtrx matrix def\n"
48 "/hatch {\n"
49 "  pathbbox\n"
50 "  /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
51 "  l cvi gap idiv gap mul\n"
52 "  gap\n"
53 "  r cvi gap idiv gap mul\n"
54 "  {t moveto 0 b t sub rlineto}\n"
55 "  for\n"
56 "} bind def\n";
57
58 static char psbeginsetup[] =
59 "%%BeginSetup\n";
60
61 static char psendsetup[] =
62 "%%EndSetup\n";
63
64 static char psbeginfeature[] = /* feature, value */
65 "mark {\n"
66 "%%%%BeginFeature: %s %s\n";
67
68 static char psendfeature[] =
69 "\n%%EndFeature\n"
70 "} stopped cleartomark\n";
71
72 static char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
73 "%%%%Page: %s %d\n"
74 "%%%%BeginPageSetup\n"
75 "/pgsave save def\n"
76 "72 %d div 72 %d div scale\n"
77 "%d %d translate\n"
78 "1 -1 scale\n"
79 "%d rotate\n"
80 "%%%%EndPageSetup\n";
81
82 static char psendpage[] =
83 "pgsave restore\n"
84 "showpage\n";
85
86 static char psfooter[] = /* pages */
87 "%%%%Trailer\n"
88 "%%%%Pages: %d\n"
89 "%%%%EOF\n";
90
91 static char psmoveto[] = /* x, y */
92 "%d %d moveto\n";
93
94 static char pslineto[] = /* x, y */
95 "%d %d lineto\n";
96
97 static char psstroke[] = 
98 "stroke\n";
99
100 static char psrectangle[] = /* x, y, width, height, -width */
101 "%d %d moveto\n"
102 "%d 0 rlineto\n"
103 "0 %d rlineto\n"
104 "%d 0 rlineto\n"
105 "closepath\n";
106
107 static char psshow[] = /* string */
108 "(%s) show\n";
109
110 static char pssetfont[] = /* fontname, xscale, yscale, ascent, escapement */
111 "/%s findfont\n"
112 "[%d 0 0 %d 0 0]\n"
113 "%d 10 div matrix rotate\n"
114 "matrix concatmatrix\n"
115 "makefont setfont\n";
116
117 static char pssetlinewidth[] = /* width */
118 "%d setlinewidth\n";
119
120 static char pssetdash[] = /* dash, offset */
121 "[%s] %d setdash\n";
122
123 static char pssetgray[] = /* gray */
124 "%.2f setgray\n";
125
126 static char pssetrgbcolor[] = /* r, g, b */
127 "%.2f %.2f %.2f setrgbcolor\n";
128
129 static char psarc[] = /* x, y, w, h, ang1, ang2 */
130 "tmpmtrx currentmatrix pop\n"
131 "%d %d translate\n"
132 "%d %d scale\n"
133 "0 0 0.5 %.1f %.1f arc\n"
134 "tmpmtrx setmatrix\n";
135
136 static char psgsave[] =
137 "gsave\n";
138
139 static char psgrestore[] =
140 "grestore\n";
141
142 static char psfill[] =
143 "fill\n";
144
145 static char pseofill[] =
146 "eofill\n";
147
148 static char psclosepath[] =
149 "closepath\n";
150
151 static char psclip[] =
152 "clip\n";
153
154 static char pseoclip[] =
155 "eoclip\n";
156
157 static char pshatch[] =
158 "hatch\n";
159
160 static char psrotate[] = /* ang */
161 "%.1f rotate\n";
162
163 char *PSDRV_ANSIVector[256] = {
164 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x00 */
165 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
166 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x10 */
167 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
168 "space",        "exclam",       "quotedbl",     "numbersign", /* 0x20 */
169 "dollar",       "percent",      "ampersand",    "quotesingle",
170 "parenleft",    "parenright",   "asterisk",     "plus",
171 "comma",        "hyphen",       "period",       "slash",
172 "zero",         "one",          "two",          "three", /* 0x30 */
173 "four",         "five",         "six",          "seven",
174 "eight",        "nine",         "colon",        "semicolon",
175 "less",         "equal",        "greater",      "question",
176 "at",           "A",            "B",            "C", /* 0x40 */
177 "D",            "E",            "F",            "G",
178 "H",            "I",            "J",            "K",
179 "L",            "M",            "N",            "O",
180 "P",            "Q",            "R",            "S", /* 0x50 */
181 "T",            "U",            "V",            "W",
182 "X",            "Y",            "Z",            "bracketleft",
183 "backslash",    "bracketright", "asciicircum",  "underscore",
184 "grave",        "a",            "b",            "c", /* 0x60 */
185 "d",            "e",            "f",            "g",
186 "h",            "i",            "j",            "k",
187 "l",            "m",            "n",            "o",
188 "p",            "q",            "r",            "s", /* 0x70 */
189 "t",            "u",            "v",            "w",
190 "x",            "y",            "z",            "braceleft",
191 "bar",          "braceright",   "asciitilde",   NULL,
192 NULL,           NULL,           NULL,           NULL, /* 0x80 */
193 NULL,           NULL,           NULL,           NULL,
194 NULL,           NULL,           NULL,           NULL,
195 NULL,           NULL,           NULL,           NULL,
196 NULL,           "quoteleft",    "quoteright",   "quotedblleft", /* 0x90 */
197 "quotedblright","bullet",       "endash",       "emdash",
198 NULL,           NULL,           NULL,           NULL,
199 NULL,           NULL,           NULL,           NULL,
200 "space",        "exclamdown",   "cent",         "sterling", /* 0xa0 */
201 "currency",     "yen",          "brokenbar",    "section",
202 "dieresis",     "copyright",    "ordfeminine",  "guillemotleft",
203 "logicalnot",   "hyphen",       "registered",   "macron",
204 "degree",       "plusminus",    "twosuperior",  "threesuperior", /* 0xb0 */
205 "acute",        "mu",           "paragraph",    "periodcentered",
206 "cedilla",      "onesuperior",  "ordmasculine", "guillemotright",
207 "onequarter",   "onehalf",      "threequarters","questiondown",
208 "Agrave",       "Aacute",       "Acircumflex",  "Atilde", /* 0xc0 */
209 "Adieresis",    "Aring",        "AE",           "Ccedilla",
210 "Egrave",       "Eacute",       "Ecircumflex",  "Edieresis",
211 "Igrave",       "Iacute",       "Icircumflex",  "Idieresis",
212 "Eth",          "Ntilde",       "Ograve",       "Oacute", /* 0xd0 */
213 "Ocircumflex",  "Otilde",       "Odieresis",    "multiply",
214 "Oslash",       "Ugrave",       "Uacute",       "Ucircumflex",
215 "Udieresis",    "Yacute",       "Thorn",        "germandbls",
216 "agrave",       "aacute",       "acircumflex",  "atilde", /* 0xe0 */
217 "adieresis",    "aring",        "ae",           "ccedilla",
218 "egrave",       "eacute",       "ecircumflex",  "edieresis",
219 "igrave",       "iacute",       "icircumflex",  "idieresis",
220 "eth",          "ntilde",       "ograve",       "oacute", /* 0xf0 */
221 "ocircumflex",  "otilde",       "odieresis",    "divide",
222 "oslash",       "ugrave",       "uacute",       "ucircumflex",
223 "udieresis",    "yacute",       "thorn",        "ydieresis"
224 };
225
226
227 char psreencodefont[] = /* newfontname basefontname*/
228 "/%s /%s reencodefont\n";
229
230
231 int PSDRV_WriteSpool(DC *dc, LPSTR lpData, WORD cch)
232 {
233     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
234
235     if(physDev->job.NeedPageHeader) {
236         physDev->job.PageNo++;
237         if( !PSDRV_WriteNewPage(dc) )
238             return FALSE;
239         physDev->job.NeedPageHeader = FALSE;
240     }
241     return WriteSpool16( physDev->job.hJob, lpData, cch );
242 }
243
244
245 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
246                          char *invocation)
247 {
248
249     char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
250                              strlen(feature) + strlen(value));
251
252
253     sprintf(buf, psbeginfeature, feature, value);
254     WriteSpool16( hJob, buf, strlen(buf) );
255
256     WriteSpool16( hJob, invocation, strlen(invocation) );
257
258     WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
259     
260     HeapFree( PSDRV_Heap, 0, buf );
261     return 1;
262 }
263
264
265
266 INT PSDRV_WriteHeader( DC *dc, char *title, int len )
267 {
268     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
269     char *buf, *titlebuf, *orient, vectbuf[256];
270     INPUTSLOT *slot;
271     PAGESIZE *page;
272     int urx, ury, i, j;
273
274     titlebuf = (char *)HeapAlloc( PSDRV_Heap, 0, len+1 );
275     if(!titlebuf) {
276         WARN(psdrv, "HeapAlloc failed\n");
277         return 0;
278     }
279     memcpy(titlebuf, title, len);
280     titlebuf[len] = '\0';
281
282     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) + len + 30);
283     if(!buf) {
284         WARN(psdrv, "HeapAlloc failed\n");
285         HeapFree( PSDRV_Heap, 0, titlebuf );
286         return 0;
287     }
288
289     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
290       /* BBox co-ords are in default user co-ord system so urx < ury even in
291          landscape mode */
292         urx = (int) (dc->w.devCaps->vertSize * 72.0 / 25.4);
293         ury = (int) (dc->w.devCaps->horzSize * 72.0 / 25.4);
294         orient = "Landscape";
295     } else {
296         urx = (int) (dc->w.devCaps->horzSize * 72.0 / 25.4);
297         ury = (int) (dc->w.devCaps->vertSize * 72.0 / 25.4);
298         orient = "Portrait";
299     }
300
301     /* FIXME should do something better with BBox */
302
303     sprintf(buf, psheader, title, 0, 0, urx, ury, orient);              
304
305     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
306                                                      strlen(buf) ) {
307         WARN(psdrv, "WriteSpool error\n");
308         HeapFree( PSDRV_Heap, 0, titlebuf );
309         HeapFree( PSDRV_Heap, 0, buf );
310         return 0;
311     }
312     HeapFree( PSDRV_Heap, 0, titlebuf );
313     HeapFree( PSDRV_Heap, 0, buf );
314
315     WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
316     WriteSpool16( physDev->job.hJob, psvectorstart, strlen(psvectorstart) );
317     
318     for(i = 0; i < 256; i += 8) {
319         vectbuf[0] = '\0';
320         for(j = 0; j < 8; j++) {
321             strcat(vectbuf, "/");
322             if(PSDRV_ANSIVector[i+j]) {
323                 strcat(vectbuf, PSDRV_ANSIVector[i+j]);
324                 strcat(vectbuf, " ");
325             } else {
326                 strcat(vectbuf, ".notdef ");
327             }
328         }
329         strcat(vectbuf, "\n");
330         WriteSpool16( physDev->job.hJob, vectbuf, strlen(vectbuf) );
331     }
332
333     WriteSpool16( physDev->job.hJob, psvectorend, strlen(psvectorend) );
334     WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
335     WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
336
337
338     WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
339
340     for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
341         if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
342             if(slot->InvocationString) {
343                 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
344                              slot->InvocationString);
345                 break;
346             }
347         }
348     }
349
350     for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
351         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
352             if(page->InvocationString) {
353                 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
354                              page->InvocationString);
355                 break;
356             }
357         }
358     }
359
360     WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
361
362
363     return 1;
364 }
365
366
367 INT PSDRV_WriteFooter( DC *dc )
368 {
369     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
370     char *buf;
371
372     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
373     if(!buf) {
374         WARN(psdrv, "HeapAlloc failed\n");
375         return 0;
376     }
377
378     sprintf(buf, psfooter, physDev->job.PageNo);
379
380     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
381                                                      strlen(buf) ) {
382         WARN(psdrv, "WriteSpool error\n");
383         HeapFree( PSDRV_Heap, 0, buf );
384         return 0;
385     }
386     HeapFree( PSDRV_Heap, 0, buf );
387     return 1;
388 }
389
390
391
392 INT PSDRV_WriteEndPage( DC *dc )
393 {
394     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
395
396     if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) != 
397                                                      sizeof(psendpage)-1 ) {
398         WARN(psdrv, "WriteSpool error\n");
399         return 0;
400     }
401     return 1;
402 }
403
404
405
406
407 INT PSDRV_WriteNewPage( DC *dc )
408 {
409     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
410     char *buf;
411     char name[100];
412     signed int xtrans, ytrans, rotation;
413
414     sprintf(name, "%d", physDev->job.PageNo);
415
416     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
417     if(!buf) {
418         WARN(psdrv, "HeapAlloc failed\n");
419         return 0;
420     }
421
422     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
423         if(physDev->pi->ppd->LandscapeOrientation == -90) {
424             xtrans = dc->w.devCaps->vertRes;
425             ytrans = dc->w.devCaps->horzRes;
426             rotation = 90;
427         } else {
428             xtrans = ytrans = 0;
429             rotation = -90;
430         }
431     } else {
432         xtrans = 0;
433         ytrans = dc->w.devCaps->vertRes;
434         rotation = 0;
435     }
436
437     sprintf(buf, psnewpage, name, physDev->job.PageNo,
438             dc->w.devCaps->logPixelsX, dc->w.devCaps->logPixelsY,
439             xtrans, ytrans, rotation);
440
441     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
442                                                      strlen(buf) ) {
443         WARN(psdrv, "WriteSpool error\n");
444         HeapFree( PSDRV_Heap, 0, buf );
445         return 0;
446     }
447     HeapFree( PSDRV_Heap, 0, buf );
448     return 1;
449 }
450
451
452 BOOL PSDRV_WriteMoveTo(DC *dc, INT x, INT y)
453 {
454     char buf[100];
455
456     sprintf(buf, psmoveto, x, y);
457     return PSDRV_WriteSpool(dc, buf, strlen(buf));
458 }
459
460 BOOL PSDRV_WriteLineTo(DC *dc, INT x, INT y)
461 {
462     char buf[100];
463
464     sprintf(buf, pslineto, x, y);
465     return PSDRV_WriteSpool(dc, buf, strlen(buf));
466 }
467
468
469 BOOL PSDRV_WriteStroke(DC *dc)
470 {
471     return PSDRV_WriteSpool(dc, psstroke, sizeof(psstroke)-1);
472 }
473
474
475
476 BOOL PSDRV_WriteRectangle(DC *dc, INT x, INT y, INT width, 
477                         INT height)
478 {
479     char buf[100];
480
481     sprintf(buf, psrectangle, x, y, width, height, -width);
482     return PSDRV_WriteSpool(dc, buf, strlen(buf));
483 }
484
485 BOOL PSDRV_WriteArc(DC *dc, INT x, INT y, INT w, INT h, double ang1,
486                       double ang2)
487 {
488     char buf[256];
489
490     /* Make angles -ve and swap order because we're working with an upside
491        down y-axis */
492     sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
493     return PSDRV_WriteSpool(dc, buf, strlen(buf));
494 }
495
496 static char encodingext[] = "-ANSI";
497
498 BOOL PSDRV_WriteSetFont(DC *dc, BOOL UseANSI)
499 {
500     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
501     char *buf, *newbuf;
502
503     buf = (char *)HeapAlloc( PSDRV_Heap, 0,
504              sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
505
506     if(!buf) {
507         WARN(psdrv, "HeapAlloc failed\n");
508         return FALSE;
509     }
510
511     newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
512               strlen(physDev->font.afm->FontName) + sizeof(encodingext));
513
514     if(!newbuf) {
515         WARN(psdrv, "HeapAlloc failed\n");
516         HeapFree(PSDRV_Heap, 0, buf);
517         return FALSE;
518     }
519
520     if(UseANSI)
521         sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
522     else
523         strcpy(newbuf, physDev->font.afm->FontName);
524
525     sprintf(buf, pssetfont, newbuf, 
526                 physDev->font.size, -physDev->font.size,
527                 -physDev->font.escapement);
528
529     PSDRV_WriteSpool(dc, buf, strlen(buf));
530     HeapFree(PSDRV_Heap, 0, buf);
531     return TRUE;
532 }    
533
534 BOOL PSDRV_WriteSetColor(DC *dc, PSCOLOR *color)
535 {
536     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
537     char buf[256];
538
539     if(PSDRV_CmpColor(&physDev->inkColor, color))
540         return TRUE;
541
542     PSDRV_CopyColor(&physDev->inkColor, color);
543     switch(color->type) {
544     case PSCOLOR_RGB:
545         sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
546                 color->value.rgb.b);
547         return PSDRV_WriteSpool(dc, buf, strlen(buf));
548
549     case PSCOLOR_GRAY:  
550         sprintf(buf, pssetgray, color->value.gray.i);
551         return PSDRV_WriteSpool(dc, buf, strlen(buf));
552         
553     default:
554         ERR(psdrv, "Unkonwn colour type %d\n", color->type);
555         break;
556     }
557
558     return FALSE;
559 }
560
561 BOOL PSDRV_WriteSetPen(DC *dc)
562 {
563     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
564     char buf[256];
565
566     sprintf(buf, pssetlinewidth, physDev->pen.width);
567     PSDRV_WriteSpool(dc, buf, strlen(buf));
568
569     if(physDev->pen.dash) {
570         sprintf(buf, pssetdash, physDev->pen.dash, 0);
571         PSDRV_WriteSpool(dc, buf, strlen(buf));
572     }
573
574     return TRUE;
575 }
576
577 BOOL PSDRV_WriteReencodeFont(DC *dc)
578 {
579     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
580     char *buf, *newbuf;
581  
582     buf = (char *)HeapAlloc( PSDRV_Heap, 0,
583              sizeof(psreencodefont) + 2 * strlen(physDev->font.afm->FontName) 
584                              + sizeof(encodingext));
585
586     if(!buf) {
587         WARN(psdrv, "HeapAlloc failed\n");
588         return FALSE;
589     }
590
591     newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
592               strlen(physDev->font.afm->FontName) + sizeof(encodingext));
593
594     if(!newbuf) {
595         WARN(psdrv, "HeapAlloc failed\n");
596         HeapFree(PSDRV_Heap, 0, buf);
597         return FALSE;
598     }
599
600     sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
601     sprintf(buf, psreencodefont, newbuf, physDev->font.afm->FontName);
602
603     PSDRV_WriteSpool(dc, buf, strlen(buf));
604
605     HeapFree(PSDRV_Heap, 0, newbuf);
606     HeapFree(PSDRV_Heap, 0, buf);
607     return TRUE;
608 }    
609
610 BOOL PSDRV_WriteShow(DC *dc, char *str, INT count)
611 {
612     char *buf, *buf1;
613     INT buflen = count + 10, i, done;
614
615     buf = (char *)HeapAlloc( PSDRV_Heap, 0, buflen );
616     
617     for(i = done = 0; i < count; i++) {
618         if(!isprint(str[i])) {
619             if(done + 4 >= buflen)
620                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
621             sprintf(buf + done, "\\%03o", (int)(unsigned char)str[i] );
622             done += 4;
623         } else if(str[i] == '\\' || str[i] == '(' || str[i] == ')' ) {
624             if(done + 2 >= buflen)
625                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
626             buf[done++] = '\\';
627             buf[done++] = str[i];
628         } else {
629             if(done + 1 >= buflen)
630                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
631             buf[done++] = str[i];
632         }
633     }
634     buf[done] = '\0';
635
636     buf1 = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psshow) + done);
637
638     sprintf(buf1, psshow, buf);
639
640     PSDRV_WriteSpool(dc, buf1, strlen(buf1));
641     HeapFree(PSDRV_Heap, 0, buf);
642     HeapFree(PSDRV_Heap, 0, buf1);
643
644     return TRUE;
645 }    
646
647 BOOL PSDRV_WriteFill(DC *dc)
648 {
649     return PSDRV_WriteSpool(dc, psfill, sizeof(psfill)-1);
650 }
651
652 BOOL PSDRV_WriteEOFill(DC *dc)
653 {
654     return PSDRV_WriteSpool(dc, pseofill, sizeof(pseofill)-1);
655 }
656
657 BOOL PSDRV_WriteGSave(DC *dc)
658 {
659     return PSDRV_WriteSpool(dc, psgsave, sizeof(psgsave)-1);
660 }
661
662 BOOL PSDRV_WriteGRestore(DC *dc)
663 {
664     return PSDRV_WriteSpool(dc, psgrestore, sizeof(psgrestore)-1);
665 }
666
667 BOOL PSDRV_WriteClosePath(DC *dc)
668 {
669     return PSDRV_WriteSpool(dc, psclosepath, sizeof(psclosepath)-1);
670 }
671
672 BOOL PSDRV_WriteClip(DC *dc)
673 {
674     return PSDRV_WriteSpool(dc, psclip, sizeof(psclip)-1);
675 }
676
677 BOOL PSDRV_WriteEOClip(DC *dc)
678 {
679     return PSDRV_WriteSpool(dc, pseoclip, sizeof(pseoclip)-1);
680 }
681
682 BOOL PSDRV_WriteHatch(DC *dc)
683 {
684     return PSDRV_WriteSpool(dc, pshatch, sizeof(pshatch)-1);
685 }
686
687 BOOL PSDRV_WriteRotate(DC *dc, float ang)
688 {
689     char buf[256];
690
691     sprintf(buf, psrotate, ang);
692     return PSDRV_WriteSpool(dc, buf, strlen(buf));
693 }
694
695 BOOL PSDRV_WriteIndexColorSpaceBegin(DC *dc, int size)
696 {
697     char buf[256];
698     sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
699     return PSDRV_WriteSpool(dc, buf, strlen(buf));
700 }
701
702 BOOL PSDRV_WriteIndexColorSpaceEnd(DC *dc)
703 {
704     char buf[] = ">\n] setcolorspace\n";
705     return PSDRV_WriteSpool(dc, buf, sizeof(buf) - 1);
706
707
708 BOOL PSDRV_WriteRGB(DC *dc, COLORREF *map, int number)
709 {
710     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
711     int i;
712
713     ptr = buf;
714     for(i = 0; i < number; i++) {
715         sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]), 
716                 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
717                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
718         ptr += 7;
719     }
720     PSDRV_WriteSpool(dc, buf, number * 7);
721     HeapFree(PSDRV_Heap, 0, buf);
722     return TRUE;
723 }
724
725
726 BOOL PSDRV_WriteImageDict(DC *dc, WORD depth, INT xDst, INT yDst,
727                           INT widthDst, INT heightDst, INT widthSrc,
728                           INT heightSrc)
729 {
730     char start[] = "%d %d translate\n%d %d scale\n<<\n"
731       " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
732       " /ImageMatrix [%d 0 0 %d 0 %d]\n";
733
734     char decode1[] = " /Decode [0 %d]\n";
735     char decode3[] = " /Decode [0 1 0 1 0 1]\n";
736
737     char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
738
739     char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
740
741     sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
742             (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
743
744     PSDRV_WriteSpool(dc, buf, strlen(buf));
745
746     switch(depth) {
747     case 8:
748         sprintf(buf, decode1, 255);
749         break;
750
751     case 4:
752         sprintf(buf, decode1, 15);
753         break;
754
755     case 1:
756         sprintf(buf, decode1, 1);
757         break;
758
759     default:
760         strcpy(buf, decode3);
761         break;
762     }
763
764     PSDRV_WriteSpool(dc, buf, strlen(buf));
765
766     PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
767
768     HeapFree(PSDRV_Heap, 0, buf);
769     return TRUE;
770 }
771
772 BOOL PSDRV_WriteBytes(DC *dc, const BYTE *bytes, int number)
773 {
774     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
775     char *ptr;
776     int i;
777     
778     ptr = buf;
779     
780     for(i = 0; i < number; i++) {
781         sprintf(ptr, "%02x%c", bytes[i],
782                 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
783         ptr += 3;
784     }
785     PSDRV_WriteSpool(dc, buf, number * 3);
786
787     HeapFree(PSDRV_Heap, 0, buf);
788     return TRUE;
789 }
790
791 BOOL PSDRV_WriteDIBits16(DC *dc, const WORD *words, int number)
792 {
793     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
794     char *ptr;
795     int i;
796     
797     ptr = buf;
798     
799     for(i = 0; i < number; i++) {
800         int r, g, b;
801
802         /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
803
804         r = words[i] >> 10 & 0x1f;
805         r = r << 3 | r >> 2;
806         g = words[i] >> 5 & 0x1f;
807         g = g << 3 | g >> 2;
808         b = words[i] & 0x1f;
809         b = b << 3 | b >> 2;
810         sprintf(ptr, "%02x%02x%02x%c", r, g, b,
811                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
812         ptr += 7;
813     }
814     PSDRV_WriteSpool(dc, buf, number * 7);
815
816     HeapFree(PSDRV_Heap, 0, buf);
817     return TRUE;
818 }
819
820 BOOL PSDRV_WriteDIBits24(DC *dc, const BYTE *bits, int number)
821 {
822     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
823     char *ptr;
824     int i;
825     
826     ptr = buf;
827     
828     for(i = 0; i < number; i++) {
829         sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
830                 bits[i * 3],
831                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
832         ptr += 7;
833     }
834     PSDRV_WriteSpool(dc, buf, number * 7);
835
836     HeapFree(PSDRV_Heap, 0, buf);
837     return TRUE;
838 }
839
840 BOOL PSDRV_WriteDIBits32(DC *dc, const BYTE *bits, int number)
841 {
842     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
843     char *ptr;
844     int i;
845     
846     ptr = buf;
847     
848     for(i = 0; i < number; i++) {
849         sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
850                 bits[i * 4],
851                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
852         ptr += 7;
853     }
854     PSDRV_WriteSpool(dc, buf, number * 7);
855
856     HeapFree(PSDRV_Heap, 0, buf);
857     return TRUE;
858 }