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.

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

mercurial