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 "QtCore/qstring.h"
59#include "QtCore/qvector.h"
60#include "private/qstroker_p.h"
61#include "private/qpaintengine_p.h"
62#include "private/qfontengine_p.h"
63#include "private/qfontsubset_p.h"
64#include "qpagelayout.h"
65
66QT_BEGIN_NAMESPACE
67
68const char *qt_real_to_string(qreal val, char *buf);
69const char *qt_int_to_string(int val, char *buf);
70
71namespace QPdf {
72
73 class ByteStream
74 {
75 public:
76 // fileBacking means that ByteStream will buffer the contents on disk
77 // if the size exceeds a certain threshold. In this case, if a byte
78 // array was passed in, its contents may no longer correspond to the
79 // ByteStream contents.
80 explicit ByteStream(bool fileBacking = false);
81 explicit ByteStream(QByteArray *ba, bool fileBacking = false);
82 ~ByteStream();
83 ByteStream &operator <<(char chr);
84 ByteStream &operator <<(const char *str);
85 ByteStream &operator <<(const QByteArray &str);
86 ByteStream &operator <<(const ByteStream &src);
87 ByteStream &operator <<(qreal val);
88 ByteStream &operator <<(int val);
89 ByteStream &operator <<(const QPointF &p);
90 // Note that the stream may be invalidated by calls that insert data.
91 QIODevice *stream();
92 void clear();
93
94 static inline int maxMemorySize() { return 100000000; }
95 static inline int chunkSize() { return 10000000; }
96
97 protected:
98 void constructor_helper(QIODevice *dev);
99 void constructor_helper(QByteArray *ba);
100
101 private:
102 void prepareBuffer();
103
104 private:
105 QIODevice *dev;
106 QByteArray ba;
107 bool fileBackingEnabled;
108 bool fileBackingActive;
109 bool handleDirty;
110 };
111
112 enum PathFlags {
113 ClipPath,
114 FillPath,
115 StrokePath,
116 FillAndStrokePath
117 };
118 QByteArray generatePath(const QPainterPath &path, const QTransform &matrix, PathFlags flags);
119 QByteArray generateMatrix(const QTransform &matrix);
120 QByteArray generateDashes(const QPen &pen);
121 QByteArray patternForBrush(const QBrush &b);
122
123 struct Stroker {
124 Stroker();
125 void setPen(const QPen &pen, QPainter::RenderHints hints);
126 void strokePath(const QPainterPath &path);
127 ByteStream *stream;
128 bool first;
129 QTransform matrix;
130 bool cosmeticPen;
131 private:
132 QStroker basicStroker;
133 QDashStroker dashStroker;
134 QStrokerOps *stroker;
135 };
136
137 QByteArray ascii85Encode(const QByteArray &input);
138
139 const char *toHex(ushort u, char *buffer);
140 const char *toHex(uchar u, char *buffer);
141
142}
143
144
145class QPdfPage : public QPdf::ByteStream
146{
147public:
148 QPdfPage();
149
150 QVector<uint> images;
151 QVector<uint> graphicStates;
152 QVector<uint> patterns;
153 QVector<uint> fonts;
154 QVector<uint> annotations;
155
156 void streamImage(int w, int h, int object);
157
158 QSize pageSize;
159private:
160};
161
162class QPdfWriter;
163class QPdfEnginePrivate;
164
165class Q_GUI_EXPORT QPdfEngine : public QPaintEngine
166{
167 Q_DECLARE_PRIVATE(QPdfEngine)
168 friend class QPdfWriter;
169public:
170 // keep in sync with QPagedPaintDevice::PdfVersion and QPdfEnginePrivate::writeHeader()::mapping!
171 enum PdfVersion
172 {
173 Version_1_4,
174 Version_A1b,
175 Version_1_6
176 };
177
178 QPdfEngine();
179 QPdfEngine(QPdfEnginePrivate &d);
180 ~QPdfEngine() {}
181
182 void setOutputFilename(const QString &filename);
183
184 void setResolution(int resolution);
185 int resolution() const;
186
187 void setPdfVersion(PdfVersion version);
188
189 void setDocumentXmpMetadata(const QByteArray &xmpMetadata);
190 QByteArray documentXmpMetadata() const;
191
192 void addFileAttachment(const QString &fileName, const QByteArray &data, const QString &mimeType);
193
194 // reimplementations QPaintEngine
195 bool begin(QPaintDevice *pdev) override;
196 bool end() override;
197
198 void drawPoints(const QPointF *points, int pointCount) override;
199 void drawLines(const QLineF *lines, int lineCount) override;
200 void drawRects(const QRectF *rects, int rectCount) override;
201 void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) override;
202 void drawPath (const QPainterPath & path) override;
203
204 void drawTextItem(const QPointF &p, const QTextItem &textItem) override;
205
206 void drawPixmap (const QRectF & rectangle, const QPixmap & pixmap, const QRectF & sr) override;
207 void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
208 Qt::ImageConversionFlags flags = Qt::AutoColor) override;
209 void drawTiledPixmap (const QRectF & rectangle, const QPixmap & pixmap, const QPointF & point) override;
210
211 void drawHyperlink(const QRectF &r, const QUrl &url);
212
213 void updateState(const QPaintEngineState &state) override;
214
215 int metric(QPaintDevice::PaintDeviceMetric metricType) const;
216 Type type() const override;
217 // end reimplementations QPaintEngine
218
219 // Printer stuff...
220 bool newPage();
221
222 // Page layout stuff
223 void setPageLayout(const QPageLayout &pageLayout);
224 void setPageSize(const QPageSize &pageSize);
225 void setPageOrientation(QPageLayout::Orientation orientation);
226 void setPageMargins(const QMarginsF &margins, QPageLayout::Unit units = QPageLayout::Point);
227
228 QPageLayout pageLayout() const;
229
230 void setPen();
231 void setBrush();
232 void setupGraphicsState(QPaintEngine::DirtyFlags flags);
233
234private:
235 void updateClipPath(const QPainterPath & path, Qt::ClipOperation op);
236};
237
238class Q_GUI_EXPORT QPdfEnginePrivate : public QPaintEnginePrivate
239{
240 Q_DECLARE_PUBLIC(QPdfEngine)
241public:
242 QPdfEnginePrivate();
243 ~QPdfEnginePrivate();
244
245 inline uint requestObject() { return currentObject++; }
246
247 void writeHeader();
248 void writeTail();
249
250 int addImage(const QImage &image, bool *bitmap, bool lossless, qint64 serial_no);
251 int addConstantAlphaObject(int brushAlpha, int penAlpha = 255);
252 int addBrushPattern(const QTransform &matrix, bool *specifyColor, int *gStateObject);
253
254 void drawTextItem(const QPointF &p, const QTextItemInt &ti);
255
256 QTransform pageMatrix() const;
257
258 void newPage();
259
260 int currentObject;
261
262 QPdfPage* currentPage;
263 QPdf::Stroker stroker;
264
265 QPointF brushOrigin;
266 QBrush brush;
267 QPen pen;
268 QVector<QPainterPath> clips;
269 bool clipEnabled;
270 bool allClipped;
271 bool hasPen;
272 bool hasBrush;
273 bool simplePen;
274 bool needsTransform;
275 qreal opacity;
276 QPdfEngine::PdfVersion pdfVersion;
277
278 QHash<QFontEngine::FaceId, QFontSubset *> fonts;
279
280 QPaintDevice *pdev;
281
282 // the device the output is in the end streamed to.
283 QIODevice *outDevice;
284 bool ownsDevice;
285
286 // printer options
287 QString outputFileName;
288 QString title;
289 QString creator;
290 bool embedFonts;
291 int resolution;
292 bool grayscale;
293
294 // Page layout: size, orientation and margins
295 QPageLayout m_pageLayout;
296
297private:
298 int gradientBrush(const QBrush &b, const QTransform &matrix, int *gStateObject);
299 int generateGradientShader(const QGradient *gradient, const QTransform &matrix, bool alpha = false);
300 int generateLinearGradientShader(const QLinearGradient *lg, const QTransform &matrix, bool alpha);
301 int generateRadialGradientShader(const QRadialGradient *gradient, const QTransform &matrix, bool alpha);
302 int createShadingFunction(const QGradient *gradient, int from, int to, bool reflect, bool alpha);
303
304 void writeInfo();
305 int writeXmpDcumentMetaData();
306 int writeOutputIntent();
307 void writePageRoot();
308 void writeAttachmentRoot();
309 void writeFonts();
310 void embedFont(QFontSubset *font);
311 qreal calcUserUnit() const;
312
313 QVector<int> xrefPositions;
314 QDataStream* stream;
315 int streampos;
316
317 int writeImage(const QByteArray &data, int width, int height, int depth,
318 int maskObject, int softMaskObject, bool dct = false, bool isMono = false);
319 void writePage();
320
321 int addXrefEntry(int object, bool printostr = true);
322 void printString(const QString &string);
323 void xprintf(const char* fmt, ...);
324 inline void write(const QByteArray &data) {
325 stream->writeRawData(data.constData(), len: data.size());
326 streampos += data.size();
327 }
328
329 int writeCompressed(const char *src, int len);
330 inline int writeCompressed(const QByteArray &data) { return writeCompressed(src: data.constData(), len: data.length()); }
331 int writeCompressed(QIODevice *dev);
332
333 struct AttachmentInfo
334 {
335 AttachmentInfo (const QString &fileName, const QByteArray &data, const QString &mimeType)
336 : fileName(fileName), data(data), mimeType(mimeType) {}
337 QString fileName;
338 QByteArray data;
339 QString mimeType;
340 };
341
342 // various PDF objects
343 int pageRoot, embeddedfilesRoot, namesRoot, catalog, info, graphicsState, patternColorSpace;
344 QVector<uint> pages;
345 QHash<qint64, uint> imageCache;
346 QHash<QPair<uint, uint>, uint > alphaCache;
347 QVector<AttachmentInfo> fileCache;
348 QByteArray xmpDocumentMetadata;
349};
350
351QT_END_NAMESPACE
352
353#endif // QT_NO_PDF
354
355#endif // QPDF_P_H
356
357

source code of qtbase/src/gui/painting/qpdf_p.h