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