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