Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qpdf_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
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/qfontengine_p.h"
60 #include "QtGui/qprinter.h"
61 #include "private/qfontsubset_p.h"
62 #include "private/qpaintengine_alpha_p.h"
63 #include "qprintengine.h"
64 #include "qbuffer.h"
65
66 #ifndef QT_NO_PRINTER
67
68 QT_BEGIN_NAMESPACE
69
70 #define PPK_CupsOptions QPrintEngine::PrintEnginePropertyKey(0xfe00)
71 #define PPK_CupsPageRect QPrintEngine::PrintEnginePropertyKey(0xfe01)
72 #define PPK_CupsPaperRect QPrintEngine::PrintEnginePropertyKey(0xfe02)
73 #define PPK_CupsStringPageSize QPrintEngine::PrintEnginePropertyKey(0xfe03)
74
75 const char *qt_real_to_string(qreal val, char *buf);
76 const char *qt_int_to_string(int val, char *buf);
77
78 namespace QPdf {
79
80     class ByteStream
81     {
82     public:
83         // fileBacking means that ByteStream will buffer the contents on disk
84         // if the size exceeds a certain threshold. In this case, if a byte
85         // array was passed in, its contents may no longer correspond to the
86         // ByteStream contents.
87         explicit ByteStream(bool fileBacking = false);
88         explicit ByteStream(QByteArray *ba, bool fileBacking = false);
89         ~ByteStream();
90         ByteStream &operator <<(char chr);
91         ByteStream &operator <<(const char *str);
92         ByteStream &operator <<(const QByteArray &str);
93         ByteStream &operator <<(const ByteStream &src);
94         ByteStream &operator <<(qreal val);
95         ByteStream &operator <<(int val);
96         ByteStream &operator <<(const QPointF &p);
97         // Note that the stream may be invalidated by calls that insert data.
98         QIODevice *stream();
99         void clear();
100
101         static inline int maxMemorySize() { return 100000000; }
102         static inline int chunkSize()     { return 10000000; }
103
104     protected:
105         void constructor_helper(QIODevice *dev);
106         void constructor_helper(QByteArray *ba);
107
108     private:
109         void prepareBuffer();
110
111     private:
112         QIODevice *dev;
113         QByteArray ba;
114         bool fileBackingEnabled;
115         bool fileBackingActive;
116         bool handleDirty;
117     };
118
119     enum PathFlags {
120         ClipPath,
121         FillPath,
122         StrokePath,
123         FillAndStrokePath
124     };
125     QByteArray generatePath(const QPainterPath &path, const QTransform &matrix, PathFlags flags);
126     QByteArray generateMatrix(const QTransform &matrix);
127     QByteArray generateDashes(const QPen &pen);
128     QByteArray patternForBrush(const QBrush &b);
129 #ifdef USE_NATIVE_GRADIENTS
130     QByteArray generateLinearGradientShader(const QLinearGradient *lg, const QPointF *page_rect, bool alpha = false);
131 #endif
132
133     struct Stroker {
134         Stroker();
135         void setPen(const QPen &pen);
136         void strokePath(const QPainterPath &path);
137         ByteStream *stream;
138         bool first;
139         QTransform matrix;
140         bool cosmeticPen;
141     private:
142         QStroker basicStroker;
143         QDashStroker dashStroker;
144         QStrokerOps *stroker;
145     };
146
147     QByteArray ascii85Encode(const QByteArray &input);
148
149     const char *toHex(ushort u, char *buffer);
150     const char *toHex(uchar u, char *buffer);
151
152
153     struct PaperSize {
154         int width, height; // in postscript points
155     };
156     PaperSize paperSize(QPrinter::PaperSize paperSize);
157     const char *paperSizeToString(QPrinter::PaperSize paperSize);
158
159 }
160
161
162 class QPdfPage : public QPdf::ByteStream
163 {
164 public:
165     QPdfPage();
166
167     QVector<uint> images;
168     QVector<uint> graphicStates;
169     QVector<uint> patterns;
170     QVector<uint> fonts;
171     QVector<uint> annotations;
172
173     void streamImage(int w, int h, int object);
174
175     QSize pageSize;
176 private:
177 };
178
179
180 class QPdfBaseEnginePrivate;
181
182 class QPdfBaseEngine : public QAlphaPaintEngine, public QPrintEngine
183 {
184     Q_DECLARE_PRIVATE(QPdfBaseEngine)
185 public:
186     QPdfBaseEngine(QPdfBaseEnginePrivate &d, PaintEngineFeatures f);
187     ~QPdfBaseEngine() {}
188
189     // reimplementations QPaintEngine
190     bool begin(QPaintDevice *pdev);
191     bool end();
192
193     void drawPoints(const QPointF *points, int pointCount);
194     void drawLines(const QLineF *lines, int lineCount);
195     void drawRects(const QRectF *rects, int rectCount);
196     void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
197     void drawPath (const QPainterPath & path);
198
199     void drawTextItem(const QPointF &p, const QTextItem &textItem);
200
201     void updateState(const QPaintEngineState &state);
202
203     int metric(QPaintDevice::PaintDeviceMetric metricType) const;
204     // end reimplementations QPaintEngine
205
206     // Printer stuff...
207     bool newPage();
208     void setProperty(PrintEnginePropertyKey key, const QVariant &value);
209     QVariant property(PrintEnginePropertyKey key) const;
210
211     void setPen();
212     virtual void setBrush() = 0;
213     void setupGraphicsState(QPaintEngine::DirtyFlags flags);
214
215 private:
216     void updateClipPath(const QPainterPath & path, Qt::ClipOperation op);
217 };
218
219 class QPdfBaseEnginePrivate : public QAlphaPaintEnginePrivate
220 {
221     Q_DECLARE_PUBLIC(QPdfBaseEngine)
222 public:
223     QPdfBaseEnginePrivate(QPrinter::PrinterMode m);
224     ~QPdfBaseEnginePrivate();
225
226     bool openPrintDevice();
227     void closePrintDevice();
228
229
230     virtual void drawTextItem(const QPointF &p, const QTextItemInt &ti);
231     inline uint requestObject() { return currentObject++; }
232
233     QRect paperRect() const;
234     QRect pageRect() const;
235
236     bool postscript;
237     int currentObject;
238
239     QPdfPage* currentPage;
240     QPdf::Stroker stroker;
241
242     QPointF brushOrigin;
243     QBrush brush;
244     QPen pen;
245     QList<QPainterPath> clips;
246     bool clipEnabled;
247     bool allClipped;
248     bool hasPen;
249     bool hasBrush;
250     bool simplePen;
251     qreal opacity;
252     bool useAlphaEngine;
253
254     QHash<QFontEngine::FaceId, QFontSubset *> fonts;
255
256     QPaintDevice *pdev;
257
258     // the device the output is in the end streamed to.
259     QIODevice *outDevice;
260     int fd;
261
262     // printer options
263     QString outputFileName;
264     QString printerName;
265     QString printProgram;
266     QString selectionOption;
267     QString title;
268     QString creator;
269     QPrinter::DuplexMode duplex;
270     bool collate;
271     bool fullPage;
272     bool embedFonts;
273     int copies;
274     int resolution;
275     QPrinter::PageOrder pageOrder;
276     QPrinter::Orientation orientation;
277     QPrinter::PaperSize paperSize;
278     QPrinter::ColorMode colorMode;
279     QPrinter::PaperSource paperSource;
280
281     QStringList cupsOptions;
282     QRect cupsPaperRect;
283     QRect cupsPageRect;
284     QString cupsStringPageSize;
285     QSizeF customPaperSize; // in postscript points
286     bool hasCustomPageMargins;
287     qreal leftMargin, topMargin, rightMargin, bottomMargin;
288
289 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
290     QString cupsTempFile;
291 #endif
292 };
293
294 QT_END_NAMESPACE
295
296 #endif // QT_NO_PRINTER
297
298 #endif // QPDF_P_H
299