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" |
| 17 | |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 18 | // Add the resources, starting at firstIndex to the catalog, removing any dupes. |
| 19 | // A hash table would be really nice here. |
| 20 | static void addResourcesToCatalog(int firstIndex, bool firstPage, |
| 21 | SkTDArray<SkPDFObject*>* resourceList, |
| 22 | SkPDFCatalog* catalog) { |
| 23 | for (int i = firstIndex; i < resourceList->count(); i++) { |
| 24 | int index = resourceList->find((*resourceList)[i]); |
| 25 | if (index != i) { |
| 26 | (*resourceList)[i]->unref(); |
| 27 | resourceList->removeShuffle(i); |
| 28 | i--; |
| 29 | } else { |
| 30 | catalog->addObject((*resourceList)[i], firstPage); |
| 31 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 35 | static void perform_font_subsetting(SkPDFCatalog* catalog, |
| 36 | const SkTDArray<SkPDFPage*>& pages, |
| 37 | SkTDArray<SkPDFObject*>* substitutes) { |
| 38 | SkASSERT(catalog); |
| 39 | SkASSERT(substitutes); |
| 40 | |
| 41 | SkPDFGlyphSetMap usage; |
| 42 | for (int i = 0; i < pages.count(); ++i) { |
| 43 | usage.merge(pages[i]->getFontGlyphUsage()); |
| 44 | } |
| 45 | SkPDFGlyphSetMap::F2BIter iterator(usage); |
commit-bot@chromium.org | aa537d4 | 2013-02-28 19:03:13 +0000 | [diff] [blame] | 46 | const SkPDFGlyphSetMap::FontGlyphSetPair* entry = iterator.next(); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 47 | while (entry) { |
| 48 | SkPDFFont* subsetFont = |
| 49 | entry->fFont->getFontSubset(entry->fGlyphSet); |
| 50 | if (subsetFont) { |
| 51 | catalog->setSubstitute(entry->fFont, subsetFont); |
| 52 | substitutes->push(subsetFont); // Transfer ownership to substitutes |
| 53 | } |
| 54 | entry = iterator.next(); |
| 55 | } |
| 56 | } |
| 57 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 58 | SkPDFDocument::SkPDFDocument(Flags flags) |
vandebo@chromium.org | 7332207 | 2011-06-21 21:19:41 +0000 | [diff] [blame] | 59 | : fXRefFileOffset(0), |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 60 | fSecondPageFirstResourceIndex(0), |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 61 | fTrailerDict(NULL) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 62 | fCatalog.reset(new SkPDFCatalog(flags)); |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 63 | fDocCatalog = SkNEW_ARGS(SkPDFDict, ("Catalog")); |
| 64 | fCatalog->addObject(fDocCatalog, true); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | SkPDFDocument::~SkPDFDocument() { |
| 68 | fPages.safeUnrefAll(); |
| 69 | |
| 70 | // The page tree has both child and parent pointers, so it creates a |
| 71 | // reference cycle. We must clear that cycle to properly reclaim memory. |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 72 | for (int i = 0; i < fPageTree.count(); i++) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 73 | fPageTree[i]->clear(); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 74 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 75 | fPageTree.safeUnrefAll(); |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 76 | fPageResources.safeUnrefAll(); |
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); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool SkPDFDocument::emitPDF(SkWStream* stream) { |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 84 | if (fPages.isEmpty()) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 85 | return false; |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 86 | } |
| 87 | for (int i = 0; i < fPages.count(); i++) { |
| 88 | if (fPages[i] == NULL) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 92 | |
| 93 | // We haven't emitted the document before if fPageTree is empty. |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 94 | if (fPageTree.isEmpty()) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 95 | SkPDFDict* pageTreeRoot; |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 96 | SkPDFPage::GeneratePageTree(fPages, fCatalog.get(), &fPageTree, |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 97 | &pageTreeRoot); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 98 | fDocCatalog->insert("Pages", new SkPDFObjRef(pageTreeRoot))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 99 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 100 | /* TODO(vandebo): output intent |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 101 | SkAutoTUnref<SkPDFDict> outputIntent = new SkPDFDict("OutputIntent"); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 102 | outputIntent->insert("S", new SkPDFName("GTS_PDFA1"))->unref(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 103 | outputIntent->insert("OutputConditionIdentifier", |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 104 | new SkPDFString("sRGB"))->unref(); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 105 | SkAutoTUnref<SkPDFArray> intentArray = new SkPDFArray; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 106 | intentArray->append(outputIntent.get()); |
| 107 | fDocCatalog->insert("OutputIntent", intentArray.get()); |
| 108 | */ |
| 109 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 110 | SkPDFDict* dests = SkNEW(SkPDFDict); // fPageResources owns reference |
| 111 | fCatalog->addObject(dests, true /* onFirstPage */); |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 112 | fPageResources.push(dests); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 113 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 114 | bool firstPage = true; |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 115 | for (int i = 0; i < fPages.count(); i++) { |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 116 | int resourceCount = fPageResources.count(); |
| 117 | fPages[i]->finalizePage(fCatalog.get(), firstPage, &fPageResources); |
| 118 | addResourcesToCatalog(resourceCount, firstPage, &fPageResources, |
| 119 | fCatalog.get()); |
edisonn@google.com | 66bedbb | 2013-03-19 17:19:05 +0000 | [diff] [blame] | 120 | fPages[i]->appendDestinations(dests); |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 121 | if (i == 0) { |
| 122 | firstPage = false; |
| 123 | fSecondPageFirstResourceIndex = fPageResources.count(); |
| 124 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 125 | } |
| 126 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 127 | fDocCatalog->insert("Dests", SkNEW_ARGS(SkPDFObjRef, (dests)))->unref(); |
| 128 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 129 | // Build font subsetting info before proceeding. |
| 130 | perform_font_subsetting(fCatalog.get(), fPages, &fSubstitutes); |
| 131 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 132 | // Figure out the size of things and inform the catalog of file offsets. |
| 133 | off_t fileOffset = headerSize(); |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 134 | fileOffset += fCatalog->setFileOffset(fDocCatalog, fileOffset); |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 135 | fileOffset += fCatalog->setFileOffset(fPages[0], fileOffset); |
caryclark@google.com | 1445a0d | 2012-06-06 12:04:24 +0000 | [diff] [blame] | 136 | fileOffset += fPages[0]->getPageSize(fCatalog.get(), |
| 137 | (size_t) fileOffset); |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 138 | for (int i = 0; i < fSecondPageFirstResourceIndex; i++) { |
| 139 | fileOffset += fCatalog->setFileOffset(fPageResources[i], |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 140 | fileOffset); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 141 | } |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 142 | // 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] | 143 | fileOffset += fCatalog->setSubstituteResourcesOffsets(fileOffset, true); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 144 | if (fPages.count() > 1) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 145 | // TODO(vandebo): For linearized format, save the start of the |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 146 | // first page xref table and calculate the size. |
| 147 | } |
| 148 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 149 | for (int i = 0; i < fPageTree.count(); i++) { |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 150 | fileOffset += fCatalog->setFileOffset(fPageTree[i], fileOffset); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 151 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 152 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 153 | for (int i = 1; i < fPages.count(); i++) { |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 154 | fileOffset += fPages[i]->getPageSize(fCatalog.get(), fileOffset); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 155 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 156 | |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 157 | for (int i = fSecondPageFirstResourceIndex; |
| 158 | i < fPageResources.count(); |
| 159 | i++) { |
| 160 | fileOffset += fCatalog->setFileOffset(fPageResources[i], |
| 161 | fileOffset); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 162 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 163 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 164 | fileOffset += fCatalog->setSubstituteResourcesOffsets(fileOffset, |
| 165 | false); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 166 | fXRefFileOffset = fileOffset; |
| 167 | } |
| 168 | |
| 169 | emitHeader(stream); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 170 | fDocCatalog->emitObject(stream, fCatalog.get(), true); |
| 171 | fPages[0]->emitObject(stream, fCatalog.get(), true); |
| 172 | fPages[0]->emitPage(stream, fCatalog.get()); |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 173 | for (int i = 0; i < fSecondPageFirstResourceIndex; i++) { |
| 174 | fPageResources[i]->emit(stream, fCatalog.get(), true); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 175 | } |
| 176 | fCatalog->emitSubstituteResources(stream, true); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 177 | // TODO(vandebo): Support linearized format |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 178 | // if (fPages.size() > 1) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 179 | // // 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] | 180 | // fCatalog->emitXrefTable(stream, true); |
| 181 | // } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 182 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 183 | for (int i = 0; i < fPageTree.count(); i++) { |
| 184 | fPageTree[i]->emitObject(stream, fCatalog.get(), true); |
| 185 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 186 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 187 | for (int i = 1; i < fPages.count(); i++) { |
| 188 | fPages[i]->emitPage(stream, fCatalog.get()); |
| 189 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 190 | |
robertphillips@google.com | acef3c4 | 2013-03-20 14:42:51 +0000 | [diff] [blame] | 191 | for (int i = fSecondPageFirstResourceIndex; |
| 192 | i < fPageResources.count(); |
| 193 | i++) { |
| 194 | fPageResources[i]->emit(stream, fCatalog.get(), true); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 195 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 196 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 197 | fCatalog->emitSubstituteResources(stream, false); |
| 198 | int64_t objCount = fCatalog->emitXrefTable(stream, fPages.count() > 1); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 199 | emitFooter(stream, objCount); |
| 200 | return true; |
| 201 | } |
| 202 | |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 203 | bool SkPDFDocument::setPage(int pageNumber, SkPDFDevice* pdfDevice) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 204 | if (!fPageTree.isEmpty()) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 205 | return false; |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | pageNumber--; |
| 209 | SkASSERT(pageNumber >= 0); |
| 210 | |
vandebo@chromium.org | 00223fa | 2011-07-22 01:48:55 +0000 | [diff] [blame] | 211 | if (pageNumber >= fPages.count()) { |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 212 | int oldSize = fPages.count(); |
| 213 | fPages.setCount(pageNumber + 1); |
| 214 | for (int i = oldSize; i <= pageNumber; i++) { |
| 215 | fPages[i] = NULL; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | SkPDFPage* page = new SkPDFPage(pdfDevice); |
| 220 | SkSafeUnref(fPages[pageNumber]); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 221 | fPages[pageNumber] = page; // Reference from new passed to fPages. |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 225 | bool SkPDFDocument::appendPage(SkPDFDevice* pdfDevice) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 226 | if (!fPageTree.isEmpty()) { |
vandebo@chromium.org | fb6a53a | 2011-07-18 23:13:19 +0000 | [diff] [blame] | 227 | return false; |
| 228 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 229 | |
| 230 | SkPDFPage* page = new SkPDFPage(pdfDevice); |
| 231 | fPages.push(page); // Reference from new passed to fPages. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 235 | void SkPDFDocument::getCountOfFontTypes( |
| 236 | int counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1]) const { |
vandebo@chromium.org | 6e08bfe | 2012-03-26 23:58:04 +0000 | [diff] [blame] | 237 | sk_bzero(counts, sizeof(int) * |
| 238 | (SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1)); |
vandebo@chromium.org | 7d6c8f9 | 2012-03-22 20:45:15 +0000 | [diff] [blame] | 239 | SkTDArray<SkFontID> seenFonts; |
| 240 | |
| 241 | for (int pageNumber = 0; pageNumber < fPages.count(); pageNumber++) { |
| 242 | const SkTDArray<SkPDFFont*>& fontResources = |
| 243 | fPages[pageNumber]->getFontResources(); |
| 244 | for (int font = 0; font < fontResources.count(); font++) { |
| 245 | SkFontID fontID = fontResources[font]->typeface()->uniqueID(); |
| 246 | if (seenFonts.find(fontID) == -1) { |
| 247 | counts[fontResources[font]->getType()]++; |
| 248 | seenFonts.push(fontID); |
| 249 | } |
| 250 | } |
| 251 | } |
vandebo@chromium.org | d897bfb | 2011-05-31 18:18:21 +0000 | [diff] [blame] | 252 | } |
| 253 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 254 | void SkPDFDocument::emitHeader(SkWStream* stream) { |
| 255 | stream->writeText("%PDF-1.4\n%"); |
| 256 | // The PDF spec recommends including a comment with four bytes, all |
| 257 | // with their high bits set. This is "Skia" with the high bits set. |
| 258 | stream->write32(0xD3EBE9E1); |
| 259 | stream->writeText("\n"); |
| 260 | } |
| 261 | |
| 262 | size_t SkPDFDocument::headerSize() { |
| 263 | SkDynamicMemoryWStream buffer; |
| 264 | emitHeader(&buffer); |
| 265 | return buffer.getOffset(); |
| 266 | } |
| 267 | |
| 268 | void SkPDFDocument::emitFooter(SkWStream* stream, int64_t objCount) { |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 269 | if (NULL == fTrailerDict) { |
| 270 | fTrailerDict = SkNEW(SkPDFDict); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 271 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 272 | // TODO(vandebo): Linearized format will take a Prev entry too. |
| 273 | // TODO(vandebo): PDF/A requires an ID entry. |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 274 | fTrailerDict->insertInt("Size", int(objCount)); |
reed@google.com | af77727 | 2012-09-20 18:19:26 +0000 | [diff] [blame] | 275 | fTrailerDict->insert("Root", new SkPDFObjRef(fDocCatalog))->unref(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | stream->writeText("trailer\n"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 279 | fTrailerDict->emitObject(stream, fCatalog.get(), false); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 280 | stream->writeText("\nstartxref\n"); |
| 281 | stream->writeBigDecAsText(fXRefFileOffset); |
| 282 | stream->writeText("\n%%EOF"); |
| 283 | } |