Debian Codegraph @

src/pdf/SkPDFDocumentPriv.h

raw
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 */
7 #ifndef SkPDFDocumentPriv_DEFINED
8 #define SkPDFDocumentPriv_DEFINED
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkDocument.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSpan.h" // IWYU pragma: keep
18 #include "include/core/SkStream.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/docs/SkPDFDocument.h"
23 #include "include/private/base/SkMutex.h"
24 #include "include/private/base/SkSemaphore.h"
25 #include "src/base/SkUTF.h"
26 #include "src/core/SkTHash.h"
27 #include "src/pdf/SkPDFBitmap.h"
28 #include "src/pdf/SkPDFFont.h"
29 #include "src/pdf/SkPDFGraphicState.h"
30 #include "src/pdf/SkPDFShader.h"
31 #include "src/pdf/SkPDFTag.h"
32 #include "src/pdf/SkPDFTypes.h"
33 #include "src/pdf/SkUUID.h"
34
35 #include <cstddef>
36 #include <cstdint>
37 #include <atomic>
38 #include <vector>
39 #include <memory>
40
41 class SkDescriptor;
42 class SkExecutor;
43 class SkPDFDevice;
44 struct SkAdvancedTypefaceMetrics;
45 struct SkBitmapKey;
46 class SkMatrix;
47
48 namespace SkPDFGradientShader {
49 struct Key;
50 struct KeyHash;
51 } // namespace SkPDFGradientShader
52
53 const char* SkPDFGetElemIdKey();
54
55 // Logically part of SkPDFDocument, but separate to keep similar functionality together.
56 class SkPDFOffsetMap {
57 public:
58 void markStartOfDocument(const SkWStream*);
59 void markStartOfObject(int referenceNumber, const SkWStream*);
60 int objectCount() const;
61 int emitCrossReferenceTable(SkWStream* s) const;
62 private:
63 std::vector<int> fOffsets;
64 size_t fBaseOffset = SIZE_MAX;
65 };
66
67
68 struct SkPDFNamedDestination {
69 sk_sp<SkData> fName;
70 SkPoint fPoint;
71 SkPDFIndirectReference fPage;
72 };
73
74
75 struct SkPDFLink {
76 enum class Type {
77 kNone,
78 kUrl,
79 kNamedDestination,
80 };
81
82 SkPDFLink(Type type, SkData* data, const SkRect& rect, int elemId)
83 : fType(type)
84 , fData(sk_ref_sp(data))
85 , fRect(rect)
86 , fElemId(elemId) {}
87 const Type fType;
88 // The url or named destination, depending on |fType|.
89 const sk_sp<SkData> fData;
90 const SkRect fRect;
91 const int fElemId;
92 };
93
94
95 /** Concrete implementation of SkDocument that creates PDF files. This
96 class does not produced linearized or optimized PDFs; instead it
97 it attempts to use a minimum amount of RAM. */
98 class SkPDFDocument : public SkDocument {
99 public:
100 SkPDFDocument(SkWStream*, SkPDF::Metadata);
101 ~SkPDFDocument() override;
102 SkCanvas* onBeginPage(SkScalar, SkScalar) override;
103 void onEndPage() override;
104 void onClose(SkWStream*) override;
105 void onAbort() override;
106
107 /**
108 Serialize the object, as well as any other objects it
109 indirectly refers to. If any any other objects have been added
110 to the SkPDFObjNumMap without serializing them, they will be
111 serialized as well.
112
113 It might go without saying that objects should not be changed
114 after calling serialize, since those changes will be too late.
115 */
116 SkPDFIndirectReference emit(const SkPDFObject&, SkPDFIndirectReference);
117 SkPDFIndirectReference emit(const SkPDFObject& o) { return this->emit(o, this->reserveRef()); }
118
119 template <typename T>
120 void emitStream(const SkPDFDict& dict, T writeStream, SkPDFIndirectReference ref) {
121 SkAutoMutexExclusive lock(fMutex);
122 SkWStream* stream = this->beginObject(ref);
123 dict.emitObject(stream);
124 stream->writeText(" stream\n");
125 writeStream(stream);
126 stream->writeText("\nendstream");
127 this->endObject();
128 }
129
130 const SkPDF::Metadata& metadata() const { return fMetadata; }
131
132 SkPDFIndirectReference getPage(size_t pageIndex) const;
133 bool hasCurrentPage() const { return bool(fPageDevice); }
134 SkPDFIndirectReference currentPage() const {
135 return SkASSERT(this->hasCurrentPage() && !fPageRefs.empty()), fPageRefs.back();
136 }
137
138 // Create a new marked-content identifier (MCID) to be used with a marked-content sequence
139 // parented by the structure element (StructElem) with the given element identifier (elemId).
140 // Returns a false Mark if if elemId does not refer to a StructElem.
141 SkPDFStructTree::Mark createMarkForElemId(int elemId);
142
143 // Create a key to use with /StructParent in a content item (usually an annotation) which refers
144 // to the structure element (StructElem) with the given element identifier (elemId).
145 // Returns -1 if elemId does not refer to a StructElem.
146 int createStructParentKeyForElemId(int elemId, SkPDFIndirectReference contentItemRef);
147
148 void addStructElemTitle(int elemId, SkSpan<const char>);
149
150 std::unique_ptr<SkPDFArray> getAnnotations();
151
152 // Every reference returned by this method must be passed to `emit` exactly once.
153 SkPDFIndirectReference reserveRef() { return SkPDFIndirectReference{fNextObjectNumber++}; }
154
155 // Returns a tag to prepend to a PostScript name of a subset font. Includes the '+'.
156 SkString nextFontSubsetTag();
157
158 SkExecutor* executor() const { return fExecutor; }
159 void incrementJobCount();
160 void signalJobComplete();
161 size_t currentPageIndex() { return fPages.size(); }
162 size_t pageCount() { return fPageRefs.size(); }
163
164 const SkMatrix& currentPageTransform() const;
165
166 // Canonicalized objects
167 skia_private::THashMap<SkPDFImageShaderKey,
168 SkPDFIndirectReference,
169 SkPDFImageShaderKey::Hash> fImageShaderMap;
170 skia_private::THashMap<SkPDFGradientShader::Key,
171 SkPDFIndirectReference,
172 SkPDFGradientShader::KeyHash> fGradientPatternMap;
173 skia_private::THashMap<SkBitmapKey, SkPDFIndirectReference> fPDFBitmapMap;
174 skia_private::THashMap<SkPDFIccProfileKey,
175 SkPDFIndirectReference,
176 SkPDFIccProfileKey::Hash> fICCProfileMap;
177 skia_private::THashMap<SkTypefaceID, std::unique_ptr<SkAdvancedTypefaceMetrics>> fTypefaceMetrics;
178 skia_private::THashMap<SkTypefaceID, std::vector<SkString>> fType1GlyphNames;
179 skia_private::THashMap<SkTypefaceID, std::vector<SkUnichar>> fToUnicodeMap;
180 skia_private::THashMap<SkTypefaceID, skia_private::THashMap<SkGlyphID, SkString>> fToUnicodeMapEx;
181 skia_private::THashMap<SkTypefaceID, SkPDFIndirectReference> fFontDescriptors;
182 skia_private::THashMap<SkTypefaceID, SkPDFIndirectReference> fType3FontDescriptors;
183 skia_private::THashTable<sk_sp<SkPDFStrike>, const SkDescriptor&, SkPDFStrike::Traits> fStrikes;
184 skia_private::THashMap<SkPDFStrokeGraphicState,
185 SkPDFIndirectReference,
186 SkPDFStrokeGraphicState::Hash> fStrokeGSMap;
187 skia_private::THashMap<SkPDFFillGraphicState,
188 SkPDFIndirectReference,
189 SkPDFFillGraphicState::Hash> fFillGSMap;
190 SkPDFIndirectReference fInvertFunction;
191 SkPDFIndirectReference fNoSmaskGraphicState;
192 std::vector<std::unique_ptr<SkPDFLink>> fCurrentPageLinks;
193 std::vector<SkPDFNamedDestination> fNamedDestinations;
194
195 private:
196 SkPDFOffsetMap fOffsetMap;
197 SkCanvas fCanvas;
198 std::vector<std::unique_ptr<SkPDFDict>> fPages;
199 std::vector<SkPDFIndirectReference> fPageRefs;
200
201 sk_sp<SkPDFDevice> fPageDevice;
202 std::atomic<int> fNextObjectNumber = {1};
203 std::atomic<int> fJobCount = {0};
204 uint32_t fNextFontSubsetTag = {0};
205 SkUUID fUUID;
206 SkPDFIndirectReference fInfoDict;
207 SkPDFIndirectReference fXMP;
208 const SkPDF::Metadata fMetadata;
209 const SkScalar fRasterScale;
210 const SkScalar fInverseRasterScale;
211 SkExecutor *const fExecutor;
212
213 // For tagged PDFs.
214 SkPDFStructTree fStructTree;
215
216 SkMutex fMutex;
217 SkSemaphore fSemaphore;
218
219 void waitForJobs();
220 SkWStream* beginObject(SkPDFIndirectReference);
221 void endObject();
222 };
223
224 #endif // SkPDFDocumentPriv_DEFINED