reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 1 | /* |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 8 | #include "SkPDFCanon.h" |
halcanary | 66be626 | 2016-03-21 13:01:34 -0700 | [diff] [blame] | 9 | #include "SkPDFCanvas.h" |
reed | 5867736 | 2014-10-09 05:30:10 -0700 | [diff] [blame] | 10 | #include "SkPDFDevice.h" |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 11 | #include "SkPDFDocument.h" |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 12 | #include "SkPDFFont.h" |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 13 | #include "SkPDFStream.h" |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 14 | #include "SkPDFUtils.h" |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 15 | #include "SkStream.h" |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 16 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 17 | |
| 18 | SkPDFObjectSerializer::SkPDFObjectSerializer() : fBaseOffset(0), fNextToBeSerialized(0) {} |
| 19 | |
| 20 | template <class T> static void renew(T* t) { t->~T(); new (t) T; } |
| 21 | |
| 22 | void SkPDFObjectSerializer::addObjectRecursively(const sk_sp<SkPDFObject>& object) { |
| 23 | fObjNumMap.addObjectRecursively(object.get(), fSubstituteMap); |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 24 | } |
| 25 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 26 | void SkPDFObjectSerializer::serializeHeader(SkWStream* wStream, const SkPDFMetadata& md) { |
| 27 | fBaseOffset = wStream->bytesWritten(); |
| 28 | static const char kHeader[] = "%PDF-1.4\n%\xE1\xE9\xEB\xD3\n"; |
| 29 | wStream->write(kHeader, strlen(kHeader)); |
| 30 | // The PDF spec recommends including a comment with four |
| 31 | // bytes, all with their high bits set. "\xD3\xEB\xE9\xE1" is |
| 32 | // "Skia" with the high bits set. |
| 33 | fInfoDict.reset(md.createDocumentInformationDict()); |
| 34 | this->addObjectRecursively(fInfoDict); |
| 35 | this->serializeObjects(wStream); |
| 36 | } |
| 37 | |
| 38 | // Serialize all objects in the fObjNumMap that have not yet been serialized; |
| 39 | void SkPDFObjectSerializer::serializeObjects(SkWStream* wStream) { |
| 40 | const SkTArray<sk_sp<SkPDFObject>>& objects = fObjNumMap.objects(); |
| 41 | while (fNextToBeSerialized < objects.count()) { |
| 42 | SkPDFObject* object = objects[fNextToBeSerialized].get(); |
| 43 | int32_t index = fNextToBeSerialized + 1; // Skip object 0. |
| 44 | // "The first entry in the [XREF] table (object number 0) is |
| 45 | // always free and has a generation number of 65,535; it is |
| 46 | // the head of the linked list of free objects." |
| 47 | SkASSERT(fOffsets.count() == fNextToBeSerialized); |
| 48 | fOffsets.push(this->offset(wStream)); |
| 49 | SkASSERT(object == fSubstituteMap.getSubstitute(object)); |
| 50 | wStream->writeDecAsText(index); |
| 51 | wStream->writeText(" 0 obj\n"); // Generation number is always 0. |
| 52 | object->emitObject(wStream, fObjNumMap, fSubstituteMap); |
| 53 | wStream->writeText("\nendobj\n"); |
| 54 | object->drop(); |
| 55 | ++fNextToBeSerialized; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Xref table and footer |
| 60 | void SkPDFObjectSerializer::serializeFooter(SkWStream* wStream, |
| 61 | const sk_sp<SkPDFObject> docCatalog, |
| 62 | sk_sp<SkPDFObject> id) { |
| 63 | this->serializeObjects(wStream); |
| 64 | int32_t xRefFileOffset = this->offset(wStream); |
| 65 | // Include the special zeroth object in the count. |
| 66 | int32_t objCount = SkToS32(fOffsets.count() + 1); |
| 67 | wStream->writeText("xref\n0 "); |
| 68 | wStream->writeDecAsText(objCount); |
| 69 | wStream->writeText("\n0000000000 65535 f \n"); |
| 70 | for (int i = 0; i < fOffsets.count(); i++) { |
| 71 | wStream->writeBigDecAsText(fOffsets[i], 10); |
| 72 | wStream->writeText(" 00000 n \n"); |
| 73 | } |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 74 | SkPDFDict trailerDict; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 75 | trailerDict.insertInt("Size", objCount); |
| 76 | SkASSERT(docCatalog); |
| 77 | trailerDict.insertObjRef("Root", docCatalog); |
| 78 | SkASSERT(fInfoDict); |
| 79 | trailerDict.insertObjRef("Info", std::move(fInfoDict)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 80 | if (id) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 81 | trailerDict.insertObject("ID", std::move(id)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 82 | } |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 83 | wStream->writeText("trailer\n"); |
| 84 | trailerDict.emitObject(wStream, fObjNumMap, fSubstituteMap); |
| 85 | wStream->writeText("\nstartxref\n"); |
| 86 | wStream->writeBigDecAsText(xRefFileOffset); |
| 87 | wStream->writeText("\n%%EOF"); |
| 88 | } |
| 89 | |
| 90 | int32_t SkPDFObjectSerializer::offset(SkWStream* wStream) { |
| 91 | size_t offset = wStream->bytesWritten(); |
| 92 | SkASSERT(offset > fBaseOffset); |
| 93 | return SkToS32(offset - fBaseOffset); |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 94 | } |
| 95 | |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 96 | static void perform_font_subsetting( |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 97 | const SkTArray<sk_sp<const SkPDFDevice>>& pageDevices, |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 98 | SkPDFSubstituteMap* substituteMap) { |
| 99 | SkASSERT(substituteMap); |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 100 | |
| 101 | SkPDFGlyphSetMap usage; |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 102 | for (const sk_sp<const SkPDFDevice>& pageDevice : pageDevices) { |
| 103 | usage.merge(pageDevice->getFontGlyphUsage()); |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 104 | } |
| 105 | SkPDFGlyphSetMap::F2BIter iterator(usage); |
| 106 | const SkPDFGlyphSetMap::FontGlyphSetPair* entry = iterator.next(); |
| 107 | while (entry) { |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 108 | sk_sp<SkPDFFont> subsetFont( |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 109 | entry->fFont->getFontSubset(entry->fGlyphSet)); |
| 110 | if (subsetFont) { |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 111 | substituteMap->setSubstitute(entry->fFont, subsetFont.get()); |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 112 | } |
| 113 | entry = iterator.next(); |
| 114 | } |
| 115 | } |
| 116 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 117 | static sk_sp<SkPDFDict> create_pdf_page(const SkPDFDevice* pageDevice) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 118 | auto page = sk_make_sp<SkPDFDict>("Page"); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 119 | page->insertObject("Resources", pageDevice->makeResourceDict()); |
halcanary | 72266fd | 2015-05-05 08:00:24 -0700 | [diff] [blame] | 120 | page->insertObject("MediaBox", pageDevice->copyMediaBox()); |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 121 | auto annotations = sk_make_sp<SkPDFArray>(); |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 122 | pageDevice->appendAnnotations(annotations.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 123 | if (annotations->size() > 0) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 124 | page->insertObject("Annots", std::move(annotations)); |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 125 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 126 | auto content = pageDevice->content(); |
| 127 | page->insertObjRef("Contents", sk_make_sp<SkPDFStream>(content.get())); |
| 128 | return page; |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 131 | // return root node. |
| 132 | static sk_sp<SkPDFDict> generate_page_tree( |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 133 | SkTDArray<SkPDFDict*>& pages, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 134 | SkTDArray<SkPDFDict*>* pageTree) { |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 135 | // PDF wants a tree describing all the pages in the document. We arbitrary |
| 136 | // choose 8 (kNodeSize) as the number of allowed children. The internal |
| 137 | // nodes have type "Pages" with an array of children, a parent pointer, and |
| 138 | // the number of leaves below the node as "Count." The leaves are passed |
| 139 | // into the method, have type "Page" and need a parent pointer. This method |
| 140 | // builds the tree bottom up, skipping internal nodes that would have only |
| 141 | // one child. |
| 142 | static const int kNodeSize = 8; |
| 143 | |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 144 | // curNodes takes a reference to its items, which it passes to pageTree. |
| 145 | SkTDArray<SkPDFDict*> curNodes; |
| 146 | curNodes.setReserve(pages.count()); |
| 147 | for (int i = 0; i < pages.count(); i++) { |
| 148 | SkSafeRef(pages[i]); |
| 149 | curNodes.push(pages[i]); |
| 150 | } |
| 151 | |
| 152 | // nextRoundNodes passes its references to nodes on to curNodes. |
| 153 | SkTDArray<SkPDFDict*> nextRoundNodes; |
| 154 | nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize); |
| 155 | |
| 156 | int treeCapacity = kNodeSize; |
| 157 | do { |
| 158 | for (int i = 0; i < curNodes.count(); ) { |
| 159 | if (i > 0 && i + 1 == curNodes.count()) { |
| 160 | nextRoundNodes.push(curNodes[i]); |
| 161 | break; |
| 162 | } |
| 163 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 164 | auto newNode = sk_make_sp<SkPDFDict>("Pages"); |
| 165 | auto kids = sk_make_sp<SkPDFArray>(); |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 166 | kids->reserve(kNodeSize); |
| 167 | |
| 168 | int count = 0; |
| 169 | for (; i < curNodes.count() && count < kNodeSize; i++, count++) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 170 | curNodes[i]->insertObjRef("Parent", newNode); |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 171 | kids->appendObjRef(sk_ref_sp(curNodes[i])); |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 172 | |
| 173 | // TODO(vandebo): put the objects in strict access order. |
| 174 | // Probably doesn't matter because they are so small. |
| 175 | if (curNodes[i] != pages[0]) { |
| 176 | pageTree->push(curNodes[i]); // Transfer reference. |
| 177 | } else { |
| 178 | SkSafeUnref(curNodes[i]); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // treeCapacity is the number of leaf nodes possible for the |
| 183 | // current set of subtrees being generated. (i.e. 8, 64, 512, ...). |
| 184 | // It is hard to count the number of leaf nodes in the current |
| 185 | // subtree. However, by construction, we know that unless it's the |
| 186 | // last subtree for the current depth, the leaf count will be |
| 187 | // treeCapacity, otherwise it's what ever is left over after |
| 188 | // consuming treeCapacity chunks. |
| 189 | int pageCount = treeCapacity; |
| 190 | if (i == curNodes.count()) { |
| 191 | pageCount = ((pages.count() - 1) % treeCapacity) + 1; |
| 192 | } |
halcanary | 72266fd | 2015-05-05 08:00:24 -0700 | [diff] [blame] | 193 | newNode->insertInt("Count", pageCount); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 194 | newNode->insertObject("Kids", std::move(kids)); |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 195 | nextRoundNodes.push(newNode.release()); // Transfer reference. |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | curNodes = nextRoundNodes; |
| 199 | nextRoundNodes.rewind(); |
| 200 | treeCapacity *= kNodeSize; |
| 201 | } while (curNodes.count() > 1); |
| 202 | |
| 203 | pageTree->push(curNodes[0]); // Transfer reference. |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 204 | return sk_ref_sp(curNodes[0]); |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 205 | } |
| 206 | |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 207 | #if 0 |
| 208 | // TODO(halcanary): expose notEmbeddableCount in SkDocument |
| 209 | void GetCountOfFontTypes( |
| 210 | const SkTDArray<SkPDFDevice*>& pageDevices, |
| 211 | int counts[SkAdvancedTypefaceMetrics::kOther_Font + 1], |
| 212 | int* notSubsettableCount, |
| 213 | int* notEmbeddableCount) { |
| 214 | sk_bzero(counts, sizeof(int) * |
| 215 | (SkAdvancedTypefaceMetrics::kOther_Font + 1)); |
| 216 | SkTDArray<SkFontID> seenFonts; |
| 217 | int notSubsettable = 0; |
| 218 | int notEmbeddable = 0; |
| 219 | |
| 220 | for (int pageNumber = 0; pageNumber < pageDevices.count(); pageNumber++) { |
| 221 | const SkTDArray<SkPDFFont*>& fontResources = |
| 222 | pageDevices[pageNumber]->getFontResources(); |
| 223 | for (int font = 0; font < fontResources.count(); font++) { |
| 224 | SkFontID fontID = fontResources[font]->typeface()->uniqueID(); |
| 225 | if (seenFonts.find(fontID) == -1) { |
| 226 | counts[fontResources[font]->getType()]++; |
| 227 | seenFonts.push(fontID); |
| 228 | if (!fontResources[font]->canSubset()) { |
| 229 | notSubsettable++; |
| 230 | } |
| 231 | if (!fontResources[font]->canEmbed()) { |
| 232 | notEmbeddable++; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | if (notSubsettableCount) { |
| 238 | *notSubsettableCount = notSubsettable; |
| 239 | |
| 240 | } |
| 241 | if (notEmbeddableCount) { |
| 242 | *notEmbeddableCount = notEmbeddable; |
| 243 | } |
| 244 | } |
| 245 | #endif |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 246 | |
| 247 | template <typename T> static T* clone(const T* o) { return o ? new T(*o) : nullptr; } |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 248 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 249 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 250 | SkPDFDocument::SkPDFDocument(SkWStream* stream, |
| 251 | void (*doneProc)(SkWStream*, bool), |
| 252 | SkScalar rasterDpi, |
| 253 | SkPixelSerializer* jpegEncoder) |
| 254 | : SkDocument(stream, doneProc) |
| 255 | , fRasterDpi(rasterDpi) { |
| 256 | fCanon.setPixelSerializer(SkSafeRef(jpegEncoder)); |
| 257 | } |
| 258 | |
| 259 | SkPDFDocument::~SkPDFDocument() { |
| 260 | // subclasses of SkDocument must call close() in their destructors. |
| 261 | this->close(); |
| 262 | } |
| 263 | |
| 264 | void SkPDFDocument::serialize(const sk_sp<SkPDFObject>& object) { |
| 265 | fObjectSerializer.addObjectRecursively(object); |
| 266 | fObjectSerializer.serializeObjects(this->getStream()); |
| 267 | } |
| 268 | |
| 269 | SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height, |
| 270 | const SkRect& trimBox) { |
| 271 | SkASSERT(!fCanvas.get()); // endPage() was called before this. |
| 272 | if (fPageDevices.empty()) { |
| 273 | // if this is the first page if the document. |
| 274 | fObjectSerializer.serializeHeader(this->getStream(), fMetadata); |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 275 | } |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 276 | SkISize pageSize = SkISize::Make( |
| 277 | SkScalarRoundToInt(width), SkScalarRoundToInt(height)); |
| 278 | sk_sp<SkPDFDevice> device( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame^] | 279 | SkPDFDevice::Create(pageSize, fRasterDpi, this)); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 280 | fCanvas = sk_make_sp<SkPDFCanvas>(device); |
| 281 | fPageDevices.push_back(std::move(device)); |
| 282 | fCanvas->clipRect(trimBox); |
| 283 | fCanvas->translate(trimBox.x(), trimBox.y()); |
| 284 | return fCanvas.get(); |
| 285 | } |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 286 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 287 | void SkPDFDocument::onEndPage() { |
| 288 | SkASSERT(fCanvas.get()); |
| 289 | fCanvas->flush(); |
| 290 | fCanvas.reset(nullptr); |
| 291 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 292 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 293 | void SkPDFDocument::onAbort() { |
| 294 | fCanvas.reset(nullptr); |
| 295 | fPageDevices.reset(); |
| 296 | fCanon.reset(); |
| 297 | renew(&fObjectSerializer); |
| 298 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 299 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 300 | void SkPDFDocument::setMetadata(const SkDocument::Attribute info[], |
| 301 | int infoCount, |
| 302 | const SkTime::DateTime* creationDate, |
| 303 | const SkTime::DateTime* modifiedDate) { |
| 304 | fMetadata.fInfo.reset(info, infoCount); |
| 305 | fMetadata.fCreation.reset(clone(creationDate)); |
| 306 | fMetadata.fModified.reset(clone(modifiedDate)); |
| 307 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 308 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 309 | bool SkPDFDocument::onClose(SkWStream* stream) { |
| 310 | SkASSERT(!fCanvas.get()); |
| 311 | if (fPageDevices.empty()) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 312 | fPageDevices.reset(); |
halcanary | 2e3f9d8 | 2015-02-27 12:41:03 -0800 | [diff] [blame] | 313 | fCanon.reset(); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 314 | renew(&fObjectSerializer); |
| 315 | return false; |
| 316 | } |
| 317 | SkTDArray<SkPDFDict*> pages; // TODO: SkTArray<sk_sp<SkPDFDict>> |
| 318 | auto dests = sk_make_sp<SkPDFDict>(); |
| 319 | |
| 320 | for (const sk_sp<const SkPDFDevice>& pageDevice : fPageDevices) { |
| 321 | SkASSERT(pageDevice); |
| 322 | SkASSERT(fPageDevices[0]->getCanon() == pageDevice->getCanon()); |
| 323 | sk_sp<SkPDFDict> page(create_pdf_page(pageDevice.get())); |
| 324 | pageDevice->appendDestinations(dests.get(), page.get()); |
| 325 | pages.push(page.release()); |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 326 | } |
| 327 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 328 | auto docCatalog = sk_make_sp<SkPDFDict>("Catalog"); |
| 329 | |
| 330 | sk_sp<SkPDFObject> id, xmp; |
| 331 | #ifdef SK_PDF_GENERATE_PDFA |
| 332 | SkPDFMetadata::UUID uuid = metadata.uuid(); |
| 333 | // We use the same UUID for Document ID and Instance ID since this |
| 334 | // is the first revision of this document (and Skia does not |
| 335 | // support revising existing PDF documents). |
| 336 | // If we are not in PDF/A mode, don't use a UUID since testing |
| 337 | // works best with reproducible outputs. |
| 338 | id.reset(SkPDFMetadata::CreatePdfId(uuid, uuid)); |
| 339 | xmp.reset(metadata.createXMPObject(uuid, uuid)); |
| 340 | docCatalog->insertObjRef("Metadata", std::move(xmp)); |
| 341 | |
| 342 | // sRGB is specified by HTML, CSS, and SVG. |
| 343 | auto outputIntent = sk_make_sp<SkPDFDict>("OutputIntent"); |
| 344 | outputIntent->insertName("S", "GTS_PDFA1"); |
| 345 | outputIntent->insertString("RegistryName", "http://www.color.org"); |
| 346 | outputIntent->insertString("OutputConditionIdentifier", |
| 347 | "sRGB IEC61966-2.1"); |
| 348 | auto intentArray = sk_make_sp<SkPDFArray>(); |
| 349 | intentArray->appendObject(std::move(outputIntent)); |
| 350 | // Don't specify OutputIntents if we are not in PDF/A mode since |
| 351 | // no one has ever asked for this feature. |
| 352 | docCatalog->insertObject("OutputIntents", std::move(intentArray)); |
| 353 | #endif |
| 354 | |
| 355 | SkTDArray<SkPDFDict*> pageTree; |
| 356 | docCatalog->insertObjRef("Pages", generate_page_tree(pages, &pageTree)); |
| 357 | |
| 358 | if (dests->size() > 0) { |
| 359 | docCatalog->insertObjRef("Dests", std::move(dests)); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 360 | } |
| 361 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 362 | // Build font subsetting info before calling addObjectRecursively(). |
| 363 | perform_font_subsetting(fPageDevices, &fObjectSerializer.fSubstituteMap); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 364 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 365 | fObjectSerializer.addObjectRecursively(docCatalog); |
| 366 | fObjectSerializer.serializeObjects(this->getStream()); |
| 367 | fObjectSerializer.serializeFooter( |
| 368 | this->getStream(), docCatalog, std::move(id)); |
| 369 | pageTree.unrefAll(); // TODO(halcanary): make this unnecesary by |
| 370 | // refactoring generate_page_tree(). |
| 371 | pages.unrefAll(); |
| 372 | fPageDevices.reset(); |
| 373 | fCanon.reset(); |
| 374 | renew(&fObjectSerializer); |
| 375 | return true; |
| 376 | } |
| 377 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 378 | /////////////////////////////////////////////////////////////////////////////// |
| 379 | |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 380 | sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream, |
| 381 | void (*proc)(SkWStream*, bool), |
| 382 | SkScalar dpi, |
| 383 | SkPixelSerializer* jpeg) { |
| 384 | return stream ? sk_make_sp<SkPDFDocument>(stream, proc, dpi, jpeg) : nullptr; |
| 385 | } |
| 386 | |
halcanary | 8c92dc1 | 2015-02-19 18:50:05 -0800 | [diff] [blame] | 387 | SkDocument* SkDocument::CreatePDF(SkWStream* stream, SkScalar dpi) { |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 388 | return SkPDFMakeDocument(stream, nullptr, dpi, nullptr).release(); |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | SkDocument* SkDocument::CreatePDF(SkWStream* stream, |
| 392 | SkScalar dpi, |
| 393 | SkPixelSerializer* jpegEncoder) { |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 394 | return SkPDFMakeDocument(stream, nullptr, dpi, jpegEncoder).release(); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 395 | } |
| 396 | |
halcanary | 8c92dc1 | 2015-02-19 18:50:05 -0800 | [diff] [blame] | 397 | SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 398 | auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 399 | SkAutoTDelete<SkFILEWStream> stream(new SkFILEWStream(path)); |
| 400 | return stream->isValid() |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 401 | ? SkPDFMakeDocument(stream.release(), delete_wstream, dpi, nullptr).release() |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 402 | : nullptr; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 403 | } |