vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 1 | /* |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame^] | 2 | * Copyright (C) 2011 Google Inc. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 3 | * |
| 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.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 22 | // Add the resources, starting at firstIndex to the catalog, removing any dupes. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 23 | // A hash table would be really nice here. |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 24 | void addResourcesToCatalog(int firstIndex, bool firstPage, |
| 25 | SkTDArray<SkPDFObject*>* resourceList, |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 26 | SkPDFCatalog* catalog) { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 27 | for (int i = firstIndex; i < resourceList->count(); i++) { |
| 28 | int index = resourceList->find((*resourceList)[i]); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 29 | if (index != i) { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 30 | (*resourceList)[i]->unref(); |
| 31 | resourceList->removeShuffle(i); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame^] | 32 | i--; |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 33 | } else { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 34 | catalog->addObject((*resourceList)[i], firstPage); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | SkPDFDocument::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 | |
| 45 | SkPDFDocument::~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 | |
| 56 | bool 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.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame^] | 68 | |
| 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.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 85 | 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.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 89 | addResourcesToCatalog(resourceCount, first_page, &fPageResources, |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 90 | &fCatalog); |
| 91 | if (i == 0) { |
| 92 | first_page = false; |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 93 | fSecondPageFirstResourceIndex = fPageResources.count(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 94 | } |
| 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.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 102 | for (int i = 0; i < fSecondPageFirstResourceIndex; i++) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 103 | 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.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 115 | for (int i = fSecondPageFirstResourceIndex; |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 116 | 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.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 127 | for (int i = 0; i < fSecondPageFirstResourceIndex; i++) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 128 | 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.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 141 | for (int i = fSecondPageFirstResourceIndex; i < fPageResources.count(); i++) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 142 | 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 | |
| 149 | bool 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 | |
| 163 | void 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 | |
| 171 | size_t SkPDFDocument::headerSize() { |
| 172 | SkDynamicMemoryWStream buffer; |
| 173 | emitHeader(&buffer); |
| 174 | return buffer.getOffset(); |
| 175 | } |
| 176 | |
| 177 | void 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 | } |