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