blob: ace2f45185b64dd6b1077e09cc2b9761b8216e6c [file] [log] [blame]
reed@google.com99ac02b2013-06-07 20:30:16 +00001/*
halcanarya43b4152015-03-25 12:15:04 -07002 * Copyright 2011 Google Inc.
reed@google.com99ac02b2013-06-07 20:30:16 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Hal Canary7cbf5e32017-07-12 13:10:23 -04008#include "SkPDFDocument.h"
9
10#include "SkCanvas.h"
halcanary022c2bd2016-09-02 11:29:46 -070011#include "SkMakeUnique.h"
msarettc213f0d2016-08-01 14:23:32 -070012#include "SkPDFCanon.h"
reed58677362014-10-09 05:30:10 -070013#include "SkPDFDevice.h"
halcanaryf12a1672015-09-23 12:45:49 -070014#include "SkPDFUtils.h"
halcanarya43b4152015-03-25 12:15:04 -070015#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040016#include "SkTo.h"
halcanaryf12a1672015-09-23 12:45:49 -070017
halcanary50e82e62016-03-21 13:45:05 -070018SkPDFObjectSerializer::SkPDFObjectSerializer() : fBaseOffset(0), fNextToBeSerialized(0) {}
19
halcanarya50151d2016-03-25 11:57:49 -070020SkPDFObjectSerializer::~SkPDFObjectSerializer() {
21 for (int i = 0; i < fObjNumMap.objects().count(); ++i) {
22 fObjNumMap.objects()[i]->drop();
23 }
24}
Hal Canary67362362018-04-24 13:58:37 -040025SkPDFObjectSerializer::SkPDFObjectSerializer(SkPDFObjectSerializer&&) = default;
26SkPDFObjectSerializer& SkPDFObjectSerializer::operator=(SkPDFObjectSerializer&&) = default;
27
halcanarya50151d2016-03-25 11:57:49 -070028
halcanary50e82e62016-03-21 13:45:05 -070029void SkPDFObjectSerializer::addObjectRecursively(const sk_sp<SkPDFObject>& object) {
halcanary530032a2016-08-18 14:22:52 -070030 fObjNumMap.addObjectRecursively(object.get());
halcanarya43b4152015-03-25 12:15:04 -070031}
32
halcanaryad5dcd12016-03-31 06:59:00 -070033#define SKPDF_MAGIC "\xD3\xEB\xE9\xE1"
Mike Klein8f11d4d2018-01-24 12:42:55 -050034#ifndef SK_BUILD_FOR_WIN
halcanaryad5dcd12016-03-31 06:59:00 -070035static_assert((SKPDF_MAGIC[0] & 0x7F) == "Skia"[0], "");
36static_assert((SKPDF_MAGIC[1] & 0x7F) == "Skia"[1], "");
37static_assert((SKPDF_MAGIC[2] & 0x7F) == "Skia"[2], "");
38static_assert((SKPDF_MAGIC[3] & 0x7F) == "Skia"[3], "");
39#endif
halcanary4b656662016-04-27 07:45:18 -070040void SkPDFObjectSerializer::serializeHeader(SkWStream* wStream,
41 const SkDocument::PDFMetadata& md) {
halcanary50e82e62016-03-21 13:45:05 -070042 fBaseOffset = wStream->bytesWritten();
halcanaryad5dcd12016-03-31 06:59:00 -070043 static const char kHeader[] = "%PDF-1.4\n%" SKPDF_MAGIC "\n";
Hal Canaryc172f9b2017-05-27 20:29:44 -040044 wStream->writeText(kHeader);
halcanary50e82e62016-03-21 13:45:05 -070045 // The PDF spec recommends including a comment with four
46 // bytes, all with their high bits set. "\xD3\xEB\xE9\xE1" is
47 // "Skia" with the high bits set.
halcanary4b656662016-04-27 07:45:18 -070048 fInfoDict = SkPDFMetadata::MakeDocumentInformationDict(md);
halcanary50e82e62016-03-21 13:45:05 -070049 this->addObjectRecursively(fInfoDict);
50 this->serializeObjects(wStream);
51}
halcanaryad5dcd12016-03-31 06:59:00 -070052#undef SKPDF_MAGIC
halcanary50e82e62016-03-21 13:45:05 -070053
54// Serialize all objects in the fObjNumMap that have not yet been serialized;
55void SkPDFObjectSerializer::serializeObjects(SkWStream* wStream) {
56 const SkTArray<sk_sp<SkPDFObject>>& objects = fObjNumMap.objects();
57 while (fNextToBeSerialized < objects.count()) {
58 SkPDFObject* object = objects[fNextToBeSerialized].get();
59 int32_t index = fNextToBeSerialized + 1; // Skip object 0.
60 // "The first entry in the [XREF] table (object number 0) is
61 // always free and has a generation number of 65,535; it is
62 // the head of the linked list of free objects."
63 SkASSERT(fOffsets.count() == fNextToBeSerialized);
Mike Reed5edcd312018-08-08 11:23:41 -040064 fOffsets.push_back(this->offset(wStream));
halcanary50e82e62016-03-21 13:45:05 -070065 wStream->writeDecAsText(index);
66 wStream->writeText(" 0 obj\n"); // Generation number is always 0.
halcanary530032a2016-08-18 14:22:52 -070067 object->emitObject(wStream, fObjNumMap);
halcanary50e82e62016-03-21 13:45:05 -070068 wStream->writeText("\nendobj\n");
69 object->drop();
70 ++fNextToBeSerialized;
71 }
72}
73
74// Xref table and footer
75void SkPDFObjectSerializer::serializeFooter(SkWStream* wStream,
76 const sk_sp<SkPDFObject> docCatalog,
77 sk_sp<SkPDFObject> id) {
78 this->serializeObjects(wStream);
79 int32_t xRefFileOffset = this->offset(wStream);
80 // Include the special zeroth object in the count.
81 int32_t objCount = SkToS32(fOffsets.count() + 1);
82 wStream->writeText("xref\n0 ");
83 wStream->writeDecAsText(objCount);
84 wStream->writeText("\n0000000000 65535 f \n");
85 for (int i = 0; i < fOffsets.count(); i++) {
86 wStream->writeBigDecAsText(fOffsets[i], 10);
87 wStream->writeText(" 00000 n \n");
88 }
halcanarya43b4152015-03-25 12:15:04 -070089 SkPDFDict trailerDict;
halcanary50e82e62016-03-21 13:45:05 -070090 trailerDict.insertInt("Size", objCount);
91 SkASSERT(docCatalog);
92 trailerDict.insertObjRef("Root", docCatalog);
93 SkASSERT(fInfoDict);
94 trailerDict.insertObjRef("Info", std::move(fInfoDict));
halcanary34422612015-10-12 10:11:18 -070095 if (id) {
halcanary8103a342016-03-08 15:10:16 -080096 trailerDict.insertObject("ID", std::move(id));
halcanary34422612015-10-12 10:11:18 -070097 }
halcanary50e82e62016-03-21 13:45:05 -070098 wStream->writeText("trailer\n");
halcanary530032a2016-08-18 14:22:52 -070099 trailerDict.emitObject(wStream, fObjNumMap);
halcanary50e82e62016-03-21 13:45:05 -0700100 wStream->writeText("\nstartxref\n");
101 wStream->writeBigDecAsText(xRefFileOffset);
102 wStream->writeText("\n%%EOF");
103}
104
105int32_t SkPDFObjectSerializer::offset(SkWStream* wStream) {
106 size_t offset = wStream->bytesWritten();
107 SkASSERT(offset > fBaseOffset);
108 return SkToS32(offset - fBaseOffset);
halcanarya43b4152015-03-25 12:15:04 -0700109}
110
halcanary2f7ebcb2015-03-25 12:45:28 -0700111
halcanary8103a342016-03-08 15:10:16 -0800112// return root node.
halcanarycc77c122016-03-23 06:26:31 -0700113static sk_sp<SkPDFDict> generate_page_tree(SkTArray<sk_sp<SkPDFDict>>* pages) {
halcanary2f7ebcb2015-03-25 12:45:28 -0700114 // PDF wants a tree describing all the pages in the document. We arbitrary
115 // choose 8 (kNodeSize) as the number of allowed children. The internal
116 // nodes have type "Pages" with an array of children, a parent pointer, and
117 // the number of leaves below the node as "Count." The leaves are passed
118 // into the method, have type "Page" and need a parent pointer. This method
119 // builds the tree bottom up, skipping internal nodes that would have only
120 // one child.
121 static const int kNodeSize = 8;
122
halcanary2f7ebcb2015-03-25 12:45:28 -0700123 // curNodes takes a reference to its items, which it passes to pageTree.
halcanarycc77c122016-03-23 06:26:31 -0700124 int totalPageCount = pages->count();
125 SkTArray<sk_sp<SkPDFDict>> curNodes;
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400126 curNodes.swap(*pages);
halcanary2f7ebcb2015-03-25 12:45:28 -0700127
128 // nextRoundNodes passes its references to nodes on to curNodes.
halcanary2f7ebcb2015-03-25 12:45:28 -0700129 int treeCapacity = kNodeSize;
130 do {
halcanarycc77c122016-03-23 06:26:31 -0700131 SkTArray<sk_sp<SkPDFDict>> nextRoundNodes;
halcanary2f7ebcb2015-03-25 12:45:28 -0700132 for (int i = 0; i < curNodes.count(); ) {
133 if (i > 0 && i + 1 == curNodes.count()) {
halcanarycc77c122016-03-23 06:26:31 -0700134 SkASSERT(curNodes[i]);
135 nextRoundNodes.emplace_back(std::move(curNodes[i]));
halcanary2f7ebcb2015-03-25 12:45:28 -0700136 break;
137 }
138
halcanaryece83922016-03-08 08:32:12 -0800139 auto newNode = sk_make_sp<SkPDFDict>("Pages");
140 auto kids = sk_make_sp<SkPDFArray>();
halcanary2f7ebcb2015-03-25 12:45:28 -0700141 kids->reserve(kNodeSize);
142
143 int count = 0;
144 for (; i < curNodes.count() && count < kNodeSize; i++, count++) {
halcanarycc77c122016-03-23 06:26:31 -0700145 SkASSERT(curNodes[i]);
halcanary8103a342016-03-08 15:10:16 -0800146 curNodes[i]->insertObjRef("Parent", newNode);
halcanarycc77c122016-03-23 06:26:31 -0700147 kids->appendObjRef(std::move(curNodes[i]));
halcanary2f7ebcb2015-03-25 12:45:28 -0700148 }
149
150 // treeCapacity is the number of leaf nodes possible for the
151 // current set of subtrees being generated. (i.e. 8, 64, 512, ...).
152 // It is hard to count the number of leaf nodes in the current
153 // subtree. However, by construction, we know that unless it's the
154 // last subtree for the current depth, the leaf count will be
155 // treeCapacity, otherwise it's what ever is left over after
156 // consuming treeCapacity chunks.
157 int pageCount = treeCapacity;
158 if (i == curNodes.count()) {
halcanarycc77c122016-03-23 06:26:31 -0700159 pageCount = ((totalPageCount - 1) % treeCapacity) + 1;
halcanary2f7ebcb2015-03-25 12:45:28 -0700160 }
halcanary72266fd2015-05-05 08:00:24 -0700161 newNode->insertInt("Count", pageCount);
halcanary8103a342016-03-08 15:10:16 -0800162 newNode->insertObject("Kids", std::move(kids));
halcanarycc77c122016-03-23 06:26:31 -0700163 nextRoundNodes.emplace_back(std::move(newNode));
halcanary2f7ebcb2015-03-25 12:45:28 -0700164 }
halcanarycc77c122016-03-23 06:26:31 -0700165 SkDEBUGCODE( for (const auto& n : curNodes) { SkASSERT(!n); } );
halcanary2f7ebcb2015-03-25 12:45:28 -0700166
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400167 curNodes.swap(nextRoundNodes);
halcanarycc77c122016-03-23 06:26:31 -0700168 nextRoundNodes.reset();
halcanary2f7ebcb2015-03-25 12:45:28 -0700169 treeCapacity *= kNodeSize;
170 } while (curNodes.count() > 1);
halcanarycc77c122016-03-23 06:26:31 -0700171 return std::move(curNodes[0]);
halcanary2f7ebcb2015-03-25 12:45:28 -0700172}
173
halcanarya43b4152015-03-25 12:15:04 -0700174////////////////////////////////////////////////////////////////////////////////
reed@google.com99ac02b2013-06-07 20:30:16 +0000175
halcanary50e82e62016-03-21 13:45:05 -0700176SkPDFDocument::SkPDFDocument(SkWStream* stream,
Mike Reed2e3f2e92017-12-15 20:43:03 +0000177 const SkDocument::PDFMetadata& metadata)
Hal Canaryc5980d02018-01-08 15:02:36 -0500178 : SkDocument(stream)
Mike Reeda4daf192017-12-14 13:25:04 -0500179 , fMetadata(metadata) {
halcanary50e82e62016-03-21 13:45:05 -0700180}
181
182SkPDFDocument::~SkPDFDocument() {
183 // subclasses of SkDocument must call close() in their destructors.
184 this->close();
185}
186
187void SkPDFDocument::serialize(const sk_sp<SkPDFObject>& object) {
188 fObjectSerializer.addObjectRecursively(object);
189 fObjectSerializer.serializeObjects(this->getStream());
190}
191
Hal Canaryb1de5f92017-07-01 22:17:15 -0400192SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height) {
halcanary50e82e62016-03-21 13:45:05 -0700193 SkASSERT(!fCanvas.get()); // endPage() was called before this.
halcanarycc77c122016-03-23 06:26:31 -0700194 if (fPages.empty()) {
halcanary50e82e62016-03-21 13:45:05 -0700195 // if this is the first page if the document.
196 fObjectSerializer.serializeHeader(this->getStream(), fMetadata);
halcanarycc77c122016-03-23 06:26:31 -0700197 fDests = sk_make_sp<SkPDFDict>();
Mike Reeda4daf192017-12-14 13:25:04 -0500198 if (fMetadata.fPDFA) {
halcanary4b656662016-04-27 07:45:18 -0700199 SkPDFMetadata::UUID uuid = SkPDFMetadata::CreateUUID(fMetadata);
halcanary8cd4a242016-04-07 08:56:15 -0700200 // We use the same UUID for Document ID and Instance ID since this
201 // is the first revision of this document (and Skia does not
202 // support revising existing PDF documents).
203 // If we are not in PDF/A mode, don't use a UUID since testing
204 // works best with reproducible outputs.
halcanary4b656662016-04-27 07:45:18 -0700205 fID = SkPDFMetadata::MakePdfId(uuid, uuid);
206 fXMP = SkPDFMetadata::MakeXMPObject(fMetadata, uuid, uuid);
halcanary8cd4a242016-04-07 08:56:15 -0700207 fObjectSerializer.addObjectRecursively(fXMP);
208 fObjectSerializer.serializeObjects(this->getStream());
halcanary488165e2016-04-22 06:10:21 -0700209 }
halcanary712fdf72015-12-10 08:59:43 -0800210 }
Hal Canary82591142018-05-09 14:20:12 -0400211 SkScalar rasterScale = this->rasterDpi() / SkPDFUtils::kDpiForRasterScaleOne;
212 SkISize pageSize = {SkScalarRoundToInt(width * rasterScale),
213 SkScalarRoundToInt(height * rasterScale)};
214
Hal Canarya0622582017-06-29 18:51:35 -0400215 fPageDevice = sk_make_sp<SkPDFDevice>(pageSize, this);
216 fPageDevice->setFlip(); // Only the top-level device needs to be flipped.
Herb Derbyefe39bc2018-05-01 17:06:20 -0400217 fCanvas.reset(new SkCanvas(fPageDevice));
Hal Canary82591142018-05-09 14:20:12 -0400218 fCanvas->scale(rasterScale, rasterScale);
halcanary50e82e62016-03-21 13:45:05 -0700219 return fCanvas.get();
220}
skia.committer@gmail.com63193672013-06-08 07:01:13 +0000221
halcanary50e82e62016-03-21 13:45:05 -0700222void SkPDFDocument::onEndPage() {
223 SkASSERT(fCanvas.get());
224 fCanvas->flush();
225 fCanvas.reset(nullptr);
halcanarycc77c122016-03-23 06:26:31 -0700226 SkASSERT(fPageDevice);
halcanarycc77c122016-03-23 06:26:31 -0700227 auto page = sk_make_sp<SkPDFDict>("Page");
228 page->insertObject("Resources", fPageDevice->makeResourceDict());
Hal Canary82591142018-05-09 14:20:12 -0400229
230 SkScalar rasterScale = SkPDFUtils::kDpiForRasterScaleOne / this->rasterDpi();
231
232 SkISize pageSize = fPageDevice->imageInfo().dimensions();
233 page->insertObject("MediaBox", SkPDFUtils::RectToArray(
234 {0, 0, pageSize.width() * rasterScale, pageSize.height() * rasterScale}));
235
halcanarycc77c122016-03-23 06:26:31 -0700236 auto annotations = sk_make_sp<SkPDFArray>();
237 fPageDevice->appendAnnotations(annotations.get());
238 if (annotations->size() > 0) {
239 page->insertObject("Annots", std::move(annotations));
240 }
halcanaryac0e00d2016-07-27 11:12:23 -0700241 auto contentObject = sk_make_sp<SkPDFStream>(fPageDevice->content());
halcanarycc77c122016-03-23 06:26:31 -0700242 this->serialize(contentObject);
243 page->insertObjRef("Contents", std::move(contentObject));
244 fPageDevice->appendDestinations(fDests.get(), page.get());
245 fPages.emplace_back(std::move(page));
246 fPageDevice.reset(nullptr);
halcanary50e82e62016-03-21 13:45:05 -0700247}
reed@google.com99ac02b2013-06-07 20:30:16 +0000248
halcanary50e82e62016-03-21 13:45:05 -0700249void SkPDFDocument::onAbort() {
halcanary530032a2016-08-18 14:22:52 -0700250 this->reset();
251}
252
253void SkPDFDocument::reset() {
halcanary50e82e62016-03-21 13:45:05 -0700254 fCanvas.reset(nullptr);
halcanarycc77c122016-03-23 06:26:31 -0700255 fPages.reset();
Hal Canary67362362018-04-24 13:58:37 -0400256 fCanon = SkPDFCanon();
257 fObjectSerializer = SkPDFObjectSerializer();
halcanary530032a2016-08-18 14:22:52 -0700258 fFonts.reset();
halcanary50e82e62016-03-21 13:45:05 -0700259}
reed@google.com99ac02b2013-06-07 20:30:16 +0000260
halcanary78daeff2016-04-07 12:34:59 -0700261static sk_sp<SkData> SkSrgbIcm() {
msarettc213f0d2016-08-01 14:23:32 -0700262 // Source: http://www.argyllcms.com/icclibsrc.html
263 static const char kProfile[] =
264 "\0\0\14\214argl\2 \0\0mntrRGB XYZ \7\336\0\1\0\6\0\26\0\17\0:acspM"
265 "SFT\0\0\0\0IEC sRGB\0\0\0\0\0\0\0\0\0\0\0\0\0\0\366\326\0\1\0\0\0\0"
266 "\323-argl\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"
267 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\21desc\0\0\1P\0\0\0\231cprt\0"
268 "\0\1\354\0\0\0gdmnd\0\0\2T\0\0\0pdmdd\0\0\2\304\0\0\0\210tech\0\0\3"
269 "L\0\0\0\14vued\0\0\3X\0\0\0gview\0\0\3\300\0\0\0$lumi\0\0\3\344\0\0"
270 "\0\24meas\0\0\3\370\0\0\0$wtpt\0\0\4\34\0\0\0\24bkpt\0\0\0040\0\0\0"
271 "\24rXYZ\0\0\4D\0\0\0\24gXYZ\0\0\4X\0\0\0\24bXYZ\0\0\4l\0\0\0\24rTR"
272 "C\0\0\4\200\0\0\10\14gTRC\0\0\4\200\0\0\10\14bTRC\0\0\4\200\0\0\10"
273 "\14desc\0\0\0\0\0\0\0?sRGB IEC61966-2.1 (Equivalent to www.srgb.co"
274 "m 1998 HP profile)\0\0\0\0\0\0\0\0\0\0\0?sRGB IEC61966-2.1 (Equiva"
275 "lent to www.srgb.com 1998 HP profile)\0\0\0\0\0\0\0\0text\0\0\0\0C"
276 "reated by Graeme W. Gill. Released into the public domain. No Warr"
277 "anty, Use at your own risk.\0\0desc\0\0\0\0\0\0\0\26IEC http://www"
278 ".iec.ch\0\0\0\0\0\0\0\0\0\0\0\26IEC http://www.iec.ch\0\0\0\0\0\0\0"
279 "\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\0"
280 "\0\0\0\0\0\0desc\0\0\0\0\0\0\0.IEC 61966-2.1 Default RGB colour sp"
281 "ace - sRGB\0\0\0\0\0\0\0\0\0\0\0.IEC 61966-2.1 Default RGB colour "
282 "space - sRGB\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0sig \0\0\0"
283 "\0CRT desc\0\0\0\0\0\0\0\rIEC61966-2.1\0\0\0\0\0\0\0\0\0\0\0\rIEC6"
284 "1966-2.1\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"
285 "\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\0view\0\0\0\0"
286 "\0\23\244|\0\24_0\0\20\316\2\0\3\355\262\0\4\23\n\0\3\\g\0\0\0\1XY"
287 "Z \0\0\0\0\0L\n=\0P\0\0\0W\36\270meas\0\0\0\0\0\0\0\1\0\0\0\0\0\0\0"
288 "\0\0\0\0\0\0\0\0\0\0\0\2\217\0\0\0\2XYZ \0\0\0\0\0\0\363Q\0\1\0\0\0"
289 "\1\26\314XYZ \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0XYZ \0\0\0\0\0\0o\240"
290 "\0\0008\365\0\0\3\220XYZ \0\0\0\0\0\0b\227\0\0\267\207\0\0\30\331X"
291 "YZ \0\0\0\0\0\0$\237\0\0\17\204\0\0\266\304curv\0\0\0\0\0\0\4\0\0\0"
292 "\0\5\0\n\0\17\0\24\0\31\0\36\0#\0(\0-\0002\0007\0;\0@\0E\0J\0O\0T\0"
293 "Y\0^\0c\0h\0m\0r\0w\0|\0\201\0\206\0\213\0\220\0\225\0\232\0\237\0"
294 "\244\0\251\0\256\0\262\0\267\0\274\0\301\0\306\0\313\0\320\0\325\0"
295 "\333\0\340\0\345\0\353\0\360\0\366\0\373\1\1\1\7\1\r\1\23\1\31\1\37"
296 "\1%\1+\0012\0018\1>\1E\1L\1R\1Y\1`\1g\1n\1u\1|\1\203\1\213\1\222\1"
297 "\232\1\241\1\251\1\261\1\271\1\301\1\311\1\321\1\331\1\341\1\351\1"
298 "\362\1\372\2\3\2\14\2\24\2\35\2&\2/\0028\2A\2K\2T\2]\2g\2q\2z\2\204"
299 "\2\216\2\230\2\242\2\254\2\266\2\301\2\313\2\325\2\340\2\353\2\365"
300 "\3\0\3\13\3\26\3!\3-\0038\3C\3O\3Z\3f\3r\3~\3\212\3\226\3\242\3\256"
301 "\3\272\3\307\3\323\3\340\3\354\3\371\4\6\4\23\4 \4-\4;\4H\4U\4c\4q"
302 "\4~\4\214\4\232\4\250\4\266\4\304\4\323\4\341\4\360\4\376\5\r\5\34"
303 "\5+\5:\5I\5X\5g\5w\5\206\5\226\5\246\5\265\5\305\5\325\5\345\5\366"
304 "\6\6\6\26\6'\0067\6H\6Y\6j\6{\6\214\6\235\6\257\6\300\6\321\6\343\6"
305 "\365\7\7\7\31\7+\7=\7O\7a\7t\7\206\7\231\7\254\7\277\7\322\7\345\7"
306 "\370\10\13\10\37\0102\10F\10Z\10n\10\202\10\226\10\252\10\276\10\322"
307 "\10\347\10\373\t\20\t%\t:\tO\td\ty\t\217\t\244\t\272\t\317\t\345\t"
308 "\373\n\21\n'\n=\nT\nj\n\201\n\230\n\256\n\305\n\334\n\363\13\13\13"
309 "\"\0139\13Q\13i\13\200\13\230\13\260\13\310\13\341\13\371\14\22\14"
310 "*\14C\14\\\14u\14\216\14\247\14\300\14\331\14\363\r\r\r&\r@\rZ\rt\r"
311 "\216\r\251\r\303\r\336\r\370\16\23\16.\16I\16d\16\177\16\233\16\266"
312 "\16\322\16\356\17\t\17%\17A\17^\17z\17\226\17\263\17\317\17\354\20"
313 "\t\20&\20C\20a\20~\20\233\20\271\20\327\20\365\21\23\0211\21O\21m\21"
314 "\214\21\252\21\311\21\350\22\7\22&\22E\22d\22\204\22\243\22\303\22"
315 "\343\23\3\23#\23C\23c\23\203\23\244\23\305\23\345\24\6\24'\24I\24j"
316 "\24\213\24\255\24\316\24\360\25\22\0254\25V\25x\25\233\25\275\25\340"
317 "\26\3\26&\26I\26l\26\217\26\262\26\326\26\372\27\35\27A\27e\27\211"
318 "\27\256\27\322\27\367\30\33\30@\30e\30\212\30\257\30\325\30\372\31"
319 " \31E\31k\31\221\31\267\31\335\32\4\32*\32Q\32w\32\236\32\305\32\354"
320 "\33\24\33;\33c\33\212\33\262\33\332\34\2\34*\34R\34{\34\243\34\314"
321 "\34\365\35\36\35G\35p\35\231\35\303\35\354\36\26\36@\36j\36\224\36"
322 "\276\36\351\37\23\37>\37i\37\224\37\277\37\352 \25 A l \230 \304 \360"
323 "!\34!H!u!\241!\316!\373\"'\"U\"\202\"\257\"\335#\n#8#f#\224#\302#\360"
324 "$\37$M$|$\253$\332%\t%8%h%\227%\307%\367&'&W&\207&\267&\350'\30'I'"
325 "z'\253'\334(\r(?(q(\242(\324)\6)8)k)\235)\320*\2*5*h*\233*\317+\2+"
326 "6+i+\235+\321,\5,9,n,\242,\327-\14-A-v-\253-\341.\26.L.\202.\267.\356"
327 "/$/Z/\221/\307/\376050l0\2440\3331\0221J1\2021\2721\3622*2c2\2332\324"
328 "3\r3F3\1773\2703\3614+4e4\2364\3305\0235M5\2075\3025\375676r6\2566"
329 "\3517$7`7\2347\3278\0248P8\2148\3109\0059B9\1779\2749\371:6:t:\262"
330 ":\357;-;k;\252;\350<'<e<\244<\343=\"=a=\241=\340> >`>\240>\340?!?a"
331 "?\242?\342@#@d@\246@\347A)AjA\254A\356B0BrB\265B\367C:C}C\300D\3DG"
332 "D\212D\316E\22EUE\232E\336F\"FgF\253F\360G5G{G\300H\5HKH\221H\327I"
333 "\35IcI\251I\360J7J}J\304K\14KSK\232K\342L*LrL\272M\2MJM\223M\334N%"
334 "NnN\267O\0OIO\223O\335P'PqP\273Q\6QPQ\233Q\346R1R|R\307S\23S_S\252"
335 "S\366TBT\217T\333U(UuU\302V\17V\\V\251V\367WDW\222W\340X/X}X\313Y\32"
336 "YiY\270Z\7ZVZ\246Z\365[E[\225[\345\\5\\\206\\\326]']x]\311^\32^l^\275"
337 "_\17_a_\263`\5`W`\252`\374aOa\242a\365bIb\234b\360cCc\227c\353d@d\224"
338 "d\351e=e\222e\347f=f\222f\350g=g\223g\351h?h\226h\354iCi\232i\361j"
339 "Hj\237j\367kOk\247k\377lWl\257m\10m`m\271n\22nkn\304o\36oxo\321p+p"
340 "\206p\340q:q\225q\360rKr\246s\1s]s\270t\24tpt\314u(u\205u\341v>v\233"
341 "v\370wVw\263x\21xnx\314y*y\211y\347zFz\245{\4{c{\302|!|\201|\341}A"
342 "}\241~\1~b~\302\177#\177\204\177\345\200G\200\250\201\n\201k\201\315"
343 "\2020\202\222\202\364\203W\203\272\204\35\204\200\204\343\205G\205"
344 "\253\206\16\206r\206\327\207;\207\237\210\4\210i\210\316\2113\211\231"
345 "\211\376\212d\212\312\2130\213\226\213\374\214c\214\312\2151\215\230"
346 "\215\377\216f\216\316\2176\217\236\220\6\220n\220\326\221?\221\250"
347 "\222\21\222z\222\343\223M\223\266\224 \224\212\224\364\225_\225\311"
348 "\2264\226\237\227\n\227u\227\340\230L\230\270\231$\231\220\231\374"
349 "\232h\232\325\233B\233\257\234\34\234\211\234\367\235d\235\322\236"
350 "@\236\256\237\35\237\213\237\372\240i\240\330\241G\241\266\242&\242"
351 "\226\243\6\243v\243\346\244V\244\307\2458\245\251\246\32\246\213\246"
352 "\375\247n\247\340\250R\250\304\2517\251\251\252\34\252\217\253\2\253"
353 "u\253\351\254\\\254\320\255D\255\270\256-\256\241\257\26\257\213\260"
354 "\0\260u\260\352\261`\261\326\262K\262\302\2638\263\256\264%\264\234"
355 "\265\23\265\212\266\1\266y\266\360\267h\267\340\270Y\270\321\271J\271"
356 "\302\272;\272\265\273.\273\247\274!\274\233\275\25\275\217\276\n\276"
357 "\204\276\377\277z\277\365\300p\300\354\301g\301\343\302_\302\333\303"
358 "X\303\324\304Q\304\316\305K\305\310\306F\306\303\307A\307\277\310="
359 "\310\274\311:\311\271\3128\312\267\3136\313\266\3145\314\265\3155\315"
360 "\265\3166\316\266\3177\317\270\3209\320\272\321<\321\276\322?\322\301"
361 "\323D\323\306\324I\324\313\325N\325\321\326U\326\330\327\\\327\340"
362 "\330d\330\350\331l\331\361\332v\332\373\333\200\334\5\334\212\335\20"
363 "\335\226\336\34\336\242\337)\337\257\3406\340\275\341D\341\314\342"
364 "S\342\333\343c\343\353\344s\344\374\345\204\346\r\346\226\347\37\347"
365 "\251\3502\350\274\351F\351\320\352[\352\345\353p\353\373\354\206\355"
366 "\21\355\234\356(\356\264\357@\357\314\360X\360\345\361r\361\377\362"
367 "\214\363\31\363\247\3644\364\302\365P\365\336\366m\366\373\367\212"
368 "\370\31\370\250\3718\371\307\372W\372\347\373w\374\7\374\230\375)\375"
369 "\272\376K\376\334\377m\377\377";
370 const size_t kProfileLength = 3212;
371 static_assert(kProfileLength == sizeof(kProfile) - 1, "");
372 return SkData::MakeWithoutCopy(kProfile, kProfileLength);
halcanary78daeff2016-04-07 12:34:59 -0700373}
374
375static sk_sp<SkPDFStream> make_srgb_color_profile() {
halcanaryfe8f0e02016-07-27 14:14:04 -0700376 sk_sp<SkPDFStream> stream = sk_make_sp<SkPDFStream>(SkSrgbIcm());
halcanaryfa251062016-07-29 10:13:18 -0700377 stream->dict()->insertInt("N", 3);
halcanary78daeff2016-04-07 12:34:59 -0700378 sk_sp<SkPDFArray> array = sk_make_sp<SkPDFArray>();
379 array->appendScalar(0.0f);
380 array->appendScalar(1.0f);
381 array->appendScalar(0.0f);
382 array->appendScalar(1.0f);
383 array->appendScalar(0.0f);
384 array->appendScalar(1.0f);
halcanaryfa251062016-07-29 10:13:18 -0700385 stream->dict()->insertObject("Range", std::move(array));
halcanary78daeff2016-04-07 12:34:59 -0700386 return stream;
387}
halcanary488165e2016-04-22 06:10:21 -0700388
389static sk_sp<SkPDFArray> make_srgb_output_intents() {
390 // sRGB is specified by HTML, CSS, and SVG.
391 auto outputIntent = sk_make_sp<SkPDFDict>("OutputIntent");
392 outputIntent->insertName("S", "GTS_PDFA1");
393 outputIntent->insertString("RegistryName", "http://www.color.org");
394 outputIntent->insertString("OutputConditionIdentifier",
395 "Custom");
396 outputIntent->insertString("Info","sRGB IEC61966-2.1");
397 outputIntent->insertObjRef("DestOutputProfile",
398 make_srgb_color_profile());
399 auto intentArray = sk_make_sp<SkPDFArray>();
400 intentArray->appendObject(std::move(outputIntent));
401 return intentArray;
402}
halcanary78daeff2016-04-07 12:34:59 -0700403
reedd14df7c2016-09-22 14:12:46 -0700404void SkPDFDocument::onClose(SkWStream* stream) {
halcanary50e82e62016-03-21 13:45:05 -0700405 SkASSERT(!fCanvas.get());
halcanarycc77c122016-03-23 06:26:31 -0700406 if (fPages.empty()) {
halcanary530032a2016-08-18 14:22:52 -0700407 this->reset();
reedd14df7c2016-09-22 14:12:46 -0700408 return;
halcanary50e82e62016-03-21 13:45:05 -0700409 }
halcanary50e82e62016-03-21 13:45:05 -0700410 auto docCatalog = sk_make_sp<SkPDFDict>("Catalog");
Mike Reeda4daf192017-12-14 13:25:04 -0500411 if (fMetadata.fPDFA) {
halcanary8cd4a242016-04-07 08:56:15 -0700412 SkASSERT(fXMP);
413 docCatalog->insertObjRef("Metadata", fXMP);
halcanary50e82e62016-03-21 13:45:05 -0700414 // Don't specify OutputIntents if we are not in PDF/A mode since
415 // no one has ever asked for this feature.
halcanary488165e2016-04-22 06:10:21 -0700416 docCatalog->insertObject("OutputIntents", make_srgb_output_intents());
417 }
418 SkASSERT(!fPages.empty());
halcanarycc77c122016-03-23 06:26:31 -0700419 docCatalog->insertObjRef("Pages", generate_page_tree(&fPages));
halcanary488165e2016-04-22 06:10:21 -0700420 SkASSERT(fPages.empty());
halcanary50e82e62016-03-21 13:45:05 -0700421
halcanarycc77c122016-03-23 06:26:31 -0700422 if (fDests->size() > 0) {
423 docCatalog->insertObjRef("Dests", std::move(fDests));
reed@google.com99ac02b2013-06-07 20:30:16 +0000424 }
425
halcanary50e82e62016-03-21 13:45:05 -0700426 // Build font subsetting info before calling addObjectRecursively().
halcanary530032a2016-08-18 14:22:52 -0700427 SkPDFCanon* canon = &fCanon;
428 fFonts.foreach([canon](SkPDFFont* p){ p->getFontSubset(canon); });
halcanary50e82e62016-03-21 13:45:05 -0700429 fObjectSerializer.addObjectRecursively(docCatalog);
430 fObjectSerializer.serializeObjects(this->getStream());
halcanary488165e2016-04-22 06:10:21 -0700431 fObjectSerializer.serializeFooter(this->getStream(), docCatalog, fID);
halcanary530032a2016-08-18 14:22:52 -0700432 this->reset();
halcanary50e82e62016-03-21 13:45:05 -0700433}
434
reed@google.com99ac02b2013-06-07 20:30:16 +0000435///////////////////////////////////////////////////////////////////////////////
436
halcanary23f4d4d2016-03-12 05:59:39 -0800437sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
Mike Reed2e3f2e92017-12-15 20:43:03 +0000438 const SkDocument::PDFMetadata& metadata) {
Mike Reeda4daf192017-12-14 13:25:04 -0500439 SkDocument::PDFMetadata meta = metadata;
440 if (meta.fRasterDPI <= 0) {
441 meta.fRasterDPI = 72.0f;
Hal Canarya0622582017-06-29 18:51:35 -0400442 }
Mike Reeda4daf192017-12-14 13:25:04 -0500443 if (meta.fEncodingQuality < 0) {
444 meta.fEncodingQuality = 0;
445 }
Hal Canaryc5980d02018-01-08 15:02:36 -0500446 return stream ? sk_make_sp<SkPDFDocument>(stream, meta) : nullptr;
halcanary23f4d4d2016-03-12 05:59:39 -0800447}
448
Mike Reeda4daf192017-12-14 13:25:04 -0500449sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream, const PDFMetadata& metadata) {
Hal Canaryc5980d02018-01-08 15:02:36 -0500450 return SkPDFMakeDocument(stream, metadata);
Mike Reeda4daf192017-12-14 13:25:04 -0500451}
452
453sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream) {
Hal Canaryc5980d02018-01-08 15:02:36 -0500454 return SkPDFMakeDocument(stream, PDFMetadata());
Mike Reeda4daf192017-12-14 13:25:04 -0500455}
456