blob: 77a9fdbb6194f08ad8abe5b689c9a94cd448caa3 [file] [log] [blame]
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkPDFDevice.h"
18#include "SkPDFDocument.h"
19#include "SkPDFPage.h"
20#include "SkStream.h"
21
vandebo@chromium.orga5180862010-10-26 19:48:49 +000022// Add the resources, starting at firstIndex to the catalog, removing any dupes.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000023// A hash table would be really nice here.
vandebo@chromium.orga5180862010-10-26 19:48:49 +000024void addResourcesToCatalog(int firstIndex, bool firstPage,
25 SkTDArray<SkPDFObject*>* resourceList,
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000026 SkPDFCatalog* catalog) {
vandebo@chromium.orga5180862010-10-26 19:48:49 +000027 for (int i = firstIndex; i < resourceList->count(); i++) {
28 int index = resourceList->find((*resourceList)[i]);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000029 if (index != i) {
vandebo@chromium.orga5180862010-10-26 19:48:49 +000030 (*resourceList)[i]->unref();
31 resourceList->removeShuffle(i);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000032 i--;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000033 } else {
vandebo@chromium.orga5180862010-10-26 19:48:49 +000034 catalog->addObject((*resourceList)[i], firstPage);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000035 }
36 }
37}
38
39SkPDFDocument::SkPDFDocument() : fXRefFileOffset(0) {
40 fDocCatalog = new SkPDFDict("Catalog");
41 fDocCatalog->unref(); // SkRefPtr and new both took a reference.
42 fCatalog.addObject(fDocCatalog.get(), true);
43}
44
45SkPDFDocument::~SkPDFDocument() {
46 fPages.safeUnrefAll();
47
48 // The page tree has both child and parent pointers, so it creates a
49 // reference cycle. We must clear that cycle to properly reclaim memory.
50 for (int i = 0; i < fPageTree.count(); i++)
51 fPageTree[i]->clear();
52 fPageTree.safeUnrefAll();
53 fPageResources.safeUnrefAll();
54}
55
56bool SkPDFDocument::emitPDF(SkWStream* stream) {
57 if (fPages.isEmpty())
58 return false;
59
60 // We haven't emitted the document before if fPageTree is empty.
61 if (fPageTree.count() == 0) {
62 SkPDFDict* pageTreeRoot;
63 SkPDFPage::generatePageTree(fPages, &fCatalog, &fPageTree,
64 &pageTreeRoot);
65 SkRefPtr<SkPDFObjRef> pageTreeRootRef = new SkPDFObjRef(pageTreeRoot);
66 pageTreeRootRef->unref(); // SkRefPtr and new both took a reference.
67 fDocCatalog->insert("Pages", pageTreeRootRef.get());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000068
69 /* TODO(vandebo) output intent
70 SkRefPtr<SkPDFDict> outputIntent = new SkPDFDict("OutputIntent");
71 outputIntent->unref(); // SkRefPtr and new both took a reference.
72 SkRefPtr<SkPDFName> intentSubtype = new SkPDFName("GTS_PDFA1");
73 intentSubtype->unref(); // SkRefPtr and new both took a reference.
74 outputIntent->insert("S", intentSubtype.get());
75 SkRefPtr<SkPDFString> intentIdentifier = new SkPDFString("sRGB");
76 intentIdentifier->unref(); // SkRefPtr and new both took a reference.
77 outputIntent->insert("OutputConditionIdentifier",
78 intentIdentifier.get());
79 SkRefPtr<SkPDFArray> intentArray = new SkPDFArray;
80 intentArray->unref(); // SkRefPtr and new both took a reference.
81 intentArray->append(outputIntent.get());
82 fDocCatalog->insert("OutputIntent", intentArray.get());
83 */
84
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000085 bool first_page = true;
86 for (int i = 0; i < fPages.count(); i++) {
87 int resourceCount = fPageResources.count();
88 fPages[i]->finalizePage(&fCatalog, first_page, &fPageResources);
vandebo@chromium.orga5180862010-10-26 19:48:49 +000089 addResourcesToCatalog(resourceCount, first_page, &fPageResources,
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000090 &fCatalog);
91 if (i == 0) {
92 first_page = false;
vandebo@chromium.orga5180862010-10-26 19:48:49 +000093 fSecondPageFirstResourceIndex = fPageResources.count();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000094 }
95 }
96
97 // Figure out the size of things and inform the catalog of file offsets.
98 off_t fileOffset = headerSize();
99 fileOffset += fCatalog.setFileOffset(fDocCatalog.get(), fileOffset);
100 fileOffset += fCatalog.setFileOffset(fPages[0], fileOffset);
101 fileOffset += fPages[0]->getPageSize(&fCatalog, fileOffset);
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000102 for (int i = 0; i < fSecondPageFirstResourceIndex; i++)
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000103 fileOffset += fCatalog.setFileOffset(fPageResources[i], fileOffset);
104 if (fPages.count() > 1) {
105 // TODO(vandebo) For linearized format, save the start of the
106 // first page xref table and calculate the size.
107 }
108
109 for (int i = 0; i < fPageTree.count(); i++)
110 fileOffset += fCatalog.setFileOffset(fPageTree[i], fileOffset);
111
112 for (int i = 1; i < fPages.count(); i++)
113 fileOffset += fPages[i]->getPageSize(&fCatalog, fileOffset);
114
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000115 for (int i = fSecondPageFirstResourceIndex;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000116 i < fPageResources.count();
117 i++)
118 fileOffset += fCatalog.setFileOffset(fPageResources[i], fileOffset);
119
120 fXRefFileOffset = fileOffset;
121 }
122
123 emitHeader(stream);
124 fDocCatalog->emitObject(stream, &fCatalog, true);
125 fPages[0]->emitObject(stream, &fCatalog, true);
126 fPages[0]->emitPage(stream, &fCatalog);
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000127 for (int i = 0; i < fSecondPageFirstResourceIndex; i++)
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000128 fPageResources[i]->emitObject(stream, &fCatalog, true);
129 // TODO(vandebo) support linearized format
130 //if (fPages.size() > 1) {
131 // // TODO(vandebo) save the file offset for the first page xref table.
132 // fCatalog.emitXrefTable(stream, true);
133 //}
134
135 for (int i = 0; i < fPageTree.count(); i++)
136 fPageTree[i]->emitObject(stream, &fCatalog, true);
137
138 for (int i = 1; i < fPages.count(); i++)
139 fPages[i]->emitPage(stream, &fCatalog);
140
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000141 for (int i = fSecondPageFirstResourceIndex; i < fPageResources.count(); i++)
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000142 fPageResources[i]->emitObject(stream, &fCatalog, true);
143
144 int64_t objCount = fCatalog.emitXrefTable(stream, fPages.count() > 1);
145 emitFooter(stream, objCount);
146 return true;
147}
148
149bool SkPDFDocument::appendPage(const SkRefPtr<SkPDFDevice>& pdfDevice) {
150 if (fPageTree.count() != 0)
151 return false;
152
153 SkPDFPage* page = new SkPDFPage(pdfDevice);
154 fPages.push(page); // Reference from new passed to fPages.
155 // The rest of the pages will be added to the catalog along with the rest
156 // of the page tree. But the first page has to be marked as such, so we
157 // handle it here.
158 if (fPages.count() == 1)
159 fCatalog.addObject(page, true);
160 return true;
161}
162
163void SkPDFDocument::emitHeader(SkWStream* stream) {
164 stream->writeText("%PDF-1.4\n%");
165 // The PDF spec recommends including a comment with four bytes, all
166 // with their high bits set. This is "Skia" with the high bits set.
167 stream->write32(0xD3EBE9E1);
168 stream->writeText("\n");
169}
170
171size_t SkPDFDocument::headerSize() {
172 SkDynamicMemoryWStream buffer;
173 emitHeader(&buffer);
174 return buffer.getOffset();
175}
176
177void SkPDFDocument::emitFooter(SkWStream* stream, int64_t objCount) {
178 if (fTrailerDict.get() == NULL) {
179 fTrailerDict = new SkPDFDict();
180 fTrailerDict->unref(); // SkRefPtr and new both took a reference.
181
182 SkPDFInt* objCountInt = new SkPDFInt(objCount);
183 fTrailerDict->insert("Size", objCountInt);
184 objCountInt->unref(); // insert took a ref and we're done with it.
185
186 // TODO(vandebo) Linearized format will take a Prev entry too.
187
188 SkPDFObjRef* docCatalogRef = new SkPDFObjRef(fDocCatalog.get());
189 fTrailerDict->insert("Root", docCatalogRef);
190 docCatalogRef->unref(); // insert took a ref and we're done with it.
191
192 // TODO(vandebo) PDF/A requires an ID entry.
193 }
194
195 stream->writeText("trailer\n");
196 fTrailerDict->emitObject(stream, &fCatalog, false);
197 stream->writeText("\nstartxref\n");
198 stream->writeBigDecAsText(fXRefFileOffset);
199 stream->writeText("\n%%EOF");
200}