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