blob: dd68b5bb3996a74fb32e16d7388adfc432d972d8 [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
halcanary99e22fb2016-05-26 08:31:06 -07008#include "SkColorSpace_Base.h"
halcanary66be6262016-03-21 13:01:34 -07009#include "SkPDFCanvas.h"
reed58677362014-10-09 05:30:10 -070010#include "SkPDFDevice.h"
halcanary23f4d4d2016-03-12 05:59:39 -080011#include "SkPDFDocument.h"
halcanaryf12a1672015-09-23 12:45:49 -070012#include "SkPDFUtils.h"
halcanarya43b4152015-03-25 12:15:04 -070013#include "SkStream.h"
halcanaryf12a1672015-09-23 12:45:49 -070014
halcanary50e82e62016-03-21 13:45:05 -070015SkPDFObjectSerializer::SkPDFObjectSerializer() : fBaseOffset(0), fNextToBeSerialized(0) {}
16
17template <class T> static void renew(T* t) { t->~T(); new (t) T; }
18
halcanarya50151d2016-03-25 11:57:49 -070019SkPDFObjectSerializer::~SkPDFObjectSerializer() {
20 for (int i = 0; i < fObjNumMap.objects().count(); ++i) {
21 fObjNumMap.objects()[i]->drop();
22 }
23}
24
halcanary50e82e62016-03-21 13:45:05 -070025void SkPDFObjectSerializer::addObjectRecursively(const sk_sp<SkPDFObject>& object) {
26 fObjNumMap.addObjectRecursively(object.get(), fSubstituteMap);
halcanarya43b4152015-03-25 12:15:04 -070027}
28
halcanaryad5dcd12016-03-31 06:59:00 -070029#define SKPDF_MAGIC "\xD3\xEB\xE9\xE1"
30#ifndef SK_BUILD_FOR_WIN32
31static_assert((SKPDF_MAGIC[0] & 0x7F) == "Skia"[0], "");
32static_assert((SKPDF_MAGIC[1] & 0x7F) == "Skia"[1], "");
33static_assert((SKPDF_MAGIC[2] & 0x7F) == "Skia"[2], "");
34static_assert((SKPDF_MAGIC[3] & 0x7F) == "Skia"[3], "");
35#endif
halcanary4b656662016-04-27 07:45:18 -070036void SkPDFObjectSerializer::serializeHeader(SkWStream* wStream,
37 const SkDocument::PDFMetadata& md) {
halcanary50e82e62016-03-21 13:45:05 -070038 fBaseOffset = wStream->bytesWritten();
halcanaryad5dcd12016-03-31 06:59:00 -070039 static const char kHeader[] = "%PDF-1.4\n%" SKPDF_MAGIC "\n";
halcanary50e82e62016-03-21 13:45:05 -070040 wStream->write(kHeader, strlen(kHeader));
41 // The PDF spec recommends including a comment with four
42 // bytes, all with their high bits set. "\xD3\xEB\xE9\xE1" is
43 // "Skia" with the high bits set.
halcanary4b656662016-04-27 07:45:18 -070044 fInfoDict = SkPDFMetadata::MakeDocumentInformationDict(md);
halcanary50e82e62016-03-21 13:45:05 -070045 this->addObjectRecursively(fInfoDict);
46 this->serializeObjects(wStream);
47}
halcanaryad5dcd12016-03-31 06:59:00 -070048#undef SKPDF_MAGIC
halcanary50e82e62016-03-21 13:45:05 -070049
50// Serialize all objects in the fObjNumMap that have not yet been serialized;
51void SkPDFObjectSerializer::serializeObjects(SkWStream* wStream) {
52 const SkTArray<sk_sp<SkPDFObject>>& objects = fObjNumMap.objects();
53 while (fNextToBeSerialized < objects.count()) {
54 SkPDFObject* object = objects[fNextToBeSerialized].get();
55 int32_t index = fNextToBeSerialized + 1; // Skip object 0.
56 // "The first entry in the [XREF] table (object number 0) is
57 // always free and has a generation number of 65,535; it is
58 // the head of the linked list of free objects."
59 SkASSERT(fOffsets.count() == fNextToBeSerialized);
60 fOffsets.push(this->offset(wStream));
61 SkASSERT(object == fSubstituteMap.getSubstitute(object));
62 wStream->writeDecAsText(index);
63 wStream->writeText(" 0 obj\n"); // Generation number is always 0.
64 object->emitObject(wStream, fObjNumMap, fSubstituteMap);
65 wStream->writeText("\nendobj\n");
66 object->drop();
67 ++fNextToBeSerialized;
68 }
69}
70
71// Xref table and footer
72void SkPDFObjectSerializer::serializeFooter(SkWStream* wStream,
73 const sk_sp<SkPDFObject> docCatalog,
74 sk_sp<SkPDFObject> id) {
75 this->serializeObjects(wStream);
76 int32_t xRefFileOffset = this->offset(wStream);
77 // Include the special zeroth object in the count.
78 int32_t objCount = SkToS32(fOffsets.count() + 1);
79 wStream->writeText("xref\n0 ");
80 wStream->writeDecAsText(objCount);
81 wStream->writeText("\n0000000000 65535 f \n");
82 for (int i = 0; i < fOffsets.count(); i++) {
83 wStream->writeBigDecAsText(fOffsets[i], 10);
84 wStream->writeText(" 00000 n \n");
85 }
halcanarya43b4152015-03-25 12:15:04 -070086 SkPDFDict trailerDict;
halcanary50e82e62016-03-21 13:45:05 -070087 trailerDict.insertInt("Size", objCount);
88 SkASSERT(docCatalog);
89 trailerDict.insertObjRef("Root", docCatalog);
90 SkASSERT(fInfoDict);
91 trailerDict.insertObjRef("Info", std::move(fInfoDict));
halcanary34422612015-10-12 10:11:18 -070092 if (id) {
halcanary8103a342016-03-08 15:10:16 -080093 trailerDict.insertObject("ID", std::move(id));
halcanary34422612015-10-12 10:11:18 -070094 }
halcanary50e82e62016-03-21 13:45:05 -070095 wStream->writeText("trailer\n");
96 trailerDict.emitObject(wStream, fObjNumMap, fSubstituteMap);
97 wStream->writeText("\nstartxref\n");
98 wStream->writeBigDecAsText(xRefFileOffset);
99 wStream->writeText("\n%%EOF");
100}
101
102int32_t SkPDFObjectSerializer::offset(SkWStream* wStream) {
103 size_t offset = wStream->bytesWritten();
104 SkASSERT(offset > fBaseOffset);
105 return SkToS32(offset - fBaseOffset);
halcanarya43b4152015-03-25 12:15:04 -0700106}
107
halcanary2f7ebcb2015-03-25 12:45:28 -0700108
halcanary8103a342016-03-08 15:10:16 -0800109// return root node.
halcanarycc77c122016-03-23 06:26:31 -0700110static sk_sp<SkPDFDict> generate_page_tree(SkTArray<sk_sp<SkPDFDict>>* pages) {
halcanary2f7ebcb2015-03-25 12:45:28 -0700111 // PDF wants a tree describing all the pages in the document. We arbitrary
112 // choose 8 (kNodeSize) as the number of allowed children. The internal
113 // nodes have type "Pages" with an array of children, a parent pointer, and
114 // the number of leaves below the node as "Count." The leaves are passed
115 // into the method, have type "Page" and need a parent pointer. This method
116 // builds the tree bottom up, skipping internal nodes that would have only
117 // one child.
118 static const int kNodeSize = 8;
119
halcanary2f7ebcb2015-03-25 12:45:28 -0700120 // curNodes takes a reference to its items, which it passes to pageTree.
halcanarycc77c122016-03-23 06:26:31 -0700121 int totalPageCount = pages->count();
122 SkTArray<sk_sp<SkPDFDict>> curNodes;
123 curNodes.swap(pages);
halcanary2f7ebcb2015-03-25 12:45:28 -0700124
125 // nextRoundNodes passes its references to nodes on to curNodes.
halcanary2f7ebcb2015-03-25 12:45:28 -0700126 int treeCapacity = kNodeSize;
127 do {
halcanarycc77c122016-03-23 06:26:31 -0700128 SkTArray<sk_sp<SkPDFDict>> nextRoundNodes;
halcanary2f7ebcb2015-03-25 12:45:28 -0700129 for (int i = 0; i < curNodes.count(); ) {
130 if (i > 0 && i + 1 == curNodes.count()) {
halcanarycc77c122016-03-23 06:26:31 -0700131 SkASSERT(curNodes[i]);
132 nextRoundNodes.emplace_back(std::move(curNodes[i]));
halcanary2f7ebcb2015-03-25 12:45:28 -0700133 break;
134 }
135
halcanaryece83922016-03-08 08:32:12 -0800136 auto newNode = sk_make_sp<SkPDFDict>("Pages");
137 auto kids = sk_make_sp<SkPDFArray>();
halcanary2f7ebcb2015-03-25 12:45:28 -0700138 kids->reserve(kNodeSize);
139
140 int count = 0;
141 for (; i < curNodes.count() && count < kNodeSize; i++, count++) {
halcanarycc77c122016-03-23 06:26:31 -0700142 SkASSERT(curNodes[i]);
halcanary8103a342016-03-08 15:10:16 -0800143 curNodes[i]->insertObjRef("Parent", newNode);
halcanarycc77c122016-03-23 06:26:31 -0700144 kids->appendObjRef(std::move(curNodes[i]));
halcanary2f7ebcb2015-03-25 12:45:28 -0700145 }
146
147 // treeCapacity is the number of leaf nodes possible for the
148 // current set of subtrees being generated. (i.e. 8, 64, 512, ...).
149 // It is hard to count the number of leaf nodes in the current
150 // subtree. However, by construction, we know that unless it's the
151 // last subtree for the current depth, the leaf count will be
152 // treeCapacity, otherwise it's what ever is left over after
153 // consuming treeCapacity chunks.
154 int pageCount = treeCapacity;
155 if (i == curNodes.count()) {
halcanarycc77c122016-03-23 06:26:31 -0700156 pageCount = ((totalPageCount - 1) % treeCapacity) + 1;
halcanary2f7ebcb2015-03-25 12:45:28 -0700157 }
halcanary72266fd2015-05-05 08:00:24 -0700158 newNode->insertInt("Count", pageCount);
halcanary8103a342016-03-08 15:10:16 -0800159 newNode->insertObject("Kids", std::move(kids));
halcanarycc77c122016-03-23 06:26:31 -0700160 nextRoundNodes.emplace_back(std::move(newNode));
halcanary2f7ebcb2015-03-25 12:45:28 -0700161 }
halcanarycc77c122016-03-23 06:26:31 -0700162 SkDEBUGCODE( for (const auto& n : curNodes) { SkASSERT(!n); } );
halcanary2f7ebcb2015-03-25 12:45:28 -0700163
halcanarycc77c122016-03-23 06:26:31 -0700164 curNodes.swap(&nextRoundNodes);
165 nextRoundNodes.reset();
halcanary2f7ebcb2015-03-25 12:45:28 -0700166 treeCapacity *= kNodeSize;
167 } while (curNodes.count() > 1);
halcanarycc77c122016-03-23 06:26:31 -0700168 return std::move(curNodes[0]);
halcanary2f7ebcb2015-03-25 12:45:28 -0700169}
170
halcanarya43b4152015-03-25 12:15:04 -0700171#if 0
172// TODO(halcanary): expose notEmbeddableCount in SkDocument
173void GetCountOfFontTypes(
174 const SkTDArray<SkPDFDevice*>& pageDevices,
175 int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1],
176 int* notSubsettableCount,
177 int* notEmbeddableCount) {
178 sk_bzero(counts, sizeof(int) *
179 (SkAdvancedTypefaceMetrics::kOther_Font + 1));
180 SkTDArray<SkFontID> seenFonts;
181 int notSubsettable = 0;
182 int notEmbeddable = 0;
183
184 for (int pageNumber = 0; pageNumber < pageDevices.count(); pageNumber++) {
185 const SkTDArray<SkPDFFont*>& fontResources =
186 pageDevices[pageNumber]->getFontResources();
187 for (int font = 0; font < fontResources.count(); font++) {
188 SkFontID fontID = fontResources[font]->typeface()->uniqueID();
189 if (seenFonts.find(fontID) == -1) {
190 counts[fontResources[font]->getType()]++;
191 seenFonts.push(fontID);
192 if (!fontResources[font]->canSubset()) {
193 notSubsettable++;
194 }
195 if (!fontResources[font]->canEmbed()) {
196 notEmbeddable++;
197 }
198 }
199 }
200 }
201 if (notSubsettableCount) {
202 *notSubsettableCount = notSubsettable;
203
204 }
205 if (notEmbeddableCount) {
206 *notEmbeddableCount = notEmbeddable;
207 }
208}
209#endif
halcanaryf12a1672015-09-23 12:45:49 -0700210
211template <typename T> static T* clone(const T* o) { return o ? new T(*o) : nullptr; }
halcanarya43b4152015-03-25 12:15:04 -0700212////////////////////////////////////////////////////////////////////////////////
reed@google.com99ac02b2013-06-07 20:30:16 +0000213
halcanary50e82e62016-03-21 13:45:05 -0700214SkPDFDocument::SkPDFDocument(SkWStream* stream,
215 void (*doneProc)(SkWStream*, bool),
216 SkScalar rasterDpi,
halcanary4b656662016-04-27 07:45:18 -0700217 const SkDocument::PDFMetadata& metadata,
218 sk_sp<SkPixelSerializer> jpegEncoder,
halcanary488165e2016-04-22 06:10:21 -0700219 bool pdfa)
halcanary50e82e62016-03-21 13:45:05 -0700220 : SkDocument(stream, doneProc)
halcanary488165e2016-04-22 06:10:21 -0700221 , fRasterDpi(rasterDpi)
halcanary4b656662016-04-27 07:45:18 -0700222 , fMetadata(metadata)
halcanary488165e2016-04-22 06:10:21 -0700223 , fPDFA(pdfa) {
halcanary4b656662016-04-27 07:45:18 -0700224 fCanon.setPixelSerializer(std::move(jpegEncoder));
halcanary50e82e62016-03-21 13:45:05 -0700225}
226
227SkPDFDocument::~SkPDFDocument() {
228 // subclasses of SkDocument must call close() in their destructors.
229 this->close();
230}
231
232void SkPDFDocument::serialize(const sk_sp<SkPDFObject>& object) {
233 fObjectSerializer.addObjectRecursively(object);
234 fObjectSerializer.serializeObjects(this->getStream());
235}
236
237SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height,
238 const SkRect& trimBox) {
239 SkASSERT(!fCanvas.get()); // endPage() was called before this.
halcanarycc77c122016-03-23 06:26:31 -0700240 if (fPages.empty()) {
halcanary50e82e62016-03-21 13:45:05 -0700241 // if this is the first page if the document.
242 fObjectSerializer.serializeHeader(this->getStream(), fMetadata);
halcanarycc77c122016-03-23 06:26:31 -0700243 fDests = sk_make_sp<SkPDFDict>();
halcanary488165e2016-04-22 06:10:21 -0700244 if (fPDFA) {
halcanary4b656662016-04-27 07:45:18 -0700245 SkPDFMetadata::UUID uuid = SkPDFMetadata::CreateUUID(fMetadata);
halcanary8cd4a242016-04-07 08:56:15 -0700246 // We use the same UUID for Document ID and Instance ID since this
247 // is the first revision of this document (and Skia does not
248 // support revising existing PDF documents).
249 // If we are not in PDF/A mode, don't use a UUID since testing
250 // works best with reproducible outputs.
halcanary4b656662016-04-27 07:45:18 -0700251 fID = SkPDFMetadata::MakePdfId(uuid, uuid);
252 fXMP = SkPDFMetadata::MakeXMPObject(fMetadata, uuid, uuid);
halcanary8cd4a242016-04-07 08:56:15 -0700253 fObjectSerializer.addObjectRecursively(fXMP);
254 fObjectSerializer.serializeObjects(this->getStream());
halcanary488165e2016-04-22 06:10:21 -0700255 }
halcanary712fdf72015-12-10 08:59:43 -0800256 }
halcanary50e82e62016-03-21 13:45:05 -0700257 SkISize pageSize = SkISize::Make(
258 SkScalarRoundToInt(width), SkScalarRoundToInt(height));
halcanarycc77c122016-03-23 06:26:31 -0700259 fPageDevice.reset(
halcanary989da4a2016-03-21 14:33:17 -0700260 SkPDFDevice::Create(pageSize, fRasterDpi, this));
halcanarycc77c122016-03-23 06:26:31 -0700261 fCanvas = sk_make_sp<SkPDFCanvas>(fPageDevice);
halcanary50e82e62016-03-21 13:45:05 -0700262 fCanvas->clipRect(trimBox);
263 fCanvas->translate(trimBox.x(), trimBox.y());
264 return fCanvas.get();
265}
skia.committer@gmail.com63193672013-06-08 07:01:13 +0000266
halcanary50e82e62016-03-21 13:45:05 -0700267void SkPDFDocument::onEndPage() {
268 SkASSERT(fCanvas.get());
269 fCanvas->flush();
270 fCanvas.reset(nullptr);
halcanarycc77c122016-03-23 06:26:31 -0700271 SkASSERT(fPageDevice);
halcanarycc77c122016-03-23 06:26:31 -0700272 auto page = sk_make_sp<SkPDFDict>("Page");
273 page->insertObject("Resources", fPageDevice->makeResourceDict());
274 page->insertObject("MediaBox", fPageDevice->copyMediaBox());
275 auto annotations = sk_make_sp<SkPDFArray>();
276 fPageDevice->appendAnnotations(annotations.get());
277 if (annotations->size() > 0) {
278 page->insertObject("Annots", std::move(annotations));
279 }
halcanaryac0e00d2016-07-27 11:12:23 -0700280 auto contentObject = sk_make_sp<SkPDFStream>(fPageDevice->content());
halcanarycc77c122016-03-23 06:26:31 -0700281 this->serialize(contentObject);
282 page->insertObjRef("Contents", std::move(contentObject));
283 fPageDevice->appendDestinations(fDests.get(), page.get());
284 fPages.emplace_back(std::move(page));
285 fPageDevice.reset(nullptr);
halcanary50e82e62016-03-21 13:45:05 -0700286}
reed@google.com99ac02b2013-06-07 20:30:16 +0000287
halcanary50e82e62016-03-21 13:45:05 -0700288void SkPDFDocument::onAbort() {
289 fCanvas.reset(nullptr);
halcanarycc77c122016-03-23 06:26:31 -0700290 fPages.reset();
halcanary50e82e62016-03-21 13:45:05 -0700291 fCanon.reset();
292 renew(&fObjectSerializer);
halcanary3c35fb32016-06-30 11:55:07 -0700293 renew(&fGlyphUsage);
halcanary50e82e62016-03-21 13:45:05 -0700294}
reed@google.com99ac02b2013-06-07 20:30:16 +0000295
halcanary4b656662016-04-27 07:45:18 -0700296#ifdef SK_SUPPORT_LEGACY_DOCUMENT_API
halcanary50e82e62016-03-21 13:45:05 -0700297void SkPDFDocument::setMetadata(const SkDocument::Attribute info[],
298 int infoCount,
299 const SkTime::DateTime* creationDate,
300 const SkTime::DateTime* modifiedDate) {
halcanary4b656662016-04-27 07:45:18 -0700301 for (int i = 0; i < infoCount; ++i) {
302 const SkDocument::Attribute& kv = info[i];
303 SkPDFMetadata::SetMetadataByKey(kv.fKey, kv.fValue, &fMetadata);
304 }
305 if (creationDate) {
306 fMetadata.fCreation.fEnabled = true;
307 fMetadata.fCreation.fDateTime = *creationDate;
308 }
309 if (modifiedDate) {
310 fMetadata.fModified.fEnabled = true;
311 fMetadata.fModified.fDateTime = *modifiedDate;
312 }
halcanary50e82e62016-03-21 13:45:05 -0700313}
halcanary4b656662016-04-27 07:45:18 -0700314#endif // SK_SUPPORT_LEGACY_DOCUMENT_API
reed@google.com99ac02b2013-06-07 20:30:16 +0000315
halcanary78daeff2016-04-07 12:34:59 -0700316static sk_sp<SkData> SkSrgbIcm() {
halcanary99e22fb2016-05-26 08:31:06 -0700317 auto srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
318 return as_CSB(srgb)->writeToICC();
halcanary78daeff2016-04-07 12:34:59 -0700319}
320
321static sk_sp<SkPDFStream> make_srgb_color_profile() {
halcanaryfe8f0e02016-07-27 14:14:04 -0700322 sk_sp<SkPDFStream> stream = sk_make_sp<SkPDFStream>(SkSrgbIcm());
halcanaryfa251062016-07-29 10:13:18 -0700323 stream->dict()->insertInt("N", 3);
halcanary78daeff2016-04-07 12:34:59 -0700324 sk_sp<SkPDFArray> array = sk_make_sp<SkPDFArray>();
325 array->appendScalar(0.0f);
326 array->appendScalar(1.0f);
327 array->appendScalar(0.0f);
328 array->appendScalar(1.0f);
329 array->appendScalar(0.0f);
330 array->appendScalar(1.0f);
halcanaryfa251062016-07-29 10:13:18 -0700331 stream->dict()->insertObject("Range", std::move(array));
halcanary78daeff2016-04-07 12:34:59 -0700332 return stream;
333}
halcanary488165e2016-04-22 06:10:21 -0700334
335static sk_sp<SkPDFArray> make_srgb_output_intents() {
336 // sRGB is specified by HTML, CSS, and SVG.
337 auto outputIntent = sk_make_sp<SkPDFDict>("OutputIntent");
338 outputIntent->insertName("S", "GTS_PDFA1");
339 outputIntent->insertString("RegistryName", "http://www.color.org");
340 outputIntent->insertString("OutputConditionIdentifier",
341 "Custom");
342 outputIntent->insertString("Info","sRGB IEC61966-2.1");
343 outputIntent->insertObjRef("DestOutputProfile",
344 make_srgb_color_profile());
345 auto intentArray = sk_make_sp<SkPDFArray>();
346 intentArray->appendObject(std::move(outputIntent));
347 return intentArray;
348}
halcanary78daeff2016-04-07 12:34:59 -0700349
halcanary50e82e62016-03-21 13:45:05 -0700350bool SkPDFDocument::onClose(SkWStream* stream) {
351 SkASSERT(!fCanvas.get());
halcanarycc77c122016-03-23 06:26:31 -0700352 if (fPages.empty()) {
353 fPages.reset();
halcanary2e3f9d82015-02-27 12:41:03 -0800354 fCanon.reset();
halcanary50e82e62016-03-21 13:45:05 -0700355 renew(&fObjectSerializer);
halcanary3c35fb32016-06-30 11:55:07 -0700356 renew(&fGlyphUsage);
halcanary50e82e62016-03-21 13:45:05 -0700357 return false;
358 }
halcanary50e82e62016-03-21 13:45:05 -0700359 auto docCatalog = sk_make_sp<SkPDFDict>("Catalog");
halcanary488165e2016-04-22 06:10:21 -0700360 if (fPDFA) {
halcanary8cd4a242016-04-07 08:56:15 -0700361 SkASSERT(fXMP);
362 docCatalog->insertObjRef("Metadata", fXMP);
halcanary50e82e62016-03-21 13:45:05 -0700363 // Don't specify OutputIntents if we are not in PDF/A mode since
364 // no one has ever asked for this feature.
halcanary488165e2016-04-22 06:10:21 -0700365 docCatalog->insertObject("OutputIntents", make_srgb_output_intents());
366 }
367 SkASSERT(!fPages.empty());
halcanarycc77c122016-03-23 06:26:31 -0700368 docCatalog->insertObjRef("Pages", generate_page_tree(&fPages));
halcanary488165e2016-04-22 06:10:21 -0700369 SkASSERT(fPages.empty());
halcanary50e82e62016-03-21 13:45:05 -0700370
halcanarycc77c122016-03-23 06:26:31 -0700371 if (fDests->size() > 0) {
372 docCatalog->insertObjRef("Dests", std::move(fDests));
reed@google.com99ac02b2013-06-07 20:30:16 +0000373 }
374
halcanary50e82e62016-03-21 13:45:05 -0700375 // Build font subsetting info before calling addObjectRecursively().
halcanarycc77c122016-03-23 06:26:31 -0700376 for (const auto& entry : fGlyphUsage) {
377 sk_sp<SkPDFFont> subsetFont(
halcanary3c35fb32016-06-30 11:55:07 -0700378 entry.fFont->getFontSubset(&entry.fGlyphSet));
halcanarycc77c122016-03-23 06:26:31 -0700379 if (subsetFont) {
380 fObjectSerializer.fSubstituteMap.setSubstitute(
381 entry.fFont, subsetFont.get());
382 }
383 }
halcanaryf12a1672015-09-23 12:45:49 -0700384
halcanary50e82e62016-03-21 13:45:05 -0700385 fObjectSerializer.addObjectRecursively(docCatalog);
386 fObjectSerializer.serializeObjects(this->getStream());
halcanary488165e2016-04-22 06:10:21 -0700387 fObjectSerializer.serializeFooter(this->getStream(), docCatalog, fID);
halcanary50e82e62016-03-21 13:45:05 -0700388 fCanon.reset();
389 renew(&fObjectSerializer);
halcanary3c35fb32016-06-30 11:55:07 -0700390 renew(&fGlyphUsage);
halcanary50e82e62016-03-21 13:45:05 -0700391 return true;
392}
393
reed@google.com99ac02b2013-06-07 20:30:16 +0000394///////////////////////////////////////////////////////////////////////////////
395
halcanary23f4d4d2016-03-12 05:59:39 -0800396sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
397 void (*proc)(SkWStream*, bool),
398 SkScalar dpi,
halcanary4b656662016-04-27 07:45:18 -0700399 const SkDocument::PDFMetadata& metadata,
400 sk_sp<SkPixelSerializer> jpeg,
halcanary488165e2016-04-22 06:10:21 -0700401 bool pdfa) {
halcanary4b656662016-04-27 07:45:18 -0700402 return stream ? sk_make_sp<SkPDFDocument>(stream, proc, dpi, metadata,
403 std::move(jpeg), pdfa)
halcanary488165e2016-04-22 06:10:21 -0700404 : nullptr;
halcanary23f4d4d2016-03-12 05:59:39 -0800405}
406
halcanary4b656662016-04-27 07:45:18 -0700407sk_sp<SkDocument> SkDocument::MakePDF(const char path[], SkScalar dpi) {
halcanary385fe4d2015-08-26 13:07:48 -0700408 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; };
halcanaryb8fb9932016-03-28 07:58:30 -0700409 std::unique_ptr<SkFILEWStream> stream(new SkFILEWStream(path));
halcanary23f4d4d2016-03-12 05:59:39 -0800410 return stream->isValid()
halcanary4b656662016-04-27 07:45:18 -0700411 ? SkPDFMakeDocument(stream.release(), delete_wstream, dpi,
412 SkDocument::PDFMetadata(), nullptr,
413 false)
414 : nullptr;
415}
416
417sk_sp<SkDocument> SkDocument::MakePDF(SkWStream* stream,
418 SkScalar dpi,
419 const SkDocument::PDFMetadata& metadata,
420 sk_sp<SkPixelSerializer> jpegEncoder,
421 bool pdfa) {
422 return SkPDFMakeDocument(stream, nullptr, dpi, metadata,
423 std::move(jpegEncoder), pdfa);
reed@google.com99ac02b2013-06-07 20:30:16 +0000424}