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/SkStream.h"
12 #include "include/docs/SkPDFDocument.h"
13 #include "include/private/SkMutex.h"
14 #include "include/private/SkTHash.h"
15 #include "src/pdf/SkPDFMetadata.h"
16 #include "src/pdf/SkPDFTag.h"
17 
18 #include <atomic>
19 #include <vector>
20 #include <memory>
21 
22 class SkExecutor;
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 }
35 
36 const char* SkPDFGetNodeIdKey();
37 
38 // Logically part of SkPDFDocument, but separate to keep similar functionality together.
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;
47     size_t fBaseOffset = SIZE_MAX;
48 };
49 
50 
51 struct SkPDFNamedDestination {
52     sk_sp<SkData> fName;
53     SkPoint fPoint;
54     SkPDFIndirectReference fPage;
55 };
56 
57 /** Concrete implementation of SkDocument that creates PDF files. This
58     class does not produced linearized or optimized PDFs; instead it
59     it attempts to use a minimum amount of RAM. */
60 class SkPDFDocument : public SkDocument {
61 public:
62     SkPDFDocument(SkWStream*, SkPDF::Metadata);
63     ~SkPDFDocument() override;
64     SkCanvas* onBeginPage(SkScalar, SkScalar) override;
65     void onEndPage() override;
66     void onClose(SkWStream*) override;
67     void onAbort() override;
68 
69     /**
70        Serialize the object, as well as any other objects it
71        indirectly refers to.  If any any other objects have been added
72        to the SkPDFObjNumMap without serializing them, they will be
73        serialized as well.
74 
75        It might go without saying that objects should not be changed
76        after calling serialize, since those changes will be too late.
77      */
78     SkPDFIndirectReference emit(const SkPDFObject&, SkPDFIndirectReference);
emit(const SkPDFObject & o)79     SkPDFIndirectReference emit(const SkPDFObject& o) { return this->emit(o, this->reserveRef()); }
80 
81     template <typename T>
emitStream(const SkPDFDict & dict,T writeStream,SkPDFIndirectReference ref)82     void emitStream(const SkPDFDict& dict, T writeStream, SkPDFIndirectReference ref) {
83         SkAutoMutexExclusive lock(fMutex);
84         SkWStream* stream = this->beginObject(ref);
85         dict.emitObject(stream);
86         stream->writeText(" stream\n");
87         writeStream(stream);
88         stream->writeText("\nendstream");
89         this->endObject();
90     }
91 
metadata()92     const SkPDF::Metadata& metadata() const { return fMetadata; }
93 
94     SkPDFIndirectReference getPage(size_t pageIndex) const;
currentPage()95     SkPDFIndirectReference currentPage() const {
96         return SkASSERT(!fPageRefs.empty()), fPageRefs.back();
97     }
98     // Returns -1 if no mark ID.
99     int getMarkIdForNodeId(int nodeId);
100 
reserveRef()101     SkPDFIndirectReference reserveRef() { return SkPDFIndirectReference{fNextObjectNumber++}; }
102 
executor()103     SkExecutor* executor() const { return fExecutor; }
104     void incrementJobCount();
105     void signalJobComplete();
currentPageIndex()106     size_t currentPageIndex() { return fPages.size(); }
pageCount()107     size_t pageCount() { return fPageRefs.size(); }
108 
109     const SkMatrix& currentPageTransform() const;
110 
111     // Canonicalized objects
112     SkTHashMap<SkPDFImageShaderKey, SkPDFIndirectReference> fImageShaderMap;
113     SkTHashMap<SkPDFGradientShader::Key, SkPDFIndirectReference, SkPDFGradientShader::KeyHash>
114         fGradientPatternMap;
115     SkTHashMap<SkBitmapKey, SkPDFIndirectReference> fPDFBitmapMap;
116     SkTHashMap<uint32_t, std::unique_ptr<SkAdvancedTypefaceMetrics>> fTypefaceMetrics;
117     SkTHashMap<uint32_t, std::vector<SkString>> fType1GlyphNames;
118     SkTHashMap<uint32_t, std::vector<SkUnichar>> fToUnicodeMap;
119     SkTHashMap<uint32_t, SkPDFIndirectReference> fFontDescriptors;
120     SkTHashMap<uint32_t, SkPDFIndirectReference> fType3FontDescriptors;
121     SkTHashMap<uint64_t, SkPDFFont> fFontMap;
122     SkTHashMap<SkPDFStrokeGraphicState, SkPDFIndirectReference> fStrokeGSMap;
123     SkTHashMap<SkPDFFillGraphicState, SkPDFIndirectReference> fFillGSMap;
124     SkPDFIndirectReference fInvertFunction;
125     SkPDFIndirectReference fNoSmaskGraphicState;
126 
127     std::vector<std::pair<sk_sp<SkData>, SkRect>> fCurrentPageLinkToURLs;
128     std::vector<std::pair<sk_sp<SkData>, SkRect>> fCurrentPageLinkToDestinations;
129     std::vector<SkPDFNamedDestination> fNamedDestinations;
130 
131 private:
132     SkPDFOffsetMap fOffsetMap;
133     SkCanvas fCanvas;
134     std::vector<std::unique_ptr<SkPDFDict>> fPages;
135     std::vector<SkPDFIndirectReference> fPageRefs;
136 
137     sk_sp<SkPDFDevice> fPageDevice;
138     std::atomic<int> fNextObjectNumber = {1};
139     std::atomic<int> fJobCount = {0};
140     SkUUID fUUID;
141     SkPDFIndirectReference fInfoDict;
142     SkPDFIndirectReference fXMP;
143     SkPDF::Metadata fMetadata;
144     SkScalar fRasterScale = 1;
145     SkScalar fInverseRasterScale = 1;
146     SkExecutor* fExecutor = nullptr;
147 
148     // For tagged PDFs.
149     SkPDFTagTree fTagTree;
150 
151     SkMutex fMutex;
152     SkSemaphore fSemaphore;
153 
154     void waitForJobs();
155     SkWStream* beginObject(SkPDFIndirectReference);
156     void endObject();
157 };
158 
159 #endif  // SkPDFDocumentPriv_DEFINED
160