epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 10 | #include "SkData.h" |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 11 | #include "SkPDFCatalog.h" |
| 12 | #include "SkPDFDevice.h" |
| 13 | #include "SkPDFPage.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 14 | #include "SkPDFResourceDict.h" |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 15 | |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 16 | SkPDFPage::SkPDFPage(SkPDFDevice* content) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 17 | : SkPDFDict("Page"), |
| 18 | fDevice(content) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 19 | SkSafeRef(content); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | SkPDFPage::~SkPDFPage() {} |
| 23 | |
| 24 | void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage, |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 25 | const SkTSet<SkPDFObject*>& knownResourceObjects, |
| 26 | SkTSet<SkPDFObject*>* newResourceObjects) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 27 | SkPDFResourceDict* resourceDict = fDevice->getResourceDict(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 28 | if (fContentStream.get() == NULL) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 29 | insert("Resources", resourceDict); |
reed@google.com | 2a006c1 | 2012-09-19 17:05:55 +0000 | [diff] [blame] | 30 | SkSafeUnref(this->insert("MediaBox", fDevice->copyMediaBox())); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 31 | if (!SkToBool(catalog->getDocumentFlags() & |
| 32 | SkPDFDocument::kNoLinks_Flags)) { |
reed@google.com | 2a006c1 | 2012-09-19 17:05:55 +0000 | [diff] [blame] | 33 | SkPDFArray* annots = fDevice->getAnnotations(); |
| 34 | if (annots && annots->size() > 0) { |
| 35 | insert("Annots", annots); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 38 | |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 39 | SkAutoTUnref<SkData> content(fDevice->copyContentToData()); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 40 | fContentStream.reset(new SkPDFStream(content.get())); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 41 | insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 42 | } |
| 43 | catalog->addObject(fContentStream.get(), firstPage); |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 44 | resourceDict->getReferencedResources(knownResourceObjects, |
| 45 | newResourceObjects, |
| 46 | true); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | off_t SkPDFPage::getPageSize(SkPDFCatalog* catalog, off_t fileOffset) { |
| 50 | SkASSERT(fContentStream.get() != NULL); |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 51 | catalog->setFileOffset(fContentStream.get(), fileOffset); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 52 | return fContentStream->getOutputSize(catalog, true); |
| 53 | } |
| 54 | |
| 55 | void SkPDFPage::emitPage(SkWStream* stream, SkPDFCatalog* catalog) { |
| 56 | SkASSERT(fContentStream.get() != NULL); |
| 57 | fContentStream->emitObject(stream, catalog, true); |
| 58 | } |
| 59 | |
| 60 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 61 | void SkPDFPage::GeneratePageTree(const SkTDArray<SkPDFPage*>& pages, |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 62 | SkPDFCatalog* catalog, |
| 63 | SkTDArray<SkPDFDict*>* pageTree, |
| 64 | SkPDFDict** rootNode) { |
vandebo@chromium.org | bd36ebe | 2011-04-08 01:22:45 +0000 | [diff] [blame] | 65 | // PDF wants a tree describing all the pages in the document. We arbitrary |
| 66 | // choose 8 (kNodeSize) as the number of allowed children. The internal |
| 67 | // nodes have type "Pages" with an array of children, a parent pointer, and |
| 68 | // the number of leaves below the node as "Count." The leaves are passed |
| 69 | // into the method, have type "Page" and need a parent pointer. This method |
| 70 | // builds the tree bottom up, skipping internal nodes that would have only |
| 71 | // one child. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 72 | static const int kNodeSize = 8; |
| 73 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 74 | SkAutoTUnref<SkPDFName> kidsName(new SkPDFName("Kids")); |
| 75 | SkAutoTUnref<SkPDFName> countName(new SkPDFName("Count")); |
| 76 | SkAutoTUnref<SkPDFName> parentName(new SkPDFName("Parent")); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 77 | |
| 78 | // curNodes takes a reference to its items, which it passes to pageTree. |
| 79 | SkTDArray<SkPDFDict*> curNodes; |
| 80 | curNodes.setReserve(pages.count()); |
| 81 | for (int i = 0; i < pages.count(); i++) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 82 | SkSafeRef(pages[i]); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 83 | curNodes.push(pages[i]); |
| 84 | } |
| 85 | |
| 86 | // nextRoundNodes passes its references to nodes on to curNodes. |
| 87 | SkTDArray<SkPDFDict*> nextRoundNodes; |
| 88 | nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize); |
| 89 | |
vandebo@chromium.org | bd36ebe | 2011-04-08 01:22:45 +0000 | [diff] [blame] | 90 | int treeCapacity = kNodeSize; |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 91 | do { |
| 92 | for (int i = 0; i < curNodes.count(); ) { |
| 93 | if (i > 0 && i + 1 == curNodes.count()) { |
| 94 | nextRoundNodes.push(curNodes[i]); |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | SkPDFDict* newNode = new SkPDFDict("Pages"); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 99 | SkAutoTUnref<SkPDFObjRef> newNodeRef(new SkPDFObjRef(newNode)); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 100 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 101 | SkAutoTUnref<SkPDFArray> kids(new SkPDFArray); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 102 | kids->reserve(kNodeSize); |
| 103 | |
| 104 | int count = 0; |
| 105 | for (; i < curNodes.count() && count < kNodeSize; i++, count++) { |
| 106 | curNodes[i]->insert(parentName.get(), newNodeRef.get()); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 107 | kids->append(new SkPDFObjRef(curNodes[i]))->unref(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 108 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 109 | // TODO(vandebo): put the objects in strict access order. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 110 | // Probably doesn't matter because they are so small. |
| 111 | if (curNodes[i] != pages[0]) { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 112 | pageTree->push(curNodes[i]); // Transfer reference. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 113 | catalog->addObject(curNodes[i], false); |
| 114 | } else { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 115 | SkSafeUnref(curNodes[i]); |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 116 | catalog->addObject(curNodes[i], true); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
vandebo@chromium.org | 3c97442 | 2013-01-30 21:44:40 +0000 | [diff] [blame] | 120 | // treeCapacity is the number of leaf nodes possible for the |
| 121 | // current set of subtrees being generated. (i.e. 8, 64, 512, ...). |
| 122 | // It is hard to count the number of leaf nodes in the current |
| 123 | // subtree. However, by construction, we know that unless it's the |
| 124 | // last subtree for the current depth, the leaf count will be |
| 125 | // treeCapacity, otherwise it's what ever is left over after |
| 126 | // consuming treeCapacity chunks. |
vandebo@chromium.org | bd36ebe | 2011-04-08 01:22:45 +0000 | [diff] [blame] | 127 | int pageCount = treeCapacity; |
vandebo@chromium.org | 3c97442 | 2013-01-30 21:44:40 +0000 | [diff] [blame] | 128 | if (i == curNodes.count()) { |
| 129 | pageCount = ((pages.count() - 1) % treeCapacity) + 1; |
vandebo@chromium.org | bd36ebe | 2011-04-08 01:22:45 +0000 | [diff] [blame] | 130 | } |
| 131 | newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref(); |
vandebo@chromium.org | 3c97442 | 2013-01-30 21:44:40 +0000 | [diff] [blame] | 132 | newNode->insert(kidsName.get(), kids.get()); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 133 | nextRoundNodes.push(newNode); // Transfer reference. |
| 134 | } |
| 135 | |
| 136 | curNodes = nextRoundNodes; |
| 137 | nextRoundNodes.rewind(); |
vandebo@chromium.org | bd36ebe | 2011-04-08 01:22:45 +0000 | [diff] [blame] | 138 | treeCapacity *= kNodeSize; |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 139 | } while (curNodes.count() > 1); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 140 | |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 141 | pageTree->push(curNodes[0]); // Transfer reference. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 142 | catalog->addObject(curNodes[0], false); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 143 | if (rootNode) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 144 | *rootNode = curNodes[0]; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 145 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 146 | } |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 147 | |
| 148 | const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const { |
| 149 | return fDevice->getFontResources(); |
| 150 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 151 | |
| 152 | const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const { |
| 153 | return fDevice->getFontGlyphUsage(); |
| 154 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 155 | |
| 156 | void SkPDFPage::appendDestinations(SkPDFDict* dict) { |
| 157 | fDevice->appendDestinations(dict, this); |
| 158 | } |