gfx/skia/trunk/src/pdf/SkPDFPage.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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  */
    10 #include "SkPDFCatalog.h"
    11 #include "SkPDFDevice.h"
    12 #include "SkPDFPage.h"
    13 #include "SkPDFResourceDict.h"
    14 #include "SkStream.h"
    16 SkPDFPage::SkPDFPage(SkPDFDevice* content)
    17     : SkPDFDict("Page"),
    18       fDevice(content) {
    19   SkSafeRef(content);
    20 }
    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);
    36             }
    37         }
    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 }
    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 }
    55 void SkPDFPage::emitPage(SkWStream* stream, SkPDFCatalog* catalog) {
    56     SkASSERT(fContentStream.get() != NULL);
    57     fContentStream->emitObject(stream, catalog, true);
    58 }
    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;
    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++) {
    82         SkSafeRef(pages[i]);
    83         curNodes.push(pages[i]);
    84     }
    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;
    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             }
    98             SkPDFDict* newNode = new SkPDFDict("Pages");
    99             SkAutoTUnref<SkPDFObjRef> newNodeRef(new SkPDFObjRef(newNode));
   101             SkAutoTUnref<SkPDFArray> kids(new SkPDFArray);
   102             kids->reserve(kNodeSize);
   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();
   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             }
   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         }
   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);
   143     if (rootNode) {
   144         *rootNode = curNodes[0];
   145     }
   146 }
   148 const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const {
   149     return fDevice->getFontResources();
   150 }
   152 const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const {
   153     return fDevice->getFontGlyphUsage();
   154 }
   156 void SkPDFPage::appendDestinations(SkPDFDict* dict) {
   157     fDevice->appendDestinations(dict, this);
   158 }

mercurial