Rev 21951 | Rev 22052 | 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; |
||
21942 | craig | 119 | // PageItem* m_item; |
21107 | craig | 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(), |
||
21942 | craig | 134 | // m_item(ite), |
21107 | craig | 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()) |
21894 | jghali | 173 | m_pathBuffer += pdfFont.name + "_gl" + Pdf::toPdf(gl.glyph) + " Do\n"; |
21563 | jghali | 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()) |
||
21894 | jghali | 274 | m_pathBuffer += pdfFont.name + "_gl" + Pdf::toPdf(gl.glyph) + " Do\n"; |
21563 | jghali | 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 | { |
810 | if (in.length() < 1) |
||
811 | return "<>"; |
||
812 | if (!Options.Encrypt) |
||
813 | { |
||
20103 | avox | 814 | //tmp = "(" + PDFEncode(in) + ")"; |
20112 | jghali | 815 | return Pdf::toLiteralString(in); |
19114 | avox | 816 | } |
20112 | jghali | 817 | |
20103 | avox | 818 | // rc4_context_t rc4; |
819 | // QByteArray us(in.length(), ' '); |
||
820 | // QByteArray ou(in.length(), ' '); |
||
821 | // for (int a = 0; a < in.length(); ++a) |
||
822 | // us[a] = static_cast<uchar>(QChar(in.at(a)).cell()); |
||
823 | // QByteArray step1 = writer.ComputeRC4Key(ObjNum); |
||
824 | // rc4_init(&rc4, reinterpret_cast<uchar*>(step1.data()), qMin(KeyLen+5, 16)); |
||
825 | // rc4_encrypt(&rc4, reinterpret_cast<uchar*>(us.data()), reinterpret_cast<uchar*>(ou.data()), in.length()); |
||
826 | // QString uk = ""; |
||
827 | // for (int cl = 0; cl < in.length(); ++cl) |
||
828 | // uk += QChar(ou[cl]); |
||
20112 | jghali | 829 | |
830 | return Pdf::toHexString(writer.encryptBytes(in, ObjNum)); |
||
19114 | avox | 831 | } |
832 | |||
20103 | avox | 833 | QByteArray PDFLibCore::EncStringUTF16(const QString & in, PdfId ObjNum) |
19114 | avox | 834 | { |
835 | if (in.length() < 1) |
||
836 | return "<>"; |
||
837 | if (!Options.Encrypt) |
||
838 | { |
||
20112 | jghali | 839 | QByteArray us = Pdf::toUTF16(in); |
20103 | avox | 840 | // QString uk = ""; |
841 | // for (int cl = 0; cl < us.size(); ++cl) |
||
842 | // uk += QChar(us[cl]); |
||
843 | return Pdf::toHexString(us); |
||
19114 | avox | 844 | } |
20103 | avox | 845 | // rc4_context_t rc4; |
20112 | jghali | 846 | QByteArray us = Pdf::toUTF16(in); |
20103 | avox | 847 | // QByteArray ou(us.size(), ' '); |
848 | // QByteArray step1 = writer.ComputeRC4Key(ObjNum); |
||
849 | // rc4_init(&rc4, reinterpret_cast<uchar*>(step1.data()), qMin(KeyLen+5, 16)); |
||
850 | // rc4_encrypt(&rc4, reinterpret_cast<uchar*>(us.data()), reinterpret_cast<uchar*>(ou.data()), ou.size()); |
||
851 | // QString uk = ""; |
||
852 | // for (int cl = 0; cl < ou.size(); ++cl) |
||
853 | // uk += QChar(ou[cl]); |
||
854 | QByteArray tmp = Pdf::toHexString(writer.encryptBytes(us, ObjNum)); |
||
19114 | avox | 855 | return tmp; |
856 | } |
||
857 | |||
20103 | avox | 858 | bool PDFLibCore::EncodeArrayToStream(const QByteArray& in, PdfId ObjNum) |
19114 | avox | 859 | { |
860 | if (in.size() < 1) |
||
861 | return true; |
||
862 | bool succeed = false; |
||
863 | if (Options.Encrypt) |
||
864 | { |
||
20103 | avox | 865 | ScStreamFilter* rc4Encode = writer.openStreamFilter(true, ObjNum); |
866 | if (rc4Encode->openFilter()) |
||
19114 | avox | 867 | { |
20103 | avox | 868 | succeed = rc4Encode->writeData(in.data(), in.size()); |
869 | succeed &= rc4Encode->closeFilter(); |
||
19114 | avox | 870 | } |
20112 | jghali | 871 | delete rc4Encode; |
19114 | avox | 872 | } |
873 | else |
||
20103 | avox | 874 | writer.write(in); |
875 | return (writer.getOutStream().status() == QDataStream::Ok); |
||
19114 | avox | 876 | } |
877 | |||
20103 | avox | 878 | int PDFLibCore::WriteImageToStream(ScImage& image, PdfId ObjNum, ColorSpaceEnum format, bool precal) |
11599 | jghali | 879 | { |
17758 | jghali | 880 | bool fromCmyk, succeed = false; |
11599 | jghali | 881 | int bytesWritten = 0; |
20103 | avox | 882 | |
20112 | jghali | 883 | ScStreamFilter* rc4Encode = writer.openStreamFilter(Options.Encrypt, ObjNum); |
884 | if (rc4Encode->openFilter()) |
||
885 | { |
||
886 | switch (format) |
||
887 | { |
||
888 | case ColorSpaceMonochrome : |
||
889 | fromCmyk = !Options.UseRGB && !Options.isGrayscale && !(doc.HasCMS && Options.UseProfiles2); |
||
890 | succeed = image.writeMonochromeDataToFilter(rc4Encode, fromCmyk); break; |
||
891 | case ColorSpaceGray : |
||
892 | succeed = image.writeGrayDataToFilter(rc4Encode, precal); break; |
||
893 | case ColorSpaceCMYK : |
||
894 | succeed = image.writeCMYKDataToFilter(rc4Encode); break; |
||
895 | default : |
||
896 | succeed = image.writeRGBDataToFilter(rc4Encode); break; |
||
897 | } |
||
898 | succeed &= rc4Encode->closeFilter(); |
||
899 | bytesWritten = rc4Encode->writtenToStream(); |
||
900 | delete rc4Encode; |
||
901 | } |
||
11599 | jghali | 902 | return (succeed ? bytesWritten : 0); |
903 | } |
||
904 | |||
20103 | avox | 905 | int PDFLibCore::WriteJPEGImageToStream(ScImage& image, const QString& fn, PdfId ObjNum, int quality, ColorSpaceEnum format, |
17758 | jghali | 906 | bool sameFile, bool precal) |
11599 | jghali | 907 | { |
908 | bool succeed = true; |
||
909 | int bytesWritten = 0; |
||
910 | QFileInfo fInfo(fn); |
||
911 | QString ext = fInfo.suffix().toLower(); |
||
912 | QString jpgFileName, tmpFile; |
||
913 | if (extensionIndicatesJPEG(ext) && sameFile) |
||
914 | jpgFileName = fn; |
||
11445 | jghali | 915 | else |
11599 | jghali | 916 | { |
21526 | craig | 917 | tmpFile = QDir::toNativeSeparators(ScPaths::tempFileDir() + "sc.jpg"); |
17758 | jghali | 918 | if (format == ColorSpaceGray && (!precal)) |
11599 | jghali | 919 | image.convertToGray(); |
17758 | jghali | 920 | if (image.convert2JPG(tmpFile, quality, format == ColorSpaceCMYK, format == ColorSpaceGray)) |
11599 | jghali | 921 | jpgFileName = tmpFile; |
922 | } |
||
923 | if (jpgFileName.isEmpty()) |
||
11792 | fschmid | 924 | return 0; |
11599 | jghali | 925 | if (Options.Encrypt) |
926 | { |
||
927 | succeed = false; |
||
20112 | jghali | 928 | ScStreamFilter* rc4Encode = writer.openStreamFilter(true, ObjNum); |
929 | if (rc4Encode->openFilter()) |
||
11599 | jghali | 930 | { |
20103 | avox | 931 | succeed = copyFileToFilter(jpgFileName, *rc4Encode); |
932 | succeed &= rc4Encode->closeFilter(); |
||
933 | bytesWritten = rc4Encode->writtenToStream(); |
||
11599 | jghali | 934 | } |
20112 | jghali | 935 | delete rc4Encode; |
11599 | jghali | 936 | } |
937 | else |
||
938 | { |
||
20103 | avox | 939 | succeed &= copyFileToStream(jpgFileName, writer.getOutStream()); |
11599 | jghali | 940 | QFileInfo jpgInfo(jpgFileName); |
941 | bytesWritten = jpgInfo.size(); |
||
942 | } |
||
943 | if (!tmpFile.isEmpty() && QFile::exists(tmpFile)) |
||
944 | QFile::remove(tmpFile); |
||
945 | return (succeed ? bytesWritten : 0); |
||
11445 | jghali | 946 | } |
947 | |||
20103 | avox | 948 | int PDFLibCore::WriteFlateImageToStream(ScImage& image, PdfId ObjNum, ColorSpaceEnum format, bool precal) |
11599 | jghali | 949 | { |
20112 | jghali | 950 | bool fromCmyk, succeed = false; |
951 | int bytesWritten = 0; |
||
952 | ScStreamFilter* rc4Encode = writer.openStreamFilter(Options.Encrypt, ObjNum); |
||
953 | ScFlateEncodeFilter flateEncode(rc4Encode); |
||
954 | if (flateEncode.openFilter()) |
||
955 | { |
||
956 | switch (format) |
||
957 | { |
||
958 | case ColorSpaceMonochrome : |
||
959 | fromCmyk = !Options.UseRGB && !Options.isGrayscale && !(doc.HasCMS && Options.UseProfiles2); |
||
960 | succeed = image.writeMonochromeDataToFilter(&flateEncode, fromCmyk); break; |
||
961 | case ColorSpaceGray : |
||
962 | succeed = image.writeGrayDataToFilter(&flateEncode, precal); break; |
||
963 | case ColorSpaceCMYK : |
||
964 | succeed = image.writeCMYKDataToFilter(&flateEncode); break; |
||
965 | default : |
||
966 | succeed = image.writeRGBDataToFilter(&flateEncode); break; |
||
967 | } |
||
968 | succeed &= flateEncode.closeFilter(); |
||
969 | bytesWritten = flateEncode.writtenToStream(); |
||
970 | } |
||
971 | delete rc4Encode; |
||
972 | return (succeed ? bytesWritten : 0); |
||
11599 | jghali | 973 | } |
974 | |||
3133 | fschmid | 975 | |
19114 | avox | 976 | |
20103 | avox | 977 | bool PDFLibCore::PDF_Begin_Doc(const QString& fn, SCFonts &AllFonts, const QMap<QString, QMap<uint, FPointArray> >& DocFonts, BookMView* vi) |
19114 | avox | 978 | { |
20112 | jghali | 979 | if (!writer.open(fn)) |
980 | return false; |
||
981 | |||
6407 | fschmid | 982 | inPattern = 0; |
3133 | fschmid | 983 | Bvie = vi; |
984 | BookMinUse = false; |
||
985 | UsedFontsP.clear(); |
||
8454 | fschmid | 986 | UsedFontsF.clear(); |
20112 | jghali | 987 | |
988 | writer.writeHeader(Options.Version); |
||
989 | |||
20103 | avox | 990 | // if (((Options.Version == PDFOptions::PDFVersion_15) || (Options.Version == PDFOptions::PDFVersion_X4)) && (Options.useLayers)) |
991 | // ObjCounter = 10; |
||
992 | // else |
||
993 | // ObjCounter = 9; |
||
994 | // switch (Options.Version) |
||
995 | // { |
||
996 | // case PDFOptions::PDFVersion_X1a: |
||
997 | // case PDFOptions::PDFVersion_X3: |
||
998 | // case PDFOptions::PDFVersion_13: |
||
999 | // PutDoc("%PDF-1.3\n"); |
||
1000 | // break; |
||
1001 | // case PDFOptions::PDFVersion_14: |
||
1002 | // PutDoc("%PDF-1.4\n"); |
||
1003 | // break; |
||
1004 | // case PDFOptions::PDFVersion_X4: |
||
1005 | // case PDFOptions::PDFVersion_15: |
||
1006 | // PutDoc("%PDF-1.5\n"); |
||
1007 | // break; |
||
1008 | // } |
||
1009 | // if (PDF_IsPDFX()) |
||
1010 | // ObjCounter++; |
||
1011 | // PutDoc("%\xc7\xec\x8f\xa2\n"); |
||
1012 | |||
20112 | jghali | 1013 | PDF_Begin_Catalog(); |
1014 | PDF_Begin_MetadataAndEncrypt(); |
||
1015 | PDF_Begin_WriteUsedFonts(AllFonts, PDF_Begin_FindUsedFonts(AllFonts, DocFonts)); |
||
1016 | PDF_Begin_Colors(); |
||
1017 | PDF_Begin_Layers(); |
||
1018 | |||
1019 | return true; |
||
20103 | avox | 1020 | } |
1021 | |||
1022 | void PDFLibCore::PDF_Begin_Catalog() |
||
1023 | { |
||
20112 | jghali | 1024 | writer.startObj(writer.CatalogObj); |
1025 | PutDoc("<<\n/Type /Catalog"); |
||
1026 | PutDoc("\n/Outlines " + Pdf::toObjRef(writer.OutlinesObj) + |
||
1027 | "\n/Pages " + Pdf::toObjRef(writer.PagesObj) + |
||
1028 | "\n/Dests " + Pdf::toObjRef(writer.DestsObj) + |
||
1029 | "\n/AcroForm " + Pdf::toObjRef(writer.AcroFormObj) + |
||
1030 | "\n/Names "+ Pdf::toObjRef(writer.NamesObj) + |
||
1031 | "\n/Threads " + Pdf::toObjRef(writer.ThreadsObj) + |
||
1032 | "\n"); |
||
19114 | avox | 1033 | if (((Options.Version == PDFOptions::PDFVersion_15) || (Options.Version == PDFOptions::PDFVersion_X4)) && (Options.useLayers)) |
20112 | jghali | 1034 | { |
1035 | writer.OCPropertiesObj = writer.newObject(); |
||
20103 | avox | 1036 | PutDoc("/OCProperties " + Pdf::toObjRef(writer.OCPropertiesObj) + "\n"); |
20112 | jghali | 1037 | } |
1038 | if (PDF_IsPDFX()) |
||
19114 | avox | 1039 | { |
20112 | jghali | 1040 | writer.OutputIntentObj = writer.newObject(); |
1041 | PutDoc("/OutputIntents [ " + Pdf::toObjRef(writer.OutputIntentObj) + " ]\n"); |
||
1042 | } |
||
1043 | if ((Options.Version == PDFOptions::PDFVersion_X4)) |
||
1044 | { |
||
1045 | writer.MetaDataObj = writer.newObject(); |
||
20103 | avox | 1046 | PutDoc("/Metadata "+ Pdf::toObjRef(writer.MetaDataObj) + "\n"); |
19114 | avox | 1047 | } |
1048 | PutDoc("/PageLayout "); |
||
1049 | switch (Options.PageLayout) |
||
1050 | { |
||
1051 | case PDFOptions::SinglePage: |
||
1052 | PutDoc("/SinglePage\n"); |
||
1053 | break; |
||
1054 | case PDFOptions::OneColumn: |
||
1055 | PutDoc("/OneColumn\n"); |
||
1056 | break; |
||
1057 | case PDFOptions::TwoColumnLeft: |
||
1058 | PutDoc("/TwoColumnLeft\n"); |
||
1059 | break; |
||
1060 | case PDFOptions::TwoColumnRight: |
||
1061 | PutDoc("/TwoColumnRight\n"); |
||
1062 | break; |
||
1063 | } |
||
1064 | if (Options.displayBookmarks) |
||
1065 | PutDoc("/PageMode /UseOutlines\n"); |
||
1066 | else if (Options.displayFullscreen) |
||
1067 | PutDoc("/PageMode /FullScreen\n"); |
||
1068 | else if (Options.displayThumbs) |
||
1069 | PutDoc("/PageMode /UseThumbs\n"); |
||
1070 | else if ((Options.Version == PDFOptions::PDFVersion_15) && (Options.displayLayers)) |
||
1071 | PutDoc("/PageMode /UseOC\n"); |
||
1072 | if (!Options.openAction.isEmpty()) |
||
1073 | { |
||
20112 | jghali | 1074 | PutDoc("/OpenAction << /S /JavaScript /JS (this."+Pdf::toPdfDocEncoding(Options.openAction)+"\\(\\)) >>\n"); |
19114 | avox | 1075 | } |
21942 | craig | 1076 | QDateTime dt = QDateTime::currentDateTimeUtc(); |
20112 | jghali | 1077 | Datum = Pdf::toDateString(dt); |
20103 | avox | 1078 | // "D:"; |
1079 | // tmp.sprintf("%4d", d.year()); |
||
1080 | // tmp.replace(QRegExp(" "), "0"); |
||
1081 | // Datum += tmp; |
||
1082 | // tmp.sprintf("%2d", d.month()); |
||
1083 | // tmp.replace(QRegExp(" "), "0"); |
||
1084 | // Datum += tmp; |
||
1085 | // tmp.sprintf("%2d", d.day()); |
||
1086 | // tmp.replace(QRegExp(" "), "0"); |
||
1087 | // Datum += tmp; |
||
1088 | // tmp = dt.time().toString(); |
||
1089 | // tmp.replace(QRegExp(":"), ""); |
||
1090 | // Datum += tmp; |
||
1091 | // Datum += "Z"; |
||
12429 | fschmid | 1092 | |
19114 | avox | 1093 | // only include XMP to PDF/X-4 at the moment, could easily be extended to include it to any PDF |
1094 | if (Options.Version == PDFOptions::PDFVersion_X4) |
||
1095 | generateXMP(dt.toString("yyyy-MM-ddThh:mm:ssZ")); |
||
1096 | |||
1097 | /* The following code makes the resulting PDF "Reader enabled" in Acrobat Reader 8 |
||
1098 | but sadly it doesn't work with newer version, because its based on a bug in AR 8 |
||
1099 | PutDoc("/Perms\n"); |
||
1100 | PutDoc("<<\n"); |
||
1101 | PutDoc("/UR3\n"); |
||
1102 | PutDoc("<<\n"); |
||
1103 | PutDoc("/M ("+Datum+")\n"); |
||
1104 | PutDoc("/Name (Scribus "+QString(VERSION)+")\n"); |
||
1105 | PutDoc("/Reference [\n"); |
||
1106 | PutDoc("<<\n"); |
||
1107 | PutDoc("/TransformParams\n"); |
||
1108 | PutDoc("<<\n"); |
||
1109 | PutDoc("/Type /TransformParams\n"); |
||
1110 | PutDoc("/V /2.2\n"); |
||
1111 | PutDoc("/Document [/FullSave]\n"); |
||
1112 | PutDoc("/Annots [/Create/Delete/Modify/Copy/Import/Export]\n"); |
||
1113 | PutDoc("/Form [/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate]\n"); |
||
1114 | PutDoc("/Signature [/Modify]\n"); |
||
1115 | PutDoc(">>\n"); |
||
1116 | PutDoc("/TransformMethod /UR3\n"); |
||
1117 | PutDoc("/Type /SigRef\n"); |
||
1118 | PutDoc(">>\n"); |
||
1119 | PutDoc("]\n"); |
||
1120 | PutDoc("/Type /Sig\n"); |
||
1121 | PutDoc(">>\n"); |
||
1122 | PutDoc(">>\n"); |
||
1123 | */ |
||
1124 | |||
1125 | PutDoc("/ViewerPreferences\n<<\n/PageDirection "); |
||
1126 | PutDoc( Options.Binding == 0 ? "/L2R\n" : "/R2L\n"); |
||
1127 | if (Options.hideToolBar) |
||
1128 | PutDoc("/HideToolbar true\n"); |
||
1129 | if (Options.hideMenuBar) |
||
1130 | PutDoc("/HideMenubar true\n"); |
||
1131 | if (Options.fitWindow) |
||
1132 | PutDoc("/FitWindow true\n"); |
||
20112 | jghali | 1133 | PutDoc(" >>\n>>"); |
1134 | writer.endObj(writer.CatalogObj); |
||
20103 | avox | 1135 | } |
1136 | |||
1137 | void PDFLibCore::PDF_Begin_MetadataAndEncrypt() |
||
1138 | { |
||
19114 | avox | 1139 | QString IDg(Datum); |
1140 | IDg += Options.fileName; |
||
1141 | IDg += "Scribus "+QString(VERSION); |
||
1142 | IDg += "Scribus PDF Library "+QString(VERSION); |
||
1143 | IDg += doc.documentInfo().title(); |
||
1144 | IDg += doc.documentInfo().author(); |
||
1145 | IDg += "/False"; |
||
20112 | jghali | 1146 | writer.setFileId(Pdf::toPdfDocEncoding(IDg)); |
19114 | avox | 1147 | if (Options.Encrypt) |
1148 | { |
||
20112 | jghali | 1149 | writer.setEncryption((Options.Version == PDFOptions::PDFVersion_14) || (Options.Version == PDFOptions::PDFVersion_15), Pdf::toPdfDocEncoding(Options.PassOwner), Pdf::toPdfDocEncoding(Options.PassUser), Options.Permissions); |
19114 | avox | 1150 | } |
20103 | avox | 1151 | writer.startObj(writer.InfoObj); |
20112 | jghali | 1152 | PutDoc("<<\n/Creator " + EncString(QByteArray("Scribus ") + VERSION, writer.InfoObj) + "\n"); |
20103 | avox | 1153 | PutDoc("/Producer " + EncString(QByteArray("Scribus PDF Library ") + VERSION, writer.InfoObj) + "\n"); |
19114 | avox | 1154 | QString docTitle = doc.documentInfo().title(); |
19115 | avox | 1155 | if ((PDF_IsPDFX()) && (docTitle.isEmpty())) |
20103 | avox | 1156 | PutDoc("/Title " + EncStringUTF16(doc.DocName, writer.InfoObj) + "\n"); |
19114 | avox | 1157 | else |
20103 | avox | 1158 | PutDoc("/Title " + EncStringUTF16(doc.documentInfo().title(), writer.InfoObj) + "\n"); |
1159 | PutDoc("/Author " + EncStringUTF16(doc.documentInfo().author(), writer.InfoObj) + "\n"); |
||
1160 | PutDoc("/Subject " + EncStringUTF16(doc.documentInfo().subject(), writer.InfoObj) + "\n"); |
||
1161 | PutDoc("/Keywords " + EncStringUTF16(doc.documentInfo().keywords(), writer.InfoObj) + "\n"); |
||
1162 | PutDoc("/CreationDate " + EncString(Datum, writer.InfoObj) + "\n"); |
||
1163 | PutDoc("/ModDate " + EncString(Datum, writer.InfoObj) + "\n"); |
||
19114 | avox | 1164 | if (Options.Version == PDFOptions::PDFVersion_X1a) |
1165 | { |
||
1166 | PutDoc("/GTS_PDFXVersion (PDF/X-1:2001)\n"); |
||
1167 | PutDoc("/GTS_PDFXConformance (PDF/X-1a:2001)\n"); |
||
1168 | } |
||
19115 | avox | 1169 | if (Options.Version == PDFOptions::PDFVersion_X3) |
1170 | PutDoc("/GTS_PDFXVersion (PDF/X-3:2002)\n"); |
||
19114 | avox | 1171 | if (Options.Version == PDFOptions::PDFVersion_X4) |
1172 | PutDoc("/GTS_PDFXVersion (PDF/X-4)\n"); |
||
20112 | jghali | 1173 | PutDoc("/Trapped /False\n>>"); |
1174 | writer.endObj(writer.InfoObj); |
||
20103 | avox | 1175 | |
20112 | jghali | 1176 | // Encrypt |
1177 | |||
1178 | // for (int t = 0; t < 6; ++t) |
||
20103 | avox | 1179 | // XRef.append(bytesWritten()); |
1180 | // if (((Options.Version == PDFOptions::PDFVersion_15) || (Options.Version == PDFOptions::PDFVersion_X4)) && (Options.useLayers)) |
||
1181 | // XRef.append(bytesWritten()); |
||
1182 | // if (PDF_IsPDFX()) |
||
1183 | // XRef.append(bytesWritten()); |
||
1184 | // if (Options.Version == PDFOptions::PDFVersion_X4) |
||
1185 | // XRef.append(bytesWritten()); |
||
19114 | avox | 1186 | if (Options.Encrypt) |
20103 | avox | 1187 | { // now done in writer.setEncrption(): |
1188 | // writer.EncryptObj = writer.newObject(); |
||
1189 | // writer.startObj(writer.EncryptObj); |
||
1190 | // PutDoc("<<\n/Filter /Standard\n"); |
||
1191 | // PutDoc( KeyLen > 5 ? "/R 3\n/V 2\n/Length 128\n" : "/R 2\n/V 1\n"); |
||
1192 | // PutDoc("/O "+Pdf::toHexString(ok)+"\n"); |
||
1193 | // PutDoc("/U "+Pdf::toHexString(uk)+"\n"); |
||
20112 | jghali | 1194 | // PutDoc("/P "+Pdf::toPdf(Options.Permissions)+"\n>>"); |
1195 | // writer.endObj(writer.EncryptObj); |
||
19114 | avox | 1196 | } |
20103 | avox | 1197 | } |
1198 | |||
1199 | QMap<QString, QMap<uint, FPointArray> > |
||
1200 | PDFLibCore::PDF_Begin_FindUsedFonts(SCFonts &AllFonts, const QMap<QString, QMap<uint, FPointArray> >& DocFonts) |
||
1201 | { |
||
5387 | avox | 1202 | QMap<QString, QMap<uint, FPointArray> > ReallyUsed; |
3133 | fschmid | 1203 | ReallyUsed.clear(); |
1204 | PageItem* pgit; |
||
20103 | avox | 1205 | QMap<int, QByteArray> ind2PDFabr; |
1206 | const QByteArray tmpf[] = {"/Courier", "/Courier-Bold", "/Courier-Oblique", "/Courier-BoldOblique", |
||
4017 | fschmid | 1207 | "/Helvetica", "/Helvetica-Bold", "/Helvetica-Oblique", "/Helvetica-BoldOblique", |
4546 | subik | 1208 | "/Times-Roman", "/Times-Bold", "/Times-Italic", "/Times-BoldItalic", |
4017 | fschmid | 1209 | "/ZapfDingbats", "/Symbol"}; |
1210 | size_t ar = sizeof(tmpf) / sizeof(*tmpf); |
||
1211 | for (uint ax = 0; ax < ar; ++ax) |
||
1212 | ind2PDFabr[ax] = tmpf[ax]; |
||
21709 | jghali | 1213 | |
1214 | QList<PageItem*> allItems = doc.FrameItems.values(); |
||
1215 | while (allItems.count() > 0) |
||
3133 | fschmid | 1216 | { |
21709 | jghali | 1217 | pgit = allItems.takeFirst(); |
1218 | if (pgit->isGroup() || pgit->isTable()) |
||
3133 | fschmid | 1219 | { |
21709 | jghali | 1220 | allItems = pgit->getItemList() + allItems; |
1221 | continue; |
||
1222 | } |
||
1223 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
||
1224 | { |
||
1225 | if (pgit->isAnnotation()) |
||
17312 | fschmid | 1226 | { |
21709 | jghali | 1227 | int annotType = pgit->annotation().Type(); |
1228 | bool mustEmbed = ((annotType >= Annotation::Button) && (annotType <= Annotation::Listbox) && (annotType != Annotation::Checkbox)); |
||
1229 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
||
1230 | StdFonts.insert("/ZapfDingbats", ""); |
||
1231 | if (pgit->itemText.length() > 0 || mustEmbed) |
||
17312 | fschmid | 1232 | { |
21709 | jghali | 1233 | if (Options.Version < PDFOptions::PDFVersion_14) |
1234 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
21744 | jghali | 1235 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
1236 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
17312 | fschmid | 1237 | } |
1238 | } |
||
21709 | jghali | 1239 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1240 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1241 | for (uint e = start; e < stop; ++e) |
||
8092 | fschmid | 1242 | { |
21709 | jghali | 1243 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
1244 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
8092 | fschmid | 1245 | } |
3133 | fschmid | 1246 | } |
1247 | } |
||
21709 | jghali | 1248 | |
1249 | allItems = doc.MasterItems; |
||
1250 | while (allItems.count() > 0) |
||
3133 | fschmid | 1251 | { |
21709 | jghali | 1252 | pgit = allItems.takeFirst(); |
1253 | if (pgit->isGroup() || pgit->isTable()) |
||
3133 | fschmid | 1254 | { |
21709 | jghali | 1255 | allItems = pgit->getItemList() + allItems; |
1256 | continue; |
||
1257 | } |
||
1258 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
||
1259 | { |
||
1260 | if (pgit->isAnnotation()) |
||
17312 | fschmid | 1261 | { |
21709 | jghali | 1262 | int annotType = pgit->annotation().Type(); |
1263 | bool mustEmbed = ((annotType >= Annotation::Button) && (annotType <= Annotation::Listbox) && (annotType != Annotation::Checkbox)); |
||
1264 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
||
1265 | StdFonts.insert("/ZapfDingbats", ""); |
||
1266 | if (pgit->itemText.length() > 0 || mustEmbed) |
||
17312 | fschmid | 1267 | { |
21709 | jghali | 1268 | if (Options.Version < PDFOptions::PDFVersion_14) |
1269 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
1270 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
||
1271 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
17312 | fschmid | 1272 | } |
1273 | } |
||
21709 | jghali | 1274 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1275 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1276 | for (uint e = start; e < stop; ++e) |
||
8092 | fschmid | 1277 | { |
21709 | jghali | 1278 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
1279 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
1280 | } |
||
1281 | } |
||
1282 | } |
||
1283 | |||
1284 | allItems = *(doc.Items); |
||
1285 | while (allItems.count() > 0) |
||
1286 | { |
||
1287 | pgit = allItems.takeFirst(); |
||
1288 | if (pgit->isGroup() || pgit->isTable()) |
||
1289 | { |
||
1290 | allItems = pgit->getItemList() + allItems; |
||
1291 | continue; |
||
1292 | } |
||
1293 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
||
1294 | { |
||
1295 | if (pgit->isAnnotation()) |
||
1296 | { |
||
1297 | int annotType = pgit->annotation().Type(); |
||
1298 | bool mustEmbed = ((annotType >= Annotation::Button) && (annotType <= Annotation::Listbox) && (annotType != Annotation::Checkbox)); |
||
1299 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
||
1300 | StdFonts.insert("/ZapfDingbats", ""); |
||
1301 | if (pgit->itemText.length() > 0 || mustEmbed) |
||
12763 | fschmid | 1302 | { |
21709 | jghali | 1303 | if (Options.Version < PDFOptions::PDFVersion_14) |
1304 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
1305 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
||
21707 | jghali | 1306 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
16953 | fschmid | 1307 | } |
8092 | fschmid | 1308 | } |
21709 | jghali | 1309 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1310 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1311 | for (uint e = start; e < stop; ++e) |
||
1312 | { |
||
1313 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
||
1314 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
1315 | } |
||
3133 | fschmid | 1316 | } |
1317 | } |
||
21709 | jghali | 1318 | /* if (Options.docInfoMarks) |
3133 | fschmid | 1319 | { |
21709 | jghali | 1320 | StdFonts.insert("/Helvetica", ""); |
1321 | } */ |
||
1322 | QStringList patterns = doc.getPatternDependencyList(doc.getUsedPatterns()); |
||
1323 | for (int c = 0; c < patterns.count(); ++c) |
||
1324 | { |
||
1325 | ScPattern pa = doc.docPatterns[patterns[c]]; |
||
1326 | allItems = pa.items; |
||
1327 | while (allItems.count() > 0) |
||
3133 | fschmid | 1328 | { |
21709 | jghali | 1329 | pgit = allItems.takeFirst(); |
1330 | if (pgit->isGroup() || pgit->isTable()) |
||
17312 | fschmid | 1331 | { |
21709 | jghali | 1332 | allItems = pgit->getItemList() + allItems; |
1333 | continue; |
||
17312 | fschmid | 1334 | } |
16953 | fschmid | 1335 | if ((pgit->itemType() == PageItem::TextFrame) || (pgit->itemType() == PageItem::PathText)) |
8092 | fschmid | 1336 | { |
16953 | fschmid | 1337 | if (pgit->isAnnotation()) |
12763 | fschmid | 1338 | { |
17952 | fschmid | 1339 | if ((pgit->annotation().Type() == Annotation::Checkbox) || (pgit->annotation().Type() == Annotation::RadioButton)) |
16953 | fschmid | 1340 | StdFonts.insert("/ZapfDingbats", ""); |
21709 | jghali | 1341 | if (pgit->itemText.length() > 0) |
16953 | fschmid | 1342 | { |
1343 | if (Options.Version < PDFOptions::PDFVersion_14) |
||
1344 | StdFonts.insert(ind2PDFabr[pgit->annotation().Font()], ""); |
||
21707 | jghali | 1345 | QString replacementName = pgit->itemText.defaultStyle().charStyle().font().replacementName(); |
1346 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
16953 | fschmid | 1347 | } |
12763 | fschmid | 1348 | } |
17751 | jghali | 1349 | uint start = pgit->isTextFrame() ? (uint) pgit->firstInFrame() : 0; |
1350 | uint stop = pgit->isTextFrame() ? (uint) pgit->lastInFrame() + 1 : (uint) pgit->itemText.length(); |
||
1351 | for (uint e = start; e < stop; ++e) |
||
16953 | fschmid | 1352 | { |
21707 | jghali | 1353 | QString replacementName = pgit->itemText.charStyle(e).font().replacementName(); |
1354 | ReallyUsed.insert(replacementName, DocFonts[replacementName]); |
||
16953 | fschmid | 1355 | } |
8092 | fschmid | 1356 | } |
3133 | fschmid | 1357 | } |
1358 | } |
||
20112 | jghali | 1359 | return ReallyUsed; |
20103 | avox | 1360 | } |
17628 | jghali | 1361 | |
20103 | avox | 1362 | static QByteArray sanitizeFontName(QString fn) |
1363 | { |
||
20112 | jghali | 1364 | return Pdf::toPdfDocEncoding(fn.replace( QRegExp("[\\s\\/\\{\\[\\]\\}\\<\\>\\(\\)\\%]"), "_" )); |
20103 | avox | 1365 | } |
17628 | jghali | 1366 | |
20103 | avox | 1367 | static QList<Pdf::Resource> asColorSpace(QList<PdfICCD> iccCSlist) |
1368 | { |
||
20112 | jghali | 1369 | QList<Pdf::Resource> result; |
21951 | craig | 1370 | foreach (const Pdf::Resource& r, iccCSlist) |
20112 | jghali | 1371 | result.append(r); |
1372 | return result; |
||
20103 | avox | 1373 | } |
1374 | |||
1375 | static QList<Pdf::Resource> asColorSpace(QList<PdfSpotC> spotMapValues) |
||
1376 | { |
||
20112 | jghali | 1377 | QList<Pdf::Resource> result; |
21951 | craig | 1378 | foreach (const Pdf::Resource& r, spotMapValues) |
20112 | jghali | 1379 | result.append(r); |
1380 | return result; |
||
20103 | avox | 1381 | } |
1382 | |||
1383 | void PDFLibCore::PDF_WriteStandardFonts() |
||
1384 | { |
||
20112 | jghali | 1385 | int a = 0; |
1386 | QMap<QString, QString>::Iterator itStd; |
||
1387 | for (itStd = StdFonts.begin(); itStd != StdFonts.end(); ++itStd) |
||
1388 | { |
||
1389 | PdfId fontObject = writer.newObject(); |
||
1390 | writer.startObj(fontObject); |
||
1391 | PutDoc("<<\n/Type /Font\n/Subtype /Type1\n"); |
||
1392 | PutDoc("/Name /FoStd"+Pdf::toPdf(a)+"\n"); |
||
1393 | PutDoc("/BaseFont "+Pdf::toPdfDocEncoding(itStd.key())+"\n"); |
||
1394 | if (itStd.key() != "/ZapfDingbats") |
||
1395 | { |
||
1396 | PutDoc("/Encoding << \n"); |
||
1397 | PutDoc("/Differences [ \n"); |
||
1398 | PutDoc("24 /breve /caron /circumflex /dotaccent /hungarumlaut /ogonek /ring /tilde\n"); |
||
1399 | PutDoc("39 /quotesingle 96 /grave 128 /bullet /dagger /daggerdbl /ellipsis /emdash /endash /florin /fraction /guilsinglleft /guilsinglright\n"); |
||
1400 | PutDoc("/minus /perthousand /quotedblbase /quotedblleft /quotedblright /quoteleft /quoteright /quotesinglbase /trademark /fi /fl /Lslash /OE /Scaron\n"); |
||
21744 | jghali | 1401 | PutDoc("/Ydieresis /Zcaron /dotlessi /lslash /oe /scaron /zcaron 160 /Euro 164 /currency 166 /brokenbar 168 /dieresis /copyright /ordfeminine 172 /logicalnot\n"); |
20112 | jghali | 1402 | PutDoc("/.notdef /registered /macron /degree /plusminus /twosuperior /threesuperior /acute /mu 183 /periodcentered /cedilla /onesuperior /ordmasculine\n"); |
1403 | PutDoc("188 /onequarter /onehalf /threequarters 192 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex\n"); |
||
1404 | PutDoc("/Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash\n"); |
||
1405 | PutDoc("/Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla\n"); |
||
1406 | PutDoc("/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis\n"); |
||
1407 | PutDoc("/divide /oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis\n"); |
||
1408 | PutDoc("] >>\n"); |
||
1409 | } |
||
1410 | PutDoc(">>"); |
||
1411 | writer.endObj(fontObject); |
||
1412 | pageData.FObjects["FoStd"+Pdf::toPdf(a)] = fontObject; |
||
1413 | itStd.value() = "FoStd"+Pdf::toPdf(a); |
||
1414 | a++; |
||
1415 | } |
||
20103 | avox | 1416 | } |
1417 | |||
1418 | |||
1419 | PdfFont PDFLibCore::PDF_WriteType3Font(const QByteArray& name, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
1420 | { |
||
20112 | jghali | 1421 | PdfFont result; |
1422 | result.name = Pdf::toName(name); |
||
1423 | result.method = Use_Type3; |
||
1424 | result.encoding = Encode_256; |
||
20103 | avox | 1425 | |
22008 | jghali | 1426 | // TrueType font rasterizers use non-zero winding file rule, |
1427 | // vs even-odd for current Postscript font rasterizers |
||
1428 | // Note: OTF CFF2 will also use non-zero winding rule, so |
||
1429 | // change code below when adding CCF2 support to Scribus |
||
1430 | // ref: https://www.microsoft.com/typography/OTSpec/cff2.htm |
||
1431 | bool useNonZeroRule = (face.type() == ScFace::TTF); |
||
1432 | |||
20112 | jghali | 1433 | uint SubFonts = 0; |
1434 | int glyphCount = 0; |
||
1435 | double minx = std::numeric_limits<double>::max(); |
||
1436 | double miny = std::numeric_limits<double>::max(); |
||
1437 | double maxx = -std::numeric_limits<double>::max(); |
||
1438 | double maxy = -std::numeric_limits<double>::max(); |
||
1439 | QList<uint> glyphWidths; |
||
1440 | QList<QByteArray> charProcs; |
||
1441 | QByteArray encoding = "<< /Type /Encoding\n/Differences [ 0\n"; |
||
1442 | QByteArray fon; |
||
1443 | QMap<uint, uint> glyphMapping; |
||
1444 | ScFace::FaceEncoding gl; |
||
1445 | face.glyphNames(gl); |
||
1446 | QMap<uint,FPointArray>::ConstIterator ig; |
||
1447 | for (ig = RealGlyphs.cbegin(); ig != RealGlyphs.cend(); ++ig) |
||
1448 | { |
||
1449 | FPoint np, np1, np2; |
||
1450 | bool nPath = true; |
||
1451 | fon.resize(0); |
||
1452 | if (ig.value().size() > 3) |
||
1453 | { |
||
1454 | FPointArray gly = ig.value(); |
||
1455 | QTransform mat; |
||
1456 | mat.scale(100.0, -100.0); |
||
1457 | gly.map(mat); |
||
1458 | gly.translate(0, 1000); |
||
1459 | for (int poi = 0; poi < gly.size()-3; poi += 4) |
||
1460 | { |
||
1461 | if (gly.isMarker(poi)) |
||
1462 | { |
||
1463 | fon += "h\n"; |
||
1464 | nPath = true; |
||
1465 | continue; |
||
1466 | } |
||
1467 | if (nPath) |
||
1468 | { |
||
1469 | np = gly.point(poi); |
||
1470 | fon += FToStr(np.x())+" "+FToStr(np.y())+" m\n"; |
||
1471 | nPath = false; |
||
1472 | } |
||
1473 | np = gly.point(poi+1); |
||
1474 | np1 = gly.point(poi+3); |
||
1475 | np2 = gly.point(poi+2); |
||
1476 | fon += FToStr(np.x()) + " " + FToStr(np.y()) + " " + FToStr(np1.x()) + " " + FToStr(np1.y()) + " " + FToStr(np2.x()) + " " + FToStr(np2.y()) + " c\n"; |
||
1477 | } |
||
22008 | jghali | 1478 | fon += useNonZeroRule? "h f\n" : "h f*\n"; |
20112 | jghali | 1479 | np = getMinClipF(&gly); |
1480 | np1 = getMaxClipF(&gly); |
||
1481 | } |
||
1482 | else |
||
1483 | { |
||
20684 | jghali | 1484 | // 13654: make evince emit warning about "Syntax Error: No current point in closepath" |
1485 | // PDF specification is not quite clear about that, but a single "h" operator might not |
||
1486 | // be a valid way to make an empty path |
||
1487 | // fon = "h"; |
||
20112 | jghali | 1488 | np = FPoint(0, 0); |
1489 | np1 = FPoint(0, 0); |
||
1490 | } |
||
1491 | 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"); |
||
1492 | minx = qMin(minx, np.x()); |
||
1493 | miny = qMin(miny, np.y()); |
||
1494 | maxx = qMax(maxx, np1.x()); |
||
1495 | maxy = qMax(maxy, np1.y()); |
||
1496 | glyphWidths.append(qRound(np1.x())); |
||
1497 | PdfId charProcObject = writer.newObject(); |
||
1498 | charProcs.append(Pdf::toName(gl[ig.key()].second)+" "+Pdf::toPdf(charProcObject)+" 0 R\n"); |
||
1499 | encoding += Pdf::toName(gl[ig.key()].second)+" "; |
||
1500 | glyphMapping.insert(ig.key(), glyphCount + SubFonts * 256); |
||
1501 | writer.startObj(charProcObject); |
||
1502 | if (Options.Compress) |
||
1503 | fon = CompressArray(fon); |
||
1504 | PutDoc("<< /Length "+Pdf::toPdf(fon.length()+1)); |
||
1505 | if (Options.Compress) |
||
1506 | PutDoc("\n/Filter /FlateDecode"); |
||
1507 | PutDoc("\n>>\nstream\n"+EncStream(fon, charProcObject)+"\nendstream"); |
||
1508 | writer.endObj(charProcObject); |
||
1509 | glyphCount++; |
||
1510 | int glyphsLeft = RealGlyphs.count() - SubFonts * 256; |
||
1511 | if ((glyphCount > 255) || (glyphCount == glyphsLeft)) |
||
1512 | { |
||
1513 | PdfId fontWidths = writer.newObject(); |
||
1514 | writer.startObj(fontWidths); |
||
1515 | PutDoc("[ "); |
||
1516 | for (int ww = 0; ww < glyphWidths.count(); ++ww) |
||
1517 | { |
||
1518 | PutDoc(Pdf::toPdf(glyphWidths[ww])+" "); |
||
1519 | } |
||
1520 | PutDoc("]"); |
||
1521 | writer.endObj(fontWidths); |
||
1522 | PdfId fontCharProcs = writer.newObject(); |
||
1523 | writer.startObj(fontCharProcs); |
||
1524 | PutDoc("<<\n"); |
||
1525 | for (int ww = 0; ww < charProcs.count(); ++ww) |
||
1526 | { |
||
1527 | PutDoc(charProcs[ww]); |
||
1528 | } |
||
1529 | PutDoc(">>"); |
||
1530 | writer.endObj(fontCharProcs); |
||
1531 | PdfId fontEncoding = writer.newObject(); |
||
1532 | writer.startObj(fontEncoding); |
||
1533 | PutDoc(encoding); |
||
1534 | PutDoc("]\n"); |
||
1535 | PutDoc(">>"); |
||
1536 | writer.endObj(fontEncoding); |
||
1537 | PdfId font3Object = writer.newObject(); |
||
1538 | writer.startObj(font3Object); |
||
1539 | PutDoc("<<\n/Type /Font\n/Subtype /Type3\n"); |
||
1540 | PutDoc("/Name /"+name+"S"+Pdf::toPdf(SubFonts)+"\n"); |
||
1541 | PutDoc("/FirstChar 0\n"); |
||
1542 | PutDoc("/LastChar "+Pdf::toPdf(glyphCount-1)+"\n"); |
||
1543 | PutDoc("/Widths "+Pdf::toPdf(fontWidths)+" 0 R\n"); |
||
1544 | PutDoc("/CharProcs "+Pdf::toPdf(fontCharProcs)+" 0 R\n"); |
||
1545 | PutDoc("/FontBBox ["+Pdf::toPdf(qRound(minx))+" "+Pdf::toPdf(qRound(miny))+" "+Pdf::toPdf(qRound(maxx))+ " "+Pdf::toPdf(qRound(maxy))+"]\n"); |
||
1546 | PutDoc("/FontMatrix [0.001 0 0 0.001 0 0]\n"); |
||
1547 | PutDoc("/Encoding "+Pdf::toPdf(fontEncoding)+" 0 R\n"); |
||
1548 | PutDoc(">>"); |
||
1549 | writer.endObj(font3Object); |
||
1550 | pageData.FObjects[name+"S"+Pdf::toPdf(SubFonts)] = font3Object; |
||
1551 | charProcs.clear(); |
||
1552 | glyphWidths.clear(); |
||
1553 | // glyphMapping.clear(); |
||
1554 | glyphCount = 0; |
||
1555 | ++SubFonts; |
||
1556 | minx = std::numeric_limits<double>::max(); |
||
1557 | miny = std::numeric_limits<double>::max(); |
||
1558 | maxx = -std::numeric_limits<double>::max(); |
||
1559 | maxy = -std::numeric_limits<double>::max(); |
||
1560 | encoding = "<< /Type /Encoding\n/Differences [ 0\n"; |
||
1561 | } |
||
1562 | } |
||
1563 | result.glyphmap = glyphMapping; |
||
1564 | return result; |
||
20103 | avox | 1565 | } |
1566 | |||
1567 | |||
1568 | PdfFont PDFLibCore::PDF_WriteGlyphsAsXForms(const QByteArray& fontName, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
1569 | { |
||
20112 | jghali | 1570 | PdfFont result; |
1571 | result.name = Pdf::toName(fontName); |
||
1572 | result.method = Use_XForm; |
||
1573 | result.encoding = Encode_224; |
||
20103 | avox | 1574 | |
22008 | jghali | 1575 | // TrueType font rasterizers use non-zero winding file rule, |
1576 | // vs even-odd for current Postscript font rasterizers |
||
1577 | // Note: OTF CFF2 will also use non-zero winding rule, so |
||
1578 | // change code below when adding CCF2 support to Scribus |
||
1579 | // ref: https://www.microsoft.com/typography/OTSpec/cff2.htm |
||
1580 | bool useNonZeroRule = (face.type() == ScFace::TTF); |
||
1581 | |||
20112 | jghali | 1582 | QByteArray fon; |
1583 | QMap<uint,FPointArray>::ConstIterator ig; |
||
1584 | for (ig = RealGlyphs.cbegin(); ig != RealGlyphs.cend(); ++ig) |
||
1585 | { |
||
1586 | FPoint np, np1, np2; |
||
1587 | bool nPath = true; |
||
1588 | fon.resize(0); |
||
1589 | if (ig.value().size() > 3) |
||
1590 | { |
||
1591 | FPointArray gly = ig.value(); |
||
1592 | QTransform mat; |
||
1593 | mat.scale(0.1, 0.1); |
||
1594 | gly.map(mat); |
||
1595 | for (int poi = 0; poi < gly.size()-3; poi += 4) |
||
1596 | { |
||
1597 | if (gly.isMarker(poi)) |
||
1598 | { |
||
1599 | fon += "h\n"; |
||
1600 | nPath = true; |
||
1601 | continue; |
||
1602 | } |
||
1603 | if (nPath) |
||
1604 | { |
||
1605 | np = gly.point(poi); |
||
1606 | fon += FToStr(np.x())+" "+FToStr(-np.y())+" m\n"; |
||
1607 | nPath = false; |
||
1608 | } |
||
1609 | np = gly.point(poi+1); |
||
1610 | np1 = gly.point(poi+3); |
||
1611 | np2 = gly.point(poi+2); |
||
1612 | fon += FToStr(np.x()) + " " + FToStr(-np.y()) + " " + |
||
1613 | FToStr(np1.x()) + " " + FToStr(-np1.y()) + " " + |
||
1614 | FToStr(np2.x()) + " " + FToStr(-np2.y()) + " c\n"; |
||
1615 | } |
||
22008 | jghali | 1616 | fon += useNonZeroRule? "h f\n" : "h f*\n"; |
20112 | jghali | 1617 | np = getMinClipF(&gly); |
1618 | np1 = getMaxClipF(&gly); |
||
1619 | } |
||
1620 | else |
||
1621 | { |
||
20684 | jghali | 1622 | // 13654: make evince emit warning about "Syntax Error: No current point in closepath" |
1623 | // PDF specification is not quite clear about that, but a single "h" operator might not |
||
1624 | // be a valid way to make an empty path |
||
1625 | fon = " " /*"h"*/; |
||
20112 | jghali | 1626 | np = FPoint(0, 0); |
1627 | np1 = FPoint(0, 0); |
||
1628 | } |
||
1629 | PdfId fontGlyphXForm = writer.newObject(); |
||
1630 | writer.startObj(fontGlyphXForm); |
||
1631 | PutDoc("<<\n/Type /XObject\n/Subtype /Form\n/FormType 1\n"); |
||
1632 | PutDoc("/BBox [ "+FToStr(np.x())+" "+FToStr(-np.y())+" "+FToStr(np1.x())+ " "+FToStr(-np1.y())+" ]\n"); |
||
1633 | PutDoc("/Resources << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]\n"); |
||
1634 | PutDoc(">>\n"); |
||
1635 | if (Options.Compress) |
||
1636 | fon = CompressArray(fon); |
||
1637 | PutDoc("/Length "+Pdf::toPdf(fon.length()+1)); |
||
1638 | if (Options.Compress) |
||
1639 | PutDoc("\n/Filter /FlateDecode"); |
||
1640 | PutDoc(" >>\nstream\n"+EncStream(fon, fontGlyphXForm)+"\nendstream"); |
||
1641 | writer.endObj(fontGlyphXForm); |
||
21894 | jghali | 1642 | pageData.XObjects[fontName + "_gl" + Pdf::toPdf(ig.key())] = fontGlyphXForm; |
20112 | jghali | 1643 | } |
1644 | return result; |
||
20103 | avox | 1645 | } |
1646 | |||
1647 | PdfId PDFLibCore::PDF_EmbedFontObject(const QByteArray& font, const QByteArray& subtype) |
||
1648 | |||
1649 | { |
||
20112 | jghali | 1650 | PdfId embeddedFontObject = writer.newObject(); |
1651 | writer.startObj(embeddedFontObject); |
||
1652 | int len = font.length(); |
||
1653 | QByteArray ttf = (Options.Compress? CompressArray(font) : font); |
||
1654 | //qDebug() << QString("sfnt data: size=%1 compressed=%2").arg(len).arg(bb.length()); |
||
1655 | PutDoc("<<\n/Length " + Pdf::toPdf(ttf.length() + 1) + "\n"); |
||
1656 | PutDoc("/Length1 " + Pdf::toPdf(len) + "\n"); |
||
1657 | if (subtype.size() > 0) |
||
1658 | PutDoc("/Subtype " + subtype); |
||
1659 | if (Options.Compress) |
||
1660 | PutDoc("/Filter /FlateDecode\n"); |
||
1661 | PutDoc(">>\nstream\n"); |
||
1662 | EncodeArrayToStream(ttf, embeddedFontObject); |
||
1663 | PutDoc("\nendstream"); |
||
1664 | writer.endObj(embeddedFontObject); |
||
1665 | return embeddedFontObject; |
||
20103 | avox | 1666 | } |
1667 | |||
21775 | jghali | 1668 | QByteArray PDFLibCore::PDF_GenerateSubsetTag(const QByteArray& fontName, QList<uint> usedGlyphs) |
1669 | { |
||
1670 | uint hash, mod; |
||
1671 | QVector<uint> glyphVec = usedGlyphs.toVector(); |
||
1672 | |||
1673 | hash = qHashBits(fontName.constData(), fontName.size(), 0); |
||
1674 | hash = qHashBits(glyphVec.constData(), glyphVec.size() * sizeof(uint), hash); |
||
1675 | |||
1676 | QByteArray subsetTag(6, (char) 0); |
||
1677 | for (int i = 0; i < 6; ++i) |
||
1678 | { |
||
1679 | mod = hash % 26; |
||
1680 | subsetTag[i] = 'A' + mod; |
||
1681 | hash = (hash - mod) / 26; |
||
1682 | } |
||
1683 | |||
1684 | return subsetTag; |
||
1685 | } |
||
1686 | |||
20103 | avox | 1687 | PdfId PDFLibCore::PDF_WriteFontDescriptor(const QByteArray& fontName, ScFace& face, ScFace::FontFormat fformat, PdfId embeddedFontObject) |
1688 | { |
||
20112 | jghali | 1689 | PdfId fontDescriptor = writer.newObject(); |
1690 | writer.startObj(fontDescriptor); |
||
1691 | // TODO: think about QByteArray ScFace::getFontDescriptor() -- AV |
||
1692 | PutDoc("<<\n/Type /FontDescriptor\n"); |
||
1693 | PutDoc("/FontName " + Pdf::toName(fontName) + "\n"); |
||
1694 | PutDoc("/FontBBox [ " + Pdf::toAscii(face.pdfFontBBoxAsString()) + " ]\n"); |
||
1695 | PutDoc("/Flags "); |
||
1696 | //FIXME: isItalic() should be queried from ScFace, not from Qt -- AV |
||
1697 | //QFontInfo fo = QFontInfo(it.data()); |
||
1698 | int pfl = 0; |
||
1699 | if (face.isFixedPitch()) |
||
1700 | pfl = pfl ^ 1; |
||
1701 | //if (fo.italic()) |
||
1702 | if (face.italicAngleAsString() != "0") |
||
1703 | pfl = pfl ^ 64; |
||
1704 | // pfl = pfl ^ 4; |
||
1705 | pfl = pfl ^ 32; |
||
1706 | PutDoc(Pdf::toPdf(pfl)+"\n"); |
||
1707 | PutDoc("/Ascent " + Pdf::toAscii(face.pdfAscentAsString())+"\n"); |
||
1708 | PutDoc("/Descent " + Pdf::toAscii(face.pdfDescentAsString())+"\n"); |
||
1709 | PutDoc("/CapHeight " + Pdf::toAscii(face.pdfCapHeightAsString())+"\n"); |
||
1710 | PutDoc("/ItalicAngle " + Pdf::toAscii(face.italicAngleAsString())+"\n"); |
||
1711 | PutDoc("/StemV 1\n"); |
||
1712 | if (embeddedFontObject != 0) |
||
1713 | { |
||
1714 | if (fformat == ScFace::SFNT || fformat == ScFace::TTCF) |
||
1715 | { |
||
1716 | if (face.type() == ScFace::OTF) |
||
1717 | { |
||
1718 | PutDoc("/FontFile3 "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1719 | } |
||
1720 | else |
||
1721 | { |
||
1722 | PutDoc("/FontFile2 "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1723 | } |
||
1724 | } |
||
1725 | else if (fformat == ScFace::PFB) |
||
1726 | PutDoc("/FontFile "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1727 | else if (fformat == ScFace::PFA) |
||
1728 | PutDoc("/FontFile "+Pdf::toPdf(embeddedFontObject)+" 0 R\n"); |
||
1729 | } |
||
1730 | PutDoc(">>"); |
||
1731 | writer.endObj(fontDescriptor); |
||
1732 | return fontDescriptor; |
||
20103 | avox | 1733 | } |
1734 | |||
1735 | PdfFont PDFLibCore::PDF_EncodeCidFont(const QByteArray& fontName, ScFace& face, const QByteArray& baseFont, PdfId fontDes, const ScFace::FaceEncoding& gl, const QMap<uint,uint> glyphmap ) |
||
1736 | { |
||
20112 | jghali | 1737 | PdfFont result; |
1738 | result.name = Pdf::toName(fontName); |
||
1739 | result.method = glyphmap.isEmpty()? Use_Embedded : Use_Subset; |
||
1740 | result.encoding = glyphmap.isEmpty()? Encode_IdentityH : Encode_Subset; |
||
1741 | result.glyphmap = glyphmap; |
||
1742 | |||
1743 | PdfId fontWidths2 = writer.newObject(); |
||
1744 | writer.startObj(fontWidths2); |
||
1745 | QList<QByteArray> toUnicodeMaps; |
||
1746 | QList<int> toUnicodeMapsCount; |
||
1747 | QByteArray toUnicodeMap = ""; |
||
1748 | int toUnicodeMapCounter = 0; |
||
1749 | |||
1750 | PutDoc("[ "); |
||
1751 | QList<uint> keys = gl.uniqueKeys(); |
||
1752 | QList<uint>::iterator git; |
||
1753 | bool seenNotDef = false; |
||
1754 | for (git = keys.begin(); git != keys.end(); ++git) |
||
1755 | { |
||
1756 | uint gid = result.encoding == Encode_Subset? glyphmap[*git] : *git; |
||
1757 | if (gid > 0 || !seenNotDef) |
||
1758 | { |
||
1759 | seenNotDef |= (gid == 0); |
||
1760 | PutDoc(Pdf::toPdf(gid)+" ["+Pdf::toPdf(static_cast<int>(face.glyphWidth(*git)* 1000))+"] " ); |
||
1761 | QString tmp, tmp2; |
||
1762 | tmp.sprintf("%04X", gid); |
||
1763 | tmp2.sprintf("%04X", gl.value(*git).first); |
||
1764 | toUnicodeMap += "<" + Pdf::toAscii(tmp)+ "> <" + Pdf::toAscii(tmp2) + ">\n"; |
||
1765 | toUnicodeMapCounter++; |
||
1766 | if (toUnicodeMapCounter == 100) |
||
1767 | { |
||
1768 | toUnicodeMaps.append(toUnicodeMap); |
||
1769 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1770 | toUnicodeMap = ""; |
||
1771 | toUnicodeMapCounter = 0; |
||
1772 | } |
||
1773 | } |
||
1774 | } |
||
1775 | PutDoc("]"); |
||
1776 | writer.endObj(fontWidths2); |
||
1777 | if (toUnicodeMapCounter != 0) |
||
1778 | { |
||
1779 | toUnicodeMaps.append(toUnicodeMap); |
||
1780 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1781 | } |
||
1782 | QByteArray toUnicodeMapStream = ""; |
||
1783 | toUnicodeMapStream += "/CIDInit /ProcSet findresource begin\n"; |
||
1784 | toUnicodeMapStream += "12 dict begin\n"; |
||
1785 | toUnicodeMapStream += "begincmap\n"; |
||
1786 | toUnicodeMapStream += "/CIDSystemInfo <<\n"; |
||
1787 | toUnicodeMapStream += "/Registry (Adobe)\n"; |
||
1788 | toUnicodeMapStream += "/Ordering (UCS)\n"; |
||
1789 | toUnicodeMapStream += "/Supplement 0\n"; |
||
1790 | toUnicodeMapStream += ">> def\n"; |
||
1791 | toUnicodeMapStream += "/CMapName /Adobe-Identity-UCS def\n"; |
||
1792 | toUnicodeMapStream += "/CMapType 2 def\n"; |
||
1793 | toUnicodeMapStream += "1 begincodespacerange\n"; |
||
1794 | toUnicodeMapStream += "<0000> <FFFF>\n"; |
||
1795 | toUnicodeMapStream += "endcodespacerange\n"; |
||
1796 | for (int uniC = 0; uniC < toUnicodeMaps.count(); uniC++) |
||
1797 | { |
||
1798 | toUnicodeMapStream += Pdf::toPdf(toUnicodeMapsCount[uniC]); |
||
1799 | toUnicodeMapStream += " beginbfchar\n"; |
||
1800 | toUnicodeMapStream += toUnicodeMaps[uniC]; |
||
1801 | toUnicodeMapStream += "endbfchar\n"; |
||
1802 | } |
||
1803 | toUnicodeMapStream += "endcmap\n"; |
||
1804 | toUnicodeMapStream += "CMapName currentdict /CMap defineresource pop\n"; |
||
1805 | toUnicodeMapStream += "end\n"; |
||
1806 | toUnicodeMapStream += "end\n"; |
||
1807 | PdfId fontToUnicode2 = WritePDFStream(toUnicodeMapStream); |
||
1808 | PdfId fontObject2 = writer.newObject(); |
||
1809 | writer.startObj(fontObject2); |
||
1810 | PutDoc("<<\n/Type /Font\n/Subtype /Type0\n"); |
||
1811 | PutDoc("/Name " + Pdf::toName(fontName) + "\n"); |
||
21759 | jghali | 1812 | PutDoc("/BaseFont "+ Pdf::toName(baseFont) +"\n"); |
20112 | jghali | 1813 | PutDoc("/Encoding /Identity-H\n"); |
1814 | PutDoc("/ToUnicode "+Pdf::toPdf(fontToUnicode2)+" 0 R\n"); |
||
1815 | PutDoc("/DescendantFonts ["); |
||
1816 | PutDoc("<</Type /Font"); |
||
1817 | if (face.type() == ScFace::OTF) |
||
1818 | PutDoc("/Subtype /CIDFontType0"); |
||
1819 | else |
||
1820 | PutDoc("/Subtype /CIDFontType2"); |
||
21759 | jghali | 1821 | PutDoc("/BaseFont " + Pdf::toName(baseFont)); |
20112 | jghali | 1822 | PutDoc("/FontDescriptor " + Pdf::toPdf(fontDes)+ " 0 R"); |
1823 | PutDoc("/CIDSystemInfo <</Ordering(Identity)/Registry(Adobe)/Supplement 0>>"); |
||
1824 | PutDoc("/DW 1000"); |
||
1825 | PutDoc("/W "+Pdf::toPdf(fontWidths2)+" 0 R"); |
||
1826 | PutDoc("/CIDToGIDMap /Identity"); |
||
1827 | PutDoc(">>"); // close CIDFont dictionary |
||
1828 | PutDoc("]\n"); // close DescendantFonts array |
||
1829 | PutDoc(">>"); |
||
1830 | writer.endObj(fontObject2); |
||
1831 | pageData.FObjects[fontName] = fontObject2; |
||
1832 | return result; |
||
20103 | avox | 1833 | } |
1834 | |||
1835 | |||
1836 | PdfFont PDFLibCore::PDF_EncodeSimpleFont(const QByteArray& fontName, ScFace& face, const QByteArray& baseFont, const QByteArray& subtype, bool isEmbedded, PdfId fontDes, const ScFace::FaceEncoding& gl) |
||
1837 | { |
||
20112 | jghali | 1838 | PdfFont result; |
1839 | result.name = Pdf::toName(fontName); |
||
1840 | result.method = isEmbedded? Use_Embedded : Use_System; |
||
1841 | result.encoding = Encode_224; |
||
1842 | |||
1843 | int nglyphs = 0; |
||
1844 | ScFace::FaceEncoding::ConstIterator gli; |
||
1845 | for (gli = gl.cbegin(); gli != gl.cend(); ++gli) |
||
1846 | { |
||
1847 | if (gli.key() > static_cast<uint>(nglyphs)) |
||
1848 | nglyphs = gli.key(); |
||
1849 | } |
||
1850 | ++nglyphs; |
||
1851 | // qDebug() << QString("pdflib: nglyphs %1 max %2").arg(nglyphs).arg(face.maxGlyph()); |
||
1852 | |||
1853 | uint Fcc = nglyphs / 224; |
||
1854 | if ((nglyphs % 224) != 0) |
||
1855 | Fcc += 1; |
||
1856 | for (uint Fc = 0; Fc < Fcc; ++Fc) |
||
1857 | { |
||
1858 | PdfId fontWidths2 = writer.newObject(); |
||
1859 | writer.startObj(fontWidths2); |
||
1860 | int chCount = 32; |
||
1861 | 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 "); |
||
1862 | for (int ww = 32; ww < 256; ++ww) |
||
1863 | { |
||
1864 | uint glyph = 224 * Fc + ww - 32; |
||
1865 | if (gl.contains(glyph)) |
||
1866 | PutDoc(Pdf::toPdf(static_cast<int>(face.glyphWidth(glyph)* 1000))+" "); |
||
1867 | else |
||
1868 | PutDoc("0 "); |
||
1869 | chCount++; |
||
1870 | if (signed(glyph) == nglyphs-1) |
||
1871 | break; |
||
1872 | } |
||
1873 | PutDoc("]"); |
||
1874 | writer.endObj(fontWidths2); |
||
1875 | PdfId fontEncoding2 = writer.newObject(); |
||
1876 | writer.startObj(fontEncoding2); |
||
1877 | QStringList toUnicodeMaps; |
||
1878 | QList<int> toUnicodeMapsCount; |
||
1879 | QString toUnicodeMap = ""; |
||
1880 | int toUnicodeMapCounter = 0; |
||
1881 | PutDoc("<< /Type /Encoding\n"); |
||
1882 | PutDoc("/Differences [ \n"); |
||
1883 | int crc = 0; |
||
1884 | bool startOfSeq = true; |
||
1885 | for (int ww2 = 32; ww2 < 256; ++ww2) |
||
1886 | { |
||
1887 | uint glyph = 224 * Fc + ww2 - 32; |
||
1888 | ScFace::FaceEncoding::ConstIterator glIt = gl.find(glyph); |
||
1889 | if (glIt != gl.cend() && !glIt.value().second.isEmpty()) |
||
1890 | { |
||
1891 | if (startOfSeq) |
||
1892 | { |
||
1893 | PutDoc(Pdf::toPdf(ww2)+" "); |
||
1894 | startOfSeq = false; |
||
1895 | } |
||
1896 | PutDoc(Pdf::toName(glIt.value().second)+" "); |
||
1897 | QString tmp, tmp2; |
||
1898 | tmp.sprintf("%02X", ww2); |
||
1899 | tmp2.sprintf("%04X", glIt.value().first); |
||
1900 | toUnicodeMap += "<" + Pdf::toAscii(tmp) + "> <" + Pdf::toAscii(tmp2) + ">\n"; |
||
1901 | //QString("<%1> <%2>\n").arg(tmp).arg((tmp2)); |
||
1902 | toUnicodeMapCounter++; |
||
1903 | if (toUnicodeMapCounter == 100) |
||
1904 | { |
||
1905 | toUnicodeMaps.append(toUnicodeMap); |
||
1906 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1907 | toUnicodeMap = ""; |
||
1908 | toUnicodeMapCounter = 0; |
||
1909 | } |
||
1910 | crc++; |
||
1911 | } |
||
1912 | else |
||
1913 | { |
||
1914 | startOfSeq = true; |
||
1915 | } |
||
1916 | if (signed(glyph) == nglyphs-1) |
||
1917 | break; |
||
1918 | if (crc > 8) |
||
1919 | { |
||
1920 | PutDoc("\n"); |
||
1921 | crc = 0; |
||
1922 | } |
||
1923 | } |
||
1924 | if (toUnicodeMapCounter != 0) |
||
1925 | { |
||
1926 | toUnicodeMaps.append(toUnicodeMap); |
||
1927 | toUnicodeMapsCount.append(toUnicodeMapCounter); |
||
1928 | } |
||
1929 | PutDoc("]\n"); |
||
1930 | PutDoc(">>"); |
||
1931 | writer.endObj(fontEncoding2); |
||
1932 | QByteArray toUnicodeMapStream = ""; |
||
1933 | toUnicodeMapStream += "/CIDInit /ProcSet findresource begin\n"; |
||
1934 | toUnicodeMapStream += "12 dict begin\n"; |
||
1935 | toUnicodeMapStream += "begincmap\n"; |
||
1936 | toUnicodeMapStream += "/CIDSystemInfo <<\n"; |
||
1937 | toUnicodeMapStream += "/Registry (Adobe)\n"; |
||
1938 | toUnicodeMapStream += "/Ordering (UCS)\n"; |
||
1939 | toUnicodeMapStream += "/Supplement 0\n"; |
||
1940 | toUnicodeMapStream += ">> def\n"; |
||
1941 | toUnicodeMapStream += "/CMapName /Adobe-Identity-UCS def\n"; |
||
1942 | toUnicodeMapStream += "/CMapType 2 def\n"; |
||
1943 | toUnicodeMapStream += "1 begincodespacerange\n"; |
||
1944 | toUnicodeMapStream += "<0000> <FFFF>\n"; |
||
1945 | toUnicodeMapStream += "endcodespacerange\n"; |
||
1946 | for (int uniC = 0; uniC < toUnicodeMaps.count(); uniC++) |
||
1947 | { |
||
1948 | toUnicodeMapStream += Pdf::toPdf(toUnicodeMapsCount[uniC]); |
||
1949 | toUnicodeMapStream += " beginbfchar\n"; |
||
1950 | toUnicodeMapStream += toUnicodeMaps[uniC]; |
||
1951 | toUnicodeMapStream += "endbfchar\n"; |
||
1952 | } |
||
1953 | toUnicodeMapStream += "endcmap\n"; |
||
1954 | toUnicodeMapStream += "CMapName currentdict /CMap defineresource pop\n"; |
||
1955 | toUnicodeMapStream += "end\n"; |
||
1956 | toUnicodeMapStream += "end\n"; |
||
1957 | PdfId fontToUnicode2 = WritePDFStream(toUnicodeMapStream); |
||
1958 | PdfId fontObject2 = writer.newObject(); |
||
1959 | writer.startObj(fontObject2); |
||
1960 | PutDoc("<<\n/Type /Font\n/Subtype "); |
||
1961 | PutDoc(subtype + "\n"); |
||
1962 | PutDoc("/Name "+Pdf::toName(fontName)+"S"+Pdf::toPdf(Fc)+"\n"); |
||
21759 | jghali | 1963 | PutDoc("/BaseFont "+ Pdf::toName(baseFont) + "\n"); |
20112 | jghali | 1964 | PutDoc("/FirstChar 0\n"); |
1965 | PutDoc("/LastChar "+Pdf::toPdf(chCount-1)+"\n"); |
||
1966 | PutDoc("/Widths "+Pdf::toPdf(fontWidths2)+" 0 R\n"); |
||
1967 | PutDoc("/Encoding "+Pdf::toPdf(fontEncoding2)+" 0 R\n"); |
||
1968 | PutDoc("/ToUnicode "+Pdf::toPdf(fontToUnicode2)+" 0 R\n"); |
||
1969 | PutDoc("/FontDescriptor "+Pdf::toPdf(fontDes)+" 0 R\n"); |
||
1970 | PutDoc(">>"); |
||
1971 | writer.endObj(fontObject2); |
||
1972 | pageData.FObjects[fontName + "S"+Pdf::toPdf(Fc)] = fontObject2; |
||
1973 | } // for(Fc) |
||
21744 | jghali | 1974 | |
1975 | return result; |
||
1976 | } |
||
1977 | |||
1978 | PdfFont PDFLibCore::PDF_EncodeFormFont(const QByteArray& fontName, ScFace& face, const QByteArray& baseFont, const QByteArray& subtype, PdfId fontDes) |
||
1979 | { |
||
1980 | PdfFont formFont; |
||
1981 | formFont.name = Pdf::toName(fontName) + "Form"; |
||
1982 | formFont.usage = Used_in_Forms; |
||
1983 | |||
20112 | jghali | 1984 | PdfId fontWidthsForm = writer.newObject(); |
1985 | writer.startObj(fontWidthsForm); |
||
1986 | 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 | 1987 | for (uint ww = 32; ww < 256; ++ww) |
20112 | jghali | 1988 | { |
21744 | jghali | 1989 | int unicode = Pdf::fromPDFDocEncoding(ww); |
1990 | uint glyph = face.char2CMap(unicode); |
||
1991 | if (glyph > 0) |
||
20112 | jghali | 1992 | PutDoc(Pdf::toPdf(static_cast<int>(face.glyphWidth(glyph)* 1000))+" "); |
1993 | else |
||
1994 | PutDoc("0 "); |
||
1995 | } |
||
1996 | PutDoc("]"); |
||
1997 | writer.endObj(fontWidthsForm); |
||
21744 | jghali | 1998 | |
20112 | jghali | 1999 | PdfId fontObjectForm = writer.newObject(); |
2000 | writer.startObj(fontObjectForm); |
||
2001 | PutDoc("<<\n/Type /Font\n/Subtype "); |
||
2002 | PutDoc(subtype + "\n"); |
||
21759 | jghali | 2003 | PutDoc("/Name " + formFont.name + "\n"); |
2004 | PutDoc("/BaseFont "+ Pdf::toName(baseFont) +"\n"); |
||
20112 | jghali | 2005 | PutDoc("/Encoding << \n"); |
2006 | PutDoc("/Differences [ \n"); |
||
2007 | PutDoc("24 /breve /caron /circumflex /dotaccent /hungarumlaut /ogonek /ring /tilde\n"); |
||
2008 | PutDoc("39 /quotesingle 96 /grave 128 /bullet /dagger /daggerdbl /ellipsis /emdash /endash /florin /fraction /guilsinglleft /guilsinglright\n"); |
||
2009 | PutDoc("/minus /perthousand /quotedblbase /quotedblleft /quotedblright /quoteleft /quoteright /quotesinglbase /trademark /fi /fl /Lslash /OE /Scaron\n"); |
||
21744 | jghali | 2010 | PutDoc("/Ydieresis /Zcaron /dotlessi /lslash /oe /scaron /zcaron 160 /Euro 164 /currency 166 /brokenbar 168 /dieresis /copyright /ordfeminine 172 /logicalnot\n"); |
20112 | jghali | 2011 | PutDoc("/.notdef /registered /macron /degree /plusminus /twosuperior /threesuperior /acute /mu 183 /periodcentered /cedilla /onesuperior /ordmasculine\n"); |
2012 | PutDoc("188 /onequarter /onehalf /threequarters 192 /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute /Ecircumflex\n"); |
||
2013 | PutDoc("/Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash\n"); |
||
2014 | PutDoc("/Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla\n"); |
||
2015 | PutDoc("/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis\n"); |
||
2016 | PutDoc("/divide /oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis\n"); |
||
2017 | PutDoc("] >>\n"); |
||
2018 | PutDoc("/FirstChar 0\n"); |
||
2019 | PutDoc("/LastChar 255\n"); |
||
2020 | PutDoc("/Widths "+Pdf::toPdf(fontWidthsForm)+" 0 R\n"); |
||
2021 | PutDoc("/FontDescriptor "+Pdf::toPdf(fontDes)+" 0 R\n"); |
||
2022 | PutDoc(">>"); |
||
2023 | writer.endObj(fontObjectForm); |
||
20103 | avox | 2024 | |
21744 | jghali | 2025 | pageData.FObjects[fontName + "Form"] = fontObjectForm; |
2026 | UsedFontsF.insert(face.replacementName(), formFont); |
||
2027 | |||
2028 | return formFont; |
||
20103 | avox | 2029 | } |
2030 | |||
21075 | craig | 2031 | /* |
20103 | avox | 2032 | static void dumpFont(QString name, QByteArray data) |
2033 | { |
||
20112 | jghali | 2034 | QFile file(name); |
20980 | craig | 2035 | if (file.open(QIODevice::WriteOnly)) |
2036 | { |
||
2037 | QDataStream out(&file); |
||
2038 | out.writeRawData(data.data(), data.length()); |
||
2039 | file.close(); |
||
2040 | } |
||
20103 | avox | 2041 | } |
2042 | |||
2043 | static void dumpCFF(QString name, cff::CFF font) |
||
2044 | { |
||
20112 | jghali | 2045 | QFile file(name); |
20980 | craig | 2046 | if (file.open(QIODevice::WriteOnly)) |
2047 | { |
||
2048 | QDataStream out(&file); |
||
2049 | font.dump(out); |
||
2050 | file.close(); |
||
2051 | } |
||
20103 | avox | 2052 | } |
21075 | craig | 2053 | */ |
20103 | avox | 2054 | |
2055 | |||
2056 | PdfFont PDFLibCore::PDF_WriteTtfSubsetFont(const QByteArray& fontName, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
2057 | { |
||
20112 | jghali | 2058 | QByteArray font; |
2059 | face.RawData(font); |
||
20974 | jghali | 2060 | /*dumpFont(face.psName() + ".ttf", font);*/ |
20112 | jghali | 2061 | QList<ScFace::gid_type> glyphs = RealGlyphs.uniqueKeys(); |
2062 | glyphs.removeAll(0); |
||
2063 | glyphs.prepend(0); |
||
2064 | QByteArray subset = sfnt::subsetFace(font, glyphs); |
||
20974 | jghali | 2065 | /*dumpFont(face.psName()+"subs.ttf", subset);*/ |
21775 | jghali | 2066 | QByteArray baseFont = sanitizeFontName(face.psName()); |
2067 | QByteArray subsetTag = PDF_GenerateSubsetTag(baseFont, glyphs); |
||
2068 | QByteArray subsetName = subsetTag + '+' + baseFont; |
||
20112 | jghali | 2069 | PdfId embeddedFontObj = PDF_EmbedFontObject(subset, QByteArray()); |
21775 | jghali | 2070 | PdfId fontDes = PDF_WriteFontDescriptor(subsetName, face, face.format(), embeddedFontObj); |
2071 | |||
20112 | jghali | 2072 | ScFace::FaceEncoding fullEncoding, subEncoding; |
2073 | QMap<uint,uint> glyphmap; |
||
2074 | face.glyphNames(fullEncoding); |
||
2075 | for (int i = 0; i < glyphs.length(); ++i) |
||
2076 | { |
||
2077 | glyphmap[glyphs[i]] = i; |
||
2078 | qDebug() << glyphs[i] << " --> " << i << QChar(fullEncoding[glyphs[i]].first); |
||
2079 | } |
||
2080 | |||
21775 | jghali | 2081 | PdfFont result = PDF_EncodeCidFont(fontName, face, subsetName, fontDes, fullEncoding, glyphmap); |
20112 | jghali | 2082 | return result; |
20103 | avox | 2083 | } |
2084 | |||
2085 | |||
2086 | PdfFont PDFLibCore::PDF_WriteCffSubsetFont(const QByteArray& fontName, ScFace& face, const QMap<uint,FPointArray>& RealGlyphs) |
||
2087 | { |
||
20112 | jghali | 2088 | // QByteArray sfnt; //TEST |
2089 | // face.RawData(sfnt); |
||
2090 | // QByteArray cff = sfnt::getTable(sfnt, "CFF "); |
||
2091 | // dumpFont(fontName, cff); |
||
2092 | // cff::CFF cfffont(cff); |
||
2093 | // cfffont.dump(); |
||
2094 | // QByteArray subsetfont = cff::subsetFace(cff, it.value().keys()); |
||
2095 | // dumpFont(it.key() + "subs.cff", subsetfont); |
||
2096 | // cff::CFF subset(subsetfont); |
||
2097 | // subset.dump(); |
||
2098 | // PDF_WriteFontDescriptor(fontName, face, fformat, 0); |
||
2099 | // // END |
||
2100 | |||
20136 | avox | 2101 | QByteArray font, data; |
2102 | face.RawData(data); |
||
2103 | font = sfnt::getTable(data, "CFF "); |
||
20974 | jghali | 2104 | /*dumpFont(face.psName() + ".cff", font);*/ |
20112 | jghali | 2105 | QList<ScFace::gid_type> glyphs = RealGlyphs.uniqueKeys(); |
2106 | glyphs.removeAll(0); |
||
2107 | glyphs.prepend(0); |
||
2108 | QByteArray subset = cff::subsetFace(font, glyphs); |
||
20974 | jghali | 2109 | /*dumpFont(face.psName()+"subs.cff", subset);*/ |
21775 | jghali | 2110 | QByteArray baseFont = sanitizeFontName(face.psName()); |
2111 | QByteArray subsetTag = PDF_GenerateSubsetTag(baseFont, glyphs); |
||
2112 | QByteArray subsetName = subsetTag + '+' + baseFont; |
||
20112 | jghali | 2113 | PdfId embeddedFontObj = PDF_EmbedFontObject(subset, "/CIDFontType0C"); |
21775 | jghali | 2114 | PdfId fontDes = PDF_WriteFontDescriptor(subsetName, face, face.format(), embeddedFontObj); |
2115 | |||
20112 | jghali | 2116 | ScFace::FaceEncoding fullEncoding, subEncoding; |
2117 | QMap<uint,uint> glyphmap; |
||
2118 | face.glyphNames(fullEncoding); |
||
2119 | for (int i = 0; i < glyphs.length(); ++i) |
||
2120 | { |
||
2121 | glyphmap[glyphs[i]] = i; |
||
2122 | qDebug() << glyphs[i] << " --> " << i << QChar(fullEncoding[glyphs[i]].first); |
||
2123 | } |
||
2124 | |||
21775 | jghali | 2125 | PdfFont result = PDF_EncodeCidFont(fontName, face, subsetName, fontDes, fullEncoding, glyphmap); |
20112 | jghali | 2126 | return result; |
20103 | avox | 2127 | } |
2128 | |||
2129 | |||
2130 | PdfId PDFLibCore::PDF_EmbedType1AsciiFontObject(const QByteArray& fon) |
||
2131 | { |
||
20112 | jghali | 2132 | QByteArray fon2; |
2133 | PdfId embeddedFontObject = writer.newObject(); |
||
2134 | writer.startObj(embeddedFontObject); |
||
2135 | int len1 = fon.indexOf("eexec")+5; |
||
2136 | fon2 = fon.left(len1) + "\n"; |
||
2137 | int len2 = fon.indexOf("0000000000000000000000000"); |
||
2138 | if (len2 == -1) |
||
2139 | len2 = fon.length()+1; |
||
2140 | // QString tm; |
||
2141 | // uint value; |
||
2142 | // bool ok = true; |
||
2143 | // int count = 0; |
||
2144 | // for (int xx = len1; xx < len2-1; ++xx) |
||
2145 | // { |
||
2146 | // tm = fon.at(xx); |
||
2147 | // if ((tm == QChar(13)) || (tm == QChar(10))) |
||
2148 | // continue; |
||
2149 | // xx++; |
||
2150 | // count++; |
||
2151 | // tm += fon.at(xx); |
||
2152 | // value = tm.toUInt(&ok, 16); |
||
2153 | // fon2 += char(value); |
||
2154 | // } |
||
2155 | QByteArray hexData = QByteArray::fromHex(fon.mid(len1, len2-len1)); |
||
2156 | fon2 += hexData; |
||
2157 | fon2 += fon.mid(len2); |
||
2158 | if (Options.Compress) |
||
2159 | fon2 = CompressArray(fon2); |
||