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