1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef INCLUDED_PDFI_OUTDEV_HXX
23 #define INCLUDED_PDFI_OUTDEV_HXX
24 
25 #if defined __GNUC__
26 #pragma GCC system_header
27 #elif defined __SUNPRO_CC
28 #pragma disable_warn
29 #elif defined _MSC_VER
30 #pragma warning(push, 1)
31 #endif
32 
33 #include "GfxState.h"
34 #include "GfxFont.h"
35 #include "UnicodeMap.h"
36 #include "Link.h"
37 #include "Object.h"
38 #include "OutputDev.h"
39 #include "Stream.h"
40 #ifndef SYSTEM_POPPLER
41 #  include "parseargs.h"
42 #endif
43 #include "GlobalParams.h"
44 #include "PDFDoc.h"
45 
46 #if defined __SUNPRO_CC
47 #pragma enable_warn
48 #elif defined _MSC_VER
49 #pragma warning(pop)
50 #endif
51 
52 #include <hash_map>
53 #include <vector>
54 
55 class GfxPath;
56 class GfxFont;
57 class PDFDoc;
58 #ifndef SYSTEM_POPPLER
59 typedef GString GooString;
60 #endif
61 
62 namespace pdfi
63 {
64     struct FontAttributes
65     {
66         FontAttributes( const GooString& familyName_,
67                         bool           isEmbedded_,
68                         bool           isBold_,
69                         bool           isItalic_,
70                         bool           isUnderline_,
71                         double         size_ ) :
72             familyName(),
73             isEmbedded(isEmbedded_),
74             isBold(isBold_),
75             isItalic(isItalic_),
76             isUnderline(isUnderline_),
77             size(size_)
78         {
79             familyName.append(const_cast<GooString*>(&familyName_));
80         }
81 
82         FontAttributes() :
83             familyName(),
84             isEmbedded(false),
85             isBold(false),
86             isItalic(false),
87             isUnderline(false),
88             size(0.0)
89         {}
90 
91         // xdpf goo stuff is so totally borked...
92         // ...need to hand-code assignment
93         FontAttributes( const FontAttributes& rSrc ) :
94             familyName(),
95             isEmbedded(rSrc.isEmbedded),
96             isBold(rSrc.isBold),
97             isItalic(rSrc.isItalic),
98             isUnderline(rSrc.isUnderline),
99             size(rSrc.size)
100         {
101             familyName.append(const_cast<GooString*>(&rSrc.familyName));
102         }
103 
104         FontAttributes& operator=( const FontAttributes& rSrc )
105         {
106             familyName.clear();
107             familyName.append(const_cast<GooString*>(&rSrc.familyName));
108 
109             isEmbedded  = rSrc.isEmbedded;
110             isBold      = rSrc.isBold;
111             isItalic    = rSrc.isItalic;
112             isUnderline = rSrc.isUnderline;
113             size        = rSrc.size;
114 
115             return *this;
116         }
117 
118         bool operator==(const FontAttributes& rFont) const
119         {
120             return const_cast<GooString*>(&familyName)->cmp(
121                 const_cast<GooString*>(&rFont.familyName))==0 &&
122                 isEmbedded == rFont.isEmbedded &&
123                 isBold == rFont.isBold &&
124                 isItalic == rFont.isItalic &&
125                 isUnderline == rFont.isUnderline &&
126                 size == rFont.size;
127         }
128 
129         GooString     familyName;
130         bool        isEmbedded;
131         bool        isBold;
132         bool        isItalic;
133         bool        isUnderline;
134         double      size;
135     };
136 
137     class PDFOutDev : public OutputDev
138     {
139         // not owned by this class
140         PDFDoc*                                 m_pDoc;
141         mutable std::hash_map< long long,
142                                FontAttributes > m_aFontMap;
143         UnicodeMap*                             m_pUtf8Map;
144 
145         int  parseFont( long long nNewId, GfxFont* pFont, GfxState* state ) const;
146         void writeFontFile( GfxFont* gfxFont ) const;
147         void printPath( GfxPath* pPath ) const;
148 
149     public:
150         explicit PDFOutDev( PDFDoc* pDoc );
151 
152         //----- get info about output device
153 
154         // Does this device use upside-down coordinates?
155         // (Upside-down means (0,0) is the top left corner of the page.)
156         virtual GBool upsideDown() { return gTrue; }
157 
158         // Does this device use drawChar() or drawString()?
159         virtual GBool useDrawChar() { return gTrue; }
160 
161         // Does this device use beginType3Char/endType3Char?  Otherwise,
162         // text in Type 3 fonts will be drawn with drawChar/drawString.
163         virtual GBool interpretType3Chars() { return gFalse; }
164 
165         // Does this device need non-text content?
166         virtual GBool needNonText() { return gTrue; }
167 
168         //----- initialization and control
169 
170         // Set default transform matrix.
171         virtual void setDefaultCTM(double *ctm);
172 
173         // Start a page.
174         virtual void startPage(int pageNum, GfxState *state, XRef *xref);
175 
176         // End a page.
177         virtual void endPage();
178 
179         // Dump page contents to display.
180         // virtual void dump() {}
181 
182         //----- coordinate conversion
183 
184         // Convert between device and user coordinates.
185         // virtual void cvtDevToUser(double dx, double dy, double *ux, double *uy);
186         // virtual void cvtUserToDev(double ux, double uy, int *dx, int *dy);
187 
188         //----- link borders
189         virtual void processLink(AnnotLink *link);
190 
191         //----- save/restore graphics state
192         virtual void saveState(GfxState *state);
193         virtual void restoreState(GfxState *state);
194 
195         //----- update graphics state
196         // virtual void updateAll(GfxState *state);
197         virtual void updateCTM(GfxState *state, double m11, double m12,
198                                double m21, double m22, double m31, double m32);
199         virtual void updateLineDash(GfxState *state);
200         virtual void updateFlatness(GfxState *state);
201         virtual void updateLineJoin(GfxState *state);
202         virtual void updateLineCap(GfxState *state);
203         virtual void updateMiterLimit(GfxState *state);
204         virtual void updateLineWidth(GfxState *state);
205         virtual void updateFillColor(GfxState *state);
206         virtual void updateStrokeColor(GfxState *state);
207         virtual void updateFillOpacity(GfxState *state);
208         virtual void updateStrokeOpacity(GfxState *state);
209         virtual void updateBlendMode(GfxState *state);
210 
211         //----- update text state
212         virtual void updateFont(GfxState *state);
213         // virtual void updateTextMat(GfxState *state);
214         // virtual void updateCharSpace(GfxState *state) {}
215         virtual void updateRender(GfxState *state);
216         // virtual void updateRise(GfxState *state) {}
217         // virtual void updateWordSpace(GfxState *state) {}
218         // virtual void updateHorizScaling(GfxState *state) {}
219         // virtual void updateTextPos(GfxState *state) {}
220         // virtual void updateTextShift(GfxState *state, double shift) {}
221 
222         //----- path painting
223         virtual void stroke(GfxState *state);
224         virtual void fill(GfxState *state);
225         virtual void eoFill(GfxState *state);
226 
227         //----- path clipping
228         virtual void clip(GfxState *state);
229         virtual void eoClip(GfxState *state);
230 
231         //----- text drawing
232         virtual void drawChar(GfxState *state, double x, double y,
233                               double dx, double dy,
234                               double originX, double originY,
235                               CharCode code, int nBytes, Unicode *u, int uLen);
236         virtual void drawString(GfxState *state, GooString *s);
237         virtual void endTextObject(GfxState *state);
238 
239         //----- image drawing
240         virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
241                                    int width, int height, GBool invert,
242                                    GBool inlineImg);
243         virtual void drawImage(GfxState *state, Object *ref, Stream *str,
244                                int width, int height, GfxImageColorMap *colorMap,
245                                int *maskColors, GBool inlineImg);
246         virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str,
247                                      int width, int height,
248                                      GfxImageColorMap *colorMap,
249                                      Stream *maskStr, int maskWidth, int maskHeight,
250                                      GBool maskInvert);
251         virtual void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
252                                          int width, int height,
253                                          GfxImageColorMap *colorMap,
254                                          Stream *maskStr,
255                                          int maskWidth, int maskHeight,
256                                          GfxImageColorMap *maskColorMap);
257 
258         //----- OPI functions
259         // virtual void opiBegin(GfxState *state, Dict *opiDict);
260         // virtual void opiEnd(GfxState *state, Dict *opiDict);
261 
262         //----- Type 3 font operators
263         // virtual void type3D0(GfxState *state, double wx, double wy) {}
264         // virtual void type3D1(GfxState *state, double wx, double wy,
265         //                      double llx, double lly, double urx, double ury) {}
266 
267         //----- PostScript XObjects
268         // virtual void psXObject(Stream *psStream, Stream *level1Stream) {}
269 
270         void setPageNum( int nNumPages );
271     };
272 }
273 
274 extern FILE* g_binary_out;
275 
276 // note: if you ever hcange Output_t, please keep in mind that the current code
277 // relies on it being of 8 bit size
278 typedef char Output_t;
279 typedef std::vector< Output_t > OutputBuffer;
280 
281 #endif /* INCLUDED_PDFI_OUTDEV_HXX */
282 
283