Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
14#include "src/core/SkTHash.h"
15#include "src/pdf/SkPDFBitmap.h"
18#include "src/pdf/SkPDFShader.h"
19#include "src/pdf/SkPDFTag.h"
20
21#include <atomic>
22#include <vector>
23#include <memory>
24
25class SkExecutor;
26class SkPDFDevice;
27class SkPDFFont;
29struct SkBitmapKey;
30
32struct Key;
33struct KeyHash;
34} // namespace SkPDFGradientShader
35
36const char* SkPDFGetNodeIdKey();
37
38// Logically part of SkPDFDocument, but separate to keep similar functionality together.
40public:
41 void markStartOfDocument(const SkWStream*);
42 void markStartOfObject(int referenceNumber, const SkWStream*);
43 int objectCount() const;
45private:
46 std::vector<int> fOffsets;
47 size_t fBaseOffset = SIZE_MAX;
48};
49
50
56
57
58struct SkPDFLink {
59 enum class Type {
60 kNone,
61 kUrl,
63 };
64
65 SkPDFLink(Type type, SkData* data, const SkRect& rect, int nodeId)
66 : fType(type)
67 , fData(sk_ref_sp(data))
68 , fRect(rect)
69 , fNodeId(nodeId) {}
70 const Type fType;
71 // The url or named destination, depending on |fType|.
74 const int fNodeId;
75};
76
77
78/** Concrete implementation of SkDocument that creates PDF files. This
79 class does not produced linearized or optimized PDFs; instead it
80 it attempts to use a minimum amount of RAM. */
81class SkPDFDocument : public SkDocument {
82public:
84 ~SkPDFDocument() override;
86 void onEndPage() override;
87 void onClose(SkWStream*) override;
88 void onAbort() override;
89
90 /**
91 Serialize the object, as well as any other objects it
92 indirectly refers to. If any any other objects have been added
93 to the SkPDFObjNumMap without serializing them, they will be
94 serialized as well.
95
96 It might go without saying that objects should not be changed
97 after calling serialize, since those changes will be too late.
98 */
100 SkPDFIndirectReference emit(const SkPDFObject& o) { return this->emit(o, this->reserveRef()); }
101
102 template <typename T>
103 void emitStream(const SkPDFDict& dict, T writeStream, SkPDFIndirectReference ref) {
104 SkAutoMutexExclusive lock(fMutex);
105 SkWStream* stream = this->beginObject(ref);
106 dict.emitObject(stream);
107 stream->writeText(" stream\n");
108 writeStream(stream);
109 stream->writeText("\nendstream");
110 this->endObject();
111 }
112
113 const SkPDF::Metadata& metadata() const { return fMetadata; }
114
115 SkPDFIndirectReference getPage(size_t pageIndex) const;
116 bool hasCurrentPage() const { return bool(fPageDevice); }
118 return SkASSERT(this->hasCurrentPage() && !fPageRefs.empty()), fPageRefs.back();
119 }
120 // Used to allow marked content to refer to its corresponding structure
121 // tree node, via a page entry in the parent tree. Returns -1 if no
122 // mark ID.
124 // Used to allow annotations to refer to their corresponding structure
125 // tree node, via the struct parent tree. Returns -1 if no struct parent
126 // key.
127 int createStructParentKeyForNodeId(int nodeId);
128
129 void addNodeTitle(int nodeId, SkSpan<const char>);
130
131 std::unique_ptr<SkPDFArray> getAnnotations();
132
134
135 // Returns a tag to prepend to a PostScript name of a subset font. Includes the '+'.
137
138 SkExecutor* executor() const { return fExecutor; }
139 void incrementJobCount();
140 void signalJobComplete();
141 size_t currentPageIndex() { return fPages.size(); }
142 size_t pageCount() { return fPageRefs.size(); }
143
144 const SkMatrix& currentPageTransform() const;
145
146 // Canonicalized objects
171 std::vector<std::unique_ptr<SkPDFLink>> fCurrentPageLinks;
172 std::vector<SkPDFNamedDestination> fNamedDestinations;
173
174private:
175 SkPDFOffsetMap fOffsetMap;
176 SkCanvas fCanvas;
177 std::vector<std::unique_ptr<SkPDFDict>> fPages;
178 std::vector<SkPDFIndirectReference> fPageRefs;
179
180 sk_sp<SkPDFDevice> fPageDevice;
181 std::atomic<int> fNextObjectNumber = {1};
182 std::atomic<int> fJobCount = {0};
183 uint32_t fNextFontSubsetTag = {0};
184 SkUUID fUUID;
185 SkPDFIndirectReference fInfoDict;
187 SkPDF::Metadata fMetadata;
188 SkScalar fRasterScale = 1;
189 SkScalar fInverseRasterScale = 1;
190 SkExecutor* fExecutor = nullptr;
191
192 // For tagged PDFs.
193 SkPDFTagTree fTagTree;
194
195 SkMutex fMutex;
196 SkSemaphore fSemaphore;
197
198 void waitForJobs();
199 SkWStream* beginObject(SkPDFIndirectReference);
200 void endObject();
201};
202
203#endif // SkPDFDocumentPriv_DEFINED
#define SkASSERT(cond)
Definition SkAssert.h:116
const char * SkPDFGetNodeIdKey()
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
void emitObject(SkWStream *stream) const override
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 >)
skia_private::THashMap< uint32_t, SkPDFIndirectReference > fFontDescriptors
SkPDFIndirectReference fInvertFunction
const SkPDF::Metadata & metadata() const
const SkMatrix & currentPageTransform() const
std::vector< SkPDFNamedDestination > fNamedDestinations
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
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
#define T
SkPDFIndirectReference fPage
Definition SkUUID.h:9