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