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