michael@0: michael@0: /* michael@0: * Copyright 2010 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkPDFCatalog.h" michael@0: #include "SkPDFDevice.h" michael@0: #include "SkPDFPage.h" michael@0: #include "SkPDFResourceDict.h" michael@0: #include "SkStream.h" michael@0: michael@0: SkPDFPage::SkPDFPage(SkPDFDevice* content) michael@0: : SkPDFDict("Page"), michael@0: fDevice(content) { michael@0: SkSafeRef(content); michael@0: } michael@0: michael@0: SkPDFPage::~SkPDFPage() {} michael@0: michael@0: void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage, michael@0: const SkTSet& knownResourceObjects, michael@0: SkTSet* newResourceObjects) { michael@0: SkPDFResourceDict* resourceDict = fDevice->getResourceDict(); michael@0: if (fContentStream.get() == NULL) { michael@0: insert("Resources", resourceDict); michael@0: SkSafeUnref(this->insert("MediaBox", fDevice->copyMediaBox())); michael@0: if (!SkToBool(catalog->getDocumentFlags() & michael@0: SkPDFDocument::kNoLinks_Flags)) { michael@0: SkPDFArray* annots = fDevice->getAnnotations(); michael@0: if (annots && annots->size() > 0) { michael@0: insert("Annots", annots); michael@0: } michael@0: } michael@0: michael@0: SkAutoTUnref content(fDevice->content()); michael@0: fContentStream.reset(new SkPDFStream(content.get())); michael@0: insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref(); michael@0: } michael@0: catalog->addObject(fContentStream.get(), firstPage); michael@0: resourceDict->getReferencedResources(knownResourceObjects, michael@0: newResourceObjects, michael@0: true); michael@0: } michael@0: michael@0: off_t SkPDFPage::getPageSize(SkPDFCatalog* catalog, off_t fileOffset) { michael@0: SkASSERT(fContentStream.get() != NULL); michael@0: catalog->setFileOffset(fContentStream.get(), fileOffset); michael@0: return fContentStream->getOutputSize(catalog, true); michael@0: } michael@0: michael@0: void SkPDFPage::emitPage(SkWStream* stream, SkPDFCatalog* catalog) { michael@0: SkASSERT(fContentStream.get() != NULL); michael@0: fContentStream->emitObject(stream, catalog, true); michael@0: } michael@0: michael@0: // static michael@0: void SkPDFPage::GeneratePageTree(const SkTDArray& pages, michael@0: SkPDFCatalog* catalog, michael@0: SkTDArray* pageTree, michael@0: SkPDFDict** rootNode) { michael@0: // PDF wants a tree describing all the pages in the document. We arbitrary michael@0: // choose 8 (kNodeSize) as the number of allowed children. The internal michael@0: // nodes have type "Pages" with an array of children, a parent pointer, and michael@0: // the number of leaves below the node as "Count." The leaves are passed michael@0: // into the method, have type "Page" and need a parent pointer. This method michael@0: // builds the tree bottom up, skipping internal nodes that would have only michael@0: // one child. michael@0: static const int kNodeSize = 8; michael@0: michael@0: SkAutoTUnref kidsName(new SkPDFName("Kids")); michael@0: SkAutoTUnref countName(new SkPDFName("Count")); michael@0: SkAutoTUnref parentName(new SkPDFName("Parent")); michael@0: michael@0: // curNodes takes a reference to its items, which it passes to pageTree. michael@0: SkTDArray curNodes; michael@0: curNodes.setReserve(pages.count()); michael@0: for (int i = 0; i < pages.count(); i++) { michael@0: SkSafeRef(pages[i]); michael@0: curNodes.push(pages[i]); michael@0: } michael@0: michael@0: // nextRoundNodes passes its references to nodes on to curNodes. michael@0: SkTDArray nextRoundNodes; michael@0: nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize); michael@0: michael@0: int treeCapacity = kNodeSize; michael@0: do { michael@0: for (int i = 0; i < curNodes.count(); ) { michael@0: if (i > 0 && i + 1 == curNodes.count()) { michael@0: nextRoundNodes.push(curNodes[i]); michael@0: break; michael@0: } michael@0: michael@0: SkPDFDict* newNode = new SkPDFDict("Pages"); michael@0: SkAutoTUnref newNodeRef(new SkPDFObjRef(newNode)); michael@0: michael@0: SkAutoTUnref kids(new SkPDFArray); michael@0: kids->reserve(kNodeSize); michael@0: michael@0: int count = 0; michael@0: for (; i < curNodes.count() && count < kNodeSize; i++, count++) { michael@0: curNodes[i]->insert(parentName.get(), newNodeRef.get()); michael@0: kids->append(new SkPDFObjRef(curNodes[i]))->unref(); michael@0: michael@0: // TODO(vandebo): put the objects in strict access order. michael@0: // Probably doesn't matter because they are so small. michael@0: if (curNodes[i] != pages[0]) { michael@0: pageTree->push(curNodes[i]); // Transfer reference. michael@0: catalog->addObject(curNodes[i], false); michael@0: } else { michael@0: SkSafeUnref(curNodes[i]); michael@0: catalog->addObject(curNodes[i], true); michael@0: } michael@0: } michael@0: michael@0: // treeCapacity is the number of leaf nodes possible for the michael@0: // current set of subtrees being generated. (i.e. 8, 64, 512, ...). michael@0: // It is hard to count the number of leaf nodes in the current michael@0: // subtree. However, by construction, we know that unless it's the michael@0: // last subtree for the current depth, the leaf count will be michael@0: // treeCapacity, otherwise it's what ever is left over after michael@0: // consuming treeCapacity chunks. michael@0: int pageCount = treeCapacity; michael@0: if (i == curNodes.count()) { michael@0: pageCount = ((pages.count() - 1) % treeCapacity) + 1; michael@0: } michael@0: newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref(); michael@0: newNode->insert(kidsName.get(), kids.get()); michael@0: nextRoundNodes.push(newNode); // Transfer reference. michael@0: } michael@0: michael@0: curNodes = nextRoundNodes; michael@0: nextRoundNodes.rewind(); michael@0: treeCapacity *= kNodeSize; michael@0: } while (curNodes.count() > 1); michael@0: michael@0: pageTree->push(curNodes[0]); // Transfer reference. michael@0: catalog->addObject(curNodes[0], false); michael@0: if (rootNode) { michael@0: *rootNode = curNodes[0]; michael@0: } michael@0: } michael@0: michael@0: const SkTDArray& SkPDFPage::getFontResources() const { michael@0: return fDevice->getFontResources(); michael@0: } michael@0: michael@0: const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const { michael@0: return fDevice->getFontGlyphUsage(); michael@0: } michael@0: michael@0: void SkPDFPage::appendDestinations(SkPDFDict* dict) { michael@0: fDevice->appendDestinations(dict, this); michael@0: }