Home | History | Annotate | Download | only in pdf
      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 "SkPDFResourceDict.h"
     14 #include "SkStream.h"
     15 
     16 SkPDFPage::SkPDFPage(SkPDFDevice* content)
     17     : SkPDFDict("Page"),
     18       fDevice(content) {
     19   SkSafeRef(content);
     20 }
     21 
     22 SkPDFPage::~SkPDFPage() {}
     23 
     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);
     36             }
     37         }
     38 
     39         SkAutoTUnref<SkStream> content(fDevice->content());
     40         fContentStream.reset(new SkPDFStream(content.get()));
     41         insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref();
     42     }
     43     catalog->addObject(fContentStream.get(), firstPage);
     44     resourceDict->getReferencedResources(knownResourceObjects,
     45                                          newResourceObjects,
     46                                          true);
     47 }
     48 
     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);
     53 }
     54 
     55 void SkPDFPage::emitPage(SkWStream* stream, SkPDFCatalog* catalog) {
     56     SkASSERT(fContentStream.get() != NULL);
     57     fContentStream->emitObject(stream, catalog, true);
     58 }
     59 
     60 // static
     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
     71     // one child.
     72     static const int kNodeSize = 8;
     73 
     74     SkAutoTUnref<SkPDFName> kidsName(new SkPDFName("Kids"));
     75     SkAutoTUnref<SkPDFName> countName(new SkPDFName("Count"));
     76     SkAutoTUnref<SkPDFName> parentName(new SkPDFName("Parent"));
     77 
     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++) {
     82         SkSafeRef(pages[i]);
     83         curNodes.push(pages[i]);
     84     }
     85 
     86     // nextRoundNodes passes its references to nodes on to curNodes.
     87     SkTDArray<SkPDFDict*> nextRoundNodes;
     88     nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize);
     89 
     90     int treeCapacity = kNodeSize;
     91     do {
     92         for (int i = 0; i < curNodes.count(); ) {
     93             if (i > 0 && i + 1 == curNodes.count()) {
     94                 nextRoundNodes.push(curNodes[i]);
     95                 break;
     96             }
     97 
     98             SkPDFDict* newNode = new SkPDFDict("Pages");
     99             SkAutoTUnref<SkPDFObjRef> newNodeRef(new SkPDFObjRef(newNode));
    100 
    101             SkAutoTUnref<SkPDFArray> kids(new SkPDFArray);
    102             kids->reserve(kNodeSize);
    103 
    104             int count = 0;
    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();
    108 
    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);
    114                 } else {
    115                     SkSafeUnref(curNodes[i]);
    116                     catalog->addObject(curNodes[i], true);
    117                 }
    118             }
    119 
    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;
    130             }
    131             newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref();
    132             newNode->insert(kidsName.get(), kids.get());
    133             nextRoundNodes.push(newNode);  // Transfer reference.
    134         }
    135 
    136         curNodes = nextRoundNodes;
    137         nextRoundNodes.rewind();
    138         treeCapacity *= kNodeSize;
    139     } while (curNodes.count() > 1);
    140 
    141     pageTree->push(curNodes[0]);  // Transfer reference.
    142     catalog->addObject(curNodes[0], false);
    143     if (rootNode) {
    144         *rootNode = curNodes[0];
    145     }
    146 }
    147 
    148 const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const {
    149     return fDevice->getFontResources();
    150 }
    151 
    152 const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const {
    153     return fDevice->getFontGlyphUsage();
    154 }
    155 
    156 void SkPDFPage::appendDestinations(SkPDFDict* dict) {
    157     fDevice->appendDestinations(dict, this);
    158 }
    159