2 * PostScript output functions
4 * Copyright 1998 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
30 static char psheader[] = /* title llx lly urx ury orientation */
32 "%%%%Creator: Wine PostScript Driver\n"
34 "%%%%BoundingBox: %d %d %d %d\n"
35 "%%%%Pages: (atend)\n"
36 "%%%%Orientation: %s\n"
39 static char psbeginprolog[] =
42 static char psendprolog[] =
45 static char psprolog[] =
46 "/tmpmtrx matrix def\n"
49 " /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
50 " l cvi gap idiv gap mul\n"
52 " r cvi gap idiv gap mul\n"
53 " {t moveto 0 b t sub rlineto}\n"
57 static char psbeginsetup[] =
60 static char psendsetup[] =
63 static char psbeginfeature[] = /* feature, value */
65 "%%%%BeginFeature: %s %s\n";
67 static char psendfeature[] =
69 "} stopped cleartomark\n";
71 static char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
73 "%%%%BeginPageSetup\n"
75 "72 %d div 72 %d div scale\n"
81 static char psendpage[] =
85 static char psfooter[] = /* pages */
90 static char psmoveto[] = /* x, y */
93 static char pslineto[] = /* x, y */
96 static char psstroke[] =
99 static char psrectangle[] = /* x, y, width, height, -width */
106 static char psrrectangle[] = /* x, y, width, height, -width */
113 static const char psglyphshow[] = /* glyph name */
116 static char pssetfont[] = /* fontname, xscale, yscale, ascent, escapement */
119 "%d 10 div matrix rotate\n"
120 "matrix concatmatrix\n"
121 "makefont setfont\n";
123 static char pssetlinewidth[] = /* width */
126 static char pssetdash[] = /* dash, offset */
129 static char pssetgray[] = /* gray */
132 static char pssetrgbcolor[] = /* r, g, b */
133 "%.2f %.2f %.2f setrgbcolor\n";
135 static char psarc[] = /* x, y, w, h, ang1, ang2 */
136 "tmpmtrx currentmatrix pop\n"
139 "0 0 0.5 %.1f %.1f arc\n"
140 "tmpmtrx setmatrix\n";
142 static char psgsave[] =
145 static char psgrestore[] =
148 static char psfill[] =
151 static char pseofill[] =
154 static char psnewpath[] =
157 static char psclosepath[] =
160 static char psclip[] =
163 static char psinitclip[] =
166 static char pseoclip[] =
169 static char psrectclip[] =
170 "%d %d %d %d rectclip\n";
172 static char psrectclip2[] =
175 static char pshatch[] =
178 static char psrotate[] = /* ang */
181 static char psarrayget[] =
184 static char psarrayput[] =
187 static char psarraydef[] =
188 "/%s %d array def\n";
191 int PSDRV_WriteSpool(PSDRV_PDEVICE *physDev, LPSTR lpData, WORD cch)
193 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
194 if( !PSDRV_StartPage(physDev) )
197 return WriteSpool16( physDev->job.hJob, lpData, cch );
201 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
205 char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
206 strlen(feature) + strlen(value));
209 sprintf(buf, psbeginfeature, feature, value);
210 WriteSpool16( hJob, buf, strlen(buf) );
212 WriteSpool16( hJob, invocation, strlen(invocation) );
214 WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
216 HeapFree( PSDRV_Heap, 0, buf );
222 INT PSDRV_WriteHeader( PSDRV_PDEVICE *physDev, LPCSTR title )
227 int llx, lly, urx, ury;
229 TRACE("'%s'\n", title);
231 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
232 strlen(title) + 30 );
234 WARN("HeapAlloc failed\n");
238 /* BBox co-ords are in default user co-ord system so urx < ury even in
240 llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
241 lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
242 urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
243 ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
245 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
246 orient = "Landscape";
251 /* FIXME should do something better with BBox */
253 sprintf(buf, psheader, title, llx, lly, urx, ury, orient);
255 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
257 WARN("WriteSpool error\n");
258 HeapFree( PSDRV_Heap, 0, buf );
261 HeapFree( PSDRV_Heap, 0, buf );
263 WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
264 WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
265 WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
267 WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
269 for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
270 if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
271 if(slot->InvocationString) {
272 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
273 slot->InvocationString);
279 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
280 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
281 if(page->InvocationString) {
282 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
283 page->InvocationString);
289 WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
296 INT PSDRV_WriteFooter( PSDRV_PDEVICE *physDev )
300 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
302 WARN("HeapAlloc failed\n");
306 sprintf(buf, psfooter, physDev->job.PageNo);
308 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
310 WARN("WriteSpool error\n");
311 HeapFree( PSDRV_Heap, 0, buf );
314 HeapFree( PSDRV_Heap, 0, buf );
320 INT PSDRV_WriteEndPage( PSDRV_PDEVICE *physDev )
322 if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) !=
323 sizeof(psendpage)-1 ) {
324 WARN("WriteSpool error\n");
333 INT PSDRV_WriteNewPage( PSDRV_PDEVICE *physDev )
337 signed int xtrans, ytrans, rotation;
339 sprintf(name, "%d", physDev->job.PageNo);
341 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
343 WARN("HeapAlloc failed\n");
347 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
348 if(physDev->pi->ppd->LandscapeOrientation == -90) {
349 xtrans = physDev->ImageableArea.right;
350 ytrans = physDev->ImageableArea.top;
353 xtrans = physDev->ImageableArea.left;
354 ytrans = physDev->ImageableArea.bottom;
358 xtrans = physDev->ImageableArea.left;
359 ytrans = physDev->ImageableArea.top;
363 sprintf(buf, psnewpage, name, physDev->job.PageNo,
364 physDev->logPixelsX, physDev->logPixelsY,
365 xtrans, ytrans, rotation);
367 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
369 WARN("WriteSpool error\n");
370 HeapFree( PSDRV_Heap, 0, buf );
373 HeapFree( PSDRV_Heap, 0, buf );
378 BOOL PSDRV_WriteMoveTo(PSDRV_PDEVICE *physDev, INT x, INT y)
382 sprintf(buf, psmoveto, x, y);
383 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
386 BOOL PSDRV_WriteLineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
390 sprintf(buf, pslineto, x, y);
391 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
395 BOOL PSDRV_WriteStroke(PSDRV_PDEVICE *physDev)
397 return PSDRV_WriteSpool(physDev, psstroke, sizeof(psstroke)-1);
402 BOOL PSDRV_WriteRectangle(PSDRV_PDEVICE *physDev, INT x, INT y, INT width,
407 sprintf(buf, psrectangle, x, y, width, height, -width);
408 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
411 BOOL PSDRV_WriteRRectangle(PSDRV_PDEVICE *physDev, INT x, INT y, INT width,
416 sprintf(buf, psrrectangle, x, y, width, height, -width);
417 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
420 BOOL PSDRV_WriteArc(PSDRV_PDEVICE *physDev, INT x, INT y, INT w, INT h, double ang1,
425 /* Make angles -ve and swap order because we're working with an upside
427 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
428 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
431 BOOL PSDRV_WriteSetFont(PSDRV_PDEVICE *physDev)
435 buf = (char *)HeapAlloc( PSDRV_Heap, 0,
436 sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
439 WARN("HeapAlloc failed\n");
443 sprintf(buf, pssetfont, physDev->font.afm->FontName,
444 physDev->font.size, -physDev->font.size,
445 -physDev->font.escapement);
447 PSDRV_WriteSpool(physDev, buf, strlen(buf));
448 HeapFree(PSDRV_Heap, 0, buf);
452 BOOL PSDRV_WriteSetColor(PSDRV_PDEVICE *physDev, PSCOLOR *color)
456 PSDRV_CopyColor(&physDev->inkColor, color);
457 switch(color->type) {
459 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
461 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
464 sprintf(buf, pssetgray, color->value.gray.i);
465 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
468 ERR("Unkonwn colour type %d\n", color->type);
475 BOOL PSDRV_WriteSetPen(PSDRV_PDEVICE *physDev)
479 sprintf(buf, pssetlinewidth, physDev->pen.width);
480 PSDRV_WriteSpool(physDev, buf, strlen(buf));
482 if(physDev->pen.dash) {
483 sprintf(buf, pssetdash, physDev->pen.dash, 0);
484 PSDRV_WriteSpool(physDev, buf, strlen(buf));
490 BOOL PSDRV_WriteGlyphShow(PSDRV_PDEVICE *physDev, LPCWSTR str, INT count)
495 for (i = 0; i < count; ++i)
500 name = PSDRV_UVMetrics(str[i], physDev->font.afm)->N->sz;
501 l = snprintf(buf, sizeof(buf), psglyphshow, name);
503 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1)
505 WARN("Unusable glyph name '%s' - ignoring\n", name);
509 PSDRV_WriteSpool(physDev, buf, l);
515 BOOL PSDRV_WriteFill(PSDRV_PDEVICE *physDev)
517 return PSDRV_WriteSpool(physDev, psfill, sizeof(psfill)-1);
520 BOOL PSDRV_WriteEOFill(PSDRV_PDEVICE *physDev)
522 return PSDRV_WriteSpool(physDev, pseofill, sizeof(pseofill)-1);
525 BOOL PSDRV_WriteGSave(PSDRV_PDEVICE *physDev)
527 return PSDRV_WriteSpool(physDev, psgsave, sizeof(psgsave)-1);
530 BOOL PSDRV_WriteGRestore(PSDRV_PDEVICE *physDev)
532 return PSDRV_WriteSpool(physDev, psgrestore, sizeof(psgrestore)-1);
535 BOOL PSDRV_WriteNewPath(PSDRV_PDEVICE *physDev)
537 return PSDRV_WriteSpool(physDev, psnewpath, sizeof(psnewpath)-1);
540 BOOL PSDRV_WriteClosePath(PSDRV_PDEVICE *physDev)
542 return PSDRV_WriteSpool(physDev, psclosepath, sizeof(psclosepath)-1);
545 BOOL PSDRV_WriteClip(PSDRV_PDEVICE *physDev)
547 return PSDRV_WriteSpool(physDev, psclip, sizeof(psclip)-1);
550 BOOL PSDRV_WriteEOClip(PSDRV_PDEVICE *physDev)
552 return PSDRV_WriteSpool(physDev, pseoclip, sizeof(pseoclip)-1);
555 BOOL PSDRV_WriteInitClip(PSDRV_PDEVICE *physDev)
557 return PSDRV_WriteSpool(physDev, psinitclip, sizeof(psinitclip)-1);
560 BOOL PSDRV_WriteHatch(PSDRV_PDEVICE *physDev)
562 return PSDRV_WriteSpool(physDev, pshatch, sizeof(pshatch)-1);
565 BOOL PSDRV_WriteRotate(PSDRV_PDEVICE *physDev, float ang)
569 sprintf(buf, psrotate, ang);
570 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
573 BOOL PSDRV_WriteIndexColorSpaceBegin(PSDRV_PDEVICE *physDev, int size)
576 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
577 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
580 BOOL PSDRV_WriteIndexColorSpaceEnd(PSDRV_PDEVICE *physDev)
582 char buf[] = ">\n] setcolorspace\n";
583 return PSDRV_WriteSpool(physDev, buf, sizeof(buf) - 1);
586 BOOL PSDRV_WriteRGB(PSDRV_PDEVICE *physDev, COLORREF *map, int number)
588 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
592 for(i = 0; i < number; i++) {
593 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
594 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
595 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
598 PSDRV_WriteSpool(physDev, buf, number * 7);
599 HeapFree(PSDRV_Heap, 0, buf);
604 BOOL PSDRV_WriteImageDict(PSDRV_PDEVICE *physDev, WORD depth, INT xDst, INT yDst,
605 INT widthDst, INT heightDst, INT widthSrc,
606 INT heightSrc, char *bits)
608 char start[] = "%d %d translate\n%d %d scale\n<<\n"
609 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
610 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
612 char decode1[] = " /Decode [0 %d]\n";
613 char decode3[] = " /Decode [0 1 0 1 0 1]\n";
615 char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
616 char endbits[] = " /DataSource <%s>\n>> image\n";
618 char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
620 sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
621 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
623 PSDRV_WriteSpool(physDev, buf, strlen(buf));
627 sprintf(buf, decode1, 255);
631 sprintf(buf, decode1, 15);
635 sprintf(buf, decode1, 1);
639 strcpy(buf, decode3);
643 PSDRV_WriteSpool(physDev, buf, strlen(buf));
646 PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
648 sprintf(buf, endbits, bits);
649 PSDRV_WriteSpool(physDev, buf, strlen(buf));
652 HeapFree(PSDRV_Heap, 0, buf);
657 BOOL PSDRV_WriteBytes(PSDRV_PDEVICE *physDev, const BYTE *bytes, int number)
659 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
665 for(i = 0; i < number; i++) {
666 sprintf(ptr, "%02x%c", bytes[i],
667 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
670 PSDRV_WriteSpool(physDev, buf, number * 3);
672 HeapFree(PSDRV_Heap, 0, buf);
676 BOOL PSDRV_WriteDIBits16(PSDRV_PDEVICE *physDev, const WORD *words, int number)
678 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
684 for(i = 0; i < number; i++) {
687 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
689 r = words[i] >> 10 & 0x1f;
691 g = words[i] >> 5 & 0x1f;
695 sprintf(ptr, "%02x%02x%02x%c", r, g, b,
696 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
699 PSDRV_WriteSpool(physDev, buf, number * 7);
701 HeapFree(PSDRV_Heap, 0, buf);
705 BOOL PSDRV_WriteDIBits24(PSDRV_PDEVICE *physDev, const BYTE *bits, int number)
707 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
713 for(i = 0; i < number; i++) {
714 sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
716 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
719 PSDRV_WriteSpool(physDev, buf, number * 7);
721 HeapFree(PSDRV_Heap, 0, buf);
725 BOOL PSDRV_WriteDIBits32(PSDRV_PDEVICE *physDev, const BYTE *bits, int number)
727 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
733 for(i = 0; i < number; i++) {
734 sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
736 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
739 PSDRV_WriteSpool(physDev, buf, number * 7);
741 HeapFree(PSDRV_Heap, 0, buf);
745 BOOL PSDRV_WriteArrayGet(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nIndex)
749 sprintf(buf, psarrayget, pszArrayName, nIndex);
750 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
753 BOOL PSDRV_WriteArrayPut(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nIndex, LONG lObject)
757 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
758 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
761 BOOL PSDRV_WriteArrayDef(PSDRV_PDEVICE *physDev, CHAR *pszArrayName, INT nSize)
765 sprintf(buf, psarraydef, pszArrayName, nSize);
766 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
769 BOOL PSDRV_WriteRectClip(PSDRV_PDEVICE *physDev, INT x, INT y, INT w, INT h)
773 sprintf(buf, psrectclip, x, y, w, h);
774 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
777 BOOL PSDRV_WriteRectClip2(PSDRV_PDEVICE *physDev, CHAR *pszArrayName)
781 sprintf(buf, psrectclip2, pszArrayName);
782 return PSDRV_WriteSpool(physDev, buf, strlen(buf));
785 BOOL PSDRV_WritePatternDict(PSDRV_PDEVICE *physDev, BITMAP *bm, BYTE *bits)
787 char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
788 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n";
790 char end[] = " end\n }\n>>\n matrix makepattern setpattern\n";
795 w = bm->bmWidth & ~0x7;
796 h = bm->bmHeight & ~0x7;
798 buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
799 sprintf(buf, start, w, h, w, h);
800 PSDRV_WriteSpool(physDev, buf, strlen(buf));
801 PSDRV_WriteIndexColorSpaceBegin(physDev, 1);
802 map[0] = GetTextColor( physDev->hdc );
803 map[1] = GetBkColor( physDev->hdc );
804 PSDRV_WriteRGB(physDev, map, 2);
805 PSDRV_WriteIndexColorSpaceEnd(physDev);
807 for(y = h-1; y >= 0; y--) {
808 for(x = 0; x < w/8; x++) {
809 sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
813 PSDRV_WriteImageDict(physDev, 1, 0, 0, 8, 8, 8, 8, buf);
814 PSDRV_WriteSpool(physDev, end, sizeof(end) - 1);
815 HeapFree(PSDRV_Heap, 0, buf);