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