Winspool DocumentProperties and DeviceCapabilities should now work on
[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 "debugtools.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 psrrectangle[] = /* x, y, width, height, -width */
108 "%d %d rmoveto\n"
109 "%d 0 rlineto\n"
110 "0 %d rlineto\n"
111 "%d 0 rlineto\n"
112 "closepath\n";
113
114 static char psshow[] = /* string */
115 "(%s) show\n";
116
117 static char pssetfont[] = /* fontname, xscale, yscale, ascent, escapement */
118 "/%s findfont\n"
119 "[%d 0 0 %d 0 0]\n"
120 "%d 10 div matrix rotate\n"
121 "matrix concatmatrix\n"
122 "makefont setfont\n";
123
124 static char pssetlinewidth[] = /* width */
125 "%d setlinewidth\n";
126
127 static char pssetdash[] = /* dash, offset */
128 "[%s] %d setdash\n";
129
130 static char pssetgray[] = /* gray */
131 "%.2f setgray\n";
132
133 static char pssetrgbcolor[] = /* r, g, b */
134 "%.2f %.2f %.2f setrgbcolor\n";
135
136 static char psarc[] = /* x, y, w, h, ang1, ang2 */
137 "tmpmtrx currentmatrix pop\n"
138 "%d %d translate\n"
139 "%d %d scale\n"
140 "0 0 0.5 %.1f %.1f arc\n"
141 "tmpmtrx setmatrix\n";
142
143 static char psgsave[] =
144 "gsave\n";
145
146 static char psgrestore[] =
147 "grestore\n";
148
149 static char psfill[] =
150 "fill\n";
151
152 static char pseofill[] =
153 "eofill\n";
154
155 static char psnewpath[] =
156 "newpath\n";
157
158 static char psclosepath[] =
159 "closepath\n";
160
161 static char psclip[] =
162 "clip\n";
163
164 static char pseoclip[] =
165 "eoclip\n";
166
167 static char psrectclip[] =
168 "%s rectclip\n"; 
169
170 static char pshatch[] =
171 "hatch\n";
172
173 static char psrotate[] = /* ang */
174 "%.1f rotate\n";
175
176 static char psarrayget[] = 
177 "%s %d get\n";
178
179 static char psarrayput[] = 
180 "%s %d %ld put\n";
181
182 static char psarraydef[] = 
183 "/%s %d array def\n";
184
185 char *PSDRV_ANSIVector[256] = {
186 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x00 */
187 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
188 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x10 */
189 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
190 "space",        "exclam",       "quotedbl",     "numbersign", /* 0x20 */
191 "dollar",       "percent",      "ampersand",    "quotesingle",
192 "parenleft",    "parenright",   "asterisk",     "plus",
193 "comma",        "hyphen",       "period",       "slash",
194 "zero",         "one",          "two",          "three", /* 0x30 */
195 "four",         "five",         "six",          "seven",
196 "eight",        "nine",         "colon",        "semicolon",
197 "less",         "equal",        "greater",      "question",
198 "at",           "A",            "B",            "C", /* 0x40 */
199 "D",            "E",            "F",            "G",
200 "H",            "I",            "J",            "K",
201 "L",            "M",            "N",            "O",
202 "P",            "Q",            "R",            "S", /* 0x50 */
203 "T",            "U",            "V",            "W",
204 "X",            "Y",            "Z",            "bracketleft",
205 "backslash",    "bracketright", "asciicircum",  "underscore",
206 "grave",        "a",            "b",            "c", /* 0x60 */
207 "d",            "e",            "f",            "g",
208 "h",            "i",            "j",            "k",
209 "l",            "m",            "n",            "o",
210 "p",            "q",            "r",            "s", /* 0x70 */
211 "t",            "u",            "v",            "w",
212 "x",            "y",            "z",            "braceleft",
213 "bar",          "braceright",   "asciitilde",   NULL,
214 NULL,           NULL,           NULL,           NULL, /* 0x80 */
215 NULL,           NULL,           NULL,           NULL,
216 NULL,           NULL,           NULL,           NULL,
217 NULL,           NULL,           NULL,           NULL,
218 NULL,           "quoteleft",    "quoteright",   "quotedblleft", /* 0x90 */
219 "quotedblright","bullet",       "endash",       "emdash",
220 NULL,           NULL,           NULL,           NULL,
221 NULL,           NULL,           NULL,           NULL,
222 "space",        "exclamdown",   "cent",         "sterling", /* 0xa0 */
223 "currency",     "yen",          "brokenbar",    "section",
224 "dieresis",     "copyright",    "ordfeminine",  "guillemotleft",
225 "logicalnot",   "hyphen",       "registered",   "macron",
226 "degree",       "plusminus",    "twosuperior",  "threesuperior", /* 0xb0 */
227 "acute",        "mu",           "paragraph",    "periodcentered",
228 "cedilla",      "onesuperior",  "ordmasculine", "guillemotright",
229 "onequarter",   "onehalf",      "threequarters","questiondown",
230 "Agrave",       "Aacute",       "Acircumflex",  "Atilde", /* 0xc0 */
231 "Adieresis",    "Aring",        "AE",           "Ccedilla",
232 "Egrave",       "Eacute",       "Ecircumflex",  "Edieresis",
233 "Igrave",       "Iacute",       "Icircumflex",  "Idieresis",
234 "Eth",          "Ntilde",       "Ograve",       "Oacute", /* 0xd0 */
235 "Ocircumflex",  "Otilde",       "Odieresis",    "multiply",
236 "Oslash",       "Ugrave",       "Uacute",       "Ucircumflex",
237 "Udieresis",    "Yacute",       "Thorn",        "germandbls",
238 "agrave",       "aacute",       "acircumflex",  "atilde", /* 0xe0 */
239 "adieresis",    "aring",        "ae",           "ccedilla",
240 "egrave",       "eacute",       "ecircumflex",  "edieresis",
241 "igrave",       "iacute",       "icircumflex",  "idieresis",
242 "eth",          "ntilde",       "ograve",       "oacute", /* 0xf0 */
243 "ocircumflex",  "otilde",       "odieresis",    "divide",
244 "oslash",       "ugrave",       "uacute",       "ucircumflex",
245 "udieresis",    "yacute",       "thorn",        "ydieresis"
246 };
247
248
249 char psreencodefont[] = /* newfontname basefontname*/
250 "/%s /%s reencodefont\n";
251
252
253 int PSDRV_WriteSpool(DC *dc, LPSTR lpData, WORD cch)
254 {
255     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
256
257     if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
258         if( !PSDRV_StartPage(dc) )
259             return FALSE;
260     }
261     return WriteSpool16( physDev->job.hJob, lpData, cch );
262 }
263
264
265 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
266                          char *invocation)
267 {
268
269     char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
270                              strlen(feature) + strlen(value));
271
272
273     sprintf(buf, psbeginfeature, feature, value);
274     WriteSpool16( hJob, buf, strlen(buf) );
275
276     WriteSpool16( hJob, invocation, strlen(invocation) );
277
278     WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
279     
280     HeapFree( PSDRV_Heap, 0, buf );
281     return 1;
282 }
283
284
285
286 INT PSDRV_WriteHeader( DC *dc, LPCSTR title )
287 {
288     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
289     char *buf, *orient, vectbuf[256];
290     INPUTSLOT *slot;
291     PAGESIZE *page;
292     int urx, ury, i, j;
293
294     TRACE("'%s'\n", title);
295
296     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
297                              strlen(title) + 30 );
298     if(!buf) {
299         WARN("HeapAlloc failed\n");
300         return 0;
301     }
302
303     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
304       /* BBox co-ords are in default user co-ord system so urx < ury even in
305          landscape mode */
306         urx = (int) (dc->w.devCaps->vertSize * 72.0 / 25.4);
307         ury = (int) (dc->w.devCaps->horzSize * 72.0 / 25.4);
308         orient = "Landscape";
309     } else {
310         urx = (int) (dc->w.devCaps->horzSize * 72.0 / 25.4);
311         ury = (int) (dc->w.devCaps->vertSize * 72.0 / 25.4);
312         orient = "Portrait";
313     }
314
315     /* FIXME should do something better with BBox */
316
317     sprintf(buf, psheader, title, 0, 0, urx, ury, orient);              
318
319     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
320                                                      strlen(buf) ) {
321         WARN("WriteSpool error\n");
322         HeapFree( PSDRV_Heap, 0, buf );
323         return 0;
324     }
325     HeapFree( PSDRV_Heap, 0, buf );
326
327     WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
328     WriteSpool16( physDev->job.hJob, psvectorstart, strlen(psvectorstart) );
329     
330     for(i = 0; i < 256; i += 8) {
331         vectbuf[0] = '\0';
332         for(j = 0; j < 8; j++) {
333             strcat(vectbuf, "/");
334             if(PSDRV_ANSIVector[i+j]) {
335                 strcat(vectbuf, PSDRV_ANSIVector[i+j]);
336                 strcat(vectbuf, " ");
337             } else {
338                 strcat(vectbuf, ".notdef ");
339             }
340         }
341         strcat(vectbuf, "\n");
342         WriteSpool16( physDev->job.hJob, vectbuf, strlen(vectbuf) );
343     }
344
345     WriteSpool16( physDev->job.hJob, psvectorend, strlen(psvectorend) );
346     WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
347     WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
348
349
350     WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
351
352     for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
353         if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
354             if(slot->InvocationString) {
355                 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
356                              slot->InvocationString);
357                 break;
358             }
359         }
360     }
361
362     for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
363         if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
364             if(page->InvocationString) {
365                 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
366                              page->InvocationString);
367                 break;
368             }
369         }
370     }
371
372     WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
373
374
375     return 1;
376 }
377
378
379 INT PSDRV_WriteFooter( DC *dc )
380 {
381     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
382     char *buf;
383
384     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
385     if(!buf) {
386         WARN("HeapAlloc failed\n");
387         return 0;
388     }
389
390     sprintf(buf, psfooter, physDev->job.PageNo);
391
392     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
393                                                      strlen(buf) ) {
394         WARN("WriteSpool error\n");
395         HeapFree( PSDRV_Heap, 0, buf );
396         return 0;
397     }
398     HeapFree( PSDRV_Heap, 0, buf );
399     return 1;
400 }
401
402
403
404 INT PSDRV_WriteEndPage( DC *dc )
405 {
406     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
407
408     if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) != 
409                                                      sizeof(psendpage)-1 ) {
410         WARN("WriteSpool error\n");
411         return 0;
412     }
413     return 1;
414 }
415
416
417
418
419 INT PSDRV_WriteNewPage( DC *dc )
420 {
421     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
422     char *buf;
423     char name[100];
424     signed int xtrans, ytrans, rotation;
425
426     sprintf(name, "%d", physDev->job.PageNo);
427
428     buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
429     if(!buf) {
430         WARN("HeapAlloc failed\n");
431         return 0;
432     }
433
434     if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
435         if(physDev->pi->ppd->LandscapeOrientation == -90) {
436             xtrans = dc->w.devCaps->vertRes;
437             ytrans = dc->w.devCaps->horzRes;
438             rotation = 90;
439         } else {
440             xtrans = ytrans = 0;
441             rotation = -90;
442         }
443     } else {
444         xtrans = 0;
445         ytrans = dc->w.devCaps->vertRes;
446         rotation = 0;
447     }
448
449     sprintf(buf, psnewpage, name, physDev->job.PageNo,
450             dc->w.devCaps->logPixelsX, dc->w.devCaps->logPixelsY,
451             xtrans, ytrans, rotation);
452
453     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) != 
454                                                      strlen(buf) ) {
455         WARN("WriteSpool error\n");
456         HeapFree( PSDRV_Heap, 0, buf );
457         return 0;
458     }
459     HeapFree( PSDRV_Heap, 0, buf );
460     return 1;
461 }
462
463
464 BOOL PSDRV_WriteMoveTo(DC *dc, INT x, INT y)
465 {
466     char buf[100];
467
468     sprintf(buf, psmoveto, x, y);
469     return PSDRV_WriteSpool(dc, buf, strlen(buf));
470 }
471
472 BOOL PSDRV_WriteLineTo(DC *dc, INT x, INT y)
473 {
474     char buf[100];
475
476     sprintf(buf, pslineto, x, y);
477     return PSDRV_WriteSpool(dc, buf, strlen(buf));
478 }
479
480
481 BOOL PSDRV_WriteStroke(DC *dc)
482 {
483     return PSDRV_WriteSpool(dc, psstroke, sizeof(psstroke)-1);
484 }
485
486
487
488 BOOL PSDRV_WriteRectangle(DC *dc, INT x, INT y, INT width, 
489                         INT height)
490 {
491     char buf[100];
492
493     sprintf(buf, psrectangle, x, y, width, height, -width);
494     return PSDRV_WriteSpool(dc, buf, strlen(buf));
495 }
496
497 BOOL PSDRV_WriteRRectangle(DC *dc, INT x, INT y, INT width,
498       INT height)
499 {
500     char buf[100];
501
502     sprintf(buf, psrrectangle, x, y, width, height, -width);
503     return PSDRV_WriteSpool(dc, buf, strlen(buf));
504 }
505
506 BOOL PSDRV_WriteArc(DC *dc, INT x, INT y, INT w, INT h, double ang1,
507                       double ang2)
508 {
509     char buf[256];
510
511     /* Make angles -ve and swap order because we're working with an upside
512        down y-axis */
513     sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
514     return PSDRV_WriteSpool(dc, buf, strlen(buf));
515 }
516
517 static char encodingext[] = "-ANSI";
518
519 BOOL PSDRV_WriteSetFont(DC *dc, BOOL UseANSI)
520 {
521     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
522     char *buf, *newbuf;
523
524     buf = (char *)HeapAlloc( PSDRV_Heap, 0,
525              sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
526
527     if(!buf) {
528         WARN("HeapAlloc failed\n");
529         return FALSE;
530     }
531
532     newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
533               strlen(physDev->font.afm->FontName) + sizeof(encodingext));
534
535     if(!newbuf) {
536         WARN("HeapAlloc failed\n");
537         HeapFree(PSDRV_Heap, 0, buf);
538         return FALSE;
539     }
540
541     if(UseANSI)
542         sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
543     else
544         strcpy(newbuf, physDev->font.afm->FontName);
545
546     sprintf(buf, pssetfont, newbuf, 
547                 physDev->font.size, -physDev->font.size,
548                 -physDev->font.escapement);
549
550     PSDRV_WriteSpool(dc, buf, strlen(buf));
551     HeapFree(PSDRV_Heap, 0, buf);
552     return TRUE;
553 }    
554
555 BOOL PSDRV_WriteSetColor(DC *dc, PSCOLOR *color)
556 {
557     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
558     char buf[256];
559
560     PSDRV_CopyColor(&physDev->inkColor, color);
561     switch(color->type) {
562     case PSCOLOR_RGB:
563         sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
564                 color->value.rgb.b);
565         return PSDRV_WriteSpool(dc, buf, strlen(buf));
566
567     case PSCOLOR_GRAY:  
568         sprintf(buf, pssetgray, color->value.gray.i);
569         return PSDRV_WriteSpool(dc, buf, strlen(buf));
570         
571     default:
572         ERR("Unkonwn colour type %d\n", color->type);
573         break;
574     }
575
576     return FALSE;
577 }
578
579 BOOL PSDRV_WriteSetPen(DC *dc)
580 {
581     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
582     char buf[256];
583
584     sprintf(buf, pssetlinewidth, physDev->pen.width);
585     PSDRV_WriteSpool(dc, buf, strlen(buf));
586
587     if(physDev->pen.dash) {
588         sprintf(buf, pssetdash, physDev->pen.dash, 0);
589         PSDRV_WriteSpool(dc, buf, strlen(buf));
590     }
591
592     return TRUE;
593 }
594
595 BOOL PSDRV_WriteReencodeFont(DC *dc)
596 {
597     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
598     char *buf, *newbuf;
599  
600     buf = (char *)HeapAlloc( PSDRV_Heap, 0,
601              sizeof(psreencodefont) + 2 * strlen(physDev->font.afm->FontName) 
602                              + sizeof(encodingext));
603
604     if(!buf) {
605         WARN("HeapAlloc failed\n");
606         return FALSE;
607     }
608
609     newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
610               strlen(physDev->font.afm->FontName) + sizeof(encodingext));
611
612     if(!newbuf) {
613         WARN("HeapAlloc failed\n");
614         HeapFree(PSDRV_Heap, 0, buf);
615         return FALSE;
616     }
617
618     sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
619     sprintf(buf, psreencodefont, newbuf, physDev->font.afm->FontName);
620
621     PSDRV_WriteSpool(dc, buf, strlen(buf));
622
623     HeapFree(PSDRV_Heap, 0, newbuf);
624     HeapFree(PSDRV_Heap, 0, buf);
625     return TRUE;
626 }    
627
628 BOOL PSDRV_WriteShow(DC *dc, char *str, INT count)
629 {
630     char *buf, *buf1;
631     INT buflen = count + 10, i, done;
632
633     buf = (char *)HeapAlloc( PSDRV_Heap, 0, buflen );
634     
635     for(i = done = 0; i < count; i++) {
636         if(!isprint(str[i])) {
637             if(done + 4 >= buflen)
638                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
639             sprintf(buf + done, "\\%03o", (int)(unsigned char)str[i] );
640             done += 4;
641         } else if(str[i] == '\\' || str[i] == '(' || str[i] == ')' ) {
642             if(done + 2 >= buflen)
643                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
644             buf[done++] = '\\';
645             buf[done++] = str[i];
646         } else {
647             if(done + 1 >= buflen)
648                 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
649             buf[done++] = str[i];
650         }
651     }
652     buf[done] = '\0';
653
654     buf1 = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psshow) + done);
655
656     sprintf(buf1, psshow, buf);
657
658     PSDRV_WriteSpool(dc, buf1, strlen(buf1));
659     HeapFree(PSDRV_Heap, 0, buf);
660     HeapFree(PSDRV_Heap, 0, buf1);
661
662     return TRUE;
663 }    
664
665 BOOL PSDRV_WriteFill(DC *dc)
666 {
667     return PSDRV_WriteSpool(dc, psfill, sizeof(psfill)-1);
668 }
669
670 BOOL PSDRV_WriteEOFill(DC *dc)
671 {
672     return PSDRV_WriteSpool(dc, pseofill, sizeof(pseofill)-1);
673 }
674
675 BOOL PSDRV_WriteGSave(DC *dc)
676 {
677     return PSDRV_WriteSpool(dc, psgsave, sizeof(psgsave)-1);
678 }
679
680 BOOL PSDRV_WriteGRestore(DC *dc)
681 {
682     return PSDRV_WriteSpool(dc, psgrestore, sizeof(psgrestore)-1);
683 }
684
685 BOOL PSDRV_WriteNewPath(DC *dc)
686 {
687     return PSDRV_WriteSpool(dc, psnewpath, sizeof(psnewpath)-1);
688 }
689
690 BOOL PSDRV_WriteClosePath(DC *dc)
691 {
692     return PSDRV_WriteSpool(dc, psclosepath, sizeof(psclosepath)-1);
693 }
694
695 BOOL PSDRV_WriteClip(DC *dc)
696 {
697     return PSDRV_WriteSpool(dc, psclip, sizeof(psclip)-1);
698 }
699
700 BOOL PSDRV_WriteEOClip(DC *dc)
701 {
702     return PSDRV_WriteSpool(dc, pseoclip, sizeof(pseoclip)-1);
703 }
704
705 BOOL PSDRV_WriteHatch(DC *dc)
706 {
707     return PSDRV_WriteSpool(dc, pshatch, sizeof(pshatch)-1);
708 }
709
710 BOOL PSDRV_WriteRotate(DC *dc, float ang)
711 {
712     char buf[256];
713
714     sprintf(buf, psrotate, ang);
715     return PSDRV_WriteSpool(dc, buf, strlen(buf));
716 }
717
718 BOOL PSDRV_WriteIndexColorSpaceBegin(DC *dc, int size)
719 {
720     char buf[256];
721     sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
722     return PSDRV_WriteSpool(dc, buf, strlen(buf));
723 }
724
725 BOOL PSDRV_WriteIndexColorSpaceEnd(DC *dc)
726 {
727     char buf[] = ">\n] setcolorspace\n";
728     return PSDRV_WriteSpool(dc, buf, sizeof(buf) - 1);
729
730
731 BOOL PSDRV_WriteRGB(DC *dc, COLORREF *map, int number)
732 {
733     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
734     int i;
735
736     ptr = buf;
737     for(i = 0; i < number; i++) {
738         sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]), 
739                 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
740                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
741         ptr += 7;
742     }
743     PSDRV_WriteSpool(dc, buf, number * 7);
744     HeapFree(PSDRV_Heap, 0, buf);
745     return TRUE;
746 }
747
748
749 BOOL PSDRV_WriteImageDict(DC *dc, WORD depth, INT xDst, INT yDst,
750                           INT widthDst, INT heightDst, INT widthSrc,
751                           INT heightSrc, char *bits)
752 {
753     char start[] = "%d %d translate\n%d %d scale\n<<\n"
754       " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
755       " /ImageMatrix [%d 0 0 %d 0 %d]\n";
756
757     char decode1[] = " /Decode [0 %d]\n";
758     char decode3[] = " /Decode [0 1 0 1 0 1]\n";
759
760     char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
761     char endbits[] = " /DataSource <%s>\n>> image\n";
762
763     char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
764
765     sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
766             (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
767
768     PSDRV_WriteSpool(dc, buf, strlen(buf));
769
770     switch(depth) {
771     case 8:
772         sprintf(buf, decode1, 255);
773         break;
774
775     case 4:
776         sprintf(buf, decode1, 15);
777         break;
778
779     case 1:
780         sprintf(buf, decode1, 1);
781         break;
782
783     default:
784         strcpy(buf, decode3);
785         break;
786     }
787
788     PSDRV_WriteSpool(dc, buf, strlen(buf));
789
790     if(!bits)
791         PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
792     else {
793         sprintf(buf, endbits, bits);
794         PSDRV_WriteSpool(dc, buf, strlen(buf));
795     }
796
797     HeapFree(PSDRV_Heap, 0, buf);
798     return TRUE;
799 }
800
801
802 BOOL PSDRV_WriteBytes(DC *dc, const BYTE *bytes, int number)
803 {
804     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
805     char *ptr;
806     int i;
807     
808     ptr = buf;
809     
810     for(i = 0; i < number; i++) {
811         sprintf(ptr, "%02x%c", bytes[i],
812                 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
813         ptr += 3;
814     }
815     PSDRV_WriteSpool(dc, buf, number * 3);
816
817     HeapFree(PSDRV_Heap, 0, buf);
818     return TRUE;
819 }
820
821 BOOL PSDRV_WriteDIBits16(DC *dc, const WORD *words, int number)
822 {
823     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
824     char *ptr;
825     int i;
826     
827     ptr = buf;
828     
829     for(i = 0; i < number; i++) {
830         int r, g, b;
831
832         /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
833
834         r = words[i] >> 10 & 0x1f;
835         r = r << 3 | r >> 2;
836         g = words[i] >> 5 & 0x1f;
837         g = g << 3 | g >> 2;
838         b = words[i] & 0x1f;
839         b = b << 3 | b >> 2;
840         sprintf(ptr, "%02x%02x%02x%c", r, g, b,
841                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
842         ptr += 7;
843     }
844     PSDRV_WriteSpool(dc, buf, number * 7);
845
846     HeapFree(PSDRV_Heap, 0, buf);
847     return TRUE;
848 }
849
850 BOOL PSDRV_WriteDIBits24(DC *dc, const BYTE *bits, int number)
851 {
852     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
853     char *ptr;
854     int i;
855     
856     ptr = buf;
857     
858     for(i = 0; i < number; i++) {
859         sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
860                 bits[i * 3],
861                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
862         ptr += 7;
863     }
864     PSDRV_WriteSpool(dc, buf, number * 7);
865
866     HeapFree(PSDRV_Heap, 0, buf);
867     return TRUE;
868 }
869
870 BOOL PSDRV_WriteDIBits32(DC *dc, const BYTE *bits, int number)
871 {
872     char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
873     char *ptr;
874     int i;
875     
876     ptr = buf;
877     
878     for(i = 0; i < number; i++) {
879         sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
880                 bits[i * 4],
881                 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
882         ptr += 7;
883     }
884     PSDRV_WriteSpool(dc, buf, number * 7);
885
886     HeapFree(PSDRV_Heap, 0, buf);
887     return TRUE;
888 }
889
890 BOOL PSDRV_WriteArrayGet(DC *dc, CHAR *pszArrayName, INT nIndex)
891 {
892     char buf[100];
893
894     sprintf(buf, psarrayget, pszArrayName, nIndex);
895     return PSDRV_WriteSpool(dc, buf, strlen(buf));
896 }
897
898 BOOL PSDRV_WriteArrayPut(DC *dc, CHAR *pszArrayName, INT nIndex, LONG lObject)
899 {
900     char buf[100];
901
902     sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
903     return PSDRV_WriteSpool(dc, buf, strlen(buf));
904 }
905
906 BOOL PSDRV_WriteArrayDef(DC *dc, CHAR *pszArrayName, INT nSize)
907 {
908     char buf[100];
909
910     sprintf(buf, psarraydef, pszArrayName, nSize);
911     return PSDRV_WriteSpool(dc, buf, strlen(buf));
912 }
913
914 BOOL PSDRV_WriteRectClip(DC *dc, CHAR *pszArrayName)
915 {
916     char buf[100];
917
918     sprintf(buf, psrectclip, pszArrayName);
919     return PSDRV_WriteSpool(dc, buf, strlen(buf));
920 }
921
922
923 BOOL PSDRV_WritePatternDict(DC *dc, BITMAP *bm, BYTE *bits)
924 {
925     char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
926       "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n";
927
928     char end[] = "  end\n }\n>>\n matrix makepattern setpattern\n";
929     char *buf, *ptr;
930     INT w, h, x, y;
931     COLORREF map[2];
932
933     w = bm->bmWidth & ~0x7;
934     h = bm->bmHeight & ~0x7;
935
936     buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
937     sprintf(buf, start, w, h, w, h);
938     PSDRV_WriteSpool(dc,  buf, strlen(buf));
939     PSDRV_WriteIndexColorSpaceBegin(dc, 1);
940     map[0] = dc->w.textColor;
941     map[1] = dc->w.backgroundColor;
942     PSDRV_WriteRGB(dc, map, 2);
943     PSDRV_WriteIndexColorSpaceEnd(dc);
944     ptr = buf;
945     for(y = h-1; y >= 0; y--) {
946         for(x = 0; x < w/8; x++) {
947             sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
948             ptr += 2;
949         }
950     }
951     PSDRV_WriteImageDict(dc, 1, 0, 0, 8, 8, 8, 8, buf);
952     PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
953     HeapFree(PSDRV_Heap, 0, buf);
954     return TRUE;
955 }
956
957
958
959