Rev 21759 | Rev 21805 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
3133 | fschmid | 7 | /*************************************************************************** |
14043 | jghali | 8 | pdflib_core.cpp - description |
9 | ------------------- |
||
10 | begin : Sat Jan 19 2002 |
||
11 | copyright : (C) 2002 by Franz Schmid |
||
12 | email : Franz.Schmid@altmuehlnet.de |
||
3133 | fschmid | 13 | ***************************************************************************/ |
14 | |||
15 | /*************************************************************************** |
||
16 | * * |
||
17 | * This program is free software; you can redistribute it and/or modify * |
||
18 | * it under the terms of the GNU General Public License as published by * |
||
19 | * the Free Software Foundation; either version 2 of the License, or * |
||
20 | * (at your option) any later version. * |
||
21 | * * |
||
22 | ***************************************************************************/ |
||
23 | |||
10054 | craig | 24 | #include "pdflib_core.h" |
3133 | fschmid | 25 | |
26 | #include "scconfig.h" |
||
27 | |||
17539 | jghali | 28 | #if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES) |
14170 | jghali | 29 | #define _USE_MATH_DEFINES |
30 | #endif |
||
12080 | avox | 31 | #include <cmath> |
32 | #include <cstdlib> |
||
4229 | craig | 33 | #include <string> |
12080 | avox | 34 | #ifdef HAVE_UNISTD_H |
35 | #include <unistd.h> |
||
36 | #endif |
||
37 | |||
38 | #include "rc4.h" |
||
39 | |||
40 | #include <QByteArray> |
||
19914 | craig | 41 | #include <QCryptographicHash> |
10223 | cbradney | 42 | #include <QDateTime> |
10004 | fschmid | 43 | #include <QDataStream> |
12080 | avox | 44 | #include <QDebug> |
10223 | cbradney | 45 | #include <QDir> |
12080 | avox | 46 | #include <QFileInfo> |
47 | #include <QImage> |
||
9803 | fschmid | 48 | #include <QList> |
11453 | fschmid | 49 | #include <QPainterPath> |
8501 | cbradney | 50 | #include <QPixmap> |
12080 | avox | 51 | #include <QRect> |
52 | #include <QRegExp> |
||
19671 | jghali | 53 | #include <QScopedPointer> |
12080 | avox | 54 | #include <QStack> |
55 | #include <QString> |
||
13839 | fschmid | 56 | #include <QTemporaryFile> |
10614 | cbradney | 57 | #include <QTextCodec> |
14043 | jghali | 58 | #include <QtXml> |
59 | #include <QUuid> |
||
3133 | fschmid | 60 | |
10601 | mrdocs | 61 | #include "cmsettings.h" |
4028 | cbradney | 62 | #include "commonstrings.h" |
3133 | fschmid | 63 | #include "pageitem.h" |
10601 | mrdocs | 64 | #include "pageitem_textframe.h" |
16258 | fschmid | 65 | #include "pageitem_group.h" |
17312 | fschmid | 66 | #include "pageitem_table.h" |
10212 | cbradney | 67 | #include "pdfoptions.h" |
68 | #include "prefscontext.h" |
||
69 | #include "prefsmanager.h" |
||
70 | #include "sccolor.h" |
||
71 | #include "sccolorengine.h" |
||
72 | #include "scfonts.h" |
||
21107 | craig | 73 | #include "text/textlayoutpainter.h" |
20103 | avox | 74 | #include "fonts/cff.h" |
75 | #include "fonts/sfnt.h" |
||
16736 | jghali | 76 | #include "scpage.h" |
5917 | jghali | 77 | #include "scpaths.h" |
10212 | cbradney | 78 | #include "scpattern.h" |
19115 | avox | 79 | |
5243 | cbradney | 80 | #include "scribuscore.h" |
3699 | cbradney | 81 | #include "scribusdoc.h" |
11599 | jghali | 82 | #include "scstreamfilter_flate.h" |
83 | #include "scstreamfilter_rc4.h" |
||
17312 | fschmid | 84 | #include "tableutils.h" |
3133 | fschmid | 85 | #include "util.h" |
11605 | jghali | 86 | #include "util_file.h" |
10212 | cbradney | 87 | #include "util_formats.h" |
10203 | cbradney | 88 | #include "util_math.h" |
12517 | fschmid | 89 | #include "util_ghostscript.h" |
21107 | craig | 90 | #include "text/boxes.h" |
16736 | jghali | 91 | |
13839 | fschmid | 92 | #ifdef HAVE_OSG |
14930 | fschmid | 93 | #include "third_party/prc/exportPRC.h" |
13839 | fschmid | 94 | #endif |
3133 | fschmid | 95 | |
16736 | jghali | 96 | #include "ui/bookmwin.h" |
97 | #include "ui/bookmarkpalette.h" |
||
98 | #include "ui/multiprogressdialog.h" |
||
99 | |||
3133 | fschmid | 100 | using namespace std; |
17312 | fschmid | 101 | using namespace TableUtils; |
3133 | fschmid | 102 | |
12096 | jghali | 103 | #if defined(_WIN32) |
104 | #undef GetObject |
||
105 | #endif |
||
3133 | fschmid | 106 | |
21107 | craig | 107 | static inline QByteArray FToStr(double c) |
108 | { |
||
109 | double v = c; |
||
110 | if (fabs(c) < 0.0000001) |
||
111 | v = 0.0; |
||
112 | return QByteArray::number(v, 'f', 5); |
||
113 | } |
||
6562 | fschmid | 114 | |
21107 | craig | 115 | class PdfPainter: public TextLayoutPainter |
116 | { |
||
117 | QByteArray m_glyphBuffer; |
||
118 | QByteArray m_pathBuffer; |
||
119 | PageItem* m_item; |
||
120 | QMap<QString, PdfFont> m_UsedFontsP; |
||
121 | PDFLibCore *m_pdf; |
||
122 | uint m_PNr; |
||
123 | const ScPage* m_page; |
||
124 | |||
125 | QByteArray transformToStr(QTransform tr) |
||
126 | { |
||
127 | return FToStr(tr.m11()) + " " + FToStr(-tr.m12()) + " " + FToStr(-tr.m21()) + " " + FToStr(tr.m22()) + " " + FToStr(tr.dx()) + " " + FToStr(-tr.dy()); |
||
128 | } |
||
129 | |||
130 | public: |
||
131 | PdfPainter(PageItem *ite, PDFLibCore *pdf, uint num, const ScPage* pag) : |
||
132 | m_glyphBuffer(), |
||
133 | m_pathBuffer(), |
||
134 | m_item(ite), |
||
135 | m_pdf(pdf), |
||
136 | m_PNr(num), |
||
137 | m_page(pag) |
||
138 | {} |
||
139 | |||
140 | ~PdfPainter() {} |
||
141 | |||
21563 | jghali | 142 | void drawGlyph(const GlyphCluster& gc) |
21107 | craig | 143 | { |
21563 | jghali | 144 | if (gc.isControlGlyphs() || gc.isEmpty()) |
21107 | craig | 145 | return; |
21563 | jghali | 146 | |
147 | double current_x = 0.0; |
||
148 | foreach (const GlyphLayout& gl, gc.glyphs()) { |
||
149 | PdfFont pdfFont = m_pdf->UsedFontsP[font().replacementName()]; |
||
150 | QByteArray StrokeColor; |
||
151 | QByteArray FillColor; |
||
21107 | craig | 152 | |
21563 | jghali | 153 | if (strokeColor().color != CommonStrings::None) |
154 | StrokeColor = m_pdf->putColor(strokeColor().color, strokeColor().shade, false); |
||
155 | if (fillColor().color != CommonStrings::None) |
||
156 | FillColor = m_pdf->putColor(fillColor().color, fillColor().shade, true); |
||
21107 | craig | 157 | |
21563 | jghali | 158 | QTransform transform = matrix(); |
159 | if (pdfFont.method == Use_XForm) |
||
160 | { |
||
161 | if (!FillColor.isEmpty()) |
||
162 | m_pathBuffer += FillColor; |
||
21107 | craig | 163 | |
21563 | jghali | 164 | m_pathBuffer += "q\n"; |
165 | m_pathBuffer += transformToStr(transform) + " cm\n"; |
||
166 | m_pathBuffer += FToStr(fontSize()) + " 0 0 " + FToStr(fontSize()) + " " + FToStr(x() + gl.xoffset + current_x) + " " + FToStr((y() - fontSize() + gl.yoffset) * -1) + " cm\n"; |
||
21107 | craig | 167 | |
21563 | jghali | 168 | if (gl.scaleV != 1.0) |
169 | m_pathBuffer += "1 0 0 1 0 " + FToStr(((fontSize() - fontSize() * gl.scaleV) / fontSize()) * -1) + " cm\n"; |
||
170 | m_pathBuffer += FToStr(qMax(gl.scaleH, 0.1)) + " 0 0 " + FToStr(qMax(gl.scaleV, 0.1)) + " 0 0 cm\n"; |
||
21107 | craig | 171 | |
21563 | jghali | 172 | if (!FillColor.isEmpty()) |
173 | m_pathBuffer += pdfFont.name + Pdf::toPdf(gl.glyph) + " Do\n"; |
||
174 | m_pathBuffer += "Q\n"; |
||
21107 | craig | 175 | } |
176 | else |
||
21563 | jghali | 177 | { |
178 | uint gid = gl.glyph; |
||
179 | uint fontNr = 65535; |
||
21107 | craig | 180 | |
181 | switch (pdfFont.encoding) |
||
182 | { |
||
21563 | jghali | 183 | case Encode_256: |
184 | gid = pdfFont.glyphmap[gid]; |
||
185 | fontNr = gid / 256; |
||
186 | gid = gid % 256; |
||
187 | break; |
||
21107 | craig | 188 | case Encode_224: |
21563 | jghali | 189 | fontNr = gid / 224; |
190 | gid = gid % 224 + 32; |
||
21107 | craig | 191 | break; |
21563 | jghali | 192 | case Encode_Subset: |
193 | gid = pdfFont.glyphmap[gid]; |
||
194 | break; |
||
21107 | craig | 195 | case Encode_IdentityH: |
196 | break; |
||
197 | } |
||
198 | |||
21563 | jghali | 199 | if (fontNr == 65535) |
200 | m_glyphBuffer += pdfFont.name + " " + FToStr(fontSize()) + " Tf\n"; |
||
201 | else |
||
202 | m_glyphBuffer += pdfFont.name + "S" + Pdf::toPdf(fontNr) + " " + FToStr(fontSize()) + " Tf\n"; |
||
21107 | craig | 203 | |
21563 | jghali | 204 | if (!StrokeColor.isEmpty()) |
205 | m_glyphBuffer += StrokeColor; |
||
21107 | craig | 206 | |
21563 | jghali | 207 | if (!FillColor.isEmpty()) |
208 | m_glyphBuffer += FillColor; |
||
21107 | craig | 209 | |
21563 | jghali | 210 | m_glyphBuffer += "0 Tr\n"; |
21107 | craig | 211 | |
21563 | jghali | 212 | transform.translate(x() + gl.xoffset + current_x , y() + gl.yoffset); |
213 | transform.scale(qMax(gl.scaleH, 0.1), qMax(gl.scaleV, 0.1)); |
||
214 | m_glyphBuffer += transformToStr(transform) + " Tm\n"; |
||
21107 | craig | 215 | |
216 | |||
21563 | jghali | 217 | if (pdfFont.method != Use_Type3 || !FillColor.isEmpty()) |
21107 | craig | 218 | { |
21563 | jghali | 219 | switch (pdfFont.encoding) |
21107 | craig | 220 | { |
21563 | jghali | 221 | case Encode_224: |
222 | case Encode_256: |
||
223 | m_glyphBuffer += Pdf::toHexString8(gid) + " Tj\n"; |
||
224 | break; |
||
225 | case Encode_IdentityH: |
||
226 | default: |
||
227 | m_glyphBuffer += Pdf::toHexString16(gid) + " Tj\n"; |
||
228 | break; |
||
21107 | craig | 229 | } |
230 | } |
||
231 | } |
||
21563 | jghali | 232 | current_x += gl.xadvance; |
21107 | craig | 233 | } |
21563 | jghali | 234 | } |
21107 | craig | 235 | |
21563 | jghali | 236 | void drawGlyphOutline(const GlyphCluster& gc, bool fill) |
237 | { |
||
238 | if (gc.isControlGlyphs() || gc.isEmpty()) |
||
239 | return; |
||
21107 | craig | 240 | |
21563 | jghali | 241 | double current_x = 0.0; |
242 | foreach (const GlyphLayout& gl, gc.glyphs()) { |
||
243 | PdfFont pdfFont = m_pdf->UsedFontsP[font().replacementName()]; |
||
244 | QByteArray StrokeColor; |
||
245 | QByteArray FillColor; |
||
246 | QTransform transform = matrix(); |
||
21107 | craig | 247 | |
21563 | jghali | 248 | if (strokeColor().color != CommonStrings::None) |
249 | StrokeColor = m_pdf->putColor(strokeColor().color, strokeColor().shade, false); |
||
250 | if (fill && fillColor().color != CommonStrings::None) |
||
251 | FillColor = m_pdf->putColor(fillColor().color, fillColor().shade, true); |
||
21107 | craig | 252 | |
21563 | jghali | 253 | if (pdfFont.method == Use_XForm) |
254 | { |
||
255 | if (!StrokeColor.isEmpty()) |
||
256 | { |
||
257 | m_pathBuffer += FToStr(strokeWidth() / fontSize()) + " w\n[] 0 d\n0 J\n0 j\n"; |
||
258 | m_pathBuffer += StrokeColor; |
||
259 | } |
||
21107 | craig | 260 | |
21563 | jghali | 261 | if (!FillColor.isEmpty()) |
262 | m_pathBuffer += FillColor; |
||
263 | |||
21107 | craig | 264 | m_pathBuffer += "q\n"; |
265 | m_pathBuffer += transformToStr(transform) + " cm\n"; |
||
21563 | jghali | 266 | m_pathBuffer += FToStr(fontSize()) + " 0 0 " + FToStr(fontSize()) + " " + FToStr(x() + gl.xoffset) + " " + FToStr((y() - fontSize() + gl.yoffset) * -1) + " cm\n"; |
21107 | craig | 267 | |
21563 | jghali | 268 | if (gc.scaleV() != 1.0) |
269 | m_pathBuffer += "1 0 0 1 0 " + FToStr(((fontSize() - fontSize() * (gc.scaleV())) / fontSize()) * -1) + " cm\n"; |
||
270 | |||
271 | m_pathBuffer += FToStr(qMax(gc.scaleH(), 0.1)) + " 0 0 " + FToStr(qMax(gc.scaleV(), 0.1)) + " 0 0 cm\n"; |
||
272 | |||
273 | if (!FillColor.isEmpty()) |
||
274 | m_pathBuffer += pdfFont.name + Pdf::toPdf(gl.glyph) + " Do\n"; |
||
275 | |||
21107 | craig | 276 | FPointArray outline = font().glyphOutline(gl.glyph); |
277 | QTransform mat; |
||
278 | mat.scale(0.1, 0.1); |
||
279 | outline.map(mat); |
||
280 | bool nPath = true; |
||
281 | FPoint np; |
||
282 | if (outline.size() > 3) |
||
283 | { |
||
284 | for (int poi = 0; poi < outline.size() - 3; poi += 4) |
||
285 | { |
||
286 | if (outline.isMarker(poi)) |
||
287 | { |
||
288 | m_pathBuffer += "h\n"; |
||
289 | nPath = true; |
||
290 | continue; |
||
291 | } |
||
292 | |||
293 | if (nPath) |
||
294 | { |
||
295 | np = outline.point(poi); |
||
296 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " m\n"; |
||
297 | nPath = false; |
||
298 | } |
||
299 | |||
300 | np = outline.point(poi + 1); |
||
301 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " "; |
||
302 | np = outline.point(poi + 3); |
||
303 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " "; |
||
304 | np = outline.point(poi + 2); |
||
305 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " c\n"; |
||
306 | } |
||
307 | } |
||
308 | m_pathBuffer += "h s\n"; |
||
309 | m_pathBuffer += "Q\n"; |
||
310 | } |
||
311 | else |
||
312 | { |
||
21563 | jghali | 313 | if (!StrokeColor.isEmpty()) |
314 | m_pathBuffer += StrokeColor; |
||
21107 | craig | 315 | |
21563 | jghali | 316 | uint gid = gl.glyph; |
317 | uint fontNr = 65535; |
||
21107 | craig | 318 | switch (pdfFont.encoding) |
319 | { |
||
21563 | jghali | 320 | case Encode_256: |
321 | gid = pdfFont.glyphmap[gid]; |
||
322 | fontNr = gid / 256; |
||
323 | gid = gid % 256; |
||
324 | break; |
||
21107 | craig | 325 | case Encode_224: |
21563 | jghali | 326 | fontNr = gid / 224; |
327 | gid = gid % 224 + 32; |
||
21107 | craig | 328 | break; |
21563 | jghali | 329 | case Encode_Subset: |
330 | gid = pdfFont.glyphmap[gid]; |
||
331 | break; |
||
21107 | craig | 332 | case Encode_IdentityH: |
333 | break; |
||
334 | } |
||
21563 | jghali | 335 | |
336 | if (fontNr == 65535) |
||
337 | m_glyphBuffer += pdfFont.name + " " + FToStr(fontSize()) + " Tf\n"; |
||
338 | else |
||
339 | m_glyphBuffer += pdfFont.name + "S" + Pdf::toPdf(fontNr) + " " + FToStr(fontSize()) + " Tf\n"; |
||
340 | |||
341 | if (!StrokeColor.isEmpty()) |
||
342 | m_glyphBuffer += StrokeColor; |
||
343 | |||
344 | if (!FillColor.isEmpty()) |
||
345 | m_glyphBuffer += FillColor; |
||
346 | |||
347 | if (pdfFont.method == Use_Type3 && !StrokeColor.isEmpty()) |
||
348 | { |
||
349 | m_pathBuffer += "q\n"; |
||
350 | m_pathBuffer += FToStr(strokeWidth()) + " w\n[] 0 d\n0 J\n0 j\n"; |
||
351 | |||
352 | transform.scale(fontSize(), fontSize()); |
||
353 | transform.translate(x() + gl.xoffset + current_x, y() + gl.yoffset); |
||
354 | if (gc.scaleV() != 1.0) |
||
355 | transform.translate(0, ((fontSize() - fontSize() * gc.scaleV()) / fontSize()) * -1); |
||
356 | transform.scale(qMax(gc.scaleH(), 0.1), qMax(gc.scaleV(), 0.1)); |
||
357 | m_pathBuffer += transformToStr(transform) + " cm\n"; |
||
358 | |||
359 | /* paint outline */ |
||
360 | FPointArray outline = font().glyphOutline(gl.glyph); |
||
361 | QTransform mat; |
||
362 | mat.scale(0.1, 0.1); |
||
363 | outline.map(mat); |
||
364 | bool nPath = true; |
||
365 | FPoint np; |
||
366 | if (outline.size() > 3) |
||
367 | { |
||
368 | for (int poi = 0; poi < outline.size() - 3; poi += 4) |
||
369 | { |
||
370 | if (outline.isMarker(poi)) |
||
371 | { |
||
372 | m_pathBuffer += "h\n"; |
||
373 | nPath = true; |
||
374 | continue; |
||
375 | } |
||
376 | |||
377 | if (nPath) |
||
378 | { |
||
379 | np = outline.point(poi); |
||
380 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " m\n"; |
||
381 | nPath = false; |
||
382 | } |
||
383 | |||
384 | np = outline.point(poi + 1); |
||
385 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " "; |
||
386 | np = outline.point(poi + 3); |
||
387 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " "; |
||
388 | np = outline.point(poi + 2); |
||
389 | m_pathBuffer += FToStr(np.x()) + " " + FToStr(-np.y()) + " c\n"; |
||
390 | } |
||
391 | } |
||
392 | m_pathBuffer += "h s\n"; |
||
393 | m_pathBuffer += "Q\n"; |
||
394 | } |
||
395 | else |
||
396 | { |
||
397 | m_glyphBuffer += FToStr(strokeWidth()) + " w "; |
||
398 | if (fill) |
||
399 | m_glyphBuffer += "2 Tr\n"; |
||
400 | else |
||
401 | m_glyphBuffer += "1 Tr\n"; |
||
402 | } |
||
403 | |||
404 | transform.translate(x() + gl.xoffset + current_x, y() + gl.yoffset); |
||
405 | transform.scale(qMax(gc.scaleH(), 0.1), qMax(gc.scaleV(), 0.1)); |
||
406 | m_glyphBuffer += transformToStr(transform) + " Tm\n"; |
||
407 | |||
408 | if (pdfFont.method != Use_Type3 || !FillColor.isEmpty()) |
||
409 | { |
||
410 | switch (pdfFont.encoding) |
||
411 | { |
||
412 | case Encode_224: |
||
413 | case Encode_256: |
||
414 | m_glyphBuffer += Pdf::toHexString8(gid) + " Tj\n"; |
||
415 | break; |
||
416 | case Encode_IdentityH: |
||
417 | default: |
||
418 | m_glyphBuffer += Pdf::toHexString16(gid) + " Tj\n"; |
||
419 | break; |
||
420 | } |
||
421 | } |
||
21107 | craig | 422 | } |
21563 | jghali | 423 | current_x += gl.xadvance; |
21107 | craig | 424 | } |
425 | } |
||
426 | |||
427 | void drawLine(QPointF start, QPointF end) |
||
428 | { |
||
429 | QTransform transform = matrix(); |
||
430 | transform.translate(x(), y()); |
||
431 | m_pathBuffer += "q\n"; |
||
432 | m_pathBuffer += transformToStr(transform) + " cm\n"; |
||
433 | m_pathBuffer += m_pdf->putColor(strokeColor().color, strokeColor().shade, false); |
||
434 | m_pathBuffer += FToStr(strokeWidth())+" w\n"; |
||
435 | m_pathBuffer += FToStr(start.x()) + " " + FToStr(-start.y()) + " m\n"; |
||
436 | m_pathBuffer += FToStr(end.x()) + " " + FToStr(-end.y()) + " l\n"; |
||
437 | m_pathBuffer += "S\n"; |
||
438 | m_pathBuffer += "Q\n"; |
||
439 | } |
||
440 | |||
441 | QByteArray getBuffer() |
||
442 | { |
||
443 | return "BT\n" + m_glyphBuffer + "ET\n" + m_pathBuffer; |
||
444 | } |
||
445 | |||
446 | void drawRect(QRectF rect) |
||
447 | { |
||
448 | QTransform transform = matrix(); |
||
449 | // transform.translate(x(), y()); |
||
450 | double rectX = x() + rect.x(); |
||
451 | double rectY = -y() - rect.y(); |
||
452 | m_glyphBuffer += "q\n"; |
||
453 | m_glyphBuffer += transformToStr(transform) + " cm\n"; |
||
454 | m_glyphBuffer += "n\n"; |
||
455 | m_glyphBuffer += m_pdf->putColor(fillColor().color, fillColor().shade, true); |
||
456 | m_glyphBuffer += m_pdf->putColor(strokeColor().color, strokeColor().shade, false); |
||
457 | m_glyphBuffer += FToStr(rectX) + " " + FToStr(rectY) + " m\n"; |
||
458 | m_glyphBuffer += FToStr(rectX + rect.width()) + " " + FToStr(rectY) + " l\n"; |
||
459 | m_glyphBuffer += FToStr(rectX + rect.width()) + " " + FToStr(rectY - rect.height()) + " l\n"; |
||
460 | m_glyphBuffer += FToStr(rectX) + " " + FToStr(rectY - rect.height()) + " l\n"; |
||
461 | m_glyphBuffer += "h\nf\n"; |
||
462 | m_glyphBuffer += "Q\n"; |
||
463 | } |
||
464 | |||
465 | void drawObject(PageItem* embedded) |
||
466 | { |
||
467 | m_glyphBuffer += "ET\n"+m_pathBuffer; |
||
468 | m_pathBuffer = ""; |
||
469 | |||
470 | m_pathBuffer += "q\n"; |
||
21301 | fschmid | 471 | m_pathBuffer += FToStr(scaleH()) + " 0 0 " + FToStr(scaleV()) + " " + FToStr(x() + embedded->gXpos) + " " + FToStr(-(y() + embedded->gYpos)) + " cm\n"; |
21107 | craig | 472 | |
473 | QByteArray output; |
||
474 | if (!m_pdf->PDF_ProcessItem(output, embedded, m_page, m_PNr, true)) |
||
475 | return; |
||
476 | m_pathBuffer += output; |
||
477 | m_pathBuffer += "Q\n"; |
||
478 | m_glyphBuffer += m_pathBuffer+"\n"; |
||
479 | m_pathBuffer = ""; |
||
480 | |||
481 | m_glyphBuffer += "BT\n"; |
||
482 | } |
||
483 | }; |
||
484 | |||
10054 | craig | 485 | PDFLibCore::PDFLibCore(ScribusDoc & docu) |
4264 | craig | 486 | : QObject(&docu), |
4223 | craig | 487 | doc(docu), |
488 | ActPageP(0), |
||
14925 | cbradney | 489 | Options(doc.pdfOptions()), |
4223 | craig | 490 | Bvie(0), |
19114 | avox | 491 | ucs2Codec(0), |
20103 | avox | 492 | // ObjCounter(7), |
4223 | craig | 493 | ResNam("RE"), |
494 | ResCount(0), |
||
495 | NDnam("LI"), |
||
496 | NDnum(0), |
||
20103 | avox | 497 | // KeyGen(""), |
498 | // OwnerKey(""), |
||
499 | // UserKey(""), |
||
500 | // FileID(""), |
||
501 | // EncryKey(""), |
||
502 | // Encrypt(0), |
||
503 | // KeyLen(5), |
||
4223 | craig | 504 | colorsToUse(), |
505 | spotNam("Spot"), |
||
506 | spotCount(0), |
||
4225 | craig | 507 | progressDialog(0), |
4223 | craig | 508 | abortExport(false), |
20089 | jghali | 509 | usingGUI(ScCore->usingGUI()), |
510 | bleedDisplacementX(0), |
||
511 | bleedDisplacementY(0) |
||
3133 | fschmid | 512 | { |
20103 | avox | 513 | // KeyGen.resize(32); |
514 | // OwnerKey.resize(32); |
||
515 | // UserKey.resize(32); |
||
516 | // FileID.resize(16); |
||
517 | // EncryKey.resize(5); |
||
3133 | fschmid | 518 | Catalog.Outlines = 2; |
519 | Catalog.PageTree = 3; |
||
520 | Catalog.Dest = 4; |
||
521 | PageTree.Count = 0; |
||
522 | Outlines.First = 0; |
||
523 | Outlines.Last = 0; |
||
524 | Outlines.Count = 0; |
||
20103 | avox | 525 | pageData.ObjNum = 0; |
526 | pageData.Thumb = 0; |
||
527 | // int kg_array[] = {0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, |
||
528 | // 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, |
||
529 | // 0x64, 0x53, 0x69, 0x7a}; |
||
530 | // for (int a = 0; a < 32; ++a) |
||
531 | // KeyGen[a] = kg_array[a]; |
||
4028 | cbradney | 532 | if (usingGUI) |
533 | { |
||
10508 | cbradney | 534 | progressDialog = new MultiProgressDialog( tr("Saving PDF"), CommonStrings::tr_Cancel, doc.scMW()); |
4224 | craig | 535 | Q_CHECK_PTR(progressDialog); |
536 | QStringList barNames, barTexts; |
||
537 | barNames << "EMP" << "EP" << "ECPI"; |
||
5370 | cbradney | 538 | barTexts << tr("Exporting Master Page:") << tr("Exporting Page:") << tr("Exporting Items on Current Page:"); |
9803 | fschmid | 539 | QList<bool> barsNumeric; |
5370 | cbradney | 540 | barsNumeric << true << true << false; |
541 | progressDialog->addExtraProgressBars(barNames, barTexts, barsNumeric); |
||
8559 | subik | 542 | connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelRequested())); |
4028 | cbradney | 543 | } |
3133 | fschmid | 544 | } |
545 | |||
10054 | craig | 546 | PDFLibCore::~PDFLibCore() |
4224 | craig | 547 | { |
548 | delete progressDialog; |
||
549 | } |
||
550 | |||
4229 | craig | 551 | |
19115 | avox | 552 | bool PDFLibCore::PDF_IsPDFX() |
553 | { |
||
554 | if (Options.Version == PDFOptions::PDFVersion_X1a) |
||
555 | return true; |
||
556 | if (Options.Version == PDFOptions::PDFVersion_X3) |
||
557 | return true; |
||
558 | if (Options.Version == PDFOptions::PDFVersion_X4) |
||
559 | return true; |
||
560 | return false; |
||
561 | } |
||
562 | |||
563 | bool PDFLibCore::PDF_IsPDFX(PDFOptions::PDFVersion ver) |
||
564 | { |
||
565 | if (ver == PDFOptions::PDFVersion_X1a) |
||
566 | return true; |
||
567 | if (ver == PDFOptions::PDFVersion_X3) |
||
568 | return true; |
||
569 | if (ver == PDFOptions::PDFVersion_X4) |
||
570 | return true; |
||
571 | return false; |
||
572 | } |
||
573 | |||
10054 | craig | 574 | bool PDFLibCore::doExport(const QString& fn, const QString& nam, int Components, |
4264 | craig | 575 | const std::vector<int> & pageNs, const QMap<int,QPixmap> & thumbs) |
3133 | fschmid | 576 | { |
577 | QPixmap pm; |
||
11060 | jghali | 578 | bool ret = false, error = false; |
579 | int pc_exportpages=0; |
||
580 | int pc_exportmasterpages=0; |
||
4224 | craig | 581 | if (usingGUI) |
582 | progressDialog->show(); |
||
5387 | avox | 583 | QMap<QString, QMap<uint, FPointArray> > usedFonts; |
584 | usedFonts.clear(); |
||
585 | doc.getUsedFonts(usedFonts); |
||
19114 | avox | 586 | ucs2Codec = QTextCodec::codecForName("ISO-10646-UCS-2"); |
17758 | jghali | 587 | if (!ucs2Codec) |
19114 | avox | 588 | ucs2Codec = QTextCodec::codecForName("UTF-16"); |
589 | if (!ucs2Codec) |
||
13764 | jghali | 590 | { |
13791 | pierre | 591 | PDF_Error( tr("Qt build miss both \"UTF-16\" and \"ISO-10646-UCS-2\" text codecs, pdf export is not possible") ); |
13764 | jghali | 592 | return false; |
593 | } |
||
13955 | cbradney | 594 | if (PDF_Begin_Doc(fn, PrefsManager::instance()->appPrefs.fontPrefs.AvailFonts, usedFonts, doc.scMW()->bookmarkPalette->BView)) |
3133 | fschmid | 595 | { |
596 | QMap<int, int> pageNsMpa; |
||
597 | for (uint a = 0; a < pageNs.size(); ++a) |
||
598 | { |
||
14653 | jghali | 599 | pageNsMpa.insert(doc.MasterNames[doc.DocPages.at(pageNs[a]-1)->MPageNam], 0); |
3133 | fschmid | 600 | } |
4028 | cbradney | 601 | if (usingGUI) |
3133 | fschmid | 602 | { |
4028 | cbradney | 603 | progressDialog->setOverallTotalSteps(pageNsMpa.count()+pageNs.size()); |
604 | progressDialog->setTotalSteps("EMP", pageNsMpa.count()); |
||
605 | progressDialog->setTotalSteps("EP", pageNs.size()); |
||
606 | progressDialog->setOverallProgress(0); |
||
607 | progressDialog->setProgress("EMP", 0); |
||
608 | progressDialog->setProgress("EP", 0); |
||
609 | } |
||
10018 | fschmid | 610 | for (int ap = 0; ap < doc.MasterPages.count() && !abortExport; ++ap) |
4028 | cbradney | 611 | { |
4264 | craig | 612 | if (doc.MasterItems.count() != 0) |
3133 | fschmid | 613 | { |
614 | if (pageNsMpa.contains(ap)) |
||
615 | { |
||
5243 | cbradney | 616 | qApp->processEvents(); |
11060 | jghali | 617 | if (!PDF_TemplatePage(doc.MasterPages.at(ap))) |
618 | error = abortExport = true; |
||
4028 | cbradney | 619 | ++pc_exportmasterpages; |
3133 | fschmid | 620 | } |
621 | } |
||
4546 | subik | 622 | if (usingGUI) |
623 | { |
||
4028 | cbradney | 624 | progressDialog->setProgress("EMP", pc_exportmasterpages); |
625 | progressDialog->setOverallProgress(pc_exportmasterpages+pc_exportpages); |
||
626 | } |
||
3133 | fschmid | 627 | } |
4028 | cbradney | 628 | for (uint a = 0; a < pageNs.size() && !abortExport; ++a) |
3133 | fschmid | 629 | { |
14925 | cbradney | 630 | if (doc.pdfOptions().Thumbnails) |
3133 | fschmid | 631 | pm = thumbs[pageNs[a]]; |
5243 | cbradney | 632 | qApp->processEvents(); |
4028 | cbradney | 633 | if (abortExport) break; |
11060 | jghali | 634 | |
14653 | jghali | 635 | PDF_Begin_Page(doc.DocPages.at(pageNs[a]-1), pm); |
5243 | cbradney | 636 | qApp->processEvents(); |
4028 | cbradney | 637 | if (abortExport) break; |
11060 | jghali | 638 | |
14925 | cbradney | 639 | if (!PDF_ProcessPage(doc.DocPages.at(pageNs[a]-1), pageNs[a]-1, doc.pdfOptions().doClip)) |
11060 | jghali | 640 | error = abortExport = true; |
5243 | cbradney | 641 | qApp->processEvents(); |
4028 | cbradney | 642 | if (abortExport) break; |
11060 | jghali | 643 | |
15444 | craig | 644 | PDF_End_Page(a); |
4028 | cbradney | 645 | pc_exportpages++; |
646 | if (usingGUI) |
||
647 | { |
||
648 | progressDialog->setProgress("EP", pc_exportpages); |
||
649 | progressDialog->setOverallProgress(pc_exportmasterpages+pc_exportpages); |
||
650 | } |
||
3133 | fschmid | 651 | } |
20681 | craig | 652 | ret = true;//Even when aborting we return true. Don't want that "couldnt write msg" |
4028 | cbradney | 653 | if (!abortExport) |
654 | { |
||
19115 | avox | 655 | if (PDF_IsPDFX(doc.pdfOptions().Version)) |
14925 | cbradney | 656 | ret = PDF_End_Doc(ScCore->PrinterProfiles[doc.pdfOptions().PrintProf], nam, Components); |
4028 | cbradney | 657 | else |
12387 | jghali | 658 | ret = PDF_End_Doc(); |
4028 | cbradney | 659 | } |
3133 | fschmid | 660 | else |
4028 | cbradney | 661 | closeAndCleanup(); |
3133 | fschmid | 662 | } |
4029 | cbradney | 663 | if (usingGUI) |
664 | progressDialog->close(); |
||
11060 | jghali | 665 | return (ret && !error); |
3133 | fschmid | 666 | } |
667 | |||
11060 | jghali | 668 | const QString& PDFLibCore::errorMessage(void) const |
669 | { |
||
670 | return ErrorMessage; |
||
671 | } |
||
672 | |||
11441 | jghali | 673 | bool PDFLibCore::exportAborted(void) const |
674 | { |
||
675 | return abortExport; |
||
676 | } |
||
677 | |||
20103 | avox | 678 | //#define StartObj(n) writer.startObj((n)) |
679 | #define PutDoc(s) writer.write(s) |
||
680 | //#define newObject() writer.newObject() |
||
3133 | fschmid | 681 | |
682 | |||
20103 | avox | 683 | //void PDFLibCore::StartObj(PdfId nr) |
684 | //{ |
||
685 | // for (int i=XRef.size(); i < nr; ++i) |
||
686 | // XRef.append(0); |
||
687 | // XRef[nr-1] = bytesWritten(); |
||
688 | // PutDoc(Pdf::toPdf(nr)+ " 0 obj\n"); |
||
689 | //} |
||
690 | |||
691 | //// Encode a string for inclusion in a |
||
692 | //// PDF (literal) . |
||
693 | //static QByteArray PDFEncode(const QString & in) |
||
694 | //{ |
||
695 | // QByteArray tmp(""); |
||
696 | // for (int d = 0; d < in.length(); ++d) |
||
697 | // { |
||
698 | // QChar cc(in.at(d)); |
||
699 | // if ((cc == '(') || (cc == ')') || (cc == '\\')) |
||
700 | // tmp += '\\'; |
||
701 | // else if ((cc == '\r') || (cc == '\n')) |
||
702 | // { |
||
703 | // tmp += (cc == '\r') ? "\\r" : "\\n"; |
||
704 | // continue; |
||
705 | // } |
||
706 | // tmp += cc; |
||
707 | // } |
||
708 | // return tmp; |
||
709 | //} |
||
710 | |||
711 | static QByteArray blendMode(int code) |
||
12080 | avox | 712 | { |
713 | switch (code) |
||
714 | { |
||
715 | case 0: |
||
716 | return("Normal"); |
||
717 | break; |
||
718 | case 1: |
||
719 | return("Darken"); |
||
720 | break; |
||
721 | case 2: |
||
722 | return("Lighten"); |
||
723 | break; |
||
724 | case 3: |
||
725 | return("Multiply"); |
||
726 | break; |
||
727 | case 4: |
||
728 | return("Screen"); |
||
729 | break; |
||
730 | case 5: |
||
731 | return("Overlay"); |
||
732 | break; |
||
733 | case 6: |
||
734 | return("HardLight"); |
||
735 | break; |
||
736 | case 7: |
||
737 | return("SoftLight"); |
||
738 | break; |
||
739 | case 8: |
||
740 | return("Difference"); |
||
741 | break; |
||
742 | case 9: |
||
743 | return("Exclusion"); |
||
744 | break; |
||
745 | case 10: |
||
746 | return("ColorDodge"); |
||
747 | break; |
||
748 | case 11: |
||
749 | return("ColorBurn"); |
||
750 | break; |
||
751 | case 12: |
||
752 | return("Hue"); |
||
753 | break; |
||
754 | case 13: |
||
755 | return("Saturation"); |
||
756 | break; |
||
757 | case 14: |
||
758 | return("Color"); |
||
759 | break; |
||
760 | case 15: |
||
761 | return("Luminosity"); |
||
762 | break; |
||
763 | default: |
||
764 | return ""; |
||
765 | } |
||
14043 | jghali | 766 | } |
20103 | avox | 767 | // |
768 | //QByteArray PDFLibCore::EncodeUTF16(const QString &in) |
||
769 | //{ |
||
770 | // QString tmp = in; |
||
771 | // QByteArray cres = ucs2Codec->fromUnicode( tmp ); |
||
772 | //#ifndef WORDS_BIGENDIAN |
||
773 | // // on little endian systems we need to swap bytes: |
||
774 | // uchar sw; |
||
775 | // for(int d = 0; d < cres.size()-1; d += 2) |
||
776 | // { |
||
777 | // sw = cres[d]; |
||
778 | // cres[d] = cres[d+1]; |
||
779 | // cres[d+1] = sw; |
||
780 | // } |
||
781 | //#endif |
||
782 | // return cres; |
||
783 | //} |
||
12080 | avox | 784 | |
20103 | avox | 785 | QByteArray PDFLibCore::EncStream(const QByteArray & in, PdfId ObjNum) |
19114 | avox | 786 | { |
787 | if (in.length() < 1) |
||
20103 | avox | 788 | return QByteArray(); |
19114 | avox | 789 | else if (!Options.Encrypt) |
790 | return in; |
||
20112 | jghali | 791 | else |
792 | return writer.encryptBytes(in, ObjNum); |
||
20103 | avox | 793 | // rc4_context_t rc4; |
794 | // QByteArray tmp(in); |
||
795 | // QByteArray us(tmp.length(), ' '); |
||
796 | // QByteArray ou(tmp.length(), ' '); |
||
797 | // for (int a = 0; a < tmp.length(); ++a) |
||
20112 | jghali | 798 | // us[a] = (tmp.at(a)); |
799 | // QByteArray step1 = writer.ComputeRC4Key(ObjNum); |
||
20103 | avox | 800 | // rc4_init(&rc4, reinterpret_cast<uchar*>(step1.data()), qMin(KeyLen+5, 16)); |
801 | // rc4_encrypt(&rc4, reinterpret_cast<uchar*>(us.data()), reinterpret_cast<uchar*>(ou.data()), tmp.length()); |
||
802 | // QString uk = ""; |
||
803 | // for (int cl = 0; cl < tmp.length(); ++cl) |
||
804 | // uk += QChar(ou[cl]); |
||
805 | // return ou; |
||
19114 | avox | 806 | } |
807 | |||
20103 | avox | 808 | QByteArray PDFLibCore::EncString(const QByteArray & in, PdfId ObjNum) |
19114 | avox | 809 | { |
20103 | avox | 810 | QByteArray tmp; |
19114 | avox | 811 | if (in.length() < 1) |
812 | return "<>"; |
||
813 | if (!Options.Encrypt) |
||
814 | { |
||
20103 | avox | 815 | //tmp = "(" + PDFEncode(in) + ")"; |
20112 | jghali | 816 | return Pdf::toLiteralString(in); |
19114 | avox | 817 | } |
20112 | jghali | 818 | |
20103 | avox | 819 | // rc4_context_t rc4; |
820 | // QByteArray us(in.length(), ' '); |
||
821 | // QByteArray ou(in.length(), ' '); |
||
822 | // for (int a = 0; a < in.length(); ++a) |
||
823 | // us[a] = static_cast<uchar>(QChar(in.at(a)).cell()); |
||
824 | // QByteArray step1 = writer.ComputeRC4Key(ObjNum); |
||
825 | // rc4_init(&rc4, reinterpret_cast<uchar*>(step1.data()), qMin(KeyLen+5, 16)); |
||
826 | // rc4_encrypt(&rc4, reinterpret_cast<uchar*>(us.data()), reinterpret_cast<uchar*>(ou.data()), in.length()); |
||
827 | // QString uk = ""; |
||
828 | // for (int cl = 0; cl < in.length(); ++cl) |
||
829 | // uk += QChar(ou[cl]); |
||
20112 | jghali | 830 | |
831 | return Pdf::toHexString(writer.encryptBytes(in, ObjNum)); |
||
19114 | avox | 832 | } |
833 | |||
20103 | avox | 834 | QByteArray PDFLibCore::EncStringUTF16(const QString & in, PdfId ObjNum) |
19114 | avox | 835 | { |
836 | if (in.length() < 1) |
||
837 | return "<>"; |
||
838 | if (!Options.Encrypt) |
||
839 | { |
||
20112 | jghali | 840 | QByteArray us = Pdf::toUTF16(in); |
20103 | avox | 841 | // QString uk = ""; |
842 | // for (int cl = 0; cl < us.size(); ++cl) |
||
843 | // uk += QChar(us[cl]); |
||
844 | return Pdf::toHexString(us); |
||
19114 | avox | 845 | } |
20103 | avox | 846 | // rc4_context_t rc4; |
20112 | jghali | 847 | QByteArray us = Pdf::toUTF16(in); |
20103 | avox | 848 | // QByteArray ou(us.size(), ' '); |
849 | // QByteArray step1 = writer.ComputeRC4Key(ObjNum); |
||
850 | // rc4_init(&rc4, reinterpret_cast<uchar*>(step1.data()), qMin(KeyLen+5, 16)); |
||
851 | // rc4_encrypt(&rc4, reinterpret_cast<uchar*>(us.data()), reinterpret_cast<uchar*>(ou.data()), ou.size()); |
||
852 | // QString uk = ""; |
||
853 | // for (int cl = 0; cl < ou.size(); ++cl) |
||
854 | // uk += QChar(ou[cl]); |
||
855 | QByteArray tmp = Pdf::toHexString(writer.encryptBytes(us, ObjNum)); |
||
19114 | avox | 856 | return tmp; |
857 | } |
||
858 | |||
20103 | avox | 859 | bool PDFLibCore::EncodeArrayToStream(const QByteArray& in, PdfId ObjNum) |
19114 | avox | 860 | { |
861 | if (in.size() < 1) |
||
862 | return true; |
||
863 | bool succeed = false; |
||
864 | if (Options.Encrypt) |
||
865 | { |
||
20103 | avox | 866 | ScStreamFilter* rc4Encode = writer.openStreamFilter(true, ObjNum); |
867 | if (rc4Encode->openFilter()) |
||
19114 | avox | 868 | { |
20103 | avox | 869 | succeed = rc4Encode->writeData(in.data(), in.size()); |
870 | succeed &= rc4Encode->closeFilter(); |
||
19114 | avox | 871 | } |
20112 | jghali | 872 | delete rc4Encode; |
19114 | avox | 873 | } |
874 | else |
||
20103 | avox | 875 | writer.write(in); |
876 | return (writer.getOutStream().status() == QDataStream::Ok); |
||
19114 | avox | 877 | } |
878 | |||
20103 | avox | 879 | int PDFLibCore::WriteImageToStream(ScImage& image, PdfId ObjNum, ColorSpaceEnum format, bool precal) |
11599 | jghali | 880 | { |
17758 | jghali | 881 | bool fromCmyk, succeed = false; |
11599 | jghali | 882 | int bytesWritten = 0; |
20103 | avox | 883 | |
20112 | jghali | 884 | ScStreamFilter* rc4Encode = writer.openStreamFilter(Options.Encrypt, ObjNum); |
885 | if (rc4Encode->openFilter()) |
||
886 | { |
||
887 | switch (format) |
||
888 | { |
||
889 | case ColorSpaceMonochrome : |
||
890 | fromCmyk = !Options.UseRGB && !Options.isGrayscale && !(doc.HasCMS && Options.UseProfiles2); |
||
891 | succeed = image.writeMonochromeDataToFilter(rc4Encode, fromCmyk); break; |
||
892 | case ColorSpaceGray : |
||
893 | succeed = image.writeGrayDataToFilter(rc4Encode, precal); break; |
||
894 | case ColorSpaceCMYK : |
||
895 | succeed = image.writeCMYKDataToFilter(rc4Encode); break; |
||
896 | default : |
||
897 | succeed = image.writeRGBDataToFilter(rc4Encode); break; |
||
898 | } |
||
899 | succeed &= rc4Encode->closeFilter(); |
||
900 | bytesWritten = rc4Encode->writtenToStream(); |
||
901 | delete rc4Encode; |
||
902 | } |
||
11599 | jghali | 903 | return (succeed ? bytesWritten : 0); |
904 | } |
||
905 | |||
20103 | avox | 906 | int PDFLibCore::WriteJPEGImageToStream(ScImage& image, const QString& fn, PdfId ObjNum, int quality, ColorSpaceEnum format, |
17758 | jghali | 907 | bool sameFile, bool precal) |
11599 | jghali | 908 | { |
909 | bool succeed = true; |
||
910 | int bytesWritten = 0; |
||
911 | QFileInfo fInfo(fn); |
||
912 | QString ext = fInfo.suffix().toLower(); |
||
913 | QString jpgFileName, tmpFile; |
||
914 | if (extensionIndicatesJPEG(ext) && sameFile) |
||
915 | jpgFileName = fn; |
||
11445 | jghali | 916 | else |
11599 | jghali | 917 | { |
21526 | craig | 918 | tmpFile = QDir::toNativeSeparators(ScPaths::tempFileDir() + "sc.jpg"); |
17758 | jghali | 919 | if (format == ColorSpaceGray && (!precal)) |
11599 | jghali | 920 | image.convertToGray(); |
17758 | jghali | 921 | if (image.convert2JPG(tmpFile, quality, format == ColorSpaceCMYK, format == ColorSpaceGray)) |
11599 | jghali | 922 | jpgFileName = tmpFile; |
923 | } |
||
924 | if (jpgFileName.isEmpty()) |
||
11792 | fschmid | 925 | return 0; |
11599 | jghali | 926 | if (Options.Encrypt) |
927 | { |
||
928 | succeed = false; |
||
20112 | jghali | 929 | ScStreamFilter* rc4Encode = writer.openStreamFilter(true, ObjNum); |
930 | if (rc4Encode->openFilter()) |
||
11599 | jghali | 931 | { |
20103 | avox | 932 | succeed = copyFileToFilter(jpgFileName, *rc4Encode); |
933 | succeed &= rc4Encode->closeFilter(); |
||
934 | bytesWritten = rc4Encode->writtenToStream(); |
||
11599 | jghali | 935 | } |
20112 | jghali | 936 | delete rc4Encode; |
11599 | jghali | 937 | } |
938 | else |
||
939 | { |
||
20103 | avox | 940 | succeed &= copyFileToStream(jpgFileName, writer.getOutStream()); |
11599 | jghali | 941 | QFileInfo jpgInfo(jpgFileName); |
942 | bytesWritten = jpgInfo.size(); |
||
943 | } |
||
944 | if (!tmpFile.isEmpty() && QFile::exists(tmpFile)) |
||
945 | QFile::remove(tmpFile); |
||
946 | return (succeed ? bytesWritten : 0); |
||
11445 | jghali | 947 | } |
948 | |||
20103 | avox | 949 | int PDFLibCore::WriteFlateImageToStream(ScImage& image, PdfId ObjNum, ColorSpaceEnum format, bool precal) |
11599 | jghali | 950 | { |
20112 | jghali | 951 | bool fromCmyk, succeed = false; |
952 | int bytesWritten = 0; |
||
953 | ScStreamFilter* rc4Encode = writer.openStreamFilter(Options.Encrypt, ObjNum); |
||
954 | ScFlateEncodeFilter flateEncode(rc4Encode); |
||
955 | if (flateEncode.openFilter()) |
||
956 | { |
||
957 | switch (format) |
||
958 | { |
||
959 | case ColorSpaceMonochrome : |
||
960 | fromCmyk = !Options.UseRGB && !Options.isGrayscale && !(doc.HasCMS && Options.UseProfiles2); |
||
961 | succeed = image.writeMonochromeDataToFilter(&flateEncode, fromCmyk); break; |
||
962 | case ColorSpaceGray : |
||
963 | succeed = image.writeGrayDataToFilter(&flateEncode, precal); break; |
||
964 | case ColorSpaceCMYK : |
||
965 | succeed = image.writeCMYKDataToFilter(&flateEncode); break; |
||
966 | default : |
||
967 | succeed = image.writeRGBDataToFilter(&flateEncode); break; |
||
968 | } |
||
969 | succeed &= flateEncode.closeFilter(); |
||
970 | bytesWritten = flateEncode.writtenToStream(); |
||
971 | } |
||
972 | delete rc4Encode; |
||
973 | return (succeed ? bytesWritten : 0); |
||
11599 | jghali | 974 | } |
975 | |||
3133 | fschmid | 976 | |
19114 | avox | 977 | |
20103 | avox | 978 | bool PDFLibCore::PDF_Begin_Doc(const QString& fn, SCFonts &AllFonts, const QMap<QString, QMap<uint, FPointArray> >& DocFonts, BookMView* vi) |
19114 | avox | 979 | { |
20112 | jghali | 980 | if (!writer.open(fn)) |
981 | return false; |
||
982 | |||
983 | QFileInfo fiBase(fn); |
||
984 | QString baseDir = fiBase.absolutePath(); |
||
19114 | avox | 985 | |
20103 | avox | 986 | // QString tmp; |
987 | // QFileInfo fd; |
||
988 | // QString fext; |
||
6407 | fschmid | 989 | inPattern = 0; |
3133 | fschmid | 990 | Bvie = vi; |
991 | BookMinUse = false; |
||
992 | UsedFontsP.clear(); |
||
8454 | fschmid | 993 | UsedFontsF.clear(); |
20112 | jghali | 994 | |
995 | writer.writeHeader(Options.Version); |
||
996 | |||
20103 | avox | 997 | // if (((Options.Version == PDFOptions::PDFVersion_15) || (Options.Version == PDFOptions::PDFVersion_X4)) && (Options.useLayers)) |
998 | // ObjCounter = 10; |
||
999 | // else |
||
1000 | // ObjCounter = 9; |
||
1001 | // switch (Options.Version) |
||
1002 | // { |
||
1003 | // case PDFOptions::PDFVersion_X1a: |
||
1004 | // case PDFOptions::PDFVersion_X3: |
||
1005 | // case PDFOptions::PDFVersion_13: |
||
1006 | // PutDoc("%PDF-1.3\n"); |
||
1007 | // break; |
||
1008 | // case PDFOptions::PDFVersion_14: |
||
1009 | // PutDoc("%PDF-1.4\n"); |
||
1010 | // break; |
||
1011 | // case PDFOptions::PDFVersion_X4: |
||
1012 | // case PDFOptions::PDFVersion_15: |
||
1013 | // PutDoc("%PDF-1.5\n"); |
||
1014 | // break; |
||
1015 | // } |
||
1016 | // if (PDF_IsPDFX()) |
||
1017 | // ObjCounter++; |
||
1018 | // PutDoc("%\xc7\xec\x8f\xa2\n"); |
||
1019 | |||
20112 | jghali | 1020 | PDF_Begin_Catalog(); |
1021 | PDF_Begin_MetadataAndEncrypt(); |
||
1022 | PDF_Begin_WriteUsedFonts(AllFonts, PDF_Begin_FindUsedFonts(AllFonts, DocFonts)); |
||
1023 | PDF_Begin_Colors(); |
||
1024 | PDF_Begin_Layers(); |
||
1025 | |||
1026 | return true; |
||
20103 | avox | 1027 | } |
1028 | |||
1029 | void PDFLibCore::PDF_Begin_Catalog() |
||
1030 | { |
||
20112 | jghali | 1031 | writer.startObj(writer.CatalogObj); |
1032 | PutDoc("<<\n/Type /Catalog"); |
||
1033 | PutDoc("\n/Outlines " + Pdf::toObjRef(writer.OutlinesObj) + |
||
1034 | "\n/Pages " + Pdf::toObjRef(writer.PagesObj) + |
||
1035 | "\n/Dests " + Pdf::toObjRef(writer.DestsObj) + |
||
1036 | "\n/AcroForm " + Pdf::toObjRef(writer.AcroFormObj) + |
||
1037 | "\n/Names "+ Pdf::toObjRef(writer.NamesObj) + |
||
1038 | "\n/Threads " + Pdf::toObjRef(writer.ThreadsObj) + |
||
1039 | "\n"); |
||
19114 | avox | 1040 | if (((Options.Version == PDFOptions::PDFVersion_15) || (Options.Version == PDFOptions::PDFVersion_X4)) && (Options.useLayers)) |
20112 | jghali | 1041 | { |
1042 | writer.OCPropertiesObj = writer.newObject(); |
||
20103 | avox | 1043 | PutDoc("/OCProperties " + Pdf::toObjRef(writer.OCPropertiesObj) + "\n"); |
20112 | jghali | 1044 | } |
1045 | if (PDF_IsPDFX()) |
||
19114 | avox | 1046 | { |
20112 | jghali | 1047 | writer.OutputIntentObj = writer.newObject(); |
1048 | PutDoc("/OutputIntents [ " + Pdf::toObjRef(writer.OutputIntentObj) + " ]\n"); |
||
1049 | } |
||
1050 | if ((Options.Version == PDFOptions::PDFVersion_X4)) |
||
1051 | { |
||
1052 | writer.MetaDataObj = writer.newObject(); |
||
20103 | avox | 1053 | PutDoc("/Metadata "+ Pdf::toObjRef(writer.MetaDataObj) + "\n"); |
19114 | avox | 1054 | } |
1055 | PutDoc("/PageLayout "); |
||
1056 | switch (Options.PageLayout) |
||
1057 | { |
||
1058 | case PDFOptions::SinglePage: |
||
1059 | PutDoc("/SinglePage\n"); |
||
1060 | break; |
||
1061 | case PDFOptions::OneColumn: |
||
1062 | PutDoc("/OneColumn\n"); |
||
1063 | break; |
||
1064 | case PDFOptions::TwoColumnLeft: |
||
1065 | PutDoc("/TwoColumnLeft\n"); |
||
1066 | break; |
||
1067 | case PDFOptions::TwoColumnRight: |
||
1068 | PutDoc("/TwoColumnRight\n"); |
||
1069 | break; |
||
1070 | } |
||
1071 | if (Options.displayBookmarks) |
||
1072 | PutDoc("/PageMode /UseOutlines\n"); |
||
1073 | else if (Options.displayFullscreen) |
||
1074 | PutDoc("/PageMode /FullScreen\n"); |
||
1075 | else if (Options.displayThumbs) |
||
1076 | PutDoc("/PageMode /UseThumbs\n"); |
||
1077 | else if ((Options.Version == PDFOptions::PDFVersion_15) && (Options.displayLayers)) |
||
1078 | PutDoc("/PageMode /UseOC\n"); |
||
1079 | if (!Options.openAction.isEmpty()) |
||
1080 | { |
||
20112 | jghali | 1081 | PutDoc("/OpenAction << /S /JavaScript /JS (this."+Pdf::toPdfDocEncoding(Options.openAction)+"\\(\\)) >>\n"); |
19114 | avox | 1082 | } |
20112 | jghali | 1083 | QString tmp; |
19114 | avox | 1084 | QDateTime dt = QDateTime::currentDateTime().toUTC(); |
20112 | jghali | 1085 | Datum = Pdf::toDateString(dt); |
20103 | avox | 1086 | // "D:"; |
1087 | // tmp.sprintf("%4d", d.year()); |
||
1088 | // tmp.replace(QRegExp(" "), "0"); |
||
1089 | // Datum += tmp; |
||
1090 | // tmp.sprintf("%2d", d.month()); |
||
1091 | // tmp.replace(QRegExp(" "), "0"); |
||
1092 | // Datum += tmp; |
||
1093 | // tmp.sprintf("%2d", d.day()); |
||
1094 | // tmp.replace(QRegExp(" "), "0"); |
||
1095 | // Datum += tmp; |
||
1096 | // tmp = dt.time().toString(); |
||
1097 | // tmp.replace(QRegExp(":"), ""); |
||
1098 | // Datum += tmp; |
||
1099 | // Datum += "Z"; |
||
12429 | fschmid | 1100 | |
19114 | avox | 1101 | // only include XMP to PDF/X-4 at the moment, could easily be extended to include it to any PDF |
1102 | if (Options.Version == PDFOptions::PDFVersion_X4) |
||
1103 | generateXMP(dt.toString("yyyy-MM-ddThh:mm:ssZ")); |
||
1104 | |||
1105 | /* The following code makes the resulting PDF "Reader enabled" in Acrobat Reader 8 |
||
1106 | but sadly it doesn't work with newer version, because its based on a bug in AR 8 |
||
1107 | PutDoc("/Perms\n"); |
||
1108 | PutDoc("<<\n"); |
||
1109 | PutDoc("/UR3\n"); |
||
1110 | PutDoc("<<\n"); |
||
1111 | PutDoc("/M ("+Datum+")\n"); |
||
1112 | PutDoc("/Name (Scribus "+QString(VERSION)+")\n"); |
||
1113 | PutDoc("/Reference [\n"); |
||
1114 | PutDoc("<<\n"); |
||
1115 | PutDoc("/TransformParams\n"); |
||
1116 | PutDoc("<<\n"); |
||
1117 | PutDoc("/Type /TransformParams\n"); |
||
1118 | PutDoc("/V /2.2\n"); |
||
1119 | PutDoc("/Document [/FullSave]\n"); |
||
1120 | PutDoc("/Annots [/Create/Delete/Modify/Copy/Import/Export]\n"); |
||
1121 | PutDoc("/Form [/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate]\n"); |
||
1122 | PutDoc("/Signature [/Modify]\n"); |
||
1123 | PutDoc(">>\n"); |
||
1124 | PutDoc("/TransformMethod /UR3\n"); |
||
1125 | PutDoc("/Type /SigRef\n"); |
||
1126 | PutDoc(">>\n"); |
||
1127 | PutDoc("]\n"); |
||
1128 | PutDoc("/Type /Sig\n"); |
||
1129 | PutDoc(">>\n"); |
||
1130 | PutDoc(">>\n"); |
||
1131 | */ |
||
1132 | |||
1133 | PutDoc("/ViewerPreferences\n<<\n/PageDirection "); |
||
1134 | PutDoc( Options.Binding == 0 ? "/L2R\n" : "/R2L\n"); |
||
1135 | if (Options.hideToolBar) |
||
1136 | PutDoc("/HideToolbar true\n"); |
||
1137 | if (Options.hideMenuBar) |
||
1138 | PutDoc("/HideMenubar true\n"); |
||
1139 | if (Options.fitWindow) |
||
1140 | PutDoc("/FitWindow true\n"); |
||
20112 | jghali | 1141 | PutDoc(" >>\n>>"); |
1142 | writer.endObj(writer.CatalogObj); |
||
20103 | avox | 1143 | } |
1144 | |||
1145 | void PDFLibCore::PDF_Begin_MetadataAndEncrypt() |
||
1146 | { |
||
19114 | avox | 1147 | QString IDg(Datum); |
1148 | IDg += Options.fileName; |
||
1149 | IDg += "Scribus "+QString(VERSION); |
||
1150 | IDg += "Scribus PDF Library "+QString(VERSION); |
||
1151 | IDg += doc.documentInfo().title(); |
||
1152 | IDg += doc.documentInfo().author(); |
||
1153 | IDg += "/False"; |
||
20112 | jghali | 1154 | writer.setFileId(Pdf::toPdfDocEncoding(IDg)); |
19114 | avox | 1155 | if (Options.Encrypt) |
1156 | { |
||
20112 | jghali | 1157 | writer.setEncryption((Options.Version == PDFOptions::PDFVersion_14) || (Options.Version == PDFOptions::PDFVersion_15), Pdf::toPdfDocEncoding(Options.PassOwner), Pdf::toPdfDocEncoding(Options.PassUser), Options.Permissions); |
19114 | avox | 1158 | } |
20103 | avox | 1159 | writer.startObj(writer.InfoObj); |
20112 | jghali | 1160 | PutDoc("<<\n/Creator " + EncString(QByteArray("Scribus ") + VERSION, writer.InfoObj) + "\n"); |
20103 | avox | 1161 | PutDoc("/Producer " + EncString(QByteArray("Scribus PDF Library ") + VERSION, writer.InfoObj) + "\n"); |
19114 | avox | 1162 | QString docTitle = doc.documentInfo().title(); |
19115 | avox | 1163 | if ((PDF_IsPDFX()) && (docTitle.isEmpty())) |
20103 | avox | 1164 | PutDoc("/Title " + EncStringUTF16(doc.DocName, writer.InfoObj) + "\n"); |
19114 | avox | 1165 | else |
20103 | avox | 1166 | PutDoc("/Title " + EncStringUTF16(doc.documentInfo().title(), writer.InfoObj) + "\n"); |
1167 | PutDoc("/Author " + EncStringUTF16(doc.documentInfo().author(), writer.InfoObj) + "\n"); |
||
1168 | PutDoc("/Subject " + EncStringUTF16(doc.documentInfo().subject(), writer.InfoObj) + "\n"); |
||
1169 | PutDoc("/Keywords " + EncStringUTF16(doc.documentInfo().keywords(), writer.InfoObj) + "\n"); |
||
1170 | PutDoc("/CreationDate " + EncString(Datum, writer.InfoObj) + "\n"); |
||
1171 | PutDoc("/ModDate " + EncString(Datum, writer.InfoObj) + "\n"); |
||
19114 | avox | 1172 | if (Options.Version == PDFOptions::PDFVersion_X1a) |
1173 | { |
||
1174 | PutDoc("/GTS_PDFXVersion (PDF/X-1:2001)\n"); |
||
1175 | PutDoc("/GTS_PDFXConformance (PDF/X-1a:2001)\n"); |
||
1176 | } |
||
19115 | avox | 1177 | if (Options.Version == PDFOptions::PDFVersion_X3) |
1178 | PutDoc("/GTS_PDFXVersion (PDF/X-3:2002)\n"); |
||
19114 | avox | 1179 | if (Options.Version == PDFOptions::PDFVersion_X4) |
1180 | PutDoc("/GTS_PDFXVersion (PDF/X-4)\n"); |
||
20112 | jghali | 1181 | PutDoc("/Trapped /False\n>>"); |
1182 | writer.endObj(writer.InfoObj); |
||
20103 | avox | 1183 | |
20112 | jghali | 1184 | // Encrypt |
1185 | |||
1186 | // for (int t = 0; t < 6; ++t) |
||
20103 | avox | 1187 | // XRef.append(bytesWritten()); |
1188 | // if (((Options.Version == PDFOptions::PDFVersion_15) || (Options.Version == PDFOptions::PDFVersion_X4)) && (Options.useLayers)) |
||
1189 | // XRef.append(bytesWritten()); |
||
1190 | // if (PDF_IsPDFX()) |
||
1191 | // XRef.append(bytesWritten()); |
||
1192 | // if (Options.Version == PDFOptions::PDFVersion_X4) |
||
1193 | // XRef.append(bytesWritten()); |
||
19114 | avox | 1194 | if (Options.Encrypt) |
20103 | avox | 1195 | { // now done in writer.setEncrption(): |
1196 | // writer.EncryptObj = writer.newObject(); |
||
1197 | // writer.startObj(writer.EncryptObj); |
||
1198 | // PutDoc("<<\n/Filter /Standard\n"); |
||
1199 | // PutDoc( KeyLen > 5 ? "/R 3\n/V 2\n/Length 128\n" : "/R 2\n/V 1\n"); |
||
1200 | // PutDoc("/O "+Pdf::toHexString(ok)+"\n"); |
||
1201 | // PutDoc("/U "+Pdf::toHexString(uk)+"\n"); |
||
20112 | jghali | 1202 | // PutDoc("/P "+Pdf::toPdf(Options.Permissions)+"\n>>"); |
1203 | // writer.endObj(writer.EncryptObj); |
||
19114 | avox | 1204 | } |
20103 | avox | 1205 | } |
1206 | |||
1207 | QMap<QString, QMap<uint, FPointArray> > |
||
1208 | PDFLibCore::PDF_Begin_FindUsedFonts(SCFonts &AllFonts, const QMap<QString, QMap<uint, FPointArray> >& DocFonts) |
||
1209 | { |
||
5387 | avox | 1210 | QMap<QString, QMap<uint, FPointArray> > ReallyUsed; |
3133 | fschmid | 1211 | ReallyUsed.clear(); |
1212 | PageItem* pgit; |
||
20103 | avox | 1213 | QMap<int, QByteArray> ind2PDFabr; |
1214 | const QByteArray tmpf[] = {"/Courier", "/Courier-Bold", "/Courier-Oblique", "/Courier-BoldOblique", |
||
4017 | fschmid | 1215 | "/Helvetica", "/Helvetica-Bold", "/Helvetica-Oblique", "/Helvetica-BoldOblique", |
4546 | subik | 1216 | "/Times-Roman", "/Times-Bold", "/Times-Italic", "/Times-BoldItalic", |
4017 | fschmid | 1217 | "/ZapfDingbats", "/Symbol"}; |
1218 | size_t ar = sizeof(tmpf) / sizeof(*tmpf); |
||
1219 | for (uint ax = 0; ax < ar; ++ax) |
||
1220 | ind2PDFabr[ax] = tmpf[ax]; |
||
21709 | jghali | 1221 | |
1222 | QList<PageItem*> allItems = doc.FrameItems.values(); |
||
1223 | while (allItems.count() > 0) |
||
3133 | fschmid | 1224 | { |
21709 | jghali | 1225 | pgit = allItems.takeFirst(); |
1226 | if (pgit->isGroup() || pgit->isTable()) |
||
3133 | fschmid | 1227 | { |
21709 | jghali | 1228 | allItems = pgit->getItemList() + allItems; |
1229 | continue; |
||
1230 | } |
||
1231 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
||
1232 | { |
||
1233 | if (pgit->isAnnotation()) |
||
17312 | fschmid | 1234 | { |
21709 | jghali | 1235 | int annotType = pgit->annotation().Type(); |
1236 | bool mustEmbed = ((annotType >= Annotation::Button) && (annotType <= Annotation::Listbox) && (annotType != Annotation::Checkbox)); |
||
1237 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
||
1238 | StdFonts.insert("/ZapfDingbats", ""); |
||
1239 | if (pgit->itemText.length() > 0 || mustEmbed) |
||
17312 | fschmid | 1240 | { |
21709 | jghali | 1241 | if (Options.Version < PDFOptions::PDFVersion_14) |
1242 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
21744 | jghali | 1243 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
1244 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
17312 | fschmid | 1245 | } |
1246 | } |
||
21709 | jghali | 1247 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1248 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1249 | for (uint e = start; e < stop; ++e) |
||
8092 | fschmid | 1250 | { |
21709 | jghali | 1251 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
1252 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
8092 | fschmid | 1253 | } |
3133 | fschmid | 1254 | } |
1255 | } |
||
21709 | jghali | 1256 | |
1257 | allItems = doc.MasterItems; |
||
1258 | while (allItems.count() > 0) |
||
3133 | fschmid | 1259 | { |
21709 | jghali | 1260 | pgit = allItems.takeFirst(); |
1261 | if (pgit->isGroup() || pgit->isTable()) |
||
3133 | fschmid | 1262 | { |
21709 | jghali | 1263 | allItems = pgit->getItemList() + allItems; |
1264 | continue; |
||
1265 | } |
||
1266 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
||
1267 | { |
||
1268 | if (pgit->isAnnotation()) |
||
17312 | fschmid | 1269 | { |
21709 | jghali | 1270 | int annotType = pgit->annotation().Type(); |
1271 | bool mustEmbed = ((annotType >= Annotation::Button) && (annotType <= Annotation::Listbox) && (annotType != Annotation::Checkbox)); |
||
1272 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
||
1273 | StdFonts.insert("/ZapfDingbats", ""); |
||
1274 | if (pgit->itemText.length() > 0 || mustEmbed) |
||
17312 | fschmid | 1275 | { |
21709 | jghali | 1276 | if (Options.Version < PDFOptions::PDFVersion_14) |
1277 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
1278 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
||
1279 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
17312 | fschmid | 1280 | } |
1281 | } |
||
21709 | jghali | 1282 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1283 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1284 | for (uint e = start; e < stop; ++e) |
||
8092 | fschmid | 1285 | { |
21709 | jghali | 1286 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
1287 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
1288 | } |
||
1289 | } |
||
1290 | } |
||
1291 | |||
1292 | allItems = *(doc.Items); |
||
1293 | while (allItems.count() > 0) |
||
1294 | { |
||
1295 | pgit = allItems.takeFirst(); |
||
1296 | if (pgit->isGroup() || pgit->isTable()) |
||
1297 | { |
||
1298 | allItems = pgit->getItemList() + allItems; |
||
1299 | continue; |
||
1300 | } |
||
1301 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
||
1302 | { |
||
1303 | if (pgit->isAnnotation()) |
||
1304 | { |
||
1305 | int annotType = pgit->annotation().Type(); |
||
1306 | bool mustEmbed = ((annotType >= Annotation::Button) && (annotType <= Annotation::Listbox) && (annotType != Annotation::Checkbox)); |
||
1307 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
||
1308 | StdFonts.insert("/ZapfDingbats", ""); |
||
1309 | if (pgit->itemText.length() > 0 || mustEmbed) |
||
12763 | fschmid | 1310 | { |
21709 | jghali | 1311 | if (Options.Version < PDFOptions::PDFVersion_14) |
1312 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
1313 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
||
21707 | jghali | 1314 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
16953 | fschmid | 1315 | } |
8092 | fschmid | 1316 | } |
21709 | jghali | 1317 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1318 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1319 | for (uint e = start; e < stop; ++e) |
||
1320 | { |
||
1321 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
||
1322 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
1323 | } |
||
3133 | fschmid | 1324 | } |
1325 | } |
||
21709 | jghali | 1326 | /* if (Options.docInfoMarks) |
3133 | fschmid | 1327 | { |
21709 | jghali | 1328 | StdFonts.insert("/Helvetica", ""); |
1329 | } */ |
||
1330 | QStringList patterns = doc.getPatternDependencyList(doc.getUsedPatterns()); |
||
1331 | for (int c = 0; c < patterns.count(); ++c) |
||
1332 | { |
||
1333 | ScPattern pa = doc.docPatterns[patterns[c]]; |
||
1334 | allItems = pa.items; |
||
1335 | while (allItems.count() > 0) |
||
3133 | fschmid | 1336 | { |
21709 | jghali | 1337 | pgit = allItems.takeFirst(); |
1338 | if (pgit->isGroup() || pgit->isTable()) |
||
17312 | fschmid | 1339 | { |
21709 | jghali | 1340 | allItems = pgit->getItemList() + allItems; |
1341 | continue; |
||
17312 | fschmid | 1342 | } |
16953 | fschmid | 1343 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
8092 | fschmid | 1344 | { |
16953 | fschmid | 1345 | if (pgit->isAnnotation()) |
12763 | fschmid | 1346 | { |
17952 | fschmid | 1347 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
16953 | fschmid | 1348 | StdFonts.insert("/ZapfDingbats", ""); |
21709 | jghali | 1349 | if (pgit->itemText.length() > 0) |
16953 | fschmid | 1350 | { |
1351 | if (Options.Version < PDFOptions::PDFVersion_14) |
||
1352 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
21707 | jghali | 1353 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
1354 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
16953 | fschmid | 1355 | } |
12763 | fschmid | 1356 | } |
17751 | jghali | 1357 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1358 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1359 | for (uint e = start; e < stop; ++e) |
||
16953 | fschmid | 1360 | { |
21707 | jghali | 1361 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
1362 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
16953 | fschmid | 1363 | } |
8092 | fschmid | 1364 | } |
3133 | fschmid | 1365 | } |
1366 | } |
||
20112 | jghali | 1367 | return ReallyUsed; |
20103 | avox | 1368 | } |
17628 | jghali | 1369 | |
20103 | avox | 1370 | static QByteArray sanitizeFontName(QString fn) |
1371 | { |
||
20112 | jghali | 1372 | return Pdf::toPdfDocEncoding(fn.replace( QRegExp("[\\s\\/\\{\\[\\]\\}\\<\\>\\(\\)\\%]"), "_" )); |
20103 | avox | 1373 | } |
17628 | jghali | 1374 | |
20103 | avox | 1375 | static QList<Pdf::Resource> asColorSpace(QList<PdfICCD> iccCSlist) |
1376 | { |
||
20112 | jghali | 1377 | QList<Pdf::Resource> result; |
1378 | foreach (Pdf::Resource r, iccCSlist) |
||
1379 | { |
||
1380 | result.append(r); |
||
1381 | } |
||
1382 | return result; |
||
20103 | avox | 1383 | } |
1384 | |||
1385 | static QList<Pdf::Resource> asColorSpace(QList<PdfSpotC> spotMapValues) |
||
1386 | { |
||
20112 | jghali | 1387 | QList<Pdf::Resource> result; |
1388 | foreach (Pdf::Resource r, spotMapValues) |
||
1389 | { |
||
1390 | result.append(r); |
||
1391 | } |
||
1392 | return result; |
||
20103 | avox | 1393 | } |
1394 | |||
1395 | void PDFLibCore::PDF_WriteStandardFonts() |
||
1396 | { |
||
20112 | jghali | 1397 | int a = 0; |
1398 | QMap<QString, QString>::Iterator itStd; |
||
1399 | for (itStd = StdFonts.begin(); itStd != StdFonts.end(); ++itStd) |
||
1400 | { |
||
1401 | PdfId fontObject = writer.newObject(); |
||
1402 | writer.startObj(fontObject); |
||
1403 | PutDoc("<<\n/Type /Font\n/Subtype /Type1\n"); |
||
1404 | PutDoc("/Name /FoStd"+Pdf::toPdf(a)+"\n"); |
||
1405 | PutDoc("/BaseFont "+Pdf::toPdfDocEncoding(itStd.key())+"\n"); |
||
1406 | if (itStd.key() != "/ZapfDingbats") |
||
1407 | { |
||
1408 | PutDoc("/Encoding << \n"); |
||
1409 | PutDoc("/Differences [ \n"); |
||
1410 | PutDoc("24 /breve /caron /circumflex /dotaccent /hungarumlaut /ogonek /ring /tilde\n"); |
||
1411 | PutDoc("39 /quotesingle 96 /grave 128 /bullet /dagger /daggerdbl /ellipsis /emdash /endash /florin /fraction /guilsinglleft /guilsinglright\n"); |
||
1412 | PutDoc("/minus /perthousand /quotedblbase /quotedblleft /quotedblright /quoteleft /quoteright /quotesinglbase /trademark /fi /fl /Lslash /OE /Scaron\n"); |
||
21744 | jghali | 1413 | PutDoc("/Ydieresis /Zcaron /dotlessi /lslash /oe /scaron /zcaron 160 /Euro 164 /currency 166 /brokenbar 168 /dieresis /copyright /ordfeminine 172 /logicalnot\n"); |
20112 | jghali | 1414 | PutDoc("/.notdef /registered /macron /degree /plusminus /twosuperior /threesuperior /acute /mu 183 /periodcentered /cedilla /onesuperior /ordmasculine\n"); |
1415 | PutDoc("188 /onequarter /onehalf /threequarters 192 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex\n"); |
||
1416 | PutDoc("/Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash\n"); |
||
1417 | PutDoc("/Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla\n"); |
||
1418 | PutDoc("/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis\n"); |
||
1419 | PutDoc("/divide /oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis\n"); |
||
1420 | PutDoc("] >>\n"); |
||
1421 | } |
||
1422 | PutDoc(">>"); |
||
1423 | writer.endObj(fontObject); |
||
1424 | pageData.FObjects["FoStd"+Pdf::toPdf(a)] = fontObject; |
||
1425 | itStd.value() = "FoStd"+Pdf::toPdf(a); |
||
1426 | a++; |
||
1427 | } |
||
20103 | avox | 1428 | } |
1429 | |||
1430 | |||
1431 | PdfFont PDFLibCore::PDF_WriteType3Font(const QByteArray& name, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
1432 | { |
||
20112 | jghali | 1433 | PdfFont result; |
1434 | result.name = Pdf::toName(name); |
||
1435 | result.method = Use_Type3; |
||
1436 | result.encoding = Encode_256; |
||
20103 | avox | 1437 | |
20112 | jghali | 1438 | uint SubFonts = 0; |
1439 | int glyphCount = 0; |
||
1440 | double minx = std::numeric_limits<double>::max(); |
||
1441 | double miny = std::numeric_limits<double>::max(); |
||
1442 | double maxx = -std::numeric_limits<double>::max(); |
||
1443 | double maxy = -std::numeric_limits<double>::max(); |
||
1444 | QList<uint> glyphWidths; |
||
1445 | QList<QByteArray> charProcs; |
||
1446 | QByteArray encoding = "<< /Type /Encoding\n/Differences [ 0\n"; |
||
1447 | QByteArray fon; |
||
1448 | QMap<uint, uint> glyphMapping; |
||
1449 | ScFace::FaceEncoding gl; |
||
1450 | face.glyphNames(gl); |
||
1451 | QMap<uint,FPointArray>::ConstIterator ig; |
||
1452 | for (ig = RealGlyphs.cbegin(); ig != RealGlyphs.cend(); ++ig) |
||
1453 | { |
||
1454 | FPoint np, np1, np2; |
||
1455 | bool nPath = true; |
||
1456 | fon.resize(0); |
||
1457 | if (ig.value().size() > 3) |
||
1458 | { |
||
1459 | FPointArray gly = ig.value(); |
||
1460 | QTransform mat; |
||
1461 | mat.scale(100.0, -100.0); |
||
1462 | gly.map(mat); |
||
1463 | gly.translate(0, 1000); |
||
1464 | for (int poi = 0; poi < gly.size()-3; poi += 4) |
||
1465 | { |
||
1466 | if (gly.isMarker(poi)) |
||
1467 | { |
||
1468 | fon += "h\n"; |
||
1469 | nPath = true; |
||
1470 | continue; |
||
1471 | } |
||
1472 | if (nPath) |
||
1473 | { |
||
1474 | np = gly.point(poi); |
||
1475 | fon += FToStr(np.x())+" "+FToStr(np.y())+" m\n"; |
||
1476 | nPath = false; |
||
1477 | } |
||
1478 | np = gly.point(poi+1); |
||
1479 | np1 = gly.point(poi+3); |
||
1480 | np2 = gly.point(poi+2); |
||
1481 | fon += FToStr(np.x()) + " " + FToStr(np.y()) + " " + FToStr(np1.x()) + " " + FToStr(np1.y()) + " " + FToStr(np2.x()) + " " + FToStr(np2.y()) + " c\n"; |
||
1482 | } |
||
1483 | fon += "h f*\n"; |
||
1484 | np = getMinClipF(&gly); |
||
1485 | np1 = getMaxClipF(&gly); |
||
1486 | } |
||
1487 | else |
||
1488 | { |
||
20684 | jghali | 1489 | // 13654: make evince emit warning about "Syntax Error: No current point in closepath" |
1490 | // PDF specification is not quite clear about that, but a single "h" operator might not |
||
1491 | // be a valid way to make an empty path |
||
1492 | // fon = "h"; |
||
20112 | jghali | 1493 | np = FPoint(0, 0); |
1494 | np1 = FPoint(0, 0); |
||
1495 | } |
||
1496 | fon.prepend(Pdf::toPdf(qRound(np1.x())) + " 0 "+Pdf::toPdf(qRound(np.x()))+" "+Pdf::toPdf(qRound(np.y()))+" "+Pdf::toPdf(qRound(np1.x()))+ " "+Pdf::toPdf(qRound(np1.y()))+" d1\n"); |
||
1497 | minx = qMin(minx, np.x()); |
||
1498 | miny = qMin(miny, np.y()); |
||
1499 | maxx = qMax(maxx, np1.x()); |
||
1500 | maxy = qMax(maxy, np1.y()); |
||
1501 | glyphWidths.append(qRound(np1.x())); |
||
1502 | PdfId charProcObject = writer.newObject(); |
||
1503 | charProcs.append(Pdf::toName(gl[ig.key()].second)+" "+Pdf::toPdf(charProcObject)+" 0 R\n"); |
||
1504 | encoding += Pdf::toName(gl[ig.key()].second)+" "; |
||
1505 | glyphMapping.insert(ig.key(), glyphCount + SubFonts * 256); |
||
1506 | writer.startObj(charProcObject); |
||
1507 | if (Options.Compress) |
||
1508 | fon = CompressArray(fon); |
||
1509 | PutDoc("<< /Length "+Pdf::toPdf(fon.length()+1)); |
||
1510 | if (Options.Compress) |
||
1511 | PutDoc("\n/Filter /FlateDecode"); |
||
1512 | PutDoc("\n>>\nstream\n"+EncStream(fon, charProcObject)+"\nendstream"); |
||
1513 | writer.endObj(charProcObject); |
||
1514 | glyphCount++; |
||
1515 | int glyphsLeft = RealGlyphs.count() - SubFonts * 256; |
||
1516 | if ((glyphCount > 255) || (glyphCount == glyphsLeft)) |
||
1517 | { |
||
1518 | PdfId fontWidths = writer.newObject(); |
||
1519 | writer.startObj(fontWidths); |
||
1520 | PutDoc("[ "); |
||
1521 | for (int ww = 0; ww < glyphWidths.count(); ++ww) |
||
1522 | { |
||
1523 | PutDoc(Pdf::toPdf(glyphWidths[ww])+" "); |
||
1524 | } |
||
1525 | PutDoc("]"); |
||
1526 | writer.endObj(fontWidths); |
||
1527 | PdfId fontCharProcs = writer.newObject(); |
||
1528 | writer.startObj(fontCharProcs); |
||
1529 | PutDoc("<<\n"); |
||
1530 | for (int ww = 0; ww < charProcs.count(); ++ww) |
||
1531 | { |
||
1532 | PutDoc(charProcs[ww]); |
||
1533 | } |
||
1534 | PutDoc(">>"); |
||
1535 | writer.endObj(fontCharProcs); |
||
1536 | PdfId fontEncoding = writer.newObject(); |
||
1537 | writer.startObj(fontEncoding); |
||
1538 | PutDoc(encoding); |
||
1539 | PutDoc("]\n"); |
||
1540 | PutDoc(">>"); |
||
1541 | writer.endObj(fontEncoding); |
||
1542 | PdfId font3Object = writer.newObject(); |
||
1543 | writer.startObj(font3Object); |
||
1544 | PutDoc("<<\n/Type /Font\n/Subtype /Type3\n"); |
||
1545 | PutDoc("/Name /"+name+"S"+Pdf::toPdf(SubFonts)+"\n"); |
||
1546 | PutDoc("/FirstChar 0\n"); |
||
1547 | PutDoc("/LastChar "+Pdf::toPdf(glyphCount-1)+"\n"); |
||
1548 | PutDoc("/Widths "+Pdf::toPdf(fontWidths)+" 0 R\n"); |
||
1549 | PutDoc("/CharProcs "+Pdf::toPdf(fontCharProcs)+" 0 R\n"); |
||
1550 | PutDoc("/FontBBox ["+Pdf::toPdf(qRound(minx))+" "+Pdf::toPdf(qRound(miny))+" "+Pdf::toPdf(qRound(maxx))+ " "+Pdf::toPdf(qRound(maxy))+"]\n"); |
||
1551 | PutDoc("/FontMatrix [0.001 0 0 0.001 0 0]\n"); |
||
1552 | PutDoc("/Encoding "+Pdf::toPdf(fontEncoding)+" 0 R\n"); |
||
1553 | PutDoc(">>"); |
||
1554 | writer.endObj(font3Object); |
||
1555 | pageData.FObjects[name+"S"+Pdf::toPdf(SubFonts)] = font3Object; |
||
1556 | charProcs.clear(); |
||
1557 | glyphWidths.clear(); |
||
1558 | // glyphMapping.clear(); |
||
1559 | glyphCount = 0; |
||
1560 | ++SubFonts; |
||
1561 | minx = std::numeric_limits<double>::max(); |
||
1562 | miny = std::numeric_limits<double>::max(); |
||
1563 | maxx = -std::numeric_limits<double>::max(); |
||
1564 | maxy = -std::numeric_limits<double>::max(); |
||
1565 | encoding = "<< /Type /Encoding\n/Differences [ 0\n"; |
||
1566 | } |
||
1567 | } |
||
1568 | result.glyphmap = glyphMapping; |
||
1569 | return result; |
||
20103 | avox | 1570 | } |
1571 | |||
1572 | |||
1573 | PdfFont PDFLibCore::PDF_WriteGlyphsAsXForms(const QByteArray& fontName, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
1574 | { |
||
20112 | jghali | 1575 | PdfFont result; |
1576 | result.name = Pdf::toName(fontName); |
||
1577 | result.method = Use_XForm; |
||
1578 | result.encoding = Encode_224; |
||
20103 | avox | 1579 | |
20112 | jghali | 1580 | QByteArray fon; |
1581 | QMap<uint,FPointArray>::ConstIterator ig; |
||
1582 | for (ig = RealGlyphs.cbegin(); ig != RealGlyphs.cend(); ++ig) |
||
1583 | { |
||
1584 | FPoint np, np1, np2; |
||
1585 | bool nPath = true; |
||
1586 | fon.resize(0); |
||
1587 | if (ig.value().size() > 3) |
||
1588 | { |
||
1589 | FPointArray gly = ig.value(); |
||
1590 | QTransform mat; |
||
1591 | mat.scale(0.1, 0.1); |
||
1592 | gly.map(mat); |
||
1593 | for (int poi = 0; poi < gly.size()-3; poi += 4) |
||
1594 | { |
||
1595 | if (gly.isMarker(poi)) |
||
1596 | { |
||
1597 | fon += "h\n"; |
||
1598 | nPath = true; |
||
1599 | continue; |
||
1600 | } |
||
1601 | if (nPath) |
||
1602 | { |
||
1603 | np = gly.point(poi); |
||
1604 | fon += FToStr(np.x())+" "+FToStr(-np.y())+" m\n"; |
||
1605 | nPath = false; |
||
1606 | } |
||
1607 | np = gly.point(poi+1); |
||
1608 | np1 = gly.point(poi+3); |
||
1609 | np2 = gly.point(poi+2); |
||
1610 | fon += FToStr(np.x()) + " " + FToStr(-np.y()) + " " + |
||
1611 | FToStr(np1.x()) + " " + FToStr(-np1.y()) + " " + |
||
1612 | FToStr(np2.x()) + " " + FToStr(-np2.y()) + " c\n"; |
||
1613 | } |
||
1614 | fon += "h f*\n"; |
||
1615 | np = getMinClipF(&gly); |
||
1616 | np1 = getMaxClipF(&gly); |
||
1617 | } |
||
1618 | else |
||
1619 | { |
||
20684 | jghali | 1620 | // 13654: make evince emit warning about "Syntax Error: No current point in closepath" |
1621 | // PDF specification is not quite clear about that, but a single "h" operator might not |
||
1622 | // be a valid way to make an empty path |
||
1623 | fon = " " /*"h"*/; |
||
20112 | jghali | 1624 | np = FPoint(0, 0); |
1625 | np1 = FPoint(0, 0); |
||
1626 | } |
||
1627 | PdfId fontGlyphXForm = writer.newObject(); |
||
1628 | writer.startObj(fontGlyphXForm); |
||
1629 | PutDoc("<<\n/Type /XObject\n/Subtype /Form\n/FormType 1\n"); |
||
1630 | PutDoc("/BBox [ "+FToStr(np.x())+" "+FToStr(-np.y())+" "+FToStr(np1.x())+ " "+FToStr(-np1.y())+" ]\n"); |
||
1631 | PutDoc("/Resources << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]\n"); |
||
1632 | PutDoc(">>\n"); |
||
1633 | if (Options.Compress) |
||
1634 | fon = CompressArray(fon); |
||
1635 | PutDoc("/Length "+Pdf::toPdf(fon.length()+1)); |
||
1636 | if (Options.Compress) |
||
1637 | PutDoc("\n/Filter /FlateDecode"); |
||
1638 | PutDoc(" >>\nstream\n"+EncStream(fon, fontGlyphXForm)+"\nendstream"); |
||
1639 | writer.endObj(fontGlyphXForm); |
||
1640 | pageData.XObjects[fontName + Pdf::toPdf(ig.key())] = fontGlyphXForm; |
||
1641 | } |
||
1642 | return result; |
||
20103 | avox | 1643 | } |
1644 | |||
1645 | PdfId PDFLibCore::PDF_EmbedFontObject(const QByteArray& font, const QByteArray& subtype) |
||
1646 | |||
1647 | { |
||
20112 | jghali | 1648 | PdfId embeddedFontObject = writer.newObject(); |
1649 | writer.startObj(embeddedFontObject); |
||
1650 | int len = font.length(); |
||
1651 | QByteArray ttf = (Options.Compress? CompressArray(font) : font); |
||
1652 | //qDebug() << QString("sfnt data: size=%1 compressed=%2").arg(len).arg(bb.length()); |
||
1653 | PutDoc("<<\n/Length " + Pdf::toPdf(ttf.length() + 1) + "\n"); |
||
1654 | PutDoc("/Length1 " + Pdf::toPdf(len) + "\n"); |
||
1655 | if (subtype.size() > 0) |
||
1656 | PutDoc("/Subtype " + subtype); |
||
1657 | if (Options.Compress) |
||
1658 | PutDoc("/Filter /FlateDecode\n"); |
||
1659 | PutDoc(">>\nstream\n"); |
||
1660 | EncodeArrayToStream(ttf, embeddedFontObject); |
||
1661 | PutDoc("\nendstream"); |
||
1662 | writer.endObj(embeddedFontObject); |
||
1663 | return embeddedFontObject; |
||
20103 | avox | 1664 | } |
1665 | |||
21775 | jghali | 1666 | QByteArray PDFLibCore::PDF_GenerateSubsetTag(const QByteArray& fontName, QList<uint> usedGlyphs) |
1667 | { |
||
1668 | uint hash, mod; |
||
1669 | QVector<uint> glyphVec = usedGlyphs.toVector(); |
||
1670 | |||
1671 | hash = qHashBits(fontName.constData(), fontName.size(), 0); |
||
1672 | hash = qHashBits(glyphVec.constData(), glyphVec.size() * sizeof(uint), hash); |
||
1673 | |||
1674 | QByteArray subsetTag(6, (char) 0); |
||
1675 | for (int i = 0; i < 6; ++i) |
||
1676 | { |
||
1677 | mod = hash % 26; |
||
1678 | subsetTag[i] = 'A' + mod; |
||
1679 | hash = (hash - mod) / 26; |
||
1680 | } |
||
1681 | |||
1682 | return subsetTag; |
||
1683 | } |
||
1684 | |||
20103 | avox | 1685 | PdfId PDFLibCore::PDF_WriteFontDescriptor(const QByteArray& fontName, ScFace& face, ScFace::FontFormat fformat, PdfId embeddedFontObject) |
1686 | { |
||
20112 | jghali | 1687 | PdfId fontDescriptor = writer.newObject(); |
1688 | writer.startObj(fontDescriptor); |
||
1689 | // TODO: think about QByteArray ScFace::getFontDescriptor() -- AV |
||
1690 | PutDoc("<<\n/Type /FontDescriptor\n"); |
||
1691 | PutDoc("/FontName " + Pdf::toName(fontName) + "\n"); |
||
1692 | PutDoc("/FontBBox [ " + Pdf::toAscii(face.pdfFontBBoxAsString()) + " ]\n"); |
||
1693 | PutDoc("/Flags "); |
||
1694 | //FIXME: isItalic() should be queried from ScFace, not from Qt -- AV |
||
1695 | //QFontInfo fo = QFontInfo(it.data()); |
||
1696 | int pfl = 0; |
||
1697 | if (face.isFixedPitch()) |
||
1698 | pfl = pfl ^ 1; |
||
1699 | //if (fo.italic()) |
||
1700 | if (face.italicAngleAsString() != "0") |
||
1701 | pfl = pfl ^ 64; |
||
1702 | // pfl = pfl ^ 4; |
||
1703 | pfl = pfl ^ 32; |
||
1704 | PutDoc(Pdf::toPdf(pfl)+"\n"); |
||
1705 | PutDoc("/Ascent " + Pdf::toAscii(face.pdfAscentAsString())+"\n"); |
||
1706 | PutDoc("/Descent " + Pdf::toAscii(face.pdfDescentAsString())+"\n"); |
||
1707 | PutDoc("/CapHeight " + Pdf::toAscii(face.pdfCapHeightAsString())+"\n"); |
||
1708 | PutDoc("/ItalicAngle " + Pdf::toAscii(face.italicAngleAsString())+"\n"); |
||
1709 | PutDoc("/StemV 1\n"); |
||
1710 | if (embeddedFontObject != 0) |
||
1711 | { |
||
1712 | if (fformat == ScFace::SFNT || fformat == ScFace::TTCF) |
||
1713 | { |
||
1714 | if (face.type() == ScFace::OTF) |
||
1715 | { |
||
1716 | PutDoc("/FontFile3 "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1717 | } |
||
1718 | else |
||
1719 | { |
||
1720 | PutDoc("/FontFile2 "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1721 | } |
||
1722 | } |
||
1723 | else if (fformat == ScFace::PFB) |
||
1724 | PutDoc("/FontFile "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1725 | else if (fformat == ScFace::PFA) |
||
1726 | PutDoc("/FontFile "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1727 | } |
||
1728 | PutDoc(">>"); |
||
1729 | writer.endObj(fontDescriptor); |
||
1730 | return fontDescriptor; |
||
20103 | avox | 1731 | } |
1732 | |||
1733 | PdfFont PDFLibCore::PDF_EncodeCidFont(const QByteArray& fontName, ScFace& face, const QByteArray& baseFont, PdfId fontDes, const ScFace::FaceEncoding& gl, const QMap<uint,uint> glyphmap ) |
||
1734 | { |
||
20112 | jghali | 1735 | PdfFont result; |
1736 | result.name = Pdf::toName(fontName); |
||
1737 | result.method = glyphmap.isEmpty()? Use_Embedded : Use_Subset; |
||
1738 | result.encoding = glyphmap.isEmpty()? Encode_IdentityH : Encode_Subset; |
||
1739 | result.glyphmap = glyphmap; |
||
1740 | |||
1741 | PdfId fontWidths2 = writer.newObject(); |
||
1742 | writer.startObj(fontWidths2); |
||
1743 | QList<QByteArray> toUnicodeMaps; |
||
1744 | QList<int> toUnicodeMapsCount; |
||
1745 | QByteArray toUnicodeMap = ""; |
||
1746 | int toUnicodeMapCounter = 0; |
||
1747 | |||
1748 | PutDoc("[ "); |
||
1749 | QList<uint> keys = gl.uniqueKeys(); |
||
1750 | QList<uint>::iterator git; |
||
1751 | bool seenNotDef = false; |
||
1752 | for (git = keys.begin(); git != keys.end(); ++git) |
||
1753 | { |
||
1754 | uint gid = result.encoding == Encode_Subset? glyphmap[*git] : *git; |
||
1755 | if (gid > 0 || !seenNotDef) |
||
1756 | { |
||
1757 | seenNotDef |= (gid == 0); |
||
1758 | PutDoc(Pdf::toPdf(gid)+" ["+Pdf::toPdf(static_cast<int>(face.glyphWidth(*git)* 1000))+"] " ); |
||
1759 | QString tmp, tmp2; |
||
1760 | tmp.sprintf("%04X", gid); |
||
1761 | tmp2.sprintf("%04X", gl.value(*git).first); |
||
1762 | toUnicodeMap += "<" + Pdf::toAscii(tmp)+ "> <" + Pdf::toAscii(tmp2) + ">\n"; |
||
1763 | toUnicodeMapCounter++; |
||
1764 | if (toUnicodeMapCounter == 100) |
||
1765 | { |
||
1766 | toUnicodeMaps.append(toUnicodeMap); |
||
1767 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1768 | toUnicodeMap = ""; |
||
1769 | toUnicodeMapCounter = 0; |
||
1770 | } |
||
1771 | } |
||
1772 | } |
||
1773 | PutDoc("]"); |
||
1774 | writer.endObj(fontWidths2); |
||
1775 | if (toUnicodeMapCounter != 0) |
||
1776 | { |
||
1777 | toUnicodeMaps.append(toUnicodeMap); |
||
1778 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1779 | } |
||
1780 | QByteArray toUnicodeMapStream = ""; |
||
1781 | toUnicodeMapStream += "/CIDInit /ProcSet findresource begin\n"; |
||
1782 | toUnicodeMapStream += "12 dict begin\n"; |
||
1783 | toUnicodeMapStream += "begincmap\n"; |
||
1784 | toUnicodeMapStream += "/CIDSystemInfo <<\n"; |
||
1785 | toUnicodeMapStream += "/Registry (Adobe)\n"; |
||
1786 | toUnicodeMapStream += "/Ordering (UCS)\n"; |
||
1787 | toUnicodeMapStream += "/Supplement 0\n"; |
||
1788 | toUnicodeMapStream += ">> def\n"; |
||
1789 | toUnicodeMapStream += "/CMapName /Adobe-Identity-UCS def\n"; |
||
1790 | toUnicodeMapStream += "/CMapType 2 def\n"; |
||
1791 | toUnicodeMapStream += "1 begincodespacerange\n"; |
||
1792 | toUnicodeMapStream += "<0000> <FFFF>\n"; |
||
1793 | toUnicodeMapStream += "endcodespacerange\n"; |
||
1794 | for (int uniC = 0; uniC < toUnicodeMaps.count(); uniC++) |
||
1795 | { |
||
1796 | toUnicodeMapStream += Pdf::toPdf(toUnicodeMapsCount[uniC]); |
||
1797 | toUnicodeMapStream += " beginbfchar\n"; |
||
1798 | toUnicodeMapStream += toUnicodeMaps[uniC]; |
||
1799 | toUnicodeMapStream += "endbfchar\n"; |
||
1800 | } |
||
1801 | toUnicodeMapStream += "endcmap\n"; |
||
1802 | toUnicodeMapStream += "CMapName currentdict /CMap defineresource pop\n"; |
||
1803 | toUnicodeMapStream += "end\n"; |
||
1804 | toUnicodeMapStream += "end\n"; |
||
1805 | PdfId fontToUnicode2 = WritePDFStream(toUnicodeMapStream); |
||
1806 | PdfId fontObject2 = writer.newObject(); |
||
1807 | writer.startObj(fontObject2); |
||
1808 | PutDoc("<<\n/Type /Font\n/Subtype /Type0\n"); |
||
1809 | PutDoc("/Name " + Pdf::toName(fontName) + "\n"); |
||
21759 | jghali | 1810 | PutDoc("/BaseFont "+ Pdf::toName(baseFont) +"\n"); |
20112 | jghali | 1811 | PutDoc("/Encoding /Identity-H\n"); |
1812 | PutDoc("/ToUnicode "+Pdf::toPdf(fontToUnicode2)+" 0 R\n"); |
||
1813 | PutDoc("/DescendantFonts ["); |
||
1814 | PutDoc("<</Type /Font"); |
||
1815 | if (face.type() == ScFace::OTF) |
||
1816 | PutDoc("/Subtype /CIDFontType0"); |
||
1817 | else |
||
1818 | PutDoc("/Subtype /CIDFontType2"); |
||
21759 | jghali | 1819 | PutDoc("/BaseFont " + Pdf::toName(baseFont)); |
20112 | jghali | 1820 | PutDoc("/FontDescriptor " + Pdf::toPdf(fontDes)+ " 0 R"); |
1821 | PutDoc("/CIDSystemInfo <</Ordering(Identity)/Registry(Adobe)/Supplement 0>>"); |
||
1822 | PutDoc("/DW 1000"); |
||
1823 | PutDoc("/W "+Pdf::toPdf(fontWidths2)+" 0 R"); |
||
1824 | PutDoc("/CIDToGIDMap /Identity"); |
||
1825 | PutDoc(">>"); // close CIDFont dictionary |
||
1826 | PutDoc("]\n"); // close DescendantFonts array |
||
1827 | PutDoc(">>"); |
||
1828 | writer.endObj(fontObject2); |
||
1829 | pageData.FObjects[fontName] = fontObject2; |
||
1830 | return result; |
||
20103 | avox | 1831 | } |
1832 | |||
1833 | |||
1834 | PdfFont PDFLibCore::PDF_EncodeSimpleFont(const QByteArray& fontName, ScFace& face, const QByteArray& baseFont, const QByteArray& subtype, bool isEmbedded, PdfId fontDes, const ScFace::FaceEncoding& gl) |
||
1835 | { |
||
20112 | jghali | 1836 | PdfFont result; |
1837 | result.name = Pdf::toName(fontName); |
||
1838 | result.method = isEmbedded? Use_Embedded : Use_System; |
||
1839 | result.encoding = Encode_224; |
||
1840 | |||
1841 | int nglyphs = 0; |
||
1842 | ScFace::FaceEncoding::ConstIterator gli; |
||
1843 | for (gli = gl.cbegin(); gli != gl.cend(); ++gli) |
||
1844 | { |
||
1845 | if (gli.key() > static_cast<uint>(nglyphs)) |
||
1846 | nglyphs = gli.key(); |
||
1847 | } |
||
1848 | ++nglyphs; |
||
1849 | // qDebug() << QString("pdflib: nglyphs %1 max %2").arg(nglyphs).arg(face.maxGlyph()); |
||
1850 | |||
1851 | uint Fcc = nglyphs / 224; |
||
1852 | if ((nglyphs % 224) != 0) |
||
1853 | Fcc += 1; |
||
1854 | for (uint Fc = 0; Fc < Fcc; ++Fc) |
||
1855 | { |
||
1856 | PdfId fontWidths2 = writer.newObject(); |
||
1857 | writer.startObj(fontWidths2); |
||
1858 | int chCount = 32; |
||
1859 | PutDoc("[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "); |
||
1860 | for (int ww = 32; ww < 256; ++ww) |
||
1861 | { |
||
1862 | uint glyph = 224 * Fc + ww - 32; |
||
1863 | if (gl.contains(glyph)) |
||
1864 | PutDoc(Pdf::toPdf(static_cast<int>(face.glyphWidth(glyph)* 1000))+" "); |
||
1865 | else |
||
1866 | PutDoc("0 "); |
||
1867 | chCount++; |
||
1868 | if (signed(glyph) == nglyphs-1) |
||
1869 | break; |
||
1870 | } |
||
1871 | PutDoc("]"); |
||
1872 | writer.endObj(fontWidths2); |
||
1873 | PdfId fontEncoding2 = writer.newObject(); |
||
1874 | writer.startObj(fontEncoding2); |
||
1875 | QStringList toUnicodeMaps; |
||
1876 | QList<int> toUnicodeMapsCount; |
||
1877 | QString toUnicodeMap = ""; |
||
1878 | int toUnicodeMapCounter = 0; |
||
1879 | PutDoc("<< /Type /Encoding\n"); |
||
1880 | PutDoc("/Differences [ \n"); |
||
1881 | int crc = 0; |
||
1882 | bool startOfSeq = true; |
||
1883 | for (int ww2 = 32; ww2 < 256; ++ww2) |
||
1884 | { |
||
1885 | uint glyph = 224 * Fc + ww2 - 32; |
||
1886 | ScFace::FaceEncoding::ConstIterator glIt = gl.find(glyph); |
||
1887 | if (glIt != gl.cend() && !glIt.value().second.isEmpty()) |
||
1888 | { |
||
1889 | if (startOfSeq) |
||
1890 | { |
||
1891 | PutDoc(Pdf::toPdf(ww2)+" "); |
||
1892 | startOfSeq = false; |
||
1893 | } |
||
1894 | PutDoc(Pdf::toName(glIt.value().second)+" "); |
||
1895 | QString tmp, tmp2; |
||
1896 | tmp.sprintf("%02X", ww2); |
||
1897 | tmp2.sprintf("%04X", glIt.value().first); |
||
1898 | toUnicodeMap += "<" + Pdf::toAscii(tmp) + "> <" + Pdf::toAscii(tmp2) + ">\n"; |
||
1899 | //QString("<%1> <%2>\n").arg(tmp).arg((tmp2)); |
||
1900 | toUnicodeMapCounter++; |
||
1901 | if (toUnicodeMapCounter == 100) |
||
1902 | { |
||
1903 | toUnicodeMaps.append(toUnicodeMap); |
||
1904 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1905 | toUnicodeMap = ""; |
||
1906 | toUnicodeMapCounter = 0; |
||
1907 | } |
||
1908 | crc++; |
||
1909 | } |
||
1910 | else |
||
1911 | { |
||
1912 | startOfSeq = true; |
||
1913 | } |
||
1914 | if (signed(glyph) == nglyphs-1) |
||
1915 | break; |
||
1916 | if (crc > 8) |
||
1917 | { |
||
1918 | PutDoc("\n"); |
||
1919 | crc = 0; |
||
1920 | } |
||
1921 | } |
||
1922 | if (toUnicodeMapCounter != 0) |
||
1923 | { |
||
1924 | toUnicodeMaps.append(toUnicodeMap); |
||
1925 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1926 | } |
||
1927 | PutDoc("]\n"); |
||
1928 | PutDoc(">>"); |
||
1929 | writer.endObj(fontEncoding2); |
||
1930 | QByteArray toUnicodeMapStream = ""; |
||
1931 | toUnicodeMapStream += "/CIDInit /ProcSet findresource begin\n"; |
||
1932 | toUnicodeMapStream += "12 dict begin\n"; |
||
1933 | toUnicodeMapStream += "begincmap\n"; |
||
1934 | toUnicodeMapStream += "/CIDSystemInfo <<\n"; |
||
1935 | toUnicodeMapStream += "/Registry (Adobe)\n"; |
||
1936 | toUnicodeMapStream += "/Ordering (UCS)\n"; |
||
1937 | toUnicodeMapStream += "/Supplement 0\n"; |
||
1938 | toUnicodeMapStream += ">> def\n"; |
||
1939 | toUnicodeMapStream += "/CMapName /Adobe-Identity-UCS def\n"; |
||
1940 | toUnicodeMapStream += "/CMapType 2 def\n"; |
||
1941 | toUnicodeMapStream += "1 begincodespacerange\n"; |
||
1942 | toUnicodeMapStream += "<0000> <FFFF>\n"; |
||
1943 | toUnicodeMapStream += "endcodespacerange\n"; |
||
1944 | for (int uniC = 0; uniC < toUnicodeMaps.count(); uniC++) |
||
1945 | { |
||
1946 | toUnicodeMapStream += Pdf::toPdf(toUnicodeMapsCount[uniC]); |
||
1947 | toUnicodeMapStream += " beginbfchar\n"; |
||
1948 | toUnicodeMapStream += toUnicodeMaps[uniC]; |
||
1949 | toUnicodeMapStream += "endbfchar\n"; |
||
1950 | } |
||
1951 | toUnicodeMapStream += "endcmap\n"; |
||
1952 | toUnicodeMapStream += "CMapName currentdict /CMap defineresource pop\n"; |
||
1953 | toUnicodeMapStream += "end\n"; |
||
1954 | toUnicodeMapStream += "end\n"; |
||
1955 | PdfId fontToUnicode2 = WritePDFStream(toUnicodeMapStream); |
||
1956 | PdfId fontObject2 = writer.newObject(); |
||
1957 | writer.startObj(fontObject2); |
||
1958 | PutDoc("<<\n/Type /Font\n/Subtype "); |
||
1959 | PutDoc(subtype + "\n"); |
||
1960 | PutDoc("/Name "+Pdf::toName(fontName)+"S"+Pdf::toPdf(Fc)+"\n"); |
||
21759 | jghali | 1961 | PutDoc("/BaseFont "+ Pdf::toName(baseFont) + "\n"); |
20112 | jghali | 1962 | PutDoc("/FirstChar 0\n"); |
1963 | PutDoc("/LastChar "+Pdf::toPdf(chCount-1)+"\n"); |
||
1964 | PutDoc("/Widths "+Pdf::toPdf(fontWidths2)+" 0 R\n"); |
||
1965 | PutDoc("/Encoding "+Pdf::toPdf(fontEncoding2)+" 0 R\n"); |
||
1966 | PutDoc("/ToUnicode "+Pdf::toPdf(fontToUnicode2)+" 0 R\n"); |
||
1967 | PutDoc("/FontDescriptor "+Pdf::toPdf(fontDes)+" 0 R\n"); |
||
1968 | PutDoc(">>"); |
||
1969 | writer.endObj(fontObject2); |
||
1970 | pageData.FObjects[fontName + "S"+Pdf::toPdf(Fc)] = fontObject2; |
||
1971 | } // for(Fc) |
||
21744 | jghali | 1972 | |
1973 | return result; |
||
1974 | } |
||
1975 | |||
1976 | PdfFont PDFLibCore::PDF_EncodeFormFont(const QByteArray& fontName, ScFace& face, const QByteArray& baseFont, const QByteArray& subtype, PdfId fontDes) |
||
1977 | { |
||
1978 | PdfFont formFont; |
||
1979 | formFont.name = Pdf::toName(fontName) + "Form"; |
||
1980 | formFont.usage = Used_in_Forms; |
||
1981 | |||
20112 | jghali | 1982 | PdfId fontWidthsForm = writer.newObject(); |
1983 | writer.startObj(fontWidthsForm); |
||
1984 | PutDoc("[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "); |
||
21563 | jghali | 1985 | for (uint ww = 32; ww < 256; ++ww) |
20112 | jghali | 1986 | { |
21744 | jghali | 1987 | int unicode = Pdf::fromPDFDocEncoding(ww); |
1988 | uint glyph = face.char2CMap(unicode); |
||
1989 | if (glyph > 0) |
||
20112 | jghali | 1990 | PutDoc(Pdf::toPdf(static_cast<int>(face.glyphWidth(glyph)* 1000))+" "); |
1991 | else |
||
1992 | PutDoc("0 "); |
||
1993 | } |
||
1994 | PutDoc("]"); |
||
1995 | writer.endObj(fontWidthsForm); |
||
21744 | jghali | 1996 | |
20112 | jghali | 1997 | PdfId fontObjectForm = writer.newObject(); |
1998 | writer.startObj(fontObjectForm); |
||
1999 | PutDoc("<<\n/Type /Font\n/Subtype "); |
||
2000 | PutDoc(subtype + "\n"); |
||
21759 | jghali | 2001 | PutDoc("/Name " + formFont.name + "\n"); |
2002 | PutDoc("/BaseFont "+ Pdf::toName(baseFont) +"\n"); |
||
20112 | jghali | 2003 | PutDoc("/Encoding << \n"); |
2004 | PutDoc("/Differences [ \n"); |
||
2005 | PutDoc("24 /breve /caron /circumflex /dotaccent /hungarumlaut /ogonek /ring /tilde\n"); |
||
2006 | PutDoc("39 /quotesingle 96 /grave 128 /bullet /dagger /daggerdbl /ellipsis /emdash /endash /florin /fraction /guilsinglleft /guilsinglright\n"); |
||
2007 | PutDoc("/minus /perthousand /quotedblbase /quotedblleft /quotedblright /quoteleft /quoteright /quotesinglbase /trademark /fi /fl /Lslash /OE /Scaron\n"); |
||
21744 | jghali | 2008 | PutDoc("/Ydieresis /Zcaron /dotlessi /lslash /oe /scaron /zcaron 160 /Euro 164 /currency 166 /brokenbar 168 /dieresis /copyright /ordfeminine 172 /logicalnot\n"); |
20112 | jghali | 2009 | PutDoc("/.notdef /registered /macron /degree /plusminus /twosuperior /threesuperior /acute /mu 183 /periodcentered /cedilla /onesuperior /ordmasculine\n"); |
2010 | PutDoc("188 /onequarter /onehalf /threequarters 192 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex\n"); |
||
2011 | PutDoc("/Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash\n"); |
||
2012 | PutDoc("/Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla\n"); |
||
2013 | PutDoc("/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis\n"); |
||
2014 | PutDoc("/divide /oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis\n"); |
||
2015 | PutDoc("] >>\n"); |
||
2016 | PutDoc("/FirstChar 0\n"); |
||
2017 | PutDoc("/LastChar 255\n"); |
||
2018 | PutDoc("/Widths "+Pdf::toPdf(fontWidthsForm)+" 0 R\n"); |
||
2019 | PutDoc("/FontDescriptor "+Pdf::toPdf(fontDes)+" 0 R\n"); |
||
2020 | PutDoc(">>"); |
||
2021 | writer.endObj(fontObjectForm); |
||
20103 | avox | 2022 | |
21744 | jghali | 2023 | pageData.FObjects[fontName + "Form"] = fontObjectForm; |
2024 | UsedFontsF.insert(face.replacementName(), formFont); |
||
2025 | |||
2026 | return formFont; |
||
20103 | avox | 2027 | } |
2028 | |||
21075 | craig | 2029 | /* |
20103 | avox | 2030 | static void dumpFont(QString name, QByteArray data) |
2031 | { |
||
20112 | jghali | 2032 | QFile file(name); |
20980 | craig | 2033 | if (file.open(QIODevice::WriteOnly)) |
2034 | { |
||
2035 | QDataStream out(&file); |
||
2036 | out.writeRawData(data.data(), data.length()); |
||
2037 | file.close(); |
||
2038 | } |
||
20103 | avox | 2039 | } |
2040 | |||
2041 | static void dumpCFF(QString name, cff::CFF font) |
||
2042 | { |
||
20112 | jghali | 2043 | QFile file(name); |
20980 | craig | 2044 | if (file.open(QIODevice::WriteOnly)) |
2045 | { |
||
2046 | QDataStream out(&file); |
||
2047 | font.dump(out); |
||
2048 | file.close(); |
||
2049 | } |
||
20103 | avox | 2050 | } |
21075 | craig | 2051 | */ |
20103 | avox | 2052 | |
2053 | |||
2054 | PdfFont PDFLibCore::PDF_WriteTtfSubsetFont(const QByteArray& fontName, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
2055 | { |
||
20112 | jghali | 2056 | QByteArray font; |
2057 | face.RawData(font); |
||
20974 | jghali | 2058 | /*dumpFont(face.psName() + ".ttf", font);*/ |
20112 | jghali | 2059 | QList<ScFace::gid_type> glyphs = RealGlyphs.uniqueKeys(); |
2060 | glyphs.removeAll(0); |
||
2061 | glyphs.prepend(0); |
||
2062 | QByteArray subset = sfnt::subsetFace(font, glyphs); |
||
20974 | jghali | 2063 | /*dumpFont(face.psName()+"subs.ttf", subset);*/ |
21775 | jghali | 2064 | QByteArray baseFont = sanitizeFontName(face.psName()); |
2065 | QByteArray subsetTag = PDF_GenerateSubsetTag(baseFont, glyphs); |
||
2066 | QByteArray subsetName = subsetTag + '+' + baseFont; |
||
20112 | jghali | 2067 | PdfId embeddedFontObj = PDF_EmbedFontObject(subset, QByteArray()); |
21775 | jghali | 2068 | PdfId fontDes = PDF_WriteFontDescriptor(subsetName, face, face.format(), embeddedFontObj); |
2069 | |||
20112 | jghali | 2070 | ScFace::FaceEncoding fullEncoding, subEncoding; |
2071 | QMap<uint,uint> glyphmap; |
||
2072 | face.glyphNames(fullEncoding); |
||
2073 | for (int i = 0; i < glyphs.length(); ++i) |
||
2074 | { |
||
2075 | glyphmap[glyphs[i]] = i; |
||
2076 | qDebug() << glyphs[i] << " --> " << i << QChar(fullEncoding[glyphs[i]].first); |
||
2077 | } |
||
2078 | |||
21775 | jghali | 2079 | PdfFont result = PDF_EncodeCidFont(fontName, face, subsetName, fontDes, fullEncoding, glyphmap); |
20112 | jghali | 2080 | return result; |
20103 | avox | 2081 | } |
2082 | |||
2083 | |||
2084 | PdfFont PDFLibCore::PDF_WriteCffSubsetFont(const QByteArray& fontName, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
2085 | { |
||
20112 | jghali | 2086 | // QByteArray sfnt; //TEST |
2087 | // face.RawData(sfnt); |
||
2088 | // QByteArray cff = sfnt::getTable(sfnt, "CFF "); |
||
2089 | // dumpFont(fontName, cff); |
||
2090 | // cff::CFF cfffont(cff); |
||
2091 | // cfffont.dump(); |
||
2092 | // QByteArray subsetfont = cff::subsetFace(cff, it.value().keys()); |
||
2093 | // dumpFont(it.key() + "subs.cff", subsetfont); |
||
2094 | // cff::CFF subset(subsetfont); |
||
2095 | // subset.dump(); |
||
2096 | // PDF_WriteFontDescriptor(fontName, face, fformat, 0); |
||
2097 | // // END |
||
2098 | |||
20136 | avox | 2099 | QByteArray font, data; |
2100 | face.RawData(data); |
||
2101 | font = sfnt::getTable(data, "CFF "); |
||
20974 | jghali | 2102 | /*dumpFont(face.psName() + ".cff", font);*/ |
20112 | jghali | 2103 | QList<ScFace::gid_type> glyphs = RealGlyphs.uniqueKeys(); |
2104 | glyphs.removeAll(0); |
||
2105 | glyphs.prepend(0); |
||
2106 | QByteArray subset = cff::subsetFace(font, glyphs); |
||
20974 | jghali | 2107 | /*dumpFont(face.psName()+"subs.cff", subset);*/ |
21775 | jghali | 2108 | QByteArray baseFont = sanitizeFontName(face.psName()); |
2109 | QByteArray subsetTag = PDF_GenerateSubsetTag(baseFont, glyphs); |
||
2110 | QByteArray subsetName = subsetTag + '+' + baseFont; |
||
20112 | jghali | 2111 | PdfId embeddedFontObj = PDF_EmbedFontObject(subset, "/CIDFontType0C"); |
21775 | jghali | 2112 | PdfId fontDes = PDF_WriteFontDescriptor(subsetName, face, face.format(), embeddedFontObj); |
2113 | |||
20112 | jghali | 2114 | ScFace::FaceEncoding fullEncoding, subEncoding; |
2115 | QMap<uint,uint> glyphmap; |
||
2116 | face.glyphNames(fullEncoding); |
||
2117 | for (int i = 0; i < glyphs.length(); ++i) |
||
2118 | { |
||
2119 | glyphmap[glyphs[i]] = i; |
||
2120 | qDebug() << glyphs[i] << " --> " << i << QChar(fullEncoding[glyphs[i]].first); |
||
2121 | } |
||
2122 | |||
21775 | jghali | 2123 | PdfFont result = PDF_EncodeCidFont(fontName, face, subsetName, fontDes, fullEncoding, glyphmap); |
20112 | jghali | 2124 | return result; |
20103 | avox | 2125 | } |
2126 | |||
2127 | |||
2128 | PdfId PDFLibCore::PDF_EmbedType1AsciiFontObject(const QByteArray& fon) |
||
2129 | { |
||
20112 | jghali | 2130 | QByteArray fon2; |
2131 | PdfId embeddedFontObject = writer.newObject(); |
||
2132 | writer.startObj(embeddedFontObject); |
||
2133 | int len1 = fon.indexOf("eexec")+5; |
||
2134 | fon2 = fon.left(len1) + "\n"; |
||
2135 | int len2 = fon.indexOf("0000000000000000000000000"); |
||
2136 | if (len2 == -1) |
||
2137 | len2 = fon.length()+1; |
||
2138 | // QString tm; |
||
2139 | // uint value; |
||
2140 | // bool ok = true; |
||
2141 | // int count = 0; |
||
2142 | // for (int xx = len1; xx < len2-1; ++xx) |
||
2143 | // { |
||
2144 | // tm = fon.at(xx); |
||
2145 | // if ((tm == QChar(13)) || (tm == QChar(10))) |
||
2146 | // continue; |
||
2147 | // xx++; |
||
2148 | // count++; |
||
2149 | // tm += fon.at(xx); |
||
2150 | // value = tm.toUInt(&ok, 16); |
||
2151 | // fon2 += char(value); |
||
2152 | // } |
||
2153 | QByteArray hexData = QByteArray::fromHex(fon.mid(len1, len2-len1)); |
||
2154 | fon2 += hexData; |
||
2155 | fon2 += fon.mid(len2); |
||
2156 | if (Options.Compress) |
||
2157 | fon2 = CompressArray(fon2); |
||
2158 | PutDoc("<<\n/Length "+Pdf::toPdf(fon2.length()+1)+"\n"); |
||
2159 | PutDoc("/Length1 "+Pdf::toPdf(len1+1)+"\n"); |
||
2160 | PutDoc("/Length2 "+Pdf::toPdf(hexData.length())+"\n"); |
||
2161 | if(static_cast<int>(fon.length()-len2) == -1) |
||
2162 | PutDoc("/Length3 0\n"); |
||
2163 | else |
||
2164 | PutDoc("/Length3 "+Pdf::toPdf( |