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