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