1 //========================================================================
2 //
3 // PSOutputDev.h
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 #ifndef PSOUTPUTDEV_H
10 #define PSOUTPUTDEV_H
11 
12 #include <aconf.h>
13 
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17 
18 #include <stddef.h>
19 #include "config.h"
20 #include "Object.h"
21 #include "GlobalParams.h"
22 #include "OutputDev.h"
23 
24 class GHash;
25 class PDFDoc;
26 class XRef;
27 class Function;
28 class GfxPath;
29 class GfxFont;
30 class GfxColorSpace;
31 class GfxSeparationColorSpace;
32 class PDFRectangle;
33 class PSOutCustomColor;
34 class PSOutputDev;
35 class PSFontFileInfo;
36 
37 //------------------------------------------------------------------------
38 // PSOutputDev
39 //------------------------------------------------------------------------
40 
41 enum PSOutMode {
42   psModePS,
43   psModeEPS,
44   psModeForm
45 };
46 
47 enum PSFileType {
48   psFile,			// write to file
49   psPipe,			// write to pipe
50   psStdout,			// write to stdout
51   psGeneric			// write to a generic stream
52 };
53 
54 enum PSOutCustomCodeLocation {
55   psOutCustomDocSetup,
56   psOutCustomPageSetup
57 };
58 
59 typedef void (*PSOutputFunc)(void *stream, const char *data, int len);
60 
61 typedef GString *(*PSOutCustomCodeCbk)(PSOutputDev *psOut,
62 				       PSOutCustomCodeLocation loc, int n,
63 				       void *data);
64 
65 class PSOutputDev: public OutputDev {
66 public:
67 
68   // Open a PostScript output file, and write the prolog.
69   PSOutputDev(char *fileName, PDFDoc *docA,
70 	      int firstPage, int lastPage, PSOutMode modeA,
71 	      int imgLLXA = 0, int imgLLYA = 0,
72 	      int imgURXA = 0, int imgURYA = 0,
73 	      GBool manualCtrlA = gFalse,
74 	      PSOutCustomCodeCbk customCodeCbkA = NULL,
75 	      void *customCodeCbkDataA = NULL);
76 
77   // Open a PSOutputDev that will write to a generic stream.
78   PSOutputDev(PSOutputFunc outputFuncA, void *outputStreamA,
79 	      PDFDoc *docA,
80 	      int firstPage, int lastPage, PSOutMode modeA,
81 	      int imgLLXA = 0, int imgLLYA = 0,
82 	      int imgURXA = 0, int imgURYA = 0,
83 	      GBool manualCtrlA = gFalse,
84 	      PSOutCustomCodeCbk customCodeCbkA = NULL,
85 	      void *customCodeCbkDataA = NULL);
86 
87   // Destructor -- writes the trailer and closes the file.
88   virtual ~PSOutputDev();
89 
90   // Check if file was successfully created.
isOk()91   virtual GBool isOk() { return ok; }
92 
93   // Returns false if there have been any errors on the output stream.
94   GBool checkIO();
95 
96   //---- get info about output device
97 
98   // Does this device use upside-down coordinates?
99   // (Upside-down means (0,0) is the top left corner of the page.)
upsideDown()100   virtual GBool upsideDown() { return gFalse; }
101 
102   // Does this device use drawChar() or drawString()?
useDrawChar()103   virtual GBool useDrawChar() { return gFalse; }
104 
105   // Does this device use tilingPatternFill()?  If this returns false,
106   // tiling pattern fills will be reduced to a series of other drawing
107   // operations.
useTilingPatternFill()108   virtual GBool useTilingPatternFill() { return gTrue; }
109 
110   // Does this device use functionShadedFill(), axialShadedFill(), and
111   // radialShadedFill()?  If this returns false, these shaded fills
112   // will be reduced to a series of other drawing operations.
useShadedFills()113   virtual GBool useShadedFills()
114     { return level >= psLevel2; }
115 
116   // Does this device use drawForm()?  If this returns false,
117   // form-type XObjects will be interpreted (i.e., unrolled).
useDrawForm()118   virtual GBool useDrawForm() { return preload; }
119 
120   // Does this device use beginType3Char/endType3Char?  Otherwise,
121   // text in Type 3 fonts will be drawn with drawChar/drawString.
interpretType3Chars()122   virtual GBool interpretType3Chars() { return gFalse; }
123 
124   //----- header/trailer (used only if manualCtrl is true)
125 
126   // Write the document-level header.
127   void writeHeader(int firstPage, int lastPage,
128 		   PDFRectangle *mediaBox, PDFRectangle *cropBox,
129 		   int pageRotate);
130 
131   // Write the Xpdf procset.
132   void writeXpdfProcset();
133 
134   // Write the document-level setup.
135   void writeDocSetup(Catalog *catalog, int firstPage, int lastPage);
136 
137   // Write the trailer for the current page.
138   void writePageTrailer();
139 
140   // Write the document trailer.
141   void writeTrailer();
142 
143   //----- initialization and control
144 
145   // Check to see if a page slice should be displayed.  If this
146   // returns false, the page display is aborted.  Typically, an
147   // OutputDev will use some alternate means to display the page
148   // before returning false.
149   virtual GBool checkPageSlice(Page *page, double hDPI, double vDPI,
150 			       int rotate, GBool useMediaBox, GBool crop,
151 			       int sliceX, int sliceY, int sliceW, int sliceH,
152 			       GBool printing,
153 			       GBool (*abortCheckCbk)(void *data) = NULL,
154 			       void *abortCheckCbkData = NULL);
155 
156   // Start a page.
157   virtual void startPage(int pageNum, GfxState *state);
158 
159   // End a page.
160   virtual void endPage();
161 
162   //----- save/restore graphics state
163   virtual void saveState(GfxState *state);
164   virtual void restoreState(GfxState *state);
165 
166   //----- update graphics state
167   virtual void updateCTM(GfxState *state, double m11, double m12,
168 			 double m21, double m22, double m31, double m32);
169   virtual void updateLineDash(GfxState *state);
170   virtual void updateFlatness(GfxState *state);
171   virtual void updateLineJoin(GfxState *state);
172   virtual void updateLineCap(GfxState *state);
173   virtual void updateMiterLimit(GfxState *state);
174   virtual void updateLineWidth(GfxState *state);
175   virtual void updateFillColorSpace(GfxState *state);
176   virtual void updateStrokeColorSpace(GfxState *state);
177   virtual void updateFillColor(GfxState *state);
178   virtual void updateStrokeColor(GfxState *state);
179   virtual void updateFillOverprint(GfxState *state);
180   virtual void updateStrokeOverprint(GfxState *state);
181   virtual void updateTransfer(GfxState *state);
182 
183   //----- update text state
184   virtual void updateFont(GfxState *state);
185   virtual void updateTextMat(GfxState *state);
186   virtual void updateCharSpace(GfxState *state);
187   virtual void updateRender(GfxState *state);
188   virtual void updateRise(GfxState *state);
189   virtual void updateWordSpace(GfxState *state);
190   virtual void updateHorizScaling(GfxState *state);
191   virtual void updateTextPos(GfxState *state);
192   virtual void updateTextShift(GfxState *state, double shift);
193   virtual void saveTextPos(GfxState *state);
194   virtual void restoreTextPos(GfxState *state);
195 
196   //----- path painting
197   virtual void stroke(GfxState *state);
198   virtual void fill(GfxState *state);
199   virtual void eoFill(GfxState *state);
200   virtual void tilingPatternFill(GfxState *state, Gfx *gfx, Object *strRef,
201 				 int paintType, Dict *resDict,
202 				 double *mat, double *bbox,
203 				 int x0, int y0, int x1, int y1,
204 				 double xStep, double yStep);
205   virtual GBool functionShadedFill(GfxState *state,
206 				   GfxFunctionShading *shading);
207   virtual GBool axialShadedFill(GfxState *state, GfxAxialShading *shading);
208   virtual GBool radialShadedFill(GfxState *state, GfxRadialShading *shading);
209 
210   //----- path clipping
211   virtual void clip(GfxState *state);
212   virtual void eoClip(GfxState *state);
213   virtual void clipToStrokePath(GfxState *state);
214 
215   //----- text drawing
216   virtual void drawString(GfxState *state, GString *s);
217   virtual void endTextObject(GfxState *state);
218 
219   //----- image drawing
220   virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
221 			     int width, int height, GBool invert,
222 			     GBool inlineImg, GBool interpolate);
223   virtual void drawImage(GfxState *state, Object *ref, Stream *str,
224 			 int width, int height, GfxImageColorMap *colorMap,
225 			 int *maskColors, GBool inlineImg, GBool interpolate);
226   virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str,
227 			       int width, int height,
228 			       GfxImageColorMap *colorMap,
229 			       Stream *maskStr, int maskWidth, int maskHeight,
230 			       GBool maskInvert, GBool interpolate);
231 
232 #if OPI_SUPPORT
233   //----- OPI functions
234   virtual void opiBegin(GfxState *state, Dict *opiDict);
235   virtual void opiEnd(GfxState *state, Dict *opiDict);
236 #endif
237 
238   //----- Type 3 font operators
239   virtual void type3D0(GfxState *state, double wx, double wy);
240   virtual void type3D1(GfxState *state, double wx, double wy,
241 		       double llx, double lly, double urx, double ury);
242 
243   //----- form XObjects
244   virtual void drawForm(Ref ref);
245 
246   //----- PostScript XObjects
247   virtual void psXObject(Stream *psStream, Stream *level1Stream);
248 
249   //----- miscellaneous
setOffset(double x,double y)250   void setOffset(double x, double y)
251     { tx0 = x; ty0 = y; }
setScale(double x,double y)252   void setScale(double x, double y)
253     { xScale0 = x; yScale0 = y; }
setRotate(int rotateA)254   void setRotate(int rotateA)
255     { rotate0 = rotateA; }
setClip(double llx,double lly,double urx,double ury)256   void setClip(double llx, double lly, double urx, double ury)
257     { clipLLX0 = llx; clipLLY0 = lly; clipURX0 = urx; clipURY0 = ury; }
setUnderlayCbk(void (* cbk)(PSOutputDev * psOut,void * data),void * data)258   void setUnderlayCbk(void (*cbk)(PSOutputDev *psOut, void *data),
259 		      void *data)
260     { underlayCbk = cbk; underlayCbkData = data; }
setOverlayCbk(void (* cbk)(PSOutputDev * psOut,void * data),void * data)261   void setOverlayCbk(void (*cbk)(PSOutputDev *psOut, void *data),
262 		     void *data)
263     { overlayCbk = cbk; overlayCbkData = data; }
264 
265   void writePSChar(char c);
266   void writePSBlock(char *s, int len);
267   void writePS(const char *s);
268   void writePSFmt(const char *fmt, ...);
269   void writePSString(GString *s);
270   void writePSName(const char *s);
271 
272 private:
273 
274   void init(PSOutputFunc outputFuncA, void *outputStreamA,
275 	    PSFileType fileTypeA, PDFDoc *docA,
276 	    int firstPage, int lastPage, PSOutMode modeA,
277 	    int imgLLXA, int imgLLYA, int imgURXA, int imgURYA,
278 	    GBool manualCtrlA);
279   void setupResources(Dict *resDict);
280   void setupFonts(Dict *resDict);
281   void setupFont(GfxFont *font, Dict *parentResDict);
282   PSFontFileInfo *setupEmbeddedType1Font(GfxFont *font, Ref *id);
283   PSFontFileInfo *setupExternalType1Font(GfxFont *font, GString *fileName);
284   PSFontFileInfo *setupEmbeddedType1CFont(GfxFont *font, Ref *id);
285   PSFontFileInfo *setupEmbeddedOpenTypeT1CFont(GfxFont *font, Ref *id);
286   PSFontFileInfo *setupEmbeddedTrueTypeFont(GfxFont *font, Ref *id);
287   PSFontFileInfo *setupExternalTrueTypeFont(GfxFont *font, GString *fileName,
288 					    int fontNum);
289   PSFontFileInfo *setupEmbeddedCIDType0Font(GfxFont *font, Ref *id);
290   PSFontFileInfo *setupEmbeddedCIDTrueTypeFont(GfxFont *font, Ref *id,
291 					       GBool needVerticalMetrics);
292   PSFontFileInfo *setupExternalCIDTrueTypeFont(GfxFont *font,
293 					       GString *fileName,
294 					       int fontNum,
295 					       GBool needVerticalMetrics);
296   PSFontFileInfo *setupEmbeddedOpenTypeCFFFont(GfxFont *font, Ref *id);
297   PSFontFileInfo *setupType3Font(GfxFont *font, Dict *parentResDict);
298   GString *makePSFontName(GfxFont *font, Ref *id);
299   void setupImages(Dict *resDict);
300   void setupImage(Ref id, Stream *str, GBool mask);
301   void setupForms(Dict *resDict);
302   void setupForm(Object *strRef, Object *strObj);
303   void addProcessColor(double c, double m, double y, double k);
304   void addCustomColor(GfxSeparationColorSpace *sepCS);
305   void doPath(GfxPath *path);
306   void doImageL1(Object *ref, GfxImageColorMap *colorMap,
307 		 GBool invert, GBool inlineImg,
308 		 Stream *str, int width, int height, int len);
309   void doImageL1Sep(GfxImageColorMap *colorMap,
310 		    GBool invert, GBool inlineImg,
311 		    Stream *str, int width, int height, int len);
312   void doImageL2(Object *ref, GfxImageColorMap *colorMap,
313 		 GBool invert, GBool inlineImg,
314 		 Stream *str, int width, int height, int len,
315 		 int *maskColors, Stream *maskStr,
316 		 int maskWidth, int maskHeight, GBool maskInvert);
317   void doImageL3(Object *ref, GfxImageColorMap *colorMap,
318 		 GBool invert, GBool inlineImg,
319 		 Stream *str, int width, int height, int len,
320 		 int *maskColors, Stream *maskStr,
321 		 int maskWidth, int maskHeight, GBool maskInvert);
322   void dumpColorSpaceL2(GfxColorSpace *colorSpace,
323 			GBool genXform, GBool updateColors,
324 			GBool map01);
325 #if OPI_SUPPORT
326   void opiBegin20(GfxState *state, Dict *dict);
327   void opiBegin13(GfxState *state, Dict *dict);
328   void opiTransform(GfxState *state, double x0, double y0,
329 		    double *x1, double *y1);
330   GBool getFileSpec(Object *fileSpec, Object *fileName);
331 #endif
332   void cvtFunction(Function *func);
333   GString *filterPSName(GString *name);
334   void writePSTextLine(GString *s);
335 
336   PSLevel level;		// PostScript level (1, 2, separation)
337   PSOutMode mode;		// PostScript mode (PS, EPS, form)
338   int paperWidth;		// width of paper, in pts
339   int paperHeight;		// height of paper, in pts
340   GBool paperMatch;		// true if paper size is set to match each page
341   int imgLLX, imgLLY,		// imageable area, in pts
342       imgURX, imgURY;
343   GBool preload;		// load all images into memory, and
344 				//   predefine forms
345 
346   PSOutputFunc outputFunc;
347   void *outputStream;
348   PSFileType fileType;		// file / pipe / stdout
349   GBool manualCtrl;
350   int seqPage;			// current sequential page number
351   void (*underlayCbk)(PSOutputDev *psOut, void *data);
352   void *underlayCbkData;
353   void (*overlayCbk)(PSOutputDev *psOut, void *data);
354   void *overlayCbkData;
355   GString *(*customCodeCbk)(PSOutputDev *psOut,
356 			    PSOutCustomCodeLocation loc, int n,
357 			    void *data);
358   void *customCodeCbkData;
359 
360   PDFDoc *doc;
361   XRef *xref;			// the xref table for this PDF file
362 
363   GList *fontInfo;		// info for each font [PSFontInfo]
364   GHash *fontFileInfo;		// info for each font file [PSFontFileInfo]
365   Ref *imgIDs;			// list of image IDs for in-memory images
366   int imgIDLen;			// number of entries in imgIDs array
367   int imgIDSize;		// size of imgIDs array
368   Ref *formIDs;			// list of IDs for predefined forms
369   int formIDLen;		// number of entries in formIDs array
370   int formIDSize;		// size of formIDs array
371   GList *xobjStack;		// stack of XObject dicts currently being
372 				//   processed
373   int numSaves;			// current number of gsaves
374   int numTilingPatterns;	// current number of nested tiling patterns
375   int nextFunc;			// next unique number to use for a function
376 
377   GList *paperSizes;		// list of used paper sizes, if paperMatch
378 				//   is true [PSOutPaperSize]
379   double tx0, ty0;		// global translation
380   double xScale0, yScale0;	// global scaling
381   int rotate0;			// rotation angle (0, 90, 180, 270)
382   double clipLLX0, clipLLY0,
383          clipURX0, clipURY0;
384   double tx, ty;		// global translation for current page
385   double xScale, yScale;	// global scaling for current page
386   int rotate;			// rotation angle for current page
387   double epsX1, epsY1,		// EPS bounding box (unrotated)
388          epsX2, epsY2;
389 
390   GString *embFontList;		// resource comments for embedded fonts
391 
392   int processColors;		// used process colors
393   PSOutCustomColor		// used custom colors
394     *customColors;
395 
396   GBool haveTextClip;		// set if text has been drawn with a
397 				//   clipping render mode
398 
399   GBool inType3Char;		// inside a Type 3 CharProc
400   GString *t3String;		// Type 3 content string
401   double t3WX, t3WY,		// Type 3 character parameters
402          t3LLX, t3LLY, t3URX, t3URY;
403   GBool t3FillColorOnly;	// operators should only use the fill color
404   GBool t3Cacheable;		// cleared if char is not cacheable
405   GBool t3NeedsRestore;		// set if a 'q' operator was issued
406 
407 #if OPI_SUPPORT
408   int opi13Nest;		// nesting level of OPI 1.3 objects
409   int opi20Nest;		// nesting level of OPI 2.0 objects
410 #endif
411 
412   GBool ok;			// set up ok?
413 
414   friend class WinPDFPrinter;
415 };
416 
417 #endif
418