Rewrite TrueType font metric parsing code.
[wine.git] / dlls / wineps / ps.c
blob64e27b0cb9c11ca9965157af6300dd71513b32f3
1 /*
2 * PostScript output functions
4 * Copyright 1998 Huw D M Davies
6 */
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include "psdrv.h"
12 #include "winspool.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(psdrv);
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";
26 static char psbeginprolog[] =
27 "%%BeginProlog\n";
29 static char psendprolog[] =
30 "%%EndProlog\n";
32 static char psvectorstart[] =
33 "/ANSIEncoding [\n";
35 static char psvectorend[] =
36 "] def\n";
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";
59 static char psbeginsetup[] =
60 "%%BeginSetup\n";
62 static char psendsetup[] =
63 "%%EndSetup\n";
65 static char psbeginfeature[] = /* feature, value */
66 "mark {\n"
67 "%%%%BeginFeature: %s %s\n";
69 static char psendfeature[] =
70 "\n%%EndFeature\n"
71 "} stopped cleartomark\n";
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";
83 static char psendpage[] =
84 "pgsave restore\n"
85 "showpage\n";
87 static char psfooter[] = /* pages */
88 "%%%%Trailer\n"
89 "%%%%Pages: %d\n"
90 "%%%%EOF\n";
92 static char psmoveto[] = /* x, y */
93 "%d %d moveto\n";
95 static char pslineto[] = /* x, y */
96 "%d %d lineto\n";
98 static char psstroke[] =
99 "stroke\n";
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";
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";
115 #if 0
116 static char psshow[] = /* string */
117 "(%s) show\n";
118 #endif
120 static const char psglyphshow[] = /* glyph name */
121 "/%s glyphshow\n";
123 static char pssetfont[] = /* fontname, xscale, yscale, ascent, escapement */
124 "/%s findfont\n"
125 "[%d 0 0 %d 0 0]\n"
126 "%d 10 div matrix rotate\n"
127 "matrix concatmatrix\n"
128 "makefont setfont\n";
130 static char pssetlinewidth[] = /* width */
131 "%d setlinewidth\n";
133 static char pssetdash[] = /* dash, offset */
134 "[%s] %d setdash\n";
136 static char pssetgray[] = /* gray */
137 "%.2f setgray\n";
139 static char pssetrgbcolor[] = /* r, g, b */
140 "%.2f %.2f %.2f setrgbcolor\n";
142 static char psarc[] = /* x, y, w, h, ang1, ang2 */
143 "tmpmtrx currentmatrix pop\n"
144 "%d %d translate\n"
145 "%d %d scale\n"
146 "0 0 0.5 %.1f %.1f arc\n"
147 "tmpmtrx setmatrix\n";
149 static char psgsave[] =
150 "gsave\n";
152 static char psgrestore[] =
153 "grestore\n";
155 static char psfill[] =
156 "fill\n";
158 static char pseofill[] =
159 "eofill\n";
161 static char psnewpath[] =
162 "newpath\n";
164 static char psclosepath[] =
165 "closepath\n";
167 static char psclip[] =
168 "clip\n";
170 static char psinitclip[] =
171 "initclip\n";
173 static char pseoclip[] =
174 "eoclip\n";
176 static char psrectclip[] =
177 "%d %d %d %d rectclip\n";
179 static char psrectclip2[] =
180 "%s rectclip\n";
182 static char pshatch[] =
183 "hatch\n";
185 static char psrotate[] = /* ang */
186 "%.1f rotate\n";
188 static char psarrayget[] =
189 "%s %d get\n";
191 static char psarrayput[] =
192 "%s %d %ld put\n";
194 static char psarraydef[] =
195 "/%s %d array def\n";
197 char *PSDRV_ANSIVector[256] = {
198 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x00 */
199 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
200 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 0x10 */
201 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
202 "space", "exclam", "quotedbl", "numbersign", /* 0x20 */
203 "dollar", "percent", "ampersand", "quotesingle",
204 "parenleft", "parenright", "asterisk", "plus",
205 "comma", "hyphen", "period", "slash",
206 "zero", "one", "two", "three", /* 0x30 */
207 "four", "five", "six", "seven",
208 "eight", "nine", "colon", "semicolon",
209 "less", "equal", "greater", "question",
210 "at", "A", "B", "C", /* 0x40 */
211 "D", "E", "F", "G",
212 "H", "I", "J", "K",
213 "L", "M", "N", "O",
214 "P", "Q", "R", "S", /* 0x50 */
215 "T", "U", "V", "W",
216 "X", "Y", "Z", "bracketleft",
217 "backslash", "bracketright", "asciicircum", "underscore",
218 "grave", "a", "b", "c", /* 0x60 */
219 "d", "e", "f", "g",
220 "h", "i", "j", "k",
221 "l", "m", "n", "o",
222 "p", "q", "r", "s", /* 0x70 */
223 "t", "u", "v", "w",
224 "x", "y", "z", "braceleft",
225 "bar", "braceright", "asciitilde", NULL,
226 NULL, NULL, NULL, NULL, /* 0x80 */
227 NULL, NULL, NULL, NULL,
228 NULL, NULL, NULL, NULL,
229 NULL, NULL, NULL, NULL,
230 NULL, "quoteleft", "quoteright", "quotedblleft", /* 0x90 */
231 "quotedblright","bullet", "endash", "emdash",
232 NULL, NULL, NULL, NULL,
233 NULL, NULL, NULL, NULL,
234 "space", "exclamdown", "cent", "sterling", /* 0xa0 */
235 "currency", "yen", "brokenbar", "section",
236 "dieresis", "copyright", "ordfeminine", "guillemotleft",
237 "logicalnot", "hyphen", "registered", "macron",
238 "degree", "plusminus", "twosuperior", "threesuperior", /* 0xb0 */
239 "acute", "mu", "paragraph", "periodcentered",
240 "cedilla", "onesuperior", "ordmasculine", "guillemotright",
241 "onequarter", "onehalf", "threequarters","questiondown",
242 "Agrave", "Aacute", "Acircumflex", "Atilde", /* 0xc0 */
243 "Adieresis", "Aring", "AE", "Ccedilla",
244 "Egrave", "Eacute", "Ecircumflex", "Edieresis",
245 "Igrave", "Iacute", "Icircumflex", "Idieresis",
246 "Eth", "Ntilde", "Ograve", "Oacute", /* 0xd0 */
247 "Ocircumflex", "Otilde", "Odieresis", "multiply",
248 "Oslash", "Ugrave", "Uacute", "Ucircumflex",
249 "Udieresis", "Yacute", "Thorn", "germandbls",
250 "agrave", "aacute", "acircumflex", "atilde", /* 0xe0 */
251 "adieresis", "aring", "ae", "ccedilla",
252 "egrave", "eacute", "ecircumflex", "edieresis",
253 "igrave", "iacute", "icircumflex", "idieresis",
254 "eth", "ntilde", "ograve", "oacute", /* 0xf0 */
255 "ocircumflex", "otilde", "odieresis", "divide",
256 "oslash", "ugrave", "uacute", "ucircumflex",
257 "udieresis", "yacute", "thorn", "ydieresis"
261 char psreencodefont[] = /* newfontname basefontname*/
262 "/%s /%s reencodefont\n";
265 int PSDRV_WriteSpool(DC *dc, LPSTR lpData, WORD cch)
267 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
269 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
270 if( !PSDRV_StartPage(dc) )
271 return FALSE;
273 return WriteSpool16( physDev->job.hJob, lpData, cch );
277 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
278 char *invocation)
281 char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
282 strlen(feature) + strlen(value));
285 sprintf(buf, psbeginfeature, feature, value);
286 WriteSpool16( hJob, buf, strlen(buf) );
288 WriteSpool16( hJob, invocation, strlen(invocation) );
290 WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
292 HeapFree( PSDRV_Heap, 0, buf );
293 return 1;
298 INT PSDRV_WriteHeader( DC *dc, LPCSTR title )
300 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
301 char *buf, *orient, vectbuf[256];
302 INPUTSLOT *slot;
303 PAGESIZE *page;
304 int llx, lly, urx, ury, i, j;
306 TRACE("'%s'\n", title);
308 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
309 strlen(title) + 30 );
310 if(!buf) {
311 WARN("HeapAlloc failed\n");
312 return 0;
315 /* BBox co-ords are in default user co-ord system so urx < ury even in
316 landscape mode */
317 llx = physDev->PageSize.left * 72.0 / dc->devCaps->logPixelsX;
318 lly = physDev->PageSize.bottom * 72.0 / dc->devCaps->logPixelsY;
319 urx = physDev->PageSize.right * 72.0 / dc->devCaps->logPixelsX;
320 ury = physDev->PageSize.top * 72.0 / dc->devCaps->logPixelsY;
322 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
323 orient = "Landscape";
324 } else {
325 orient = "Portrait";
328 /* FIXME should do something better with BBox */
330 sprintf(buf, psheader, title, llx, lly, urx, ury, orient);
332 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
333 strlen(buf) ) {
334 WARN("WriteSpool error\n");
335 HeapFree( PSDRV_Heap, 0, buf );
336 return 0;
338 HeapFree( PSDRV_Heap, 0, buf );
340 WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
341 WriteSpool16( physDev->job.hJob, psvectorstart, strlen(psvectorstart) );
343 for(i = 0; i < 256; i += 8) {
344 vectbuf[0] = '\0';
345 for(j = 0; j < 8; j++) {
346 strcat(vectbuf, "/");
347 if(PSDRV_ANSIVector[i+j]) {
348 strcat(vectbuf, PSDRV_ANSIVector[i+j]);
349 strcat(vectbuf, " ");
350 } else {
351 strcat(vectbuf, ".notdef ");
354 strcat(vectbuf, "\n");
355 WriteSpool16( physDev->job.hJob, vectbuf, strlen(vectbuf) );
358 WriteSpool16( physDev->job.hJob, psvectorend, strlen(psvectorend) );
359 WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
360 WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
363 WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
365 for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
366 if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
367 if(slot->InvocationString) {
368 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
369 slot->InvocationString);
370 break;
375 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
376 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
377 if(page->InvocationString) {
378 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
379 page->InvocationString);
380 break;
385 WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
388 return 1;
392 INT PSDRV_WriteFooter( DC *dc )
394 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
395 char *buf;
397 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
398 if(!buf) {
399 WARN("HeapAlloc failed\n");
400 return 0;
403 sprintf(buf, psfooter, physDev->job.PageNo);
405 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
406 strlen(buf) ) {
407 WARN("WriteSpool error\n");
408 HeapFree( PSDRV_Heap, 0, buf );
409 return 0;
411 HeapFree( PSDRV_Heap, 0, buf );
412 return 1;
417 INT PSDRV_WriteEndPage( DC *dc )
419 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
421 if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) !=
422 sizeof(psendpage)-1 ) {
423 WARN("WriteSpool error\n");
424 return 0;
426 return 1;
432 INT PSDRV_WriteNewPage( DC *dc )
434 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
435 char *buf;
436 char name[100];
437 signed int xtrans, ytrans, rotation;
439 sprintf(name, "%d", physDev->job.PageNo);
441 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
442 if(!buf) {
443 WARN("HeapAlloc failed\n");
444 return 0;
447 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
448 if(physDev->pi->ppd->LandscapeOrientation == -90) {
449 xtrans = physDev->PageSize.right;
450 ytrans = physDev->PageSize.top;
451 rotation = 90;
452 } else {
453 xtrans = physDev->PageSize.left;
454 ytrans = physDev->PageSize.bottom;
455 rotation = -90;
457 } else {
458 xtrans = physDev->PageSize.left;
459 ytrans = physDev->PageSize.top;
460 rotation = 0;
463 sprintf(buf, psnewpage, name, physDev->job.PageNo,
464 dc->devCaps->logPixelsX, dc->devCaps->logPixelsY,
465 xtrans, ytrans, rotation);
467 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
468 strlen(buf) ) {
469 WARN("WriteSpool error\n");
470 HeapFree( PSDRV_Heap, 0, buf );
471 return 0;
473 HeapFree( PSDRV_Heap, 0, buf );
474 return 1;
478 BOOL PSDRV_WriteMoveTo(DC *dc, INT x, INT y)
480 char buf[100];
482 sprintf(buf, psmoveto, x, y);
483 return PSDRV_WriteSpool(dc, buf, strlen(buf));
486 BOOL PSDRV_WriteLineTo(DC *dc, INT x, INT y)
488 char buf[100];
490 sprintf(buf, pslineto, x, y);
491 return PSDRV_WriteSpool(dc, buf, strlen(buf));
495 BOOL PSDRV_WriteStroke(DC *dc)
497 return PSDRV_WriteSpool(dc, psstroke, sizeof(psstroke)-1);
502 BOOL PSDRV_WriteRectangle(DC *dc, INT x, INT y, INT width,
503 INT height)
505 char buf[100];
507 sprintf(buf, psrectangle, x, y, width, height, -width);
508 return PSDRV_WriteSpool(dc, buf, strlen(buf));
511 BOOL PSDRV_WriteRRectangle(DC *dc, INT x, INT y, INT width,
512 INT height)
514 char buf[100];
516 sprintf(buf, psrrectangle, x, y, width, height, -width);
517 return PSDRV_WriteSpool(dc, buf, strlen(buf));
520 BOOL PSDRV_WriteArc(DC *dc, INT x, INT y, INT w, INT h, double ang1,
521 double ang2)
523 char buf[256];
525 /* Make angles -ve and swap order because we're working with an upside
526 down y-axis */
527 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
528 return PSDRV_WriteSpool(dc, buf, strlen(buf));
531 static char encodingext[] = "-ANSI";
533 BOOL PSDRV_WriteSetFont(DC *dc, BOOL UseANSI)
535 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
536 char *buf, *newbuf;
538 buf = (char *)HeapAlloc( PSDRV_Heap, 0,
539 sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
541 if(!buf) {
542 WARN("HeapAlloc failed\n");
543 return FALSE;
546 newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
547 strlen(physDev->font.afm->FontName) + sizeof(encodingext));
549 if(!newbuf) {
550 WARN("HeapAlloc failed\n");
551 HeapFree(PSDRV_Heap, 0, buf);
552 return FALSE;
555 if(UseANSI)
556 sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
557 else
558 strcpy(newbuf, physDev->font.afm->FontName);
560 sprintf(buf, pssetfont, newbuf,
561 physDev->font.size, -physDev->font.size,
562 -physDev->font.escapement);
564 PSDRV_WriteSpool(dc, buf, strlen(buf));
565 HeapFree(PSDRV_Heap, 0, buf);
566 return TRUE;
569 BOOL PSDRV_WriteSetColor(DC *dc, PSCOLOR *color)
571 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
572 char buf[256];
574 PSDRV_CopyColor(&physDev->inkColor, color);
575 switch(color->type) {
576 case PSCOLOR_RGB:
577 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
578 color->value.rgb.b);
579 return PSDRV_WriteSpool(dc, buf, strlen(buf));
581 case PSCOLOR_GRAY:
582 sprintf(buf, pssetgray, color->value.gray.i);
583 return PSDRV_WriteSpool(dc, buf, strlen(buf));
585 default:
586 ERR("Unkonwn colour type %d\n", color->type);
587 break;
590 return FALSE;
593 BOOL PSDRV_WriteSetPen(DC *dc)
595 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
596 char buf[256];
598 sprintf(buf, pssetlinewidth, physDev->pen.width);
599 PSDRV_WriteSpool(dc, buf, strlen(buf));
601 if(physDev->pen.dash) {
602 sprintf(buf, pssetdash, physDev->pen.dash, 0);
603 PSDRV_WriteSpool(dc, buf, strlen(buf));
606 return TRUE;
609 BOOL PSDRV_WriteReencodeFont(DC *dc)
611 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
612 char *buf, *newbuf;
614 buf = (char *)HeapAlloc( PSDRV_Heap, 0,
615 sizeof(psreencodefont) + 2 * strlen(physDev->font.afm->FontName)
616 + sizeof(encodingext));
618 if(!buf) {
619 WARN("HeapAlloc failed\n");
620 return FALSE;
623 newbuf = (char *)HeapAlloc( PSDRV_Heap, 0,
624 strlen(physDev->font.afm->FontName) + sizeof(encodingext));
626 if(!newbuf) {
627 WARN("HeapAlloc failed\n");
628 HeapFree(PSDRV_Heap, 0, buf);
629 return FALSE;
632 sprintf(newbuf, "%s%s", physDev->font.afm->FontName, encodingext);
633 sprintf(buf, psreencodefont, newbuf, physDev->font.afm->FontName);
635 PSDRV_WriteSpool(dc, buf, strlen(buf));
637 HeapFree(PSDRV_Heap, 0, newbuf);
638 HeapFree(PSDRV_Heap, 0, buf);
639 return TRUE;
642 #if 0
643 BOOL PSDRV_WriteShow(DC *dc, LPCWSTR str, INT count)
645 char *buf, *buf1;
646 INT buflen = count + 10, i, done;
648 buf = (char *)HeapAlloc( PSDRV_Heap, 0, buflen );
650 for(i = done = 0; i < count; i++) {
651 char c = PSDRV_UnicodeToANSI(str[i]);
652 if(!isprint(c)) {
653 if(done + 4 >= buflen)
654 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
655 sprintf(buf + done, "\\%03o", (int)(unsigned char)c);
656 done += 4;
657 } else if(c == '\\' || c == '(' || c == ')' ) {
658 if(done + 2 >= buflen)
659 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
660 buf[done++] = '\\';
661 buf[done++] = c;
662 } else {
663 if(done + 1 >= buflen)
664 buf = HeapReAlloc( PSDRV_Heap, 0, buf, buflen += 10 );
665 buf[done++] = c;
668 buf[done] = '\0';
670 buf1 = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psshow) + done);
672 sprintf(buf1, psshow, buf);
674 PSDRV_WriteSpool(dc, buf1, strlen(buf1));
675 HeapFree(PSDRV_Heap, 0, buf);
676 HeapFree(PSDRV_Heap, 0, buf1);
678 return TRUE;
680 #endif
682 BOOL PSDRV_WriteShow(DC *dc, LPCWSTR str, INT count)
684 char buf[128];
685 int i;
687 for (i = 0; i < count; ++i)
689 LPCSTR name;
690 int l;
692 name = PSDRV_UVMetrics(str[i],
693 ((PSDRV_PDEVICE *)dc->physDev)->font.afm)->N->sz;
694 l = snprintf(buf, sizeof(buf), psglyphshow, name);
696 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1)
698 WARN("Unusable glyph name '%s' - ignoring\n", name);
699 continue;
702 PSDRV_WriteSpool(dc, buf, l);
705 return TRUE;
708 BOOL PSDRV_WriteFill(DC *dc)
710 return PSDRV_WriteSpool(dc, psfill, sizeof(psfill)-1);
713 BOOL PSDRV_WriteEOFill(DC *dc)
715 return PSDRV_WriteSpool(dc, pseofill, sizeof(pseofill)-1);
718 BOOL PSDRV_WriteGSave(DC *dc)
720 return PSDRV_WriteSpool(dc, psgsave, sizeof(psgsave)-1);
723 BOOL PSDRV_WriteGRestore(DC *dc)
725 return PSDRV_WriteSpool(dc, psgrestore, sizeof(psgrestore)-1);
728 BOOL PSDRV_WriteNewPath(DC *dc)
730 return PSDRV_WriteSpool(dc, psnewpath, sizeof(psnewpath)-1);
733 BOOL PSDRV_WriteClosePath(DC *dc)
735 return PSDRV_WriteSpool(dc, psclosepath, sizeof(psclosepath)-1);
738 BOOL PSDRV_WriteClip(DC *dc)
740 return PSDRV_WriteSpool(dc, psclip, sizeof(psclip)-1);
743 BOOL PSDRV_WriteEOClip(DC *dc)
745 return PSDRV_WriteSpool(dc, pseoclip, sizeof(pseoclip)-1);
748 BOOL PSDRV_WriteInitClip(DC *dc)
750 return PSDRV_WriteSpool(dc, psinitclip, sizeof(psinitclip)-1);
753 BOOL PSDRV_WriteHatch(DC *dc)
755 return PSDRV_WriteSpool(dc, pshatch, sizeof(pshatch)-1);
758 BOOL PSDRV_WriteRotate(DC *dc, float ang)
760 char buf[256];
762 sprintf(buf, psrotate, ang);
763 return PSDRV_WriteSpool(dc, buf, strlen(buf));
766 BOOL PSDRV_WriteIndexColorSpaceBegin(DC *dc, int size)
768 char buf[256];
769 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
770 return PSDRV_WriteSpool(dc, buf, strlen(buf));
773 BOOL PSDRV_WriteIndexColorSpaceEnd(DC *dc)
775 char buf[] = ">\n] setcolorspace\n";
776 return PSDRV_WriteSpool(dc, buf, sizeof(buf) - 1);
779 BOOL PSDRV_WriteRGB(DC *dc, COLORREF *map, int number)
781 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
782 int i;
784 ptr = buf;
785 for(i = 0; i < number; i++) {
786 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
787 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
788 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
789 ptr += 7;
791 PSDRV_WriteSpool(dc, buf, number * 7);
792 HeapFree(PSDRV_Heap, 0, buf);
793 return TRUE;
797 BOOL PSDRV_WriteImageDict(DC *dc, WORD depth, INT xDst, INT yDst,
798 INT widthDst, INT heightDst, INT widthSrc,
799 INT heightSrc, char *bits)
801 char start[] = "%d %d translate\n%d %d scale\n<<\n"
802 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
803 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
805 char decode1[] = " /Decode [0 %d]\n";
806 char decode3[] = " /Decode [0 1 0 1 0 1]\n";
808 char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
809 char endbits[] = " /DataSource <%s>\n>> image\n";
811 char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
813 sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
814 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
816 PSDRV_WriteSpool(dc, buf, strlen(buf));
818 switch(depth) {
819 case 8:
820 sprintf(buf, decode1, 255);
821 break;
823 case 4:
824 sprintf(buf, decode1, 15);
825 break;
827 case 1:
828 sprintf(buf, decode1, 1);
829 break;
831 default:
832 strcpy(buf, decode3);
833 break;
836 PSDRV_WriteSpool(dc, buf, strlen(buf));
838 if(!bits)
839 PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
840 else {
841 sprintf(buf, endbits, bits);
842 PSDRV_WriteSpool(dc, buf, strlen(buf));
845 HeapFree(PSDRV_Heap, 0, buf);
846 return TRUE;
850 BOOL PSDRV_WriteBytes(DC *dc, const BYTE *bytes, int number)
852 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
853 char *ptr;
854 int i;
856 ptr = buf;
858 for(i = 0; i < number; i++) {
859 sprintf(ptr, "%02x%c", bytes[i],
860 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
861 ptr += 3;
863 PSDRV_WriteSpool(dc, buf, number * 3);
865 HeapFree(PSDRV_Heap, 0, buf);
866 return TRUE;
869 BOOL PSDRV_WriteDIBits16(DC *dc, const WORD *words, int number)
871 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
872 char *ptr;
873 int i;
875 ptr = buf;
877 for(i = 0; i < number; i++) {
878 int r, g, b;
880 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
882 r = words[i] >> 10 & 0x1f;
883 r = r << 3 | r >> 2;
884 g = words[i] >> 5 & 0x1f;
885 g = g << 3 | g >> 2;
886 b = words[i] & 0x1f;
887 b = b << 3 | b >> 2;
888 sprintf(ptr, "%02x%02x%02x%c", r, g, b,
889 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
890 ptr += 7;
892 PSDRV_WriteSpool(dc, buf, number * 7);
894 HeapFree(PSDRV_Heap, 0, buf);
895 return TRUE;
898 BOOL PSDRV_WriteDIBits24(DC *dc, const BYTE *bits, int number)
900 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
901 char *ptr;
902 int i;
904 ptr = buf;
906 for(i = 0; i < number; i++) {
907 sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
908 bits[i * 3],
909 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
910 ptr += 7;
912 PSDRV_WriteSpool(dc, buf, number * 7);
914 HeapFree(PSDRV_Heap, 0, buf);
915 return TRUE;
918 BOOL PSDRV_WriteDIBits32(DC *dc, const BYTE *bits, int number)
920 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
921 char *ptr;
922 int i;
924 ptr = buf;
926 for(i = 0; i < number; i++) {
927 sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
928 bits[i * 4],
929 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
930 ptr += 7;
932 PSDRV_WriteSpool(dc, buf, number * 7);
934 HeapFree(PSDRV_Heap, 0, buf);
935 return TRUE;
938 BOOL PSDRV_WriteArrayGet(DC *dc, CHAR *pszArrayName, INT nIndex)
940 char buf[100];
942 sprintf(buf, psarrayget, pszArrayName, nIndex);
943 return PSDRV_WriteSpool(dc, buf, strlen(buf));
946 BOOL PSDRV_WriteArrayPut(DC *dc, CHAR *pszArrayName, INT nIndex, LONG lObject)
948 char buf[100];
950 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
951 return PSDRV_WriteSpool(dc, buf, strlen(buf));
954 BOOL PSDRV_WriteArrayDef(DC *dc, CHAR *pszArrayName, INT nSize)
956 char buf[100];
958 sprintf(buf, psarraydef, pszArrayName, nSize);
959 return PSDRV_WriteSpool(dc, buf, strlen(buf));
962 BOOL PSDRV_WriteRectClip(DC *dc, INT x, INT y, INT w, INT h)
964 char buf[100];
966 sprintf(buf, psrectclip, x, y, w, h);
967 return PSDRV_WriteSpool(dc, buf, strlen(buf));
970 BOOL PSDRV_WriteRectClip2(DC *dc, CHAR *pszArrayName)
972 char buf[100];
974 sprintf(buf, psrectclip2, pszArrayName);
975 return PSDRV_WriteSpool(dc, buf, strlen(buf));
978 BOOL PSDRV_WritePatternDict(DC *dc, BITMAP *bm, BYTE *bits)
980 char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
981 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n";
983 char end[] = " end\n }\n>>\n matrix makepattern setpattern\n";
984 char *buf, *ptr;
985 INT w, h, x, y;
986 COLORREF map[2];
988 w = bm->bmWidth & ~0x7;
989 h = bm->bmHeight & ~0x7;
991 buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
992 sprintf(buf, start, w, h, w, h);
993 PSDRV_WriteSpool(dc, buf, strlen(buf));
994 PSDRV_WriteIndexColorSpaceBegin(dc, 1);
995 map[0] = dc->textColor;
996 map[1] = dc->backgroundColor;
997 PSDRV_WriteRGB(dc, map, 2);
998 PSDRV_WriteIndexColorSpaceEnd(dc);
999 ptr = buf;
1000 for(y = h-1; y >= 0; y--) {
1001 for(x = 0; x < w/8; x++) {
1002 sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
1003 ptr += 2;
1006 PSDRV_WriteImageDict(dc, 1, 0, 0, 8, 8, 8, 8, buf);
1007 PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
1008 HeapFree(PSDRV_Heap, 0, buf);
1009 return TRUE;