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