blob: 0633d308285d116ef121e5bd754927639aaf2337 [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 2011 Google Inc.
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
vandebo@chromium.org421d6442011-07-20 17:39:01 +000010#include "SkPDFCatalog.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000011#include "SkPDFDevice.h"
12#include "SkPDFDocument.h"
vandebo@chromium.org76d6de02012-03-21 20:31:08 +000013#include "SkPDFFont.h"
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000014#include "SkPDFPage.h"
15#include "SkPDFTypes.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000016#include "SkStream.h"
edisonn@google.com6addb192013-04-02 15:33:08 +000017#include "SkTSet.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000018
edisonn@google.com6addb192013-04-02 15:33:08 +000019static void addResourcesToCatalog(bool firstPage,
20 SkTSet<SkPDFObject*>* resourceSet,
21 SkPDFCatalog* catalog) {
22 for (int i = 0; i < resourceSet->count(); i++) {
23 catalog->addObject((*resourceSet)[i], firstPage);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000024 }
25}
26
vandebo@chromium.org98594282011-07-25 22:34:12 +000027static void perform_font_subsetting(SkPDFCatalog* catalog,
28 const SkTDArray<SkPDFPage*>& pages,
29 SkTDArray<SkPDFObject*>* substitutes) {
30 SkASSERT(catalog);
31 SkASSERT(substitutes);
32
33 SkPDFGlyphSetMap usage;
34 for (int i = 0; i < pages.count(); ++i) {
35 usage.merge(pages[i]->getFontGlyphUsage());
36 }
37 SkPDFGlyphSetMap::F2BIter iterator(usage);
commit-bot@chromium.orgaa537d42013-02-28 19:03:13 +000038 const SkPDFGlyphSetMap::FontGlyphSetPair* entry = iterator.next();
vandebo@chromium.org98594282011-07-25 22:34:12 +000039 while (entry) {
40 SkPDFFont* subsetFont =
41 entry->fFont->getFontSubset(entry->fGlyphSet);
42 if (subsetFont) {
43 catalog->setSubstitute(entry->fFont, subsetFont);
44 substitutes->push(subsetFont); // Transfer ownership to substitutes
45 }
46 entry = iterator.next();
47 }
48}
49
vandebo@chromium.org421d6442011-07-20 17:39:01 +000050SkPDFDocument::SkPDFDocument(Flags flags)
vandebo@chromium.org73322072011-06-21 21:19:41 +000051 : fXRefFileOffset(0),
reed@google.comaf777272012-09-20 18:19:26 +000052 fTrailerDict(NULL) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000053 fCatalog.reset(new SkPDFCatalog(flags));
reed@google.comaf777272012-09-20 18:19:26 +000054 fDocCatalog = SkNEW_ARGS(SkPDFDict, ("Catalog"));
55 fCatalog->addObject(fDocCatalog, true);
edisonn@google.com6addb192013-04-02 15:33:08 +000056 fFirstPageResources = NULL;
57 fOtherPageResources = NULL;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000058}
59
60SkPDFDocument::~SkPDFDocument() {
61 fPages.safeUnrefAll();
62
63 // The page tree has both child and parent pointers, so it creates a
64 // reference cycle. We must clear that cycle to properly reclaim memory.
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000065 for (int i = 0; i < fPageTree.count(); i++) {
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000066 fPageTree[i]->clear();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000067 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000068 fPageTree.safeUnrefAll();
edisonn@google.com6addb192013-04-02 15:33:08 +000069
70 if (fFirstPageResources) {
71 fFirstPageResources->safeUnrefAll();
72 }
73 if (fOtherPageResources) {
74 fOtherPageResources->safeUnrefAll();
75 }
76
vandebo@chromium.org98594282011-07-25 22:34:12 +000077 fSubstitutes.safeUnrefAll();
skia.committer@gmail.com4c5ea442012-09-21 02:01:01 +000078
reed@google.comaf777272012-09-20 18:19:26 +000079 fDocCatalog->unref();
80 SkSafeUnref(fTrailerDict);
edisonn@google.com6addb192013-04-02 15:33:08 +000081 SkDELETE(fFirstPageResources);
82 SkDELETE(fOtherPageResources);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000083}
84
85bool SkPDFDocument::emitPDF(SkWStream* stream) {
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000086 if (fPages.isEmpty()) {
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000087 return false;
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000088 }
89 for (int i = 0; i < fPages.count(); i++) {
90 if (fPages[i] == NULL) {
91 return false;
92 }
93 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000094
edisonn@google.com6addb192013-04-02 15:33:08 +000095 fFirstPageResources = SkNEW(SkTSet<SkPDFObject*>);
96 fOtherPageResources = SkNEW(SkTSet<SkPDFObject*>);
97
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000098 // We haven't emitted the document before if fPageTree is empty.
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000099 if (fPageTree.isEmpty()) {
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000100 SkPDFDict* pageTreeRoot;
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000101 SkPDFPage::GeneratePageTree(fPages, fCatalog.get(), &fPageTree,
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000102 &pageTreeRoot);
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000103 fDocCatalog->insert("Pages", new SkPDFObjRef(pageTreeRoot))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000104
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000105 /* TODO(vandebo): output intent
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000106 SkAutoTUnref<SkPDFDict> outputIntent = new SkPDFDict("OutputIntent");
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000107 outputIntent->insert("S", new SkPDFName("GTS_PDFA1"))->unref();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000108 outputIntent->insert("OutputConditionIdentifier",
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +0000109 new SkPDFString("sRGB"))->unref();
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000110 SkAutoTUnref<SkPDFArray> intentArray = new SkPDFArray;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000111 intentArray->append(outputIntent.get());
112 fDocCatalog->insert("OutputIntent", intentArray.get());
113 */
114
vandebo@chromium.org1a475232013-05-10 18:29:43 +0000115 SkAutoTUnref<SkPDFDict> dests(SkNEW(SkPDFDict));
epoger@google.comb58772f2013-03-08 09:09:10 +0000116
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000117 bool firstPage = true;
edisonn@google.com6addb192013-04-02 15:33:08 +0000118 /* The references returned in newResources are transfered to
119 * fFirstPageResources or fOtherPageResources depending on firstPage and
120 * knownResources doesn't have a reference but just relies on the other
121 * two sets to maintain a reference.
122 */
123 SkTSet<SkPDFObject*> knownResources;
124
125 // mergeInto returns the number of duplicates.
126 // If there are duplicates, there is a bug and we mess ref counting.
127 SkDEBUGCODE(int duplicates =) knownResources.mergeInto(*fFirstPageResources);
128 SkASSERT(duplicates == 0);
129
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000130 for (int i = 0; i < fPages.count(); i++) {
edisonn@google.com6addb192013-04-02 15:33:08 +0000131 if (i == 1) {
robertphillips@google.comacef3c42013-03-20 14:42:51 +0000132 firstPage = false;
edisonn@google.com6addb192013-04-02 15:33:08 +0000133 SkDEBUGCODE(duplicates =) knownResources.mergeInto(*fOtherPageResources);
robertphillips@google.comacef3c42013-03-20 14:42:51 +0000134 }
edisonn@google.com6addb192013-04-02 15:33:08 +0000135 SkTSet<SkPDFObject*> newResources;
136 fPages[i]->finalizePage(
137 fCatalog.get(), firstPage, knownResources, &newResources);
138 addResourcesToCatalog(firstPage, &newResources, fCatalog.get());
139 if (firstPage) {
140 SkDEBUGCODE(duplicates =) fFirstPageResources->mergeInto(newResources);
141 } else {
142 SkDEBUGCODE(duplicates =) fOtherPageResources->mergeInto(newResources);
143 }
144 SkASSERT(duplicates == 0);
145
146 SkDEBUGCODE(duplicates =) knownResources.mergeInto(newResources);
147 SkASSERT(duplicates == 0);
148
149 fPages[i]->appendDestinations(dests);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000150 }
151
vandebo@chromium.org1a475232013-05-10 18:29:43 +0000152 if (dests->size() > 0) {
153 SkPDFDict* raw_dests = dests.get();
154 fFirstPageResources->add(dests.detach()); // Transfer ownership.
155 fCatalog->addObject(raw_dests, true /* onFirstPage */);
156 fDocCatalog->insert("Dests", SkNEW_ARGS(SkPDFObjRef, (raw_dests)))->unref();
157 }
epoger@google.comb58772f2013-03-08 09:09:10 +0000158
vandebo@chromium.org98594282011-07-25 22:34:12 +0000159 // Build font subsetting info before proceeding.
160 perform_font_subsetting(fCatalog.get(), fPages, &fSubstitutes);
161
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000162 // Figure out the size of things and inform the catalog of file offsets.
163 off_t fileOffset = headerSize();
reed@google.comaf777272012-09-20 18:19:26 +0000164 fileOffset += fCatalog->setFileOffset(fDocCatalog, fileOffset);
vandebo@chromium.org30580f62012-07-12 20:22:04 +0000165 fileOffset += fCatalog->setFileOffset(fPages[0], fileOffset);
caryclark@google.com1445a0d2012-06-06 12:04:24 +0000166 fileOffset += fPages[0]->getPageSize(fCatalog.get(),
167 (size_t) fileOffset);
edisonn@google.com6addb192013-04-02 15:33:08 +0000168 for (int i = 0; i < fFirstPageResources->count(); i++) {
169 fileOffset += fCatalog->setFileOffset((*fFirstPageResources)[i],
vandebo@chromium.org30580f62012-07-12 20:22:04 +0000170 fileOffset);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000171 }
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000172 // Add the size of resources of substitute objects used on page 1.
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000173 fileOffset += fCatalog->setSubstituteResourcesOffsets(fileOffset, true);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000174 if (fPages.count() > 1) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000175 // TODO(vandebo): For linearized format, save the start of the
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000176 // first page xref table and calculate the size.
177 }
178
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000179 for (int i = 0; i < fPageTree.count(); i++) {
vandebo@chromium.org30580f62012-07-12 20:22:04 +0000180 fileOffset += fCatalog->setFileOffset(fPageTree[i], fileOffset);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000181 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000182
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000183 for (int i = 1; i < fPages.count(); i++) {
vandebo@chromium.org30580f62012-07-12 20:22:04 +0000184 fileOffset += fPages[i]->getPageSize(fCatalog.get(), fileOffset);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000185 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000186
edisonn@google.com6addb192013-04-02 15:33:08 +0000187 for (int i = 0; i < fOtherPageResources->count(); i++) {
188 fileOffset += fCatalog->setFileOffset(
189 (*fOtherPageResources)[i], fileOffset);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000190 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000191
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000192 fileOffset += fCatalog->setSubstituteResourcesOffsets(fileOffset,
193 false);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000194 fXRefFileOffset = fileOffset;
195 }
196
197 emitHeader(stream);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000198 fDocCatalog->emitObject(stream, fCatalog.get(), true);
199 fPages[0]->emitObject(stream, fCatalog.get(), true);
200 fPages[0]->emitPage(stream, fCatalog.get());
edisonn@google.com6addb192013-04-02 15:33:08 +0000201 for (int i = 0; i < fFirstPageResources->count(); i++) {
202 (*fFirstPageResources)[i]->emit(stream, fCatalog.get(), true);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000203 }
204 fCatalog->emitSubstituteResources(stream, true);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000205 // TODO(vandebo): Support linearized format
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000206 // if (fPages.size() > 1) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000207 // // TODO(vandebo): Save the file offset for the first page xref table.
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000208 // fCatalog->emitXrefTable(stream, true);
209 // }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000210
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000211 for (int i = 0; i < fPageTree.count(); i++) {
212 fPageTree[i]->emitObject(stream, fCatalog.get(), true);
213 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000214
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000215 for (int i = 1; i < fPages.count(); i++) {
216 fPages[i]->emitPage(stream, fCatalog.get());
217 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000218
edisonn@google.com6addb192013-04-02 15:33:08 +0000219 for (int i = 0; i < fOtherPageResources->count(); i++) {
220 (*fOtherPageResources)[i]->emit(stream, fCatalog.get(), true);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000221 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000222
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000223 fCatalog->emitSubstituteResources(stream, false);
224 int64_t objCount = fCatalog->emitXrefTable(stream, fPages.count() > 1);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000225 emitFooter(stream, objCount);
226 return true;
227}
228
reed@google.com1feb3302011-07-20 18:43:19 +0000229bool SkPDFDocument::setPage(int pageNumber, SkPDFDevice* pdfDevice) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000230 if (!fPageTree.isEmpty()) {
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000231 return false;
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +0000232 }
233
234 pageNumber--;
235 SkASSERT(pageNumber >= 0);
236
vandebo@chromium.org00223fa2011-07-22 01:48:55 +0000237 if (pageNumber >= fPages.count()) {
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +0000238 int oldSize = fPages.count();
239 fPages.setCount(pageNumber + 1);
240 for (int i = oldSize; i <= pageNumber; i++) {
241 fPages[i] = NULL;
242 }
243 }
244
245 SkPDFPage* page = new SkPDFPage(pdfDevice);
246 SkSafeUnref(fPages[pageNumber]);
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000247 fPages[pageNumber] = page; // Reference from new passed to fPages.
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +0000248 return true;
249}
250
reed@google.com1feb3302011-07-20 18:43:19 +0000251bool SkPDFDocument::appendPage(SkPDFDevice* pdfDevice) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000252 if (!fPageTree.isEmpty()) {
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +0000253 return false;
254 }
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000255
256 SkPDFPage* page = new SkPDFPage(pdfDevice);
257 fPages.push(page); // Reference from new passed to fPages.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000258 return true;
259}
260
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +0000261void SkPDFDocument::getCountOfFontTypes(
262 int counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1]) const {
vandebo@chromium.org6e08bfe2012-03-26 23:58:04 +0000263 sk_bzero(counts, sizeof(int) *
264 (SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1));
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +0000265 SkTDArray<SkFontID> seenFonts;
266
267 for (int pageNumber = 0; pageNumber < fPages.count(); pageNumber++) {
268 const SkTDArray<SkPDFFont*>& fontResources =
269 fPages[pageNumber]->getFontResources();
270 for (int font = 0; font < fontResources.count(); font++) {
271 SkFontID fontID = fontResources[font]->typeface()->uniqueID();
272 if (seenFonts.find(fontID) == -1) {
273 counts[fontResources[font]->getType()]++;
274 seenFonts.push(fontID);
275 }
276 }
277 }
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +0000278}
279
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000280void SkPDFDocument::emitHeader(SkWStream* stream) {
281 stream->writeText("%PDF-1.4\n%");
282 // The PDF spec recommends including a comment with four bytes, all
283 // with their high bits set. This is "Skia" with the high bits set.
284 stream->write32(0xD3EBE9E1);
285 stream->writeText("\n");
286}
287
288size_t SkPDFDocument::headerSize() {
289 SkDynamicMemoryWStream buffer;
290 emitHeader(&buffer);
291 return buffer.getOffset();
292}
293
294void SkPDFDocument::emitFooter(SkWStream* stream, int64_t objCount) {
reed@google.comaf777272012-09-20 18:19:26 +0000295 if (NULL == fTrailerDict) {
296 fTrailerDict = SkNEW(SkPDFDict);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000297
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000298 // TODO(vandebo): Linearized format will take a Prev entry too.
299 // TODO(vandebo): PDF/A requires an ID entry.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000300 fTrailerDict->insertInt("Size", int(objCount));
reed@google.comaf777272012-09-20 18:19:26 +0000301 fTrailerDict->insert("Root", new SkPDFObjRef(fDocCatalog))->unref();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000302 }
303
304 stream->writeText("trailer\n");
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000305 fTrailerDict->emitObject(stream, fCatalog.get(), false);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000306 stream->writeText("\nstartxref\n");
307 stream->writeBigDecAsText(fXRefFileOffset);
308 stream->writeText("\n%%EOF");
309}