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(DC *dc, LPSTR lpData, WORD cch)
193 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
195 if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
196 if( !PSDRV_StartPage(dc) )
199 return WriteSpool16( physDev->job.hJob, lpData, cch );
203 INT PSDRV_WriteFeature(HANDLE16 hJob, char *feature, char *value,
207 char *buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psbeginfeature) +
208 strlen(feature) + strlen(value));
211 sprintf(buf, psbeginfeature, feature, value);
212 WriteSpool16( hJob, buf, strlen(buf) );
214 WriteSpool16( hJob, invocation, strlen(invocation) );
216 WriteSpool16( hJob, psendfeature, strlen(psendfeature) );
218 HeapFree( PSDRV_Heap, 0, buf );
224 INT PSDRV_WriteHeader( DC *dc, LPCSTR title )
226 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
230 int llx, lly, urx, ury;
232 TRACE("'%s'\n", title);
234 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
235 strlen(title) + 30 );
237 WARN("HeapAlloc failed\n");
241 /* BBox co-ords are in default user co-ord system so urx < ury even in
243 llx = physDev->PageSize.left * 72.0 / physDev->logPixelsX;
244 lly = physDev->PageSize.bottom * 72.0 / physDev->logPixelsY;
245 urx = physDev->PageSize.right * 72.0 / physDev->logPixelsX;
246 ury = physDev->PageSize.top * 72.0 / physDev->logPixelsY;
248 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
249 orient = "Landscape";
254 /* FIXME should do something better with BBox */
256 sprintf(buf, psheader, title, llx, lly, urx, ury, orient);
258 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
260 WARN("WriteSpool error\n");
261 HeapFree( PSDRV_Heap, 0, buf );
264 HeapFree( PSDRV_Heap, 0, buf );
266 WriteSpool16( physDev->job.hJob, psbeginprolog, strlen(psbeginprolog) );
267 WriteSpool16( physDev->job.hJob, psprolog, strlen(psprolog) );
268 WriteSpool16( physDev->job.hJob, psendprolog, strlen(psendprolog) );
270 WriteSpool16( physDev->job.hJob, psbeginsetup, strlen(psbeginsetup) );
272 for(slot = physDev->pi->ppd->InputSlots; slot; slot = slot->next) {
273 if(slot->WinBin == physDev->Devmode->dmPublic.dmDefaultSource) {
274 if(slot->InvocationString) {
275 PSDRV_WriteFeature(physDev->job.hJob, "*InputSlot", slot->Name,
276 slot->InvocationString);
282 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
283 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize) {
284 if(page->InvocationString) {
285 PSDRV_WriteFeature(physDev->job.hJob, "*PageSize", page->Name,
286 page->InvocationString);
292 WriteSpool16( physDev->job.hJob, psendsetup, strlen(psendsetup) );
299 INT PSDRV_WriteFooter( DC *dc )
301 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
304 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psfooter) + 100 );
306 WARN("HeapAlloc failed\n");
310 sprintf(buf, psfooter, physDev->job.PageNo);
312 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
314 WARN("WriteSpool error\n");
315 HeapFree( PSDRV_Heap, 0, buf );
318 HeapFree( PSDRV_Heap, 0, buf );
324 INT PSDRV_WriteEndPage( DC *dc )
326 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
328 if( WriteSpool16( physDev->job.hJob, psendpage, sizeof(psendpage)-1 ) !=
329 sizeof(psendpage)-1 ) {
330 WARN("WriteSpool error\n");
339 INT PSDRV_WriteNewPage( DC *dc )
341 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
344 signed int xtrans, ytrans, rotation;
346 sprintf(name, "%d", physDev->job.PageNo);
348 buf = (char *)HeapAlloc( PSDRV_Heap, 0, sizeof(psnewpage) + 200 );
350 WARN("HeapAlloc failed\n");
354 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
355 if(physDev->pi->ppd->LandscapeOrientation == -90) {
356 xtrans = physDev->PageSize.right;
357 ytrans = physDev->PageSize.top;
360 xtrans = physDev->PageSize.left;
361 ytrans = physDev->PageSize.bottom;
365 xtrans = physDev->PageSize.left;
366 ytrans = physDev->PageSize.top;
370 sprintf(buf, psnewpage, name, physDev->job.PageNo,
371 physDev->logPixelsX, physDev->logPixelsY,
372 xtrans, ytrans, rotation);
374 if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
376 WARN("WriteSpool error\n");
377 HeapFree( PSDRV_Heap, 0, buf );
380 HeapFree( PSDRV_Heap, 0, buf );
385 BOOL PSDRV_WriteMoveTo(DC *dc, INT x, INT y)
389 sprintf(buf, psmoveto, x, y);
390 return PSDRV_WriteSpool(dc, buf, strlen(buf));
393 BOOL PSDRV_WriteLineTo(DC *dc, INT x, INT y)
397 sprintf(buf, pslineto, x, y);
398 return PSDRV_WriteSpool(dc, buf, strlen(buf));
402 BOOL PSDRV_WriteStroke(DC *dc)
404 return PSDRV_WriteSpool(dc, psstroke, sizeof(psstroke)-1);
409 BOOL PSDRV_WriteRectangle(DC *dc, INT x, INT y, INT width,
414 sprintf(buf, psrectangle, x, y, width, height, -width);
415 return PSDRV_WriteSpool(dc, buf, strlen(buf));
418 BOOL PSDRV_WriteRRectangle(DC *dc, INT x, INT y, INT width,
423 sprintf(buf, psrrectangle, x, y, width, height, -width);
424 return PSDRV_WriteSpool(dc, buf, strlen(buf));
427 BOOL PSDRV_WriteArc(DC *dc, INT x, INT y, INT w, INT h, double ang1,
432 /* Make angles -ve and swap order because we're working with an upside
434 sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
435 return PSDRV_WriteSpool(dc, buf, strlen(buf));
438 BOOL PSDRV_WriteSetFont(DC *dc)
440 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
443 buf = (char *)HeapAlloc( PSDRV_Heap, 0,
444 sizeof(pssetfont) + strlen(physDev->font.afm->FontName) + 40);
447 WARN("HeapAlloc failed\n");
451 sprintf(buf, pssetfont, physDev->font.afm->FontName,
452 physDev->font.size, -physDev->font.size,
453 -physDev->font.escapement);
455 PSDRV_WriteSpool(dc, buf, strlen(buf));
456 HeapFree(PSDRV_Heap, 0, buf);
460 BOOL PSDRV_WriteSetColor(DC *dc, PSCOLOR *color)
462 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
465 PSDRV_CopyColor(&physDev->inkColor, color);
466 switch(color->type) {
468 sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
470 return PSDRV_WriteSpool(dc, buf, strlen(buf));
473 sprintf(buf, pssetgray, color->value.gray.i);
474 return PSDRV_WriteSpool(dc, buf, strlen(buf));
477 ERR("Unkonwn colour type %d\n", color->type);
484 BOOL PSDRV_WriteSetPen(DC *dc)
486 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
489 sprintf(buf, pssetlinewidth, physDev->pen.width);
490 PSDRV_WriteSpool(dc, buf, strlen(buf));
492 if(physDev->pen.dash) {
493 sprintf(buf, pssetdash, physDev->pen.dash, 0);
494 PSDRV_WriteSpool(dc, buf, strlen(buf));
500 BOOL PSDRV_WriteGlyphShow(DC *dc, LPCWSTR str, INT count)
505 for (i = 0; i < count; ++i)
510 name = PSDRV_UVMetrics(str[i],
511 ((PSDRV_PDEVICE *)dc->physDev)->font.afm)->N->sz;
512 l = snprintf(buf, sizeof(buf), psglyphshow, name);
514 if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1)
516 WARN("Unusable glyph name '%s' - ignoring\n", name);
520 PSDRV_WriteSpool(dc, buf, l);
526 BOOL PSDRV_WriteFill(DC *dc)
528 return PSDRV_WriteSpool(dc, psfill, sizeof(psfill)-1);
531 BOOL PSDRV_WriteEOFill(DC *dc)
533 return PSDRV_WriteSpool(dc, pseofill, sizeof(pseofill)-1);
536 BOOL PSDRV_WriteGSave(DC *dc)
538 return PSDRV_WriteSpool(dc, psgsave, sizeof(psgsave)-1);
541 BOOL PSDRV_WriteGRestore(DC *dc)
543 return PSDRV_WriteSpool(dc, psgrestore, sizeof(psgrestore)-1);
546 BOOL PSDRV_WriteNewPath(DC *dc)
548 return PSDRV_WriteSpool(dc, psnewpath, sizeof(psnewpath)-1);
551 BOOL PSDRV_WriteClosePath(DC *dc)
553 return PSDRV_WriteSpool(dc, psclosepath, sizeof(psclosepath)-1);
556 BOOL PSDRV_WriteClip(DC *dc)
558 return PSDRV_WriteSpool(dc, psclip, sizeof(psclip)-1);
561 BOOL PSDRV_WriteEOClip(DC *dc)
563 return PSDRV_WriteSpool(dc, pseoclip, sizeof(pseoclip)-1);
566 BOOL PSDRV_WriteInitClip(DC *dc)
568 return PSDRV_WriteSpool(dc, psinitclip, sizeof(psinitclip)-1);
571 BOOL PSDRV_WriteHatch(DC *dc)
573 return PSDRV_WriteSpool(dc, pshatch, sizeof(pshatch)-1);
576 BOOL PSDRV_WriteRotate(DC *dc, float ang)
580 sprintf(buf, psrotate, ang);
581 return PSDRV_WriteSpool(dc, buf, strlen(buf));
584 BOOL PSDRV_WriteIndexColorSpaceBegin(DC *dc, int size)
587 sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
588 return PSDRV_WriteSpool(dc, buf, strlen(buf));
591 BOOL PSDRV_WriteIndexColorSpaceEnd(DC *dc)
593 char buf[] = ">\n] setcolorspace\n";
594 return PSDRV_WriteSpool(dc, buf, sizeof(buf) - 1);
597 BOOL PSDRV_WriteRGB(DC *dc, COLORREF *map, int number)
599 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1), *ptr;
603 for(i = 0; i < number; i++) {
604 sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
605 (int)GetGValue(map[i]), (int)GetBValue(map[i]),
606 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
609 PSDRV_WriteSpool(dc, buf, number * 7);
610 HeapFree(PSDRV_Heap, 0, buf);
615 BOOL PSDRV_WriteImageDict(DC *dc, WORD depth, INT xDst, INT yDst,
616 INT widthDst, INT heightDst, INT widthSrc,
617 INT heightSrc, char *bits)
619 char start[] = "%d %d translate\n%d %d scale\n<<\n"
620 " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
621 " /ImageMatrix [%d 0 0 %d 0 %d]\n";
623 char decode1[] = " /Decode [0 %d]\n";
624 char decode3[] = " /Decode [0 1 0 1 0 1]\n";
626 char end[] = " /DataSource currentfile /ASCIIHexDecode filter\n>> image\n";
627 char endbits[] = " /DataSource <%s>\n>> image\n";
629 char *buf = HeapAlloc(PSDRV_Heap, 0, 1000);
631 sprintf(buf, start, xDst, yDst, widthDst, heightDst, widthSrc, heightSrc,
632 (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
634 PSDRV_WriteSpool(dc, buf, strlen(buf));
638 sprintf(buf, decode1, 255);
642 sprintf(buf, decode1, 15);
646 sprintf(buf, decode1, 1);
650 strcpy(buf, decode3);
654 PSDRV_WriteSpool(dc, buf, strlen(buf));
657 PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
659 sprintf(buf, endbits, bits);
660 PSDRV_WriteSpool(dc, buf, strlen(buf));
663 HeapFree(PSDRV_Heap, 0, buf);
668 BOOL PSDRV_WriteBytes(DC *dc, const BYTE *bytes, int number)
670 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 3 + 1);
676 for(i = 0; i < number; i++) {
677 sprintf(ptr, "%02x%c", bytes[i],
678 ((i & 0xf) == 0xf) || (i == number - 1) ? '\n' : ' ');
681 PSDRV_WriteSpool(dc, buf, number * 3);
683 HeapFree(PSDRV_Heap, 0, buf);
687 BOOL PSDRV_WriteDIBits16(DC *dc, const WORD *words, int number)
689 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
695 for(i = 0; i < number; i++) {
698 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
700 r = words[i] >> 10 & 0x1f;
702 g = words[i] >> 5 & 0x1f;
706 sprintf(ptr, "%02x%02x%02x%c", r, g, b,
707 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
710 PSDRV_WriteSpool(dc, buf, number * 7);
712 HeapFree(PSDRV_Heap, 0, buf);
716 BOOL PSDRV_WriteDIBits24(DC *dc, const BYTE *bits, int number)
718 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
724 for(i = 0; i < number; i++) {
725 sprintf(ptr, "%02x%02x%02x%c", bits[i * 3 + 2], bits[i * 3 + 1],
727 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
730 PSDRV_WriteSpool(dc, buf, number * 7);
732 HeapFree(PSDRV_Heap, 0, buf);
736 BOOL PSDRV_WriteDIBits32(DC *dc, const BYTE *bits, int number)
738 char *buf = HeapAlloc(PSDRV_Heap, 0, number * 7 + 1);
744 for(i = 0; i < number; i++) {
745 sprintf(ptr, "%02x%02x%02x%c", bits[i * 4 + 2], bits[i * 4 + 1],
747 ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
750 PSDRV_WriteSpool(dc, buf, number * 7);
752 HeapFree(PSDRV_Heap, 0, buf);
756 BOOL PSDRV_WriteArrayGet(DC *dc, CHAR *pszArrayName, INT nIndex)
760 sprintf(buf, psarrayget, pszArrayName, nIndex);
761 return PSDRV_WriteSpool(dc, buf, strlen(buf));
764 BOOL PSDRV_WriteArrayPut(DC *dc, CHAR *pszArrayName, INT nIndex, LONG lObject)
768 sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
769 return PSDRV_WriteSpool(dc, buf, strlen(buf));
772 BOOL PSDRV_WriteArrayDef(DC *dc, CHAR *pszArrayName, INT nSize)
776 sprintf(buf, psarraydef, pszArrayName, nSize);
777 return PSDRV_WriteSpool(dc, buf, strlen(buf));
780 BOOL PSDRV_WriteRectClip(DC *dc, INT x, INT y, INT w, INT h)
784 sprintf(buf, psrectclip, x, y, w, h);
785 return PSDRV_WriteSpool(dc, buf, strlen(buf));
788 BOOL PSDRV_WriteRectClip2(DC *dc, CHAR *pszArrayName)
792 sprintf(buf, psrectclip2, pszArrayName);
793 return PSDRV_WriteSpool(dc, buf, strlen(buf));
796 BOOL PSDRV_WritePatternDict(DC *dc, BITMAP *bm, BYTE *bits)
798 char start[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
799 "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n begin\n";
801 char end[] = " end\n }\n>>\n matrix makepattern setpattern\n";
806 w = bm->bmWidth & ~0x7;
807 h = bm->bmHeight & ~0x7;
809 buf = HeapAlloc(PSDRV_Heap, 0, sizeof(start) + 100);
810 sprintf(buf, start, w, h, w, h);
811 PSDRV_WriteSpool(dc, buf, strlen(buf));
812 PSDRV_WriteIndexColorSpaceBegin(dc, 1);
813 map[0] = dc->textColor;
814 map[1] = dc->backgroundColor;
815 PSDRV_WriteRGB(dc, map, 2);
816 PSDRV_WriteIndexColorSpaceEnd(dc);
818 for(y = h-1; y >= 0; y--) {
819 for(x = 0; x < w/8; x++) {
820 sprintf(ptr, "%02x", *(bits + x/8 + y * bm->bmWidthBytes));
824 PSDRV_WriteImageDict(dc, 1, 0, 0, 8, 8, 8, 8, buf);
825 PSDRV_WriteSpool(dc, end, sizeof(end) - 1);
826 HeapFree(PSDRV_Heap, 0, buf);