Debian Codegraph @

src/pdf/SkPDFTypes.h

raw
1 /*
2 * Copyright 2010 The Android Open Source Project
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
8 #ifndef SkPDFTypes_DEFINED
9 #define SkPDFTypes_DEFINED
10
11 #include "include/core/SkScalar.h"
12 #include "include/core/SkSpan.h"
13 #include "include/core/SkTypes.h"
14 #include "src/pdf/SkPDFUnion.h"
15
16 #include <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21
22 class SkPDFDocument;
23 class SkStreamAsset;
24 class SkString;
25 class SkWStream;
26
27 #ifndef SK_PDF_MASK_QUALITY
28 // If MASK_QUALITY is in [0,100], will be used for JpegEncoder.
29 // Otherwise, just encode masks losslessly.
30 #define SK_PDF_MASK_QUALITY 50
31 // Since these masks are used for blurry shadows, we shouldn't need
32 // high quality. Raise this value if your shadows have visible JPEG
33 // artifacts.
34 // If SkJpegEncoder::Encode fails, we will fall back to the lossless
35 // encoding.
36 #endif
37
38 struct SkPDFIndirectReference {
39 int fValue = -1;
40 explicit operator bool() const { return fValue != -1; }
41
42 bool operator==(SkPDFIndirectReference v) const {
43 return fValue == v.fValue;
44 }
45
46 bool operator!=(SkPDFIndirectReference v) const {
47 return fValue != v.fValue;
48 }
49 };
50
51 /** \class SkPDFObject
52
53 A PDF Object is the base class for primitive elements in a PDF file. A
54 common subtype is used to ease the use of indirect object references,
55 which are common in the PDF format.
56
57 */
58 class SkPDFObject {
59 public:
60 SkPDFObject() = default;
61
62 /** Subclasses must implement this method to print the object to the
63 * PDF file.
64 * @param catalog The object catalog to use.
65 * @param stream The writable output stream to send the output to.
66 */
67 virtual void emitObject(SkWStream* stream) const = 0;
68
69 virtual ~SkPDFObject() = default;
70
71 private:
72 SkPDFObject(SkPDFObject&&) = delete;
73 SkPDFObject(const SkPDFObject&) = delete;
74 SkPDFObject& operator=(SkPDFObject&&) = delete;
75 SkPDFObject& operator=(const SkPDFObject&) = delete;
76 };
77
78 ////////////////////////////////////////////////////////////////////////////////
79
80 /** \class SkPDFArray
81
82 An array object in a PDF.
83 */
84 class SkPDFArray : public SkPDFObject {
85 public:
86 /** Create a PDF array. Maximum length is 8191.
87 */
88 SkPDFArray();
89 ~SkPDFArray() override;
90
91 // The SkPDFObject interface.
92 void emitObject(SkWStream* stream) const override;
93
94 /** The size of the array.
95 */
96 size_t size() const;
97
98 /** Preallocate space for the given number of entries.
99 * @param length The number of array slots to preallocate.
100 */
101 void reserve(int length);
102
103 /** Appends a value to the end of the array.
104 * @param value The value to add to the array.
105 */
106 void appendInt(int32_t);
107 void appendColorComponent(uint8_t);
108 void appendColorComponentF(float);
109 void appendBool(bool);
110 void appendScalar(SkScalar);
111 void appendName(const char[]);
112 void appendName(SkString);
113 void appendByteString(const char[]);
114 void appendTextString(const char[]);
115 void appendByteString(SkString);
116 void appendTextString(SkString);
117 void appendObject(std::unique_ptr<SkPDFObject>&&);
118 void appendRef(SkPDFIndirectReference);
119
120 protected:
121 SkSpan<const SkPDFUnion> values() const { return SkSpan(fValues); }
122
123 private:
124 std::vector<SkPDFUnion> fValues;
125 void append(SkPDFUnion&& value);
126 };
127
128 static inline void SkPDFArray_Append(SkPDFArray* a, int v) { a->appendInt(v); }
129
130 static inline void SkPDFArray_Append(SkPDFArray* a, SkScalar v) { a->appendScalar(v); }
131
132 template <typename T, typename... Args>
133 static inline void SkPDFArray_Append(SkPDFArray* a, T v, Args... args) {
134 SkPDFArray_Append(a, v);
135 SkPDFArray_Append(a, args...);
136 }
137
138 static inline void SkPDFArray_Append(SkPDFArray* a) {}
139
140 template <typename... Args>
141 static inline std::unique_ptr<SkPDFArray> SkPDFMakeArray(Args... args) {
142 std::unique_ptr<SkPDFArray> ret(new SkPDFArray());
143 ret->reserve(sizeof...(Args));
144 SkPDFArray_Append(ret.get(), args...);
145 return ret;
146 }
147
148 /** \class SkPDFOptionalArray
149 *
150 * An SkPDFArray which may be emitted as a non-array if it contains a single entry.
151 * Search the specification for "or an array" for where this can be used.
152 */
153 class SkPDFOptionalArray final : public SkPDFArray {
154 void emitObject(SkWStream* stream) const override;
155 };
156
157 /** \class SkPDFDict
158
159 A dictionary object in a PDF.
160 */
161 class SkPDFDict final : public SkPDFObject {
162 public:
163 /** Create a PDF dictionary.
164 * @param type The value of the Type entry, nullptr for no type.
165 */
166 explicit SkPDFDict(const char type[] = nullptr);
167
168 ~SkPDFDict() override;
169
170 // The SkPDFObject interface.
171 void emitObject(SkWStream* stream) const override;
172
173 /** The size of the dictionary.
174 */
175 size_t size() const;
176
177 /** Preallocate space for n key-value pairs */
178 void reserve(int n);
179
180 /** Add the value to the dictionary with the given key.
181 * @param key The text of the key for this dictionary entry.
182 * @param value The value for this dictionary entry.
183 */
184 void insertObject(const char key[], std::unique_ptr<SkPDFObject>&&);
185 void insertObject(SkString, std::unique_ptr<SkPDFObject>&&);
186 void insertRef(const char key[], SkPDFIndirectReference);
187 void insertRef(SkString, SkPDFIndirectReference);
188
189 /** Add the value to the dictionary with the given key.
190 * @param key The text of the key for this dictionary entry.
191 * @param value The value for this dictionary entry.
192 */
193 void insertBool(const char key[], bool value);
194 void insertInt(const char key[], int32_t value);
195 void insertInt(const char key[], size_t value);
196 void insertScalar(const char key[], SkScalar value);
197 void insertColorComponentF(const char key[], SkScalar value);
198 void insertName(const char key[], const char nameValue[]);
199 void insertName(const char key[], SkString nameValue);
200 void insertByteString(const char key[], const char value[]);
201 void insertTextString(const char key[], const char value[]);
202 void insertByteString(const char key[], SkString value);
203 void insertTextString(const char key[], SkString value);
204 void insertUnion(const char key[], SkPDFUnion&&);
205
206 private:
207 std::vector<std::pair<SkPDFUnion, SkPDFUnion>> fRecords;
208 };
209
210 static inline std::unique_ptr<SkPDFDict> SkPDFMakeDict(const char* type = nullptr) {
211 return std::make_unique<SkPDFDict>(type);
212 }
213
214 enum class SkPDFSteamCompressionEnabled : bool {
215 No = false,
216 Yes = true,
217 };
218
219 // Exposed for unit testing.
220 void SkPDFWriteTextString(SkWStream* wStream, const char* cin, size_t len);
221 void SkPDFWriteByteString(SkWStream* wStream, const char* cin, size_t len);
222
223 SkPDFIndirectReference SkPDFStreamOut(
224 std::unique_ptr<SkPDFDict> dict,
225 std::unique_ptr<SkStreamAsset> stream,
226 SkPDFDocument* doc,
227 SkPDFSteamCompressionEnabled compress = SkPDFSteamCompressionEnabled::Yes);
228 #endif