1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QPDF_P_H
41#define QPDF_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtGui/private/qtguiglobal_p.h>
55
56#ifndef QT_NO_PDF
57
58#include "QtGui/qmatrix.h"
59#include "QtCore/qstring.h"
60#include "QtCore/qvector.h"
61#include "private/qstroker_p.h"
62#include "private/qpaintengine_p.h"
63#include "private/qfontengine_p.h"
64#include "private/qfontsubset_p.h"
65#include "qpagelayout.h"
66
67QT_BEGIN_NAMESPACE
68
69const char *qt_real_to_string(qreal val, char *buf);
70const char *qt_int_to_string(int val, char *buf);
71
72namespace QPdf {
73
74 class ByteStream
75 {
76 public:
77 // fileBacking means that ByteStream will buffer the contents on disk
78 // if the size exceeds a certain threshold. In this case, if a byte
79 // array was passed in, its contents may no longer correspond to the
80 // ByteStream contents.
81 explicit ByteStream(bool fileBacking = false);
82 explicit ByteStream(QByteArray *ba, bool fileBacking = false);
83 ~ByteStream();
84 ByteStream &operator <<(char chr);
85 ByteStream &operator <<(const char *str);
86 ByteStream &operator <<(const QByteArray &str);
87 ByteStream &operator <<(const ByteStream &src);
88 ByteStream &operator <<(qreal val);
89 ByteStream &operator <<(int val);
90 ByteStream &operator <<(const QPointF &p);
91 // Note that the stream may be invalidated by calls that insert data.
92 QIODevice *stream();
93 void clear();
94
95 static inline int maxMemorySize() { return 100000000; }
96 static inline int chunkSize() { return 10000000; }
97
98 protected:
99 void constructor_helper(QIODevice *dev);
100 void constructor_helper(QByteArray *ba);
101
102 private:
103 void prepareBuffer();
104
105 private:
106 QIODevice *dev;
107 QByteArray ba;
108 bool fileBackingEnabled;
109 bool fileBackingActive;
110 bool handleDirty;
111 };
112
113 enum PathFlags {
114 ClipPath,
115 FillPath,
116 StrokePath,
117 FillAndStrokePath
118 };
119 QByteArray generatePath(const QPainterPath &path, const QTransform &matrix, PathFlags flags);
120 QByteArray generateMatrix(const QTransform &matrix);
121 QByteArray generateDashes(const QPen &pen);
122 QByteArray patternForBrush(const QBrush &b);
123
124 struct Stroker {
125 Stroker();
126 void setPen(const QPen &pen, QPainter::RenderHints hints);
127 void strokePath(const QPainterPath &path);
128 ByteStream *stream;
129 bool first;
130 QTransform matrix;
131 bool cosmeticPen;
132 private:
133 QStroker basicStroker;
134 QDashStroker dashStroker;
135 QStrokerOps *stroker;
136 };
137
138 QByteArray ascii85Encode(const QByteArray &input);
139
140 const char *toHex(ushort u, char *buffer);
141 const char *toHex(uchar u, char *buffer);
142
143}
144
145
146class QPdfPage : public QPdf::ByteStream
147{
148public:
149 QPdfPage();
150
151 QVector<uint> images;
152 QVector<uint> graphicStates;
153 QVector<uint> patterns;
154 QVector<uint> fonts;
155 QVector<uint> annotations;
156
157 void streamImage(int w, int h, int object);
158
159 QSize pageSize;
160private:
161};
162
163class QPdfWriter;
164class QPdfEnginePrivate;
165
166class Q_GUI_EXPORT QPdfEngine : public QPaintEngine
167{
168 Q_DECLARE_PRIVATE(QPdfEngine)
169 friend class QPdfWriter;
170public:
171 // keep in sync with QPagedPaintDevice::PdfVersion and QPdfEnginePrivate::writeHeader()::mapping!
172 enum PdfVersion
173 {
174 Version_1_4,
175 Version_A1b,
176 Version_1_6
177 };
178
179 QPdfEngine();
180 QPdfEngine(QPdfEnginePrivate &d);
181 ~QPdfEngine() {}
182
183 void setOutputFilename(const QString &filename);
184
185 void setResolution(int resolution);
186 int resolution() const;
187
188 void setPdfVersion(PdfVersion version);
189
190 // reimplementations QPaintEngine
191 bool begin(QPaintDevice *pdev) override;
192 bool end() override;
193
194 void drawPoints(const QPointF *points, int pointCount) override;
195 void drawLines(const QLineF *lines, int lineCount) override;
196 void drawRects(const QRectF *rects, int rectCount) override;
197 void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) override;
198 void drawPath (const QPainterPath & path) override;
199
200 void drawTextItem(const QPointF &p, const QTextItem &textItem) override;
201
202 void drawPixmap (const QRectF & rectangle, const QPixmap & pixmap, const QRectF & sr) override;
203 void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
204 Qt::ImageConversionFlags flags = Qt::AutoColor) override;
205 void drawTiledPixmap (const QRectF & rectangle, const QPixmap & pixmap, const QPointF & point) override;
206
207 void drawHyperlink(const QRectF &r, const QUrl &url);
208
209 void updateState(const QPaintEngineState &state) override;
210
211 int metric(QPaintDevice::PaintDeviceMetric metricType) const;
212 Type type() const override;
213 // end reimplementations QPaintEngine
214
215 // Printer stuff...
216 bool newPage();
217
218 // Page layout stuff
219 void setPageLayout(const QPageLayout &pageLayout);
220 void setPageSize(const QPageSize &pageSize);
221 void setPageOrientation(QPageLayout::Orientation orientation);
222 void setPageMargins(const QMarginsF &margins, QPageLayout::Unit units = QPageLayout::Point);
223
224 QPageLayout pageLayout() const;
225
226 void setPen();
227 void setBrush();
228 void setupGraphicsState(QPaintEngine::DirtyFlags flags);
229
230private:
231 void updateClipPath(const QPainterPath & path, Qt::ClipOperation op);
232};
233
234class Q_GUI_EXPORT QPdfEnginePrivate : public QPaintEnginePrivate
235{
236 Q_DECLARE_PUBLIC(QPdfEngine)
237public:
238 QPdfEnginePrivate();
239 ~QPdfEnginePrivate();
240
241 inline uint requestObject() { return currentObject++; }
242
243 void writeHeader();
244 void writeTail();
245
246 int addImage(const QImage &image, bool *bitmap, bool lossless, qint64 serial_no);
247 int addConstantAlphaObject(int brushAlpha, int penAlpha = 255);
248 int addBrushPattern(const QTransform &matrix, bool *specifyColor, int *gStateObject);
249
250 void drawTextItem(const QPointF &p, const QTextItemInt &ti);
251
252 QTransform pageMatrix() const;
253
254 void newPage();
255
256 int currentObject;
257
258 QPdfPage* currentPage;
259 QPdf::Stroker stroker;
260
261 QPointF brushOrigin;
262 QBrush brush;
263 QPen pen;
264 QVector<QPainterPath> clips;
265 bool clipEnabled;
266 bool allClipped;
267 bool hasPen;
268 bool hasBrush;
269 bool simplePen;
270 qreal opacity;
271 QPdfEngine::PdfVersion pdfVersion;
272
273 QHash<QFontEngine::FaceId, QFontSubset *> fonts;
274
275 QPaintDevice *pdev;
276
277 // the device the output is in the end streamed to.
278 QIODevice *outDevice;
279 bool ownsDevice;
280
281 // printer options
282 QString outputFileName;
283 QString title;
284 QString creator;
285 bool embedFonts;
286 int resolution;
287 bool grayscale;
288
289 // Page layout: size, orientation and margins
290 QPageLayout m_pageLayout;
291
292private:
293 int gradientBrush(const QBrush &b, const QTransform &matrix, int *gStateObject);
294 int generateGradientShader(const QGradient *gradient, const QTransform &matrix, bool alpha = false);
295 int generateLinearGradientShader(const QLinearGradient *lg, const QTransform &matrix, bool alpha);
296 int generateRadialGradientShader(const QRadialGradient *gradient, const QTransform &matrix, bool alpha);
297 int createShadingFunction(const QGradient *gradient, int from, int to, bool reflect, bool alpha);
298
299 void writeInfo();
300 int writeXmpMetaData();
301 int writeOutputIntent();
302 void writePageRoot();
303 void writeFonts();
304 void embedFont(QFontSubset *font);
305 qreal calcUserUnit() const;
306
307 QVector<int> xrefPositions;
308 QDataStream* stream;
309 int streampos;
310
311 int writeImage(const QByteArray &data, int width, int height, int depth,
312 int maskObject, int softMaskObject, bool dct = false, bool isMono = false);
313 void writePage();
314
315 int addXrefEntry(int object, bool printostr = true);
316 void printString(const QString &string);
317 void xprintf(const char* fmt, ...);
318 inline void write(const QByteArray &data) {
319 stream->writeRawData(data.constData(), data.size());
320 streampos += data.size();
321 }
322
323 int writeCompressed(const char *src, int len);
324 inline int writeCompressed(const QByteArray &data) { return writeCompressed(data.constData(), data.length()); }
325 int writeCompressed(QIODevice *dev);
326
327 // various PDF objects
328 int pageRoot, catalog, info, graphicsState, patternColorSpace;
329 QVector<uint> pages;
330 QHash<qint64, uint> imageCache;
331 QHash<QPair<uint, uint>, uint > alphaCache;
332};
333
334QT_END_NAMESPACE
335
336#endif // QT_NO_PDF
337
338#endif // QPDF_P_H
339
340