Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/painting/qpdf.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | - | |||||||
2 | - | |||||||
3 | - | |||||||
4 | - | |||||||
5 | - | |||||||
6 | - | |||||||
7 | - | |||||||
8 | - | |||||||
9 | - | |||||||
10 | - | |||||||
11 | static const bool do_compress = true; | - | ||||||
12 | - | |||||||
13 | - | |||||||
14 | - | |||||||
15 | - | |||||||
16 | static const bool interpolateImages = false; | - | ||||||
17 | - | |||||||
18 | - | |||||||
19 | - | |||||||
20 | inline QPaintEngine::PaintEngineFeatures qt_pdf_decide_features() | - | ||||||
21 | { | - | ||||||
22 | QPaintEngine::PaintEngineFeatures f = QPaintEngine::AllFeatures; | - | ||||||
23 | f &= ~(QPaintEngine::PorterDuff | - | ||||||
24 | | QPaintEngine::PerspectiveTransform | - | ||||||
25 | | QPaintEngine::ObjectBoundingModeGradients | - | ||||||
26 | | QPaintEngine::ConicalGradientFill); | - | ||||||
27 | return f; | - | ||||||
28 | } | - | ||||||
29 | - | |||||||
30 | - | |||||||
31 | - | |||||||
32 | - | |||||||
33 | const char *qt_real_to_string(qreal val, char *buf) { | - | ||||||
34 | const char *ret = buf; | - | ||||||
35 | - | |||||||
36 | if (qIsNaN(val)) { | - | ||||||
37 | *(buf++) = '0'; | - | ||||||
38 | *(buf++) = ' '; | - | ||||||
39 | *buf = 0; | - | ||||||
40 | return ret; | - | ||||||
41 | } | - | ||||||
42 | - | |||||||
43 | if (val < 0) { | - | ||||||
44 | *(buf++) = '-'; | - | ||||||
45 | val = -val; | - | ||||||
46 | } | - | ||||||
47 | unsigned int ival = (unsigned int) val; | - | ||||||
48 | qreal frac = val - (qreal)ival; | - | ||||||
49 | - | |||||||
50 | int ifrac = (int)(frac * 1000000000); | - | ||||||
51 | if (ifrac == 1000000000) { | - | ||||||
52 | ++ival; | - | ||||||
53 | ifrac = 0; | - | ||||||
54 | } | - | ||||||
55 | char output[256]; | - | ||||||
56 | int i = 0; | - | ||||||
57 | while (ival) { | - | ||||||
58 | output[i] = '0' + (ival % 10); | - | ||||||
59 | ++i; | - | ||||||
60 | ival /= 10; | - | ||||||
61 | } | - | ||||||
62 | int fact = 100000000; | - | ||||||
63 | if (i == 0) { | - | ||||||
64 | *(buf++) = '0'; | - | ||||||
65 | } else { | - | ||||||
66 | while (i) { | - | ||||||
67 | *(buf++) = output[--i]; | - | ||||||
68 | fact /= 10; | - | ||||||
69 | ifrac /= 10; | - | ||||||
70 | } | - | ||||||
71 | } | - | ||||||
72 | - | |||||||
73 | if (ifrac) { | - | ||||||
74 | *(buf++) = '.'; | - | ||||||
75 | while (fact) { | - | ||||||
76 | *(buf++) = '0' + ((ifrac/fact) % 10); | - | ||||||
77 | fact /= 10; | - | ||||||
78 | } | - | ||||||
79 | } | - | ||||||
80 | *(buf++) = ' '; | - | ||||||
81 | *buf = 0; | - | ||||||
82 | return ret; | - | ||||||
83 | } | - | ||||||
84 | - | |||||||
85 | const char *qt_int_to_string(int val, char *buf) { | - | ||||||
86 | const char *ret = buf; | - | ||||||
87 | if (val < 0) { | - | ||||||
88 | *(buf++) = '-'; | - | ||||||
89 | val = -val; | - | ||||||
90 | } | - | ||||||
91 | char output[256]; | - | ||||||
92 | int i = 0; | - | ||||||
93 | while (val) { | - | ||||||
94 | output[i] = '0' + (val % 10); | - | ||||||
95 | ++i; | - | ||||||
96 | val /= 10; | - | ||||||
97 | } | - | ||||||
98 | if (i == 0) { | - | ||||||
99 | *(buf++) = '0'; | - | ||||||
100 | } else { | - | ||||||
101 | while (i) | - | ||||||
102 | *(buf++) = output[--i]; | - | ||||||
103 | } | - | ||||||
104 | *(buf++) = ' '; | - | ||||||
105 | *buf = 0; | - | ||||||
106 | return ret; | - | ||||||
107 | } | - | ||||||
108 | - | |||||||
109 | - | |||||||
110 | namespace QPdf { | - | ||||||
111 | ByteStream::ByteStream(QByteArray *byteArray, bool fileBacking) | - | ||||||
112 | : dev(new QBuffer(byteArray)), | - | ||||||
113 | fileBackingEnabled(fileBacking), | - | ||||||
114 | fileBackingActive(false), | - | ||||||
115 | handleDirty(false) | - | ||||||
116 | { | - | ||||||
117 | dev->open(QIODevice::ReadWrite | QIODevice::Append); | - | ||||||
118 | } | - | ||||||
119 | - | |||||||
120 | ByteStream::ByteStream(bool fileBacking) | - | ||||||
121 | : dev(new QBuffer(&ba)), | - | ||||||
122 | fileBackingEnabled(fileBacking), | - | ||||||
123 | fileBackingActive(false), | - | ||||||
124 | handleDirty(false) | - | ||||||
125 | { | - | ||||||
126 | dev->open(QIODevice::ReadWrite); | - | ||||||
127 | } | - | ||||||
128 | - | |||||||
129 | ByteStream::~ByteStream() | - | ||||||
130 | { | - | ||||||
131 | delete dev; | - | ||||||
132 | } | - | ||||||
133 | - | |||||||
134 | ByteStream &ByteStream::operator <<(char chr) | - | ||||||
135 | { | - | ||||||
136 | if (handleDirty) prepareBuffer(); | - | ||||||
137 | dev->write(&chr, 1); | - | ||||||
138 | return *this; | - | ||||||
139 | } | - | ||||||
140 | - | |||||||
141 | ByteStream &ByteStream::operator <<(const char *str) | - | ||||||
142 | { | - | ||||||
143 | if (handleDirty) prepareBuffer(); | - | ||||||
144 | dev->write(str, strlen(str)); | - | ||||||
145 | return *this; | - | ||||||
146 | } | - | ||||||
147 | - | |||||||
148 | ByteStream &ByteStream::operator <<(const QByteArray &str) | - | ||||||
149 | { | - | ||||||
150 | if (handleDirty) prepareBuffer(); | - | ||||||
151 | dev->write(str); | - | ||||||
152 | return *this; | - | ||||||
153 | } | - | ||||||
154 | - | |||||||
155 | ByteStream &ByteStream::operator <<(const ByteStream &src) | - | ||||||
156 | { | - | ||||||
157 | ((!(!src.dev->isSequential())) ? qt_assert("!src.dev->isSequential()",__FILE__,203209) : qt_noop()); | - | ||||||
158 | if (handleDirty) prepareBuffer(); | - | ||||||
159 | - | |||||||
160 | - | |||||||
161 | ByteStream &s = const_cast<ByteStream&>(src); | - | ||||||
162 | qint64 pos = s.dev->pos(); | - | ||||||
163 | s.dev->reset(); | - | ||||||
164 | while (!s.dev->atEnd()) { | - | ||||||
165 | QByteArray buf = s.dev->read(chunkSize()); | - | ||||||
166 | dev->write(buf); | - | ||||||
167 | } | - | ||||||
168 | s.dev->seek(pos); | - | ||||||
169 | return *this; | - | ||||||
170 | } | - | ||||||
171 | - | |||||||
172 | ByteStream &ByteStream::operator <<(qreal val) { | - | ||||||
173 | char buf[256]; | - | ||||||
174 | qt_real_to_string(val, buf); | - | ||||||
175 | *this << buf; | - | ||||||
176 | return *this; | - | ||||||
177 | } | - | ||||||
178 | - | |||||||
179 | ByteStream &ByteStream::operator <<(int val) { | - | ||||||
180 | char buf[256]; | - | ||||||
181 | qt_int_to_string(val, buf); | - | ||||||
182 | *this << buf; | - | ||||||
183 | return *this; | - | ||||||
184 | } | - | ||||||
185 | - | |||||||
186 | ByteStream &ByteStream::operator <<(const QPointF &p) { | - | ||||||
187 | char buf[256]; | - | ||||||
188 | qt_real_to_string(p.x(), buf); | - | ||||||
189 | *this << buf; | - | ||||||
190 | qt_real_to_string(p.y(), buf); | - | ||||||
191 | *this << buf; | - | ||||||
192 | return *this; | - | ||||||
193 | } | - | ||||||
194 | - | |||||||
195 | QIODevice *ByteStream::stream() | - | ||||||
196 | { | - | ||||||
197 | dev->reset(); | - | ||||||
198 | handleDirty = true; | - | ||||||
199 | return dev; | - | ||||||
200 | } | - | ||||||
201 | - | |||||||
202 | void ByteStream::clear() | - | ||||||
203 | { | - | ||||||
204 | dev->open(QIODevice::ReadWrite | QIODevice::Truncate); | - | ||||||
205 | } | - | ||||||
206 | - | |||||||
207 | void ByteStream::constructor_helper(QByteArray *ba) | - | ||||||
208 | { | - | ||||||
209 | delete dev; | - | ||||||
210 | dev = new QBuffer(ba); | - | ||||||
211 | dev->open(QIODevice::ReadWrite); | - | ||||||
212 | } | - | ||||||
213 | - | |||||||
214 | void ByteStream::prepareBuffer() | - | ||||||
215 | { | - | ||||||
216 | ((!(!dev->isSequential())) ? qt_assert("!dev->isSequential()",__FILE__,262268) : qt_noop()); | - | ||||||
217 | qint64 size = dev->size(); | - | ||||||
218 | if (fileBackingEnabled && !fileBackingActive | - | ||||||
219 | && size > maxMemorySize()) { | - | ||||||
220 | - | |||||||
221 | QTemporaryFile *newFile = new QTemporaryFile; | - | ||||||
222 | newFile->open(); | - | ||||||
223 | dev->reset(); | - | ||||||
224 | while (!dev->atEnd()) { | - | ||||||
225 | QByteArray buf = dev->read(chunkSize()); | - | ||||||
226 | newFile->write(buf); | - | ||||||
227 | } | - | ||||||
228 | delete dev; | - | ||||||
229 | dev = newFile; | - | ||||||
230 | ba.clear(); | - | ||||||
231 | fileBackingActive = true; | - | ||||||
232 | } | - | ||||||
233 | if (dev->pos() != size) { | - | ||||||
234 | dev->seek(size); | - | ||||||
235 | handleDirty = false; | - | ||||||
236 | } | - | ||||||
237 | } | - | ||||||
238 | } | - | ||||||
239 | - | |||||||
240 | - | |||||||
241 | - | |||||||
242 | QByteArray QPdf::generatePath(const QPainterPath &path, const QTransform &matrix, PathFlags flags) | - | ||||||
243 | { | - | ||||||
244 | QByteArray result; | - | ||||||
245 | if (!path.elementCount()) | - | ||||||
246 | return result; | - | ||||||
247 | - | |||||||
248 | ByteStream s(&result); | - | ||||||
249 | - | |||||||
250 | int start = -1; | - | ||||||
251 | for (int i = 0; i < path.elementCount(); ++i) { | - | ||||||
252 | const QPainterPath::Element &elm = path.elementAt(i); | - | ||||||
253 | switch (elm.type) { | - | ||||||
254 | case QPainterPath::MoveToElement: | - | ||||||
255 | if (start >= 0 | - | ||||||
256 | && path.elementAt(start).x == path.elementAt(i-1).x | - | ||||||
257 | && path.elementAt(start).y == path.elementAt(i-1).y) | - | ||||||
258 | s << "h\n"; | - | ||||||
259 | s << matrix.map(QPointF(elm.x, elm.y)) << "m\n"; | - | ||||||
260 | start = i; | - | ||||||
261 | break; | - | ||||||
262 | case QPainterPath::LineToElement: | - | ||||||
263 | s << matrix.map(QPointF(elm.x, elm.y)) << "l\n"; | - | ||||||
264 | break; | - | ||||||
265 | case QPainterPath::CurveToElement: | - | ||||||
266 | ((!(path.elementAt(i+1).type == QPainterPath::CurveToDataElement)) ? qt_assert("path.elementAt(i+1).type == QPainterPath::CurveToDataElement",__FILE__,312318) : qt_noop()); | - | ||||||
267 | ((!(path.elementAt(i+2).type == QPainterPath::CurveToDataElement)) ? qt_assert("path.elementAt(i+2).type == QPainterPath::CurveToDataElement",__FILE__,313319) : qt_noop()); | - | ||||||
268 | s << matrix.map(QPointF(elm.x, elm.y)) | - | ||||||
269 | << matrix.map(QPointF(path.elementAt(i+1).x, path.elementAt(i+1).y)) | - | ||||||
270 | << matrix.map(QPointF(path.elementAt(i+2).x, path.elementAt(i+2).y)) | - | ||||||
271 | << "c\n"; | - | ||||||
272 | i += 2; | - | ||||||
273 | break; | - | ||||||
274 | default: | - | ||||||
275 | QMessageLogger(__FILE__, 321327, __PRETTY_FUNCTION__).fatal("QPdf::generatePath(), unhandled type: %d", elm.type); | - | ||||||
276 | } | - | ||||||
277 | } | - | ||||||
278 | if (start >= 0 | - | ||||||
279 | && path.elementAt(start).x == path.elementAt(path.elementCount()-1).x | - | ||||||
280 | && path.elementAt(start).y == path.elementAt(path.elementCount()-1).y) | - | ||||||
281 | s << "h\n"; | - | ||||||
282 | - | |||||||
283 | Qt::FillRule fillRule = path.fillRule(); | - | ||||||
284 | - | |||||||
285 | const char *op = ""; | - | ||||||
286 | switch (flags) { | - | ||||||
287 | case ClipPath: | - | ||||||
288 | op = (fillRule == Qt::WindingFill) ? "W n\n" : "W* n\n"; | - | ||||||
289 | break; | - | ||||||
290 | case FillPath: | - | ||||||
291 | op = (fillRule == Qt::WindingFill) ? "f\n" : "f*\n"; | - | ||||||
292 | break; | - | ||||||
293 | case StrokePath: | - | ||||||
294 | op = "S\n"; | - | ||||||
295 | break; | - | ||||||
296 | case FillAndStrokePath: | - | ||||||
297 | op = (fillRule == Qt::WindingFill) ? "B\n" : "B*\n"; | - | ||||||
298 | break; | - | ||||||
299 | } | - | ||||||
300 | s << op; | - | ||||||
301 | return result; | - | ||||||
302 | } | - | ||||||
303 | - | |||||||
304 | QByteArray QPdf::generateMatrix(const QTransform &matrix) | - | ||||||
305 | { | - | ||||||
306 | QByteArray result; | - | ||||||
307 | ByteStream s(&result); | - | ||||||
308 | s << matrix.m11() | - | ||||||
309 | << matrix.m12() | - | ||||||
310 | << matrix.m21() | - | ||||||
311 | << matrix.m22() | - | ||||||
312 | << matrix.dx() | - | ||||||
313 | << matrix.dy() | - | ||||||
314 | << "cm\n"; | - | ||||||
315 | return result; | - | ||||||
316 | } | - | ||||||
317 | - | |||||||
318 | QByteArray QPdf::generateDashes(const QPen &pen) | - | ||||||
319 | { | - | ||||||
320 | QByteArray result; | - | ||||||
321 | ByteStream s(&result); | - | ||||||
322 | s << '['; | - | ||||||
323 | - | |||||||
324 | QVector<qreal> dasharray = pen.dashPattern(); | - | ||||||
325 | qreal w = pen.widthF(); | - | ||||||
326 | if (w < 0.001) | - | ||||||
327 | w = 1; | - | ||||||
328 | for (int i = 0; i < dasharray.size(); ++i) { | - | ||||||
329 | qreal dw = dasharray.at(i)*w; | - | ||||||
330 | if (dw < 0.0001) dw = 0.0001; | - | ||||||
331 | s << dw; | - | ||||||
332 | } | - | ||||||
333 | s << ']'; | - | ||||||
334 | s << pen.dashOffset() * w; | - | ||||||
335 | s << " d\n"; | - | ||||||
336 | return result; | - | ||||||
337 | } | - | ||||||
338 | - | |||||||
339 | - | |||||||
340 | - | |||||||
341 | static const char* const pattern_for_brush[] = { | - | ||||||
342 | 0, | - | ||||||
343 | 0, | - | ||||||
344 | "0 J\n" | - | ||||||
345 | "6 w\n" | - | ||||||
346 | "[] 0 d\n" | - | ||||||
347 | "4 0 m\n" | - | ||||||
348 | "4 8 l\n" | - | ||||||
349 | "0 4 m\n" | - | ||||||
350 | "8 4 l\n" | - | ||||||
351 | "S\n", | - | ||||||
352 | - | |||||||
353 | "0 J\n" | - | ||||||
354 | "2 w\n" | - | ||||||
355 | "[6 2] 1 d\n" | - | ||||||
356 | "0 0 m\n" | - | ||||||
357 | "0 8 l\n" | - | ||||||
358 | "8 0 m\n" | - | ||||||
359 | "8 8 l\n" | - | ||||||
360 | "S\n" | - | ||||||
361 | "[] 0 d\n" | - | ||||||
362 | "2 0 m\n" | - | ||||||
363 | "2 8 l\n" | - | ||||||
364 | "6 0 m\n" | - | ||||||
365 | "6 8 l\n" | - | ||||||
366 | "S\n" | - | ||||||
367 | "[6 2] -3 d\n" | - | ||||||
368 | "4 0 m\n" | - | ||||||
369 | "4 8 l\n" | - | ||||||
370 | "S\n", | - | ||||||
371 | - | |||||||
372 | "0 J\n" | - | ||||||
373 | "2 w\n" | - | ||||||
374 | "[6 2] 1 d\n" | - | ||||||
375 | "0 0 m\n" | - | ||||||
376 | "0 8 l\n" | - | ||||||
377 | "8 0 m\n" | - | ||||||
378 | "8 8 l\n" | - | ||||||
379 | "S\n" | - | ||||||
380 | "[2 2] -1 d\n" | - | ||||||
381 | "2 0 m\n" | - | ||||||
382 | "2 8 l\n" | - | ||||||
383 | "6 0 m\n" | - | ||||||
384 | "6 8 l\n" | - | ||||||
385 | "S\n" | - | ||||||
386 | "[6 2] -3 d\n" | - | ||||||
387 | "4 0 m\n" | - | ||||||
388 | "4 8 l\n" | - | ||||||
389 | "S\n", | - | ||||||
390 | - | |||||||
391 | "0 J\n" | - | ||||||
392 | "2 w\n" | - | ||||||
393 | "[2 2] 1 d\n" | - | ||||||
394 | "0 0 m\n" | - | ||||||
395 | "0 8 l\n" | - | ||||||
396 | "8 0 m\n" | - | ||||||
397 | "8 8 l\n" | - | ||||||
398 | "S\n" | - | ||||||
399 | "[2 2] -1 d\n" | - | ||||||
400 | "2 0 m\n" | - | ||||||
401 | "2 8 l\n" | - | ||||||
402 | "6 0 m\n" | - | ||||||
403 | "6 8 l\n" | - | ||||||
404 | "S\n" | - | ||||||
405 | "[2 2] 1 d\n" | - | ||||||
406 | "4 0 m\n" | - | ||||||
407 | "4 8 l\n" | - | ||||||
408 | "S\n", | - | ||||||
409 | - | |||||||
410 | "0 J\n" | - | ||||||
411 | "2 w\n" | - | ||||||
412 | "[2 6] -1 d\n" | - | ||||||
413 | "0 0 m\n" | - | ||||||
414 | "0 8 l\n" | - | ||||||
415 | "8 0 m\n" | - | ||||||
416 | "8 8 l\n" | - | ||||||
417 | "S\n" | - | ||||||
418 | "[2 2] 1 d\n" | - | ||||||
419 | "2 0 m\n" | - | ||||||
420 | "2 8 l\n" | - | ||||||
421 | "6 0 m\n" | - | ||||||
422 | "6 8 l\n" | - | ||||||
423 | "S\n" | - | ||||||
424 | "[2 6] 3 d\n" | - | ||||||
425 | "4 0 m\n" | - | ||||||
426 | "4 8 l\n" | - | ||||||
427 | "S\n", | - | ||||||
428 | - | |||||||
429 | "0 J\n" | - | ||||||
430 | "2 w\n" | - | ||||||
431 | "[2 6] -1 d\n" | - | ||||||
432 | "0 0 m\n" | - | ||||||
433 | "0 8 l\n" | - | ||||||
434 | "8 0 m\n" | - | ||||||
435 | "8 8 l\n" | - | ||||||
436 | "S\n" | - | ||||||
437 | "[2 6] 3 d\n" | - | ||||||
438 | "4 0 m\n" | - | ||||||
439 | "4 8 l\n" | - | ||||||
440 | "S\n", | - | ||||||
441 | - | |||||||
442 | "0 J\n" | - | ||||||
443 | "2 w\n" | - | ||||||
444 | "[2 6] -1 d\n" | - | ||||||
445 | "0 0 m\n" | - | ||||||
446 | "0 8 l\n" | - | ||||||
447 | "8 0 m\n" | - | ||||||
448 | "8 8 l\n" | - | ||||||
449 | "S\n", | - | ||||||
450 | - | |||||||
451 | "1 w\n" | - | ||||||
452 | "0 4 m\n" | - | ||||||
453 | "8 4 l\n" | - | ||||||
454 | "S\n", | - | ||||||
455 | - | |||||||
456 | "1 w\n" | - | ||||||
457 | "4 0 m\n" | - | ||||||
458 | "4 8 l\n" | - | ||||||
459 | "S\n", | - | ||||||
460 | - | |||||||
461 | "1 w\n" | - | ||||||
462 | "4 0 m\n" | - | ||||||
463 | "4 8 l\n" | - | ||||||
464 | "0 4 m\n" | - | ||||||
465 | "8 4 l\n" | - | ||||||
466 | "S\n", | - | ||||||
467 | - | |||||||
468 | "1 w\n" | - | ||||||
469 | "-1 5 m\n" | - | ||||||
470 | "5 -1 l\n" | - | ||||||
471 | "3 9 m\n" | - | ||||||
472 | "9 3 l\n" | - | ||||||
473 | "S\n", | - | ||||||
474 | - | |||||||
475 | "1 w\n" | - | ||||||
476 | "-1 3 m\n" | - | ||||||
477 | "5 9 l\n" | - | ||||||
478 | "3 -1 m\n" | - | ||||||
479 | "9 5 l\n" | - | ||||||
480 | "S\n", | - | ||||||
481 | - | |||||||
482 | "1 w\n" | - | ||||||
483 | "-1 3 m\n" | - | ||||||
484 | "5 9 l\n" | - | ||||||
485 | "3 -1 m\n" | - | ||||||
486 | "9 5 l\n" | - | ||||||
487 | "-1 5 m\n" | - | ||||||
488 | "5 -1 l\n" | - | ||||||
489 | "3 9 m\n" | - | ||||||
490 | "9 3 l\n" | - | ||||||
491 | "S\n", | - | ||||||
492 | }; | - | ||||||
493 | - | |||||||
494 | QByteArray QPdf::patternForBrush(const QBrush &b) | - | ||||||
495 | { | - | ||||||
496 | int style = b.style(); | - | ||||||
497 | if (style > Qt::DiagCrossPattern) | - | ||||||
498 | return QByteArray(); | - | ||||||
499 | return pattern_for_brush[style]; | - | ||||||
500 | } | - | ||||||
501 | - | |||||||
502 | - | |||||||
503 | static void moveToHook(qfixed x, qfixed y, void *data) | - | ||||||
504 | { | - | ||||||
505 | QPdf::Stroker *t = (QPdf::Stroker *)data; | - | ||||||
506 | if (!t->first) | - | ||||||
507 | *t->stream << "h\n"; | - | ||||||
508 | if (!t->cosmeticPen) | - | ||||||
509 | t->matrix.map(x, y, &x, &y); | - | ||||||
510 | *t->stream << x << y << "m\n"; | - | ||||||
511 | t->first = false; | - | ||||||
512 | } | - | ||||||
513 | - | |||||||
514 | static void lineToHook(qfixed x, qfixed y, void *data) | - | ||||||
515 | { | - | ||||||
516 | QPdf::Stroker *t = (QPdf::Stroker *)data; | - | ||||||
517 | if (!t->cosmeticPen) | - | ||||||
518 | t->matrix.map(x, y, &x, &y); | - | ||||||
519 | *t->stream << x << y << "l\n"; | - | ||||||
520 | } | - | ||||||
521 | - | |||||||
522 | static void cubicToHook(qfixed c1x, qfixed c1y, | - | ||||||
523 | qfixed c2x, qfixed c2y, | - | ||||||
524 | qfixed ex, qfixed ey, | - | ||||||
525 | void *data) | - | ||||||
526 | { | - | ||||||
527 | QPdf::Stroker *t = (QPdf::Stroker *)data; | - | ||||||
528 | if (!t->cosmeticPen) { | - | ||||||
529 | t->matrix.map(c1x, c1y, &c1x, &c1y); | - | ||||||
530 | t->matrix.map(c2x, c2y, &c2x, &c2y); | - | ||||||
531 | t->matrix.map(ex, ey, &ex, &ey); | - | ||||||
532 | } | - | ||||||
533 | *t->stream << c1x << c1y | - | ||||||
534 | << c2x << c2y | - | ||||||
535 | << ex << ey | - | ||||||
536 | << "c\n"; | - | ||||||
537 | } | - | ||||||
538 | - | |||||||
539 | QPdf::Stroker::Stroker() | - | ||||||
540 | : stream(0), | - | ||||||
541 | first(true), | - | ||||||
542 | dashStroker(&basicStroker) | - | ||||||
543 | { | - | ||||||
544 | stroker = &basicStroker; | - | ||||||
545 | basicStroker.setMoveToHook(moveToHook); | - | ||||||
546 | basicStroker.setLineToHook(lineToHook); | - | ||||||
547 | basicStroker.setCubicToHook(cubicToHook); | - | ||||||
548 | cosmeticPen = true; | - | ||||||
549 | basicStroker.setStrokeWidth(.1); | - | ||||||
550 | } | - | ||||||
551 | - | |||||||
552 | void QPdf::Stroker::setPen(const QPen &pen, QPainter::RenderHints hints) | - | ||||||
553 | { | - | ||||||
554 | if (pen.style() == Qt::NoPen) { | - | ||||||
555 | stroker = 0; | - | ||||||
556 | return; | - | ||||||
557 | } | - | ||||||
558 | qreal w = pen.widthF(); | - | ||||||
559 | bool zeroWidth = w < 0.0001; | - | ||||||
560 | cosmeticPen = qt_pen_is_cosmetic(pen, hints); | - | ||||||
561 | if (zeroWidth) | - | ||||||
562 | w = .1; | - | ||||||
563 | - | |||||||
564 | basicStroker.setStrokeWidth(w); | - | ||||||
565 | basicStroker.setCapStyle(pen.capStyle()); | - | ||||||
566 | basicStroker.setJoinStyle(pen.joinStyle()); | - | ||||||
567 | basicStroker.setMiterLimit(pen.miterLimit()); | - | ||||||
568 | - | |||||||
569 | QVector<qreal> dashpattern = pen.dashPattern(); | - | ||||||
570 | if (zeroWidth) { | - | ||||||
571 | for (int i = 0; i < dashpattern.size(); ++i) | - | ||||||
572 | dashpattern[i] *= 10.; | - | ||||||
573 | } | - | ||||||
574 | if (!dashpattern.isEmpty()) { | - | ||||||
575 | dashStroker.setDashPattern(dashpattern); | - | ||||||
576 | dashStroker.setDashOffset(pen.dashOffset()); | - | ||||||
577 | stroker = &dashStroker; | - | ||||||
578 | } else { | - | ||||||
579 | stroker = &basicStroker; | - | ||||||
580 | } | - | ||||||
581 | } | - | ||||||
582 | - | |||||||
583 | void QPdf::Stroker::strokePath(const QPainterPath &path) | - | ||||||
584 | { | - | ||||||
585 | if (!stroker) | - | ||||||
586 | return; | - | ||||||
587 | first = true; | - | ||||||
588 | - | |||||||
589 | stroker->strokePath(path, this, cosmeticPen ? matrix : QTransform()); | - | ||||||
590 | *stream << "h f\n"; | - | ||||||
591 | } | - | ||||||
592 | - | |||||||
593 | QByteArray QPdf::ascii85Encode(const QByteArray &input) | - | ||||||
594 | { | - | ||||||
595 | int isize = input.size()/4*4; | - | ||||||
596 | QByteArray output; | - | ||||||
597 | output.resize(input.size()*5/4+7); | - | ||||||
598 | char *out = output.data(); | - | ||||||
599 | const uchar *in = (const uchar *)input.constData(); | - | ||||||
600 | for (int i = 0; i < isize; i += 4) { | - | ||||||
601 | uint val = (((uint)in[i])<<24) + (((uint)in[i+1])<<16) + (((uint)in[i+2])<<8) + (uint)in[i+3]; | - | ||||||
602 | if (val == 0) { | - | ||||||
603 | *out = 'z'; | - | ||||||
604 | ++out; | - | ||||||
605 | } else { | - | ||||||
606 | char base[5]; | - | ||||||
607 | base[4] = val % 85; | - | ||||||
608 | val /= 85; | - | ||||||
609 | base[3] = val % 85; | - | ||||||
610 | val /= 85; | - | ||||||
611 | base[2] = val % 85; | - | ||||||
612 | val /= 85; | - | ||||||
613 | base[1] = val % 85; | - | ||||||
614 | val /= 85; | - | ||||||
615 | base[0] = val % 85; | - | ||||||
616 | *(out++) = base[0] + '!'; | - | ||||||
617 | *(out++) = base[1] + '!'; | - | ||||||
618 | *(out++) = base[2] + '!'; | - | ||||||
619 | *(out++) = base[3] + '!'; | - | ||||||
620 | *(out++) = base[4] + '!'; | - | ||||||
621 | } | - | ||||||
622 | } | - | ||||||
623 | - | |||||||
624 | int remaining = input.size() - isize; | - | ||||||
625 | if (remaining) { | - | ||||||
626 | uint val = 0; | - | ||||||
627 | for (int i = isize; i < input.size(); ++i) | - | ||||||
628 | val = (val << 8) + in[i]; | - | ||||||
629 | val <<= 8*(4-remaining); | - | ||||||
630 | char base[5]; | - | ||||||
631 | base[4] = val % 85; | - | ||||||
632 | val /= 85; | - | ||||||
633 | base[3] = val % 85; | - | ||||||
634 | val /= 85; | - | ||||||
635 | base[2] = val % 85; | - | ||||||
636 | val /= 85; | - | ||||||
637 | base[1] = val % 85; | - | ||||||
638 | val /= 85; | - | ||||||
639 | base[0] = val % 85; | - | ||||||
640 | for (int i = 0; i < remaining+1; ++i) | - | ||||||
641 | *(out++) = base[i] + '!'; | - | ||||||
642 | } | - | ||||||
643 | *(out++) = '~'; | - | ||||||
644 | *(out++) = '>'; | - | ||||||
645 | output.resize(out-output.data()); | - | ||||||
646 | return output; | - | ||||||
647 | } | - | ||||||
648 | - | |||||||
649 | const char *QPdf::toHex(ushort u, char *buffer) | - | ||||||
650 | { | - | ||||||
651 | int i = 3; | - | ||||||
652 | while (i >= 0) { | - | ||||||
653 | ushort hex = (u & 0x000f); | - | ||||||
654 | if (hex < 0x0a) | - | ||||||
655 | buffer[i] = '0'+hex; | - | ||||||
656 | else | - | ||||||
657 | buffer[i] = 'A'+(hex-0x0a); | - | ||||||
658 | u = u >> 4; | - | ||||||
659 | i--; | - | ||||||
660 | } | - | ||||||
661 | buffer[4] = '\0'; | - | ||||||
662 | return buffer; | - | ||||||
663 | } | - | ||||||
664 | - | |||||||
665 | const char *QPdf::toHex(uchar u, char *buffer) | - | ||||||
666 | { | - | ||||||
667 | int i = 1; | - | ||||||
668 | while (i >= 0) { | - | ||||||
669 | ushort hex = (u & 0x000f); | - | ||||||
670 | if (hex < 0x0a) | - | ||||||
671 | buffer[i] = '0'+hex; | - | ||||||
672 | else | - | ||||||
673 | buffer[i] = 'A'+(hex-0x0a); | - | ||||||
674 | u = u >> 4; | - | ||||||
675 | i--; | - | ||||||
676 | } | - | ||||||
677 | buffer[2] = '\0'; | - | ||||||
678 | return buffer; | - | ||||||
679 | } | - | ||||||
680 | - | |||||||
681 | - | |||||||
682 | QPdfPage::QPdfPage() | - | ||||||
683 | : QPdf::ByteStream(true) | - | ||||||
684 | { | - | ||||||
685 | } | - | ||||||
686 | - | |||||||
687 | void QPdfPage::streamImage(int w, int h, int object) | - | ||||||
688 | { | - | ||||||
689 | *this << w << "0 0 " << -h << "0 " << h << "cm /Im" << object << " Do\n"; | - | ||||||
690 | if (!images.contains(object)) | - | ||||||
691 | images.append(object); | - | ||||||
692 | } | - | ||||||
693 | - | |||||||
694 | - | |||||||
695 | QPdfEngine::QPdfEngine(QPdfEnginePrivate &dd) | - | ||||||
696 | : QPaintEngine(dd, qt_pdf_decide_features()) | - | ||||||
697 | { | - | ||||||
698 | } | - | ||||||
699 | - | |||||||
700 | QPdfEngine::QPdfEngine() | - | ||||||
701 | : QPaintEngine(*new QPdfEnginePrivate(), qt_pdf_decide_features()) | - | ||||||
702 | { | - | ||||||
703 | } | - | ||||||
704 | - | |||||||
705 | void QPdfEngine::setOutputFilename(const QString &filename) | - | ||||||
706 | { | - | ||||||
707 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
708 | d->outputFileName = filename; | - | ||||||
709 | } | - | ||||||
710 | - | |||||||
711 | - | |||||||
712 | void QPdfEngine::drawPoints (const QPointF *points, int pointCount) | - | ||||||
713 | { | - | ||||||
714 | if (!points) | - | ||||||
715 | return; | - | ||||||
716 | - | |||||||
717 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
718 | QPainterPath p; | - | ||||||
719 | for (int i=0; i!=pointCount;++i) { | - | ||||||
720 | p.moveTo(points[i]); | - | ||||||
721 | p.lineTo(points[i] + QPointF(0, 0.001)); | - | ||||||
722 | } | - | ||||||
723 | - | |||||||
724 | bool hadBrush = d->hasBrush; | - | ||||||
725 | d->hasBrush = false; | - | ||||||
726 | drawPath(p); | - | ||||||
727 | d->hasBrush = hadBrush; | - | ||||||
728 | } | - | ||||||
729 | - | |||||||
730 | void QPdfEngine::drawLines (const QLineF *lines, int lineCount) | - | ||||||
731 | { | - | ||||||
732 | if (!lines) | - | ||||||
733 | return; | - | ||||||
734 | - | |||||||
735 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
736 | QPainterPath p; | - | ||||||
737 | for (int i=0; i!=lineCount;++i) { | - | ||||||
738 | p.moveTo(lines[i].p1()); | - | ||||||
739 | p.lineTo(lines[i].p2()); | - | ||||||
740 | } | - | ||||||
741 | bool hadBrush = d->hasBrush; | - | ||||||
742 | d->hasBrush = false; | - | ||||||
743 | drawPath(p); | - | ||||||
744 | d->hasBrush = hadBrush; | - | ||||||
745 | } | - | ||||||
746 | - | |||||||
747 | void QPdfEngine::drawRects (const QRectF *rects, int rectCount) | - | ||||||
748 | { | - | ||||||
749 | if (!rects) | - | ||||||
750 | return; | - | ||||||
751 | - | |||||||
752 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
753 | - | |||||||
754 | if (d->clipEnabled && d->allClipped) | - | ||||||
755 | return; | - | ||||||
756 | if (!d->hasPen && !d->hasBrush) | - | ||||||
757 | return; | - | ||||||
758 | - | |||||||
759 | QBrush penBrush = d->pen.brush(); | - | ||||||
760 | if (d->simplePen || !d->hasPen) { | - | ||||||
761 | - | |||||||
762 | if(!d->simplePen && !d->stroker.matrix.isIdentity()) | - | ||||||
763 | *d->currentPage << "q\n" << QPdf::generateMatrix(d->stroker.matrix); | - | ||||||
764 | for (int i = 0; i < rectCount; ++i) | - | ||||||
765 | *d->currentPage << rects[i].x() << rects[i].y() << rects[i].width() << rects[i].height() << "re\n"; | - | ||||||
766 | *d->currentPage << (d->hasPen ? (d->hasBrush ? "B\n" : "S\n") : "f\n"); | - | ||||||
767 | if(!d->simplePen && !d->stroker.matrix.isIdentity()) | - | ||||||
768 | *d->currentPage << "Q\n"; | - | ||||||
769 | } else { | - | ||||||
770 | QPainterPath p; | - | ||||||
771 | for (int i=0; i!=rectCount; ++i) | - | ||||||
772 | p.addRect(rects[i]); | - | ||||||
773 | drawPath(p); | - | ||||||
774 | } | - | ||||||
775 | } | - | ||||||
776 | - | |||||||
777 | void QPdfEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) | - | ||||||
778 | { | - | ||||||
779 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
780 | - | |||||||
781 | if (!points || !pointCount) | - | ||||||
782 | return; | - | ||||||
783 | - | |||||||
784 | bool hb = d->hasBrush; | - | ||||||
785 | QPainterPath p; | - | ||||||
786 | - | |||||||
787 | switch(mode) { | - | ||||||
788 | case OddEvenMode: | - | ||||||
789 | p.setFillRule(Qt::OddEvenFill); | - | ||||||
790 | break; | - | ||||||
791 | case ConvexMode: | - | ||||||
792 | case WindingMode: | - | ||||||
793 | p.setFillRule(Qt::WindingFill); | - | ||||||
794 | break; | - | ||||||
795 | case PolylineMode: | - | ||||||
796 | d->hasBrush = false; | - | ||||||
797 | break; | - | ||||||
798 | default: | - | ||||||
799 | break; | - | ||||||
800 | } | - | ||||||
801 | - | |||||||
802 | p.moveTo(points[0]); | - | ||||||
803 | for (int i = 1; i < pointCount; ++i) | - | ||||||
804 | p.lineTo(points[i]); | - | ||||||
805 | - | |||||||
806 | if (mode != PolylineMode) | - | ||||||
807 | p.closeSubpath(); | - | ||||||
808 | drawPath(p); | - | ||||||
809 | - | |||||||
810 | d->hasBrush = hb; | - | ||||||
811 | } | - | ||||||
812 | - | |||||||
813 | void QPdfEngine::drawPath (const QPainterPath &p) | - | ||||||
814 | { | - | ||||||
815 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
816 | - | |||||||
817 | if (d->clipEnabled && d->allClipped) | - | ||||||
818 | return; | - | ||||||
819 | if (!d->hasPen && !d->hasBrush) | - | ||||||
820 | return; | - | ||||||
821 | - | |||||||
822 | if (d->simplePen) { | - | ||||||
823 | - | |||||||
824 | *d->currentPage << QPdf::generatePath(p, QTransform(), d->hasBrush ? QPdf::FillAndStrokePath : QPdf::StrokePath); | - | ||||||
825 | } else { | - | ||||||
826 | if (d->hasBrush) | - | ||||||
827 | *d->currentPage << QPdf::generatePath(p, d->stroker.matrix, QPdf::FillPath); | - | ||||||
828 | if (d->hasPen) { | - | ||||||
829 | *d->currentPage << "q\n"; | - | ||||||
830 | QBrush b = d->brush; | - | ||||||
831 | d->brush = d->pen.brush(); | - | ||||||
832 | setBrush(); | - | ||||||
833 | d->stroker.strokePath(p); | - | ||||||
834 | *d->currentPage << "Q\n"; | - | ||||||
835 | d->brush = b; | - | ||||||
836 | } | - | ||||||
837 | } | - | ||||||
838 | } | - | ||||||
839 | - | |||||||
840 | void QPdfEngine::drawPixmap (const QRectF &rectangle, const QPixmap &pixmap, const QRectF &sr) | - | ||||||
841 | { | - | ||||||
842 | if (sr.isEmpty() || rectangle.isEmpty() || pixmap.isNull()) | - | ||||||
843 | return; | - | ||||||
844 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
845 | - | |||||||
846 | QBrush b = d->brush; | - | ||||||
847 | - | |||||||
848 | QRect sourceRect = sr.toRect(); | - | ||||||
849 | QPixmap pm = sourceRect != pixmap.rect() ? pixmap.copy(sourceRect) : pixmap; | - | ||||||
850 | QImage image = pm.toImage(); | - | ||||||
851 | bool bitmap = true; | - | ||||||
852 | const int object = d->addImage(image, &bitmap, pm.cacheKey()); | - | ||||||
853 | if (object < 0) | - | ||||||
854 | return; | - | ||||||
855 | - | |||||||
856 | *d->currentPage << "q\n/GSa gs\n"; | - | ||||||
857 | *d->currentPage | - | ||||||
858 | << QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(), | - | ||||||
859 | rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix)); | - | ||||||
860 | if (bitmap) { | - | ||||||
861 | - | |||||||
862 | d->brush = d->pen.brush(); | - | ||||||
863 | } | - | ||||||
864 | setBrush(); | - | ||||||
865 | d->currentPage->streamImage(image.width(), image.height(), object); | - | ||||||
866 | *d->currentPage << "Q\n"; | - | ||||||
867 | - | |||||||
868 | d->brush = b; | - | ||||||
869 | } | - | ||||||
870 | - | |||||||
871 | void QPdfEngine::drawImage(const QRectF &rectangle, const QImage &image, const QRectF &sr, Qt::ImageConversionFlags) | - | ||||||
872 | { | - | ||||||
873 | if (sr.isEmpty() || rectangle.isEmpty() || image.isNull()) | - | ||||||
874 | return; | - | ||||||
875 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
876 | - | |||||||
877 | QRect sourceRect = sr.toRect(); | - | ||||||
878 | QImage im = sourceRect != image.rect() ? image.copy(sourceRect) : image; | - | ||||||
879 | bool bitmap = true; | - | ||||||
880 | const int object = d->addImage(im, &bitmap, im.cacheKey()); | - | ||||||
881 | if (object < 0) | - | ||||||
882 | return; | - | ||||||
883 | - | |||||||
884 | *d->currentPage << "q\n/GSa gs\n"; | - | ||||||
885 | *d->currentPage | - | ||||||
886 | << QPdf::generateMatrix(QTransform(rectangle.width() / sr.width(), 0, 0, rectangle.height() / sr.height(), | - | ||||||
887 | rectangle.x(), rectangle.y()) * (d->simplePen ? QTransform() : d->stroker.matrix)); | - | ||||||
888 | setBrush(); | - | ||||||
889 | d->currentPage->streamImage(im.width(), im.height(), object); | - | ||||||
890 | *d->currentPage << "Q\n"; | - | ||||||
891 | } | - | ||||||
892 | - | |||||||
893 | void QPdfEngine::drawTiledPixmap (const QRectF &rectangle, const QPixmap &pixmap, const QPointF &point) | - | ||||||
894 | { | - | ||||||
895 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
896 | - | |||||||
897 | bool bitmap = (pixmap.depth() == 1); | - | ||||||
898 | QBrush b = d->brush; | - | ||||||
899 | QPointF bo = d->brushOrigin; | - | ||||||
900 | bool hp = d->hasPen; | - | ||||||
901 | d->hasPen = false; | - | ||||||
902 | bool hb = d->hasBrush; | - | ||||||
903 | d->hasBrush = true; | - | ||||||
904 | - | |||||||
905 | d->brush = QBrush(pixmap); | - | ||||||
906 | if (bitmap) | - | ||||||
907 | - | |||||||
908 | d->brush.setColor(d->pen.color()); | - | ||||||
909 | - | |||||||
910 | d->brushOrigin = -point; | - | ||||||
911 | *d->currentPage << "q\n"; | - | ||||||
912 | setBrush(); | - | ||||||
913 | - | |||||||
914 | drawRects(&rectangle, 1); | - | ||||||
915 | *d->currentPage << "Q\n"; | - | ||||||
916 | - | |||||||
917 | d->hasPen = hp; | - | ||||||
918 | d->hasBrush = hb; | - | ||||||
919 | d->brush = b; | - | ||||||
920 | d->brushOrigin = bo; | - | ||||||
921 | } | - | ||||||
922 | - | |||||||
923 | void QPdfEngine::drawTextItem(const QPointF &p, const QTextItem &textItem) | - | ||||||
924 | { | - | ||||||
925 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
926 | - | |||||||
927 | if (!d->hasPen || (d->clipEnabled && d->allClipped)) | - | ||||||
928 | return; | - | ||||||
929 | - | |||||||
930 | if (d->stroker.matrix.type() >= QTransform::TxProject) { | - | ||||||
931 | QPaintEngine::drawTextItem(p, textItem); | - | ||||||
932 | return; | - | ||||||
933 | } | - | ||||||
934 | - | |||||||
935 | *d->currentPage << "q\n"; | - | ||||||
936 | if(!d->simplePen) | - | ||||||
937 | *d->currentPage << QPdf::generateMatrix(d->stroker.matrix); | - | ||||||
938 | - | |||||||
939 | bool hp = d->hasPen; | - | ||||||
940 | d->hasPen = false; | - | ||||||
941 | QBrush b = d->brush; | - | ||||||
942 | d->brush = d->pen.brush(); | - | ||||||
943 | setBrush(); | - | ||||||
944 | - | |||||||
945 | const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem); | - | ||||||
946 | ((!(ti.fontEngine->type() != QFontEngine::Multi)) ? qt_assert("ti.fontEngine->type() != QFontEngine::Multi",__FILE__,992998) : qt_noop()); | - | ||||||
947 | d->drawTextItem(p, ti); | - | ||||||
948 | d->hasPen = hp; | - | ||||||
949 | d->brush = b; | - | ||||||
950 | *d->currentPage << "Q\n"; | - | ||||||
951 | } | - | ||||||
952 | - | |||||||
953 | void QPdfEngine::drawHyperlink(const QRectF &r, const QUrl &url) | - | ||||||
954 | { | - | ||||||
955 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
956 | - | |||||||
957 | const uint annot = d->addXrefEntry(-1); | - | ||||||
958 | const QByteArray urlascii = url.toEncoded(); | - | ||||||
959 | int len = urlascii.size(); | - | ||||||
960 | QVarLengthArray<char> url_esc; | - | ||||||
961 | url_esc.reserve(len + 1); | - | ||||||
962 | for (int j = 0; j < len; j++) { | - | ||||||
963 | if (urlascii[j] == '(' || urlascii[j] == ')' || urlascii[j] == '\\') | - | ||||||
964 | url_esc.append('\\'); | - | ||||||
965 | url_esc.append(urlascii[j]); | - | ||||||
966 | } | - | ||||||
967 | url_esc.append('\0'); | - | ||||||
968 | - | |||||||
969 | char buf[256]; | - | ||||||
970 | const QRectF rr = d->pageMatrix().mapRect(r); | - | ||||||
971 | d->xprintf("<<\n/Type /Annot\n/Subtype /Link\n/Rect ["); | - | ||||||
972 | d->xprintf("%s ", qt_real_to_string(rr.left(), buf)); | - | ||||||
973 | d->xprintf("%s ", qt_real_to_string(rr.top(), buf)); | - | ||||||
974 | d->xprintf("%s ", qt_real_to_string(rr.right(), buf)); | - | ||||||
975 | d->xprintf("%s", qt_real_to_string(rr.bottom(), buf)); | - | ||||||
976 | d->xprintf("]\n/Border [0 0 0]\n/A <<\n"); | - | ||||||
977 | d->xprintf("/Type /Action\n/S /URI\n/URI (%s)\n", url_esc.constData()); | - | ||||||
978 | d->xprintf(">>\n>>\n"); | - | ||||||
979 | d->xprintf("endobj\n"); | - | ||||||
980 | d->currentPage->annotations.append(annot); | - | ||||||
981 | } | - | ||||||
982 | - | |||||||
983 | void QPdfEngine::updateState(const QPaintEngineState &state) | - | ||||||
984 | { | - | ||||||
985 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
986 | - | |||||||
987 | QPaintEngine::DirtyFlags flags = state.state(); | - | ||||||
988 | - | |||||||
989 | if (flags & DirtyTransform) | - | ||||||
990 | d->stroker.matrix = state.transform(); | - | ||||||
991 | - | |||||||
992 | if (flags & DirtyPen) { | - | ||||||
993 | d->pen = state.pen(); | - | ||||||
994 | d->hasPen = d->pen.style() != Qt::NoPen; | - | ||||||
995 | d->stroker.setPen(d->pen, state.renderHints()); | - | ||||||
996 | QBrush penBrush = d->pen.brush(); | - | ||||||
997 | bool oldSimple = d->simplePen; | - | ||||||
998 | d->simplePen = (d->hasPen && (penBrush.style() == Qt::SolidPattern) && penBrush.isOpaque() && d->opacity == 1.0); | - | ||||||
999 | if (oldSimple != d->simplePen) | - | ||||||
1000 | flags |= DirtyTransform; | - | ||||||
1001 | } else if (flags & DirtyHints) { | - | ||||||
1002 | d->stroker.setPen(d->pen, state.renderHints()); | - | ||||||
1003 | } | - | ||||||
1004 | if (flags & DirtyBrush) { | - | ||||||
1005 | d->brush = state.brush(); | - | ||||||
1006 | if (d->brush.color().alpha() == 0 && d->brush.style() == Qt::SolidPattern) | - | ||||||
1007 | d->brush.setStyle(Qt::NoBrush); | - | ||||||
1008 | d->hasBrush = d->brush.style() != Qt::NoBrush; | - | ||||||
1009 | } | - | ||||||
1010 | if (flags & DirtyBrushOrigin) { | - | ||||||
1011 | d->brushOrigin = state.brushOrigin(); | - | ||||||
1012 | flags |= DirtyBrush; | - | ||||||
1013 | } | - | ||||||
1014 | if (flags & DirtyOpacity) { | - | ||||||
1015 | d->opacity = state.opacity(); | - | ||||||
1016 | if (d->simplePen && d->opacity != 1.0) { | - | ||||||
1017 | d->simplePen = false; | - | ||||||
1018 | flags |= DirtyTransform; | - | ||||||
1019 | } | - | ||||||
1020 | } | - | ||||||
1021 | - | |||||||
1022 | bool ce = d->clipEnabled; | - | ||||||
1023 | if (flags & DirtyClipPath) { | - | ||||||
1024 | d->clipEnabled = true; | - | ||||||
1025 | updateClipPath(state.clipPath(), state.clipOperation()); | - | ||||||
1026 | } else if (flags & DirtyClipRegion) { | - | ||||||
1027 | d->clipEnabled = true; | - | ||||||
1028 | QPainterPath path; | - | ||||||
1029 | QVector<QRect> rects = state.clipRegion().rects(); | - | ||||||
1030 | for (int i = 0; i < rects.size(); ++i) | - | ||||||
1031 | path.addRect(rects.at(i)); | - | ||||||
1032 | updateClipPath(path, state.clipOperation()); | - | ||||||
1033 | flags |= DirtyClipPath; | - | ||||||
1034 | } else if (flags & DirtyClipEnabled) { | - | ||||||
1035 | d->clipEnabled = state.isClipEnabled(); | - | ||||||
1036 | } | - | ||||||
1037 | - | |||||||
1038 | if (ce != d->clipEnabled) | - | ||||||
1039 | flags |= DirtyClipPath; | - | ||||||
1040 | else if (!d->clipEnabled) | - | ||||||
1041 | flags &= ~DirtyClipPath; | - | ||||||
1042 | - | |||||||
1043 | setupGraphicsState(flags); | - | ||||||
1044 | } | - | ||||||
1045 | - | |||||||
1046 | void QPdfEngine::setupGraphicsState(QPaintEngine::DirtyFlags flags) | - | ||||||
1047 | { | - | ||||||
1048 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1049 | if (flags & DirtyClipPath) | - | ||||||
1050 | flags |= DirtyTransform|DirtyPen|DirtyBrush; | - | ||||||
1051 | - | |||||||
1052 | if (flags & DirtyTransform) { | - | ||||||
1053 | *d->currentPage << "Q\n"; | - | ||||||
1054 | flags |= DirtyPen|DirtyBrush; | - | ||||||
1055 | } | - | ||||||
1056 | - | |||||||
1057 | if (flags & DirtyClipPath) { | - | ||||||
1058 | *d->currentPage << "Q q\n"; | - | ||||||
1059 | - | |||||||
1060 | d->allClipped = false; | - | ||||||
1061 | if (d->clipEnabled && !d->clips.isEmpty()) { | - | ||||||
1062 | for (int i = 0; i < d->clips.size(); ++i) { | - | ||||||
1063 | if (d->clips.at(i).isEmpty()) { | - | ||||||
1064 | d->allClipped = true; | - | ||||||
1065 | break; | - | ||||||
1066 | } | - | ||||||
1067 | } | - | ||||||
1068 | if (!d->allClipped) { | - | ||||||
1069 | for (int i = 0; i < d->clips.size(); ++i) { | - | ||||||
1070 | *d->currentPage << QPdf::generatePath(d->clips.at(i), QTransform(), QPdf::ClipPath); | - | ||||||
1071 | } | - | ||||||
1072 | } | - | ||||||
1073 | } | - | ||||||
1074 | } | - | ||||||
1075 | - | |||||||
1076 | if (flags & DirtyTransform) { | - | ||||||
1077 | *d->currentPage << "q\n"; | - | ||||||
1078 | if (d->simplePen && !d->stroker.matrix.isIdentity()) | - | ||||||
1079 | *d->currentPage << QPdf::generateMatrix(d->stroker.matrix); | - | ||||||
1080 | } | - | ||||||
1081 | if (flags & DirtyBrush) | - | ||||||
1082 | setBrush(); | - | ||||||
1083 | if (d->simplePen && (flags & DirtyPen)) | - | ||||||
1084 | setPen(); | - | ||||||
1085 | } | - | ||||||
1086 | - | |||||||
1087 | extern QPainterPath qt_regionToPath(const QRegion ®ion); | - | ||||||
1088 | - | |||||||
1089 | void QPdfEngine::updateClipPath(const QPainterPath &p, Qt::ClipOperation op) | - | ||||||
1090 | { | - | ||||||
1091 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1092 | QPainterPath path = d->stroker.matrix.map(p); | - | ||||||
1093 | - | |||||||
1094 | - | |||||||
1095 | if (op == Qt::NoClip) { | - | ||||||
1096 | d->clipEnabled = false; | - | ||||||
1097 | d->clips.clear(); | - | ||||||
1098 | } else if (op == Qt::ReplaceClip) { | - | ||||||
1099 | d->clips.clear(); | - | ||||||
1100 | d->clips.append(path); | - | ||||||
1101 | } else if (op == Qt::IntersectClip) { | - | ||||||
1102 | d->clips.append(path); | - | ||||||
1103 | } else { | - | ||||||
1104 | - | |||||||
1105 | path = painter()->clipPath(); | - | ||||||
1106 | path = d->stroker.matrix.map(path); | - | ||||||
1107 | d->clips.clear(); | - | ||||||
1108 | d->clips.append(path); | - | ||||||
1109 | } | - | ||||||
1110 | } | - | ||||||
1111 | - | |||||||
1112 | void QPdfEngine::setPen() | - | ||||||
1113 | { | - | ||||||
1114 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1115 | if (d->pen.style() == Qt::NoPen) | - | ||||||
1116 | return; | - | ||||||
1117 | QBrush b = d->pen.brush(); | - | ||||||
1118 | ((!(b.style() == Qt::SolidPattern && b.isOpaque())) ? qt_assert("b.style() == Qt::SolidPattern && b.isOpaque()",__FILE__,11641170) : qt_noop()); | - | ||||||
1119 | - | |||||||
1120 | QColor rgba = b.color(); | - | ||||||
1121 | if (d->grayscale) { | - | ||||||
1122 | qreal gray = qGray(rgba.rgba())/255.; | - | ||||||
1123 | *d->currentPage << gray << gray << gray; | - | ||||||
1124 | } else { | - | ||||||
1125 | *d->currentPage << rgba.redF() | - | ||||||
1126 | << rgba.greenF() | - | ||||||
1127 | << rgba.blueF(); | - | ||||||
1128 | } | - | ||||||
1129 | *d->currentPage << "SCN\n"; | - | ||||||
1130 | - | |||||||
1131 | *d->currentPage << d->pen.widthF() << "w "; | - | ||||||
1132 | - | |||||||
1133 | int pdfCapStyle = 0; | - | ||||||
1134 | switch(d->pen.capStyle()) { | - | ||||||
1135 | case Qt::FlatCap: | - | ||||||
1136 | pdfCapStyle = 0; | - | ||||||
1137 | break; | - | ||||||
1138 | case Qt::SquareCap: | - | ||||||
1139 | pdfCapStyle = 2; | - | ||||||
1140 | break; | - | ||||||
1141 | case Qt::RoundCap: | - | ||||||
1142 | pdfCapStyle = 1; | - | ||||||
1143 | break; | - | ||||||
1144 | default: | - | ||||||
1145 | break; | - | ||||||
1146 | } | - | ||||||
1147 | *d->currentPage << pdfCapStyle << "J "; | - | ||||||
1148 | - | |||||||
1149 | int pdfJoinStyle = 0; | - | ||||||
1150 | switch(d->pen.joinStyle()) { | - | ||||||
1151 | case Qt::MiterJoin: | - | ||||||
1152 | case Qt::SvgMiterJoin: | - | ||||||
1153 | *d->currentPage << qMax(qreal(1.0), d->pen.miterLimit()) << "M "; | - | ||||||
1154 | pdfJoinStyle = 0; | - | ||||||
1155 | break; | - | ||||||
1156 | case Qt::BevelJoin: | - | ||||||
1157 | pdfJoinStyle = 2; | - | ||||||
1158 | break; | - | ||||||
1159 | case Qt::RoundJoin: | - | ||||||
1160 | pdfJoinStyle = 1; | - | ||||||
1161 | break; | - | ||||||
1162 | default: | - | ||||||
1163 | break; | - | ||||||
1164 | } | - | ||||||
1165 | *d->currentPage << pdfJoinStyle << "j "; | - | ||||||
1166 | - | |||||||
1167 | *d->currentPage << QPdf::generateDashes(d->pen); | - | ||||||
1168 | } | - | ||||||
1169 | - | |||||||
1170 | - | |||||||
1171 | void QPdfEngine::setBrush() | - | ||||||
1172 | { | - | ||||||
1173 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1174 | Qt::BrushStyle style = d->brush.style(); | - | ||||||
1175 | if (style == Qt::NoBrush) | - | ||||||
1176 | return; | - | ||||||
1177 | - | |||||||
1178 | bool specifyColor; | - | ||||||
1179 | int gStateObject = 0; | - | ||||||
1180 | int patternObject = d->addBrushPattern(d->stroker.matrix, &specifyColor, &gStateObject); | - | ||||||
1181 | if (!patternObject && !specifyColor) | - | ||||||
1182 | return; | - | ||||||
1183 | - | |||||||
1184 | *d->currentPage << (patternObject ? "/PCSp cs " : "/CSp cs "); | - | ||||||
1185 | if (specifyColor) { | - | ||||||
1186 | QColor rgba = d->brush.color(); | - | ||||||
1187 | if (d->grayscale) { | - | ||||||
1188 | qreal gray = qGray(rgba.rgba())/255.; | - | ||||||
1189 | *d->currentPage << gray << gray << gray; | - | ||||||
1190 | } else { | - | ||||||
1191 | *d->currentPage << rgba.redF() | - | ||||||
1192 | << rgba.greenF() | - | ||||||
1193 | << rgba.blueF(); | - | ||||||
1194 | } | - | ||||||
1195 | } | - | ||||||
1196 | if (patternObject) | - | ||||||
1197 | *d->currentPage << "/Pat" << patternObject; | - | ||||||
1198 | *d->currentPage << "scn\n"; | - | ||||||
1199 | - | |||||||
1200 | if (gStateObject) | - | ||||||
1201 | *d->currentPage << "/GState" << gStateObject << "gs\n"; | - | ||||||
1202 | else | - | ||||||
1203 | *d->currentPage << "/GSa gs\n"; | - | ||||||
1204 | } | - | ||||||
1205 | - | |||||||
1206 | - | |||||||
1207 | bool QPdfEngine::newPage() | - | ||||||
1208 | { | - | ||||||
1209 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1210 | if (!isActive()) | - | ||||||
1211 | return false; | - | ||||||
1212 | d->newPage(); | - | ||||||
1213 | - | |||||||
1214 | setupGraphicsState(DirtyBrush|DirtyPen|DirtyClipPath); | - | ||||||
1215 | QFile *outfile = qobject_cast<QFile*> (d->outDevice); | - | ||||||
1216 | if (outfile && outfile->error() != QFile::NoError) | - | ||||||
1217 | return false; | - | ||||||
1218 | return true; | - | ||||||
1219 | } | - | ||||||
1220 | - | |||||||
1221 | QPaintEngine::Type QPdfEngine::type() const | - | ||||||
1222 | { | - | ||||||
1223 | return QPaintEngine::Pdf; | - | ||||||
1224 | } | - | ||||||
1225 | - | |||||||
1226 | void QPdfEngine::setResolution(int resolution) | - | ||||||
1227 | { | - | ||||||
1228 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1229 | d->resolution = resolution; | - | ||||||
1230 | } | - | ||||||
1231 | - | |||||||
1232 | int QPdfEngine::resolution() const | - | ||||||
1233 | { | - | ||||||
1234 | const QPdfEnginePrivate * const d = d_func(); | - | ||||||
1235 | return d->resolution; | - | ||||||
1236 | } | - | ||||||
1237 | - | |||||||
1238 | void QPdfEngine::setPageLayout(const QPageLayout &pageLayout) | - | ||||||
1239 | { | - | ||||||
1240 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1241 | d->m_pageLayout = pageLayout; | - | ||||||
1242 | } | - | ||||||
1243 | - | |||||||
1244 | void QPdfEngine::setPageSize(const QPageSize &pageSize) | - | ||||||
1245 | { | - | ||||||
1246 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1247 | d->m_pageLayout.setPageSize(pageSize); | - | ||||||
1248 | } | - | ||||||
1249 | - | |||||||
1250 | void QPdfEngine::setPageOrientation(QPageLayout::Orientation orientation) | - | ||||||
1251 | { | - | ||||||
1252 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1253 | d->m_pageLayout.setOrientation(orientation); | - | ||||||
1254 | } | - | ||||||
1255 | - | |||||||
1256 | void QPdfEngine::setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) | - | ||||||
1257 | { | - | ||||||
1258 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1259 | d->m_pageLayout.setUnits(units); | - | ||||||
1260 | d->m_pageLayout.setMargins(margins); | - | ||||||
1261 | } | - | ||||||
1262 | - | |||||||
1263 | QPageLayout QPdfEngine::pageLayout() const | - | ||||||
1264 | { | - | ||||||
1265 | const QPdfEnginePrivate * const d = d_func(); | - | ||||||
1266 | return d->m_pageLayout; | - | ||||||
1267 | } | - | ||||||
1268 | - | |||||||
1269 | - | |||||||
1270 | int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const | - | ||||||
1271 | { | - | ||||||
1272 | const QPdfEnginePrivate * const d = d_func(); | - | ||||||
1273 | int val; | - | ||||||
1274 | switch (metricType) { | - | ||||||
1275 | case QPaintDevice::PdmWidth: | - | ||||||
1276 | val = d->m_pageLayout.paintRectPixels(d->resolution).width(); | - | ||||||
1277 | break; | - | ||||||
1278 | case QPaintDevice::PdmHeight: | - | ||||||
1279 | val = d->m_pageLayout.paintRectPixels(d->resolution).height(); | - | ||||||
1280 | break; | - | ||||||
1281 | case QPaintDevice::PdmDpiX: | - | ||||||
1282 | case QPaintDevice::PdmDpiY: | - | ||||||
1283 | val = d->resolution; | - | ||||||
1284 | break; | - | ||||||
1285 | case QPaintDevice::PdmPhysicalDpiX: | - | ||||||
1286 | case QPaintDevice::PdmPhysicalDpiY: | - | ||||||
1287 | val = 1200; | - | ||||||
1288 | break; | - | ||||||
1289 | case QPaintDevice::PdmWidthMM: | - | ||||||
1290 | val = qRound(d->m_pageLayout.paintRect(QPageLayout::Millimeter).width()); | - | ||||||
1291 | break; | - | ||||||
1292 | case QPaintDevice::PdmHeightMM: | - | ||||||
1293 | val = qRound(d->m_pageLayout.paintRect(QPageLayout::Millimeter).height()); | - | ||||||
1294 | break; | - | ||||||
1295 | case QPaintDevice::PdmNumColors: | - | ||||||
1296 | val = 2147483647; | - | ||||||
1297 | break; | - | ||||||
1298 | case QPaintDevice::PdmDepth: | - | ||||||
1299 | val = 32; | - | ||||||
1300 | break; | - | ||||||
1301 | case QPaintDevice::PdmDevicePixelRatio: | - | ||||||
1302 | val = 1; | - | ||||||
1303 | break; | - | ||||||
1304 | case QPaintDevice::PdmDevicePixelRatioScaled: | - | ||||||
1305 | val = 1 * QPaintDevice::devicePixelRatioFScale(); | - | ||||||
1306 | break; | - | ||||||
1307 | default: | - | ||||||
1308 | QMessageLogger(__FILE__, 13541360, __PRETTY_FUNCTION__).warning("QPdfWriter::metric: Invalid metric command"); | - | ||||||
1309 | return 0; | - | ||||||
1310 | } | - | ||||||
1311 | return val; | - | ||||||
1312 | } | - | ||||||
1313 | - | |||||||
1314 | QPdfEnginePrivate::QPdfEnginePrivate() | - | ||||||
1315 | : clipEnabled(false), allClipped(false), hasPen(true), hasBrush(false), simplePen(false), | - | ||||||
1316 | outDevice(0), ownsDevice(false), | - | ||||||
1317 | embedFonts(true), | - | ||||||
1318 | grayscale(false), | - | ||||||
1319 | m_pageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF(10, 10, 10, 10)) | - | ||||||
1320 | { | - | ||||||
1321 | resolution = 1200; | - | ||||||
1322 | currentObject = 1; | - | ||||||
1323 | currentPage = 0; | - | ||||||
1324 | stroker.stream = 0; | - | ||||||
1325 | - | |||||||
1326 | streampos = 0; | - | ||||||
1327 | - | |||||||
1328 | stream = new QDataStream; | - | ||||||
1329 | } | - | ||||||
1330 | - | |||||||
1331 | bool QPdfEngine::begin(QPaintDevice *pdev) | - | ||||||
1332 | { | - | ||||||
1333 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1334 | d->pdev = pdev; | - | ||||||
1335 | - | |||||||
1336 | if (!d->outDevice) { | - | ||||||
1337 | if (!d->outputFileName.isEmpty()) { | - | ||||||
1338 | QFile *file = new QFile(d->outputFileName); | - | ||||||
1339 | if (!file->open(QFile::WriteOnly|QFile::Truncate)) { | - | ||||||
1340 | delete file; | - | ||||||
1341 | return false; | - | ||||||
1342 | } | - | ||||||
1343 | d->outDevice = file; | - | ||||||
1344 | } else { | - | ||||||
1345 | return false; | - | ||||||
1346 | } | - | ||||||
1347 | d->ownsDevice = true; | - | ||||||
1348 | } | - | ||||||
1349 | - | |||||||
1350 | d->currentObject = 1; | - | ||||||
1351 | - | |||||||
1352 | d->currentPage = new QPdfPage; | - | ||||||
1353 | d->stroker.stream = d->currentPage; | - | ||||||
1354 | d->opacity = 1.0; | - | ||||||
1355 | - | |||||||
1356 | d->stream->setDevice(d->outDevice); | - | ||||||
1357 | - | |||||||
1358 | d->streampos = 0; | - | ||||||
1359 | d->hasPen = true; | - | ||||||
1360 | d->hasBrush = false; | - | ||||||
1361 | d->clipEnabled = false; | - | ||||||
1362 | d->allClipped = false; | - | ||||||
1363 | - | |||||||
1364 | d->xrefPositions.clear(); | - | ||||||
1365 | d->pageRoot = 0; | - | ||||||
1366 | d->catalog = 0; | - | ||||||
1367 | d->info = 0; | - | ||||||
1368 | d->graphicsState = 0; | - | ||||||
1369 | d->patternColorSpace = 0; | - | ||||||
1370 | d->simplePen = false; | - | ||||||
1371 | - | |||||||
1372 | d->pages.clear(); | - | ||||||
1373 | d->imageCache.clear(); | - | ||||||
1374 | d->alphaCache.clear(); | - | ||||||
1375 | - | |||||||
1376 | setActive(true); | - | ||||||
1377 | d->writeHeader(); | - | ||||||
1378 | newPage(); | - | ||||||
1379 | - | |||||||
1380 | return true; | - | ||||||
1381 | } | - | ||||||
1382 | - | |||||||
1383 | bool QPdfEngine::end() | - | ||||||
1384 | { | - | ||||||
1385 | QPdfEnginePrivate * const d = d_func(); | - | ||||||
1386 | d->writeTail(); | - | ||||||
1387 | - | |||||||
1388 | d->stream->unsetDevice(); | - | ||||||
1389 | - | |||||||
1390 | qDeleteAll(d->fonts); | - | ||||||
1391 | d->fonts.clear(); | - | ||||||
1392 | delete d->currentPage; | - | ||||||
1393 | d->currentPage = 0; | - | ||||||
1394 | - | |||||||
1395 | if (d->outDevice && d->ownsDevice) { | - | ||||||
1396 | d->outDevice->close(); | - | ||||||
1397 | delete d->outDevice; | - | ||||||
1398 | d->outDevice = 0; | - | ||||||
1399 | } | - | ||||||
1400 | - | |||||||
1401 | setActive(false); | - | ||||||
1402 | return true; | - | ||||||
1403 | } | - | ||||||
1404 | - | |||||||
1405 | QPdfEnginePrivate::~QPdfEnginePrivate() | - | ||||||
1406 | { | - | ||||||
1407 | qDeleteAll(fonts); | - | ||||||
1408 | delete currentPage; | - | ||||||
1409 | delete stream; | - | ||||||
1410 | } | - | ||||||
1411 | - | |||||||
1412 | void QPdfEnginePrivate::writeHeader() | - | ||||||
1413 | { | - | ||||||
1414 | addXrefEntry(0,false); | - | ||||||
1415 | - | |||||||
1416 | xprintf("%%PDF-1.4\n"); | - | ||||||
1417 | - | |||||||
1418 | writeInfo(); | - | ||||||
1419 | - | |||||||
1420 | catalog = addXrefEntry(-1); | - | ||||||
1421 | pageRoot = requestObject(); | - | ||||||
1422 | xprintf("<<\n" | - | ||||||
1423 | "/Type /Catalog\n" | - | ||||||
1424 | "/Pages %d 0 R\n" | - | ||||||
1425 | ">>\n" | - | ||||||
1426 | "endobj\n", pageRoot); | - | ||||||
1427 | - | |||||||
1428 | - | |||||||
1429 | graphicsState = addXrefEntry(-1); | - | ||||||
1430 | xprintf("<<\n" | - | ||||||
1431 | "/Type /ExtGState\n" | - | ||||||
1432 | "/SA true\n" | - | ||||||
1433 | "/SM 0.02\n" | - | ||||||
1434 | "/ca 1.0\n" | - | ||||||
1435 | "/CA 1.0\n" | - | ||||||
1436 | "/AIS false\n" | - | ||||||
1437 | "/SMask /None" | - | ||||||
1438 | ">>\n" | - | ||||||
1439 | "endobj\n"); | - | ||||||
1440 | - | |||||||
1441 | - | |||||||
1442 | patternColorSpace = addXrefEntry(-1); | - | ||||||
1443 | xprintf("[/Pattern /DeviceRGB]\n" | - | ||||||
1444 | "endobj\n"); | - | ||||||
1445 | } | - | ||||||
1446 | - | |||||||
1447 | void QPdfEnginePrivate::writeInfo() | - | ||||||
1448 | { | - | ||||||
1449 | info = addXrefEntry(-1); | - | ||||||
1450 | xprintf("<<\n/Title "); | - | ||||||
1451 | printString(title); | - | ||||||
1452 | xprintf("\n/Creator "); | - | ||||||
1453 | printString(creator); | - | ||||||
1454 | xprintf("\n/Producer "); | - | ||||||
1455 | printString(QString::fromLatin1("Qt " "5.67.4"1")); | - | ||||||
1456 | QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||
1457 | QTime t = now.time(); | - | ||||||
1458 | QDate d = now.date(); | - | ||||||
1459 | xprintf("\n/CreationDate (D:%d%02d%02d%02d%02d%02d)\n", | - | ||||||
1460 | d.year(), | - | ||||||
1461 | d.month(), | - | ||||||
1462 | d.day(), | - | ||||||
1463 | t.hour(), | - | ||||||
1464 | t.minute(), | - | ||||||
1465 | t.second()); | - | ||||||
1466 | xprintf(">>\n" | - | ||||||
1467 | "endobj\n"); | - | ||||||
1468 | } | - | ||||||
1469 | - | |||||||
1470 | void QPdfEnginePrivate::writePageRoot() | - | ||||||
1471 | { | - | ||||||
1472 | addXrefEntry(pageRoot); | - | ||||||
1473 | - | |||||||
1474 | xprintf("<<\n" | - | ||||||
1475 | "/Type /Pages\n" | - | ||||||
1476 | "/Kids \n" | - | ||||||
1477 | "[\n"); | - | ||||||
1478 | int size = pages.size(); | - | ||||||
1479 | for (int i = 0; i < size; ++i) | - | ||||||
1480 | xprintf("%d 0 R\n", pages[i]); | - | ||||||
1481 | xprintf("]\n"); | - | ||||||
1482 | - | |||||||
1483 | - | |||||||
1484 | xprintf("/Count %d\n", pages.size()); | - | ||||||
1485 | - | |||||||
1486 | xprintf("/ProcSet [/PDF /Text /ImageB /ImageC]\n" | - | ||||||
1487 | ">>\n" | - | ||||||
1488 | "endobj\n"); | - | ||||||
1489 | } | - | ||||||
1490 | - | |||||||
1491 | - | |||||||
1492 | void QPdfEnginePrivate::embedFont(QFontSubset *font) | - | ||||||
1493 | { | - | ||||||
1494 | - | |||||||
1495 | int fontObject = font->object_id; | - | ||||||
1496 | QByteArray fontData = font->toTruetype(); | - | ||||||
1497 | int fontDescriptor = requestObject(); | - | ||||||
1498 | int fontstream = requestObject(); | - | ||||||
1499 | int cidfont = requestObject(); | - | ||||||
1500 | int toUnicode = requestObject(); | - | ||||||
1501 | - | |||||||
1502 | QFontEngine::Properties properties = font->fontEngine->properties(); | - | ||||||
1503 | QByteArray postscriptName = properties.postscriptName.replace(' ', '_'); | - | ||||||
1504 | - | |||||||
1505 | { | - | ||||||
1506 | qreal scale = 1000/properties.emSquare.toReal(); | - | ||||||
1507 | addXrefEntry(fontDescriptor); | - | ||||||
1508 | QByteArray descriptor; | - | ||||||
1509 | QPdf::ByteStream s(&descriptor); | - | ||||||
1510 | s << "<< /Type /FontDescriptor\n" | - | ||||||
1511 | "/FontName /Q"; | - | ||||||
1512 | int tag = fontDescriptor; | - | ||||||
1513 | for (int i = 0; i < 5; ++i) { | - | ||||||
1514 | s << (char)('A' + (tag % 26)); | - | ||||||
1515 | tag /= 26; | - | ||||||
1516 | } | - | ||||||
1517 | s << '+' << postscriptName << "\n" | - | ||||||
1518 | "/Flags " << 4 << "\n" | - | ||||||
1519 | "/FontBBox [" | - | ||||||
1520 | << properties.boundingBox.x()*scale | - | ||||||
1521 | << -(properties.boundingBox.y() + properties.boundingBox.height())*scale | - | ||||||
1522 | << (properties.boundingBox.x() + properties.boundingBox.width())*scale | - | ||||||
1523 | << -properties.boundingBox.y()*scale << "]\n" | - | ||||||
1524 | "/ItalicAngle " << properties.italicAngle.toReal() << "\n" | - | ||||||
1525 | "/Ascent " << properties.ascent.toReal()*scale << "\n" | - | ||||||
1526 | "/Descent " << -properties.descent.toReal()*scale << "\n" | - | ||||||
1527 | "/CapHeight " << properties.capHeight.toReal()*scale << "\n" | - | ||||||
1528 | "/StemV " << properties.lineWidth.toReal()*scale << "\n" | - | ||||||
1529 | "/FontFile2 " << fontstream << "0 R\n" | - | ||||||
1530 | ">> endobj\n"; | - | ||||||
1531 | write(descriptor); | - | ||||||
1532 | } | - | ||||||
1533 | { | - | ||||||
1534 | addXrefEntry(fontstream); | - | ||||||
1535 | QByteArray header; | - | ||||||
1536 | QPdf::ByteStream s(&header); | - | ||||||
1537 | - | |||||||
1538 | int length_object = requestObject(); | - | ||||||
1539 | s << "<<\n" | - | ||||||
1540 | "/Length1 " << fontData.size() << "\n" | - | ||||||
1541 | "/Length " << length_object << "0 R\n"; | - | ||||||
1542 | if (do_compress) | - | ||||||
1543 | s << "/Filter /FlateDecode\n"; | - | ||||||
1544 | s << ">>\n" | - | ||||||
1545 | "stream\n"; | - | ||||||
1546 | write(header); | - | ||||||
1547 | int len = writeCompressed(fontData); | - | ||||||
1548 | write("endstream\n" | - | ||||||
1549 | "endobj\n"); | - | ||||||
1550 | addXrefEntry(length_object); | - | ||||||
1551 | xprintf("%d\n" | - | ||||||
1552 | "endobj\n", len); | - | ||||||
1553 | } | - | ||||||
1554 | { | - | ||||||
1555 | addXrefEntry(cidfont); | - | ||||||
1556 | QByteArray cid; | - | ||||||
1557 | QPdf::ByteStream s(&cid); | - | ||||||
1558 | s << "<< /Type /Font\n" | - | ||||||
1559 | "/Subtype /CIDFontType2\n" | - | ||||||
1560 | "/BaseFont /" << postscriptName << "\n" | - | ||||||
1561 | "/CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >>\n" | - | ||||||
1562 | "/FontDescriptor " << fontDescriptor << "0 R\n" | - | ||||||
1563 | "/CIDToGIDMap /Identity\n" | - | ||||||
1564 | << font->widthArray() << | - | ||||||
1565 | ">>\n" | - | ||||||
1566 | "endobj\n"; | - | ||||||
1567 | write(cid); | - | ||||||
1568 | } | - | ||||||
1569 | { | - | ||||||
1570 | addXrefEntry(toUnicode); | - | ||||||
1571 | QByteArray touc = font->createToUnicodeMap(); | - | ||||||
1572 | xprintf("<< /Length %d >>\n" | - | ||||||
1573 | "stream\n", touc.length()); | - | ||||||
1574 | write(touc); | - | ||||||
1575 | write("endstream\n" | - | ||||||
1576 | "endobj\n"); | - | ||||||
1577 | } | - | ||||||
1578 | { | - | ||||||
1579 | addXrefEntry(fontObject); | - | ||||||
1580 | QByteArray font; | - | ||||||
1581 | QPdf::ByteStream s(&font); | - | ||||||
1582 | s << "<< /Type /Font\n" | - | ||||||
1583 | "/Subtype /Type0\n" | - | ||||||
1584 | "/BaseFont /" << postscriptName << "\n" | - | ||||||
1585 | "/Encoding /Identity-H\n" | - | ||||||
1586 | "/DescendantFonts [" << cidfont << "0 R]\n" | - | ||||||
1587 | "/ToUnicode " << toUnicode << "0 R" | - | ||||||
1588 | ">>\n" | - | ||||||
1589 | "endobj\n"; | - | ||||||
1590 | write(font); | - | ||||||
1591 | } | - | ||||||
1592 | } | - | ||||||
1593 | - | |||||||
1594 | - | |||||||
1595 | void QPdfEnginePrivate::writeFonts() | - | ||||||
1596 | { | - | ||||||
1597 | for (QHash<QFontEngine::FaceId, QFontSubset *>::iterator it = fonts.begin(); it != fonts.end(); ++it) { | - | ||||||
1598 | embedFont(*it); | - | ||||||
1599 | delete *it; | - | ||||||
1600 | } | - | ||||||
1601 | fonts.clear(); | - | ||||||
1602 | } | - | ||||||
1603 | - | |||||||
1604 | void QPdfEnginePrivate::writePage() | - | ||||||
1605 | { | - | ||||||
1606 | if (pages.empty()
| 0 | ||||||
1607 | return; never executed: return; | 0 | ||||||
1608 | - | |||||||
1609 | *currentPage << "Q Q\n"; | - | ||||||
1610 | - | |||||||
1611 | uint pageStream = requestObject(); | - | ||||||
1612 | uint pageStreamLength = requestObject(); | - | ||||||
1613 | uint resources = requestObject(); | - | ||||||
1614 | uint annots = requestObject(); | - | ||||||
1615 | - | |||||||
1616 | addXrefEntry(pages.lastconstLast()); | - | ||||||
1617 | xprintf("<<\n" | - | ||||||
1618 | "/Type /Page\n" | - | ||||||
1619 | "/Parent %d 0 R\n" | - | ||||||
1620 | "/Contents %d 0 R\n" | - | ||||||
1621 | "/Resources %d 0 R\n" | - | ||||||
1622 | "/Annots %d 0 R\n" | - | ||||||
1623 | "/MediaBox [0 0 %d %d]\n" | - | ||||||
1624 | ">>\n" | - | ||||||
1625 | "endobj\n", | - | ||||||
1626 | pageRoot, pageStream, resources, annots, | - | ||||||
1627 | - | |||||||
1628 | currentPage->pageSize.width(), currentPage->pageSize.height()); | - | ||||||
1629 | - | |||||||
1630 | addXrefEntry(resources); | - | ||||||
1631 | xprintf("<<\n" | - | ||||||
1632 | "/ColorSpace <<\n" | - | ||||||
1633 | "/PCSp %d 0 R\n" | - | ||||||
1634 | "/CSp /DeviceRGB\n" | - | ||||||
1635 | "/CSpg /DeviceGray\n" | - | ||||||
1636 | ">>\n" | - | ||||||
1637 | "/ExtGState <<\n" | - | ||||||
1638 | "/GSa %d 0 R\n", | - | ||||||
1639 | patternColorSpace, graphicsState); | - | ||||||
1640 | - | |||||||
1641 | for (int i = 0; i < currentPage->graphicStates.size()
| 0 | ||||||
1642 | xprintf("/GState%d %d 0 R\n", currentPage->graphicStates.at(i), currentPage->graphicStates.at(i)); never executed: xprintf("/GState%d %d 0 R\n", currentPage->graphicStates.at(i), currentPage->graphicStates.at(i)); | 0 | ||||||
1643 | xprintf(">>\n"); | - | ||||||
1644 | - | |||||||
1645 | xprintf("/Pattern <<\n"); | - | ||||||
1646 | for (int i = 0; i < currentPage->patterns.size()
| 0 | ||||||
1647 | xprintf("/Pat%d %d 0 R\n", currentPage->patterns.at(i), currentPage->patterns.at(i)); never executed: xprintf("/Pat%d %d 0 R\n", currentPage->patterns.at(i), currentPage->patterns.at(i)); | 0 | ||||||
1648 | xprintf(">>\n"); | - | ||||||
1649 | - | |||||||
1650 | xprintf("/Font <<\n"); | - | ||||||
1651 | for (int i = 0; i < currentPage->fonts.size()
| 0 | ||||||
1652 | xprintf("/F%d %d 0 R\n", currentPage->fonts[i], currentPage->fonts[i]); never executed: xprintf("/F%d %d 0 R\n", currentPage->fonts[i], currentPage->fonts[i]); | 0 | ||||||
1653 | xprintf(">>\n"); | - | ||||||
1654 | - | |||||||
1655 | xprintf("/XObject <<\n"); | - | ||||||
1656 | for (int i = 0; i<currentPage->images.size()
| 0 | ||||||
1657 | xprintf("/Im%d %d 0 R\n", currentPage->images.at(i), currentPage->images.at(i)); | - | ||||||
1658 | } never executed: end of block | 0 | ||||||
1659 | xprintf(">>\n"); | - | ||||||
1660 | - | |||||||
1661 | xprintf(">>\n" | - | ||||||
1662 | "endobj\n"); | - | ||||||
1663 | - | |||||||
1664 | addXrefEntry(annots); | - | ||||||
1665 | xprintf("[ "); | - | ||||||
1666 | for (int i = 0; i<currentPage->annotations.size()
| 0 | ||||||
1667 | xprintf("%d 0 R ", currentPage->annotations.at(i)); | - | ||||||
1668 | } never executed: end of block | 0 | ||||||
1669 | xprintf("]\nendobj\n"); | - | ||||||
1670 | - | |||||||
1671 | addXrefEntry(pageStream); | - | ||||||
1672 | xprintf("<<\n" | - | ||||||
1673 | "/Length %d 0 R\n", pageStreamLength); | - | ||||||
1674 | if (do_compress
| 0 | ||||||
1675 | xprintf("/Filter /FlateDecode\n"); never executed: xprintf("/Filter /FlateDecode\n"); | 0 | ||||||
1676 | - | |||||||
1677 | xprintf(">>\n"); | - | ||||||
1678 | xprintf("stream\n"); | - | ||||||
1679 | QIODevice *content = currentPage->stream(); | - | ||||||
1680 | int len = writeCompressed(content); | - | ||||||
1681 | xprintf("endstream\n" | - | ||||||
1682 | "endobj\n"); | - | ||||||
1683 | - | |||||||
1684 | addXrefEntry(pageStreamLength); | - | ||||||
1685 | xprintf("%d\nendobj\n",len); | - | ||||||
1686 | } never executed: end of block | 0 | ||||||
1687 | - | |||||||
1688 | void QPdfEnginePrivate::writeTail() | - | ||||||
1689 | { | - | ||||||
1690 | writePage(); | - | ||||||
1691 | writeFonts(); | - | ||||||
1692 | writePageRoot(); | - | ||||||
1693 | addXrefEntry(xrefPositions.size(),false); | - | ||||||
1694 | xprintf("xref\n" | - | ||||||
1695 | "0 %d\n" | - | ||||||
1696 | "%010d 65535 f \n", xrefPositions.size()-1, xrefPositions[0]); | - | ||||||
1697 | - | |||||||
1698 | for (int i = 1; i < xrefPositions.size()-1
| 0 | ||||||
1699 | xprintf("%010d 00000 n \n", xrefPositions[i]); never executed: xprintf("%010d 00000 n \n", xrefPositions[i]); | 0 | ||||||
1700 | - | |||||||
1701 | xprintf("trailer\n" | - | ||||||
1702 | "<<\n" | - | ||||||
1703 | "/Size %d\n" | - | ||||||
1704 | "/Info %d 0 R\n" | - | ||||||
1705 | "/Root %d 0 R\n" | - | ||||||
1706 | ">>\n" | - | ||||||
1707 | "startxref\n%d\n" | - | ||||||
1708 | "%%%%EOF\n", | - | ||||||
1709 | xrefPositions.size()-1, info, catalog, xrefPositions.lastconstLast()); | - | ||||||
1710 | } never executed: end of block | 0 | ||||||
1711 | - | |||||||
1712 | int QPdfEnginePrivate::addXrefEntry(int object, bool printostr) | - | ||||||
1713 | { | - | ||||||
1714 | if (object < 0) | - | ||||||
1715 | object = requestObject(); | - | ||||||
1716 | - | |||||||
1717 | if (object>=xrefPositions.size()) | - | ||||||
1718 | xrefPositions.resize(object+1); | - | ||||||
1719 | - | |||||||
1720 | xrefPositions[object] = streampos; | - | ||||||
1721 | if (printostr) | - | ||||||
1722 | xprintf("%d 0 obj\n",object); | - | ||||||
1723 | - | |||||||
1724 | return object; | - | ||||||
1725 | } | - | ||||||
1726 | - | |||||||
1727 | void QPdfEnginePrivate::printString(const QString &string) { | - | ||||||
1728 | - | |||||||
1729 | - | |||||||
1730 | - | |||||||
1731 | QByteArray array("(\xfe\xff"); | - | ||||||
1732 | const ushort *utf16 = string.utf16(); | - | ||||||
1733 | - | |||||||
1734 | for (int i=0; i < string.size(); ++i) { | - | ||||||
1735 | char part[2] = {char((*(utf16 + i)) >> 8), char((*(utf16 + i)) & 0xff)}; | - | ||||||
1736 | for(int j=0; j < 2; ++j) { | - | ||||||
1737 | if (part[j] == '(' || part[j] == ')' || part[j] == '\\') | - | ||||||
1738 | array.append('\\'); | - | ||||||
1739 | array.append(part[j]); | - | ||||||
1740 | } | - | ||||||
1741 | } | - | ||||||
1742 | array.append(')'); | - | ||||||
1743 | write(array); | - | ||||||
1744 | } | - | ||||||
1745 | - | |||||||
1746 | - | |||||||
1747 | - | |||||||
1748 | void QPdfEnginePrivate::xprintf(const char* fmt, ...) | - | ||||||
1749 | { | - | ||||||
1750 | if (!stream) | - | ||||||
1751 | return; | - | ||||||
1752 | - | |||||||
1753 | const int msize = 10000; | - | ||||||
1754 | char buf[msize]; | - | ||||||
1755 | - | |||||||
1756 | va_list args; | - | ||||||
1757 | __builtin_va_start(args,fmt); | - | ||||||
1758 | int bufsize = qvsnprintf(buf, msize, fmt, args); | - | ||||||
1759 | - | |||||||
1760 | ((!(bufsize<msize)) ? qt_assert("bufsize<msize",__FILE__,18161822) : qt_noop()); | - | ||||||
1761 | - | |||||||
1762 | __builtin_va_end(args); | - | ||||||
1763 | - | |||||||
1764 | stream->writeRawData(buf, bufsize); | - | ||||||
1765 | streampos += bufsize; | - | ||||||
1766 | } | - | ||||||
1767 | - | |||||||
1768 | int QPdfEnginePrivate::writeCompressed(QIODevice *dev) | - | ||||||
1769 | { | - | ||||||
1770 | - | |||||||
1771 | if (do_compress) { | - | ||||||
1772 | int size = QPdfPage::chunkSize(); | - | ||||||
1773 | int sum = 0; | - | ||||||
1774 | ::z_stream zStruct; | - | ||||||
1775 | zStruct.zalloc = 0; | - | ||||||
1776 | zStruct.zfree = 0; | - | ||||||
1777 | zStruct.opaque = 0; | - | ||||||
1778 | if (::deflateInit_((&zStruct), ((-1)), "1.2.8", (int)sizeof(z_stream)) != 0) { | - | ||||||
1779 | QMessageLogger(__FILE__, 18351841, __PRETTY_FUNCTION__).warning("QPdfStream::writeCompressed: Error in deflateInit()"); | - | ||||||
1780 | return sum; | - | ||||||
1781 | } | - | ||||||
1782 | zStruct.avail_in = 0; | - | ||||||
1783 | QByteArray in, out; | - | ||||||
1784 | out.resize(size); | - | ||||||
1785 | while (!dev->atEnd() || zStruct.avail_in != 0) { | - | ||||||
1786 | if (zStruct.avail_in == 0) { | - | ||||||
1787 | in = dev->read(size); | - | ||||||
1788 | zStruct.avail_in = in.size(); | - | ||||||
1789 | zStruct.next_in = reinterpret_cast<unsigned char*>(in.data()); | - | ||||||
1790 | if (in.size() <= 0) { | - | ||||||
1791 | QMessageLogger(__FILE__, 18471853, __PRETTY_FUNCTION__).warning("QPdfStream::writeCompressed: Error in read()"); | - | ||||||
1792 | ::deflateEnd(&zStruct); | - | ||||||
1793 | return sum; | - | ||||||
1794 | } | - | ||||||
1795 | } | - | ||||||
1796 | zStruct.next_out = reinterpret_cast<unsigned char*>(out.data()); | - | ||||||
1797 | zStruct.avail_out = out.size(); | - | ||||||
1798 | if (::deflate(&zStruct, 0) != 0) { | - | ||||||
1799 | QMessageLogger(__FILE__, 18551861, __PRETTY_FUNCTION__).warning("QPdfStream::writeCompressed: Error in deflate()"); | - | ||||||
1800 | ::deflateEnd(&zStruct); | - | ||||||
1801 | return sum; | - | ||||||
1802 | } | - | ||||||
1803 | int written = out.size() - zStruct.avail_out; | - | ||||||
1804 | stream->writeRawData(out.constData(), written); | - | ||||||
1805 | streampos += written; | - | ||||||
1806 | sum += written; | - | ||||||
1807 | } | - | ||||||
1808 | int ret; | - | ||||||
1809 | do { | - | ||||||
1810 | zStruct.next_out = reinterpret_cast<unsigned char*>(out.data()); | - | ||||||
1811 | zStruct.avail_out = out.size(); | - | ||||||
1812 | ret = ::deflate(&zStruct, 4); | - | ||||||
1813 | if (ret != 0 && ret != 1) { | - | ||||||
1814 | QMessageLogger(__FILE__, 18701876, __PRETTY_FUNCTION__).warning("QPdfStream::writeCompressed: Error in deflate()"); | - | ||||||
1815 | ::deflateEnd(&zStruct); | - | ||||||
1816 | return sum; | - | ||||||
1817 | } | - | ||||||
1818 | int written = out.size() - zStruct.avail_out; | - | ||||||
1819 | stream->writeRawData(out.constData(), written); | - | ||||||
1820 | streampos += written; | - | ||||||
1821 | sum += written; | - | ||||||
1822 | } while (ret == 0); | - | ||||||
1823 | - | |||||||
1824 | ::deflateEnd(&zStruct); | - | ||||||
1825 | - | |||||||
1826 | return sum; | - | ||||||
1827 | } else | - | ||||||
1828 | - | |||||||
1829 | { | - | ||||||
1830 | QByteArray arr; | - | ||||||
1831 | int sum = 0; | - | ||||||
1832 | while (!dev->atEnd()) { | - | ||||||
1833 | arr = dev->read(QPdfPage::chunkSize()); | - | ||||||
1834 | stream->writeRawData(arr.constData(), arr.size()); | - | ||||||
1835 | streampos += arr.size(); | - | ||||||
1836 | sum += arr.size(); | - | ||||||
1837 | } | - | ||||||
1838 | return sum; | - | ||||||
1839 | } | - | ||||||
1840 | } | - | ||||||
1841 | - | |||||||
1842 | int QPdfEnginePrivate::writeCompressed(const char *src, int len) | - | ||||||
1843 | { | - | ||||||
1844 | - | |||||||
1845 | if(do_compress) { | - | ||||||
1846 | uLongf destLen = len + len/100 + 13; | - | ||||||
1847 | Bytef* dest = new Bytef[destLen]; | - | ||||||
1848 | if (0 == ::compress(dest, &destLen, (const Bytef*) src, (uLongf)len)) { | - | ||||||
1849 | stream->writeRawData((const char*)dest, destLen); | - | ||||||
1850 | } else { | - | ||||||
1851 | QMessageLogger(__FILE__, 19071913, __PRETTY_FUNCTION__).warning("QPdfStream::writeCompressed: Error in compress()"); | - | ||||||
1852 | destLen = 0; | - | ||||||
1853 | } | - | ||||||
1854 | delete [] dest; | - | ||||||
1855 | len = destLen; | - | ||||||
1856 | } else | - | ||||||
1857 | - | |||||||
1858 | { | - | ||||||
1859 | stream->writeRawData(src,len); | - | ||||||
1860 | } | - | ||||||
1861 | streampos += len; | - | ||||||
1862 | return len; | - | ||||||
1863 | } | - | ||||||
1864 | - | |||||||
1865 | int QPdfEnginePrivate::writeImage(const QByteArray &data, int width, int height, int depth, | - | ||||||
1866 | int maskObject, int softMaskObject, bool dct, bool isMono) | - | ||||||
1867 | { | - | ||||||
1868 | int image = addXrefEntry(-1); | - | ||||||
1869 | xprintf("<<\n" | - | ||||||
1870 | "/Type /XObject\n" | - | ||||||
1871 | "/Subtype /Image\n" | - | ||||||
1872 | "/Width %d\n" | - | ||||||
1873 | "/Height %d\n", width, height); | - | ||||||
1874 | - | |||||||
1875 | if (depth == 1) { | - | ||||||
1876 | if (!isMono) { | - | ||||||
1877 | xprintf("/ImageMask true\n" | - | ||||||
1878 | "/Decode [1 0]\n"); | - | ||||||
1879 | } else { | - | ||||||
1880 | xprintf("/BitsPerComponent 1\n" | - | ||||||
1881 | "/ColorSpace /DeviceGray\n"); | - | ||||||
1882 | } | - | ||||||
1883 | } else { | - | ||||||
1884 | xprintf("/BitsPerComponent 8\n" | - | ||||||
1885 | "/ColorSpace %s\n", (depth == 32) ? "/DeviceRGB" : "/DeviceGray"); | - | ||||||
1886 | } | - | ||||||
1887 | if (maskObject > 0) | - | ||||||
1888 | xprintf("/Mask %d 0 R\n", maskObject); | - | ||||||
1889 | if (softMaskObject > 0) | - | ||||||
1890 | xprintf("/SMask %d 0 R\n", softMaskObject); | - | ||||||
1891 | - | |||||||
1892 | int lenobj = requestObject(); | - | ||||||
1893 | xprintf("/Length %d 0 R\n", lenobj); | - | ||||||
1894 | if (interpolateImages) | - | ||||||
1895 | xprintf("/Interpolate true\n"); | - | ||||||
1896 | int len = 0; | - | ||||||
1897 | if (dct) { | - | ||||||
1898 | - | |||||||
1899 | xprintf("/Filter /DCTDecode\n>>\nstream\n"); | - | ||||||
1900 | write(data); | - | ||||||
1901 | len = data.length(); | - | ||||||
1902 | } else { | - | ||||||
1903 | if (do_compress) | - | ||||||
1904 | xprintf("/Filter /FlateDecode\n>>\nstream\n"); | - | ||||||
1905 | else | - | ||||||
1906 | xprintf(">>\nstream\n"); | - | ||||||
1907 | len = writeCompressed(data); | - | ||||||
1908 | } | - | ||||||
1909 | xprintf("endstream\n" | - | ||||||
1910 | "endobj\n"); | - | ||||||
1911 | addXrefEntry(lenobj); | - | ||||||
1912 | xprintf("%d\n" | - | ||||||
1913 | "endobj\n", len); | - | ||||||
1914 | return image; | - | ||||||
1915 | } | - | ||||||
1916 | - | |||||||
1917 | struct QGradientBound { | - | ||||||
1918 | qreal start; | - | ||||||
1919 | qreal stop; | - | ||||||
1920 | int function; | - | ||||||
1921 | bool reverse; | - | ||||||
1922 | }; | - | ||||||
1923 | template<> class QTypeInfo<QGradientBound > { public: enum { isComplex = (((Q_PRIMITIVE_TYPE) & Q_PRIMITIVE_TYPE) == 0), isStatic = (((Q_PRIMITIVE_TYPE) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), isRelocatable = !isStatic || ((Q_PRIMITIVE_TYPE) & Q_RELOCATABLE_TYPE), isLarge = (sizeof(QGradientBound)>sizeof(void*)), isPointer = false, isIntegral = QtPrivate::is_integral< QGradientBound >::value, isDummy = (((Q_PRIMITIVE_TYPE) & Q_DUMMY_TYPE) != 0), sizeOf = sizeof(QGradientBound) }; static inline const char *name() { return "QGradientBound"; } }; | - | ||||||
1924 | - | |||||||
1925 | int QPdfEnginePrivate::createShadingFunction(const QGradient *gradient, int from, int to, bool reflect, bool alpha) | - | ||||||
1926 | { | - | ||||||
1927 | QGradientStops stops = gradient->stops(); | - | ||||||
1928 | if (stops.isEmpty()) { | - | ||||||
1929 | stops << QGradientStop(0, Qt::black); | - | ||||||
1930 | stops << QGradientStop(1, Qt::white); | - | ||||||
1931 | } | - | ||||||
1932 | if (stops.at(0).first > 0) | - | ||||||
1933 | stops.prepend(QGradientStop(0, stops.at(0).second)); | - | ||||||
1934 | if (stops.at(stops.size() - 1).first < 1) | - | ||||||
1935 | stops.append(QGradientStop(1, stops.at(stops.size() - 1).second)); | - | ||||||
1936 | - | |||||||
1937 | QVector<int> functions; | - | ||||||
1938 | const int numStops = stops.size(); | - | ||||||
1939 | functions.reserve(numStops - 1); | - | ||||||
1940 | for (int i = 0; i < numStops - 1; ++i) { | - | ||||||
1941 | int f = addXrefEntry(-1); | - | ||||||
1942 | QByteArray data; | - | ||||||
1943 | QPdf::ByteStream s(&data); | - | ||||||
1944 | s << "<<\n" | - | ||||||
1945 | "/FunctionType 2\n" | - | ||||||
1946 | "/Domain [0 1]\n" | - | ||||||
1947 | "/N 1\n"; | - | ||||||
1948 | if (alpha) { | - | ||||||
1949 | s << "/C0 [" << stops.at(i).second.alphaF() << "]\n" | - | ||||||
1950 | "/C1 [" << stops.at(i + 1).second.alphaF() << "]\n"; | - | ||||||
1951 | } else { | - | ||||||
1952 | s << "/C0 [" << stops.at(i).second.redF() << stops.at(i).second.greenF() << stops.at(i).second.blueF() << "]\n" | - | ||||||
1953 | "/C1 [" << stops.at(i + 1).second.redF() << stops.at(i + 1).second.greenF() << stops.at(i + 1).second.blueF() << "]\n"; | - | ||||||
1954 | } | - | ||||||
1955 | s << ">>\n" | - | ||||||
1956 | "endobj\n"; | - | ||||||
1957 | write(data); | - | ||||||
1958 | functions << f; | - | ||||||
1959 | } | - | ||||||
1960 | - | |||||||
1961 | QVector<QGradientBound> gradientBounds; | - | ||||||
1962 | gradientBounds.reserve((to - from) * (numStops - 1)); | - | ||||||
1963 | - | |||||||
1964 | for (int step = from; step < to; ++step) { | - | ||||||
1965 | if (reflect && step % 2) { | - | ||||||
1966 | for (int i = numStops - 1; i > 0; --i) { | - | ||||||
1967 | QGradientBound b; | - | ||||||
1968 | b.start = step + 1 - qBound(qreal(0.), stops.at(i).first, qreal(1.)); | - | ||||||
1969 | b.stop = step + 1 - qBound(qreal(0.), stops.at(i - 1).first, qreal(1.)); | - | ||||||
1970 | b.function = functions.at(i - 1); | - | ||||||
1971 | b.reverse = true; | - | ||||||
1972 | gradientBounds << b; | - | ||||||
1973 | } | - | ||||||
1974 | } else { | - | ||||||
1975 | for (int i = 0; i < numStops - 1; ++i) { | - | ||||||
1976 | QGradientBound b; | - | ||||||
1977 | b.start = step + qBound(qreal(0.), stops.at(i).first, qreal(1.)); | - | ||||||
1978 | b.stop = step + qBound(qreal(0.), stops.at(i + 1).first, qreal(1.)); | - | ||||||
1979 | b.function = functions.at(i); | - | ||||||
1980 | b.reverse = false; | - | ||||||
1981 | gradientBounds << b; | - | ||||||
1982 | } | - | ||||||
1983 | } | - | ||||||
1984 | } | - | ||||||
1985 | - | |||||||
1986 | - | |||||||
1987 | qreal bstart = gradientBounds.at(0).start; | - | ||||||
1988 | qreal bend = gradientBounds.at(gradientBounds.size() - 1).stop; | - | ||||||
1989 | qreal norm = 1./(bend - bstart); | - | ||||||
1990 | for (int i = 0; i < gradientBounds.size(); ++i) { | - | ||||||
1991 | gradientBounds[i].start = (gradientBounds[i].start - bstart)*norm; | - | ||||||
1992 | gradientBounds[i].stop = (gradientBounds[i].stop - bstart)*norm; | - | ||||||
1993 | } | - | ||||||
1994 | - | |||||||
1995 | int function; | - | ||||||
1996 | if (gradientBounds.size() > 1) { | - | ||||||
1997 | function = addXrefEntry(-1); | - | ||||||
1998 | QByteArray data; | - | ||||||
1999 | QPdf::ByteStream s(&data); | - | ||||||
2000 | s << "<<\n" | - | ||||||
2001 | "/FunctionType 3\n" | - | ||||||
2002 | "/Domain [0 1]\n" | - | ||||||
2003 | "/Bounds ["; | - | ||||||
2004 | for (int i = 1; i < gradientBounds.size(); ++i) | - | ||||||
2005 | s << gradientBounds.at(i).start; | - | ||||||
2006 | s << "]\n" | - | ||||||
2007 | "/Encode ["; | - | ||||||
2008 | for (int i = 0; i < gradientBounds.size(); ++i) | - | ||||||
2009 | s << (gradientBounds.at(i).reverse ? "1 0 " : "0 1 "); | - | ||||||
2010 | s << "]\n" | - | ||||||
2011 | "/Functions ["; | - | ||||||
2012 | for (int i = 0; i < gradientBounds.size(); ++i) | - | ||||||
2013 | s << gradientBounds.at(i).function << "0 R "; | - | ||||||
2014 | s << "]\n" | - | ||||||
2015 | ">>\n"; | - | ||||||
2016 | write(data); | - | ||||||
2017 | } else { | - | ||||||
2018 | function = functions.at(0); | - | ||||||
2019 | } | - | ||||||
2020 | return function; | - | ||||||
2021 | } | - | ||||||
2022 | - | |||||||
2023 | int QPdfEnginePrivate::generateLinearGradientShader(const QLinearGradient *gradient, const QTransform &matrix, bool alpha) | - | ||||||
2024 | { | - | ||||||
2025 | QPointF start = gradient->start(); | - | ||||||
2026 | QPointF stop = gradient->finalStop(); | - | ||||||
2027 | QPointF offset = stop - start; | - | ||||||
2028 | ((!(gradient->coordinateMode() == QGradient::LogicalMode)) ? qt_assert("gradient->coordinateMode() == QGradient::LogicalMode",__FILE__,20832090) : qt_noop()); | - | ||||||
2029 | - | |||||||
2030 | int from = 0; | - | ||||||
2031 | int to = 1; | - | ||||||
2032 | bool reflect = false; | - | ||||||
2033 | switch (gradient->spread()) { | - | ||||||
2034 | case QGradient::PadSpread: | - | ||||||
2035 | break; | - | ||||||
2036 | case QGradient::ReflectSpread: | - | ||||||
2037 | reflect = true; | - | ||||||
2038 | - | |||||||
2039 | case QGradient::RepeatSpread: { | - | ||||||
2040 | - | |||||||
2041 | QRectF pageRect = m_pageLayout.fullRectPixels(resolution); | - | ||||||
2042 | QTransform inv = matrix.inverted(); | - | ||||||
2043 | QPointF page_rect[4] = { inv.map(pageRect.topLeft()), | - | ||||||
2044 | inv.map(pageRect.topRight()), | - | ||||||
2045 | inv.map(pageRect.bottomLeft()), | - | ||||||
2046 | inv.map(pageRect.bottomRight()) }; | - | ||||||
2047 | - | |||||||
2048 | qreal length = offset.x()*offset.x() + offset.y()*offset.y(); | - | ||||||
2049 | - | |||||||
2050 | - | |||||||
2051 | - | |||||||
2052 | from = 2147483647; | - | ||||||
2053 | to = (-2147483647 - 1); | - | ||||||
2054 | for (int i = 0; i < 4; ++i) { | - | ||||||
2055 | qreal off = ((page_rect[i].x() - start.x()) * offset.x() + (page_rect[i].y() - start.y()) * offset.y())/length; | - | ||||||
2056 | from = qMin(from, qFloor(off)); | - | ||||||
2057 | to = qMax(to, qCeil(off)); | - | ||||||
2058 | } | - | ||||||
2059 | - | |||||||
2060 | stop = start + to * offset; | - | ||||||
2061 | start = start + from * offset; | - | ||||||
2062 | break; | - | ||||||
2063 | } | - | ||||||
2064 | } | - | ||||||
2065 | - | |||||||
2066 | int function = createShadingFunction(gradient, from, to, reflect, alpha); | - | ||||||
2067 | - | |||||||
2068 | QByteArray shader; | - | ||||||
2069 | QPdf::ByteStream s(&shader); | - | ||||||
2070 | s << "<<\n" | - | ||||||
2071 | "/ShadingType 2\n" | - | ||||||
2072 | "/ColorSpace " << (alpha ? "/DeviceGray\n" : "/DeviceRGB\n") << | - | ||||||
2073 | "/AntiAlias true\n" | - | ||||||
2074 | "/Coords [" << start.x() << start.y() << stop.x() << stop.y() << "]\n" | - | ||||||
2075 | "/Extend [true true]\n" | - | ||||||
2076 | "/Function " << function << "0 R\n" | - | ||||||
2077 | ">>\n" | - | ||||||
2078 | "endobj\n"; | - | ||||||
2079 | int shaderObject = addXrefEntry(-1); | - | ||||||
2080 | write(shader); | - | ||||||
2081 | return shaderObject; | - | ||||||
2082 | } | - | ||||||
2083 | - | |||||||
2084 | int QPdfEnginePrivate::generateRadialGradientShader(const QRadialGradient *gradient, const QTransform &matrix, bool alpha) | - | ||||||
2085 | { | - | ||||||
2086 | QPointF p1 = gradient->center(); | - | ||||||
2087 | qreal r1 = gradient->centerRadius(); | - | ||||||
2088 | QPointF p0 = gradient->focalPoint(); | - | ||||||
2089 | qreal r0 = gradient->focalRadius(); | - | ||||||
2090 | - | |||||||
2091 | ((!(gradient->coordinateMode() == QGradient::LogicalMode)) ? qt_assert("gradient->coordinateMode() == QGradient::LogicalMode",__FILE__,21462153) : qt_noop()); | - | ||||||
2092 | - | |||||||
2093 | int from = 0; | - | ||||||
2094 | int to = 1; | - | ||||||
2095 | bool reflect = false; | - | ||||||
2096 | switch (gradient->spread()) { | - | ||||||
2097 | case QGradient::PadSpread: | - | ||||||
2098 | break; | - | ||||||
2099 | case QGradient::ReflectSpread: | - | ||||||
2100 | reflect = true; | - | ||||||
2101 | - | |||||||
2102 | case QGradient::RepeatSpread: { | - | ||||||
2103 | ((!(qFuzzyIsNull(r0))) ? qt_assert("qFuzzyIsNull(r0)",__FILE__,21582165) : qt_noop()); | - | ||||||
2104 | - | |||||||
2105 | QRectF pageRect = m_pageLayout.fullRectPixels(resolution); | - | ||||||
2106 | QTransform inv = matrix.inverted(); | - | ||||||
2107 | QPointF page_rect[4] = { inv.map(pageRect.topLeft()), | - | ||||||
2108 | inv.map(pageRect.topRight()), | - | ||||||
2109 | inv.map(pageRect.bottomLeft()), | - | ||||||
2110 | inv.map(pageRect.bottomRight()) }; | - | ||||||
2111 | - | |||||||
2112 | - | |||||||
2113 | bool done = false; | - | ||||||
2114 | while (!done) { | - | ||||||
2115 | QPointF center = QPointF(p0.x() + to*(p1.x() - p0.x()), p0.y() + to*(p1.y() - p0.y())); | - | ||||||
2116 | double radius = r0 + to*(r1 - r0); | - | ||||||
2117 | double r2 = radius*radius; | - | ||||||
2118 | done = true; | - | ||||||
2119 | for (int i = 0; i < 4; ++i) { | - | ||||||
2120 | QPointF off = page_rect[i] - center; | - | ||||||
2121 | if (off.x()*off.x() + off.y()*off.y() > r2) { | - | ||||||
2122 | ++to; | - | ||||||
2123 | done = false; | - | ||||||
2124 | break; | - | ||||||
2125 | } | - | ||||||
2126 | } | - | ||||||
2127 | } | - | ||||||
2128 | p1 = QPointF(p0.x() + to*(p1.x() - p0.x()), p0.y() + to*(p1.y() - p0.y())); | - | ||||||
2129 | r1 = r0 + to*(r1 - r0); | - | ||||||
2130 | break; | - | ||||||
2131 | } | - | ||||||
2132 | } | - | ||||||
2133 | - | |||||||
2134 | int function = createShadingFunction(gradient, from, to, reflect, alpha); | - | ||||||
2135 | - | |||||||
2136 | QByteArray shader; | - | ||||||
2137 | QPdf::ByteStream s(&shader); | - | ||||||
2138 | s << "<<\n" | - | ||||||
2139 | "/ShadingType 3\n" | - | ||||||
2140 | "/ColorSpace " << (alpha ? "/DeviceGray\n" : "/DeviceRGB\n") << | - | ||||||
2141 | "/AntiAlias true\n" | - | ||||||
2142 | "/Domain [0 1]\n" | - | ||||||
2143 | "/Coords [" << p0.x() << p0.y() << r0 << p1.x() << p1.y() << r1 << "]\n" | - | ||||||
2144 | "/Extend [true true]\n" | - | ||||||
2145 | "/Function " << function << "0 R\n" | - | ||||||
2146 | ">>\n" | - | ||||||
2147 | "endobj\n"; | - | ||||||
2148 | int shaderObject = addXrefEntry(-1); | - | ||||||
2149 | write(shader); | - | ||||||
2150 | return shaderObject; | - | ||||||
2151 | } | - | ||||||
2152 | - | |||||||
2153 | int QPdfEnginePrivate::generateGradientShader(const QGradient *gradient, const QTransform &matrix, bool alpha) | - | ||||||
2154 | { | - | ||||||
2155 | switch (gradient->type()) { | - | ||||||
2156 | case never executed: QGradient::LinearGradient:case QGradient::LinearGradient: never executed: case QGradient::LinearGradient: | 0 | ||||||
2157 | return never executed: generateLinearGradientShader(static_cast<const QLinearGradient *>(gradient), matrix, alpha);return generateLinearGradientShader(static_cast<const QLinearGradient *>(gradient), matrix, alpha); never executed: return generateLinearGradientShader(static_cast<const QLinearGradient *>(gradient), matrix, alpha); | 0 | ||||||
2158 | case never executed: QGradient::RadialGradient:case QGradient::RadialGradient: never executed: case QGradient::RadialGradient: | 0 | ||||||
2159 | return never executed: generateRadialGradientShader(static_cast<const QRadialGradient *>(gradient), matrix, alpha);return generateRadialGradientShader(static_cast<const QRadialGradient *>(gradient), matrix, alpha); never executed: return generateRadialGradientShader(static_cast<const QRadialGradient *>(gradient), matrix, alpha); | 0 | ||||||
2160 | case never executed: QGradient::ConicalGradient:case QGradient::ConicalGradient: never executed: case QGradient::ConicalGradient: | 0 | ||||||
default never executed: :case QGradient::ConicalGradient: never executed: case QGradient::ConicalGradient: | ||||||||
2161 | QMessageLogger(__FILE__, 22172223, __PRETTY_FUNCTION__).warning() << "Implement("Unimplemented me!code."); | - | ||||||
2162 | break; never executed: break; | 0 | ||||||
2163 | case never executed: QGradient::NoGradient:case QGradient::NoGradient: never executed: case QGradient::NoGradient: | 0 | ||||||
2164 | break never executed: break; never executed: break; never executed: ;break; never executed: break; | 0 | ||||||
2165 | } | - | ||||||
2166 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||
2167 | } | - | ||||||
2168 | - | |||||||
2169 | int QPdfEnginePrivate::gradientBrush(const QBrush &b, const QTransform &matrix, int *gStateObject) | - | ||||||
2170 | { | - | ||||||
2171 | const QGradient *gradient = b.gradient(); | - | ||||||
2172 | - | |||||||
2173 | if (!gradient || gradient->coordinateMode() != QGradient::LogicalMode) | - | ||||||
2174 | return 0; | - | ||||||
2175 | - | |||||||
2176 | QRectF pageRect = m_pageLayout.fullRectPixels(resolution); | - | ||||||
2177 | - | |||||||
2178 | QTransform m = b.transform() * matrix; | - | ||||||
2179 | int shaderObject = generateGradientShader(gradient, m); | - | ||||||
2180 | - | |||||||
2181 | QByteArray str; | - | ||||||
2182 | QPdf::ByteStream s(&str); | - | ||||||
2183 | s << "<<\n" | - | ||||||
2184 | "/Type /Pattern\n" | - | ||||||
2185 | "/PatternType 2\n" | - | ||||||
2186 | "/Shading " << shaderObject << "0 R\n" | - | ||||||
2187 | "/Matrix [" | - | ||||||
2188 | << m.m11() | - | ||||||
2189 | << m.m12() | - | ||||||
2190 | << m.m21() | - | ||||||
2191 | << m.m22() | - | ||||||
2192 | << m.dx() | - | ||||||
2193 | << m.dy() << "]\n"; | - | ||||||
2194 | s << ">>\n" | - | ||||||
2195 | "endobj\n"; | - | ||||||
2196 | - | |||||||
2197 | int patternObj = addXrefEntry(-1); | - | ||||||
2198 | write(str); | - | ||||||
2199 | currentPage->patterns.append(patternObj); | - | ||||||
2200 | - | |||||||
2201 | if (!b.isOpaque()) { | - | ||||||
2202 | bool ca = true; | - | ||||||
2203 | QGradientStops stops = gradient->stops(); | - | ||||||
2204 | int a = stops.at(0).second.alpha(); | - | ||||||
2205 | for (int i = 1; i < stops.size(); ++i) { | - | ||||||
2206 | if (stops.at(i).second.alpha() != a) { | - | ||||||
2207 | ca = false; | - | ||||||
2208 | break; | - | ||||||
2209 | } | - | ||||||
2210 | } | - | ||||||
2211 | if (ca) { | - | ||||||
2212 | *gStateObject = addConstantAlphaObject(stops.at(0).second.alpha()); | - | ||||||
2213 | } else { | - | ||||||
2214 | int alphaShaderObject = generateGradientShader(gradient, m, true); | - | ||||||
2215 | - | |||||||
2216 | QByteArray content; | - | ||||||
2217 | QPdf::ByteStream c(&content); | - | ||||||
2218 | c << "/Shader" << alphaShaderObject << "sh\n"; | - | ||||||
2219 | - | |||||||
2220 | QByteArray form; | - | ||||||
2221 | QPdf::ByteStream f(&form); | - | ||||||
2222 | f << "<<\n" | - | ||||||
2223 | "/Type /XObject\n" | - | ||||||
2224 | "/Subtype /Form\n" | - | ||||||
2225 | "/BBox [0 0 " << pageRect.width() << pageRect.height() << "]\n" | - | ||||||
2226 | "/Group <</S /Transparency >>\n" | - | ||||||
2227 | "/Resources <<\n" | - | ||||||
2228 | "/Shading << /Shader" << alphaShaderObject << alphaShaderObject << "0 R >>\n" | - | ||||||
2229 | ">>\n"; | - | ||||||
2230 | - | |||||||
2231 | f << "/Length " << content.length() << "\n" | - | ||||||
2232 | ">>\n" | - | ||||||
2233 | "stream\n" | - | ||||||
2234 | << content | - | ||||||
2235 | << "endstream\n" | - | ||||||
2236 | "endobj\n"; | - | ||||||
2237 | - | |||||||
2238 | int softMaskFormObject = addXrefEntry(-1); | - | ||||||
2239 | write(form); | - | ||||||
2240 | *gStateObject = addXrefEntry(-1); | - | ||||||
2241 | xprintf("<< /SMask << /S /Alpha /G %d 0 R >> >>\n" | - | ||||||
2242 | "endobj\n", softMaskFormObject); | - | ||||||
2243 | currentPage->graphicStates.append(*gStateObject); | - | ||||||
2244 | } | - | ||||||
2245 | } | - | ||||||
2246 | - | |||||||
2247 | return patternObj; | - | ||||||
2248 | } | - | ||||||
2249 | - | |||||||
2250 | int QPdfEnginePrivate::addConstantAlphaObject(int brushAlpha, int penAlpha) | - | ||||||
2251 | { | - | ||||||
2252 | if (brushAlpha == 255 && penAlpha == 255) | - | ||||||
2253 | return 0; | - | ||||||
2254 | int object = alphaCache.value(QPair<uint, uint>(brushAlpha, penAlpha), 0); | - | ||||||
2255 | if (!object) { | - | ||||||
2256 | object = addXrefEntry(-1); | - | ||||||
2257 | QByteArray alphaDef; | - | ||||||
2258 | QPdf::ByteStream s(&alphaDef); | - | ||||||
2259 | s << "<<\n/ca " << (brushAlpha/qreal(255.)) << '\n'; | - | ||||||
2260 | s << "/CA " << (penAlpha/qreal(255.)) << "\n>>"; | - | ||||||
2261 | xprintf("%s\nendobj\n", alphaDef.constData()); | - | ||||||
2262 | alphaCache.insert(QPair<uint, uint>(brushAlpha, penAlpha), object); | - | ||||||
2263 | } | - | ||||||
2264 | if (currentPage->graphicStates.indexOf(object) < 0) | - | ||||||
2265 | currentPage->graphicStates.append(object); | - | ||||||
2266 | - | |||||||
2267 | return object; | - | ||||||
2268 | } | - | ||||||
2269 | - | |||||||
2270 | - | |||||||
2271 | int QPdfEnginePrivate::addBrushPattern(const QTransform &m, bool *specifyColor, int *gStateObject) | - | ||||||
2272 | { | - | ||||||
2273 | int paintType = 2; | - | ||||||
2274 | int w = 8; | - | ||||||
2275 | int h = 8; | - | ||||||
2276 | - | |||||||
2277 | *specifyColor = true; | - | ||||||
2278 | *gStateObject = 0; | - | ||||||
2279 | - | |||||||
2280 | QTransform matrix = m; | - | ||||||
2281 | matrix.translate(brushOrigin.x(), brushOrigin.y()); | - | ||||||
2282 | matrix = matrix * pageMatrix(); | - | ||||||
2283 | - | |||||||
2284 | - | |||||||
2285 | Qt::BrushStyle style = brush.style(); | - | ||||||
2286 | if (style == Qt::LinearGradientPattern || style == Qt::RadialGradientPattern) { | - | ||||||
2287 | *specifyColor = false; | - | ||||||
2288 | return gradientBrush(brush, matrix, gStateObject); | - | ||||||
2289 | } | - | ||||||
2290 | - | |||||||
2291 | if ((!brush.isOpaque() && brush.style() < Qt::LinearGradientPattern) || opacity != 1.0) | - | ||||||
2292 | *gStateObject = addConstantAlphaObject(qRound(brush.color().alpha() * opacity), | - | ||||||
2293 | qRound(pen.color().alpha() * opacity)); | - | ||||||
2294 | - | |||||||
2295 | int imageObject = -1; | - | ||||||
2296 | QByteArray pattern = QPdf::patternForBrush(brush); | - | ||||||
2297 | if (pattern.isEmpty()) { | - | ||||||
2298 | if (brush.style() != Qt::TexturePattern) | - | ||||||
2299 | return 0; | - | ||||||
2300 | QImage image = brush.textureImage(); | - | ||||||
2301 | bool bitmap = true; | - | ||||||
2302 | imageObject = addImage(image, &bitmap, image.cacheKey()); | - | ||||||
2303 | if (imageObject != -1) { | - | ||||||
2304 | QImage::Format f = image.format(); | - | ||||||
2305 | if (f != QImage::Format_MonoLSB && f != QImage::Format_Mono) { | - | ||||||
2306 | paintType = 1; | - | ||||||
2307 | *specifyColor = false; | - | ||||||
2308 | } | - | ||||||
2309 | w = image.width(); | - | ||||||
2310 | h = image.height(); | - | ||||||
2311 | QTransform m(w, 0, 0, -h, 0, h); | - | ||||||
2312 | QPdf::ByteStream s(&pattern); | - | ||||||
2313 | s << QPdf::generateMatrix(m); | - | ||||||
2314 | s << "/Im" << imageObject << " Do\n"; | - | ||||||
2315 | } | - | ||||||
2316 | } | - | ||||||
2317 | - | |||||||
2318 | QByteArray str; | - | ||||||
2319 | QPdf::ByteStream s(&str); | - | ||||||
2320 | s << "<<\n" | - | ||||||
2321 | "/Type /Pattern\n" | - | ||||||
2322 | "/PatternType 1\n" | - | ||||||
2323 | "/PaintType " << paintType << "\n" | - | ||||||
2324 | "/TilingType 1\n" | - | ||||||
2325 | "/BBox [0 0 " << w << h << "]\n" | - | ||||||
2326 | "/XStep " << w << "\n" | - | ||||||
2327 | "/YStep " << h << "\n" | - | ||||||
2328 | "/Matrix [" | - | ||||||
2329 | << matrix.m11() | - | ||||||
2330 | << matrix.m12() | - | ||||||
2331 | << matrix.m21() | - | ||||||
2332 | << matrix.m22() | - | ||||||
2333 | << matrix.dx() | - | ||||||
2334 | << matrix.dy() << "]\n" | - | ||||||
2335 | "/Resources \n<< "; | - | ||||||
2336 | if (imageObject > 0) { | - | ||||||
2337 | s << "/XObject << /Im" << imageObject << ' ' << imageObject << "0 R >> "; | - | ||||||
2338 | } | - | ||||||
2339 | s << ">>\n" | - | ||||||
2340 | "/Length " << pattern.length() << "\n" | - | ||||||
2341 | ">>\n" | - | ||||||
2342 | "stream\n" | - | ||||||
2343 | << pattern | - | ||||||
2344 | << "endstream\n" | - | ||||||
2345 | "endobj\n"; | - | ||||||
2346 | - | |||||||
2347 | int patternObj = addXrefEntry(-1); | - | ||||||
2348 | write(str); | - | ||||||
2349 | currentPage->patterns.append(patternObj); | - | ||||||
2350 | return patternObj; | - | ||||||
2351 | } | - | ||||||
2352 | - | |||||||
2353 | static inline bool is_monochrome(const QVector<QRgb> &colorTable) | - | ||||||
2354 | { | - | ||||||
2355 | return colorTable.size() == 2 | - | ||||||
2356 | && colorTable.at(0) == QColor(Qt::black).rgba() | - | ||||||
2357 | && colorTable.at(1) == QColor(Qt::white).rgba() | - | ||||||
2358 | ; | - | ||||||
2359 | } | - | ||||||
2360 | - | |||||||
2361 | - | |||||||
2362 | - | |||||||
2363 | - | |||||||
2364 | int QPdfEnginePrivate::addImage(const QImage &img, bool *bitmap, qint64 serial_no) | - | ||||||
2365 | { | - | ||||||
2366 | if (img.isNull()) | - | ||||||
2367 | return -1; | - | ||||||
2368 | - | |||||||
2369 | int object = imageCache.value(serial_no); | - | ||||||
2370 | if(object) | - | ||||||
2371 | return object; | - | ||||||
2372 | - | |||||||
2373 | QImage image = img; | - | ||||||
2374 | QImage::Format format = image.format(); | - | ||||||
2375 | if (image.depth() == 1 && *bitmap && is_monochrome(img.colorTable())) { | - | ||||||
2376 | if (format == QImage::Format_MonoLSB) | - | ||||||
2377 | image = image.convertToFormat(QImage::Format_Mono); | - | ||||||
2378 | format = QImage::Format_Mono; | - | ||||||
2379 | } else { | - | ||||||
2380 | *bitmap = false; | - | ||||||
2381 | if (format != QImage::Format_RGB32 && format != QImage::Format_ARGB32) { | - | ||||||
2382 | image = image.convertToFormat(QImage::Format_ARGB32); | - | ||||||
2383 | format = QImage::Format_ARGB32; | - | ||||||
2384 | } | - | ||||||
2385 | } | - | ||||||
2386 | - | |||||||
2387 | int w = image.width(); | - | ||||||
2388 | int h = image.height(); | - | ||||||
2389 | int d = image.depth(); | - | ||||||
2390 | - | |||||||
2391 | if (format == QImage::Format_Mono) { | - | ||||||
2392 | int bytesPerLine = (w + 7) >> 3; | - | ||||||
2393 | QByteArray data; | - | ||||||
2394 | data.resize(bytesPerLine * h); | - | ||||||
2395 | char *rawdata = data.data(); | - | ||||||
2396 | for (int y = 0; y < h; ++y) { | - | ||||||
2397 | memcpy(rawdata, image.constScanLine(y), bytesPerLine); | - | ||||||
2398 | rawdata += bytesPerLine; | - | ||||||
2399 | } | - | ||||||
2400 | object = writeImage(data, w, h, d, 0, 0, false, is_monochrome(img.colorTable())); | - | ||||||
2401 | } else { | - | ||||||
2402 | QByteArray softMaskData; | - | ||||||
2403 | bool dct = false; | - | ||||||
2404 | QByteArray imageData; | - | ||||||
2405 | bool hasAlpha = false; | - | ||||||
2406 | bool hasMask = false; | - | ||||||
2407 | - | |||||||
2408 | if (QImageWriter::supportedImageFormats().contains("jpeg") && !grayscale) { | - | ||||||
2409 | QBuffer buffer(&imageData); | - | ||||||
2410 | QImageWriter writer(&buffer, "jpeg"); | - | ||||||
2411 | writer.setQuality(94); | - | ||||||
2412 | writer.write(image); | - | ||||||
2413 | dct = true; | - | ||||||
2414 | - | |||||||
2415 | if (format != QImage::Format_RGB32) { | - | ||||||
2416 | softMaskData.resize(w * h); | - | ||||||
2417 | uchar *sdata = (uchar *)softMaskData.data(); | - | ||||||
2418 | for (int y = 0; y < h; ++y) { | - | ||||||
2419 | const QRgb *rgb = (const QRgb *)image.constScanLine(y); | - | ||||||
2420 | for (int x = 0; x < w; ++x) { | - | ||||||
2421 | uchar alpha = qAlpha(*rgb); | - | ||||||
2422 | *sdata++ = alpha; | - | ||||||
2423 | hasMask |= (alpha < 255); | - | ||||||
2424 | hasAlpha |= (alpha != 0 && alpha != 255); | - | ||||||
2425 | ++rgb; | - | ||||||
2426 | } | - | ||||||
2427 | } | - | ||||||
2428 | } | - | ||||||
2429 | } else { | - | ||||||
2430 | imageData.resize(grayscale ? w * h : 3 * w * h); | - | ||||||
2431 | uchar *data = (uchar *)imageData.data(); | - | ||||||
2432 | softMaskData.resize(w * h); | - | ||||||
2433 | uchar *sdata = (uchar *)softMaskData.data(); | - | ||||||
2434 | for (int y = 0; y < h; ++y) { | - | ||||||
2435 | const QRgb *rgb = (const QRgb *)image.constScanLine(y); | - | ||||||
2436 | if (grayscale) { | - | ||||||
2437 | for (int x = 0; x < w; ++x) { | - | ||||||
2438 | *(data++) = qGray(*rgb); | - | ||||||
2439 | uchar alpha = qAlpha(*rgb); | - | ||||||
2440 | *sdata++ = alpha; | - | ||||||
2441 | hasMask |= (alpha < 255); | - | ||||||
2442 | hasAlpha |= (alpha != 0 && alpha != 255); | - | ||||||
2443 | ++rgb; | - | ||||||
2444 | } | - | ||||||
2445 | } else { | - | ||||||
2446 | for (int x = 0; x < w; ++x) { | - | ||||||
2447 | *(data++) = qRed(*rgb); | - | ||||||
2448 | *(data++) = qGreen(*rgb); | - | ||||||
2449 | *(data++) = qBlue(*rgb); | - | ||||||
2450 | uchar alpha = qAlpha(*rgb); | - | ||||||
2451 | *sdata++ = alpha; | - | ||||||
2452 | hasMask |= (alpha < 255); | - | ||||||
2453 | hasAlpha |= (alpha != 0 && alpha != 255); | - | ||||||
2454 | ++rgb; | - | ||||||
2455 | } | - | ||||||
2456 | } | - | ||||||
2457 | } | - | ||||||
2458 | if (format == QImage::Format_RGB32) | - | ||||||
2459 | hasAlpha = hasMask = false; | - | ||||||
2460 | } | - | ||||||
2461 | int maskObject = 0; | - | ||||||
2462 | int softMaskObject = 0; | - | ||||||
2463 | if (hasAlpha) { | - | ||||||
2464 | softMaskObject = writeImage(softMaskData, w, h, 8, 0, 0); | - | ||||||
2465 | } else if (hasMask) { | - | ||||||
2466 | - | |||||||
2467 | - | |||||||
2468 | int bytesPerLine = (w + 7) >> 3; | - | ||||||
2469 | QByteArray mask(bytesPerLine * h, 0); | - | ||||||
2470 | uchar *mdata = (uchar *)mask.data(); | - | ||||||
2471 | const uchar *sdata = (const uchar *)softMaskData.constData(); | - | ||||||
2472 | for (int y = 0; y < h; ++y) { | - | ||||||
2473 | for (int x = 0; x < w; ++x) { | - | ||||||
2474 | if (*sdata) | - | ||||||
2475 | mdata[x>>3] |= (0x80 >> (x&7)); | - | ||||||
2476 | ++sdata; | - | ||||||
2477 | } | - | ||||||
2478 | mdata += bytesPerLine; | - | ||||||
2479 | } | - | ||||||
2480 | maskObject = writeImage(mask, w, h, 1, 0, 0); | - | ||||||
2481 | } | - | ||||||
2482 | object = writeImage(imageData, w, h, grayscale ? 8 : 32, | - | ||||||
2483 | maskObject, softMaskObject, dct); | - | ||||||
2484 | } | - | ||||||
2485 | imageCache.insert(serial_no, object); | - | ||||||
2486 | return object; | - | ||||||
2487 | } | - | ||||||
2488 | - | |||||||
2489 | void QPdfEnginePrivate::drawTextItem(const QPointF &p, const QTextItemInt &ti) | - | ||||||
2490 | { | - | ||||||
2491 | QPdfEngine * const q = q_func(); | - | ||||||
2492 | - | |||||||
2493 | if (ti.charFormat.isAnchor()) { | - | ||||||
2494 | qreal size = ti.fontEngine->fontDef.pixelSize; | - | ||||||
2495 | int synthesized = ti.fontEngine->synthesized(); | - | ||||||
2496 | qreal stretch = synthesized & QFontEngine::SynthesizedStretch ? ti.fontEngine->fontDef.stretch/100. : 1.; | - | ||||||
2497 | - | |||||||
2498 | QTransform trans; | - | ||||||
2499 | - | |||||||
2500 | - | |||||||
2501 | trans = QTransform(size*stretch, 0, 0, size, 0, 0); | - | ||||||
2502 | - | |||||||
2503 | trans *= QTransform(1,0,0,-1,p.x(),p.y()); | - | ||||||
2504 | - | |||||||
2505 | trans *= stroker.matrix; | - | ||||||
2506 | - | |||||||
2507 | trans *= pageMatrix(); | - | ||||||
2508 | qreal x1, y1, x2, y2; | - | ||||||
2509 | trans.map(0, 0, &x1, &y1); | - | ||||||
2510 | trans.map(ti.width.toReal()/size, (ti.ascent.toReal()-ti.descent.toReal())/size, &x2, &y2); | - | ||||||
2511 | - | |||||||
2512 | uint annot = addXrefEntry(-1); | - | ||||||
2513 | QByteArray x1s, y1s, x2s, y2s; | - | ||||||
2514 | x1s.setNum(static_cast<double>(x1), 'f'); | - | ||||||
2515 | y1s.setNum(static_cast<double>(y1), 'f'); | - | ||||||
2516 | x2s.setNum(static_cast<double>(x2), 'f'); | - | ||||||
2517 | y2s.setNum(static_cast<double>(y2), 'f'); | - | ||||||
2518 | QByteArray rectData = x1s + ' ' + y1s + ' ' + x2s + ' ' + y2s; | - | ||||||
2519 | xprintf("<<\n/Type /Annot\n/Subtype /Link\n/Rect ["); | - | ||||||
2520 | xprintf(rectData.constData()); | - | ||||||
2521 | - | |||||||
2522 | - | |||||||
2523 | - | |||||||
2524 | xprintf("]\n/Border [0 0 0]\n/A <<\n"); | - | ||||||
2525 | - | |||||||
2526 | xprintf("/Type /Action\n/S /URI\n/URI (%s)\n", | - | ||||||
2527 | ti.charFormat.anchorHref().toLatin1().constData()); | - | ||||||
2528 | xprintf(">>\n>>\n"); | - | ||||||
2529 | xprintf("endobj\n"); | - | ||||||
2530 | - | |||||||
2531 | if (!currentPage->annotations.contains(annot)) { | - | ||||||
2532 | currentPage->annotations.append(annot); | - | ||||||
2533 | } | - | ||||||
2534 | } | - | ||||||
2535 | - | |||||||
2536 | QFontEngine *fe = ti.fontEngine; | - | ||||||
2537 | - | |||||||
2538 | QFontEngine::FaceId face_id = fe->faceId(); | - | ||||||
2539 | bool noEmbed = false; | - | ||||||
2540 | if (!embedFonts | - | ||||||
2541 | || face_id.filename.isEmpty() | - | ||||||
2542 | || fe->fsType & 0x200 | - | ||||||
2543 | || fe->fsType == 2 ) { | - | ||||||
2544 | *currentPage << "Q\n"; | - | ||||||
2545 | q->QPaintEngine::drawTextItem(p, ti); | - | ||||||
2546 | *currentPage << "q\n"; | - | ||||||
2547 | if (face_id.filename.isEmpty()) | - | ||||||
2548 | return; | - | ||||||
2549 | noEmbed = true; | - | ||||||
2550 | } | - | ||||||
2551 | - | |||||||
2552 | QFontSubset *font = fonts.value(face_id, 0); | - | ||||||
2553 | if (!font) { | - | ||||||
2554 | font = new QFontSubset(fe, requestObject()); | - | ||||||
2555 | font->noEmbed = noEmbed; | - | ||||||
2556 | } | - | ||||||
2557 | fonts.insert(face_id, font); | - | ||||||
2558 | - | |||||||
2559 | if (!currentPage->fonts.contains(font->object_id)) | - | ||||||
2560 | currentPage->fonts.append(font->object_id); | - | ||||||
2561 | - | |||||||
2562 | qreal size = ti.fontEngine->fontDef.pixelSize; | - | ||||||
2563 | - | |||||||
2564 | QVarLengthArray<glyph_t> glyphs; | - | ||||||
2565 | QVarLengthArray<QFixedPoint> positions; | - | ||||||
2566 | QTransform m = QTransform::fromTranslate(p.x(), p.y()); | - | ||||||
2567 | ti.fontEngine->getGlyphPositions(ti.glyphs, m, ti.flags, | - | ||||||
2568 | glyphs, positions); | - | ||||||
2569 | if (glyphs.size() == 0) | - | ||||||
2570 | return; | - | ||||||
2571 | int synthesized = ti.fontEngine->synthesized(); | - | ||||||
2572 | qreal stretch = synthesized & QFontEngine::SynthesizedStretch ? ti.fontEngine->fontDef.stretch/100. : 1.; | - | ||||||
2573 | - | |||||||
2574 | *currentPage << "BT\n" | - | ||||||
2575 | << "/F" << font->object_id << size << "Tf " | - | ||||||
2576 | << stretch << (synthesized & QFontEngine::SynthesizedItalic | - | ||||||
2577 | ? "0 .3 -1 0 0 Tm\n" | - | ||||||
2578 | : "0 0 -1 0 0 Tm\n"); | - | ||||||
2579 | qreal last_x = 0.; | - | ||||||
2580 | qreal last_y = 0.; | - | ||||||
2581 | for (int i = 0; i < glyphs.size(); ++i) { | - | ||||||
2582 | qreal x = positions[i].x.toReal(); | - | ||||||
2583 | qreal y = positions[i].y.toReal(); | - | ||||||
2584 | if (synthesized & QFontEngine::SynthesizedItalic) | - | ||||||
2585 | x += .3*y; | - | ||||||
2586 | x /= stretch; | - | ||||||
2587 | char buf[5]; | - | ||||||
2588 | int g = font->addGlyph(glyphs[i]); | - | ||||||
2589 | *currentPage << x - last_x << last_y - y << "Td <" | - | ||||||
2590 | << QPdf::toHex((ushort)g, buf) << "> Tj\n"; | - | ||||||
2591 | last_x = x; | - | ||||||
2592 | last_y = y; | - | ||||||
2593 | } | - | ||||||
2594 | if (synthesized & QFontEngine::SynthesizedBold) { | - | ||||||
2595 | *currentPage << stretch << (synthesized & QFontEngine::SynthesizedItalic | - | ||||||
2596 | ? "0 .3 -1 0 0 Tm\n" | - | ||||||
2597 | : "0 0 -1 0 0 Tm\n"); | - | ||||||
2598 | *currentPage << "/Span << /ActualText <> >> BDC\n"; | - | ||||||
2599 | last_x = 0.5*fe->lineThickness().toReal(); | - | ||||||
2600 | last_y = 0.; | - | ||||||
2601 | for (int i = 0; i < glyphs.size(); ++i) { | - | ||||||
2602 | qreal x = positions[i].x.toReal(); | - | ||||||
2603 | qreal y = positions[i].y.toReal(); | - | ||||||
2604 | if (synthesized & QFontEngine::SynthesizedItalic) | - | ||||||
2605 | x += .3*y; | - | ||||||
2606 | x /= stretch; | - | ||||||
2607 | char buf[5]; | - | ||||||
2608 | int g = font->addGlyph(glyphs[i]); | - | ||||||
2609 | *currentPage << x - last_x << last_y - y << "Td <" | - | ||||||
2610 | << QPdf::toHex((ushort)g, buf) << "> Tj\n"; | - | ||||||
2611 | last_x = x; | - | ||||||
2612 | last_y = y; | - | ||||||
2613 | } | - | ||||||
2614 | *currentPage << "EMC\n"; | - | ||||||
2615 | } | - | ||||||
2616 | - | |||||||
2617 | - | |||||||
2618 | *currentPage << "ET\n"; | - | ||||||
2619 | } | - | ||||||
2620 | - | |||||||
2621 | QTransform QPdfEnginePrivate::pageMatrix() const | - | ||||||
2622 | { | - | ||||||
2623 | qreal scale = 72./resolution; | - | ||||||
2624 | QTransform tmp(scale, 0.0, 0.0, -scale, 0.0, m_pageLayout.fullRectPoints().height()); | - | ||||||
2625 | if (m_pageLayout.mode() != QPageLayout::FullPageMode) { | - | ||||||
2626 | QRect r = m_pageLayout.paintRectPixels(resolution); | - | ||||||
2627 | tmp.translate(r.left(), r.top()); | - | ||||||
2628 | } | - | ||||||
2629 | return tmp; | - | ||||||
2630 | } | - | ||||||
2631 | - | |||||||
2632 | void QPdfEnginePrivate::newPage() | - | ||||||
2633 | { | - | ||||||
2634 | if (currentPage && currentPage->pageSize.isEmpty()) | - | ||||||
2635 | currentPage->pageSize = m_pageLayout.fullRectPoints().size(); | - | ||||||
2636 | writePage(); | - | ||||||
2637 | - | |||||||
2638 | delete currentPage; | - | ||||||
2639 | currentPage = new QPdfPage; | - | ||||||
2640 | currentPage->pageSize = m_pageLayout.fullRectPoints().size(); | - | ||||||
2641 | stroker.stream = currentPage; | - | ||||||
2642 | pages.append(requestObject()); | - | ||||||
2643 | - | |||||||
2644 | *currentPage << "/GSa gs /CSp cs /CSp CS\n" | - | ||||||
2645 | << QPdf::generateMatrix(pageMatrix()) | - | ||||||
2646 | << "q q\n"; | - | ||||||
2647 | } | - | ||||||
2648 | - | |||||||
2649 | - | |||||||
Switch to Source code | Preprocessed file |