| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkDocument.h" | 8 #include "SkDocument.h" |
| 9 #include "SkPDFCanon.h" | 9 #include "SkPDFCanon.h" |
| 10 #include "SkPDFDevice.h" | 10 #include "SkPDFDevice.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 entry = iterator.next(); | 70 entry = iterator.next(); |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 static SkPDFObject* create_pdf_page_content(const SkPDFDevice* pageDevice) { | 74 static SkPDFObject* create_pdf_page_content(const SkPDFDevice* pageDevice) { |
| 75 SkAutoTDelete<SkStreamAsset> content(pageDevice->content()); | 75 SkAutoTDelete<SkStreamAsset> content(pageDevice->content()); |
| 76 return new SkPDFStream(content.get()); | 76 return new SkPDFStream(content.get()); |
| 77 } | 77 } |
| 78 | 78 |
| 79 static SkPDFDict* create_pdf_page(const SkPDFDevice* pageDevice) { | 79 static SkPDFDict* create_pdf_page(const SkPDFDevice* pageDevice) { |
| 80 sk_sp<SkPDFDict> page(new SkPDFDict("Page")); | 80 auto page = sk_make_sp<SkPDFDict>("Page"); |
| 81 page->insertObject("Resources", pageDevice->createResourceDict()); | 81 page->insertObject("Resources", pageDevice->createResourceDict()); |
| 82 page->insertObject("MediaBox", pageDevice->copyMediaBox()); | 82 page->insertObject("MediaBox", pageDevice->copyMediaBox()); |
| 83 sk_sp<SkPDFArray> annotations(new SkPDFArray); | 83 auto annotations = sk_make_sp<SkPDFArray>(); |
| 84 pageDevice->appendAnnotations(annotations.get()); | 84 pageDevice->appendAnnotations(annotations.get()); |
| 85 if (annotations->size() > 0) { | 85 if (annotations->size() > 0) { |
| 86 page->insertObject("Annots", annotations.release()); | 86 page->insertObject("Annots", annotations.release()); |
| 87 } | 87 } |
| 88 page->insertObjRef("Contents", create_pdf_page_content(pageDevice)); | 88 page->insertObjRef("Contents", create_pdf_page_content(pageDevice)); |
| 89 return page.release(); | 89 return page.release(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 static void generate_page_tree(const SkTDArray<SkPDFDict*>& pages, | 92 static void generate_page_tree(const SkTDArray<SkPDFDict*>& pages, |
| 93 SkTDArray<SkPDFDict*>* pageTree, | 93 SkTDArray<SkPDFDict*>* pageTree, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 114 nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize); | 114 nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize); |
| 115 | 115 |
| 116 int treeCapacity = kNodeSize; | 116 int treeCapacity = kNodeSize; |
| 117 do { | 117 do { |
| 118 for (int i = 0; i < curNodes.count(); ) { | 118 for (int i = 0; i < curNodes.count(); ) { |
| 119 if (i > 0 && i + 1 == curNodes.count()) { | 119 if (i > 0 && i + 1 == curNodes.count()) { |
| 120 nextRoundNodes.push(curNodes[i]); | 120 nextRoundNodes.push(curNodes[i]); |
| 121 break; | 121 break; |
| 122 } | 122 } |
| 123 | 123 |
| 124 sk_sp<SkPDFDict> newNode(new SkPDFDict("Pages")); | 124 auto newNode = sk_make_sp<SkPDFDict>("Pages"); |
| 125 sk_sp<SkPDFArray> kids(new SkPDFArray); | 125 auto kids = sk_make_sp<SkPDFArray>(); |
| 126 kids->reserve(kNodeSize); | 126 kids->reserve(kNodeSize); |
| 127 | 127 |
| 128 int count = 0; | 128 int count = 0; |
| 129 for (; i < curNodes.count() && count < kNodeSize; i++, count++) { | 129 for (; i < curNodes.count() && count < kNodeSize; i++, count++) { |
| 130 curNodes[i]->insertObjRef("Parent", SkRef(newNode.get())); | 130 curNodes[i]->insertObjRef("Parent", SkRef(newNode.get())); |
| 131 kids->appendObjRef(SkRef(curNodes[i])); | 131 kids->appendObjRef(SkRef(curNodes[i])); |
| 132 | 132 |
| 133 // TODO(vandebo): put the objects in strict access order. | 133 // TODO(vandebo): put the objects in strict access order. |
| 134 // Probably doesn't matter because they are so small. | 134 // Probably doesn't matter because they are so small. |
| 135 if (curNodes[i] != pages[0]) { | 135 if (curNodes[i] != pages[0]) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 167 } |
| 168 | 168 |
| 169 static bool emit_pdf_document(const SkTDArray<const SkPDFDevice*>& pageDevices, | 169 static bool emit_pdf_document(const SkTDArray<const SkPDFDevice*>& pageDevices, |
| 170 const SkPDFMetadata& metadata, | 170 const SkPDFMetadata& metadata, |
| 171 SkWStream* stream) { | 171 SkWStream* stream) { |
| 172 if (pageDevices.isEmpty()) { | 172 if (pageDevices.isEmpty()) { |
| 173 return false; | 173 return false; |
| 174 } | 174 } |
| 175 | 175 |
| 176 SkTDArray<SkPDFDict*> pages; | 176 SkTDArray<SkPDFDict*> pages; |
| 177 sk_sp<SkPDFDict> dests(new SkPDFDict); | 177 auto dests = sk_make_sp<SkPDFDict>(); |
| 178 | 178 |
| 179 for (int i = 0; i < pageDevices.count(); i++) { | 179 for (int i = 0; i < pageDevices.count(); i++) { |
| 180 SkASSERT(pageDevices[i]); | 180 SkASSERT(pageDevices[i]); |
| 181 SkASSERT(i == 0 || | 181 SkASSERT(i == 0 || |
| 182 pageDevices[i - 1]->getCanon() == pageDevices[i]->getCanon()); | 182 pageDevices[i - 1]->getCanon() == pageDevices[i]->getCanon()); |
| 183 sk_sp<SkPDFDict> page(create_pdf_page(pageDevices[i])); | 183 sk_sp<SkPDFDict> page(create_pdf_page(pageDevices[i])); |
| 184 pageDevices[i]->appendDestinations(dests.get(), page.get()); | 184 pageDevices[i]->appendDestinations(dests.get(), page.get()); |
| 185 pages.push(page.release()); | 185 pages.push(page.release()); |
| 186 } | 186 } |
| 187 | 187 |
| 188 sk_sp<SkPDFDict> docCatalog(new SkPDFDict("Catalog")); | 188 auto docCatalog = sk_make_sp<SkPDFDict>("Catalog"); |
| 189 | 189 |
| 190 sk_sp<SkPDFObject> infoDict( | 190 sk_sp<SkPDFObject> infoDict( |
| 191 metadata.createDocumentInformationDict()); | 191 metadata.createDocumentInformationDict()); |
| 192 | 192 |
| 193 sk_sp<SkPDFObject> id, xmp; | 193 sk_sp<SkPDFObject> id, xmp; |
| 194 #ifdef SK_PDF_GENERATE_PDFA | 194 #ifdef SK_PDF_GENERATE_PDFA |
| 195 SkPDFMetadata::UUID uuid = metadata.uuid(); | 195 SkPDFMetadata::UUID uuid = metadata.uuid(); |
| 196 // We use the same UUID for Document ID and Instance ID since this | 196 // We use the same UUID for Document ID and Instance ID since this |
| 197 // is the first revision of this document (and Skia does not | 197 // is the first revision of this document (and Skia does not |
| 198 // support revising existing PDF documents). | 198 // support revising existing PDF documents). |
| 199 // If we are not in PDF/A mode, don't use a UUID since testing | 199 // If we are not in PDF/A mode, don't use a UUID since testing |
| 200 // works best with reproducible outputs. | 200 // works best with reproducible outputs. |
| 201 id.reset(SkPDFMetadata::CreatePdfId(uuid, uuid)); | 201 id.reset(SkPDFMetadata::CreatePdfId(uuid, uuid)); |
| 202 xmp.reset(metadata.createXMPObject(uuid, uuid)); | 202 xmp.reset(metadata.createXMPObject(uuid, uuid)); |
| 203 docCatalog->insertObjRef("Metadata", xmp.release()); | 203 docCatalog->insertObjRef("Metadata", xmp.release()); |
| 204 | 204 |
| 205 // sRGB is specified by HTML, CSS, and SVG. | 205 // sRGB is specified by HTML, CSS, and SVG. |
| 206 sk_sp<SkPDFDict> outputIntent(new SkPDFDict("OutputIntent")); | 206 auto outputIntent = sk_make_sp<SkPDFDict>("OutputIntent"); |
| 207 outputIntent->insertName("S", "GTS_PDFA1"); | 207 outputIntent->insertName("S", "GTS_PDFA1"); |
| 208 outputIntent->insertString("RegistryName", "http://www.color.org"); | 208 outputIntent->insertString("RegistryName", "http://www.color.org"); |
| 209 outputIntent->insertString("OutputConditionIdentifier", | 209 outputIntent->insertString("OutputConditionIdentifier", |
| 210 "sRGB IEC61966-2.1"); | 210 "sRGB IEC61966-2.1"); |
| 211 sk_sp<SkPDFArray> intentArray(new SkPDFArray); | 211 auto intentArray = sk_make_sp<SkPDFArray>(); |
| 212 intentArray->appendObject(outputIntent.release()); | 212 intentArray->appendObject(outputIntent.release()); |
| 213 // Don't specify OutputIntents if we are not in PDF/A mode since | 213 // Don't specify OutputIntents if we are not in PDF/A mode since |
| 214 // no one has ever asked for this feature. | 214 // no one has ever asked for this feature. |
| 215 docCatalog->insertObject("OutputIntents", intentArray.release()); | 215 docCatalog->insertObject("OutputIntents", intentArray.release()); |
| 216 #endif | 216 #endif |
| 217 | 217 |
| 218 SkTDArray<SkPDFDict*> pageTree; | 218 SkTDArray<SkPDFDict*> pageTree; |
| 219 SkPDFDict* pageTreeRoot; | 219 SkPDFDict* pageTreeRoot; |
| 220 generate_page_tree(pages, &pageTree, &pageTreeRoot); | 220 generate_page_tree(pages, &pageTree, &pageTreeRoot); |
| 221 docCatalog->insertObjRef("Pages", SkRef(pageTreeRoot)); | 221 docCatalog->insertObjRef("Pages", SkRef(pageTreeRoot)); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 | 403 |
| 404 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { | 404 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { |
| 405 SkFILEWStream* stream = new SkFILEWStream(path); | 405 SkFILEWStream* stream = new SkFILEWStream(path); |
| 406 if (!stream->isValid()) { | 406 if (!stream->isValid()) { |
| 407 delete stream; | 407 delete stream; |
| 408 return nullptr; | 408 return nullptr; |
| 409 } | 409 } |
| 410 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; | 410 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; |
| 411 return new SkDocument_PDF(stream, delete_wstream, dpi, nullptr); | 411 return new SkDocument_PDF(stream, delete_wstream, dpi, nullptr); |
| 412 } | 412 } |
| OLD | NEW |