1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
22 #include "GlobalParams.h"
29 #include "CharCodeToUnicode.h"
30 #include "UnicodeMap.h"
36 #include "PSOutputDev.h"
39 // needed for setting type/creator of MacOS files
40 #include "ICSupport.h"
43 //------------------------------------------------------------------------
44 // PostScript prolog and setup
45 //------------------------------------------------------------------------
47 static char *prolog[] = {
48 "/xpdf 75 dict def xpdf begin",
49 "% PDF special state",
50 "/pdfDictSize 14 def",
52 " 3 1 roll 2 array astore",
53 " /setpagedevice where {",
55 " /PageSize exch def",
56 " /ImagingBBox null def",
57 " /Policies 1 dict dup begin /PageSize 3 def end def",
58 " { /Duplex true def } if",
59 " currentdict end setpagedevice",
65 " pdfDictSize dict begin",
67 " /pdfStroke [0] def",
68 " /pdfLastFill false def",
69 " /pdfLastStroke false def",
70 " /pdfTextMat [1 0 0 1 0 0] def",
71 " /pdfFontSize 0 def",
72 " /pdfCharSpacing 0 def",
73 " /pdfTextRender 0 def",
74 " /pdfTextRise 0 def",
75 " /pdfWordSpacing 0 def",
76 " /pdfHorizScaling 1 def",
78 "/pdfEndPage { end } def",
79 "% separation convention operators",
80 "/findcmykcustomcolor where {",
83 " /findcmykcustomcolor { 5 array astore } def",
85 "/setcustomcolor where {",
90 " [ exch /Separation exch dup 4 get exch /DeviceCMYK exch",
91 " 0 4 getinterval cvx",
92 " [ exch /dup load exch { mul exch dup } /forall load",
93 " /pop load dup ] cvx",
94 " ] setcolorspace setcolor",
97 "/customcolorimage where {",
100 " /customcolorimage {",
102 " [ exch /Separation exch dup 4 get exch /DeviceCMYK exch",
103 " 0 4 getinterval cvx",
104 " [ exch /dup load exch { mul exch dup } /forall load",
105 " /pop load dup ] cvx",
109 " /DataSource exch def",
110 " /ImageMatrix exch def",
111 " /BitsPerComponent exch def",
114 " /Decode [1 0] def",
122 " pdfLastStroke not {",
123 " pdfStroke aload length",
133 " findcmykcustomcolor exch setcustomcolor",
137 " /pdfLastStroke true def /pdfLastFill false def",
141 " pdfLastFill not {",
142 " pdfFill aload length",
152 " findcmykcustomcolor exch setcustomcolor",
156 " /pdfLastFill true def /pdfLastStroke false def",
161 " 4 3 roll findfont",
162 " 4 2 roll matrix scale makefont",
163 " dup length dict begin",
164 " { 1 index /FID ne { def } { pop pop } ifelse } forall",
165 " /Encoding exch def",
172 " dup length dict begin",
173 " { 1 index /FID ne { def } { pop pop } ifelse } forall",
179 "/pdfMakeFont16L3 {",
180 " 1 index /CIDFont resourcestatus {",
181 " pop pop 1 index /CIDFont findresource /CIDFontType known",
186 " 0 eq { /Identity-H } { /Identity-V } ifelse",
187 " exch 1 array astore composefont pop",
192 "% graphics state operators",
193 "/q { gsave pdfDictSize dict begin } def",
194 "/Q { end grestore } def",
195 "/cm { concat } def",
196 "/d { setdash } def",
197 "/i { setflat } def",
198 "/j { setlinejoin } def",
199 "/J { setlinecap } def",
200 "/M { setmiterlimit } def",
201 "/w { setlinewidth } def",
203 "/g { dup 1 array astore /pdfFill exch def setgray",
204 " /pdfLastFill true def /pdfLastStroke false def } def",
205 "/G { dup 1 array astore /pdfStroke exch def setgray",
206 " /pdfLastStroke true def /pdfLastFill false def } def",
207 "/rg { 3 copy 3 array astore /pdfFill exch def setrgbcolor",
208 " /pdfLastFill true def /pdfLastStroke false def } def",
209 "/RG { 3 copy 3 array astore /pdfStroke exch def setrgbcolor",
210 " /pdfLastStroke true def /pdfLastFill false def } def",
211 "/k { 4 copy 4 array astore /pdfFill exch def setcmykcolor",
212 " /pdfLastFill true def /pdfLastStroke false def } def",
213 "/K { 4 copy 4 array astore /pdfStroke exch def setcmykcolor",
214 " /pdfLastStroke true def /pdfLastFill false def } def",
215 "/ck { 6 copy 6 array astore /pdfFill exch def",
216 " findcmykcustomcolor exch setcustomcolor",
217 " /pdfLastFill true def /pdfLastStroke false def } def",
218 "/CK { 6 copy 6 array astore /pdfStroke exch def",
219 " findcmykcustomcolor exch setcustomcolor",
220 " /pdfLastStroke true def /pdfLastFill false def } def",
221 "% path segment operators",
224 "/c { curveto } def",
225 "/re { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto",
226 " neg 0 rlineto closepath } def",
227 "/h { closepath } def",
228 "% path painting operators",
229 "/S { sCol stroke } def",
230 "/Sf { fCol stroke } def",
231 "/f { fCol fill } def",
232 "/f* { fCol eofill } def",
233 "% clipping operators",
234 "/W { clip newpath } def",
235 "/W* { eoclip newpath } def",
236 "% text state operators",
237 "/Tc { /pdfCharSpacing exch def } def",
238 "/Tf { dup /pdfFontSize exch def",
239 " dup pdfHorizScaling mul exch matrix scale",
240 " pdfTextMat matrix concatmatrix dup 4 0 put dup 5 0 put",
241 " exch findfont exch makefont setfont } def",
242 "/Tr { /pdfTextRender exch def } def",
243 "/Ts { /pdfTextRise exch def } def",
244 "/Tw { /pdfWordSpacing exch def } def",
245 "/Tz { /pdfHorizScaling exch def } def",
246 "% text positioning operators",
247 "/Td { pdfTextMat transform moveto } def",
248 "/Tm { /pdfTextMat exch def } def",
249 "% text string operators",
250 "/awcp { % awidthcharpath",
252 " 1 string dup 0 3 index put 2 index charpath",
253 " 3 index 3 index rmoveto",
254 " 4 index eq { 5 index 5 index rmoveto } if",
258 "/Tj { fCol", // because stringwidth has to draw Type 3 chars
259 " 0 pdfTextRise pdfTextMat dtransform rmoveto",
260 " 1 index stringwidth pdfTextMat idtransform pop",
261 " sub 1 index length dup 0 ne { div } { pop pop 0 } ifelse",
262 " pdfWordSpacing pdfHorizScaling mul 0 pdfTextMat dtransform 32",
263 " 4 3 roll pdfCharSpacing pdfHorizScaling mul add 0",
264 " pdfTextMat dtransform",
266 " currentpoint 8 2 roll",
267 " pdfTextRender 1 and 0 eq {",
268 " 6 copy awidthshow",
270 " pdfTextRender 3 and dup 1 eq exch 2 eq or {",
272 " currentfont /FontType get 3 eq { fCol } { sCol } ifelse",
273 " false awcp currentpoint stroke moveto",
277 " 0 pdfTextRise neg pdfTextMat dtransform rmoveto } def",
278 "/Tj16 { pdfTextRender 1 and 0 eq { fCol } { sCol } ifelse",
279 " 0 pdfTextRise pdfTextMat dtransform rmoveto",
280 " 2 index stringwidth pdfTextMat idtransform pop",
282 " pdfWordSpacing pdfHorizScaling mul 0 pdfTextMat dtransform 32",
283 " 4 3 roll pdfCharSpacing pdfHorizScaling mul add 0",
284 " pdfTextMat dtransform",
285 " 6 5 roll awidthshow",
286 " 0 pdfTextRise neg pdfTextMat dtransform rmoveto } def",
287 "/Tj16V { pdfTextRender 1 and 0 eq { fCol } { sCol } ifelse",
288 " 0 pdfTextRise pdfTextMat dtransform rmoveto",
289 " 2 index stringwidth pdfTextMat idtransform exch pop",
291 " 0 pdfWordSpacing pdfTextMat dtransform 32",
292 " 4 3 roll pdfCharSpacing add 0 exch",
293 " pdfTextMat dtransform",
294 " 6 5 roll awidthshow",
295 " 0 pdfTextRise neg pdfTextMat dtransform rmoveto } def",
296 "/TJm { pdfFontSize 0.001 mul mul neg 0",
297 " pdfTextMat dtransform rmoveto } def",
298 "/TJmV { pdfFontSize 0.001 mul mul neg 0 exch",
299 " pdfTextMat dtransform rmoveto } def",
300 "% Level 1 image operators",
302 " /pdfImBuf1 4 index string def",
303 " { currentfile pdfImBuf1 readhexstring pop } image",
306 " /pdfImBuf1 4 index string def",
307 " /pdfImBuf2 4 index string def",
308 " /pdfImBuf3 4 index string def",
309 " /pdfImBuf4 4 index string def",
310 " { currentfile pdfImBuf1 readhexstring pop }",
311 " { currentfile pdfImBuf2 readhexstring pop }",
312 " { currentfile pdfImBuf3 readhexstring pop }",
313 " { currentfile pdfImBuf4 readhexstring pop }",
314 " true 4 colorimage",
317 " /pdfImBuf1 4 index 7 add 8 idiv string def",
318 " { currentfile pdfImBuf1 readhexstring pop } imagemask",
320 "% Level 2 image operators",
321 "/pdfImBuf 100 string def",
324 " { currentfile pdfImBuf readline",
325 " not { pop exit } if",
326 " (%-EOD-) eq { exit } if } loop",
329 " findcmykcustomcolor exch",
330 " dup /Width get /pdfImBuf1 exch string def",
331 " begin Width Height BitsPerComponent ImageMatrix DataSource end",
332 " /pdfImData exch def",
333 " { pdfImData pdfImBuf1 readstring pop",
334 " 0 1 2 index length 1 sub {",
335 " 1 index exch 2 copy get 255 exch sub put",
337 " 6 5 roll customcolorimage",
338 " { currentfile pdfImBuf readline",
339 " not { pop exit } if",
340 " (%-EOD-) eq { exit } if } loop",
344 " { currentfile pdfImBuf readline",
345 " not { pop exit } if",
346 " (%-EOD-) eq { exit } if } loop",
352 static char *cmapProlog[] = {
353 "/CIDInit /ProcSet findresource begin",
357 " /CMapName /Identity-H def",
358 " /CIDSystemInfo 3 dict dup begin",
359 " /Registry (Adobe) def",
360 " /Ordering (Identity) def",
361 " /Supplement 0 def",
363 " 1 begincodespacerange",
365 " endcodespacerange",
371 " currentdict CMapName exch /CMap defineresource pop",
376 " /CMapName /Identity-V def",
377 " /CIDSystemInfo 3 dict dup begin",
378 " /Registry (Adobe) def",
379 " /Ordering (Identity) def",
380 " /Supplement 0 def",
383 " 1 begincodespacerange",
385 " endcodespacerange",
391 " currentdict CMapName exch /CMap defineresource pop",
397 //------------------------------------------------------------------------
399 //------------------------------------------------------------------------
402 char *psName; // PostScript name
403 double mWidth; // width of 'm' character
406 static char *psFonts[] = {
410 "Courier-BoldOblique",
414 "Helvetica-BoldOblique",
424 static PSSubstFont psSubstFonts[] = {
425 {"Helvetica", 0.833},
426 {"Helvetica-Oblique", 0.833},
427 {"Helvetica-Bold", 0.889},
428 {"Helvetica-BoldOblique", 0.889},
429 {"Times-Roman", 0.788},
430 {"Times-Italic", 0.722},
431 {"Times-Bold", 0.833},
432 {"Times-BoldItalic", 0.778},
434 {"Courier-Oblique", 0.600},
435 {"Courier-Bold", 0.600},
436 {"Courier-BoldOblique", 0.600}
439 // Encoding info for substitute 16-bit font
445 //------------------------------------------------------------------------
447 //------------------------------------------------------------------------
449 #define psProcessCyan 1
450 #define psProcessMagenta 2
451 #define psProcessYellow 4
452 #define psProcessBlack 8
453 #define psProcessCMYK 15
455 //------------------------------------------------------------------------
457 //------------------------------------------------------------------------
459 class PSOutCustomColor {
462 PSOutCustomColor(double cA, double mA,
463 double yA, double kA, GString *nameA);
468 PSOutCustomColor *next;
471 PSOutCustomColor::PSOutCustomColor(double cA, double mA,
472 double yA, double kA, GString *nameA) {
481 PSOutCustomColor::~PSOutCustomColor() {
485 //------------------------------------------------------------------------
487 //------------------------------------------------------------------------
490 typedef void (*SignalFunc)(int);
493 static void outputToFile(void *stream, char *data, int len) {
494 fwrite(data, 1, len, (FILE *)stream);
497 PSOutputDev::PSOutputDev(char *fileName, XRef *xrefA, Catalog *catalog,
498 int firstPage, int lastPage, PSOutMode modeA) {
500 PSFileType fileTypeA;
504 fontFileNames = NULL;
511 if (!strcmp(fileName, "-")) {
512 fileTypeA = psStdout;
514 } else if (fileName[0] == '|') {
518 signal(SIGPIPE, (SignalFunc)SIG_IGN);
520 if (!(f = popen(fileName + 1, "w"))) {
521 error(-1, "Couldn't run print command '%s'", fileName);
526 error(-1, "Print commands are not supported ('%s')", fileName);
532 if (!(f = fopen(fileName, "w"))) {
533 error(-1, "Couldn't open PostScript file '%s'", fileName);
539 init(outputToFile, f, fileTypeA,
540 xrefA, catalog, firstPage, lastPage, modeA);
543 PSOutputDev::PSOutputDev(PSOutputFunc outputFuncA, void *outputStreamA,
544 XRef *xrefA, Catalog *catalog,
545 int firstPage, int lastPage, PSOutMode modeA) {
548 fontFileNames = NULL;
554 init(outputFuncA, outputStreamA, psGeneric,
555 xrefA, catalog, firstPage, lastPage, modeA);
558 void PSOutputDev::init(PSOutputFunc outputFuncA, void *outputStreamA,
559 PSFileType fileTypeA, XRef *xrefA, Catalog *catalog,
560 int firstPage, int lastPage, PSOutMode modeA) {
572 outputFunc = outputFuncA;
573 outputStream = outputStreamA;
574 fileType = fileTypeA;
576 level = globalParams->getPSLevel();
578 paperWidth = globalParams->getPSPaperWidth();
579 paperHeight = globalParams->getPSPaperHeight();
580 if (mode == psModeForm) {
581 lastPage = firstPage;
584 inType3Char = gFalse;
587 // initialize OPI nesting levels
592 // initialize fontIDs, fontFileIDs, and fontFileNames lists
595 fontIDs = (Ref *)gmalloc(fontIDSize * sizeof(Ref));
598 fontFileIDs = (Ref *)gmalloc(fontFileIDSize * sizeof(Ref));
599 fontFileNameSize = 64;
601 fontFileNames = (GString **)gmalloc(fontFileNameSize * sizeof(GString *));
605 // initialize embedded font resource comment list
606 embFontList = new GString();
611 writePS("%!PS-Adobe-3.0\n");
612 writePSFmt("%%%%Creator: xpdf/pdftops %s\n", xpdfVersion);
613 writePSFmt("%%%%LanguageLevel: %d\n",
614 (level == psLevel1 || level == psLevel1Sep) ? 1 :
615 (level == psLevel2 || level == psLevel2Sep) ? 2 : 3);
616 if (level == psLevel1Sep || level == psLevel2Sep || level == psLevel3Sep) {
617 writePS("%%DocumentProcessColors: (atend)\n");
618 writePS("%%DocumentCustomColors: (atend)\n");
620 writePS("%%DocumentSuppliedResources: (atend)\n");
621 writePSFmt("%%%%DocumentMedia: plain %d %d 0 () ()\n",
622 paperWidth, paperHeight);
623 writePSFmt("%%%%Pages: %d\n", lastPage - firstPage + 1);
624 writePS("%%EndComments\n");
625 writePS("%%BeginDefaults\n");
626 writePS("%%PageMedia: plain\n");
627 writePS("%%EndDefaults\n");
630 writePS("%!PS-Adobe-3.0 EPSF-3.0\n");
631 writePSFmt("%%%%Creator: xpdf/pdftops %s\n", xpdfVersion);
632 writePSFmt("%%%%LanguageLevel: %d\n",
633 (level == psLevel1 || level == psLevel1Sep) ? 1 :
634 (level == psLevel2 || level == psLevel2Sep) ? 2 : 3);
635 if (level == psLevel1Sep || level == psLevel2Sep || level == psLevel3Sep) {
636 writePS("%%DocumentProcessColors: (atend)\n");
637 writePS("%%DocumentCustomColors: (atend)\n");
639 page = catalog->getPage(firstPage);
640 box = page->getBox();
641 writePSFmt("%%%%BoundingBox: %d %d %d %d\n",
642 (int)floor(box->x1), (int)floor(box->y1),
643 (int)ceil(box->x2), (int)ceil(box->y2));
644 if (floor(box->x1) != ceil(box->x1) ||
645 floor(box->y1) != ceil(box->y1) ||
646 floor(box->x2) != ceil(box->x2) ||
647 floor(box->y2) != ceil(box->y2)) {
648 writePSFmt("%%%%HiResBoundingBox: %g %g %g %g\n",
649 box->x1, box->y1, box->x2, box->y2);
651 writePS("%%DocumentSuppliedResources: (atend)\n");
652 writePS("%%EndComments\n");
655 writePS("%!PS-Adobe-3.0 Resource-Form\n");
656 writePSFmt("%%%%Creator: xpdf/pdftops %s\n", xpdfVersion);
657 writePSFmt("%%%%LanguageLevel: %d\n",
658 (level == psLevel1 || level == psLevel1Sep) ? 1 :
659 (level == psLevel2 || level == psLevel2Sep) ? 2 : 3);
660 if (level == psLevel1Sep || level == psLevel2Sep || level == psLevel3Sep) {
661 writePS("%%DocumentProcessColors: (atend)\n");
662 writePS("%%DocumentCustomColors: (atend)\n");
664 writePS("%%DocumentSuppliedResources: (atend)\n");
665 writePS("%%EndComments\n");
666 page = catalog->getPage(firstPage);
667 box = page->getBox();
668 writePS("32 dict dup begin\n");
669 writePSFmt("/BBox [%d %d %d %d] def\n",
670 (int)box->x1, (int)box->y1, (int)box->x2, (int)box->y2);
671 writePS("/FormType 1 def\n");
672 writePS("/Matrix [1 0 0 1 0 0] def\n");
677 if (mode != psModeForm) {
678 writePS("%%BeginProlog\n");
680 writePSFmt("%%%%BeginResource: procset xpdf %s 0\n", xpdfVersion);
681 for (p = prolog; *p; ++p) {
682 writePSFmt("%s\n", *p);
684 writePS("%%EndResource\n");
685 if (level >= psLevel3) {
686 for (p = cmapProlog; *p; ++p) {
687 writePSFmt("%s\n", *p);
690 if (mode != psModeForm) {
691 writePS("%%EndProlog\n");
694 // set up fonts and images
695 if (mode == psModeForm) {
696 // swap the form and xpdf dicts
697 writePS("xpdf end begin dup begin\n");
699 writePS("%%BeginSetup\n");
700 writePS("xpdf begin\n");
702 for (pg = firstPage; pg <= lastPage; ++pg) {
703 page = catalog->getPage(pg);
704 if ((resDict = page->getResourceDict())) {
705 setupResources(resDict);
707 annots = new Annots(xref, page->getAnnots(&obj1));
709 for (i = 0; i < annots->getNumAnnots(); ++i) {
710 if (annots->getAnnot(i)->getAppearance(&obj1)->isStream()) {
711 obj1.streamGetDict()->lookup("Resources", &obj2);
713 setupResources(obj2.getDict());
721 if (mode != psModeForm) {
722 if (mode != psModeEPS) {
723 writePSFmt("%d %d %s pdfSetup\n",
724 paperWidth, paperHeight,
725 globalParams->getPSDuplex() ? "true" : "false");
728 if (globalParams->getPSOPI()) {
729 writePS("/opiMatrix matrix currentmatrix def\n");
732 writePS("%%EndSetup\n");
735 // initialize sequential page number
739 PSOutputDev::~PSOutputDev() {
740 PSOutCustomColor *cc;
744 if (mode == psModeForm) {
745 writePS("/Foo exch /Form defineresource pop\n");
747 writePS("%%Trailer\n");
749 writePS("%%DocumentSuppliedResources:\n");
750 writePS(embFontList->getCString());
751 if (level == psLevel1Sep || level == psLevel2Sep ||
752 level == psLevel3Sep) {
753 writePS("%%DocumentProcessColors:");
754 if (processColors & psProcessCyan) {
757 if (processColors & psProcessMagenta) {
760 if (processColors & psProcessYellow) {
763 if (processColors & psProcessBlack) {
767 writePS("%%DocumentCustomColors:");
768 for (cc = customColors; cc; cc = cc->next) {
769 writePSFmt(" (%s)", cc->name->getCString());
772 writePS("%%CMYKCustomColor:\n");
773 for (cc = customColors; cc; cc = cc->next) {
774 writePSFmt("%%%%+ %g %g %g %g (%s)\n",
775 cc->c, cc->m, cc->y, cc->k, cc->name->getCString());
780 if (fileType == psFile) {
782 ICS_MapRefNumAndAssign((short)((FILE *)outputStream)->handle);
784 fclose((FILE *)outputStream);
787 else if (fileType == psPipe) {
788 pclose((FILE *)outputStream);
790 signal(SIGPIPE, (SignalFunc)SIG_DFL);
805 for (i = 0; i < fontFileNameLen; ++i) {
806 delete fontFileNames[i];
808 gfree(fontFileNames);
811 for (i = 0; i < font16EncLen; ++i) {
812 delete font16Enc[i].enc;
816 while (customColors) {
818 customColors = cc->next;
823 void PSOutputDev::setupResources(Dict *resDict) {
824 Object xObjDict, xObj, resObj;
828 setupImages(resDict);
830 resDict->lookup("XObject", &xObjDict);
831 if (xObjDict.isDict()) {
832 for (i = 0; i < xObjDict.dictGetLength(); ++i) {
833 xObjDict.dictGetVal(i, &xObj);
834 if (xObj.isStream()) {
835 xObj.streamGetDict()->lookup("Resources", &resObj);
836 if (resObj.isDict()) {
837 setupResources(resObj.getDict());
847 void PSOutputDev::setupFonts(Dict *resDict) {
849 GfxFontDict *gfxFontDict;
853 resDict->lookup("Font", &fontDict);
854 if (fontDict.isDict()) {
855 gfxFontDict = new GfxFontDict(xref, fontDict.getDict());
856 for (i = 0; i < gfxFontDict->getNumFonts(); ++i) {
857 font = gfxFontDict->getFont(i);
858 setupFont(font, resDict);
865 void PSOutputDev::setupFont(GfxFont *font, Dict *parentResDict) {
868 PSFontParam *fontParam;
871 char type3Name[64], buf[16];
880 // check if font is already set up
881 for (i = 0; i < fontIDLen; ++i) {
882 if (fontIDs[i].num == font->getID()->num &&
883 fontIDs[i].gen == font->getID()->gen) {
888 // add entry to fontIDs list
889 if (fontIDLen >= fontIDSize) {
891 fontIDs = (Ref *)grealloc(fontIDs, fontIDSize * sizeof(Ref));
893 fontIDs[fontIDLen++] = *font->getID();
898 // check for resident 8-bit font
899 if (font->getName() &&
900 (fontParam = globalParams->getPSFont(font->getName()))) {
901 psName = fontParam->psFontName->getCString();
903 // check for embedded Type 1 font
904 } else if (globalParams->getPSEmbedType1() &&
905 font->getType() == fontType1 &&
906 font->getEmbeddedFontID(&fontFileID)) {
907 psNameStr = filterPSName(font->getEmbeddedFontName());
908 psName = psNameStr->getCString();
909 setupEmbeddedType1Font(&fontFileID, psName);
911 // check for embedded Type 1C font
912 } else if (globalParams->getPSEmbedType1() &&
913 font->getType() == fontType1C &&
914 font->getEmbeddedFontID(&fontFileID)) {
915 psNameStr = filterPSName(font->getEmbeddedFontName());
916 psName = psNameStr->getCString();
917 setupEmbeddedType1CFont(font, &fontFileID, psName);
919 // check for external Type 1 font file
920 } else if (globalParams->getPSEmbedType1() &&
921 font->getType() == fontType1 &&
922 font->getExtFontFile()) {
923 // this assumes that the PS font name matches the PDF font name
924 psName = font->getName()->getCString();
925 setupExternalType1Font(font->getExtFontFile(), psName);
927 // check for embedded TrueType font
928 } else if (globalParams->getPSEmbedTrueType() &&
929 font->getType() == fontTrueType &&
930 font->getEmbeddedFontID(&fontFileID)) {
931 psNameStr = filterPSName(font->getEmbeddedFontName());
932 psName = psNameStr->getCString();
933 setupEmbeddedTrueTypeFont(font, &fontFileID, psName);
935 // check for external TrueType font file
936 } else if (globalParams->getPSEmbedTrueType() &&
937 font->getType() == fontTrueType &&
938 font->getExtFontFile()) {
939 psNameStr = filterPSName(font->getName());
940 psName = psNameStr->getCString();
941 setupExternalTrueTypeFont(font, psName);
943 // check for embedded CID PostScript font
944 } else if (globalParams->getPSEmbedCIDPostScript() &&
945 font->getType() == fontCIDType0C &&
946 font->getEmbeddedFontID(&fontFileID)) {
947 psNameStr = filterPSName(font->getEmbeddedFontName());
948 psName = psNameStr->getCString();
949 setupEmbeddedCIDType0Font(font, &fontFileID, psName);
951 // check for embedded CID TrueType font
952 } else if (globalParams->getPSEmbedCIDTrueType() &&
953 font->getType() == fontCIDType2 &&
954 font->getEmbeddedFontID(&fontFileID)) {
955 psNameStr = filterPSName(font->getEmbeddedFontName());
956 psName = psNameStr->getCString();
957 setupEmbeddedCIDTrueTypeFont(font, &fontFileID, psName);
959 } else if (font->getType() == fontType3) {
960 sprintf(type3Name, "T3_%d_%d",
961 font->getID()->num, font->getID()->gen);
963 setupType3Font(font, psName, parentResDict);
965 // do 8-bit font substitution
966 } else if (!font->isCIDFont()) {
967 name = font->getName();
970 for (i = 0; psFonts[i]; ++i) {
971 if (name->cmp(psFonts[i]) == 0) {
978 if (font->isFixedWidth()) {
980 } else if (font->isSerif()) {
985 if (font->isBold()) {
988 if (font->isItalic()) {
991 psName = psSubstFonts[i].psName;
992 for (code = 0; code < 256; ++code) {
993 if ((charName = ((Gfx8BitFont *)font)->getCharName(code)) &&
994 charName[0] == 'm' && charName[1] == '\0') {
999 w1 = ((Gfx8BitFont *)font)->getWidth(code);
1003 w2 = psSubstFonts[i].mWidth;
1008 if (font->getType() == fontType3) {
1009 // This is a hack which makes it possible to substitute for some
1010 // Type 3 fonts. The problem is that it's impossible to know what
1011 // the base coordinate system used in the font is without actually
1012 // rendering the font.
1014 fm = font->getFontMatrix();
1016 ys *= fm[3] / fm[0];
1023 // do 16-bit font substitution
1024 } else if ((fontParam = globalParams->
1025 getPSFont16(font->getName(),
1026 ((GfxCIDFont *)font)->getCollection(),
1027 font->getWMode()))) {
1028 psName = fontParam->psFontName->getCString();
1029 if (font16EncLen >= font16EncSize) {
1030 font16EncSize += 16;
1031 font16Enc = (PSFont16Enc *)grealloc(font16Enc,
1032 font16EncSize * sizeof(PSFont16Enc));
1034 font16Enc[font16EncLen].fontID = *font->getID();
1035 font16Enc[font16EncLen].enc = fontParam->encoding->copy();
1036 if ((uMap = globalParams->getUnicodeMap(font16Enc[font16EncLen].enc))) {
1040 error(-1, "Couldn't find Unicode map for 16-bit font encoding '%s'",
1041 font16Enc[font16EncLen].enc->getCString());
1044 // give up - can't do anything with this font
1046 error(-1, "Couldn't find a font to substitute for '%s' ('%s' character collection)",
1047 font->getName() ? font->getName()->getCString() : "(unnamed)",
1048 ((GfxCIDFont *)font)->getCollection()
1049 ? ((GfxCIDFont *)font)->getCollection()->getCString()
1054 // generate PostScript code to set up the font
1055 if (font->isCIDFont()) {
1056 if (level == psLevel3 || level == psLevel3Sep) {
1057 writePSFmt("/F%d_%d /%s %d pdfMakeFont16L3\n",
1058 font->getID()->num, font->getID()->gen, psName,
1061 writePSFmt("/F%d_%d /%s %d pdfMakeFont16\n",
1062 font->getID()->num, font->getID()->gen, psName,
1066 writePSFmt("/F%d_%d /%s %g %g\n",
1067 font->getID()->num, font->getID()->gen, psName, xs, ys);
1068 for (i = 0; i < 256; i += 8) {
1069 writePSFmt((i == 0) ? "[ " : " ");
1070 for (j = 0; j < 8; ++j) {
1071 if (font->getType() == fontTrueType &&
1072 !((Gfx8BitFont *)font)->getHasEncoding()) {
1073 sprintf(buf, "c%02x", i+j);
1076 charName = ((Gfx8BitFont *)font)->getCharName(i+j);
1077 // this is a kludge for broken PDF files that encode char 32
1079 if (i+j == 32 && charName && !strcmp(charName, ".notdef")) {
1084 writePSName(charName ? charName : (char *)".notdef");
1086 writePS((i == 256-8) ? (char *)"]\n" : (char *)"\n");
1088 writePS("pdfMakeFont\n");
1096 void PSOutputDev::setupEmbeddedType1Font(Ref *id, char *psName) {
1097 static char hexChar[17] = "0123456789abcdef";
1098 Object refObj, strObj, obj1, obj2;
1100 int length1, length2;
1106 // check if font is already embedded
1107 for (i = 0; i < fontFileIDLen; ++i) {
1108 if (fontFileIDs[i].num == id->num &&
1109 fontFileIDs[i].gen == id->gen)
1113 // add entry to fontFileIDs list
1114 if (fontFileIDLen >= fontFileIDSize) {
1115 fontFileIDSize += 64;
1116 fontFileIDs = (Ref *)grealloc(fontFileIDs, fontFileIDSize * sizeof(Ref));
1118 fontFileIDs[fontFileIDLen++] = *id;
1120 // get the font stream and info
1121 refObj.initRef(id->num, id->gen);
1122 refObj.fetch(xref, &strObj);
1124 if (!strObj.isStream()) {
1125 error(-1, "Embedded font file object is not a stream");
1128 if (!(dict = strObj.streamGetDict())) {
1129 error(-1, "Embedded font stream is missing its dictionary");
1132 dict->lookup("Length1", &obj1);
1133 dict->lookup("Length2", &obj2);
1134 if (!obj1.isInt() || !obj2.isInt()) {
1135 error(-1, "Missing length fields in embedded font stream dictionary");
1140 length1 = obj1.getInt();
1141 length2 = obj2.getInt();
1145 // beginning comment
1146 writePSFmt("%%%%BeginResource: font %s\n", psName);
1147 embFontList->append("%%+ font ");
1148 embFontList->append(psName);
1149 embFontList->append("\n");
1151 // copy ASCII portion of font
1152 strObj.streamReset();
1153 for (i = 0; i < length1 && (c = strObj.streamGetChar()) != EOF; ++i) {
1157 // figure out if encrypted portion is binary or ASCII
1159 for (i = 0; i < 4; ++i) {
1160 start[i] = strObj.streamGetChar();
1161 if (start[i] == EOF) {
1162 error(-1, "Unexpected end of file in embedded font stream");
1165 if (!((start[i] >= '0' && start[i] <= '9') ||
1166 (start[i] >= 'A' && start[i] <= 'F') ||
1167 (start[i] >= 'a' && start[i] <= 'f')))
1171 // convert binary data to ASCII
1173 for (i = 0; i < 4; ++i) {
1174 writePSChar(hexChar[(start[i] >> 4) & 0x0f]);
1175 writePSChar(hexChar[start[i] & 0x0f]);
1177 while (i < length2) {
1178 if ((c = strObj.streamGetChar()) == EOF) {
1181 writePSChar(hexChar[(c >> 4) & 0x0f]);
1182 writePSChar(hexChar[c & 0x0f]);
1183 if (++i % 32 == 0) {
1191 // already in ASCII format -- just copy it
1193 for (i = 0; i < 4; ++i) {
1194 writePSChar(start[i]);
1196 for (i = 4; i < length2; ++i) {
1197 if ((c = strObj.streamGetChar()) == EOF) {
1204 // write padding and "cleartomark"
1205 for (i = 0; i < 8; ++i) {
1206 writePS("00000000000000000000000000000000"
1207 "00000000000000000000000000000000\n");
1209 writePS("cleartomark\n");
1212 writePS("%%EndResource\n");
1215 strObj.streamClose();
1219 //~ This doesn't handle .pfb files or binary eexec data (which only
1220 //~ happens in pfb files?).
1221 void PSOutputDev::setupExternalType1Font(GString *fileName, char *psName) {
1226 // check if font is already embedded
1227 for (i = 0; i < fontFileNameLen; ++i) {
1228 if (!fontFileNames[i]->cmp(fileName)) {
1233 // add entry to fontFileNames list
1234 if (fontFileNameLen >= fontFileNameSize) {
1235 fontFileNameSize += 64;
1236 fontFileNames = (GString **)grealloc(fontFileNames,
1237 fontFileNameSize * sizeof(GString *));
1239 fontFileNames[fontFileNameLen++] = fileName->copy();
1241 // beginning comment
1242 writePSFmt("%%%%BeginResource: font %s\n", psName);
1243 embFontList->append("%%+ font ");
1244 embFontList->append(psName);
1245 embFontList->append("\n");
1247 // copy the font file
1248 if (!(fontFile = fopen(fileName->getCString(), "rb"))) {
1249 error(-1, "Couldn't open external font file");
1252 while ((c = fgetc(fontFile)) != EOF) {
1258 writePS("%%EndResource\n");
1261 void PSOutputDev::setupEmbeddedType1CFont(GfxFont *font, Ref *id,
1265 Type1CFontFile *t1cFile;
1268 // check if font is already embedded
1269 for (i = 0; i < fontFileIDLen; ++i) {
1270 if (fontFileIDs[i].num == id->num &&
1271 fontFileIDs[i].gen == id->gen)
1275 // add entry to fontFileIDs list
1276 if (fontFileIDLen >= fontFileIDSize) {
1277 fontFileIDSize += 64;
1278 fontFileIDs = (Ref *)grealloc(fontFileIDs, fontFileIDSize * sizeof(Ref));
1280 fontFileIDs[fontFileIDLen++] = *id;
1282 // beginning comment
1283 writePSFmt("%%%%BeginResource: font %s\n", psName);
1284 embFontList->append("%%+ font ");
1285 embFontList->append(psName);
1286 embFontList->append("\n");
1288 // convert it to a Type 1 font
1289 fontBuf = font->readEmbFontFile(xref, &fontLen);
1290 t1cFile = new Type1CFontFile(fontBuf, fontLen);
1291 t1cFile->convertToType1(outputFunc, outputStream);
1296 writePS("%%EndResource\n");
1299 void PSOutputDev::setupEmbeddedTrueTypeFont(GfxFont *font, Ref *id,
1303 TrueTypeFontFile *ttFile;
1304 CharCodeToUnicode *ctu;
1307 // check if font is already embedded
1308 for (i = 0; i < fontFileIDLen; ++i) {
1309 if (fontFileIDs[i].num == id->num &&
1310 fontFileIDs[i].gen == id->gen)
1314 // add entry to fontFileIDs list
1315 if (fontFileIDLen >= fontFileIDSize) {
1316 fontFileIDSize += 64;
1317 fontFileIDs = (Ref *)grealloc(fontFileIDs, fontFileIDSize * sizeof(Ref));
1319 fontFileIDs[fontFileIDLen++] = *id;
1321 // beginning comment
1322 writePSFmt("%%%%BeginResource: font %s\n", psName);
1323 embFontList->append("%%+ font ");
1324 embFontList->append(psName);
1325 embFontList->append("\n");
1327 // convert it to a Type 42 font
1328 fontBuf = font->readEmbFontFile(xref, &fontLen);
1329 ttFile = new TrueTypeFontFile(fontBuf, fontLen);
1330 ctu = ((Gfx8BitFont *)font)->getToUnicode();
1331 ttFile->convertToType42(psName, ((Gfx8BitFont *)font)->getEncoding(),
1332 ctu, ((Gfx8BitFont *)font)->getHasEncoding(),
1333 outputFunc, outputStream);
1339 writePS("%%EndResource\n");
1342 void PSOutputDev::setupExternalTrueTypeFont(GfxFont *font, char *psName) {
1346 TrueTypeFontFile *ttFile;
1347 CharCodeToUnicode *ctu;
1350 // check if font is already embedded
1351 fileName = font->getExtFontFile();
1352 for (i = 0; i < fontFileNameLen; ++i) {
1353 if (!fontFileNames[i]->cmp(fileName)) {
1358 // add entry to fontFileNames list
1359 if (fontFileNameLen >= fontFileNameSize) {
1360 fontFileNameSize += 64;
1361 fontFileNames = (GString **)grealloc(fontFileNames,
1362 fontFileNameSize * sizeof(GString *));
1364 fontFileNames[fontFileNameLen++] = fileName->copy();
1366 // beginning comment
1367 writePSFmt("%%%%BeginResource: font %s\n", psName);
1368 embFontList->append("%%+ font ");
1369 embFontList->append(psName);
1370 embFontList->append("\n");
1372 // convert it to a Type 42 font
1373 fontBuf = font->readExtFontFile(&fontLen);
1374 ttFile = new TrueTypeFontFile(fontBuf, fontLen);
1375 ctu = ((Gfx8BitFont *)font)->getToUnicode();
1376 ttFile->convertToType42(psName, ((Gfx8BitFont *)font)->getEncoding(),
1377 ctu, ((Gfx8BitFont *)font)->getHasEncoding(),
1378 outputFunc, outputStream);
1384 writePS("%%EndResource\n");
1387 void PSOutputDev::setupEmbeddedCIDType0Font(GfxFont *font, Ref *id,
1391 Type1CFontFile *t1cFile;
1394 // check if font is already embedded
1395 for (i = 0; i < fontFileIDLen; ++i) {
1396 if (fontFileIDs[i].num == id->num &&
1397 fontFileIDs[i].gen == id->gen)
1401 // add entry to fontFileIDs list
1402 if (fontFileIDLen >= fontFileIDSize) {
1403 fontFileIDSize += 64;
1404 fontFileIDs = (Ref *)grealloc(fontFileIDs, fontFileIDSize * sizeof(Ref));
1406 fontFileIDs[fontFileIDLen++] = *id;
1408 // beginning comment
1409 writePSFmt("%%%%BeginResource: font %s\n", psName);
1410 embFontList->append("%%+ font ");
1411 embFontList->append(psName);
1412 embFontList->append("\n");
1414 // convert it to a Type 0 font
1415 fontBuf = font->readEmbFontFile(xref, &fontLen);
1416 t1cFile = new Type1CFontFile(fontBuf, fontLen);
1417 if (globalParams->getPSLevel() >= psLevel3) {
1418 // Level 3: use a CID font
1419 t1cFile->convertToCIDType0(psName, outputFunc, outputStream);
1421 // otherwise: use a non-CID composite font
1422 t1cFile->convertToType0(psName, outputFunc, outputStream);
1428 writePS("%%EndResource\n");
1431 void PSOutputDev::setupEmbeddedCIDTrueTypeFont(GfxFont *font, Ref *id,
1435 TrueTypeFontFile *ttFile;
1438 // check if font is already embedded
1439 for (i = 0; i < fontFileIDLen; ++i) {
1440 if (fontFileIDs[i].num == id->num &&
1441 fontFileIDs[i].gen == id->gen)
1445 // add entry to fontFileIDs list
1446 if (fontFileIDLen >= fontFileIDSize) {
1447 fontFileIDSize += 64;
1448 fontFileIDs = (Ref *)grealloc(fontFileIDs, fontFileIDSize * sizeof(Ref));
1450 fontFileIDs[fontFileIDLen++] = *id;
1452 // beginning comment
1453 writePSFmt("%%%%BeginResource: font %s\n", psName);
1454 embFontList->append("%%+ font ");
1455 embFontList->append(psName);
1456 embFontList->append("\n");
1458 // convert it to a Type 0 font
1459 fontBuf = font->readEmbFontFile(xref, &fontLen);
1460 ttFile = new TrueTypeFontFile(fontBuf, fontLen);
1461 if (globalParams->getPSLevel() >= psLevel3) {
1462 ttFile->convertToCIDType2(psName,
1463 ((GfxCIDFont *)font)->getCIDToGID(),
1464 ((GfxCIDFont *)font)->getCIDToGIDLen(),
1465 outputFunc, outputStream);
1467 // otherwise: use a non-CID composite font
1468 ttFile->convertToType0(psName, ((GfxCIDFont *)font)->getCIDToGID(),
1469 ((GfxCIDFont *)font)->getCIDToGIDLen(),
1470 outputFunc, outputStream);
1476 writePS("%%EndResource\n");
1479 void PSOutputDev::setupType3Font(GfxFont *font, char *psName,
1480 Dict *parentResDict) {
1490 // set up resources used by font
1491 if ((resDict = ((Gfx8BitFont *)font)->getResources())) {
1492 setupResources(resDict);
1494 resDict = parentResDict;
1497 // beginning comment
1498 writePSFmt("%%%%BeginResource: font %s\n", psName);
1499 embFontList->append("%%+ font ");
1500 embFontList->append(psName);
1501 embFontList->append("\n");
1504 writePS("7 dict begin\n");
1505 writePS("/FontType 3 def\n");
1506 m = font->getFontMatrix();
1507 writePSFmt("/FontMatrix [%g %g %g %g %g %g] def\n",
1508 m[0], m[1], m[2], m[3], m[4], m[5]);
1509 m = font->getFontBBox();
1510 writePSFmt("/FontBBox [%g %g %g %g] def\n",
1511 m[0], m[1], m[2], m[3]);
1512 writePS("/Encoding 256 array def\n");
1513 writePS(" 0 1 255 { Encoding exch /.notdef put } for\n");
1514 writePS("/BuildGlyph {\n");
1515 writePS(" exch /CharProcs get exch\n");
1516 writePS(" 2 copy known not { pop /.notdef } if\n");
1517 writePS(" get exec\n");
1518 writePS("} bind def\n");
1519 writePS("/BuildChar {\n");
1520 writePS(" 1 index /Encoding get exch get\n");
1521 writePS(" 1 index /BuildGlyph get exec\n");
1522 writePS("} bind def\n");
1523 if ((charProcs = ((Gfx8BitFont *)font)->getCharProcs())) {
1524 writePSFmt("/CharProcs %d dict def\n", charProcs->getLength());
1525 writePS("CharProcs begin\n");
1530 gfx = new Gfx(xref, this, resDict, &box, gFalse, NULL);
1531 inType3Char = gTrue;
1532 t3Cacheable = gFalse;
1533 for (i = 0; i < charProcs->getLength(); ++i) {
1535 writePSName(charProcs->getKey(i));
1537 gfx->display(charProcs->getVal(i, &charProc));
1541 sprintf(buf, "%g %g %g %g %g %g setcachedevice\n",
1542 t3WX, t3WY, t3LLX, t3LLY, t3URX, t3URY);
1544 sprintf(buf, "%g %g setcharwidth\n", t3WX, t3WY);
1546 (*outputFunc)(outputStream, buf, strlen(buf));
1547 (*outputFunc)(outputStream, t3String->getCString(),
1548 t3String->getLength());
1552 (*outputFunc)(outputStream, "Q\n", 2);
1555 inType3Char = gFalse;
1559 writePS("currentdict end\n");
1560 writePSFmt("/%s exch definefont pop\n", psName);
1563 writePS("%%EndResource\n");
1566 void PSOutputDev::setupImages(Dict *resDict) {
1567 Object xObjDict, xObj, xObjRef, subtypeObj;
1570 if (mode != psModeForm) {
1574 resDict->lookup("XObject", &xObjDict);
1575 if (xObjDict.isDict()) {
1576 for (i = 0; i < xObjDict.dictGetLength(); ++i) {
1577 xObjDict.dictGetValNF(i, &xObjRef);
1578 xObjDict.dictGetVal(i, &xObj);
1579 if (xObj.isStream()) {
1580 xObj.streamGetDict()->lookup("Subtype", &subtypeObj);
1581 if (subtypeObj.isName("Image")) {
1582 if (xObjRef.isRef()) {
1583 setupImage(xObjRef.getRef(), xObj.getStream());
1585 error(-1, "Image in resource dict is not an indirect reference");
1597 void PSOutputDev::setupImage(Ref id, Stream *str) {
1599 int size, line, col, i;
1601 // construct an encoder stream
1602 if (globalParams->getPSASCIIHex()) {
1603 str = new ASCIIHexEncoder(str);
1605 str = new ASCII85Encoder(str);
1608 // compute image data size
1614 } while (c == '\n' || c == '\r');
1615 if (c == '~' || c == EOF) {
1622 for (i = 1; i <= 4; ++i) {
1625 } while (c == '\n' || c == '\r');
1626 if (c == '~' || c == EOF) {
1636 } while (c != '~' && c != EOF);
1638 writePSFmt("%d array dup /ImData_%d_%d exch def\n", size, id.num, id.gen);
1640 // write the data into the array
1643 writePS("dup 0 <~");
1647 } while (c == '\n' || c == '\r');
1648 if (c == '~' || c == EOF) {
1657 for (i = 1; i <= 4; ++i) {
1660 } while (c == '\n' || c == '\r');
1661 if (c == '~' || c == EOF) {
1668 // each line is: "dup nnnnn <~...data...~> put<eol>"
1669 // so max data length = 255 - 20 = 235
1670 // chunks are 1 or 4 bytes each, so we have to stop at 232
1671 // but make it 225 just to be safe
1673 writePS("~> put\n");
1675 writePSFmt("dup %d <~", line);
1678 } while (c != '~' && c != EOF);
1679 writePS("~> put\n");
1685 void PSOutputDev::startPage(int pageNum, GfxState *state) {
1686 int x1, y1, x2, y2, width, height, t;
1692 writePSFmt("%%%%Page: %d %d\n", pageNum, seqPage);
1693 writePS("%%BeginPageSetup\n");
1695 // rotate, translate, and scale page
1696 x1 = (int)(state->getX1() + 0.5);
1697 y1 = (int)(state->getY1() + 0.5);
1698 x2 = (int)(state->getX2() + 0.5);
1699 y2 = (int)(state->getY2() + 0.5);
1702 if (width > height && width > paperWidth) {
1704 writePSFmt("%%%%PageOrientation: %s\n",
1705 state->getCTM()[0] ? "Landscape" : "Portrait");
1706 writePS("pdfStartPage\n");
1707 writePS("90 rotate\n");
1709 ty = -(y1 + paperWidth);
1715 writePSFmt("%%%%PageOrientation: %s\n",
1716 state->getCTM()[0] ? "Portrait" : "Landscape");
1717 writePS("pdfStartPage\n");
1721 if (width < paperWidth) {
1722 tx += (paperWidth - width) / 2;
1724 if (height < paperHeight) {
1725 ty += (paperHeight - height) / 2;
1727 if (tx != 0 || ty != 0) {
1728 writePSFmt("%g %g translate\n", tx, ty);
1730 if (width > paperWidth || height > paperHeight) {
1731 xScale = (double)paperWidth / (double)width;
1732 yScale = (double)paperHeight / (double)height;
1733 if (yScale < xScale) {
1738 writePSFmt("%0.4f %0.4f scale\n", xScale, xScale);
1740 xScale = yScale = 1;
1743 writePS("%%EndPageSetup\n");
1748 writePS("pdfStartPage\n");
1750 xScale = yScale = 1;
1755 writePS("/PaintProc {\n");
1756 writePS("begin xpdf begin\n");
1757 writePS("pdfStartPage\n");
1759 xScale = yScale = 1;
1765 void PSOutputDev::endPage() {
1767 if (mode == psModeForm) {
1768 writePS("pdfEndPage\n");
1769 writePS("end end\n");
1771 writePS("end end\n");
1773 writePS("showpage\n");
1774 writePS("%%PageTrailer\n");
1775 writePS("pdfEndPage\n");
1779 void PSOutputDev::saveState(GfxState *state) {
1783 void PSOutputDev::restoreState(GfxState *state) {
1787 void PSOutputDev::updateCTM(GfxState *state, double m11, double m12,
1788 double m21, double m22, double m31, double m32) {
1789 writePSFmt("[%g %g %g %g %g %g] cm\n", m11, m12, m21, m22, m31, m32);
1792 void PSOutputDev::updateLineDash(GfxState *state) {
1797 state->getLineDash(&dash, &length, &start);
1799 for (i = 0; i < length; ++i)
1800 writePSFmt("%g%s", dash[i], (i == length-1) ? "" : " ");
1801 writePSFmt("] %g d\n", start);
1804 void PSOutputDev::updateFlatness(GfxState *state) {
1805 writePSFmt("%d i\n", state->getFlatness());
1808 void PSOutputDev::updateLineJoin(GfxState *state) {
1809 writePSFmt("%d j\n", state->getLineJoin());
1812 void PSOutputDev::updateLineCap(GfxState *state) {
1813 writePSFmt("%d J\n", state->getLineCap());
1816 void PSOutputDev::updateMiterLimit(GfxState *state) {
1817 writePSFmt("%g M\n", state->getMiterLimit());
1820 void PSOutputDev::updateLineWidth(GfxState *state) {
1821 writePSFmt("%g w\n", state->getLineWidth());
1824 void PSOutputDev::updateFillColor(GfxState *state) {
1829 GfxSeparationColorSpace *sepCS;
1833 state->getFillGray(&gray);
1834 writePSFmt("%g g\n", gray);
1837 state->getFillCMYK(&cmyk);
1838 writePSFmt("%g %g %g %g k\n", cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1839 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1843 if (state->getFillColorSpace()->getMode() == csDeviceCMYK) {
1844 state->getFillCMYK(&cmyk);
1845 writePSFmt("%g %g %g %g k\n", cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1847 state->getFillRGB(&rgb);
1848 if (rgb.r == rgb.g && rgb.g == rgb.b) {
1849 writePSFmt("%g g\n", rgb.r);
1851 writePSFmt("%g %g %g rg\n", rgb.r, rgb.g, rgb.b);
1857 if (state->getFillColorSpace()->getMode() == csSeparation) {
1858 sepCS = (GfxSeparationColorSpace *)state->getFillColorSpace();
1860 sepCS->getCMYK(&color, &cmyk);
1861 writePSFmt("%g %g %g %g %g (%s) ck\n",
1862 state->getFillColor()->c[0],
1863 cmyk.c, cmyk.m, cmyk.y, cmyk.k,
1864 sepCS->getName()->getCString());
1865 addCustomColor(sepCS);
1867 state->getFillCMYK(&cmyk);
1868 writePSFmt("%g %g %g %g k\n", cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1869 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1873 t3Cacheable = gFalse;
1876 void PSOutputDev::updateStrokeColor(GfxState *state) {
1881 GfxSeparationColorSpace *sepCS;
1885 state->getStrokeGray(&gray);
1886 writePSFmt("%g G\n", gray);
1889 state->getStrokeCMYK(&cmyk);
1890 writePSFmt("%g %g %g %g K\n", cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1891 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1895 if (state->getStrokeColorSpace()->getMode() == csDeviceCMYK) {
1896 state->getStrokeCMYK(&cmyk);
1897 writePSFmt("%g %g %g %g K\n", cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1899 state->getStrokeRGB(&rgb);
1900 if (rgb.r == rgb.g && rgb.g == rgb.b) {
1901 writePSFmt("%g G\n", rgb.r);
1903 writePSFmt("%g %g %g RG\n", rgb.r, rgb.g, rgb.b);
1909 if (state->getStrokeColorSpace()->getMode() == csSeparation) {
1910 sepCS = (GfxSeparationColorSpace *)state->getStrokeColorSpace();
1912 sepCS->getCMYK(&color, &cmyk);
1913 writePSFmt("%g %g %g %g %g (%s) CK\n",
1914 state->getStrokeColor()->c[0],
1915 cmyk.c, cmyk.m, cmyk.y, cmyk.k,
1916 sepCS->getName()->getCString());
1917 addCustomColor(sepCS);
1919 state->getStrokeCMYK(&cmyk);
1920 writePSFmt("%g %g %g %g K\n", cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1921 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
1925 t3Cacheable = gFalse;
1928 void PSOutputDev::addProcessColor(double c, double m, double y, double k) {
1930 processColors |= psProcessCyan;
1933 processColors |= psProcessMagenta;
1936 processColors |= psProcessYellow;
1939 processColors |= psProcessBlack;
1943 void PSOutputDev::addCustomColor(GfxSeparationColorSpace *sepCS) {
1944 PSOutCustomColor *cc;
1948 for (cc = customColors; cc; cc = cc->next) {
1949 if (!cc->name->cmp(sepCS->getName())) {
1954 sepCS->getCMYK(&color, &cmyk);
1955 cc = new PSOutCustomColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k,
1956 sepCS->getName()->copy());
1957 cc->next = customColors;
1961 void PSOutputDev::updateFont(GfxState *state) {
1962 if (state->getFont()) {
1963 writePSFmt("/F%d_%d %g Tf\n",
1964 state->getFont()->getID()->num, state->getFont()->getID()->gen,
1965 state->getFontSize());
1969 void PSOutputDev::updateTextMat(GfxState *state) {
1972 mat = state->getTextMat();
1973 writePSFmt("[%g %g %g %g %g %g] Tm\n",
1974 mat[0], mat[1], mat[2], mat[3], mat[4], mat[5]);
1977 void PSOutputDev::updateCharSpace(GfxState *state) {
1978 writePSFmt("%g Tc\n", state->getCharSpace());
1981 void PSOutputDev::updateRender(GfxState *state) {
1984 rm = state->getRender();
1985 writePSFmt("%d Tr\n", rm);
1987 if (rm != 0 && rm != 3) {
1988 t3Cacheable = gFalse;
1992 void PSOutputDev::updateRise(GfxState *state) {
1993 writePSFmt("%g Ts\n", state->getRise());
1996 void PSOutputDev::updateWordSpace(GfxState *state) {
1997 writePSFmt("%g Tw\n", state->getWordSpace());
2000 void PSOutputDev::updateHorizScaling(GfxState *state) {
2001 writePSFmt("%g Tz\n", state->getHorizScaling());
2004 void PSOutputDev::updateTextPos(GfxState *state) {
2005 writePSFmt("%g %g Td\n", state->getLineX(), state->getLineY());
2008 void PSOutputDev::updateTextShift(GfxState *state, double shift) {
2009 if (state->getFont()->getWMode()) {
2010 writePSFmt("%g TJmV\n", shift);
2012 writePSFmt("%g TJm\n", shift);
2016 void PSOutputDev::stroke(GfxState *state) {
2017 doPath(state->getPath());
2019 // if we're construct a cacheable Type 3 glyph, we need to do
2020 // everything in the fill color
2027 void PSOutputDev::fill(GfxState *state) {
2028 doPath(state->getPath());
2032 void PSOutputDev::eoFill(GfxState *state) {
2033 doPath(state->getPath());
2037 void PSOutputDev::clip(GfxState *state) {
2038 doPath(state->getPath());
2042 void PSOutputDev::eoClip(GfxState *state) {
2043 doPath(state->getPath());
2047 void PSOutputDev::doPath(GfxPath *path) {
2048 GfxSubpath *subpath;
2049 double x0, y0, x1, y1, x2, y2, x3, y3, x4, y4;
2052 n = path->getNumSubpaths();
2054 if (n == 1 && path->getSubpath(0)->getNumPoints() == 5) {
2055 subpath = path->getSubpath(0);
2056 x0 = subpath->getX(0);
2057 y0 = subpath->getY(0);
2058 x4 = subpath->getX(4);
2059 y4 = subpath->getY(4);
2060 if (x4 == x0 && y4 == y0) {
2061 x1 = subpath->getX(1);
2062 y1 = subpath->getY(1);
2063 x2 = subpath->getX(2);
2064 y2 = subpath->getY(2);
2065 x3 = subpath->getX(3);
2066 y3 = subpath->getY(3);
2067 if (x0 == x1 && x2 == x3 && y0 == y3 && y1 == y2) {
2068 writePSFmt("%g %g %g %g re\n",
2069 x0 < x2 ? x0 : x2, y0 < y1 ? y0 : y1,
2070 fabs(x2 - x0), fabs(y1 - y0));
2072 } else if (x0 == x3 && x1 == x2 && y0 == y1 && y2 == y3) {
2073 writePSFmt("%g %g %g %g re\n",
2074 x0 < x1 ? x0 : x1, y0 < y2 ? y0 : y2,
2075 fabs(x1 - x0), fabs(y2 - y0));
2081 for (i = 0; i < n; ++i) {
2082 subpath = path->getSubpath(i);
2083 m = subpath->getNumPoints();
2084 writePSFmt("%g %g m\n", subpath->getX(0), subpath->getY(0));
2087 if (subpath->getCurve(j)) {
2088 writePSFmt("%g %g %g %g %g %g c\n", subpath->getX(j), subpath->getY(j),
2089 subpath->getX(j+1), subpath->getY(j+1),
2090 subpath->getX(j+2), subpath->getY(j+2));
2093 writePSFmt("%g %g l\n", subpath->getX(j), subpath->getY(j));
2097 if (subpath->isClosed()) {
2103 void PSOutputDev::drawString(GfxState *state, GString *s) {
2107 double dx, dy, dx2, dy2, originX, originY;
2113 int len, nChars, uLen, n, m, i, j;
2115 // check for invisible text -- this is used by Acrobat Capture
2116 if ((state->getRender() & 3) == 3) {
2120 // ignore empty strings
2121 if (s->getLength() == 0) {
2126 if (!(font = state->getFont())) {
2129 wMode = font->getWMode();
2131 // check for a subtitute 16-bit font
2133 if (font->isCIDFont()) {
2134 for (i = 0; i < font16EncLen; ++i) {
2135 if (font->getID()->num == font16Enc[i].fontID.num &&
2136 font->getID()->gen == font16Enc[i].fontID.gen) {
2137 uMap = globalParams->getUnicodeMap(font16Enc[i].enc);
2143 // compute width of chars in string, ignoring char spacing and word
2144 // spacing -- the Tj operator will adjust for the metrics of the
2145 // font that's actually used
2148 p = s->getCString();
2149 len = s->getLength();
2150 if (font->isCIDFont()) {
2156 n = font->getNextChar(p, len, &code,
2157 u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
2158 &dx2, &dy2, &originX, &originY);
2159 if (font->isCIDFont()) {
2161 for (i = 0; i < uLen; ++i) {
2162 m = uMap->mapUnicode(u[i], buf, (int)sizeof(buf));
2163 for (j = 0; j < m; ++j) {
2167 //~ this really needs to get the number of chars in the target
2168 //~ encoding - which may be more than the number of Unicode
2172 s2->append((char)((code >> 8) & 0xff));
2173 s2->append((char)(code & 0xff));
2182 dx *= state->getFontSize() * state->getHorizScaling();
2183 dy *= state->getFontSize();
2188 if (s2->getLength() > 0) {
2190 if (font->isCIDFont()) {
2192 writePSFmt(" %d %g Tj16V\n", nChars, dy);
2194 writePSFmt(" %d %g Tj16\n", nChars, dx);
2197 writePSFmt(" %g Tj\n", dx);
2200 if (font->isCIDFont()) {
2205 void PSOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
2206 int width, int height, GBool invert,
2210 len = height * ((width + 7) / 8);
2211 if (level == psLevel1 || level == psLevel1Sep) {
2212 doImageL1(NULL, invert, inlineImg, str, width, height, len);
2214 doImageL2(ref, NULL, invert, inlineImg, str, width, height, len);
2218 void PSOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
2219 int width, int height, GfxImageColorMap *colorMap,
2220 int *maskColors, GBool inlineImg) {
2223 len = height * ((width * colorMap->getNumPixelComps() *
2224 colorMap->getBits() + 7) / 8);
2227 doImageL1(colorMap, gFalse, inlineImg, str, width, height, len);
2230 //~ handle indexed, separation, ... color spaces
2231 doImageL1Sep(colorMap, gFalse, inlineImg, str, width, height, len);
2237 doImageL2(ref, colorMap, gFalse, inlineImg, str, width, height, len);
2240 t3Cacheable = gFalse;
2243 void PSOutputDev::doImageL1(GfxImageColorMap *colorMap,
2244 GBool invert, GBool inlineImg,
2245 Stream *str, int width, int height, int len) {
2246 ImageStream *imgStr;
2247 Guchar pixBuf[gfxColorMaxComps];
2251 // width, height, matrix, bits per component
2253 writePSFmt("%d %d 8 [%d 0 0 %d 0 %d] pdfIm1\n",
2255 width, -height, height);
2257 writePSFmt("%d %d %s [%d 0 0 %d 0 %d] pdfImM1\n",
2258 width, height, invert ? "true" : "false",
2259 width, -height, height);
2265 // set up to process the data stream
2266 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
2267 colorMap->getBits());
2270 // process the data stream
2272 for (y = 0; y < height; ++y) {
2275 for (x = 0; x < width; ++x) {
2276 imgStr->getPixel(pixBuf);
2277 colorMap->getGray(pixBuf, &gray);
2278 writePSFmt("%02x", (int)(gray * 255 + 0.5));
2294 for (y = 0; y < height; ++y) {
2295 for (x = 0; x < width; x += 8) {
2296 writePSFmt("%02x", str->getChar() & 0xff);
2310 void PSOutputDev::doImageL1Sep(GfxImageColorMap *colorMap,
2311 GBool invert, GBool inlineImg,
2312 Stream *str, int width, int height, int len) {
2313 ImageStream *imgStr;
2315 Guchar pixBuf[gfxColorMaxComps];
2319 // width, height, matrix, bits per component
2320 writePSFmt("%d %d 8 [%d 0 0 %d 0 %d] pdfIm1Sep\n",
2322 width, -height, height);
2324 // allocate a line buffer
2325 lineBuf = (Guchar *)gmalloc(4 * width);
2327 // set up to process the data stream
2328 imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
2329 colorMap->getBits());
2332 // process the data stream
2334 for (y = 0; y < height; ++y) {
2337 for (x = 0; x < width; ++x) {
2338 imgStr->getPixel(pixBuf);
2339 colorMap->getCMYK(pixBuf, &cmyk);
2340 lineBuf[4*x+0] = (int)(255 * cmyk.c + 0.5);
2341 lineBuf[4*x+1] = (int)(255 * cmyk.m + 0.5);
2342 lineBuf[4*x+2] = (int)(255 * cmyk.y + 0.5);
2343 lineBuf[4*x+3] = (int)(255 * cmyk.k + 0.5);
2344 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
2347 // write one line of each color component
2348 for (comp = 0; comp < 4; ++comp) {
2349 for (x = 0; x < width; ++x) {
2350 writePSFmt("%02x", lineBuf[4*x + comp]);
2367 void PSOutputDev::doImageL2(Object *ref, GfxImageColorMap *colorMap,
2368 GBool invert, GBool inlineImg,
2369 Stream *str, int width, int height, int len) {
2372 GBool useRLE, useASCII, useCompressed;
2373 GfxSeparationColorSpace *sepCS;
2381 dumpColorSpaceL2(colorMap->getColorSpace());
2382 writePS(" setcolorspace\n");
2385 // set up the image data
2386 if (mode == psModeForm || inType3Char) {
2389 str = new FixedLengthEncoder(str, len);
2390 if (globalParams->getPSASCIIHex()) {
2391 str = new ASCIIHexEncoder(str);
2393 str = new ASCII85Encoder(str);
2401 } while (c == '\n' || c == '\r');
2402 if (c == '~' || c == EOF) {
2411 for (i = 1; i <= 4; ++i) {
2414 } while (c == '\n' || c == '\r');
2415 if (c == '~' || c == EOF) {
2422 // each line is: "dup nnnnn <~...data...~> put<eol>"
2423 // so max data length = 255 - 20 = 235
2424 // chunks are 1 or 4 bytes each, so we have to stop at 232
2425 // but make it 225 just to be safe
2429 writePSFmt("<~", line);
2432 } while (c != '~' && c != EOF);
2437 // set up to use the array already created by setupImages()
2438 writePSFmt("ImData_%d_%d 0\n", ref->getRefNum(), ref->getRefGen());
2443 writePS("<<\n /ImageType 1\n");
2445 // width, height, matrix, bits per component
2446 writePSFmt(" /Width %d\n", width);
2447 writePSFmt(" /Height %d\n", height);
2448 writePSFmt(" /ImageMatrix [%d 0 0 %d 0 %d]\n", width, -height, height);
2449 writePSFmt(" /BitsPerComponent %d\n",
2450 colorMap ? colorMap->getBits() : 1);
2454 writePS(" /Decode [");
2455 if (colorMap->getColorSpace()->getMode() == csSeparation) {
2456 //~ this is a kludge -- see comment in dumpColorSpaceL2
2457 n = (1 << colorMap->getBits()) - 1;
2458 writePSFmt("%g %g", colorMap->getDecodeLow(0) * n,
2459 colorMap->getDecodeHigh(0) * n);
2461 numComps = colorMap->getNumPixelComps();
2462 for (i = 0; i < numComps; ++i) {
2466 writePSFmt("%g %g", colorMap->getDecodeLow(i),
2467 colorMap->getDecodeHigh(i));
2472 writePSFmt(" /Decode [%d %d]\n", invert ? 1 : 0, invert ? 0 : 1);
2475 if (mode == psModeForm || inType3Char) {
2478 writePS(" /DataSource { 2 copy get exch 1 add exch }\n");
2480 // end of image dictionary
2481 writePSFmt(">>\n%s\n", colorMap ? "image" : "imagemask");
2483 // get rid of the array and index
2484 writePS("pop pop\n");
2489 writePS(" /DataSource currentfile\n");
2490 s = str->getPSFilter(" ");
2491 if (inlineImg || !s) {
2494 useCompressed = gFalse;
2497 useASCII = str->isBinary();
2498 useCompressed = gTrue;
2501 writePSFmt(" /ASCII%sDecode filter\n",
2502 globalParams->getPSASCIIHex() ? "Hex" : "85");
2505 writePS(" /RunLengthDecode filter\n");
2507 if (useCompressed) {
2508 writePS(s->getCString());
2514 // cut off inline image streams at appropriate length
2516 str = new FixedLengthEncoder(str, len);
2517 } else if (useCompressed) {
2518 str = str->getBaseStream();
2521 // add RunLengthEncode and ASCIIHex/85 encode filters
2523 str = new RunLengthEncoder(str);
2526 if (globalParams->getPSASCIIHex()) {
2527 str = new ASCIIHexEncoder(str);
2529 str = new ASCII85Encoder(str);
2533 // end of image dictionary
2538 // this can't happen -- OPI dictionaries are in XObjects
2539 error(-1, "Internal: OPI in inline image");
2542 // need to read the stream to count characters -- the length
2543 // is data-dependent (because of ASCII and RLE filters)
2546 while ((c = str->getChar()) != EOF) {
2551 // +6/7 for "pdfIm\n" / "pdfImM\n"
2552 // +8 for newline + trailer
2553 n += colorMap ? 14 : 15;
2554 writePSFmt("%%%%BeginData: %d Hex Bytes\n", n);
2557 if ((level == psLevel2Sep || level == psLevel3Sep) && colorMap &&
2558 colorMap->getColorSpace()->getMode() == csSeparation) {
2560 sepCS = (GfxSeparationColorSpace *)colorMap->getColorSpace();
2561 sepCS->getCMYK(&color, &cmyk);
2562 writePSFmt("%g %g %g %g (%s) pdfImSep\n",
2563 cmyk.c, cmyk.m, cmyk.y, cmyk.k,
2564 sepCS->getName()->getCString());
2566 writePSFmt("%s\n", colorMap ? "pdfIm" : "pdfImM");
2569 // copy the stream data
2571 while ((c = str->getChar()) != EOF) {
2576 // add newline and trailer to the end
2578 writePS("%-EOD-\n");
2581 writePS("%%EndData\n");
2586 if (useRLE || useASCII || inlineImg) {
2592 void PSOutputDev::dumpColorSpaceL2(GfxColorSpace *colorSpace) {
2593 GfxCalGrayColorSpace *calGrayCS;
2594 GfxCalRGBColorSpace *calRGBCS;
2595 GfxLabColorSpace *labCS;
2596 GfxIndexedColorSpace *indexedCS;
2597 GfxSeparationColorSpace *separationCS;
2598 GfxColorSpace *baseCS;
2600 double x[gfxColorMaxComps], y[gfxColorMaxComps];
2604 int n, numComps, numAltComps;
2608 switch (colorSpace->getMode()) {
2611 writePS("/DeviceGray");
2612 processColors |= psProcessBlack;
2616 calGrayCS = (GfxCalGrayColorSpace *)colorSpace;
2617 writePS("[/CIEBasedA <<\n");
2618 writePSFmt(" /DecodeA {%g exp} bind\n", calGrayCS->getGamma());
2619 writePSFmt(" /MatrixA [%g %g %g]\n",
2620 calGrayCS->getWhiteX(), calGrayCS->getWhiteY(),
2621 calGrayCS->getWhiteZ());
2622 writePSFmt(" /WhitePoint [%g %g %g]\n",
2623 calGrayCS->getWhiteX(), calGrayCS->getWhiteY(),
2624 calGrayCS->getWhiteZ());
2625 writePSFmt(" /BlackPoint [%g %g %g]\n",
2626 calGrayCS->getBlackX(), calGrayCS->getBlackY(),
2627 calGrayCS->getBlackZ());
2629 processColors |= psProcessBlack;
2633 writePS("/DeviceRGB");
2634 processColors |= psProcessCMYK;
2638 calRGBCS = (GfxCalRGBColorSpace *)colorSpace;
2639 writePS("[/CIEBasedABC <<\n");
2640 writePSFmt(" /DecodeABC [{%g exp} bind {%g exp} bind {%g exp} bind]\n",
2641 calRGBCS->getGammaR(), calRGBCS->getGammaG(),
2642 calRGBCS->getGammaB());
2643 writePSFmt(" /MatrixABC [%g %g %g %g %g %g %g %g %g]\n",
2644 calRGBCS->getMatrix()[0], calRGBCS->getMatrix()[1],
2645 calRGBCS->getMatrix()[2], calRGBCS->getMatrix()[3],
2646 calRGBCS->getMatrix()[4], calRGBCS->getMatrix()[5],
2647 calRGBCS->getMatrix()[6], calRGBCS->getMatrix()[7],
2648 calRGBCS->getMatrix()[8]);
2649 writePSFmt(" /WhitePoint [%g %g %g]\n",
2650 calRGBCS->getWhiteX(), calRGBCS->getWhiteY(),
2651 calRGBCS->getWhiteZ());
2652 writePSFmt(" /BlackPoint [%g %g %g]\n",
2653 calRGBCS->getBlackX(), calRGBCS->getBlackY(),
2654 calRGBCS->getBlackZ());
2656 processColors |= psProcessCMYK;
2660 writePS("/DeviceCMYK");
2661 processColors |= psProcessCMYK;
2665 labCS = (GfxLabColorSpace *)colorSpace;
2666 writePS("[/CIEBasedABC <<\n");
2667 writePSFmt(" /RangeABC [0 100 %g %g %g %g]\n",
2668 labCS->getAMin(), labCS->getAMax(),
2669 labCS->getBMin(), labCS->getBMax());
2670 writePS(" /DecodeABC [{16 add 116 div} bind {500 div} bind {200 div} bind]\n");
2671 writePS(" /MatrixABC [1 1 1 1 0 0 0 0 -1]\n");
2672 writePS(" /DecodeLMN\n");
2673 writePS(" [{dup 6 29 div ge {dup dup mul mul}\n");
2674 writePSFmt(" {4 29 div sub 108 841 div mul } ifelse %g mul} bind\n",
2675 labCS->getWhiteX());
2676 writePS(" {dup 6 29 div ge {dup dup mul mul}\n");
2677 writePSFmt(" {4 29 div sub 108 841 div mul } ifelse %g mul} bind\n",
2678 labCS->getWhiteY());
2679 writePS(" {dup 6 29 div ge {dup dup mul mul}\n");
2680 writePSFmt(" {4 29 div sub 108 841 div mul } ifelse %g mul} bind]\n",
2681 labCS->getWhiteZ());
2682 writePSFmt(" /WhitePoint [%g %g %g]\n",
2683 labCS->getWhiteX(), labCS->getWhiteY(), labCS->getWhiteZ());
2684 writePSFmt(" /BlackPoint [%g %g %g]\n",
2685 labCS->getBlackX(), labCS->getBlackY(), labCS->getBlackZ());
2687 processColors |= psProcessCMYK;
2691 dumpColorSpaceL2(((GfxICCBasedColorSpace *)colorSpace)->getAlt());
2695 indexedCS = (GfxIndexedColorSpace *)colorSpace;
2696 baseCS = indexedCS->getBase();
2697 writePS("[/Indexed ");
2698 dumpColorSpaceL2(baseCS);
2699 n = indexedCS->getIndexHigh();
2700 numComps = baseCS->getNComps();
2701 lookup = indexedCS->getLookup();
2702 writePSFmt(" %d <\n", n);
2703 if (baseCS->getMode() == csDeviceN) {
2704 func = ((GfxDeviceNColorSpace *)baseCS)->getTintTransformFunc();
2705 numAltComps = ((GfxDeviceNColorSpace *)baseCS)->getAlt()->getNComps();
2707 for (i = 0; i <= n; i += 8) {
2709 for (j = i; j < i+8 && j <= n; ++j) {
2710 for (k = 0; k < numComps; ++k) {
2711 x[k] = *p++ / 255.0;
2713 func->transform(x, y);
2714 for (k = 0; k < numAltComps; ++k) {
2715 byte = (int)(y[k] * 255 + 0.5);
2718 } else if (byte > 255) {
2721 writePSFmt("%02x", byte);
2724 indexedCS->getCMYK(&color, &cmyk);
2725 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
2730 for (i = 0; i <= n; i += 8) {
2732 for (j = i; j < i+8 && j <= n; ++j) {
2733 for (k = 0; k < numComps; ++k) {
2734 writePSFmt("%02x", lookup[j * numComps + k]);
2737 indexedCS->getCMYK(&color, &cmyk);
2738 addProcessColor(cmyk.c, cmyk.m, cmyk.y, cmyk.k);
2747 //~ this is a kludge -- the correct thing would to ouput a
2748 //~ separation color space, with the specified alternate color
2749 //~ space and tint transform
2750 separationCS = (GfxSeparationColorSpace *)colorSpace;
2751 writePS("[/Indexed ");
2752 dumpColorSpaceL2(separationCS->getAlt());
2753 writePS(" 255 <\n");
2754 numComps = separationCS->getAlt()->getNComps();
2755 for (i = 0; i <= 255; i += 8) {
2757 for (j = i; j < i+8 && j <= 255; ++j) {
2758 x[0] = (double)j / 255.0;
2759 separationCS->getFunc()->transform(x, y);
2760 for (k = 0; k < numComps; ++k) {
2761 writePSFmt("%02x", (int)(255 * y[k] + 0.5));
2767 addCustomColor(separationCS);
2771 // DeviceN color spaces are a Level 3 PostScript feature.
2772 dumpColorSpaceL2(((GfxDeviceNColorSpace *)colorSpace)->getAlt());
2783 void PSOutputDev::opiBegin(GfxState *state, Dict *opiDict) {
2786 if (globalParams->getPSOPI()) {
2787 opiDict->lookup("2.0", &dict);
2788 if (dict.isDict()) {
2789 opiBegin20(state, dict.getDict());
2793 opiDict->lookup("1.3", &dict);
2794 if (dict.isDict()) {
2795 opiBegin13(state, dict.getDict());
2802 void PSOutputDev::opiBegin20(GfxState *state, Dict *dict) {
2803 Object obj1, obj2, obj3, obj4;
2804 double width, height, left, right, top, bottom;
2808 writePS("%%BeginOPI: 2.0\n");
2809 writePS("%%Distilled\n");
2811 dict->lookup("F", &obj1);
2812 if (getFileSpec(&obj1, &obj2)) {
2813 writePSFmt("%%%%ImageFileName: %s\n",
2814 obj2.getString()->getCString());
2819 dict->lookup("MainImage", &obj1);
2820 if (obj1.isString()) {
2821 writePSFmt("%%%%MainImage: %s\n", obj1.getString()->getCString());
2825 //~ ignoring 'Tags' entry
2826 //~ need to use writePSString() and deal with >255-char lines
2828 dict->lookup("Size", &obj1);
2829 if (obj1.isArray() && obj1.arrayGetLength() == 2) {
2830 obj1.arrayGet(0, &obj2);
2831 width = obj2.getNum();
2833 obj1.arrayGet(1, &obj2);
2834 height = obj2.getNum();
2836 writePSFmt("%%%%ImageDimensions: %g %g\n", width, height);
2840 dict->lookup("CropRect", &obj1);
2841 if (obj1.isArray() && obj1.arrayGetLength() == 4) {
2842 obj1.arrayGet(0, &obj2);
2843 left = obj2.getNum();
2845 obj1.arrayGet(1, &obj2);
2846 top = obj2.getNum();
2848 obj1.arrayGet(2, &obj2);
2849 right = obj2.getNum();
2851 obj1.arrayGet(3, &obj2);
2852 bottom = obj2.getNum();
2854 writePSFmt("%%%%ImageCropRect: %g %g %g %g\n", left, top, right, bottom);
2858 dict->lookup("Overprint", &obj1);
2859 if (obj1.isBool()) {
2860 writePSFmt("%%%%ImageOverprint: %s\n", obj1.getBool() ? "true" : "false");
2864 dict->lookup("Inks", &obj1);
2865 if (obj1.isName()) {
2866 writePSFmt("%%%%ImageInks: %s\n", obj1.getName());
2867 } else if (obj1.isArray() && obj1.arrayGetLength() >= 1) {
2868 obj1.arrayGet(0, &obj2);
2869 if (obj2.isName()) {
2870 writePSFmt("%%%%ImageInks: %s %d",
2871 obj2.getName(), (obj1.arrayGetLength() - 1) / 2);
2872 for (i = 1; i+1 < obj1.arrayGetLength(); i += 2) {
2873 obj1.arrayGet(i, &obj3);
2874 obj1.arrayGet(i+1, &obj4);
2875 if (obj3.isString() && obj4.isNum()) {
2877 writePSString(obj3.getString());
2878 writePSFmt(" %g", obj4.getNum());
2891 writePS("%%BeginIncludedImage\n");
2893 dict->lookup("IncludedImageDimensions", &obj1);
2894 if (obj1.isArray() && obj1.arrayGetLength() == 2) {
2895 obj1.arrayGet(0, &obj2);
2898 obj1.arrayGet(1, &obj2);
2901 writePSFmt("%%%%IncludedImageDimensions: %d %d\n", w, h);
2905 dict->lookup("IncludedImageQuality", &obj1);
2907 writePSFmt("%%%%IncludedImageQuality: %g\n", obj1.getNum());
2914 void PSOutputDev::opiBegin13(GfxState *state, Dict *dict) {
2916 int left, right, top, bottom, samples, bits, width, height;
2918 double llx, lly, ulx, uly, urx, ury, lrx, lry;
2919 double tllx, tlly, tulx, tuly, turx, tury, tlrx, tlry;
2924 writePS("/opiMatrix2 matrix currentmatrix def\n");
2925 writePS("opiMatrix setmatrix\n");
2927 dict->lookup("F", &obj1);
2928 if (getFileSpec(&obj1, &obj2)) {
2929 writePSFmt("%%ALDImageFileName: %s\n",
2930 obj2.getString()->getCString());
2935 dict->lookup("CropRect", &obj1);
2936 if (obj1.isArray() && obj1.arrayGetLength() == 4) {
2937 obj1.arrayGet(0, &obj2);
2938 left = obj2.getInt();
2940 obj1.arrayGet(1, &obj2);
2941 top = obj2.getInt();
2943 obj1.arrayGet(2, &obj2);
2944 right = obj2.getInt();
2946 obj1.arrayGet(3, &obj2);
2947 bottom = obj2.getInt();
2949 writePSFmt("%%ALDImageCropRect: %d %d %d %d\n", left, top, right, bottom);
2953 dict->lookup("Color", &obj1);
2954 if (obj1.isArray() && obj1.arrayGetLength() == 5) {
2955 obj1.arrayGet(0, &obj2);
2958 obj1.arrayGet(1, &obj2);
2961 obj1.arrayGet(2, &obj2);
2964 obj1.arrayGet(3, &obj2);
2967 obj1.arrayGet(4, &obj2);
2968 if (obj2.isString()) {
2969 writePSFmt("%%ALDImageColor: %g %g %g %g ", c, m, y, k);
2970 writePSString(obj2.getString());
2977 dict->lookup("ColorType", &obj1);
2978 if (obj1.isName()) {
2979 writePSFmt("%%ALDImageColorType: %s\n", obj1.getName());
2983 //~ ignores 'Comments' entry
2984 //~ need to handle multiple lines
2986 dict->lookup("CropFixed", &obj1);
2987 if (obj1.isArray()) {
2988 obj1.arrayGet(0, &obj2);
2989 ulx = obj2.getNum();
2991 obj1.arrayGet(1, &obj2);
2992 uly = obj2.getNum();
2994 obj1.arrayGet(2, &obj2);
2995 lrx = obj2.getNum();
2997 obj1.arrayGet(3, &obj2);
2998 lry = obj2.getNum();
3000 writePSFmt("%%ALDImageCropFixed: %g %g %g %g\n", ulx, uly, lrx, lry);
3004 dict->lookup("GrayMap", &obj1);
3005 if (obj1.isArray()) {
3006 writePS("%ALDImageGrayMap:");
3007 for (i = 0; i < obj1.arrayGetLength(); i += 16) {
3011 for (j = 0; j < 16 && i+j < obj1.arrayGetLength(); ++j) {
3012 obj1.arrayGet(i+j, &obj2);
3013 writePSFmt(" %d", obj2.getInt());
3021 dict->lookup("ID", &obj1);
3022 if (obj1.isString()) {
3023 writePSFmt("%%ALDImageID: %s\n", obj1.getString()->getCString());
3027 dict->lookup("ImageType", &obj1);
3028 if (obj1.isArray() && obj1.arrayGetLength() == 2) {
3029 obj1.arrayGet(0, &obj2);
3030 samples = obj2.getInt();
3032 obj1.arrayGet(1, &obj2);
3033 bits = obj2.getInt();
3035 writePSFmt("%%ALDImageType: %d %d\n", samples, bits);
3039 dict->lookup("Overprint", &obj1);
3040 if (obj1.isBool()) {
3041 writePSFmt("%%ALDImageOverprint: %s\n", obj1.getBool() ? "true" : "false");
3045 dict->lookup("Position", &obj1);
3046 if (obj1.isArray() && obj1.arrayGetLength() == 8) {
3047 obj1.arrayGet(0, &obj2);
3048 llx = obj2.getNum();
3050 obj1.arrayGet(1, &obj2);
3051 lly = obj2.getNum();
3053 obj1.arrayGet(2, &obj2);
3054 ulx = obj2.getNum();
3056 obj1.arrayGet(3, &obj2);
3057 uly = obj2.getNum();
3059 obj1.arrayGet(4, &obj2);
3060 urx = obj2.getNum();
3062 obj1.arrayGet(5, &obj2);
3063 ury = obj2.getNum();
3065 obj1.arrayGet(6, &obj2);
3066 lrx = obj2.getNum();
3068 obj1.arrayGet(7, &obj2);
3069 lry = obj2.getNum();
3071 opiTransform(state, llx, lly, &tllx, &tlly);
3072 opiTransform(state, ulx, uly, &tulx, &tuly);
3073 opiTransform(state, urx, ury, &turx, &tury);
3074 opiTransform(state, lrx, lry, &tlrx, &tlry);
3075 writePSFmt("%%ALDImagePosition: %g %g %g %g %g %g %g %g\n",
3076 tllx, tlly, tulx, tuly, turx, tury, tlrx, tlry);
3081 dict->lookup("Resolution", &obj1);
3082 if (obj1.isArray() && obj1.arrayGetLength() == 2) {
3083 obj1.arrayGet(0, &obj2);
3084 horiz = obj2.getNum();
3086 obj1.arrayGet(1, &obj2);
3087 vert = obj2.getNum();
3089 writePSFmt("%%ALDImageResoution: %g %g\n", horiz, vert);
3094 dict->lookup("Size", &obj1);
3095 if (obj1.isArray() && obj1.arrayGetLength() == 2) {
3096 obj1.arrayGet(0, &obj2);
3097 width = obj2.getInt();
3099 obj1.arrayGet(1, &obj2);
3100 height = obj2.getInt();
3102 writePSFmt("%%ALDImageDimensions: %d %d\n", width, height);
3106 //~ ignoring 'Tags' entry
3107 //~ need to use writePSString() and deal with >255-char lines
3109 dict->lookup("Tint", &obj1);
3111 writePSFmt("%%ALDImageTint: %g\n", obj1.getNum());
3115 dict->lookup("Transparency", &obj1);
3116 if (obj1.isBool()) {
3117 writePSFmt("%%ALDImageTransparency: %s\n", obj1.getBool() ? "true" : "false");
3121 writePS("%%BeginObject: image\n");
3122 writePS("opiMatrix2 setmatrix\n");
3126 // Convert PDF user space coordinates to PostScript default user space
3127 // coordinates. This has to account for both the PDF CTM and the
3128 // PSOutputDev page-fitting transform.
3129 void PSOutputDev::opiTransform(GfxState *state, double x0, double y0,
3130 double *x1, double *y1) {
3133 state->transform(x0, y0, x1, y1);
3145 void PSOutputDev::opiEnd(GfxState *state, Dict *opiDict) {
3148 if (globalParams->getPSOPI()) {
3149 opiDict->lookup("2.0", &dict);
3150 if (dict.isDict()) {
3151 writePS("%%EndIncludedImage\n");
3152 writePS("%%EndOPI\n");
3153 writePS("grestore\n");
3158 opiDict->lookup("1.3", &dict);
3159 if (dict.isDict()) {
3160 writePS("%%EndObject\n");
3161 writePS("restore\n");
3169 GBool PSOutputDev::getFileSpec(Object *fileSpec, Object *fileName) {
3170 if (fileSpec->isString()) {
3171 fileSpec->copy(fileName);
3174 if (fileSpec->isDict()) {
3175 fileSpec->dictLookup("DOS", fileName);
3176 if (fileName->isString()) {
3180 fileSpec->dictLookup("Mac", fileName);
3181 if (fileName->isString()) {
3185 fileSpec->dictLookup("Unix", fileName);
3186 if (fileName->isString()) {
3190 fileSpec->dictLookup("F", fileName);
3191 if (fileName->isString()) {
3198 #endif // OPI_SUPPORT
3200 void PSOutputDev::type3D0(GfxState *state, double wx, double wy) {
3201 writePSFmt("%g %g setcharwidth\n", wx, wy);
3205 void PSOutputDev::type3D1(GfxState *state, double wx, double wy,
3206 double llx, double lly, double urx, double ury) {
3213 t3String = new GString();
3215 t3Cacheable = gTrue;
3218 void PSOutputDev::psXObject(Stream *psStream, Stream *level1Stream) {
3222 if ((level == psLevel1 || level == psLevel1Sep) && level1Stream) {
3228 while ((c = str->getChar()) != EOF) {
3234 void PSOutputDev::writePSChar(char c) {
3236 t3String->append(c);
3238 (*outputFunc)(outputStream, &c, 1);
3242 void PSOutputDev::writePS(char *s) {
3244 t3String->append(s);
3246 (*outputFunc)(outputStream, s, strlen(s));
3250 void PSOutputDev::writePSFmt(const char *fmt, ...) {
3254 va_start(args, fmt);
3255 vsprintf(buf, fmt, args);
3258 t3String->append(buf);
3260 (*outputFunc)(outputStream, buf, strlen(buf));
3264 void PSOutputDev::writePSString(GString *s) {
3270 for (p = (Guchar *)s->getCString(), n = s->getLength(); n; ++p, --n) {
3271 if (*p == '(' || *p == ')' || *p == '\\') {
3273 writePSChar((char)*p);
3274 } else if (*p < 0x20 || *p >= 0x80) {
3275 sprintf(buf, "\\%03o", *p);
3277 t3String->append(buf);
3279 (*outputFunc)(outputStream, buf, strlen(buf));
3282 writePSChar((char)*p);
3288 void PSOutputDev::writePSName(char *s) {
3293 while ((c = *p++)) {
3294 if (c <= (char)0x20 || c >= (char)0x7f ||
3295 c == '(' || c == ')' || c == '<' || c == '>' ||
3296 c == '[' || c == ']' || c == '{' || c == '}' ||
3297 c == '/' || c == '%') {
3298 writePSFmt("#%02x", c & 0xff);
3305 GString *PSOutputDev::filterPSName(GString *name) {
3311 name2 = new GString();
3312 for (i = 0; i < name->getLength(); ++i) {
3313 c = name->getChar(i);
3314 if (c <= (char)0x20 || c >= (char)0x7f ||
3315 c == '(' || c == ')' || c == '<' || c == '>' ||
3316 c == '[' || c == ']' || c == '{' || c == '}' ||
3317 c == '/' || c == '%') {
3318 sprintf(buf, "#%02x", c & 0xff);