OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 The Android Open Source Project | 3 * Copyright 2010 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 #include "SkPDFCatalog.h" | 11 #include "SkPDFCatalog.h" |
12 #include "SkPDFDevice.h" | 12 #include "SkPDFDevice.h" |
13 #include "SkPDFPage.h" | 13 #include "SkPDFPage.h" |
14 #include "SkPDFResourceDict.h" | 14 #include "SkPDFResourceDict.h" |
15 | 15 |
16 SkPDFPage::SkPDFPage(SkPDFDevice* content) | 16 SkPDFPage::SkPDFPage(SkPDFDevice* content) |
17 : SkPDFDict("Page"), | 17 : SkPDFDict("Page"), |
18 fDevice(content) { | 18 fDevice(content) { |
19 SkSafeRef(content); | 19 SkSafeRef(content); |
20 } | 20 } |
21 | 21 |
22 SkPDFPage::~SkPDFPage() {} | 22 SkPDFPage::~SkPDFPage() {} |
23 | 23 |
24 void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage, | 24 void SkPDFPage::finalizePage() { |
25 const SkTSet<SkPDFObject*>& knownResourceObjects, | |
26 SkTSet<SkPDFObject*>* newResourceObjects) { | |
27 if (fContentStream.get() == NULL) { | 25 if (fContentStream.get() == NULL) { |
28 this->insert("Resources", fDevice->getResourceDict()); | 26 this->insert("Resources", fDevice->getResourceDict()); |
29 SkSafeUnref(this->insert("MediaBox", fDevice->copyMediaBox())); | 27 SkSafeUnref(this->insert("MediaBox", fDevice->copyMediaBox())); |
30 SkPDFArray* annots = fDevice->getAnnotations(); | 28 SkPDFArray* annots = fDevice->getAnnotations(); |
31 if (annots && annots->size() > 0) { | 29 if (annots && annots->size() > 0) { |
32 insert("Annots", annots); | 30 insert("Annots", annots); |
33 } | 31 } |
34 | 32 |
35 SkAutoTDelete<SkStreamAsset> content(fDevice->content()); | 33 SkAutoTDelete<SkStreamAsset> content(fDevice->content()); |
36 fContentStream.reset(new SkPDFStream(content.get())); | 34 fContentStream.reset(new SkPDFStream(content.get())); |
37 insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref(); | 35 insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref(); |
38 } | 36 } |
39 catalog->addObject(fContentStream.get(), firstPage); | |
40 } | 37 } |
41 | 38 |
42 // static | 39 // static |
43 void SkPDFPage::GeneratePageTree(const SkTDArray<SkPDFPage*>& pages, | 40 void SkPDFPage::GeneratePageTree(const SkTDArray<SkPDFPage*>& pages, |
44 SkPDFCatalog* catalog, | |
45 SkTDArray<SkPDFDict*>* pageTree, | 41 SkTDArray<SkPDFDict*>* pageTree, |
46 SkPDFDict** rootNode) { | 42 SkPDFDict** rootNode) { |
47 // PDF wants a tree describing all the pages in the document. We arbitrary | 43 // PDF wants a tree describing all the pages in the document. We arbitrary |
48 // choose 8 (kNodeSize) as the number of allowed children. The internal | 44 // choose 8 (kNodeSize) as the number of allowed children. The internal |
49 // nodes have type "Pages" with an array of children, a parent pointer, and | 45 // nodes have type "Pages" with an array of children, a parent pointer, and |
50 // the number of leaves below the node as "Count." The leaves are passed | 46 // the number of leaves below the node as "Count." The leaves are passed |
51 // into the method, have type "Page" and need a parent pointer. This method | 47 // into the method, have type "Page" and need a parent pointer. This method |
52 // builds the tree bottom up, skipping internal nodes that would have only | 48 // builds the tree bottom up, skipping internal nodes that would have only |
53 // one child. | 49 // one child. |
54 static const int kNodeSize = 8; | 50 static const int kNodeSize = 8; |
(...skipping 30 matching lines...) Expand all Loading... |
85 | 81 |
86 int count = 0; | 82 int count = 0; |
87 for (; i < curNodes.count() && count < kNodeSize; i++, count++) { | 83 for (; i < curNodes.count() && count < kNodeSize; i++, count++) { |
88 curNodes[i]->insert(parentName.get(), newNodeRef.get()); | 84 curNodes[i]->insert(parentName.get(), newNodeRef.get()); |
89 kids->append(new SkPDFObjRef(curNodes[i]))->unref(); | 85 kids->append(new SkPDFObjRef(curNodes[i]))->unref(); |
90 | 86 |
91 // TODO(vandebo): put the objects in strict access order. | 87 // TODO(vandebo): put the objects in strict access order. |
92 // Probably doesn't matter because they are so small. | 88 // Probably doesn't matter because they are so small. |
93 if (curNodes[i] != pages[0]) { | 89 if (curNodes[i] != pages[0]) { |
94 pageTree->push(curNodes[i]); // Transfer reference. | 90 pageTree->push(curNodes[i]); // Transfer reference. |
95 catalog->addObject(curNodes[i], false); | |
96 } else { | 91 } else { |
97 SkSafeUnref(curNodes[i]); | 92 SkSafeUnref(curNodes[i]); |
98 catalog->addObject(curNodes[i], true); | |
99 } | 93 } |
100 } | 94 } |
101 | 95 |
102 // treeCapacity is the number of leaf nodes possible for the | 96 // treeCapacity is the number of leaf nodes possible for the |
103 // current set of subtrees being generated. (i.e. 8, 64, 512, ...). | 97 // current set of subtrees being generated. (i.e. 8, 64, 512, ...). |
104 // It is hard to count the number of leaf nodes in the current | 98 // It is hard to count the number of leaf nodes in the current |
105 // subtree. However, by construction, we know that unless it's the | 99 // subtree. However, by construction, we know that unless it's the |
106 // last subtree for the current depth, the leaf count will be | 100 // last subtree for the current depth, the leaf count will be |
107 // treeCapacity, otherwise it's what ever is left over after | 101 // treeCapacity, otherwise it's what ever is left over after |
108 // consuming treeCapacity chunks. | 102 // consuming treeCapacity chunks. |
109 int pageCount = treeCapacity; | 103 int pageCount = treeCapacity; |
110 if (i == curNodes.count()) { | 104 if (i == curNodes.count()) { |
111 pageCount = ((pages.count() - 1) % treeCapacity) + 1; | 105 pageCount = ((pages.count() - 1) % treeCapacity) + 1; |
112 } | 106 } |
113 newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref(); | 107 newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref(); |
114 newNode->insert(kidsName.get(), kids.get()); | 108 newNode->insert(kidsName.get(), kids.get()); |
115 nextRoundNodes.push(newNode); // Transfer reference. | 109 nextRoundNodes.push(newNode); // Transfer reference. |
116 } | 110 } |
117 | 111 |
118 curNodes = nextRoundNodes; | 112 curNodes = nextRoundNodes; |
119 nextRoundNodes.rewind(); | 113 nextRoundNodes.rewind(); |
120 treeCapacity *= kNodeSize; | 114 treeCapacity *= kNodeSize; |
121 } while (curNodes.count() > 1); | 115 } while (curNodes.count() > 1); |
122 | 116 |
123 pageTree->push(curNodes[0]); // Transfer reference. | 117 pageTree->push(curNodes[0]); // Transfer reference. |
124 catalog->addObject(curNodes[0], false); | |
125 if (rootNode) { | 118 if (rootNode) { |
126 *rootNode = curNodes[0]; | 119 *rootNode = curNodes[0]; |
127 } | 120 } |
128 } | 121 } |
129 | 122 |
130 const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const { | 123 const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const { |
131 return fDevice->getFontResources(); | 124 return fDevice->getFontResources(); |
132 } | 125 } |
133 | 126 |
134 const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const { | 127 const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const { |
135 return fDevice->getFontGlyphUsage(); | 128 return fDevice->getFontGlyphUsage(); |
136 } | 129 } |
137 | 130 |
138 void SkPDFPage::appendDestinations(SkPDFDict* dict) { | 131 void SkPDFPage::appendDestinations(SkPDFDict* dict) { |
139 fDevice->appendDestinations(dict, this); | 132 fDevice->appendDestinations(dict, this); |
140 } | 133 } |
141 | 134 |
142 SkPDFObject* SkPDFPage::getContentStream() const { | 135 SkPDFObject* SkPDFPage::getContentStream() const { |
143 return fContentStream.get(); | 136 return fContentStream.get(); |
144 } | 137 } |
OLD | NEW |