epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 10 | #include "SkPDFCatalog.h" |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 11 | #include "SkPDFDevice.h" |
| 12 | #include "SkPDFDocument.h" |
vandebo@chromium.org | 76d6de0 | 2012-03-21 20:31:08 +0000 | [diff] [blame] | 13 | #include "SkPDFFont.h" |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 14 | #include "SkPDFPage.h" |
| 15 | #include "SkPDFTypes.h" |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 17 | #include "SkTSet.h" |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 18 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 19 | static 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.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 27 | static 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.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 38 | const SkPDFGlyphSetMap::FontGlyphSetPair* entry = iterator.next(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 39 | 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.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 50 | SkPDFDocument::SkPDFDocument(Flags flags) |
vandebo@chromium.org | 7332207 | 2011-06-21 21:19:41 +0000 | [diff] [blame] | 51 | : fXRefFileOffset(0), |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 52 | fTrailerDict(NULL) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 53 | fCatalog.reset(new SkPDFCatalog(flags)); |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 54 | fDocCatalog = SkNEW_ARGS(SkPDFDict, ("Catalog")); |
| 55 | fCatalog->addObject(fDocCatalog, true); |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 56 | fFirstPageResources = NULL; |
| 57 | fOtherPageResources = NULL; |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | SkPDFDocument::~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.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 65 | for (int i = 0; i < fPageTree.count(); i++) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 66 | fPageTree[i]->clear(); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 67 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 68 | fPageTree.safeUnrefAll(); |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 69 | |
| 70 | if (fFirstPageResources) { |
| 71 | fFirstPageResources->safeUnrefAll(); |
| 72 | } |
| 73 | if (fOtherPageResources) { |
| 74 | fOtherPageResources->safeUnrefAll(); |
| 75 | } |
| 76 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 77 | fSubstitutes.safeUnrefAll(); |
skia.committer@gmail.com | 4c5ea44 | 2012-09-21 02:01:01 +0000 | [diff] [blame] | 78 | |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 79 | fDocCatalog->unref(); |
| 80 | SkSafeUnref(fTrailerDict); |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 81 | SkDELETE(fFirstPageResources); |
| 82 | SkDELETE(fOtherPageResources); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool SkPDFDocument::emitPDF(SkWStream* stream) { |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 86 | if (fPages.isEmpty()) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 87 | return false; |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 88 | } |
| 89 | for (int i = 0; i < fPages.count(); i++) { |
| 90 | if (fPages[i] == NULL) { |
| 91 | return false; |
| 92 | } |
| 93 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 94 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 95 | fFirstPageResources = SkNEW(SkTSet<SkPDFObject*>); |
| 96 | fOtherPageResources = SkNEW(SkTSet<SkPDFObject*>); |
| 97 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 98 | // We haven't emitted the document before if fPageTree is empty. |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 99 | if (fPageTree.isEmpty()) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 100 | SkPDFDict* pageTreeRoot; |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 101 | SkPDFPage::GeneratePageTree(fPages, fCatalog.get(), &fPageTree, |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 102 | &pageTreeRoot); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 103 | fDocCatalog->insert("Pages", new SkPDFObjRef(pageTreeRoot))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 104 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 105 | /* TODO(vandebo): output intent |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 106 | SkAutoTUnref<SkPDFDict> outputIntent = new SkPDFDict("OutputIntent"); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 107 | outputIntent->insert("S", new SkPDFName("GTS_PDFA1"))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 108 | outputIntent->insert("OutputConditionIdentifier", |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 109 | new SkPDFString("sRGB"))->unref(); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 110 | SkAutoTUnref<SkPDFArray> intentArray = new SkPDFArray; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 111 | intentArray->append(outputIntent.get()); |
| 112 | fDocCatalog->insert("OutputIntent", intentArray.get()); |
| 113 | */ |
| 114 | |
vandebo@chromium.org | 1a47523 | 2013-05-10 18:29:43 +0000 | [diff] [blame] | 115 | SkAutoTUnref<SkPDFDict> dests(SkNEW(SkPDFDict)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 116 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 117 | bool firstPage = true; |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 118 | /* 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.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 130 | for (int i = 0; i < fPages.count(); i++) { |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 131 | if (i == 1) { |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 132 | firstPage = false; |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 133 | SkDEBUGCODE(duplicates =) knownResources.mergeInto(*fOtherPageResources); |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 134 | } |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 135 | 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.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
vandebo@chromium.org | 1a47523 | 2013-05-10 18:29:43 +0000 | [diff] [blame] | 152 | 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.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 158 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 159 | // Build font subsetting info before proceeding. |
| 160 | perform_font_subsetting(fCatalog.get(), fPages, &fSubstitutes); |
| 161 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 162 | // Figure out the size of things and inform the catalog of file offsets. |
| 163 | off_t fileOffset = headerSize(); |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 164 | fileOffset += fCatalog->setFileOffset(fDocCatalog, fileOffset); |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 165 | fileOffset += fCatalog->setFileOffset(fPages[0], fileOffset); |
caryclark@google.com | 1445a0d | 2012-06-06 12:04:24 +0000 | [diff] [blame] | 166 | fileOffset += fPages[0]->getPageSize(fCatalog.get(), |
| 167 | (size_t) fileOffset); |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 168 | for (int i = 0; i < fFirstPageResources->count(); i++) { |
| 169 | fileOffset += fCatalog->setFileOffset((*fFirstPageResources)[i], |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 170 | fileOffset); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 171 | } |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 172 | // Add the size of resources of substitute objects used on page 1. |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 173 | fileOffset += fCatalog->setSubstituteResourcesOffsets(fileOffset, true); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 174 | if (fPages.count() > 1) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 175 | // TODO(vandebo): For linearized format, save the start of the |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 176 | // first page xref table and calculate the size. |
| 177 | } |
| 178 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 179 | for (int i = 0; i < fPageTree.count(); i++) { |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 180 | fileOffset += fCatalog->setFileOffset(fPageTree[i], fileOffset); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 181 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 182 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 183 | for (int i = 1; i < fPages.count(); i++) { |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 184 | fileOffset += fPages[i]->getPageSize(fCatalog.get(), fileOffset); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 185 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 186 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 187 | for (int i = 0; i < fOtherPageResources->count(); i++) { |
| 188 | fileOffset += fCatalog->setFileOffset( |
| 189 | (*fOtherPageResources)[i], fileOffset); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 190 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 191 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 192 | fileOffset += fCatalog->setSubstituteResourcesOffsets(fileOffset, |
| 193 | false); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 194 | fXRefFileOffset = fileOffset; |
| 195 | } |
| 196 | |
| 197 | emitHeader(stream); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 198 | fDocCatalog->emitObject(stream, fCatalog.get(), true); |
| 199 | fPages[0]->emitObject(stream, fCatalog.get(), true); |
| 200 | fPages[0]->emitPage(stream, fCatalog.get()); |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 201 | for (int i = 0; i < fFirstPageResources->count(); i++) { |
| 202 | (*fFirstPageResources)[i]->emit(stream, fCatalog.get(), true); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 203 | } |
| 204 | fCatalog->emitSubstituteResources(stream, true); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 205 | // TODO(vandebo): Support linearized format |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 206 | // if (fPages.size() > 1) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 207 | // // TODO(vandebo): Save the file offset for the first page xref table. |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 208 | // fCatalog->emitXrefTable(stream, true); |
| 209 | // } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 210 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 211 | for (int i = 0; i < fPageTree.count(); i++) { |
| 212 | fPageTree[i]->emitObject(stream, fCatalog.get(), true); |
| 213 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 214 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 215 | for (int i = 1; i < fPages.count(); i++) { |
| 216 | fPages[i]->emitPage(stream, fCatalog.get()); |
| 217 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 218 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 219 | for (int i = 0; i < fOtherPageResources->count(); i++) { |
| 220 | (*fOtherPageResources)[i]->emit(stream, fCatalog.get(), true); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 221 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 222 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 223 | fCatalog->emitSubstituteResources(stream, false); |
| 224 | int64_t objCount = fCatalog->emitXrefTable(stream, fPages.count() > 1); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 225 | emitFooter(stream, objCount); |
| 226 | return true; |
| 227 | } |
| 228 | |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 229 | bool SkPDFDocument::setPage(int pageNumber, SkPDFDevice* pdfDevice) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 230 | if (!fPageTree.isEmpty()) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 231 | return false; |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | pageNumber--; |
| 235 | SkASSERT(pageNumber >= 0); |
| 236 | |
vandebo@chromium.org | 00223fa | 2011-07-22 01:48:55 +0000 | [diff] [blame] | 237 | if (pageNumber >= fPages.count()) { |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 238 | 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.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 247 | fPages[pageNumber] = page; // Reference from new passed to fPages. |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 251 | bool SkPDFDocument::appendPage(SkPDFDevice* pdfDevice) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 252 | if (!fPageTree.isEmpty()) { |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 255 | |
| 256 | SkPDFPage* page = new SkPDFPage(pdfDevice); |
| 257 | fPages.push(page); // Reference from new passed to fPages. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 261 | void SkPDFDocument::getCountOfFontTypes( |
| 262 | int counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1]) const { |
vandebo@chromium.org | 6e08bfe | 2012-03-26 23:58:04 +0000 | [diff] [blame] | 263 | sk_bzero(counts, sizeof(int) * |
| 264 | (SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1)); |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 265 | 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.org | d897bfb | 2011-05-31 18:18:21 +0000 | [diff] [blame] | 278 | } |
| 279 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 280 | void 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 | |
| 288 | size_t SkPDFDocument::headerSize() { |
| 289 | SkDynamicMemoryWStream buffer; |
| 290 | emitHeader(&buffer); |
| 291 | return buffer.getOffset(); |
| 292 | } |
| 293 | |
| 294 | void SkPDFDocument::emitFooter(SkWStream* stream, int64_t objCount) { |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 295 | if (NULL == fTrailerDict) { |
| 296 | fTrailerDict = SkNEW(SkPDFDict); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 297 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 298 | // TODO(vandebo): Linearized format will take a Prev entry too. |
| 299 | // TODO(vandebo): PDF/A requires an ID entry. |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 300 | fTrailerDict->insertInt("Size", int(objCount)); |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 301 | fTrailerDict->insert("Root", new SkPDFObjRef(fDocCatalog))->unref(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | stream->writeText("trailer\n"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 305 | fTrailerDict->emitObject(stream, fCatalog.get(), false); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 306 | stream->writeText("\nstartxref\n"); |
| 307 | stream->writeBigDecAsText(fXRefFileOffset); |
| 308 | stream->writeText("\n%%EOF"); |
| 309 | } |