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