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 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui/qprintengine.h>
|
---|
43 |
|
---|
44 | #include <qiodevice.h>
|
---|
45 | #include <qpainter.h>
|
---|
46 | #include <qbitmap.h>
|
---|
47 | #include <qpainterpath.h>
|
---|
48 | #include <qpaintdevice.h>
|
---|
49 | #include <qfile.h>
|
---|
50 | #include <qdebug.h>
|
---|
51 | #include <qimagewriter.h>
|
---|
52 | #include <qbuffer.h>
|
---|
53 | #include <qdatetime.h>
|
---|
54 |
|
---|
55 | #ifndef QT_NO_PRINTER
|
---|
56 | #include <limits.h>
|
---|
57 | #include <math.h>
|
---|
58 | #ifndef QT_NO_COMPRESS
|
---|
59 | #include <zlib.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #if defined(Q_OS_WINCE)
|
---|
63 | #include "qwinfunctions_wince.h"
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #include "qprintengine_pdf_p.h"
|
---|
67 | #include "private/qdrawhelper_p.h"
|
---|
68 |
|
---|
69 | QT_BEGIN_NAMESPACE
|
---|
70 |
|
---|
71 | extern qint64 qt_pixmap_id(const QPixmap &pixmap);
|
---|
72 | extern qint64 qt_image_id(const QImage &image);
|
---|
73 |
|
---|
74 | //#define FONT_DUMP
|
---|
75 |
|
---|
76 | // might be helpful for smooth transforms of images
|
---|
77 | // Can't use it though, as gs generates completely wrong images if this is true.
|
---|
78 | static const bool interpolateImages = false;
|
---|
79 |
|
---|
80 | #ifdef QT_NO_COMPRESS
|
---|
81 | static const bool do_compress = false;
|
---|
82 | #else
|
---|
83 | static const bool do_compress = true;
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | QPdfPage::QPdfPage()
|
---|
87 | : QPdf::ByteStream(true) // Enable file backing
|
---|
88 | {
|
---|
89 | }
|
---|
90 |
|
---|
91 | void QPdfPage::streamImage(int w, int h, int object)
|
---|
92 | {
|
---|
93 | *this << w << "0 0 " << -h << "0 " << h << "cm /Im" << object << " Do\n";
|
---|
94 | if (!images.contains(object))
|
---|
95 | images.append(object);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | inline QPaintEngine::PaintEngineFeatures qt_pdf_decide_features()
|
---|
100 | {
|
---|
101 | QPaintEngine::PaintEngineFeatures f = QPaintEngine::AllFeatures;
|
---|
102 | f &= ~(QPaintEngine::PorterDuff | QPaintEngine::PerspectiveTransform
|
---|
103 | | QPaintEngine::ObjectBoundingModeGradients
|
---|
104 | #ifndef USE_NATIVE_GRADIENTS
|
---|
105 | | QPaintEngine::LinearGradientFill
|
---|
106 | #endif
|
---|
107 | | QPaintEngine::RadialGradientFill
|
---|
108 | | QPaintEngine::ConicalGradientFill);
|
---|
109 | return f;
|
---|
110 | }
|
---|
111 |
|
---|
112 | QPdfEngine::QPdfEngine(QPrinter::PrinterMode m)
|
---|
113 | : QPdfBaseEngine(*new QPdfEnginePrivate(m), qt_pdf_decide_features())
|
---|
114 | {
|
---|
115 | state = QPrinter::Idle;
|
---|
116 | }
|
---|
117 |
|
---|
118 | QPdfEngine::~QPdfEngine()
|
---|
119 | {
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool QPdfEngine::begin(QPaintDevice *pdev)
|
---|
123 | {
|
---|
124 | Q_D(QPdfEngine);
|
---|
125 |
|
---|
126 | if(!QPdfBaseEngine::begin(pdev)) {
|
---|
127 | state = QPrinter::Error;
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 | d->stream->setDevice(d->outDevice);
|
---|
131 |
|
---|
132 | d->streampos = 0;
|
---|
133 | d->hasPen = true;
|
---|
134 | d->hasBrush = false;
|
---|
135 | d->clipEnabled = false;
|
---|
136 | d->allClipped = false;
|
---|
137 |
|
---|
138 | d->xrefPositions.clear();
|
---|
139 | d->pageRoot = 0;
|
---|
140 | d->catalog = 0;
|
---|
141 | d->info = 0;
|
---|
142 | d->graphicsState = 0;
|
---|
143 | d->patternColorSpace = 0;
|
---|
144 |
|
---|
145 | d->pages.clear();
|
---|
146 | d->imageCache.clear();
|
---|
147 |
|
---|
148 | setActive(true);
|
---|
149 | state = QPrinter::Active;
|
---|
150 | d->writeHeader();
|
---|
151 | newPage();
|
---|
152 |
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 | bool QPdfEngine::end()
|
---|
157 | {
|
---|
158 | Q_D(QPdfEngine);
|
---|
159 | d->writeTail();
|
---|
160 |
|
---|
161 | d->stream->unsetDevice();
|
---|
162 | QPdfBaseEngine::end();
|
---|
163 | setActive(false);
|
---|
164 | state = QPrinter::Idle;
|
---|
165 | return true;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | void QPdfEngine::drawPixmap (const QRectF &rectangle, const QPixmap &pixmap, const QRectF &sr)
|
---|
170 | {
|
---|
171 | if (sr.isEmpty() || rectangle.isEmpty() || pixmap.isNull())
|
---|
172 | return;
|
---|
173 | Q_D(QPdfEngine);
|
---|
174 |
|
---|
175 | QBrush b = d->brush;
|
---|
176 |
|
---|
177 | QRect sourceRect = sr.toRect();
|
---|
178 | QPixmap pm = sourceRect != pixmap.rect() ? pixmap.copy(sourceRect) : pixmap;
|
---|
179 | QImage image = pm.toImage();
|
---|
180 | bool bitmap = true;
|
---|
181 | const int object = d->addImage(image, &bitmap, pm.cacheKey());
|
---|
182 | if (object < 0)
|
---|
183 | return;
|
---|
184 |
|
---|
185 | *d->currentPage << "q\n/GSa gs\n";
|
---|
186 | *d->currentPage
|
---|
187 | << QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(),
|
---|
188 | rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix));
|
---|
189 | if (bitmap) {
|
---|
190 | // set current pen as d->brush
|
---|
191 | d->brush = d->pen.brush();
|
---|
192 | }
|
---|
193 | setBrush();
|
---|
194 | d->currentPage->streamImage(image.width(), image.height(), object);
|
---|
195 | *d->currentPage << "Q\n";
|
---|
196 |
|
---|
197 | d->brush = b;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void QPdfEngine::drawImage(const QRectF &rectangle, const QImage &image, const QRectF &sr, Qt::ImageConversionFlags)
|
---|
201 | {
|
---|
202 | if (sr.isEmpty() || rectangle.isEmpty() || image.isNull())
|
---|
203 | return;
|
---|
204 | Q_D(QPdfEngine);
|
---|
205 |
|
---|
206 | QRect sourceRect = sr.toRect();
|
---|
207 | QImage im = sourceRect != image.rect() ? image.copy(sourceRect) : image;
|
---|
208 | bool bitmap = true;
|
---|
209 | const int object = d->addImage(im, &bitmap, im.cacheKey());
|
---|
210 | if (object < 0)
|
---|
211 | return;
|
---|
212 |
|
---|
213 | *d->currentPage << "q\n/GSa gs\n";
|
---|
214 | *d->currentPage
|
---|
215 | << QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(),
|
---|
216 | rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix));
|
---|
217 | setBrush();
|
---|
218 | d->currentPage->streamImage(im.width(), im.height(), object);
|
---|
219 | *d->currentPage << "Q\n";
|
---|
220 | }
|
---|
221 |
|
---|
222 | void QPdfEngine::drawTiledPixmap (const QRectF &rectangle, const QPixmap &pixmap, const QPointF &point)
|
---|
223 | {
|
---|
224 | Q_D(QPdfEngine);
|
---|
225 |
|
---|
226 | bool bitmap = (pixmap.depth() == 1);
|
---|
227 | QBrush b = d->brush;
|
---|
228 | QPointF bo = d->brushOrigin;
|
---|
229 | bool hp = d->hasPen;
|
---|
230 | d->hasPen = false;
|
---|
231 | bool hb = d->hasBrush;
|
---|
232 | d->hasBrush = true;
|
---|
233 |
|
---|
234 | d->brush = QBrush(pixmap);
|
---|
235 | if (bitmap)
|
---|
236 | // #### fix bitmap case where we have a brush pen
|
---|
237 | d->brush.setColor(d->pen.color());
|
---|
238 |
|
---|
239 | d->brushOrigin = -point;
|
---|
240 | *d->currentPage << "q\n";
|
---|
241 | setBrush();
|
---|
242 |
|
---|
243 | drawRects(&rectangle, 1);
|
---|
244 | *d->currentPage << "Q\n";
|
---|
245 |
|
---|
246 | d->hasPen = hp;
|
---|
247 | d->hasBrush = hb;
|
---|
248 | d->brush = b;
|
---|
249 | d->brushOrigin = bo;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | void QPdfEngine::setBrush()
|
---|
254 | {
|
---|
255 | Q_D(QPdfEngine);
|
---|
256 | Qt::BrushStyle style = d->brush.style();
|
---|
257 | if (style == Qt::NoBrush)
|
---|
258 | return;
|
---|
259 |
|
---|
260 | bool specifyColor;
|
---|
261 | int gStateObject = 0;
|
---|
262 | int patternObject = d->addBrushPattern(d->stroker.matrix, &specifyColor, &gStateObject);
|
---|
263 |
|
---|
264 | *d->currentPage << (patternObject ? "/PCSp cs " : "/CSp cs ");
|
---|
265 | if (specifyColor) {
|
---|
266 | QColor rgba = d->brush.color();
|
---|
267 | if (d->colorMode == QPrinter::GrayScale) {
|
---|
268 | qreal gray = qGray(rgba.rgba())/255.;
|
---|
269 | *d->currentPage << gray << gray << gray;
|
---|
270 | } else {
|
---|
271 | *d->currentPage << rgba.redF()
|
---|
272 | << rgba.greenF()
|
---|
273 | << rgba.blueF();
|
---|
274 | }
|
---|
275 | }
|
---|
276 | if (patternObject)
|
---|
277 | *d->currentPage << "/Pat" << patternObject;
|
---|
278 | *d->currentPage << "scn\n";
|
---|
279 |
|
---|
280 | if (gStateObject)
|
---|
281 | *d->currentPage << "/GState" << gStateObject << "gs\n";
|
---|
282 | else
|
---|
283 | *d->currentPage << "/GSa gs\n";
|
---|
284 | }
|
---|
285 |
|
---|
286 | QPaintEngine::Type QPdfEngine::type() const
|
---|
287 | {
|
---|
288 | return QPaintEngine::Pdf;
|
---|
289 | }
|
---|
290 |
|
---|
291 | bool QPdfEngine::newPage()
|
---|
292 | {
|
---|
293 | Q_D(QPdfEngine);
|
---|
294 | if (!isActive())
|
---|
295 | return false;
|
---|
296 | d->newPage();
|
---|
297 | return QPdfBaseEngine::newPage();
|
---|
298 | }
|
---|
299 |
|
---|
300 | QPdfEnginePrivate::QPdfEnginePrivate(QPrinter::PrinterMode m)
|
---|
301 | : QPdfBaseEnginePrivate(m)
|
---|
302 | {
|
---|
303 | streampos = 0;
|
---|
304 |
|
---|
305 | stream = new QDataStream;
|
---|
306 | pageOrder = QPrinter::FirstPageFirst;
|
---|
307 | orientation = QPrinter::Portrait;
|
---|
308 | fullPage = false;
|
---|
309 | }
|
---|
310 |
|
---|
311 | QPdfEnginePrivate::~QPdfEnginePrivate()
|
---|
312 | {
|
---|
313 | delete stream;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | #ifdef USE_NATIVE_GRADIENTS
|
---|
318 | int QPdfEnginePrivate::gradientBrush(const QBrush &b, const QMatrix &matrix, int *gStateObject)
|
---|
319 | {
|
---|
320 | const QGradient *gradient = b.gradient();
|
---|
321 | if (!gradient)
|
---|
322 | return 0;
|
---|
323 |
|
---|
324 | QTransform inv = matrix.inverted();
|
---|
325 | QPointF page_rect[4] = { inv.map(QPointF(0, 0)),
|
---|
326 | inv.map(QPointF(width_, 0)),
|
---|
327 | inv.map(QPointF(0, height_)),
|
---|
328 | inv.map(QPointF(width_, height_)) };
|
---|
329 |
|
---|
330 | bool opaque = b.isOpaque();
|
---|
331 |
|
---|
332 | QByteArray shader;
|
---|
333 | QByteArray alphaShader;
|
---|
334 | if (gradient->type() == QGradient::LinearGradient) {
|
---|
335 | const QLinearGradient *lg = static_cast<const QLinearGradient *>(gradient);
|
---|
336 | shader = QPdf::generateLinearGradientShader(lg, page_rect);
|
---|
337 | if (!opaque)
|
---|
338 | alphaShader = QPdf::generateLinearGradientShader(lg, page_rect, true);
|
---|
339 | } else {
|
---|
340 | // #############
|
---|
341 | return 0;
|
---|
342 | }
|
---|
343 | int shaderObject = addXrefEntry(-1);
|
---|
344 | write(shader);
|
---|
345 |
|
---|
346 | QByteArray str;
|
---|
347 | QPdf::ByteStream s(&str);
|
---|
348 | s << "<<\n"
|
---|
349 | "/Type /Pattern\n"
|
---|
350 | "/PatternType 2\n"
|
---|
351 | "/Shading " << shaderObject << "0 R\n"
|
---|
352 | "/Matrix ["
|
---|
353 | << matrix.m11()
|
---|
354 | << matrix.m12()
|
---|
355 | << matrix.m21()
|
---|
356 | << matrix.m22()
|
---|
357 | << matrix.dx()
|
---|
358 | << matrix.dy() << "]\n";
|
---|
359 | s << ">>\n"
|
---|
360 | "endobj\n";
|
---|
361 |
|
---|
362 | int patternObj = addXrefEntry(-1);
|
---|
363 | write(str);
|
---|
364 | currentPage->patterns.append(patternObj);
|
---|
365 |
|
---|
366 | if (!opaque) {
|
---|
367 | bool ca = true;
|
---|
368 | QGradientStops stops = gradient->stops();
|
---|
369 | int a = stops.at(0).second.alpha();
|
---|
370 | for (int i = 1; i < stops.size(); ++i) {
|
---|
371 | if (stops.at(i).second.alpha() != a) {
|
---|
372 | ca = false;
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | if (ca) {
|
---|
377 | *gStateObject = addConstantAlphaObject(stops.at(0).second.alpha());
|
---|
378 | } else {
|
---|
379 | int alphaShaderObject = addXrefEntry(-1);
|
---|
380 | write(alphaShader);
|
---|
381 |
|
---|
382 | QByteArray content;
|
---|
383 | QPdf::ByteStream c(&content);
|
---|
384 | c << "/Shader" << alphaShaderObject << "sh\n";
|
---|
385 |
|
---|
386 | QByteArray form;
|
---|
387 | QPdf::ByteStream f(&form);
|
---|
388 | f << "<<\n"
|
---|
389 | "/Type /XObject\n"
|
---|
390 | "/Subtype /Form\n"
|
---|
391 | "/BBox [0 0 " << width_ << height_ << "]\n"
|
---|
392 | "/Group <</S /Transparency >>\n"
|
---|
393 | "/Resources <<\n"
|
---|
394 | "/Shading << /Shader" << alphaShaderObject << alphaShaderObject << "0 R >>\n"
|
---|
395 | ">>\n";
|
---|
396 |
|
---|
397 | f << "/Length " << content.length() << "\n"
|
---|
398 | ">>\n"
|
---|
399 | "stream\n"
|
---|
400 | << content
|
---|
401 | << "endstream\n"
|
---|
402 | "endobj\n";
|
---|
403 |
|
---|
404 | int softMaskFormObject = addXrefEntry(-1);
|
---|
405 | write(form);
|
---|
406 | *gStateObject = addXrefEntry(-1);
|
---|
407 | xprintf("<< /SMask << /S /Alpha /G %d 0 R >> >>\n"
|
---|
408 | "endobj\n", softMaskFormObject);
|
---|
409 | currentPage->graphicStates.append(*gStateObject);
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | return patternObj;
|
---|
414 | }
|
---|
415 | #endif
|
---|
416 |
|
---|
417 | int QPdfEnginePrivate::addConstantAlphaObject(int brushAlpha, int penAlpha)
|
---|
418 | {
|
---|
419 | if (brushAlpha == 255 && penAlpha == 255)
|
---|
420 | return 0;
|
---|
421 | int object = alphaCache.value(QPair<uint, uint>(brushAlpha, penAlpha), 0);
|
---|
422 | if (!object) {
|
---|
423 | object = addXrefEntry(-1);
|
---|
424 | QByteArray alphaDef;
|
---|
425 | QPdf::ByteStream s(&alphaDef);
|
---|
426 | s << "<<\n/ca " << (brushAlpha/qreal(255.)) << '\n';
|
---|
427 | s << "/CA " << (penAlpha/qreal(255.)) << "\n>>";
|
---|
428 | xprintf("%s\nendobj\n", alphaDef.constData());
|
---|
429 | alphaCache.insert(QPair<uint, uint>(brushAlpha, penAlpha), object);
|
---|
430 | }
|
---|
431 | if (currentPage->graphicStates.indexOf(object) < 0)
|
---|
432 | currentPage->graphicStates.append(object);
|
---|
433 |
|
---|
434 | return object;
|
---|
435 | }
|
---|
436 |
|
---|
437 | int QPdfEnginePrivate::addBrushPattern(const QTransform &m, bool *specifyColor, int *gStateObject)
|
---|
438 | {
|
---|
439 | int paintType = 2; // Uncolored tiling
|
---|
440 | int w = 8;
|
---|
441 | int h = 8;
|
---|
442 |
|
---|
443 | *specifyColor = true;
|
---|
444 | *gStateObject = 0;
|
---|
445 |
|
---|
446 | QTransform matrix = m;
|
---|
447 | matrix.translate(brushOrigin.x(), brushOrigin.y());
|
---|
448 | matrix = matrix * pageMatrix();
|
---|
449 | //qDebug() << brushOrigin << matrix;
|
---|
450 |
|
---|
451 | Qt::BrushStyle style = brush.style();
|
---|
452 | if (style == Qt::LinearGradientPattern) {// && style <= Qt::ConicalGradientPattern) {
|
---|
453 | #ifdef USE_NATIVE_GRADIENTS
|
---|
454 | *specifyColor = false;
|
---|
455 | return gradientBrush(b, matrix, gStateObject);
|
---|
456 | #else
|
---|
457 | return 0;
|
---|
458 | #endif
|
---|
459 | }
|
---|
460 |
|
---|
461 | if ((!brush.isOpaque() && brush.style() < Qt::LinearGradientPattern) || opacity != 1.0)
|
---|
462 | *gStateObject = addConstantAlphaObject(qRound(brush.color().alpha() * opacity),
|
---|
463 | qRound(pen.color().alpha() * opacity));
|
---|
464 |
|
---|
465 | int imageObject = -1;
|
---|
466 | QByteArray pattern = QPdf::patternForBrush(brush);
|
---|
467 | if (pattern.isEmpty()) {
|
---|
468 | if (brush.style() != Qt::TexturePattern)
|
---|
469 | return 0;
|
---|
470 | QImage image = brush.texture().toImage();
|
---|
471 | bool bitmap = true;
|
---|
472 | imageObject = addImage(image, &bitmap, qt_pixmap_id(brush.texture()));
|
---|
473 | if (imageObject != -1) {
|
---|
474 | QImage::Format f = image.format();
|
---|
475 | if (f != QImage::Format_MonoLSB && f != QImage::Format_Mono) {
|
---|
476 | paintType = 1; // Colored tiling
|
---|
477 | *specifyColor = false;
|
---|
478 | }
|
---|
479 | w = image.width();
|
---|
480 | h = image.height();
|
---|
481 | QTransform m(w, 0, 0, -h, 0, h);
|
---|
482 | QPdf::ByteStream s(&pattern);
|
---|
483 | s << QPdf::generateMatrix(m);
|
---|
484 | s << "/Im" << imageObject << " Do\n";
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | QByteArray str;
|
---|
489 | QPdf::ByteStream s(&str);
|
---|
490 | s << "<<\n"
|
---|
491 | "/Type /Pattern\n"
|
---|
492 | "/PatternType 1\n"
|
---|
493 | "/PaintType " << paintType << "\n"
|
---|
494 | "/TilingType 1\n"
|
---|
495 | "/BBox [0 0 " << w << h << "]\n"
|
---|
496 | "/XStep " << w << "\n"
|
---|
497 | "/YStep " << h << "\n"
|
---|
498 | "/Matrix ["
|
---|
499 | << matrix.m11()
|
---|
500 | << matrix.m12()
|
---|
501 | << matrix.m21()
|
---|
502 | << matrix.m22()
|
---|
503 | << matrix.dx()
|
---|
504 | << matrix.dy() << "]\n"
|
---|
505 | "/Resources \n<< "; // open resource tree
|
---|
506 | if (imageObject > 0) {
|
---|
507 | s << "/XObject << /Im" << imageObject << ' ' << imageObject << "0 R >> ";
|
---|
508 | }
|
---|
509 | s << ">>\n"
|
---|
510 | "/Length " << pattern.length() << "\n"
|
---|
511 | ">>\n"
|
---|
512 | "stream\n"
|
---|
513 | << pattern
|
---|
514 | << "endstream\n"
|
---|
515 | "endobj\n";
|
---|
516 |
|
---|
517 | int patternObj = addXrefEntry(-1);
|
---|
518 | write(str);
|
---|
519 | currentPage->patterns.append(patternObj);
|
---|
520 | return patternObj;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*!
|
---|
524 | * Adds an image to the pdf and return the pdf-object id. Returns -1 if adding the image failed.
|
---|
525 | */
|
---|
526 | int QPdfEnginePrivate::addImage(const QImage &img, bool *bitmap, qint64 serial_no)
|
---|
527 | {
|
---|
528 | if (img.isNull())
|
---|
529 | return -1;
|
---|
530 |
|
---|
531 | int object = imageCache.value(serial_no);
|
---|
532 | if(object)
|
---|
533 | return object;
|
---|
534 |
|
---|
535 | QImage image = img;
|
---|
536 | QImage::Format format = image.format();
|
---|
537 | if (image.depth() == 1 && *bitmap && img.colorTable().size() == 0) {
|
---|
538 | if (format == QImage::Format_MonoLSB)
|
---|
539 | image = image.convertToFormat(QImage::Format_Mono);
|
---|
540 | format = QImage::Format_Mono;
|
---|
541 | } else {
|
---|
542 | *bitmap = false;
|
---|
543 | if (format != QImage::Format_RGB32 && format != QImage::Format_ARGB32) {
|
---|
544 | image = image.convertToFormat(QImage::Format_ARGB32);
|
---|
545 | format = QImage::Format_ARGB32;
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | int w = image.width();
|
---|
550 | int h = image.height();
|
---|
551 | int d = image.depth();
|
---|
552 |
|
---|
553 | if (format == QImage::Format_Mono) {
|
---|
554 | int bytesPerLine = (w + 7) >> 3;
|
---|
555 | QByteArray data;
|
---|
556 | data.resize(bytesPerLine * h);
|
---|
557 | char *rawdata = data.data();
|
---|
558 | for (int y = 0; y < h; ++y) {
|
---|
559 | memcpy(rawdata, image.scanLine(y), bytesPerLine);
|
---|
560 | rawdata += bytesPerLine;
|
---|
561 | }
|
---|
562 | object = writeImage(data, w, h, d, 0, 0);
|
---|
563 | } else {
|
---|
564 | QByteArray softMaskData;
|
---|
565 | bool dct = false;
|
---|
566 | QByteArray imageData;
|
---|
567 | bool hasAlpha = false;
|
---|
568 | bool hasMask = false;
|
---|
569 |
|
---|
570 | if (QImageWriter::supportedImageFormats().contains("jpeg") && colorMode != QPrinter::GrayScale) {
|
---|
571 | QBuffer buffer(&imageData);
|
---|
572 | QImageWriter writer(&buffer, "jpeg");
|
---|
573 | writer.setQuality(94);
|
---|
574 | writer.write(image);
|
---|
575 | dct = true;
|
---|
576 |
|
---|
577 | if (format != QImage::Format_RGB32) {
|
---|
578 | softMaskData.resize(w * h);
|
---|
579 | uchar *sdata = (uchar *)softMaskData.data();
|
---|
580 | for (int y = 0; y < h; ++y) {
|
---|
581 | const QRgb *rgb = (const QRgb *)image.scanLine(y);
|
---|
582 | for (int x = 0; x < w; ++x) {
|
---|
583 | uchar alpha = qAlpha(*rgb);
|
---|
584 | *sdata++ = alpha;
|
---|
585 | hasMask |= (alpha < 255);
|
---|
586 | hasAlpha |= (alpha != 0 && alpha != 255);
|
---|
587 | ++rgb;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 | } else {
|
---|
592 | imageData.resize(colorMode == QPrinter::GrayScale ? w * h : 3 * w * h);
|
---|
593 | uchar *data = (uchar *)imageData.data();
|
---|
594 | softMaskData.resize(w * h);
|
---|
595 | uchar *sdata = (uchar *)softMaskData.data();
|
---|
596 | for (int y = 0; y < h; ++y) {
|
---|
597 | const QRgb *rgb = (const QRgb *)image.scanLine(y);
|
---|
598 | if (colorMode == QPrinter::GrayScale) {
|
---|
599 | for (int x = 0; x < w; ++x) {
|
---|
600 | *(data++) = qGray(*rgb);
|
---|
601 | uchar alpha = qAlpha(*rgb);
|
---|
602 | *sdata++ = alpha;
|
---|
603 | hasMask |= (alpha < 255);
|
---|
604 | hasAlpha |= (alpha != 0 && alpha != 255);
|
---|
605 | ++rgb;
|
---|
606 | }
|
---|
607 | } else {
|
---|
608 | for (int x = 0; x < w; ++x) {
|
---|
609 | *(data++) = qRed(*rgb);
|
---|
610 | *(data++) = qGreen(*rgb);
|
---|
611 | *(data++) = qBlue(*rgb);
|
---|
612 | uchar alpha = qAlpha(*rgb);
|
---|
613 | *sdata++ = alpha;
|
---|
614 | hasMask |= (alpha < 255);
|
---|
615 | hasAlpha |= (alpha != 0 && alpha != 255);
|
---|
616 | ++rgb;
|
---|
617 | }
|
---|
618 | }
|
---|
619 | }
|
---|
620 | if (format == QImage::Format_RGB32)
|
---|
621 | hasAlpha = hasMask = false;
|
---|
622 | }
|
---|
623 | int maskObject = 0;
|
---|
624 | int softMaskObject = 0;
|
---|
625 | if (hasAlpha) {
|
---|
626 | softMaskObject = writeImage(softMaskData, w, h, 8, 0, 0);
|
---|
627 | } else if (hasMask) {
|
---|
628 | // dither the soft mask to 1bit and add it. This also helps PDF viewers
|
---|
629 | // without transparency support
|
---|
630 | int bytesPerLine = (w + 7) >> 3;
|
---|
631 | QByteArray mask(bytesPerLine * h, 0);
|
---|
632 | uchar *mdata = (uchar *)mask.data();
|
---|
633 | const uchar *sdata = (const uchar *)softMaskData.constData();
|
---|
634 | for (int y = 0; y < h; ++y) {
|
---|
635 | for (int x = 0; x < w; ++x) {
|
---|
636 | if (*sdata)
|
---|
637 | mdata[x>>3] |= (0x80 >> (x&7));
|
---|
638 | ++sdata;
|
---|
639 | }
|
---|
640 | mdata += bytesPerLine;
|
---|
641 | }
|
---|
642 | maskObject = writeImage(mask, w, h, 1, 0, 0);
|
---|
643 | }
|
---|
644 | object = writeImage(imageData, w, h, colorMode == QPrinter::GrayScale ? 8 : 32,
|
---|
645 | maskObject, softMaskObject, dct);
|
---|
646 | }
|
---|
647 | imageCache.insert(serial_no, object);
|
---|
648 | return object;
|
---|
649 | }
|
---|
650 |
|
---|
651 | void QPdfEnginePrivate::drawTextItem(const QPointF &p, const QTextItemInt &ti)
|
---|
652 | {
|
---|
653 | if (ti.charFormat.isAnchor()) {
|
---|
654 | qreal size = ti.fontEngine->fontDef.pixelSize;
|
---|
655 | #ifdef Q_WS_WIN
|
---|
656 | if (ti.fontEngine->type() == QFontEngine::Win) {
|
---|
657 | QFontEngineWin *fe = static_cast<QFontEngineWin *>(ti.fontEngine);
|
---|
658 | size = fe->tm.tmHeight;
|
---|
659 | }
|
---|
660 | #endif
|
---|
661 | int synthesized = ti.fontEngine->synthesized();
|
---|
662 | qreal stretch = synthesized & QFontEngine::SynthesizedStretch ? ti.fontEngine->fontDef.stretch/100. : 1.;
|
---|
663 |
|
---|
664 | QTransform trans;
|
---|
665 | // Build text rendering matrix (Trm). We need it to map the text area to user
|
---|
666 | // space units on the PDF page.
|
---|
667 | trans = QTransform(size*stretch, 0, 0, size, 0, 0);
|
---|
668 | // Apply text matrix (Tm).
|
---|
669 | trans *= QTransform(1,0,0,-1,p.x(),p.y());
|
---|
670 | // Apply page displacement (Identity for first page).
|
---|
671 | trans *= stroker.matrix;
|
---|
672 | // Apply Current Transformation Matrix (CTM)
|
---|
673 | trans *= pageMatrix();
|
---|
674 | qreal x1, y1, x2, y2;
|
---|
675 | trans.map(0, 0, &x1, &y1);
|
---|
676 | trans.map(ti.width.toReal()/size, (ti.ascent.toReal()-ti.descent.toReal())/size, &x2, &y2);
|
---|
677 |
|
---|
678 | uint annot = addXrefEntry(-1);
|
---|
679 | #ifdef Q_DEBUG_PDF_LINKS
|
---|
680 | xprintf("<<\n/Type /Annot\n/Subtype /Link\n/Rect [%f %f %f %f]\n/Border [16 16 1]\n/A <<\n",
|
---|
681 | #else
|
---|
682 | xprintf("<<\n/Type /Annot\n/Subtype /Link\n/Rect [%f %f %f %f]\n/Border [0 0 0]\n/A <<\n",
|
---|
683 | #endif
|
---|
684 | static_cast<double>(x1),
|
---|
685 | static_cast<double>(y1),
|
---|
686 | static_cast<double>(x2),
|
---|
687 | static_cast<double>(y2));
|
---|
688 | xprintf("/Type /Action\n/S /URI\n/URI (%s)\n",
|
---|
689 | ti.charFormat.anchorHref().toLatin1().constData());
|
---|
690 | xprintf(">>\n>>\n");
|
---|
691 | xprintf("endobj\n");
|
---|
692 |
|
---|
693 | if (!currentPage->annotations.contains(annot)) {
|
---|
694 | currentPage->annotations.append(annot);
|
---|
695 | }
|
---|
696 | }
|
---|
697 |
|
---|
698 | QPdfBaseEnginePrivate::drawTextItem(p, ti);
|
---|
699 | }
|
---|
700 |
|
---|
701 | QTransform QPdfEnginePrivate::pageMatrix() const
|
---|
702 | {
|
---|
703 | qreal scale = 72./resolution;
|
---|
704 | QTransform tmp(scale, 0.0, 0.0, -scale, 0.0, height());
|
---|
705 | if (!fullPage) {
|
---|
706 | QRect r = pageRect();
|
---|
707 | tmp.translate(r.left(), r.top());
|
---|
708 | }
|
---|
709 | return tmp;
|
---|
710 | }
|
---|
711 |
|
---|
712 | void QPdfEnginePrivate::newPage()
|
---|
713 | {
|
---|
714 | if (currentPage && currentPage->pageSize.isEmpty())
|
---|
715 | currentPage->pageSize = QSize(width(), height());
|
---|
716 | writePage();
|
---|
717 |
|
---|
718 | delete currentPage;
|
---|
719 | currentPage = new QPdfPage;
|
---|
720 | currentPage->pageSize = QSize(width(), height());
|
---|
721 | stroker.stream = currentPage;
|
---|
722 | pages.append(requestObject());
|
---|
723 |
|
---|
724 | *currentPage << "/GSa gs /CSp cs /CSp CS\n"
|
---|
725 | << QPdf::generateMatrix(pageMatrix())
|
---|
726 | << "q q\n";
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | // For strings up to 10000 bytes only !
|
---|
731 | void QPdfEnginePrivate::xprintf(const char* fmt, ...)
|
---|
732 | {
|
---|
733 | if (!stream)
|
---|
734 | return;
|
---|
735 |
|
---|
736 | const int msize = 10000;
|
---|
737 | char buf[msize];
|
---|
738 |
|
---|
739 | va_list args;
|
---|
740 | va_start(args, fmt);
|
---|
741 | int bufsize = qvsnprintf(buf, msize, fmt, args);
|
---|
742 |
|
---|
743 | Q_ASSERT(bufsize<msize);
|
---|
744 |
|
---|
745 | va_end(args);
|
---|
746 |
|
---|
747 | stream->writeRawData(buf, bufsize);
|
---|
748 | streampos += bufsize;
|
---|
749 | }
|
---|
750 |
|
---|
751 | int QPdfEnginePrivate::writeCompressed(QIODevice *dev)
|
---|
752 | {
|
---|
753 | #ifndef QT_NO_COMPRESS
|
---|
754 | if (do_compress) {
|
---|
755 | int size = QPdfPage::chunkSize();
|
---|
756 | int sum = 0;
|
---|
757 | ::z_stream zStruct;
|
---|
758 | zStruct.zalloc = Z_NULL;
|
---|
759 | zStruct.zfree = Z_NULL;
|
---|
760 | zStruct.opaque = Z_NULL;
|
---|
761 | if (::deflateInit(&zStruct, Z_DEFAULT_COMPRESSION) != Z_OK) {
|
---|
762 | qWarning("QPdfStream::writeCompressed: Error in deflateInit()");
|
---|
763 | return sum;
|
---|
764 | }
|
---|
765 | zStruct.avail_in = 0;
|
---|
766 | QByteArray in, out;
|
---|
767 | out.resize(size);
|
---|
768 | while (!dev->atEnd() || zStruct.avail_in != 0) {
|
---|
769 | if (zStruct.avail_in == 0) {
|
---|
770 | in = dev->read(size);
|
---|
771 | zStruct.avail_in = in.size();
|
---|
772 | zStruct.next_in = reinterpret_cast<unsigned char*>(in.data());
|
---|
773 | if (in.size() <= 0) {
|
---|
774 | qWarning("QPdfStream::writeCompressed: Error in read()");
|
---|
775 | ::deflateEnd(&zStruct);
|
---|
776 | return sum;
|
---|
777 | }
|
---|
778 | }
|
---|
779 | zStruct.next_out = reinterpret_cast<unsigned char*>(out.data());
|
---|
780 | zStruct.avail_out = out.size();
|
---|
781 | if (::deflate(&zStruct, 0) != Z_OK) {
|
---|
782 | qWarning("QPdfStream::writeCompressed: Error in deflate()");
|
---|
783 | ::deflateEnd(&zStruct);
|
---|
784 | return sum;
|
---|
785 | }
|
---|
786 | int written = out.size() - zStruct.avail_out;
|
---|
787 | stream->writeRawData(out.constData(), written);
|
---|
788 | streampos += written;
|
---|
789 | sum += written;
|
---|
790 | }
|
---|
791 | int ret;
|
---|
792 | do {
|
---|
793 | zStruct.next_out = reinterpret_cast<unsigned char*>(out.data());
|
---|
794 | zStruct.avail_out = out.size();
|
---|
795 | ret = ::deflate(&zStruct, Z_FINISH);
|
---|
796 | if (ret != Z_OK && ret != Z_STREAM_END) {
|
---|
797 | qWarning("QPdfStream::writeCompressed: Error in deflate()");
|
---|
798 | ::deflateEnd(&zStruct);
|
---|
799 | return sum;
|
---|
800 | }
|
---|
801 | int written = out.size() - zStruct.avail_out;
|
---|
802 | stream->writeRawData(out.constData(), written);
|
---|
803 | streampos += written;
|
---|
804 | sum += written;
|
---|
805 | } while (ret == Z_OK);
|
---|
806 |
|
---|
807 | ::deflateEnd(&zStruct);
|
---|
808 |
|
---|
809 | return sum;
|
---|
810 | } else
|
---|
811 | #endif
|
---|
812 | {
|
---|
813 | QByteArray arr;
|
---|
814 | int sum = 0;
|
---|
815 | while (!dev->atEnd()) {
|
---|
816 | arr = dev->read(QPdfPage::chunkSize());
|
---|
817 | stream->writeRawData(arr.constData(), arr.size());
|
---|
818 | streampos += arr.size();
|
---|
819 | sum += arr.size();
|
---|
820 | }
|
---|
821 | return sum;
|
---|
822 | }
|
---|
823 | }
|
---|
824 |
|
---|
825 | int QPdfEnginePrivate::writeCompressed(const char *src, int len)
|
---|
826 | {
|
---|
827 | #ifndef QT_NO_COMPRESS
|
---|
828 | if(do_compress) {
|
---|
829 | uLongf destLen = len + len/100 + 13; // zlib requirement
|
---|
830 | Bytef* dest = new Bytef[destLen];
|
---|
831 | if (Z_OK == ::compress(dest, &destLen, (const Bytef*) src, (uLongf)len)) {
|
---|
832 | stream->writeRawData((const char*)dest, destLen);
|
---|
833 | } else {
|
---|
834 | qWarning("QPdfStream::writeCompressed: Error in compress()");
|
---|
835 | destLen = 0;
|
---|
836 | }
|
---|
837 | delete [] dest;
|
---|
838 | len = destLen;
|
---|
839 | } else
|
---|
840 | #endif
|
---|
841 | {
|
---|
842 | stream->writeRawData(src,len);
|
---|
843 | }
|
---|
844 | streampos += len;
|
---|
845 | return len;
|
---|
846 | }
|
---|
847 |
|
---|
848 | int QPdfEnginePrivate::writeImage(const QByteArray &data, int width, int height, int depth,
|
---|
849 | int maskObject, int softMaskObject, bool dct)
|
---|
850 | {
|
---|
851 | int image = addXrefEntry(-1);
|
---|
852 | xprintf("<<\n"
|
---|
853 | "/Type /XObject\n"
|
---|
854 | "/Subtype /Image\n"
|
---|
855 | "/Width %d\n"
|
---|
856 | "/Height %d\n", width, height);
|
---|
857 |
|
---|
858 | if (depth == 1) {
|
---|
859 | xprintf("/ImageMask true\n"
|
---|
860 | "/Decode [1 0]\n");
|
---|
861 | } else {
|
---|
862 | xprintf("/BitsPerComponent 8\n"
|
---|
863 | "/ColorSpace %s\n", (depth == 32) ? "/DeviceRGB" : "/DeviceGray");
|
---|
864 | }
|
---|
865 | if (maskObject > 0)
|
---|
866 | xprintf("/Mask %d 0 R\n", maskObject);
|
---|
867 | if (softMaskObject > 0)
|
---|
868 | xprintf("/SMask %d 0 R\n", softMaskObject);
|
---|
869 |
|
---|
870 | int lenobj = requestObject();
|
---|
871 | xprintf("/Length %d 0 R\n", lenobj);
|
---|
872 | if (interpolateImages)
|
---|
873 | xprintf("/Interpolate true\n");
|
---|
874 | int len = 0;
|
---|
875 | if (dct) {
|
---|
876 | //qDebug() << "DCT";
|
---|
877 | xprintf("/Filter /DCTDecode\n>>\nstream\n");
|
---|
878 | write(data);
|
---|
879 | len = data.length();
|
---|
880 | } else {
|
---|
881 | if (do_compress)
|
---|
882 | xprintf("/Filter /FlateDecode\n>>\nstream\n");
|
---|
883 | else
|
---|
884 | xprintf(">>\nstream\n");
|
---|
885 | len = writeCompressed(data);
|
---|
886 | }
|
---|
887 | xprintf("endstream\n"
|
---|
888 | "endobj\n");
|
---|
889 | addXrefEntry(lenobj);
|
---|
890 | xprintf("%d\n"
|
---|
891 | "endobj\n", len);
|
---|
892 | return image;
|
---|
893 | }
|
---|
894 |
|
---|
895 |
|
---|
896 | void QPdfEnginePrivate::writeHeader()
|
---|
897 | {
|
---|
898 | addXrefEntry(0,false);
|
---|
899 |
|
---|
900 | xprintf("%%PDF-1.4\n");
|
---|
901 |
|
---|
902 | writeInfo();
|
---|
903 |
|
---|
904 | catalog = addXrefEntry(-1);
|
---|
905 | pageRoot = requestObject();
|
---|
906 | xprintf("<<\n"
|
---|
907 | "/Type /Catalog\n"
|
---|
908 | "/Pages %d 0 R\n"
|
---|
909 | ">>\n"
|
---|
910 | "endobj\n", pageRoot);
|
---|
911 |
|
---|
912 | // graphics state
|
---|
913 | graphicsState = addXrefEntry(-1);
|
---|
914 | xprintf("<<\n"
|
---|
915 | "/Type /ExtGState\n"
|
---|
916 | "/SA true\n"
|
---|
917 | "/SM 0.02\n"
|
---|
918 | "/ca 1.0\n"
|
---|
919 | "/CA 1.0\n"
|
---|
920 | "/AIS false\n"
|
---|
921 | "/SMask /None"
|
---|
922 | ">>\n"
|
---|
923 | "endobj\n");
|
---|
924 |
|
---|
925 | // color space for pattern
|
---|
926 | patternColorSpace = addXrefEntry(-1);
|
---|
927 | xprintf("[/Pattern /DeviceRGB]\n"
|
---|
928 | "endobj\n");
|
---|
929 | }
|
---|
930 |
|
---|
931 | void QPdfEnginePrivate::writeInfo()
|
---|
932 | {
|
---|
933 | info = addXrefEntry(-1);
|
---|
934 | xprintf("<<\n/Title ");
|
---|
935 | printString(title);
|
---|
936 | xprintf("\n/Creator ");
|
---|
937 | printString(creator);
|
---|
938 | xprintf("\n/Producer ");
|
---|
939 | printString(QString::fromLatin1("Qt " QT_VERSION_STR " (C) 2010 Nokia Corporation and/or its subsidiary(-ies)"));
|
---|
940 | QDateTime now = QDateTime::currentDateTime().toUTC();
|
---|
941 | QTime t = now.time();
|
---|
942 | QDate d = now.date();
|
---|
943 | xprintf("\n/CreationDate (D:%d%02d%02d%02d%02d%02d)\n",
|
---|
944 | d.year(),
|
---|
945 | d.month(),
|
---|
946 | d.day(),
|
---|
947 | t.hour(),
|
---|
948 | t.minute(),
|
---|
949 | t.second());
|
---|
950 | xprintf(">>\n"
|
---|
951 | "endobj\n");
|
---|
952 | }
|
---|
953 |
|
---|
954 | void QPdfEnginePrivate::writePageRoot()
|
---|
955 | {
|
---|
956 | addXrefEntry(pageRoot);
|
---|
957 |
|
---|
958 | xprintf("<<\n"
|
---|
959 | "/Type /Pages\n"
|
---|
960 | "/Kids \n"
|
---|
961 | "[\n");
|
---|
962 | int size = pages.size();
|
---|
963 | for (int i = 0; i < size; ++i)
|
---|
964 | xprintf("%d 0 R\n", pages[i]);
|
---|
965 | xprintf("]\n");
|
---|
966 |
|
---|
967 | //xprintf("/Group <</S /Transparency /I true /K false>>\n");
|
---|
968 | xprintf("/Count %d\n", pages.size());
|
---|
969 |
|
---|
970 | xprintf("/ProcSet [/PDF /Text /ImageB /ImageC]\n"
|
---|
971 | ">>\n"
|
---|
972 | "endobj\n");
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | void QPdfEnginePrivate::embedFont(QFontSubset *font)
|
---|
977 | {
|
---|
978 | //qDebug() << "embedFont" << font->object_id;
|
---|
979 | int fontObject = font->object_id;
|
---|
980 | QByteArray fontData = font->toTruetype();
|
---|
981 | #ifdef FONT_DUMP
|
---|
982 | static int i = 0;
|
---|
983 | QString fileName("font%1.ttf");
|
---|
984 | fileName = fileName.arg(i++);
|
---|
985 | QFile ff(fileName);
|
---|
986 | ff.open(QFile::WriteOnly);
|
---|
987 | ff.write(fontData);
|
---|
988 | ff.close();
|
---|
989 | #endif
|
---|
990 |
|
---|
991 | int fontDescriptor = requestObject();
|
---|
992 | int fontstream = requestObject();
|
---|
993 | int cidfont = requestObject();
|
---|
994 | int toUnicode = requestObject();
|
---|
995 |
|
---|
996 | QFontEngine::Properties properties = font->fontEngine->properties();
|
---|
997 |
|
---|
998 | {
|
---|
999 | qreal scale = 1000/properties.emSquare.toReal();
|
---|
1000 | addXrefEntry(fontDescriptor);
|
---|
1001 | QByteArray descriptor;
|
---|
1002 | QPdf::ByteStream s(&descriptor);
|
---|
1003 | s << "<< /Type /FontDescriptor\n"
|
---|
1004 | "/FontName /Q";
|
---|
1005 | int tag = fontDescriptor;
|
---|
1006 | for (int i = 0; i < 5; ++i) {
|
---|
1007 | s << (char)('A' + (tag % 26));
|
---|
1008 | tag /= 26;
|
---|
1009 | }
|
---|
1010 | s << '+' << properties.postscriptName << "\n"
|
---|
1011 | "/Flags " << 4 << "\n"
|
---|
1012 | "/FontBBox ["
|
---|
1013 | << properties.boundingBox.x()*scale
|
---|
1014 | << -(properties.boundingBox.y() + properties.boundingBox.height())*scale
|
---|
1015 | << (properties.boundingBox.x() + properties.boundingBox.width())*scale
|
---|
1016 | << -properties.boundingBox.y()*scale << "]\n"
|
---|
1017 | "/ItalicAngle " << properties.italicAngle.toReal() << "\n"
|
---|
1018 | "/Ascent " << properties.ascent.toReal()*scale << "\n"
|
---|
1019 | "/Descent " << -properties.descent.toReal()*scale << "\n"
|
---|
1020 | "/CapHeight " << properties.capHeight.toReal()*scale << "\n"
|
---|
1021 | "/StemV " << properties.lineWidth.toReal()*scale << "\n"
|
---|
1022 | "/FontFile2 " << fontstream << "0 R\n"
|
---|
1023 | ">> endobj\n";
|
---|
1024 | write(descriptor);
|
---|
1025 | }
|
---|
1026 | {
|
---|
1027 | addXrefEntry(fontstream);
|
---|
1028 | QByteArray header;
|
---|
1029 | QPdf::ByteStream s(&header);
|
---|
1030 |
|
---|
1031 | int length_object = requestObject();
|
---|
1032 | s << "<<\n"
|
---|
1033 | "/Length1 " << fontData.size() << "\n"
|
---|
1034 | "/Length " << length_object << "0 R\n";
|
---|
1035 | if (do_compress)
|
---|
1036 | s << "/Filter /FlateDecode\n";
|
---|
1037 | s << ">>\n"
|
---|
1038 | "stream\n";
|
---|
1039 | write(header);
|
---|
1040 | int len = writeCompressed(fontData);
|
---|
1041 | write("endstream\n"
|
---|
1042 | "endobj\n");
|
---|
1043 | addXrefEntry(length_object);
|
---|
1044 | xprintf("%d\n"
|
---|
1045 | "endobj\n", len);
|
---|
1046 | }
|
---|
1047 | {
|
---|
1048 | addXrefEntry(cidfont);
|
---|
1049 | QByteArray cid;
|
---|
1050 | QPdf::ByteStream s(&cid);
|
---|
1051 | s << "<< /Type /Font\n"
|
---|
1052 | "/Subtype /CIDFontType2\n"
|
---|
1053 | "/BaseFont /" << properties.postscriptName << "\n"
|
---|
1054 | "/CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >>\n"
|
---|
1055 | "/FontDescriptor " << fontDescriptor << "0 R\n"
|
---|
1056 | "/CIDToGIDMap /Identity\n"
|
---|
1057 | << font->widthArray() <<
|
---|
1058 | ">>\n"
|
---|
1059 | "endobj\n";
|
---|
1060 | write(cid);
|
---|
1061 | }
|
---|
1062 | {
|
---|
1063 | addXrefEntry(toUnicode);
|
---|
1064 | QByteArray touc = font->createToUnicodeMap();
|
---|
1065 | xprintf("<< /Length %d >>\n"
|
---|
1066 | "stream\n", touc.length());
|
---|
1067 | write(touc);
|
---|
1068 | write("endstream\n"
|
---|
1069 | "endobj\n");
|
---|
1070 | }
|
---|
1071 | {
|
---|
1072 | addXrefEntry(fontObject);
|
---|
1073 | QByteArray font;
|
---|
1074 | QPdf::ByteStream s(&font);
|
---|
1075 | s << "<< /Type /Font\n"
|
---|
1076 | "/Subtype /Type0\n"
|
---|
1077 | "/BaseFont /" << properties.postscriptName << "\n"
|
---|
1078 | "/Encoding /Identity-H\n"
|
---|
1079 | "/DescendantFonts [" << cidfont << "0 R]\n"
|
---|
1080 | "/ToUnicode " << toUnicode << "0 R"
|
---|
1081 | ">>\n"
|
---|
1082 | "endobj\n";
|
---|
1083 | write(font);
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 |
|
---|
1088 | void QPdfEnginePrivate::writeFonts()
|
---|
1089 | {
|
---|
1090 | for (QHash<QFontEngine::FaceId, QFontSubset *>::iterator it = fonts.begin(); it != fonts.end(); ++it) {
|
---|
1091 | embedFont(*it);
|
---|
1092 | delete *it;
|
---|
1093 | }
|
---|
1094 | fonts.clear();
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | void QPdfEnginePrivate::writePage()
|
---|
1098 | {
|
---|
1099 | if (pages.empty())
|
---|
1100 | return;
|
---|
1101 |
|
---|
1102 | *currentPage << "Q Q\n";
|
---|
1103 |
|
---|
1104 | uint pageStream = requestObject();
|
---|
1105 | uint pageStreamLength = requestObject();
|
---|
1106 | uint resources = requestObject();
|
---|
1107 | uint annots = requestObject();
|
---|
1108 |
|
---|
1109 | addXrefEntry(pages.last());
|
---|
1110 | xprintf("<<\n"
|
---|
1111 | "/Type /Page\n"
|
---|
1112 | "/Parent %d 0 R\n"
|
---|
1113 | "/Contents %d 0 R\n"
|
---|
1114 | "/Resources %d 0 R\n"
|
---|
1115 | "/Annots %d 0 R\n"
|
---|
1116 | "/MediaBox [0 0 %d %d]\n"
|
---|
1117 | ">>\n"
|
---|
1118 | "endobj\n",
|
---|
1119 | pageRoot, pageStream, resources, annots,
|
---|
1120 | // make sure we use the pagesize from when we started the page, since the user may have changed it
|
---|
1121 | currentPage->pageSize.width(), currentPage->pageSize.height());
|
---|
1122 |
|
---|
1123 | addXrefEntry(resources);
|
---|
1124 | xprintf("<<\n"
|
---|
1125 | "/ColorSpace <<\n"
|
---|
1126 | "/PCSp %d 0 R\n"
|
---|
1127 | "/CSp /DeviceRGB\n"
|
---|
1128 | "/CSpg /DeviceGray\n"
|
---|
1129 | ">>\n"
|
---|
1130 | "/ExtGState <<\n"
|
---|
1131 | "/GSa %d 0 R\n",
|
---|
1132 | patternColorSpace, graphicsState);
|
---|
1133 |
|
---|
1134 | for (int i = 0; i < currentPage->graphicStates.size(); ++i)
|
---|
1135 | xprintf("/GState%d %d 0 R\n", currentPage->graphicStates.at(i), currentPage->graphicStates.at(i));
|
---|
1136 | xprintf(">>\n");
|
---|
1137 |
|
---|
1138 | xprintf("/Pattern <<\n");
|
---|
1139 | for (int i = 0; i < currentPage->patterns.size(); ++i)
|
---|
1140 | xprintf("/Pat%d %d 0 R\n", currentPage->patterns.at(i), currentPage->patterns.at(i));
|
---|
1141 | xprintf(">>\n");
|
---|
1142 |
|
---|
1143 | xprintf("/Font <<\n");
|
---|
1144 | for (int i = 0; i < currentPage->fonts.size();++i)
|
---|
1145 | xprintf("/F%d %d 0 R\n", currentPage->fonts[i], currentPage->fonts[i]);
|
---|
1146 | xprintf(">>\n");
|
---|
1147 |
|
---|
1148 | xprintf("/XObject <<\n");
|
---|
1149 | for (int i = 0; i<currentPage->images.size(); ++i) {
|
---|
1150 | xprintf("/Im%d %d 0 R\n", currentPage->images.at(i), currentPage->images.at(i));
|
---|
1151 | }
|
---|
1152 | xprintf(">>\n");
|
---|
1153 |
|
---|
1154 | xprintf(">>\n"
|
---|
1155 | "endobj\n");
|
---|
1156 |
|
---|
1157 | addXrefEntry(annots);
|
---|
1158 | xprintf("[ ");
|
---|
1159 | for (int i = 0; i<currentPage->annotations.size(); ++i) {
|
---|
1160 | xprintf("%d 0 R ", currentPage->annotations.at(i));
|
---|
1161 | }
|
---|
1162 | xprintf("]\nendobj\n");
|
---|
1163 |
|
---|
1164 | addXrefEntry(pageStream);
|
---|
1165 | xprintf("<<\n"
|
---|
1166 | "/Length %d 0 R\n", pageStreamLength); // object number for stream length object
|
---|
1167 | if (do_compress)
|
---|
1168 | xprintf("/Filter /FlateDecode\n");
|
---|
1169 |
|
---|
1170 | xprintf(">>\n");
|
---|
1171 | xprintf("stream\n");
|
---|
1172 | QIODevice *content = currentPage->stream();
|
---|
1173 | int len = writeCompressed(content);
|
---|
1174 | xprintf("endstream\n"
|
---|
1175 | "endobj\n");
|
---|
1176 |
|
---|
1177 | addXrefEntry(pageStreamLength);
|
---|
1178 | xprintf("%d\nendobj\n",len);
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | void QPdfEnginePrivate::writeTail()
|
---|
1182 | {
|
---|
1183 | writePage();
|
---|
1184 | writeFonts();
|
---|
1185 | writePageRoot();
|
---|
1186 | addXrefEntry(xrefPositions.size(),false);
|
---|
1187 | xprintf("xref\n"
|
---|
1188 | "0 %d\n"
|
---|
1189 | "%010d 65535 f \n", xrefPositions.size()-1, xrefPositions[0]);
|
---|
1190 |
|
---|
1191 | for (int i = 1; i < xrefPositions.size()-1; ++i)
|
---|
1192 | xprintf("%010d 00000 n \n", xrefPositions[i]);
|
---|
1193 |
|
---|
1194 | xprintf("trailer\n"
|
---|
1195 | "<<\n"
|
---|
1196 | "/Size %d\n"
|
---|
1197 | "/Info %d 0 R\n"
|
---|
1198 | "/Root %d 0 R\n"
|
---|
1199 | ">>\n"
|
---|
1200 | "startxref\n%d\n"
|
---|
1201 | "%%%%EOF\n",
|
---|
1202 | xrefPositions.size()-1, info, catalog, xrefPositions.last());
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | int QPdfEnginePrivate::addXrefEntry(int object, bool printostr)
|
---|
1206 | {
|
---|
1207 | if (object < 0)
|
---|
1208 | object = requestObject();
|
---|
1209 |
|
---|
1210 | if (object>=xrefPositions.size())
|
---|
1211 | xrefPositions.resize(object+1);
|
---|
1212 |
|
---|
1213 | xrefPositions[object] = streampos;
|
---|
1214 | if (printostr)
|
---|
1215 | xprintf("%d 0 obj\n",object);
|
---|
1216 |
|
---|
1217 | return object;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | void QPdfEnginePrivate::printString(const QString &string) {
|
---|
1221 | // The 'text string' type in PDF is encoded either as PDFDocEncoding, or
|
---|
1222 | // Unicode UTF-16 with a Unicode byte order mark as the first character
|
---|
1223 | // (0xfeff), with the high-order byte first.
|
---|
1224 | QByteArray array("(\xfe\xff");
|
---|
1225 | const ushort *utf16 = string.utf16();
|
---|
1226 |
|
---|
1227 | for (int i=0; i < string.size(); ++i) {
|
---|
1228 | char part[2] = {char((*(utf16 + i)) >> 8), char((*(utf16 + i)) & 0xff)};
|
---|
1229 | for(int j=0; j < 2; ++j) {
|
---|
1230 | if (part[j] == '(' || part[j] == ')' || part[j] == '\\')
|
---|
1231 | array.append('\\');
|
---|
1232 | array.append(part[j]);
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 | array.append(")");
|
---|
1236 | write(array);
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | QT_END_NAMESPACE
|
---|
1240 |
|
---|
1241 | #endif // QT_NO_PRINTER
|
---|