halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 7 | #ifndef SkPDFDocumentPriv_DEFINED |
| 8 | #define SkPDFDocumentPriv_DEFINED |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 9 | |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 11 | #include "SkMutex.h" |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 12 | #include "SkPDFDocument.h" |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 13 | #include "SkPDFMetadata.h" |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 14 | #include "SkPDFTag.h" |
Hal Canary | 2111d5f | 2019-01-02 09:44:41 -0500 | [diff] [blame] | 15 | #include "SkStream.h" |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 16 | #include "SkTHash.h" |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 17 | |
| 18 | #include <atomic> |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 19 | #include <vector> |
| 20 | #include <memory> |
halcanary | 23f4d4d | 2016-03-12 05:59:39 -0800 | [diff] [blame] | 21 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 22 | class SkExecutor; |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 23 | class SkPDFDevice; |
| 24 | class SkPDFFont; |
| 25 | struct SkAdvancedTypefaceMetrics; |
| 26 | struct SkBitmapKey; |
| 27 | struct SkPDFFillGraphicState; |
| 28 | struct SkPDFImageShaderKey; |
| 29 | struct SkPDFStrokeGraphicState; |
| 30 | |
| 31 | namespace SkPDFGradientShader { |
| 32 | struct Key; |
| 33 | struct KeyHash; |
| 34 | } |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 35 | |
| 36 | const char* SkPDFGetNodeIdKey(); |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 37 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 38 | // Logically part of SkPDFDocument, but separate to keep similar functionality together. |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 39 | class SkPDFOffsetMap { |
| 40 | public: |
| 41 | void markStartOfDocument(const SkWStream*); |
| 42 | void markStartOfObject(int referenceNumber, const SkWStream*); |
| 43 | int objectCount() const; |
| 44 | int emitCrossReferenceTable(SkWStream* s) const; |
| 45 | private: |
| 46 | std::vector<int> fOffsets; |
Hal Canary | f6462c4 | 2018-11-13 16:19:59 -0500 | [diff] [blame] | 47 | size_t fBaseOffset = SIZE_MAX; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /** Concrete implementation of SkDocument that creates PDF files. This |
| 51 | class does not produced linearized or optimized PDFs; instead it |
| 52 | it attempts to use a minimum amount of RAM. */ |
| 53 | class SkPDFDocument : public SkDocument { |
| 54 | public: |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 55 | SkPDFDocument(SkWStream*, SkPDF::Metadata); |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 56 | ~SkPDFDocument() override; |
Hal Canary | b1de5f9 | 2017-07-01 22:17:15 -0400 | [diff] [blame] | 57 | SkCanvas* onBeginPage(SkScalar, SkScalar) override; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 58 | void onEndPage() override; |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 59 | void onClose(SkWStream*) override; |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 60 | void onAbort() override; |
halcanary | e86134f | 2016-09-06 09:32:13 -0700 | [diff] [blame] | 61 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 62 | /** |
| 63 | Serialize the object, as well as any other objects it |
| 64 | indirectly refers to. If any any other objects have been added |
| 65 | to the SkPDFObjNumMap without serializing them, they will be |
| 66 | serialized as well. |
| 67 | |
| 68 | It might go without saying that objects should not be changed |
| 69 | after calling serialize, since those changes will be too late. |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 70 | */ |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 71 | SkPDFIndirectReference emit(const SkPDFObject&, SkPDFIndirectReference); |
| 72 | SkPDFIndirectReference emit(const SkPDFObject& o) { return this->emit(o, this->reserveRef()); } |
Hal Canary | 2111d5f | 2019-01-02 09:44:41 -0500 | [diff] [blame] | 73 | |
| 74 | template <typename T> |
| 75 | void emitStream(const SkPDFDict& dict, T writeStream, SkPDFIndirectReference ref) { |
| 76 | SkWStream* stream = this->beginObject(ref); |
| 77 | dict.emitObject(stream); |
| 78 | stream->writeText(" stream\n"); |
| 79 | writeStream(stream); |
| 80 | stream->writeText("\nendstream"); |
| 81 | this->endObject(); |
| 82 | } |
| 83 | |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 84 | const SkPDF::Metadata& metadata() const { return fMetadata; } |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 85 | |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 86 | SkPDFIndirectReference getPage(size_t pageIndex) const; |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 87 | // Returns -1 if no mark ID. |
| 88 | int getMarkIdForNodeId(int nodeId); |
| 89 | |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 90 | SkPDFIndirectReference reserveRef() { return SkPDFIndirectReference{fNextObjectNumber++}; } |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 91 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 92 | SkExecutor* executor() const { return fExecutor; } |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 93 | void incrementJobCount(); |
| 94 | void signalJobComplete(); |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 95 | size_t currentPageIndex() { return fPages.size(); } |
| 96 | size_t pageCount() { return fPageRefs.size(); } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 97 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 98 | // Canonicalized objects |
| 99 | SkTHashMap<SkPDFImageShaderKey, SkPDFIndirectReference> fImageShaderMap; |
| 100 | SkTHashMap<SkPDFGradientShader::Key, SkPDFIndirectReference, SkPDFGradientShader::KeyHash> |
| 101 | fGradientPatternMap; |
| 102 | SkTHashMap<SkBitmapKey, SkPDFIndirectReference> fPDFBitmapMap; |
| 103 | SkTHashMap<uint32_t, std::unique_ptr<SkAdvancedTypefaceMetrics>> fTypefaceMetrics; |
| 104 | SkTHashMap<uint32_t, std::vector<SkString>> fType1GlyphNames; |
| 105 | SkTHashMap<uint32_t, std::vector<SkUnichar>> fToUnicodeMap; |
| 106 | SkTHashMap<uint32_t, SkPDFIndirectReference> fFontDescriptors; |
| 107 | SkTHashMap<uint32_t, SkPDFIndirectReference> fType3FontDescriptors; |
| 108 | SkTHashMap<uint64_t, SkPDFFont> fFontMap; |
| 109 | SkTHashMap<SkPDFStrokeGraphicState, SkPDFIndirectReference> fStrokeGSMap; |
| 110 | SkTHashMap<SkPDFFillGraphicState, SkPDFIndirectReference> fFillGSMap; |
| 111 | SkPDFIndirectReference fInvertFunction; |
| 112 | SkPDFIndirectReference fNoSmaskGraphicState; |
| 113 | |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 114 | private: |
Hal Canary | 1addaff | 2018-12-21 11:54:37 -0500 | [diff] [blame] | 115 | SkPDFOffsetMap fOffsetMap; |
Hal Canary | 2473d94 | 2018-09-10 13:06:42 -0400 | [diff] [blame] | 116 | SkCanvas fCanvas; |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 117 | std::vector<std::unique_ptr<SkPDFDict>> fPages; |
Hal Canary | fba5b6c | 2018-12-18 12:55:18 -0500 | [diff] [blame] | 118 | std::vector<SkPDFIndirectReference> fPageRefs; |
| 119 | SkPDFDict fDests; |
halcanary | cc77c12 | 2016-03-23 06:26:31 -0700 | [diff] [blame] | 120 | sk_sp<SkPDFDevice> fPageDevice; |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 121 | std::atomic<int> fNextObjectNumber = {1}; |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 122 | std::atomic<int> fJobCount = {0}; |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 123 | SkUUID fUUID; |
| 124 | SkPDFIndirectReference fInfoDict; |
| 125 | SkPDFIndirectReference fXMP; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 126 | SkPDF::Metadata fMetadata; |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 127 | SkScalar fRasterScale = 1; |
| 128 | SkScalar fInverseRasterScale = 1; |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 129 | SkExecutor* fExecutor = nullptr; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 130 | |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 131 | // For tagged PDFs. |
Hal Canary | f34131a | 2018-12-18 15:11:03 -0500 | [diff] [blame] | 132 | SkPDFTagTree fTagTree; |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 133 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 134 | SkMutex fMutex; |
| 135 | SkSemaphore fSemaphore; |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 136 | |
Hal Canary | dd9003a | 2018-12-21 13:44:31 -0500 | [diff] [blame] | 137 | void waitForJobs(); |
Hal Canary | 2111d5f | 2019-01-02 09:44:41 -0500 | [diff] [blame] | 138 | SkWStream* beginObject(SkPDFIndirectReference); |
| 139 | void endObject(); |
halcanary | 50e82e6 | 2016-03-21 13:45:05 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 142 | #endif // SkPDFDocumentPriv_DEFINED |