blob: cfb6790876f1bba7ce5839c78e2353e6bf890aa7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 The Android Open Source Project
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
halcanary67ec1f82014-06-27 11:36:20 -070010#include "SkData.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000011#include "SkPDFCatalog.h"
12#include "SkPDFDevice.h"
13#include "SkPDFPage.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000014#include "SkPDFResourceDict.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000015
reed@google.com1feb3302011-07-20 18:43:19 +000016SkPDFPage::SkPDFPage(SkPDFDevice* content)
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000017 : SkPDFDict("Page"),
18 fDevice(content) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000019 SkSafeRef(content);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000020}
21
22SkPDFPage::~SkPDFPage() {}
23
24void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage,
edisonn@google.com6addb192013-04-02 15:33:08 +000025 const SkTSet<SkPDFObject*>& knownResourceObjects,
26 SkTSet<SkPDFObject*>* newResourceObjects) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +000027 SkPDFResourceDict* resourceDict = fDevice->getResourceDict();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000028 if (fContentStream.get() == NULL) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +000029 insert("Resources", resourceDict);
reed@google.com2a006c12012-09-19 17:05:55 +000030 SkSafeUnref(this->insert("MediaBox", fDevice->copyMediaBox()));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000031 if (!SkToBool(catalog->getDocumentFlags() &
32 SkPDFDocument::kNoLinks_Flags)) {
reed@google.com2a006c12012-09-19 17:05:55 +000033 SkPDFArray* annots = fDevice->getAnnotations();
34 if (annots && annots->size() > 0) {
35 insert("Annots", annots);
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000036 }
37 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000038
halcanary67ec1f82014-06-27 11:36:20 -070039 SkAutoTUnref<SkData> content(fDevice->copyContentToData());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000040 fContentStream.reset(new SkPDFStream(content.get()));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000041 insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000042 }
43 catalog->addObject(fContentStream.get(), firstPage);
commit-bot@chromium.org47401352013-07-23 21:49:29 +000044 resourceDict->getReferencedResources(knownResourceObjects,
45 newResourceObjects,
46 true);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000047}
48
49off_t SkPDFPage::getPageSize(SkPDFCatalog* catalog, off_t fileOffset) {
50 SkASSERT(fContentStream.get() != NULL);
vandebo@chromium.org30580f62012-07-12 20:22:04 +000051 catalog->setFileOffset(fContentStream.get(), fileOffset);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000052 return fContentStream->getOutputSize(catalog, true);
53}
54
55void SkPDFPage::emitPage(SkWStream* stream, SkPDFCatalog* catalog) {
56 SkASSERT(fContentStream.get() != NULL);
57 fContentStream->emitObject(stream, catalog, true);
58}
59
60// static
reed@google.comf6c3ebd2011-07-20 17:20:28 +000061void SkPDFPage::GeneratePageTree(const SkTDArray<SkPDFPage*>& pages,
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000062 SkPDFCatalog* catalog,
63 SkTDArray<SkPDFDict*>* pageTree,
64 SkPDFDict** rootNode) {
vandebo@chromium.orgbd36ebe2011-04-08 01:22:45 +000065 // 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.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000072 static const int kNodeSize = 8;
73
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000074 SkAutoTUnref<SkPDFName> kidsName(new SkPDFName("Kids"));
75 SkAutoTUnref<SkPDFName> countName(new SkPDFName("Count"));
76 SkAutoTUnref<SkPDFName> parentName(new SkPDFName("Parent"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000077
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++) {
reed@google.com82065d62011-02-07 15:30:46 +000082 SkSafeRef(pages[i]);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000083 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
vandebo@chromium.orgbd36ebe2011-04-08 01:22:45 +000090 int treeCapacity = kNodeSize;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000091 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");
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000099 SkAutoTUnref<SkPDFObjRef> newNodeRef(new SkPDFObjRef(newNode));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000100
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000101 SkAutoTUnref<SkPDFArray> kids(new SkPDFArray);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000102 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());
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000107 kids->append(new SkPDFObjRef(curNodes[i]))->unref();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000108
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000109 // TODO(vandebo): put the objects in strict access order.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000110 // Probably doesn't matter because they are so small.
111 if (curNodes[i] != pages[0]) {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000112 pageTree->push(curNodes[i]); // Transfer reference.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000113 catalog->addObject(curNodes[i], false);
114 } else {
reed@google.com82065d62011-02-07 15:30:46 +0000115 SkSafeUnref(curNodes[i]);
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +0000116 catalog->addObject(curNodes[i], true);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000117 }
118 }
119
vandebo@chromium.org3c974422013-01-30 21:44:40 +0000120 // 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.
vandebo@chromium.orgbd36ebe2011-04-08 01:22:45 +0000127 int pageCount = treeCapacity;
vandebo@chromium.org3c974422013-01-30 21:44:40 +0000128 if (i == curNodes.count()) {
129 pageCount = ((pages.count() - 1) % treeCapacity) + 1;
vandebo@chromium.orgbd36ebe2011-04-08 01:22:45 +0000130 }
131 newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref();
vandebo@chromium.org3c974422013-01-30 21:44:40 +0000132 newNode->insert(kidsName.get(), kids.get());
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000133 nextRoundNodes.push(newNode); // Transfer reference.
134 }
135
136 curNodes = nextRoundNodes;
137 nextRoundNodes.rewind();
vandebo@chromium.orgbd36ebe2011-04-08 01:22:45 +0000138 treeCapacity *= kNodeSize;
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000139 } while (curNodes.count() > 1);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000140
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000141 pageTree->push(curNodes[0]); // Transfer reference.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000142 catalog->addObject(curNodes[0], false);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000143 if (rootNode) {
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000144 *rootNode = curNodes[0];
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000145 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000146}
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000147
148const SkTDArray<SkPDFFont*>& SkPDFPage::getFontResources() const {
149 return fDevice->getFontResources();
150}
vandebo@chromium.org98594282011-07-25 22:34:12 +0000151
152const SkPDFGlyphSetMap& SkPDFPage::getFontGlyphUsage() const {
153 return fDevice->getFontGlyphUsage();
154}
epoger@google.comb58772f2013-03-08 09:09:10 +0000155
156void SkPDFPage::appendDestinations(SkPDFDict* dict) {
157 fDevice->appendDestinations(dict, this);
158}