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