QPDFObject_private.hh
10.7 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#ifndef QPDFOBJECT_HH
#define QPDFOBJECT_HH
// NOTE: This file is called QPDFObject_private.hh instead of QPDFObject.hh because of
// include/qpdf/QPDFObject.hh. See comments there for an explanation.
#include <qpdf/Constants.h>
#include <qpdf/JSON.hh>
#include <qpdf/JSON_writer.hh>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFObjGen.hh>
#include <qpdf/Types.h>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
class Disconnect;
class QPDFObject;
class QPDFObjectHandle;
namespace qpdf
{
class Array;
class BaseDictionary;
class Dictionary;
class Stream;
} // namespace qpdf
class QPDF_Array final
{
private:
struct Sparse
{
size_t size{0};
std::map<size_t, QPDFObjectHandle> elements;
};
public:
QPDF_Array() = default;
QPDF_Array(QPDF_Array const& other) :
sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr)
{
}
QPDF_Array(QPDF_Array&&) = default;
QPDF_Array& operator=(QPDF_Array&&) = default;
private:
friend class QPDFObject;
friend class qpdf::BaseHandle;
friend class qpdf::Array;
QPDF_Array(std::vector<QPDFObjectHandle> const& items) :
elements(items)
{
}
QPDF_Array(std::vector<QPDFObjectHandle>&& items, bool sparse);
QPDF_Array(std::vector<QPDFObjectHandle>&& items) :
elements(std::move(items))
{
}
size_t
size() const
{
return sp ? sp->size : elements.size();
}
std::unique_ptr<Sparse> sp;
std::vector<QPDFObjectHandle> elements;
};
class QPDF_Bool final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
friend class QPDFObjectHandle;
explicit QPDF_Bool(bool val) :
val(val)
{
}
bool val;
};
class QPDF_Destroyed final
{
};
class QPDF_Dictionary final
{
friend class QPDFObject;
friend class qpdf::BaseDictionary;
friend class qpdf::BaseHandle;
QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) :
items(items)
{
}
inline QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items);
std::map<std::string, QPDFObjectHandle> items;
};
class QPDF_InlineImage final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
explicit QPDF_InlineImage(std::string val) :
val(std::move(val))
{
}
std::string val;
};
class QPDF_Integer final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
friend class QPDFObjectHandle;
QPDF_Integer(long long val) :
val(val)
{
}
long long val;
};
class QPDF_Name final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
explicit QPDF_Name(std::string name) :
name(std::move(name))
{
}
std::string name;
};
class QPDF_Null final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
public:
static inline std::shared_ptr<QPDFObject> create(
std::shared_ptr<QPDFObject> parent,
std::string_view const& static_descr,
std::string var_descr);
};
class QPDF_Operator final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
QPDF_Operator(std::string val) :
val(std::move(val))
{
}
std::string val;
};
class QPDF_Real final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
QPDF_Real(std::string val) :
val(std::move(val))
{
}
inline QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes);
// Store reals as strings to avoid roundoff errors.
std::string val;
};
class QPDF_Reference
{
// This is a minimal implementation to support QPDF::replaceObject. Once we support parsing of
// objects that are an indirect reference we will need to support multiple levels of
// indirection, including the possibility of circular references.
friend class QPDFObject;
friend class qpdf::BaseHandle;
QPDF_Reference(std::shared_ptr<QPDFObject> obj) :
obj(std::move(obj))
{
}
std::shared_ptr<QPDFObject> obj;
};
class QPDF_Reserved final
{
};
class QPDF_Stream final
{
class Members
{
friend class QPDF_Stream;
friend class QPDFObject;
friend class qpdf::Stream;
friend class qpdf::BaseHandle;
public:
Members(QPDFObjectHandle stream_dict, size_t length) :
stream_dict(std::move(stream_dict)),
length(length)
{
}
private:
bool filter_on_write{true};
QPDFObjectHandle stream_dict;
size_t length{0};
std::shared_ptr<Buffer> stream_data;
std::shared_ptr<QPDFObjectHandle::StreamDataProvider> stream_provider;
std::vector<std::shared_ptr<QPDFObjectHandle::TokenFilter>> token_filters;
std::string expand_filter_name(std::string const& name) const;
std::function<std::shared_ptr<QPDFStreamFilter>()>
filter_factory(std::string const& name) const;
};
friend class QPDFObject;
friend class qpdf::BaseHandle;
friend class qpdf::Stream;
QPDF_Stream(QPDFObjectHandle stream_dict, size_t length) :
m(std::make_unique<Members>(stream_dict, length))
{
if (!stream_dict.isDictionary()) {
throw std::logic_error(
"stream object instantiated with non-dictionary object for dictionary");
}
}
std::unique_ptr<Members> m;
};
// QPDF_Strings may included embedded null characters.
class QPDF_String final
{
friend class QPDFObject;
friend class qpdf::BaseHandle;
friend class QPDFWriter;
public:
static std::shared_ptr<QPDFObject> create_utf16(std::string const& utf8_val);
std::string unparse(bool force_binary = false);
void writeJSON(int json_version, JSON::Writer& p);
std::string getUTF8Val() const;
private:
QPDF_String(std::string val) :
val(std::move(val))
{
}
bool useHexString() const;
std::string val;
};
class QPDF_Unresolved final
{
};
class QPDFObject
{
public:
template <typename T>
QPDFObject(T&& value) :
value(std::forward<T>(value))
{
}
template <typename T>
QPDFObject(QPDF* qpdf, QPDFObjGen og, T&& value) :
value(std::forward<T>(value)),
qpdf(qpdf),
og(og)
{
}
template <typename T, typename... Args>
inline static std::shared_ptr<QPDFObject> create(Args&&... args);
template <typename T, typename... Args>
inline static std::shared_ptr<QPDFObject>
create(QPDF* qpdf, QPDFObjGen og, Args&&... args)
{
return std::make_shared<QPDFObject>(
qpdf, og, std::forward<T>(T(std::forward<Args>(args)...)));
}
std::string getStringValue() const;
// Return a unique type code for the resolved object
inline qpdf_object_type_e getResolvedTypeCode() const;
// Return a unique type code for the object
qpdf_object_type_e
getTypeCode() const
{
return static_cast<qpdf_object_type_e>(value.index());
}
void
assign_null()
{
value = QPDF_Null();
qpdf = nullptr;
og = QPDFObjGen();
object_description = nullptr;
parsed_offset = -1;
}
void
move_to(std::shared_ptr<QPDFObject>& o, bool destroy)
{
o->value = std::move(value);
o->qpdf = qpdf;
o->og = og;
o->object_description = object_description;
o->parsed_offset = parsed_offset;
if (!destroy) {
value = QPDF_Reference(o);
}
}
void
swapWith(std::shared_ptr<QPDFObject> o)
{
std::swap(value, o->value);
std::swap(qpdf, o->qpdf);
std::swap(object_description, o->object_description);
std::swap(parsed_offset, o->parsed_offset);
}
void
setObjGen(QPDF* a_qpdf, QPDFObjGen a_og)
{
qpdf = a_qpdf;
og = a_og;
}
bool
isUnresolved() const
{
return getTypeCode() == ::ot_unresolved;
}
struct JSON_Descr
{
JSON_Descr(std::shared_ptr<std::string> input, std::string const& object) :
input(input),
object(object)
{
}
std::shared_ptr<std::string> input;
std::string object;
};
struct ChildDescr
{
ChildDescr(
std::shared_ptr<QPDFObject> parent,
std::string_view const& static_descr,
std::string var_descr) :
parent(parent),
static_descr(static_descr),
var_descr(var_descr)
{
}
std::weak_ptr<QPDFObject> parent;
std::string_view const& static_descr;
std::string var_descr;
};
struct ObjStreamDescr
{
ObjStreamDescr(int stream_id, int obj_id) :
stream_id(stream_id),
obj_id(obj_id) {};
int stream_id;
int obj_id;
};
using Description = std::variant<std::string, JSON_Descr, ChildDescr, ObjStreamDescr>;
void
setDescription(
QPDF* qpdf_p, std::shared_ptr<Description>& description, qpdf_offset_t offset = -1)
{
qpdf = qpdf_p;
object_description = description;
setParsedOffset(offset);
}
void
setDefaultDescription(QPDF* a_qpdf, QPDFObjGen const& a_og)
{
qpdf = a_qpdf;
og = a_og;
}
void
setChildDescription(
QPDF* a_qpdf,
std::shared_ptr<QPDFObject> parent,
std::string_view const& static_descr,
std::string var_descr)
{
object_description =
std::make_shared<Description>(ChildDescr(parent, static_descr, var_descr));
qpdf = a_qpdf;
}
std::string getDescription();
bool
hasDescription()
{
return object_description || og.isIndirect();
}
void
setParsedOffset(qpdf_offset_t offset)
{
if (parsed_offset < 0) {
parsed_offset = offset;
}
}
qpdf_offset_t
getParsedOffset()
{
return parsed_offset;
}
QPDF*
getQPDF()
{
return qpdf;
}
QPDFObjGen
getObjGen()
{
return og;
}
private:
friend class QPDF_Stream;
friend class qpdf::BaseHandle;
friend class Disconnect;
typedef std::variant<
std::monostate,
QPDF_Reserved,
QPDF_Null,
QPDF_Bool,
QPDF_Integer,
QPDF_Real,
QPDF_String,
QPDF_Name,
QPDF_Array,
QPDF_Dictionary,
QPDF_Stream,
QPDF_Operator,
QPDF_InlineImage,
QPDF_Unresolved,
QPDF_Destroyed,
QPDF_Reference>
Value;
Value value;
QPDFObject(QPDFObject const&) = delete;
QPDFObject& operator=(QPDFObject const&) = delete;
std::shared_ptr<Description> object_description;
QPDF* qpdf{nullptr};
QPDFObjGen og{};
qpdf_offset_t parsed_offset{-1};
};
#endif // QPDFOBJECT_HH