2 * PostScript output functions
4 * Copyright 1998 Huw D M Davies
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(psdrv);
17 static char psheader[] = /* title llx lly urx ury orientation */
19 "%%%%Creator: Wine PostScript Driver\n"
21 "%%%%BoundingBox: %d %d %d %d\n"
22 "%%%%Pages: (atend)\n"
23 "%%%%Orientation: %s\n"
26 static char psbeginprolog[] =
29 static char psendprolog[] =
32 static char psprolog[] =
33 "/tmpmtrx matrix def\n"
36 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
37 " l cvi gap idiv gap mul\n"
39 " r cvi gap idiv gap mul\n"
40 " {t moveto 0 b t sub rlineto}\n"
44 static char psbeginsetup[] =
47 static char psendsetup[] =
50 static char psbeginfeature[] = /* feature, value */
52 "%%%%BeginFeature: %s %s\n";
54 static char psendfeature[] =
56 "} stopped cleartomark\n";
58 static char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
60 "%%%%BeginPageSetup\n"
62 "72 %d div 72 %d div scale\n"
68 static char psendpage[] =
72 static char psfooter[] = /* pages */
77 static char psmoveto[] = /* x, y */
80 static char pslineto[] = /* x, y */
83 static char psstroke[] =
86 static char psrectangle[] = /* x, y, width, height, -width */
93 static char psrrectangle[] = /* x, y, width, height, -width */
100 static const char psglyphshow[] = /* glyph name */
103 static char pssetfont[] = /* fontname, xscale, yscale, ascent, escapement */
106 "%d 10 div matrix rotate\n"
107 "matrix concatmatrix\n"
108 "makefont setfont\n";
110 static char pssetlinewidth[] = /* width */
113 static char pssetdash[] = /* dash, offset */
116 static char pssetgray[] = /* gray */
119 static char pssetrgbcolor[] = /* r, g, b */
120 "%.2f %.2f %.2f setrgbcolor\n";
122 static char psarc[] = /* x, y, w, h, ang1, ang2 */
123 "tmpmtrx currentmatrix pop\n"
126 "0 0 0.5 %.1f %.1f arc\n"
127 "tmpmtrx setmatrix\n";
129 static char psgsave[] =
132 static char psgrestore[] =
135 static char psfill[] =
138 static char pseofill[] =
141 static char psnewpath[] =
144 static char psclosepath[] =
147 static char psclip[] =
150 static char psinitclip[] =
153 static char pseoclip[] =
156 static char psrectclip[] =
157 "%d %d %d %d rectclip\n";
159 static char psrectclip2[] =
162 static char pshatch[] =
165 static char psrotate[] = /* ang */
168 static char psarrayget[] =
171 static char psarrayput[] =
174 static char psarraydef[] =
175 "/%s %d array def\n";
178 int PSDRV_WriteSpool(DC *dc, LPSTR lpData, WORD cch)
180 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
182 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
183 if( !PSDRV_StartPage(dc) )
186 return WriteSpool16( physDev->job.hJob, lpData, cch );
190 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
194 char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
195 strlen(feature) + strlen(value));
198 sprintf(buf, psbeginfeature, feature, value);
199 WriteSpool16( hJob, buf, strlen(buf) );
201 WriteSpool16( hJob, invocation, strlen(invocation) );
203 WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
205 HeapFree( PSDRV_Heap, 0, buf );
211 INT PSDRV_WriteHeader( DC *dc, LPCSTR title )
213 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
217 int llx, lly, urx, ury;
219 TRACE("'%s'\n", title);
221 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
222 strlen(title) + 30 );
224 WARN("HeapAlloc failed\n");
228 /* BBox co-ords are in default user co-ord system so urx < ury even in
230 llx = physDev->PageSize.left * 72.0 / physDev->logPixelsX;
231 lly = physDev->PageSize.bottom * 72.0 / physDev->logPixelsY;
232 urx = physDev->PageSize.right * 72.0 / physDev->logPixelsX;
233 ury = physDev->PageSize.top * 72.0 / physDev->logPixelsY;
235 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
236 orient = "Landscape";
241 /* FIXME should do something better with BBox */
243 sprintf(buf, psheader, title, llx, lly, urx, ury, orient);
245 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
247 WARN("WriteSpool error\n");
248 HeapFree( PSDRV_Heap, 0, buf );
251 HeapFree( PSDRV_Heap, 0, buf );
253 WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
254 WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
255 WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
257 WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
259 for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
260 if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
261 if(slot->InvocationString) {
262 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
263 slot->InvocationString);
269 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
270 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
271 if(page->InvocationString) {
272 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
273 page->InvocationString);
279 WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
286 INT PSDRV_WriteFooter( DC *dc )
288 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
291 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
293 WARN("HeapAlloc failed\n");
297 sprintf(buf, psfooter, physDev->job.PageNo);
299 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
301 WARN("WriteSpool error\n");
302 HeapFree( PSDRV_Heap, 0, buf );
305 HeapFree( PSDRV_Heap, 0, buf );
311 INT PSDRV_WriteEndPage( DC *dc )
313 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
315 if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) !=
316 sizeof(psendpage)-1 ) {
317 WARN("WriteSpool error\n");
326 INT PSDRV_WriteNewPage( DC *dc )
328 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
331 signed int xtrans, ytrans, rotation;
333 sprintf(name, "%d", physDev->job.PageNo);
335 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
337 WARN("HeapAlloc failed\n");
341 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
342 if(physDev->pi->ppd->LandscapeOrientation == -90) {
343 xtrans = physDev->PageSize.right;
344 ytrans = physDev->PageSize.top;
347 xtrans = physDev->PageSize.left;
348 ytrans = physDev->PageSize.bottom;
352 xtrans = physDev->PageSize.left;
353 ytrans = physDev->PageSize.top;
357 sprintf(buf, psnewpage, name, physDev->job.PageNo,
358 physDev->logPixelsX, physDev->logPixelsY,
359 xtrans, ytrans, rotation);
361 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
363 WARN("WriteSpool error\n");
364 HeapFree( PSDRV_Heap, 0, buf );
367 HeapFree( PSDRV_Heap, 0, buf );
372 BOOL PSDRV_WriteMoveTo(DC *dc, INT x, INT y)
376 sprintf(buf, psmoveto, x, y);
377 return PSDRV_WriteSpool(dc, buf, strlen(buf));
380 BOOL PSDRV_WriteLineTo(DC *dc, INT x, INT y)
384 sprintf(buf, pslineto, x, y);
385 return PSDRV_WriteSpool(dc, buf, strlen(buf));
389 BOOL PSDRV_WriteStroke(DC *dc)
391 return PSDRV_WriteSpool(dc, psstroke, sizeof(psstroke)-1);
396 BOOL PSDRV_WriteRectangle(DC *dc, INT x, INT y, INT width,
401 sprintf(buf, psrectangle, x, y, width, height, -width);
402 return PSDRV_WriteSpool(dc, buf, strlen(buf));
405 BOOL PSDRV_WriteRRectangle(DC *dc, INT x, INT y, INT width,
410 sprintf(buf, psrrectangle, x, y, width, height, -width);
411 return PSDRV_WriteSpool(dc, buf, strlen(buf));
414 BOOL PSDRV_WriteArc(DC *dc, INT x, INT y, INT w, INT h, double ang1,
419 /* Make angles -ve and swap order because we're working with an upside
421 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
422 return PSDRV_WriteSpool(dc, buf, strlen(buf));
425 BOOL PSDRV_WriteSetFont(DC *dc)
427 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
430 buf = (char *)HeapAlloc( PSDRV_Heap, 0,
431 sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
434 WARN("HeapAlloc failed\n");
438 sprintf(buf, pssetfont, physDev->font.afm->FontName,
439 physDev->font.size, -physDev->font.size,
440 -physDev->font.escapement);
442 PSDRV_WriteSpool(dc, buf, strlen(buf));
443 HeapFree(PSDRV_Heap, 0, buf);
447 BOOL PSDRV_WriteSetColor(DC *dc, PSCOLOR *color)
449 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
452 PSDRV_CopyColor(&physDev->inkColor, color);
453 switch(color->type) {
455 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
457 return PSDRV_WriteSpool(dc, buf, strlen(buf));
460 sprintf(buf, pssetgray, color->value.gray.i);
461 return PSDRV_WriteSpool(dc, buf, strlen(buf));
464 ERR("Unkonwn colour type %d\n", color->type);
471 BOOL PSDRV_WriteSetPen(DC *dc)
473 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
476 sprintf(buf, pssetlinewidth, physDev->pen.width);
477 PSDRV_WriteSpool(dc, buf, strlen(buf));
479 if(physDev->pen.dash) {
480 sprintf(buf, pssetdash, physDev->pen.dash, 0);
481 PSDRV_WriteSpool(dc, buf, strlen(buf));
487 BOOL PSDRV_WriteGlyphShow(DC *dc, LPCWSTR str, INT count)
492 for (i = 0; i < count; ++i)
497 name = PSDRV_UVMetrics(str[i],
498 ((PSDRV_PDEVICE *)dc->physDev)->font.afm)->N->sz;
499 l = snprintf(buf, sizeof(buf), psglyphshow, name);
501 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1)
503 WARN("Unusable glyph name '%s' - ignoring\n", name);
507 PSDRV_WriteSpool(dc, buf, l);
513 BOOL PSDRV_WriteFill(DC *dc)
515 return PSDRV_WriteSpool(dc, psfill, sizeof(psfill)-1);
518 BOOL PSDRV_WriteEOFill(DC *dc)
520 return PSDRV_WriteSpool(dc, pseofill, sizeof(pseofill)-1);
523 BOOL PSDRV_WriteGSave(DC *dc)
525 return PSDRV_WriteSpool(dc, psgsave, sizeof(psgsave)-1);
528 BOOL PSDRV_WriteGRestore(DC *dc)
530 return PSDRV_WriteSpool(dc, psgrestore, sizeof(psgrestore)-1);
533 BOOL PSDRV_WriteNewPath(DC *dc)
535 return PSDRV_WriteSpool(dc, psnewpath, sizeof(psnewpath)-1);
538 BOOL PSDRV_WriteClosePath(DC *dc)
540 return PSDRV_WriteSpool(dc, psclosepath, sizeof(psclosepath)-1);
543 BOOL PSDRV_WriteClip(DC *dc)
545 return PSDRV_WriteSpool(dc, psclip, sizeof(psclip)-1);
548 BOOL PSDRV_WriteEOClip(DC *dc)
550 return PSDRV_WriteSpool(dc, pseoclip, sizeof(pseoclip)-1);
553 BOOL PSDRV_WriteInitClip(DC *dc)
555 return PSDRV_WriteSpool(dc, psinitclip, sizeof(psinitclip)-1);
558 BOOL PSDRV_WriteHatch(DC *dc)
560 return PSDRV_WriteSpool(dc, pshatch, sizeof(pshatch)-1);
563 BOOL PSDRV_WriteRotate(DC *dc, float ang)
567 sprintf(buf, psrotate, ang);
568 return PSDRV_WriteSpool(dc, buf, strlen(buf));
571 BOOL PSDRV_WriteIndexColorSpaceBegin(DC *dc, int size)
574 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
575 return PSDRV_WriteSpool(dc, buf, strlen(buf));
578 BOOL PSDRV_WriteIndexColorSpaceEnd(DC *dc)
580 char buf[] = ">\n] setcolorspace\n";
581 return PSDRV_WriteSpool(dc, buf, sizeof(buf) - 1);
584 BOOL PSDRV_WriteRGB(DC *dc, COLORREF *map, int number)
586 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
590 for(i = 0; i < number; i++) {
591 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
592 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
593 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
596 PSDRV_WriteSpool(dc, buf, number * 7);
597 HeapFree(PSDRV_Heap, 0, buf);
602 BOOL PSDRV_WriteImageDict(DC *dc, WORD depth, INT xDst, INT yDst,
603 INT widthDst, INT heightDst, INT widthSrc,
604 INT heightSrc, char *bits)
606 char start[] = "%d %d translate\n%d %d scale\n<<\n"
607 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
608 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
610 char decode1[] = " /Decode [0 %d]\n";
611 char decode3[] = " /Decode [0 1 0 1 0 1]\n";
613 char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
614 char endbits[] = " /DataSource <%s>\n>> image\n";
616 char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
618 sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
619 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
621 PSDRV_WriteSpool(dc, buf, strlen(buf));
625 sprintf(buf, decode1, 255);
629 sprintf(buf, decode1, 15);
633 sprintf(buf, decode1, 1);
637 strcpy(buf, decode3);
641 PSDRV_WriteSpool(dc, buf, strlen(buf));
644 PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
646 sprintf(buf, endbits, bits);
647 PSDRV_WriteSpool(dc, buf, strlen(buf));
650 HeapFree(PSDRV_Heap, 0, buf);
655 BOOL PSDRV_WriteBytes(DC *dc, const BYTE *bytes, int number)
657 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
663 for(i = 0; i < number; i++) {
664 sprintf(ptr, "%02x%c", bytes[i],
665 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
668 PSDRV_WriteSpool(dc, buf, number * 3);
670 HeapFree(PSDRV_Heap, 0, buf);
674 BOOL PSDRV_WriteDIBits16(DC *dc, const WORD *words, int number)
676 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
682 for(i = 0; i < number; i++) {
685 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
687 r = words[i] >> 10 & 0x1f;
689 g = words[i] >> 5 & 0x1f;
693 sprintf(ptr, "%02x%02x%02x%c", r, g, b,
694 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
697 PSDRV_WriteSpool(dc, buf, number * 7);
699 HeapFree(PSDRV_Heap, 0, buf);
703 BOOL PSDRV_WriteDIBits24(DC *dc, const BYTE *bits, int number)
705 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
711 for(i = 0; i < number; i++) {
712 sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
714 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
717 PSDRV_WriteSpool(dc, buf, number * 7);
719 HeapFree(PSDRV_Heap, 0, buf);
723 BOOL PSDRV_WriteDIBits32(DC *dc, const BYTE *bits, int number)
725 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
731 for(i = 0; i < number; i++) {
732 sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
734 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
737 PSDRV_WriteSpool(dc, buf, number * 7);
739 HeapFree(PSDRV_Heap, 0, buf);
743 BOOL PSDRV_WriteArrayGet(DC *dc, CHAR *pszArrayName, INT nIndex)
747 sprintf(buf, psarrayget, pszArrayName, nIndex);
748 return PSDRV_WriteSpool(dc, buf, strlen(buf));
751 BOOL PSDRV_WriteArrayPut(DC *dc, CHAR *pszArrayName, INT nIndex, LONG lObject)
755 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
756 return PSDRV_WriteSpool(dc, buf, strlen(buf));
759 BOOL PSDRV_WriteArrayDef(DC *dc, CHAR *pszArrayName, INT nSize)
763 sprintf(buf, psarraydef, pszArrayName, nSize);
764 return PSDRV_WriteSpool(dc, buf, strlen(buf));
767 BOOL PSDRV_WriteRectClip(DC *dc, INT x, INT y, INT w, INT h)
771 sprintf(buf, psrectclip, x, y, w, h);
772 return PSDRV_WriteSpool(dc, buf, strlen(buf));
775 BOOL PSDRV_WriteRectClip2(DC *dc, CHAR *pszArrayName)
779 sprintf(buf, psrectclip2, pszArrayName);
780 return PSDRV_WriteSpool(dc, buf, strlen(buf));
783 BOOL PSDRV_WritePatternDict(DC *dc, BITMAP *bm, BYTE *bits)
785 char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
786 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n";
788 char end[] = " end\n }\n>>\n matrix makepattern setpattern\n";
793 w = bm->bmWidth & ~0x7;
794 h = bm->bmHeight & ~0x7;
796 buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
797 sprintf(buf, start, w, h, w, h);
798 PSDRV_WriteSpool(dc, buf, strlen(buf));
799 PSDRV_WriteIndexColorSpaceBegin(dc, 1);
800 map[0] = dc->textColor;
801 map[1] = dc->backgroundColor;
802 PSDRV_WriteRGB(dc, map, 2);
803 PSDRV_WriteIndexColorSpaceEnd(dc);
805 for(y = h-1; y >= 0; y--) {
806 for(x = 0; x < w/8; x++) {
807 sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
811 PSDRV_WriteImageDict(dc, 1, 0, 0, 8, 8, 8, 8, buf);
812 PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
813 HeapFree(PSDRV_Heap, 0, buf);