QPDFObjectHandle_private.hh
13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#ifndef OBJECTHANDLE_PRIVATE_HH
#define OBJECTHANDLE_PRIVATE_HH
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QUtil.hh>
namespace qpdf
{
class Array final: public BaseHandle
{
public:
explicit Array(std::shared_ptr<QPDFObject> const& obj) :
BaseHandle(obj)
{
}
explicit Array(std::shared_ptr<QPDFObject>&& obj) :
BaseHandle(std::move(obj))
{
}
using iterator = std::vector<QPDFObjectHandle>::iterator;
using const_iterator = std::vector<QPDFObjectHandle>::const_iterator;
using const_reverse_iterator = std::vector<QPDFObjectHandle>::const_reverse_iterator;
iterator begin();
iterator end();
const_iterator cbegin();
const_iterator cend();
const_reverse_iterator crbegin();
const_reverse_iterator crend();
int size() const;
std::pair<bool, QPDFObjectHandle> at(int n) const;
bool setAt(int at, QPDFObjectHandle const& oh);
bool insert(int at, QPDFObjectHandle const& item);
void push_back(QPDFObjectHandle const& item);
bool erase(int at);
std::vector<QPDFObjectHandle> getAsVector() const;
void setFromVector(std::vector<QPDFObjectHandle> const& items);
private:
QPDF_Array* array() const;
void checkOwnership(QPDFObjectHandle const& item) const;
QPDFObjectHandle null() const;
std::unique_ptr<std::vector<QPDFObjectHandle>> sp_elements{};
};
// BaseDictionary is only used as a base class. It does not contain any methods exposed in the
// public API.
class BaseDictionary: public BaseHandle
{
public:
using iterator = std::map<std::string, QPDFObjectHandle>::iterator;
using const_iterator = std::map<std::string, QPDFObjectHandle>::const_iterator;
using reverse_iterator = std::map<std::string, QPDFObjectHandle>::reverse_iterator;
using const_reverse_iterator =
std::map<std::string, QPDFObjectHandle>::const_reverse_iterator;
iterator
begin()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.begin();
}
return {};
}
iterator
end()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.end();
}
return {};
}
const_iterator
cbegin()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.cbegin();
}
return {};
}
const_iterator
cend()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.cend();
}
return {};
}
reverse_iterator
rbegin()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.rbegin();
}
return {};
}
reverse_iterator
rend()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.rend();
}
return {};
}
const_reverse_iterator
crbegin()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.crbegin();
}
return {};
}
const_reverse_iterator
crend()
{
if (auto d = as<QPDF_Dictionary>()) {
return d->items.crend();
}
return {};
}
// The following methods are not part of the public API.
bool hasKey(std::string const& key) const;
QPDFObjectHandle getKey(std::string const& key) const;
std::set<std::string> getKeys();
std::map<std::string, QPDFObjectHandle> const& getAsMap() const;
void removeKey(std::string const& key);
void replaceKey(std::string const& key, QPDFObjectHandle value);
protected:
BaseDictionary() = default;
BaseDictionary(std::shared_ptr<QPDFObject> const& obj) :
BaseHandle(obj) {};
BaseDictionary(std::shared_ptr<QPDFObject>&& obj) :
BaseHandle(std::move(obj)) {};
BaseDictionary(BaseDictionary const&) = default;
BaseDictionary& operator=(BaseDictionary const&) = default;
BaseDictionary(BaseDictionary&&) = default;
BaseDictionary& operator=(BaseDictionary&&) = default;
~BaseDictionary() = default;
QPDF_Dictionary* dict() const;
};
class Dictionary final: public BaseDictionary
{
public:
explicit Dictionary(std::shared_ptr<QPDFObject> const& obj) :
BaseDictionary(obj)
{
}
explicit Dictionary(std::shared_ptr<QPDFObject>&& obj) :
BaseDictionary(std::move(obj))
{
}
};
class Name final: public BaseHandle
{
public:
// Put # into strings with characters unsuitable for name token
static std::string normalize(std::string const& name);
// Check whether name is valid utf-8 and whether it contains characters that require
// escaping. Return {false, false} if the name is not valid utf-8, otherwise return {true,
// true} if no characters require or {true, false} if escaping is required.
static std::pair<bool, bool> analyzeJSONEncoding(std::string const& name);
};
class Stream final: public BaseHandle
{
public:
explicit Stream(std::shared_ptr<QPDFObject> const& obj) :
BaseHandle(obj)
{
}
explicit Stream(std::shared_ptr<QPDFObject>&& obj) :
BaseHandle(std::move(obj))
{
}
Stream(
QPDF& qpdf,
QPDFObjGen og,
QPDFObjectHandle stream_dict,
qpdf_offset_t offset,
size_t length);
QPDFObjectHandle
getDict() const
{
return stream()->stream_dict;
}
bool
isDataModified() const
{
return !stream()->token_filters.empty();
}
void
setFilterOnWrite(bool val)
{
stream()->filter_on_write = val;
}
bool
getFilterOnWrite() const
{
return stream()->filter_on_write;
}
// Methods to help QPDF copy foreign streams
size_t
getLength() const
{
return stream()->length;
}
std::shared_ptr<Buffer>
getStreamDataBuffer() const
{
return stream()->stream_data;
}
std::shared_ptr<QPDFObjectHandle::StreamDataProvider>
getStreamDataProvider() const
{
return stream()->stream_provider;
}
// See comments in QPDFObjectHandle.hh for these methods.
bool pipeStreamData(
Pipeline* p,
bool* tried_filtering,
int encode_flags,
qpdf_stream_decode_level_e decode_level,
bool suppress_warnings,
bool will_retry);
std::shared_ptr<Buffer> getStreamData(qpdf_stream_decode_level_e level);
std::shared_ptr<Buffer> getRawStreamData();
void replaceStreamData(
std::shared_ptr<Buffer> data,
QPDFObjectHandle const& filter,
QPDFObjectHandle const& decode_parms);
void replaceStreamData(
std::shared_ptr<QPDFObjectHandle::StreamDataProvider> provider,
QPDFObjectHandle const& filter,
QPDFObjectHandle const& decode_parms);
void
addTokenFilter(std::shared_ptr<QPDFObjectHandle::TokenFilter> token_filter)
{
stream()->token_filters.emplace_back(token_filter);
}
JSON getStreamJSON(
int json_version,
qpdf_json_stream_data_e json_data,
qpdf_stream_decode_level_e decode_level,
Pipeline* p,
std::string const& data_filename);
qpdf_stream_decode_level_e writeStreamJSON(
int json_version,
JSON::Writer& jw,
qpdf_json_stream_data_e json_data,
qpdf_stream_decode_level_e decode_level,
Pipeline* p,
std::string const& data_filename,
bool no_data_key = false);
void
replaceDict(QPDFObjectHandle const& new_dict)
{
auto s = stream();
s->stream_dict = new_dict;
setDictDescription();
}
void setDictDescription();
static void registerStreamFilter(
std::string const& filter_name,
std::function<std::shared_ptr<QPDFStreamFilter>()> factory);
private:
QPDF_Stream::Members*
stream() const
{
if (auto s = as<QPDF_Stream>()) {
if (auto ptr = s->m.get()) {
return ptr;
}
throw std::logic_error("QPDF_Stream: unexpected nullptr");
}
throw std::runtime_error("operation for stream attempted on non-stream object");
return nullptr; // unreachable
}
bool filterable(
std::vector<std::shared_ptr<QPDFStreamFilter>>& filters,
bool& specialized_compression,
bool& lossy_compression);
void replaceFilterData(
QPDFObjectHandle const& filter, QPDFObjectHandle const& decode_parms, size_t length);
void warn(std::string const& message);
static std::map<std::string, std::string> filter_abbreviations;
static std::map<std::string, std::function<std::shared_ptr<QPDFStreamFilter>()>>
filter_factories;
};
template <typename T>
T*
BaseHandle::as() const
{
if (!obj) {
return nullptr;
}
if (std::holds_alternative<T>(obj->value)) {
return &std::get<T>(obj->value);
}
if (std::holds_alternative<QPDF_Unresolved>(obj->value)) {
return BaseHandle(QPDF::Resolver::resolved(obj->qpdf, obj->og)).as<T>();
}
if (std::holds_alternative<QPDF_Reference>(obj->value)) {
// see comment in QPDF_Reference.
return BaseHandle(std::get<QPDF_Reference>(obj->value).obj).as<T>();
}
return nullptr;
}
inline QPDFObjGen
BaseHandle::id_gen() const
{
return obj ? obj->og : QPDFObjGen();
}
inline bool
BaseHandle::indirect() const
{
return obj ? obj->og.isIndirect() : false;
}
inline bool
BaseHandle::null() const
{
return !obj || type_code() == ::ot_null;
}
inline QPDF*
BaseHandle::qpdf() const
{
return obj ? obj->qpdf : nullptr;
}
inline qpdf_object_type_e
BaseHandle::raw_type_code() const
{
return obj ? static_cast<qpdf_object_type_e>(obj->value.index()) : ::ot_uninitialized;
}
inline qpdf_object_type_e
BaseHandle::resolved_type_code() const
{
if (!obj) {
return ::ot_uninitialized;
}
if (raw_type_code() == ::ot_unresolved) {
return QPDF::Resolver::resolved(obj->qpdf, obj->og)->getTypeCode();
}
return raw_type_code();
}
inline qpdf_object_type_e
BaseHandle::type_code() const
{
if (!obj) {
return ::ot_uninitialized;
}
if (raw_type_code() == ::ot_unresolved) {
return QPDF::Resolver::resolved(obj->qpdf, obj->og)->getTypeCode();
}
if (raw_type_code() == ::ot_reference) {
return std::get<QPDF_Reference>(obj->value).obj->getTypeCode();
}
return raw_type_code();
}
} // namespace qpdf
inline QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) :
items(std::move(items))
{
}
inline std::shared_ptr<QPDFObject>
QPDF_Null::create(
std::shared_ptr<QPDFObject> parent, std::string_view const& static_descr, std::string var_descr)
{
auto n = QPDFObject::create<QPDF_Null>();
n->setChildDescription(parent->getQPDF(), parent, static_descr, var_descr);
return n;
}
inline QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) :
val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes))
{
}
template <typename T, typename... Args>
inline std::shared_ptr<QPDFObject>
QPDFObject::create(Args&&... args)
{
return std::make_shared<QPDFObject>(std::forward<T>(T(std::forward<Args>(args)...)));
}
inline qpdf::Array
QPDFObjectHandle::as_array(qpdf::typed options) const
{
if (options & qpdf::error) {
assertType("array", false);
}
if (options & qpdf::any_flag || type_code() == ::ot_array ||
(options & qpdf::optional && type_code() == ::ot_null)) {
return qpdf::Array(obj);
}
return qpdf::Array(std::shared_ptr<QPDFObject>());
}
inline qpdf::Dictionary
QPDFObjectHandle::as_dictionary(qpdf::typed options) const
{
if (options & qpdf::any_flag || type_code() == ::ot_dictionary ||
(options & qpdf::optional && type_code() == ::ot_null)) {
return qpdf::Dictionary(obj);
}
if (options & qpdf::error) {
assertType("dictionary", false);
}
return qpdf::Dictionary(std::shared_ptr<QPDFObject>());
}
inline qpdf::Stream
QPDFObjectHandle::as_stream(qpdf::typed options) const
{
if (options & qpdf::any_flag || type_code() == ::ot_stream ||
(options & qpdf::optional && type_code() == ::ot_null)) {
return qpdf::Stream(obj);
}
if (options & qpdf::error) {
assertType("stream", false);
}
return qpdf::Stream(std::shared_ptr<QPDFObject>());
}
#endif // OBJECTHANDLE_PRIVATE_HH