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