3 * Copyright 2010 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "SkPDFCatalog.h"
12 #include "SkPDFDevice.h"
13 #include "SkPDFPage.h"
14 #include "SkPDFResourceDict.h"
16 SkPDFPage::SkPDFPage(SkPDFDevice* content)
22 SkPDFPage::~SkPDFPage() {}
24 void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage,
25 const SkTSet<SkPDFObject*>& knownResourceObjects,
26 SkTSet<SkPDFObject*>* newResourceObjects) {
27 SkPDFResourceDict* resourceDict = fDevice->getResourceDict();
28 if (fContentStream.get() == NULL) {
29 insert("Resources", resourceDict);
30 SkSafeUnref(this->insert("MediaBox", fDevice->copyMediaBox()));
31 if (!SkToBool(catalog->getDocumentFlags() &
32 SkPDFDocument::kNoLinks_Flags)) {
33 SkPDFArray* annots = fDevice->getAnnotations();
34 if (annots && annots->size() > 0) {
35 insert("Annots", annots);
39 SkAutoTUnref<SkData> content(fDevice->copyContentToData());
40 fContentStream.reset(new SkPDFStream(content.get()));
41 insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref();
43 catalog->addObject(fContentStream.get(), firstPage);
44 resourceDict->getReferencedResources(knownResourceObjects,
49 off_t SkPDFPage::getPageSize(SkPDFCatalog* catalog, off_t fileOffset) {
50 SkASSERT(fContentStream.get() != NULL);
51 catalog->setFileOffset(fContentStream.get(), fileOffset);
52 return fContentStream->getOutputSize(catalog, true);
55 void SkPDFPage::emitPage(SkWStream* stream, SkPDFCatalog* catalog) {
56 SkASSERT(fContentStream.get() != NULL);
57 fContentStream->emitObject(stream, catalog, true);
61 void SkPDFPage::GeneratePageTree(const SkTDArray<SkPDFPage*>& pages,
62 SkPDFCatalog* catalog,
63 SkTDArray<SkPDFDict*>* pageTree,
64 SkPDFDict** rootNode) {
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
72 static const int kNodeSize = 8;
74 SkAutoTUnref<SkPDFName> kidsName(new SkPDFName("Kids"));
75 SkAutoTUnref<SkPDFName> countName(new SkPDFName("Count"));
76 SkAutoTUnref<SkPDFName> parentName(new SkPDFName("Parent"));
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++) {
83 curNodes.push(pages[i]);
86 // nextRoundNodes passes its references to nodes on to curNodes.
87 SkTDArray<SkPDFDict*> nextRoundNodes;
88 nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize);
90 int treeCapacity = kNodeSize;
92 for (int i = 0; i < curNodes.count(); ) {
93 if (i > 0 && i + 1 == curNodes.count()) {
94 nextRoundNodes.push(curNodes[i]);
98 SkPDFDict* newNode = new SkPDFDict("Pages");
99 SkAutoTUnref<SkPDFObjRef> newNodeRef(new SkPDFObjRef(newNode));
101 SkAutoTUnref<SkPDFArray> kids(new SkPDFArray);
102 kids->reserve(kNodeSize);
105 for (; i < curNodes.count() && count < kNodeSize; i++, count++) {
106 curNodes[i]->insert(parentName.get(), newNodeRef.get());
107 kids->append(new SkPDFObjRef(curNodes[i]))->unref();
109 // TODO(vandebo): put the objects in strict access order.
110 // Probably doesn't matter because they are so small.
111 if (curNodes[i] != pages[0]) {
112 pageTree->push(curNodes[i]); // Transfer reference.
113 catalog->addObject(curNodes[i], false);
115 SkSafeUnref(curNodes[i]);
116 catalog->addObject(curNodes[i], true);
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.
127 int pageCount = treeCapacity;
128 if (i == curNodes.count()) {
129 pageCount = ((pages.count() - 1) % treeCapacity) + 1;
131 newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref();
132 newNode->insert(kidsName.get(), kids.get());
133 nextRoundNodes.push(newNode); // Transfer reference.
136 curNodes = nextRoundNodes;
137 nextRoundNodes.rewind();
138 treeCapacity *= kNodeSize;
139 } while (curNodes.count() > 1);
141 pageTree->push(curNodes[0]); // Transfer reference.
142 catalog->addObject(curNodes[0], false);
144 *rootNode = curNodes[0];
148 const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const {
149 return fDevice->getFontResources();
152 const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const {
153 return fDevice->getFontGlyphUsage();
156 void SkPDFPage::appendDestinations(SkPDFDict* dict) {
157 fDevice->appendDestinations(dict, this);