Flutter Engine
The Flutter Engine
SkPDFDocumentPriv.h
Go to the documentation of this file.
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
11#include "include/core/SkData.h"
14#include "include/core/SkRect.h"
17#include "include/core/SkSpan.h" // IWYU pragma: keep
24#include "src/core/SkTHash.h"
25#include "src/pdf/SkPDFBitmap.h"
27#include "src/pdf/SkPDFShader.h"
28#include "src/pdf/SkPDFTag.h"
29#include "src/pdf/SkPDFTypes.h"
30#include "src/pdf/SkUUID.h"
31
32#include <cstddef>
33#include <cstdint>
34#include <atomic>
35#include <vector>
36#include <memory>
37
38class SkExecutor;
39class SkPDFDevice;
40class SkPDFFont;
42struct SkBitmapKey;
43class SkMatrix;
44
46struct Key;
47struct KeyHash;
48} // namespace SkPDFGradientShader
49
50const char* SkPDFGetNodeIdKey();
51
52// Logically part of SkPDFDocument, but separate to keep similar functionality together.
54public:
55 void markStartOfDocument(const SkWStream*);
56 void markStartOfObject(int referenceNumber, const SkWStream*);
57 int objectCount() const;
59private:
60 std::vector<int> fOffsets;
61 size_t fBaseOffset = SIZE_MAX;
62};
63
64
69};
70
71
72struct SkPDFLink {
73 enum class Type {
74 kNone,
75 kUrl,
76 kNamedDestination,
77 };
78
79 SkPDFLink(Type type, SkData* data, const SkRect& rect, int nodeId)
80 : fType(type)
82 , fRect(rect)
83 , fNodeId(nodeId) {}
84 const Type fType;
85 // The url or named destination, depending on |fType|.
88 const int fNodeId;
89};
90
91
92/** Concrete implementation of SkDocument that creates PDF files. This
93 class does not produced linearized or optimized PDFs; instead it
94 it attempts to use a minimum amount of RAM. */
95class SkPDFDocument : public SkDocument {
96public:
98 ~SkPDFDocument() override;
100 void onEndPage() override;
101 void onClose(SkWStream*) override;
102 void onAbort() override;
103
104 /**
105 Serialize the object, as well as any other objects it
106 indirectly refers to. If any any other objects have been added
107 to the SkPDFObjNumMap without serializing them, they will be
108 serialized as well.
109
110 It might go without saying that objects should not be changed
111 after calling serialize, since those changes will be too late.
112 */
114 SkPDFIndirectReference emit(const SkPDFObject& o) { return this->emit(o, this->reserveRef()); }
115
116 template <typename T>
117 void emitStream(const SkPDFDict& dict, T writeStream, SkPDFIndirectReference ref) {
118 SkAutoMutexExclusive lock(fMutex);
119 SkWStream* stream = this->beginObject(ref);
120 dict.emitObject(stream);
121 stream->writeText(" stream\n");
122 writeStream(stream);
123 stream->writeText("\nendstream");
124 this->endObject();
125 }
126
127 const SkPDF::Metadata& metadata() const { return fMetadata; }
128
129 SkPDFIndirectReference getPage(size_t pageIndex) const;
130 bool hasCurrentPage() const { return bool(fPageDevice); }
132 return SkASSERT(this->hasCurrentPage() && !fPageRefs.empty()), fPageRefs.back();
133 }
134 // Used to allow marked content to refer to its corresponding structure
135 // tree node, via a page entry in the parent tree. Returns -1 if no
136 // mark ID.
138 // Used to allow annotations to refer to their corresponding structure
139 // tree node, via the struct parent tree. Returns -1 if no struct parent
140 // key.
141 int createStructParentKeyForNodeId(int nodeId);
142
143 void addNodeTitle(int nodeId, SkSpan<const char>);
144
145 std::unique_ptr<SkPDFArray> getAnnotations();
146
148
149 // Returns a tag to prepend to a PostScript name of a subset font. Includes the '+'.
151
152 SkExecutor* executor() const { return fExecutor; }
153 void incrementJobCount();
154 void signalJobComplete();
155 size_t currentPageIndex() { return fPages.size(); }
156 size_t pageCount() { return fPageRefs.size(); }
157
158 const SkMatrix& currentPageTransform() const;
159
160 // Canonicalized objects
185 std::vector<std::unique_ptr<SkPDFLink>> fCurrentPageLinks;
186 std::vector<SkPDFNamedDestination> fNamedDestinations;
187
188private:
189 SkPDFOffsetMap fOffsetMap;
190 SkCanvas fCanvas;
191 std::vector<std::unique_ptr<SkPDFDict>> fPages;
192 std::vector<SkPDFIndirectReference> fPageRefs;
193
194 sk_sp<SkPDFDevice> fPageDevice;
195 std::atomic<int> fNextObjectNumber = {1};
196 std::atomic<int> fJobCount = {0};
197 uint32_t fNextFontSubsetTag = {0};
198 SkUUID fUUID;
199 SkPDFIndirectReference fInfoDict;
201 SkPDF::Metadata fMetadata;
202 SkScalar fRasterScale = 1;
203 SkScalar fInverseRasterScale = 1;
204 SkExecutor* fExecutor = nullptr;
205
206 // For tagged PDFs.
207 SkPDFTagTree fTagTree;
208
209 SkMutex fMutex;
210 SkSemaphore fSemaphore;
211
212 void waitForJobs();
213 SkWStream* beginObject(SkPDFIndirectReference);
214 void endObject();
215};
216
217#endif // SkPDFDocumentPriv_DEFINED
TArray< uint32_t > Key
#define SkASSERT(cond)
Definition: SkAssert.h:116
const char * SkPDFGetNodeIdKey()
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
GLenum type
Definition: SkData.h:25
void emitObject(SkWStream *stream) const override
Definition: SkPDFTypes.cpp:456
SkPDFIndirectReference emit(const SkPDFObject &o)
SkPDFIndirectReference currentPage() const
SkExecutor * executor() const
void onClose(SkWStream *) override
skia_private::THashMap< SkBitmapKey, SkPDFIndirectReference > fPDFBitmapMap
skia_private::THashMap< SkPDFStrokeGraphicState, SkPDFIndirectReference, SkPDFStrokeGraphicState::Hash > fStrokeGSMap
SkPDFIndirectReference emit(const SkPDFObject &, SkPDFIndirectReference)
size_t currentPageIndex()
skia_private::THashMap< SkPDFFillGraphicState, SkPDFIndirectReference, SkPDFFillGraphicState::Hash > fFillGSMap
void addNodeTitle(int nodeId, SkSpan< const char >)
void signalJobComplete()
skia_private::THashMap< uint32_t, SkPDFIndirectReference > fFontDescriptors
SkPDFIndirectReference fInvertFunction
const SkPDF::Metadata & metadata() const
const SkMatrix & currentPageTransform() const
std::vector< SkPDFNamedDestination > fNamedDestinations
void incrementJobCount()
skia_private::THashMap< SkPDFImageShaderKey, SkPDFIndirectReference, SkPDFImageShaderKey::Hash > fImageShaderMap
SkPDFTagTree::Mark createMarkIdForNodeId(int nodeId, SkPoint)
skia_private::THashMap< uint64_t, SkPDFFont > fFontMap
std::unique_ptr< SkPDFArray > getAnnotations()
std::vector< std::unique_ptr< SkPDFLink > > fCurrentPageLinks
skia_private::THashMap< SkPDFIccProfileKey, SkPDFIndirectReference, SkPDFIccProfileKey::Hash > fICCProfileMap
skia_private::THashMap< SkPDFGradientShader::Key, SkPDFIndirectReference, SkPDFGradientShader::KeyHash > fGradientPatternMap
void onAbort() override
skia_private::THashMap< uint32_t, std::vector< SkString > > fType1GlyphNames
int createStructParentKeyForNodeId(int nodeId)
void emitStream(const SkPDFDict &dict, T writeStream, SkPDFIndirectReference ref)
SkCanvas * onBeginPage(SkScalar, SkScalar) override
SkPDFDocument(SkWStream *, SkPDF::Metadata)
skia_private::THashMap< uint32_t, std::vector< SkUnichar > > fToUnicodeMap
SkPDFIndirectReference fNoSmaskGraphicState
SkString nextFontSubsetTag()
~SkPDFDocument() override
skia_private::THashMap< uint32_t, SkPDFIndirectReference > fType3FontDescriptors
void onEndPage() override
SkPDFIndirectReference reserveRef()
SkPDFIndirectReference getPage(size_t pageIndex) const
skia_private::THashMap< uint32_t, std::unique_ptr< SkAdvancedTypefaceMetrics > > fTypefaceMetrics
bool hasCurrentPage() const
void markStartOfObject(int referenceNumber, const SkWStream *)
void markStartOfDocument(const SkWStream *)
int emitCrossReferenceTable(SkWStream *s) const
int objectCount() const
void ref() const
Definition: SkRefCnt.h:62
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
#define T
Definition: precompiler.cc:65
SkPDFIndirectReference fPage
Definition: SkUUID.h:9
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63