reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 1 | /* |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 8 | #include "SkPDFDocument.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 9 | #include "SkPDFDocumentPriv.h" |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 10 | |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 11 | #include "SkMakeUnique.h" |
reed | 5867736 | 2014-10-09 05:30:10 -0700 | [diff] [blame] | 12 | #include "SkPDFDevice.h" |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 13 | #include "SkPDFDocument.h" |
| 14 | #include "SkPDFFont.h" |
| 15 | #include "SkPDFGradientShader.h" |
| 16 | #include "SkPDFGraphicState.h" |
| 17 | #include "SkPDFShader.h" |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 18 | #include "SkPDFTag.h" |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 19 | #include "SkPDFUtils.h" |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 20 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 21 | #include "SkTo.h" |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 22 | |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 23 | #include <utility> |
| 24 | |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 25 | // For use in SkCanvas::drawAnnotation |
| 26 | const char* SkPDFGetNodeIdKey() { |
| 27 | static constexpr char key[] = "PDF_Node_Key"; |
| 28 | return key; |
| 29 | } |
| 30 | |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 31 | //////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | void SkPDFOffsetMap::markStartOfDocument(const SkWStream* s) { fBaseOffset = s->bytesWritten(); } |
| 34 | |
Hal Canary | 016cd02 | 2018-12-27 14:24:24 -0500 | [diff] [blame] | 35 | static size_t difference(size_t minuend, size_t subtrahend) { |
| 36 | return SkASSERT(minuend >= subtrahend), minuend - subtrahend; |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void SkPDFOffsetMap::markStartOfObject(int referenceNumber, const SkWStream* s) { |
| 40 | SkASSERT(referenceNumber > 0); |
| 41 | size_t index = SkToSizeT(referenceNumber - 1); |
| 42 | if (index >= fOffsets.size()) { |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 43 | fOffsets.resize(index + 1); |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 44 | } |
Hal Canary | 016cd02 | 2018-12-27 14:24:24 -0500 | [diff] [blame] | 45 | fOffsets[index] = SkToInt(difference(s->bytesWritten(), fBaseOffset)); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 46 | } |
Hal Canary | 6736236 | 2018-04-24 13:58:37 -0400 | [diff] [blame] | 47 | |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 48 | int SkPDFOffsetMap::objectCount() const { |
| 49 | return SkToInt(fOffsets.size() + 1); // Include the special zeroth object in the count. |
Hal Canary | f6462c4 | 2018-11-13 16:19:59 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 52 | int SkPDFOffsetMap::emitCrossReferenceTable(SkWStream* s) const { |
Hal Canary | 016cd02 | 2018-12-27 14:24:24 -0500 | [diff] [blame] | 53 | int xRefFileOffset = SkToInt(difference(s->bytesWritten(), fBaseOffset)); |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 54 | s->writeText("xref\n0 "); |
| 55 | s->writeDecAsText(this->objectCount()); |
| 56 | s->writeText("\n0000000000 65535 f \n"); |
| 57 | for (int offset : fOffsets) { |
| 58 | SkASSERT(offset > 0); // Offset was set. |
| 59 | s->writeBigDecAsText(offset, 10); |
| 60 | s->writeText(" 00000 n \n"); |
| 61 | } |
| 62 | return xRefFileOffset; |
| 63 | } |
| 64 | // |
| 65 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 66 | |
halcanary | ad5dcd1 | 2016-03-31 06:59:00 -0700 | [diff] [blame] | 67 | #define SKPDF_MAGIC "\xD3\xEB\xE9\xE1" |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 68 | #ifndef SK_BUILD_FOR_WIN |
halcanary | ad5dcd1 | 2016-03-31 06:59:00 -0700 | [diff] [blame] | 69 | static_assert((SKPDF_MAGIC[0] & 0x7F) == "Skia"[0], ""); |
| 70 | static_assert((SKPDF_MAGIC[1] & 0x7F) == "Skia"[1], ""); |
| 71 | static_assert((SKPDF_MAGIC[2] & 0x7F) == "Skia"[2], ""); |
| 72 | static_assert((SKPDF_MAGIC[3] & 0x7F) == "Skia"[3], ""); |
| 73 | #endif |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 74 | static void serializeHeader(SkPDFOffsetMap* offsetMap, SkWStream* wStream) { |
| 75 | offsetMap->markStartOfDocument(wStream); |
| 76 | wStream->writeText("%PDF-1.4\n%" SKPDF_MAGIC "\n"); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 77 | // The PDF spec recommends including a comment with four |
| 78 | // bytes, all with their high bits set. "\xD3\xEB\xE9\xE1" is |
| 79 | // "Skia" with the high bits set. |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 80 | } |
halcanary | ad5dcd1 | 2016-03-31 06:59:00 -0700 | [diff] [blame] | 81 | #undef SKPDF_MAGIC |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 82 | |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 83 | static void begin_indirect_object(SkPDFOffsetMap* offsetMap, |
| 84 | SkPDFIndirectReference ref, |
| 85 | SkWStream* s) { |
| 86 | offsetMap->markStartOfObject(ref.fValue, s); |
| 87 | s->writeDecAsText(ref.fValue); |
| 88 | s->writeText(" 0 obj\n"); // Generation number is always 0. |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 91 | static void end_indirect_object(SkWStream* s) { s->writeText("\nendobj\n"); } |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 92 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 93 | // Xref table and footer |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 94 | static void serialize_footer(const SkPDFOffsetMap& offsetMap, |
| 95 | SkWStream* wStream, |
| 96 | SkPDFIndirectReference infoDict, |
| 97 | SkPDFIndirectReference docCatalog, |
| 98 | SkUUID uuid) { |
| 99 | int xRefFileOffset = offsetMap.emitCrossReferenceTable(wStream); |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 100 | SkPDFDict trailerDict; |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 101 | trailerDict.insertInt("Size", offsetMap.objectCount()); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 102 | SkASSERT(docCatalog != SkPDFIndirectReference()); |
| 103 | trailerDict.insertRef("Root", docCatalog); |
| 104 | SkASSERT(infoDict != SkPDFIndirectReference()); |
| 105 | trailerDict.insertRef("Info", infoDict); |
| 106 | if (SkUUID() != uuid) { |
| 107 | trailerDict.insertObject("ID", SkPDFMetadata::MakePdfId(uuid, uuid)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 108 | } |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 109 | wStream->writeText("trailer\n"); |
Hal Canary | f6462c4 | 2018-11-13 16:19:59 -0500 | [diff] [blame] | 110 | trailerDict.emitObject(wStream); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 111 | wStream->writeText("\nstartxref\n"); |
Cary Clark | 60ebf14 | 2018-09-06 12:22:33 +0000 | [diff] [blame] | 112 | wStream->writeBigDecAsText(xRefFileOffset); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 113 | wStream->writeText("\n%%EOF"); |
| 114 | } |
| 115 | |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 116 | static SkPDFIndirectReference generate_page_tree( |
| 117 | SkPDFDocument* doc, |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 118 | std::vector<std::unique_ptr<SkPDFDict>> pages, |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 119 | const std::vector<SkPDFIndirectReference>& pageRefs) { |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 120 | // PDF wants a tree describing all the pages in the document. We arbitrary |
| 121 | // choose 8 (kNodeSize) as the number of allowed children. The internal |
| 122 | // nodes have type "Pages" with an array of children, a parent pointer, and |
| 123 | // the number of leaves below the node as "Count." The leaves are passed |
| 124 | // into the method, have type "Page" and need a parent pointer. This method |
| 125 | // builds the tree bottom up, skipping internal nodes that would have only |
| 126 | // one child. |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 127 | SkASSERT(pages.size() > 0); |
| 128 | struct PageTreeNode { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 129 | std::unique_ptr<SkPDFDict> fNode; |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 130 | SkPDFIndirectReference fReservedRef; |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 131 | int fPageObjectDescendantCount; |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 132 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 133 | static std::vector<PageTreeNode> Layer(std::vector<PageTreeNode> vec, SkPDFDocument* doc) { |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 134 | std::vector<PageTreeNode> result; |
| 135 | static constexpr size_t kMaxNodeSize = 8; |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 136 | const size_t n = vec.size(); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 137 | SkASSERT(n >= 1); |
| 138 | const size_t result_len = (n - 1) / kMaxNodeSize + 1; |
| 139 | SkASSERT(result_len >= 1); |
| 140 | SkASSERT(n == 1 || result_len < n); |
| 141 | result.reserve(result_len); |
| 142 | size_t index = 0; |
| 143 | for (size_t i = 0; i < result_len; ++i) { |
| 144 | if (n != 1 && index + 1 == n) { // No need to create a new node. |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 145 | result.push_back(std::move(vec[index++])); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 146 | continue; |
| 147 | } |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 148 | SkPDFIndirectReference parent = doc->reserveRef(); |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 149 | auto kids_list = SkPDFMakeArray(); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 150 | int descendantCount = 0; |
| 151 | for (size_t j = 0; j < kMaxNodeSize && index < n; ++j) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 152 | PageTreeNode& node = vec[index++]; |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 153 | node.fNode->insertRef("Parent", parent); |
| 154 | kids_list->appendRef(doc->emit(*node.fNode, node.fReservedRef)); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 155 | descendantCount += node.fPageObjectDescendantCount; |
| 156 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 157 | auto next = SkPDFMakeDict("Pages"); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 158 | next->insertInt("Count", descendantCount); |
| 159 | next->insertObject("Kids", std::move(kids_list)); |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 160 | result.push_back(PageTreeNode{std::move(next), parent, descendantCount}); |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 161 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 162 | return result; |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 163 | } |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 164 | }; |
| 165 | std::vector<PageTreeNode> currentLayer; |
| 166 | currentLayer.reserve(pages.size()); |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 167 | SkASSERT(pages.size() == pageRefs.size()); |
| 168 | for (size_t i = 0; i < pages.size(); ++i) { |
| 169 | currentLayer.push_back(PageTreeNode{std::move(pages[i]), pageRefs[i], 1}); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 170 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 171 | currentLayer = PageTreeNode::Layer(std::move(currentLayer), doc); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 172 | while (currentLayer.size() > 1) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 173 | currentLayer = PageTreeNode::Layer(std::move(currentLayer), doc); |
Hal Canary | 36fe1d6 | 2018-10-08 10:17:43 -0400 | [diff] [blame] | 174 | } |
| 175 | SkASSERT(currentLayer.size() == 1); |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 176 | const PageTreeNode& root = currentLayer[0]; |
| 177 | return doc->emit(*root.fNode, root.fReservedRef); |
halcanary | 2f7ebcb | 2015-03-25 12:45:28 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 180 | template<typename T, typename... Args> |
| 181 | static void reset_object(T* dst, Args&&... args) { |
| 182 | dst->~T(); |
| 183 | new (dst) T(std::forward<Args>(args)...); |
| 184 | } |
| 185 | |
halcanary | a43b415 | 2015-03-25 12:15:04 -0700 | [diff] [blame] | 186 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 187 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 188 | SkPDFDocument::SkPDFDocument(SkWStream* stream, |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 189 | SkPDF::Metadata metadata) |
Hal Canary | c5980d0 | 2018-01-08 15:02:36 -0500 | [diff] [blame] | 190 | : SkDocument(stream) |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 191 | , fMetadata(std::move(metadata)) { |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 192 | constexpr float kDpiForRasterScaleOne = 72.0f; |
| 193 | if (fMetadata.fRasterDPI != kDpiForRasterScaleOne) { |
| 194 | fInverseRasterScale = kDpiForRasterScaleOne / fMetadata.fRasterDPI; |
| 195 | fRasterScale = fMetadata.fRasterDPI / kDpiForRasterScaleOne; |
| 196 | } |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 197 | if (fMetadata.fStructureElementTreeRoot) { |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 198 | fTagTree.init(fMetadata.fStructureElementTreeRoot); |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 199 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 200 | fExecutor = metadata.fExecutor; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | SkPDFDocument::~SkPDFDocument() { |
| 204 | // subclasses of SkDocument must call close() in their destructors. |
| 205 | this->close(); |
| 206 | } |
| 207 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 208 | SkPDFIndirectReference SkPDFDocument::emit(const SkPDFObject& object, SkPDFIndirectReference ref){ |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 209 | object.emitObject(this->beginObject(ref)); |
| 210 | this->endObject(); |
| 211 | return ref; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 214 | SkWStream* SkPDFDocument::beginObject(SkPDFIndirectReference ref) { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 215 | fMutex.acquire(); |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 216 | begin_indirect_object(&fOffsetMap, ref, this->getStream()); |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 217 | return this->getStream(); |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 218 | }; |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 219 | |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 220 | void SkPDFDocument::endObject() { |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 221 | end_indirect_object(this->getStream()); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 222 | fMutex.release(); |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 223 | }; |
| 224 | |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 225 | static SkSize operator*(SkISize u, SkScalar s) { return SkSize{u.width() * s, u.height() * s}; } |
| 226 | static SkSize operator*(SkSize u, SkScalar s) { return SkSize{u.width() * s, u.height() * s}; } |
| 227 | |
Hal Canary | b1de5f9 | 2017-07-01 22:17:15 -0400 | [diff] [blame] | 228 | SkCanvas* SkPDFDocument::onBeginPage(SkScalar width, SkScalar height) { |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 229 | SkASSERT(fCanvas.imageInfo().dimensions().isZero()); |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 230 | if (fPages.empty()) { |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 231 | // if this is the first page if the document. |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 232 | { |
| 233 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 234 | serializeHeader(&fOffsetMap, this->getStream()); |
| 235 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 238 | fInfoDict = this->emit(*SkPDFMetadata::MakeDocumentInformationDict(fMetadata)); |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 239 | if (fMetadata.fPDFA) { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 240 | fUUID = SkPDFMetadata::CreateUUID(fMetadata); |
halcanary | 8cd4a24 | 2016-04-07 08:56:15 -0700 | [diff] [blame] | 241 | // We use the same UUID for Document ID and Instance ID since this |
| 242 | // is the first revision of this document (and Skia does not |
| 243 | // support revising existing PDF documents). |
| 244 | // If we are not in PDF/A mode, don't use a UUID since testing |
| 245 | // works best with reproducible outputs. |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 246 | fXMP = SkPDFMetadata::MakeXMPObject(fMetadata, fUUID, fUUID, this); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 247 | } |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 248 | } |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 249 | // By scaling the page at the device level, we will create bitmap layer |
| 250 | // devices at the rasterized scale, not the 72dpi scale. Bitmap layer |
| 251 | // devices are created when saveLayer is called with an ImageFilter; see |
| 252 | // SkPDFDevice::onCreateDevice(). |
| 253 | SkISize pageSize = (SkSize{width, height} * fRasterScale).toRound(); |
| 254 | SkMatrix initialTransform; |
| 255 | // Skia uses the top left as the origin but PDF natively has the origin at the |
| 256 | // bottom left. This matrix corrects for that, as well as the raster scale. |
| 257 | initialTransform.setScaleTranslate(fInverseRasterScale, -fInverseRasterScale, |
| 258 | 0, fInverseRasterScale * pageSize.height()); |
| 259 | fPageDevice = sk_make_sp<SkPDFDevice>(pageSize, this, initialTransform); |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 260 | reset_object(&fCanvas, fPageDevice); |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 261 | fCanvas.scale(fRasterScale, fRasterScale); |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 262 | fPageRefs.push_back(this->reserveRef()); |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 263 | return &fCanvas; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 264 | } |
skia.committer@gmail.com | 6319367 | 2013-06-08 07:01:13 +0000 | [diff] [blame] | 265 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 266 | void SkPDFDocument::onEndPage() { |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 267 | SkASSERT(!fCanvas.imageInfo().dimensions().isZero()); |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 268 | reset_object(&fCanvas); |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 269 | SkASSERT(fPageDevice); |
Hal Canary | 8259114 | 2018-05-09 14:20:12 -0400 | [diff] [blame] | 270 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 271 | auto page = SkPDFMakeDict("Page"); |
Hal Canary | 8259114 | 2018-05-09 14:20:12 -0400 | [diff] [blame] | 272 | |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 273 | SkSize mediaSize = fPageDevice->imageInfo().dimensions() * fInverseRasterScale; |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 274 | std::unique_ptr<SkStreamAsset> pageContent = fPageDevice->content(); |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 275 | auto resourceDict = fPageDevice->makeResourceDict(); |
| 276 | auto annotations = fPageDevice->getAnnotations(); |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 277 | SkASSERT(fPageRefs.size() > 0); |
| 278 | fPageDevice->appendDestinations(&fDests, fPageRefs.back()); |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 279 | fPageDevice = nullptr; |
Hal Canary | 8259114 | 2018-05-09 14:20:12 -0400 | [diff] [blame] | 280 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 281 | page->insertObject("Resources", std::move(resourceDict)); |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 282 | page->insertObject("MediaBox", SkPDFUtils::RectToArray(SkRect::MakeSize(mediaSize))); |
| 283 | |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 284 | if (annotations) { |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 285 | page->insertObject("Annots", std::move(annotations)); |
| 286 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 287 | page->insertRef("Contents", SkPDFStreamOut(nullptr, std::move(pageContent), this)); |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 288 | // The StructParents unique identifier for each page is just its |
| 289 | // 0-based page index. |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 290 | page->insertInt("StructParents", SkToInt(this->currentPageIndex())); |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 291 | fPages.emplace_back(std::move(page)); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 292 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 293 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 294 | void SkPDFDocument::onAbort() { |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 295 | this->waitForJobs(); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 296 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 297 | |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 298 | static sk_sp<SkData> SkSrgbIcm() { |
msarett | c213f0d | 2016-08-01 14:23:32 -0700 | [diff] [blame] | 299 | // Source: http://www.argyllcms.com/icclibsrc.html |
| 300 | static const char kProfile[] = |
| 301 | "\0\0\14\214argl\2 \0\0mntrRGB XYZ \7\336\0\1\0\6\0\26\0\17\0:acspM" |
| 302 | "SFT\0\0\0\0IEC sRGB\0\0\0\0\0\0\0\0\0\0\0\0\0\0\366\326\0\1\0\0\0\0" |
| 303 | "\323-argl\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 304 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\21desc\0\0\1P\0\0\0\231cprt\0" |
| 305 | "\0\1\354\0\0\0gdmnd\0\0\2T\0\0\0pdmdd\0\0\2\304\0\0\0\210tech\0\0\3" |
| 306 | "L\0\0\0\14vued\0\0\3X\0\0\0gview\0\0\3\300\0\0\0$lumi\0\0\3\344\0\0" |
| 307 | "\0\24meas\0\0\3\370\0\0\0$wtpt\0\0\4\34\0\0\0\24bkpt\0\0\0040\0\0\0" |
| 308 | "\24rXYZ\0\0\4D\0\0\0\24gXYZ\0\0\4X\0\0\0\24bXYZ\0\0\4l\0\0\0\24rTR" |
| 309 | "C\0\0\4\200\0\0\10\14gTRC\0\0\4\200\0\0\10\14bTRC\0\0\4\200\0\0\10" |
| 310 | "\14desc\0\0\0\0\0\0\0?sRGB IEC61966-2.1 (Equivalent to www.srgb.co" |
| 311 | "m 1998 HP profile)\0\0\0\0\0\0\0\0\0\0\0?sRGB IEC61966-2.1 (Equiva" |
| 312 | "lent to www.srgb.com 1998 HP profile)\0\0\0\0\0\0\0\0text\0\0\0\0C" |
| 313 | "reated by Graeme W. Gill. Released into the public domain. No Warr" |
| 314 | "anty, Use at your own risk.\0\0desc\0\0\0\0\0\0\0\26IEC http://www" |
| 315 | ".iec.ch\0\0\0\0\0\0\0\0\0\0\0\26IEC http://www.iec.ch\0\0\0\0\0\0\0" |
| 316 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 317 | "\0\0\0\0\0\0desc\0\0\0\0\0\0\0.IEC 61966-2.1 Default RGB colour sp" |
| 318 | "ace - sRGB\0\0\0\0\0\0\0\0\0\0\0.IEC 61966-2.1 Default RGB colour " |
| 319 | "space - sRGB\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0sig \0\0\0" |
| 320 | "\0CRT desc\0\0\0\0\0\0\0\rIEC61966-2.1\0\0\0\0\0\0\0\0\0\0\0\rIEC6" |
| 321 | "1966-2.1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" |
| 322 | "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0view\0\0\0\0" |
| 323 | "\0\23\244|\0\24_0\0\20\316\2\0\3\355\262\0\4\23\n\0\3\\g\0\0\0\1XY" |
| 324 | "Z \0\0\0\0\0L\n=\0P\0\0\0W\36\270meas\0\0\0\0\0\0\0\1\0\0\0\0\0\0\0" |
| 325 | "\0\0\0\0\0\0\0\0\0\0\0\2\217\0\0\0\2XYZ \0\0\0\0\0\0\363Q\0\1\0\0\0" |
| 326 | "\1\26\314XYZ \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0XYZ \0\0\0\0\0\0o\240" |
| 327 | "\0\0008\365\0\0\3\220XYZ \0\0\0\0\0\0b\227\0\0\267\207\0\0\30\331X" |
| 328 | "YZ \0\0\0\0\0\0$\237\0\0\17\204\0\0\266\304curv\0\0\0\0\0\0\4\0\0\0" |
| 329 | "\0\5\0\n\0\17\0\24\0\31\0\36\0#\0(\0-\0002\0007\0;\0@\0E\0J\0O\0T\0" |
| 330 | "Y\0^\0c\0h\0m\0r\0w\0|\0\201\0\206\0\213\0\220\0\225\0\232\0\237\0" |
| 331 | "\244\0\251\0\256\0\262\0\267\0\274\0\301\0\306\0\313\0\320\0\325\0" |
| 332 | "\333\0\340\0\345\0\353\0\360\0\366\0\373\1\1\1\7\1\r\1\23\1\31\1\37" |
| 333 | "\1%\1+\0012\0018\1>\1E\1L\1R\1Y\1`\1g\1n\1u\1|\1\203\1\213\1\222\1" |
| 334 | "\232\1\241\1\251\1\261\1\271\1\301\1\311\1\321\1\331\1\341\1\351\1" |
| 335 | "\362\1\372\2\3\2\14\2\24\2\35\2&\2/\0028\2A\2K\2T\2]\2g\2q\2z\2\204" |
| 336 | "\2\216\2\230\2\242\2\254\2\266\2\301\2\313\2\325\2\340\2\353\2\365" |
| 337 | "\3\0\3\13\3\26\3!\3-\0038\3C\3O\3Z\3f\3r\3~\3\212\3\226\3\242\3\256" |
| 338 | "\3\272\3\307\3\323\3\340\3\354\3\371\4\6\4\23\4 \4-\4;\4H\4U\4c\4q" |
| 339 | "\4~\4\214\4\232\4\250\4\266\4\304\4\323\4\341\4\360\4\376\5\r\5\34" |
| 340 | "\5+\5:\5I\5X\5g\5w\5\206\5\226\5\246\5\265\5\305\5\325\5\345\5\366" |
| 341 | "\6\6\6\26\6'\0067\6H\6Y\6j\6{\6\214\6\235\6\257\6\300\6\321\6\343\6" |
| 342 | "\365\7\7\7\31\7+\7=\7O\7a\7t\7\206\7\231\7\254\7\277\7\322\7\345\7" |
| 343 | "\370\10\13\10\37\0102\10F\10Z\10n\10\202\10\226\10\252\10\276\10\322" |
| 344 | "\10\347\10\373\t\20\t%\t:\tO\td\ty\t\217\t\244\t\272\t\317\t\345\t" |
| 345 | "\373\n\21\n'\n=\nT\nj\n\201\n\230\n\256\n\305\n\334\n\363\13\13\13" |
| 346 | "\"\0139\13Q\13i\13\200\13\230\13\260\13\310\13\341\13\371\14\22\14" |
| 347 | "*\14C\14\\\14u\14\216\14\247\14\300\14\331\14\363\r\r\r&\r@\rZ\rt\r" |
| 348 | "\216\r\251\r\303\r\336\r\370\16\23\16.\16I\16d\16\177\16\233\16\266" |
| 349 | "\16\322\16\356\17\t\17%\17A\17^\17z\17\226\17\263\17\317\17\354\20" |
| 350 | "\t\20&\20C\20a\20~\20\233\20\271\20\327\20\365\21\23\0211\21O\21m\21" |
| 351 | "\214\21\252\21\311\21\350\22\7\22&\22E\22d\22\204\22\243\22\303\22" |
| 352 | "\343\23\3\23#\23C\23c\23\203\23\244\23\305\23\345\24\6\24'\24I\24j" |
| 353 | "\24\213\24\255\24\316\24\360\25\22\0254\25V\25x\25\233\25\275\25\340" |
| 354 | "\26\3\26&\26I\26l\26\217\26\262\26\326\26\372\27\35\27A\27e\27\211" |
| 355 | "\27\256\27\322\27\367\30\33\30@\30e\30\212\30\257\30\325\30\372\31" |
| 356 | " \31E\31k\31\221\31\267\31\335\32\4\32*\32Q\32w\32\236\32\305\32\354" |
| 357 | "\33\24\33;\33c\33\212\33\262\33\332\34\2\34*\34R\34{\34\243\34\314" |
| 358 | "\34\365\35\36\35G\35p\35\231\35\303\35\354\36\26\36@\36j\36\224\36" |
| 359 | "\276\36\351\37\23\37>\37i\37\224\37\277\37\352 \25 A l \230 \304 \360" |
| 360 | "!\34!H!u!\241!\316!\373\"'\"U\"\202\"\257\"\335#\n#8#f#\224#\302#\360" |
| 361 | "$\37$M$|$\253$\332%\t%8%h%\227%\307%\367&'&W&\207&\267&\350'\30'I'" |
| 362 | "z'\253'\334(\r(?(q(\242(\324)\6)8)k)\235)\320*\2*5*h*\233*\317+\2+" |
| 363 | "6+i+\235+\321,\5,9,n,\242,\327-\14-A-v-\253-\341.\26.L.\202.\267.\356" |
| 364 | "/$/Z/\221/\307/\376050l0\2440\3331\0221J1\2021\2721\3622*2c2\2332\324" |
| 365 | "3\r3F3\1773\2703\3614+4e4\2364\3305\0235M5\2075\3025\375676r6\2566" |
| 366 | "\3517$7`7\2347\3278\0248P8\2148\3109\0059B9\1779\2749\371:6:t:\262" |
| 367 | ":\357;-;k;\252;\350<'<e<\244<\343=\"=a=\241=\340> >`>\240>\340?!?a" |
| 368 | "?\242?\342@#@d@\246@\347A)AjA\254A\356B0BrB\265B\367C:C}C\300D\3DG" |
| 369 | "D\212D\316E\22EUE\232E\336F\"FgF\253F\360G5G{G\300H\5HKH\221H\327I" |
| 370 | "\35IcI\251I\360J7J}J\304K\14KSK\232K\342L*LrL\272M\2MJM\223M\334N%" |
| 371 | "NnN\267O\0OIO\223O\335P'PqP\273Q\6QPQ\233Q\346R1R|R\307S\23S_S\252" |
| 372 | "S\366TBT\217T\333U(UuU\302V\17V\\V\251V\367WDW\222W\340X/X}X\313Y\32" |
| 373 | "YiY\270Z\7ZVZ\246Z\365[E[\225[\345\\5\\\206\\\326]']x]\311^\32^l^\275" |
| 374 | "_\17_a_\263`\5`W`\252`\374aOa\242a\365bIb\234b\360cCc\227c\353d@d\224" |
| 375 | "d\351e=e\222e\347f=f\222f\350g=g\223g\351h?h\226h\354iCi\232i\361j" |
| 376 | "Hj\237j\367kOk\247k\377lWl\257m\10m`m\271n\22nkn\304o\36oxo\321p+p" |
| 377 | "\206p\340q:q\225q\360rKr\246s\1s]s\270t\24tpt\314u(u\205u\341v>v\233" |
| 378 | "v\370wVw\263x\21xnx\314y*y\211y\347zFz\245{\4{c{\302|!|\201|\341}A" |
| 379 | "}\241~\1~b~\302\177#\177\204\177\345\200G\200\250\201\n\201k\201\315" |
| 380 | "\2020\202\222\202\364\203W\203\272\204\35\204\200\204\343\205G\205" |
| 381 | "\253\206\16\206r\206\327\207;\207\237\210\4\210i\210\316\2113\211\231" |
| 382 | "\211\376\212d\212\312\2130\213\226\213\374\214c\214\312\2151\215\230" |
| 383 | "\215\377\216f\216\316\2176\217\236\220\6\220n\220\326\221?\221\250" |
| 384 | "\222\21\222z\222\343\223M\223\266\224 \224\212\224\364\225_\225\311" |
| 385 | "\2264\226\237\227\n\227u\227\340\230L\230\270\231$\231\220\231\374" |
| 386 | "\232h\232\325\233B\233\257\234\34\234\211\234\367\235d\235\322\236" |
| 387 | "@\236\256\237\35\237\213\237\372\240i\240\330\241G\241\266\242&\242" |
| 388 | "\226\243\6\243v\243\346\244V\244\307\2458\245\251\246\32\246\213\246" |
| 389 | "\375\247n\247\340\250R\250\304\2517\251\251\252\34\252\217\253\2\253" |
| 390 | "u\253\351\254\\\254\320\255D\255\270\256-\256\241\257\26\257\213\260" |
| 391 | "\0\260u\260\352\261`\261\326\262K\262\302\2638\263\256\264%\264\234" |
| 392 | "\265\23\265\212\266\1\266y\266\360\267h\267\340\270Y\270\321\271J\271" |
| 393 | "\302\272;\272\265\273.\273\247\274!\274\233\275\25\275\217\276\n\276" |
| 394 | "\204\276\377\277z\277\365\300p\300\354\301g\301\343\302_\302\333\303" |
| 395 | "X\303\324\304Q\304\316\305K\305\310\306F\306\303\307A\307\277\310=" |
| 396 | "\310\274\311:\311\271\3128\312\267\3136\313\266\3145\314\265\3155\315" |
| 397 | "\265\3166\316\266\3177\317\270\3209\320\272\321<\321\276\322?\322\301" |
| 398 | "\323D\323\306\324I\324\313\325N\325\321\326U\326\330\327\\\327\340" |
| 399 | "\330d\330\350\331l\331\361\332v\332\373\333\200\334\5\334\212\335\20" |
| 400 | "\335\226\336\34\336\242\337)\337\257\3406\340\275\341D\341\314\342" |
| 401 | "S\342\333\343c\343\353\344s\344\374\345\204\346\r\346\226\347\37\347" |
| 402 | "\251\3502\350\274\351F\351\320\352[\352\345\353p\353\373\354\206\355" |
| 403 | "\21\355\234\356(\356\264\357@\357\314\360X\360\345\361r\361\377\362" |
| 404 | "\214\363\31\363\247\3644\364\302\365P\365\336\366m\366\373\367\212" |
| 405 | "\370\31\370\250\3718\371\307\372W\372\347\373w\374\7\374\230\375)\375" |
| 406 | "\272\376K\376\334\377m\377\377"; |
| 407 | const size_t kProfileLength = 3212; |
| 408 | static_assert(kProfileLength == sizeof(kProfile) - 1, ""); |
| 409 | return SkData::MakeWithoutCopy(kProfile, kProfileLength); |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 412 | static SkPDFIndirectReference make_srgb_color_profile(SkPDFDocument* doc) { |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 413 | std::unique_ptr<SkPDFDict> dict = SkPDFMakeDict(); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 414 | dict->insertInt("N", 3); |
| 415 | dict->insertObject("Range", SkPDFMakeArray(0, 1, 0, 1, 0, 1)); |
| 416 | return SkPDFStreamOut(std::move(dict), SkMemoryStream::Make(SkSrgbIcm()), doc, true); |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 417 | } |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 418 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 419 | static std::unique_ptr<SkPDFArray> make_srgb_output_intents(SkPDFDocument* doc) { |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 420 | // sRGB is specified by HTML, CSS, and SVG. |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 421 | auto outputIntent = SkPDFMakeDict("OutputIntent"); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 422 | outputIntent->insertName("S", "GTS_PDFA1"); |
| 423 | outputIntent->insertString("RegistryName", "http://www.color.org"); |
| 424 | outputIntent->insertString("OutputConditionIdentifier", |
| 425 | "Custom"); |
| 426 | outputIntent->insertString("Info","sRGB IEC61966-2.1"); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 427 | outputIntent->insertRef("DestOutputProfile", make_srgb_color_profile(doc)); |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 428 | auto intentArray = SkPDFMakeArray(); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 429 | intentArray->appendObject(std::move(outputIntent)); |
| 430 | return intentArray; |
| 431 | } |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 432 | |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 433 | SkPDFIndirectReference SkPDFDocument::getPage(size_t pageIndex) const { |
| 434 | SkASSERT(pageIndex < fPageRefs.size()); |
| 435 | return fPageRefs[pageIndex]; |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | int SkPDFDocument::getMarkIdForNodeId(int nodeId) { |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 439 | return fTagTree.getMarkIdForNodeId(nodeId, SkToUInt(this->currentPageIndex())); |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 442 | static std::vector<const SkPDFFont*> get_fonts(const SkPDFDocument& canon) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 443 | std::vector<const SkPDFFont*> fonts; |
| 444 | fonts.reserve(canon.fFontMap.count()); |
Hal Canary | ac907bd | 2019-01-09 14:00:49 -0500 | [diff] [blame] | 445 | // Sort so the output PDF is reproducible. |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 446 | canon.fFontMap.foreach([&fonts](uint64_t, const SkPDFFont& font) { fonts.push_back(&font); }); |
| 447 | std::sort(fonts.begin(), fonts.end(), [](const SkPDFFont* u, const SkPDFFont* v) { |
| 448 | return u->indirectReference().fValue < v->indirectReference().fValue; |
| 449 | }); |
| 450 | return fonts; |
| 451 | } |
| 452 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 453 | void SkPDFDocument::onClose(SkWStream* stream) { |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 454 | SkASSERT(fCanvas.imageInfo().dimensions().isZero()); |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 455 | if (fPages.empty()) { |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 456 | this->waitForJobs(); |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 457 | return; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 458 | } |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 459 | auto docCatalog = SkPDFMakeDict("Catalog"); |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 460 | if (fMetadata.fPDFA) { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 461 | SkASSERT(fXMP != SkPDFIndirectReference()); |
| 462 | docCatalog->insertRef("Metadata", fXMP); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 463 | // Don't specify OutputIntents if we are not in PDF/A mode since |
| 464 | // no one has ever asked for this feature. |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 465 | docCatalog->insertObject("OutputIntents", make_srgb_output_intents(this)); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 466 | } |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 467 | |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 468 | docCatalog->insertRef("Pages", generate_page_tree(this, std::move(fPages), fPageRefs)); |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 469 | |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 470 | if (fDests.size() > 0) { |
| 471 | docCatalog->insertRef("Dests", this->emit(fDests)); |
| 472 | reset_object(&fDests); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 475 | // Handle tagged PDFs. |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 476 | if (SkPDFIndirectReference root = fTagTree.makeStructTreeRoot(this)) { |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 477 | // In the document catalog, indicate that this PDF is tagged. |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 478 | auto markInfo = SkPDFMakeDict("MarkInfo"); |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 479 | markInfo->insertBool("Marked", true); |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 480 | docCatalog->insertObject("MarkInfo", std::move(markInfo)); |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 481 | docCatalog->insertRef("StructTreeRoot", root); |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 482 | } |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 483 | |
| 484 | auto docCatalogRef = this->emit(*docCatalog); |
| 485 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 486 | for (const SkPDFFont* f : get_fonts(*this)) { |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 487 | f->emitSubset(this); |
| 488 | } |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 489 | |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 490 | this->waitForJobs(); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 491 | { |
| 492 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 493 | serialize_footer(fOffsetMap, this->getStream(), fInfoDict, docCatalogRef, fUUID); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 494 | } |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 497 | void SkPDFDocument::incrementJobCount() { fJobCount++; } |
| 498 | |
| 499 | void SkPDFDocument::signalJobComplete() { fSemaphore.signal(); } |
| 500 | |
| 501 | void SkPDFDocument::waitForJobs() { |
| 502 | // fJobCount can increase while we wait. |
| 503 | while (fJobCount > 0) { |
| 504 | fSemaphore.wait(); |
| 505 | --fJobCount; |
| 506 | } |
| 507 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 508 | |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 509 | /////////////////////////////////////////////////////////////////////////////// |
| 510 | |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 511 | void SkPDF::SetNodeId(SkCanvas* canvas, int nodeID) { |
| 512 | sk_sp<SkData> payload = SkData::MakeWithCopy(&nodeID, sizeof(nodeID)); |
| 513 | const char* key = SkPDFGetNodeIdKey(); |
| 514 | canvas->drawAnnotation({0, 0, 0, 0}, key, payload.get()); |
| 515 | } |
| 516 | |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 517 | sk_sp<SkDocument> SkPDF::MakeDocument(SkWStream* stream, const SkPDF::Metadata& metadata) { |
| 518 | SkPDF::Metadata meta = metadata; |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 519 | if (meta.fRasterDPI <= 0) { |
| 520 | meta.fRasterDPI = 72.0f; |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 521 | } |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 522 | if (meta.fEncodingQuality < 0) { |
| 523 | meta.fEncodingQuality = 0; |
| 524 | } |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 525 | return stream ? sk_make_sp<SkPDFDocument>(stream, std::move(meta)) : nullptr; |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 526 | } |
| 527 | |