LibreOffice Module sdext (master)  1
pdfioutdev_gpl.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_SDEXT_SOURCE_PDFIMPORT_XPDFWRAPPER_PDFIOUTDEV_GPL_HXX
21 #define INCLUDED_SDEXT_SOURCE_PDFIMPORT_XPDFWRAPPER_PDFIOUTDEV_GPL_HXX
22 
23 #include <sal/types.h>
24 #include <config_poppler.h>
25 
26 #if defined __GNUC__ || defined __clang__
27 # pragma GCC diagnostic push
28 # pragma GCC diagnostic ignored "-Wundef"
29 # pragma GCC diagnostic ignored "-Wunused-parameter"
30 #elif defined _MSC_VER
31 #pragma warning(push)
32 #pragma warning(disable : 4100) // unreferenced formal parameter
33 #pragma warning(disable : 4310) // cast truncates constant value
34 #endif
35 
36 #include <GfxState.h>
37 #include <GfxFont.h>
38 #include <UnicodeMap.h>
39 #include <Link.h>
40 #include <Object.h>
41 #include <OutputDev.h>
42 #include <GlobalParams.h>
43 #include <PDFDoc.h>
44 
45 #if defined __GNUC__ || defined __clang__
46 # pragma GCC diagnostic pop
47 #elif defined _MSC_VER
48 #pragma warning(pop)
49 #endif
50 
51 #include <unordered_map>
52 #include <vector>
53 #include <memory>
54 
55 class GfxPath;
56 class GfxFont;
57 class PDFDoc;
58 #if HAVE_POPPLER_VERSION_H
59 #include <cpp/poppler-version.h>
60 #else
61 #define POPPLER_VERSION "0.12.3"
62 #define POPPLER_VERSION_MAJOR 0
63 #define POPPLER_VERSION_MINOR 12
64 #define POPPLER_VERSION_MICRO 3
65 #endif
66 #define POPPLER_CHECK_VERSION(major,minor,micro) \
67  (POPPLER_VERSION_MAJOR > (major) || \
68  (POPPLER_VERSION_MAJOR == (major) && POPPLER_VERSION_MINOR > (minor)) || \
69  (POPPLER_VERSION_MAJOR == (major) && POPPLER_VERSION_MINOR == (minor) && POPPLER_VERSION_MICRO >= (micro)))
70 
71 namespace pdfi
72 {
73  struct FontAttributes
74  {
76  familyName(),
77  isEmbedded(false),
78  isBold(false),
79  isItalic(false),
80  isUnderline(false),
81  size(0.0)
82  {}
83 
84  // xpdf goo stuff is so totally borked...
85  // ...need to hand-code assignment
86  FontAttributes( const FontAttributes& rSrc ) :
87  familyName(),
88  isEmbedded(rSrc.isEmbedded),
89  isBold(rSrc.isBold),
90  isItalic(rSrc.isItalic),
92  size(rSrc.size)
93  {
94  familyName.append(&rSrc.getFamilyName());
95  }
96 
98  {
99  familyName.clear();
100  familyName.append(&rSrc.getFamilyName());
101 
102  isEmbedded = rSrc.isEmbedded;
103  isBold = rSrc.isBold;
104  isItalic = rSrc.isItalic;
105  isUnderline = rSrc.isUnderline;
106  size = rSrc.size;
107 
108  return *this;
109  }
110 
111  bool operator==(const FontAttributes& rFont) const
112  {
113  return getFamilyName().cmp(&rFont.getFamilyName())==0 &&
114  isEmbedded == rFont.isEmbedded &&
115  isBold == rFont.isBold &&
116  isItalic == rFont.isItalic &&
117  isUnderline == rFont.isUnderline &&
118  size == rFont.size;
119  }
120 
121  GooString familyName;
123  bool isBold;
124  bool isItalic;
125  bool isUnderline;
126  double size;
127 
128  private:
129  // Work around const-ness issues in the GooString API:
130  GooString & getFamilyName() const
131  { return const_cast<GooString &>(familyName); }
132  };
133 
134  // Versions before 0.15 defined GBool as int; 0.15 redefined it as bool; 0.71 dropped GBool
135 #if POPPLER_VERSION_MAJOR == 0 && POPPLER_VERSION_MINOR < 71
136  typedef GBool poppler_bool;
137 #else
138  typedef bool poppler_bool;
139 #endif
140 
141  class PDFOutDev : public OutputDev
142  {
143  // not owned by this class
144  PDFDoc* m_pDoc;
145  mutable std::unordered_map< long long,
147  std::unique_ptr<UnicodeMap> m_pUtf8Map;
149 
150  int parseFont( long long nNewId, GfxFont* pFont, GfxState* state ) const;
151  void writeFontFile( GfxFont* gfxFont ) const;
152 #if POPPLER_CHECK_VERSION(0, 83, 0)
153  static void printPath( const GfxPath* pPath );
154 #else
155  static void printPath( GfxPath* pPath );
156 #endif
157 
158  public:
159  explicit PDFOutDev( PDFDoc* pDoc );
160  virtual ~PDFOutDev() override;
161 
162  //----- get info about output device
163 
164  // Does this device use upside-down coordinates?
165  // (Upside-down means (0,0) is the top left corner of the page.)
166  virtual poppler_bool upsideDown() override { return true; }
167 
168  // Does this device use drawChar() or drawString()?
169  virtual poppler_bool useDrawChar() override { return true; }
170 
171  // Does this device use beginType3Char/endType3Char? Otherwise,
172  // text in Type 3 fonts will be drawn with drawChar/drawString.
173  virtual poppler_bool interpretType3Chars() override { return false; }
174 
175  // Does this device need non-text content?
176  virtual poppler_bool needNonText() override { return true; }
177 
178  //----- initialization and control
179 
180  // Set default transform matrix.
181 #if POPPLER_CHECK_VERSION(0, 71, 0)
182  virtual void setDefaultCTM(const double *ctm) override;
183 #else
184  virtual void setDefaultCTM(double *ctm) override;
185 #endif
186 
187  // Start a page.
188  virtual void startPage(int pageNum, GfxState *state
189 #if POPPLER_CHECK_VERSION(0, 23, 0) || POPPLER_CHECK_VERSION(0, 24, 0)
190  , XRef *xref
191 #endif
192  ) override;
193 
194  // End a page.
195  virtual void endPage() override;
196 
197  //----- link borders
198  #if POPPLER_CHECK_VERSION(0, 19, 0)
199  virtual void processLink(AnnotLink *link) override;
200  #elif POPPLER_CHECK_VERSION(0, 17, 0)
201  virtual void processLink(AnnotLink *link, Catalog *catalog) override;
202  #else
203  virtual void processLink(Link *link, Catalog *catalog) override;
204  #endif
205 
206  //----- save/restore graphics state
207  virtual void saveState(GfxState *state) override;
208  virtual void restoreState(GfxState *state) override;
209 
210  //----- update graphics state
211  virtual void updateCTM(GfxState *state, double m11, double m12,
212  double m21, double m22, double m31, double m32) override;
213  virtual void updateLineDash(GfxState *state) override;
214  virtual void updateFlatness(GfxState *state) override;
215  virtual void updateLineJoin(GfxState *state) override;
216  virtual void updateLineCap(GfxState *state) override;
217  virtual void updateMiterLimit(GfxState *state) override;
218  virtual void updateLineWidth(GfxState *state) override;
219  virtual void updateFillColor(GfxState *state) override;
220  virtual void updateStrokeColor(GfxState *state) override;
221  virtual void updateFillOpacity(GfxState *state) override;
222  virtual void updateStrokeOpacity(GfxState *state) override;
223  virtual void updateBlendMode(GfxState *state) override;
224 
225  //----- update text state
226  virtual void updateFont(GfxState *state) override;
227  virtual void updateRender(GfxState *state) override;
228 
229  //----- path painting
230  virtual void stroke(GfxState *state) override;
231  virtual void fill(GfxState *state) override;
232  virtual void eoFill(GfxState *state) override;
233 
234  //----- path clipping
235  virtual void clip(GfxState *state) override;
236  virtual void eoClip(GfxState *state) override;
237 
238  //----- text drawing
239 #if POPPLER_CHECK_VERSION(0, 82, 0)
240  virtual void drawChar(GfxState *state, double x, double y,
241  double dx, double dy,
242  double originX, double originY,
243  CharCode code, int nBytes, const Unicode *u, int uLen) override;
244 #else
245  virtual void drawChar(GfxState *state, double x, double y,
246  double dx, double dy,
247  double originX, double originY,
248  CharCode code, int nBytes, Unicode *u, int uLen) override;
249 #endif
250 #if POPPLER_CHECK_VERSION(0, 64, 0)
251  virtual void drawString(GfxState *state, const GooString *s) override;
252 #else
253  virtual void drawString(GfxState *state, GooString *s) override;
254 #endif
255  virtual void endTextObject(GfxState *state) override;
256 
257  //----- image drawing
258  virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
259  int width, int height, poppler_bool invert,
260  poppler_bool interpolate,
261  poppler_bool inlineImg) override;
262 #if POPPLER_CHECK_VERSION(0, 82, 0)
263  virtual void drawImage(GfxState *state, Object *ref, Stream *str,
264  int width, int height, GfxImageColorMap *colorMap,
265  poppler_bool interpolate,
266  const int* maskColors, poppler_bool inlineImg) override;
267 #else
268  virtual void drawImage(GfxState *state, Object *ref, Stream *str,
269  int width, int height, GfxImageColorMap *colorMap,
270  poppler_bool interpolate,
271  int* maskColors, poppler_bool inlineImg) override;
272 #endif
273  virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str,
274  int width, int height,
275  GfxImageColorMap *colorMap,
276  poppler_bool interpolate,
277  Stream *maskStr, int maskWidth, int maskHeight,
278  poppler_bool maskInvert,
279  poppler_bool maskInterpolate
280  ) override;
281  virtual void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
282  int width, int height,
283  GfxImageColorMap *colorMap,
284  poppler_bool interpolate,
285  Stream *maskStr,
286  int maskWidth, int maskHeight,
287  GfxImageColorMap *maskColorMap
288  , poppler_bool maskInterpolate
289  ) override;
290 
291  static void setPageNum( int nNumPages );
292  void setSkipImages ( bool bSkipImages );
293  };
294 }
295 
296 extern FILE* g_binary_out;
297 
298 // note: if you ever change Output_t, please keep in mind that the current code
299 // relies on it being of 8 bit size
300 typedef unsigned char Output_t;
301 typedef std::vector< Output_t > OutputBuffer;
302 
303 #if !POPPLER_CHECK_VERSION(0, 73, 0)
304 static_assert(std::is_same_v<Guchar, unsigned char>, "unexpected typedef");
305 #endif
306 
307 #endif // INCLUDED_SDEXT_SOURCE_PDFIMPORT_XPDFWRAPPER_PDFIOUTDEV_GPL_HXX
308 
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void updateLineCap(GfxState *state) override
virtual void updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32) override
FILE * g_binary_out
Definition: wrapper_gpl.cxx:26
virtual void setDefaultCTM(double *ctm) override
virtual void updateLineDash(GfxState *state) override
virtual void updateFillColor(GfxState *state) override
virtual void startPage(int pageNum, GfxState *state) override
virtual void clip(GfxState *state) override
virtual ~PDFOutDev() override
virtual poppler_bool useDrawChar() override
std::unordered_map< long long, FontAttributes > m_aFontMap
virtual void updateMiterLimit(GfxState *state) override
virtual void drawImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, poppler_bool interpolate, int *maskColors, poppler_bool inlineImg) override
virtual poppler_bool interpretType3Chars() override
virtual void eoFill(GfxState *state) override
FontAttributes & operator=(const FontAttributes &rSrc)
virtual void drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, poppler_bool interpolate, Stream *maskStr, int maskWidth, int maskHeight, GfxImageColorMap *maskColorMap, poppler_bool maskInterpolate) override
virtual void drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, CharCode code, int nBytes, Unicode *u, int uLen) override
Output one glyph.
static void setPageNum(int nNumPages)
virtual void stroke(GfxState *state) override
virtual void eoClip(GfxState *state) override
virtual poppler_bool upsideDown() override
FontAttributes(const FontAttributes &rSrc)
virtual void updateFlatness(GfxState *state) override
virtual void fill(GfxState *state) override
virtual void updateStrokeColor(GfxState *state) override
static void printPath(GfxPath *pPath)
PDFOutDev(PDFDoc *pDoc)
virtual void processLink(Link *link, Catalog *catalog) override
virtual void restoreState(GfxState *state) override
virtual void drawString(GfxState *state, GooString *s) override
virtual void updateLineJoin(GfxState *state) override
virtual void updateFont(GfxState *state) override
unsigned char Output_t
std::vector< Output_t > OutputBuffer
void setSkipImages(bool bSkipImages)
virtual void drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, poppler_bool invert, poppler_bool interpolate, poppler_bool inlineImg) override
GooString & getFamilyName() const
virtual void drawMaskedImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, poppler_bool interpolate, Stream *maskStr, int maskWidth, int maskHeight, poppler_bool maskInvert, poppler_bool maskInterpolate) override
#define POPPLER_CHECK_VERSION(major, minor, micro)
virtual void updateStrokeOpacity(GfxState *state) override
bool operator==(const FontAttributes &rFont) const
virtual void updateBlendMode(GfxState *state) override
virtual poppler_bool needNonText() override
virtual void endPage() override
virtual void endTextObject(GfxState *state) override
void writeFontFile(GfxFont *gfxFont) const
virtual void updateLineWidth(GfxState *state) override
virtual void updateFillOpacity(GfxState *state) override
GBool poppler_bool
std::unique_ptr< UnicodeMap > m_pUtf8Map
virtual void updateRender(GfxState *state) override
virtual void saveState(GfxState *state) override
int parseFont(long long nNewId, GfxFont *pFont, GfxState *state) const