QPDF_Array.cc
9.71 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
#include <qpdf/QPDFObjectHandle_private.hh>
#include <qpdf/QTC.hh>
using namespace std::literals;
using namespace qpdf;
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
inline void
Array::checkOwnership(QPDFObjectHandle const& item) const
{
if (!item) {
throw std::logic_error("Attempting to add an uninitialized object to a QPDF_Array.");
}
if (qpdf() && item.qpdf() && qpdf() != item.qpdf()) {
throw std::logic_error(
"Attempting to add an object from a different QPDF. Use "
"QPDF::copyForeignObject to add objects from another file.");
}
}
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle>&& v, bool sparse)
{
if (sparse) {
sp = std::make_unique<Sparse>();
for (auto& item: v) {
if (item.raw_type_code() != ::ot_null || item.indirect()) {
sp->elements[sp->size] = std::move(item);
}
++sp->size;
}
} else {
elements = std::move(v);
}
}
QPDF_Array*
Array::array() const
{
if (auto a = as<QPDF_Array>()) {
return a;
}
throw std::runtime_error("Expected an array but found a non-array object");
return nullptr; // unreachable
}
QPDFObjectHandle
Array::null() const
{
return null_oh;
}
int
Array::size() const
{
auto a = array();
return a->sp ? a->sp->size : int(a->elements.size());
}
std::pair<bool, QPDFObjectHandle>
Array::at(int n) const
{
auto a = array();
if (n < 0 || n >= size()) {
return {false, {}};
}
if (!a->sp) {
return {true, a->elements[size_t(n)]};
}
auto const& iter = a->sp->elements.find(n);
return {true, iter == a->sp->elements.end() ? null() : iter->second};
}
std::vector<QPDFObjectHandle>
Array::getAsVector() const
{
auto a = array();
if (a->sp) {
std::vector<QPDFObjectHandle> v;
v.reserve(size_t(size()));
for (auto const& item: a->sp->elements) {
v.resize(size_t(item.first), null_oh);
v.emplace_back(item.second);
}
v.resize(size_t(size()), null_oh);
return v;
} else {
return a->elements;
}
}
bool
Array::setAt(int at, QPDFObjectHandle const& oh)
{
if (at < 0 || at >= size()) {
return false;
}
auto a = array();
checkOwnership(oh);
if (a->sp) {
a->sp->elements[at] = oh;
} else {
a->elements[size_t(at)] = oh;
}
return true;
}
void
Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
{
auto a = array();
a->elements.resize(0);
a->elements.reserve(v.size());
for (auto const& item: v) {
checkOwnership(item);
a->elements.emplace_back(item);
}
}
bool
Array::insert(int at, QPDFObjectHandle const& item)
{
auto a = array();
int sz = size();
if (at < 0 || at > sz) {
// As special case, also allow insert beyond the end
return false;
} else if (at == sz) {
push_back(item);
} else {
checkOwnership(item);
if (a->sp) {
auto iter = a->sp->elements.crbegin();
while (iter != a->sp->elements.crend()) {
auto key = (iter++)->first;
if (key >= at) {
auto nh = a->sp->elements.extract(key);
++nh.key();
a->sp->elements.insert(std::move(nh));
} else {
break;
}
}
a->sp->elements[at] = item.getObj();
++a->sp->size;
} else {
a->elements.insert(a->elements.cbegin() + at, item.getObj());
}
}
return true;
}
void
Array::push_back(QPDFObjectHandle const& item)
{
auto a = array();
checkOwnership(item);
if (a->sp) {
a->sp->elements[(a->sp->size)++] = item;
} else {
a->elements.emplace_back(item);
}
}
bool
Array::erase(int at)
{
auto a = array();
if (at < 0 || at >= size()) {
return false;
}
if (a->sp) {
auto end = a->sp->elements.end();
if (auto iter = a->sp->elements.lower_bound(at); iter != end) {
if (iter->first == at) {
iter++;
a->sp->elements.erase(at);
}
while (iter != end) {
auto nh = a->sp->elements.extract(iter++);
--nh.key();
a->sp->elements.insert(std::move(nh));
}
}
--(a->sp->size);
} else {
a->elements.erase(a->elements.cbegin() + at);
}
return true;
}
int
QPDFObjectHandle::getArrayNItems() const
{
if (auto array = as_array(strict)) {
return array.size();
}
typeWarning("array", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle array treating as empty");
return 0;
}
QPDFObjectHandle
QPDFObjectHandle::getArrayItem(int n) const
{
if (auto array = as_array(strict)) {
if (auto const [success, oh] = array.at(n); success) {
return oh;
} else {
objectWarning("returning null for out of bounds array access");
QTC::TC("qpdf", "QPDFObjectHandle array bounds");
}
} else {
typeWarning("array", "returning null");
QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
}
static auto constexpr msg = " -> null returned from invalid array access"sv;
return QPDF_Null::create(obj, msg, "");
}
bool
QPDFObjectHandle::isRectangle() const
{
if (auto array = as_array(strict)) {
for (int i = 0; i < 4; ++i) {
if (auto item = array.at(i).second; !item.isNumber()) {
return false;
}
}
return array.size() == 4;
}
return false;
}
bool
QPDFObjectHandle::isMatrix() const
{
if (auto array = as_array(strict)) {
for (int i = 0; i < 6; ++i) {
if (auto item = array.at(i).second; !item.isNumber()) {
return false;
}
}
return array.size() == 6;
}
return false;
}
QPDFObjectHandle::Rectangle
QPDFObjectHandle::getArrayAsRectangle() const
{
if (auto array = as_array(strict)) {
if (array.size() != 4) {
return {};
}
double items[4];
for (int i = 0; i < 4; ++i) {
if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
return {};
}
}
return {
std::min(items[0], items[2]),
std::min(items[1], items[3]),
std::max(items[0], items[2]),
std::max(items[1], items[3])};
}
return {};
}
QPDFObjectHandle::Matrix
QPDFObjectHandle::getArrayAsMatrix() const
{
if (auto array = as_array(strict)) {
if (array.size() != 6) {
return {};
}
double items[6];
for (int i = 0; i < 6; ++i) {
if (auto item = array.at(i).second; !item.getValueAsNumber(items[i])) {
return {};
}
}
return {items[0], items[1], items[2], items[3], items[4], items[5]};
}
return {};
}
std::vector<QPDFObjectHandle>
QPDFObjectHandle::getArrayAsVector() const
{
if (auto array = as_array(strict)) {
return array.getAsVector();
}
typeWarning("array", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
return {};
}
void
QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
{
if (auto array = as_array(strict)) {
if (!array.setAt(n, item)) {
objectWarning("ignoring attempt to set out of bounds array item");
QTC::TC("qpdf", "QPDFObjectHandle set array bounds");
}
} else {
typeWarning("array", "ignoring attempt to set item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
}
}
void
QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
{
if (auto array = as_array(strict)) {
array.setFromVector(items);
} else {
typeWarning("array", "ignoring attempt to replace items");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items");
}
}
void
QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
{
if (auto array = as_array(strict)) {
if (!array.insert(at, item)) {
objectWarning("ignoring attempt to insert out of bounds array item");
QTC::TC("qpdf", "QPDFObjectHandle insert array bounds");
}
} else {
typeWarning("array", "ignoring attempt to insert item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
}
}
QPDFObjectHandle
QPDFObjectHandle::insertItemAndGetNew(int at, QPDFObjectHandle const& item)
{
insertItem(at, item);
return item;
}
void
QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)
{
if (auto array = as_array(strict)) {
array.push_back(item);
} else {
typeWarning("array", "ignoring attempt to append item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
}
}
QPDFObjectHandle
QPDFObjectHandle::appendItemAndGetNew(QPDFObjectHandle const& item)
{
appendItem(item);
return item;
}
void
QPDFObjectHandle::eraseItem(int at)
{
if (auto array = as_array(strict)) {
if (!array.erase(at)) {
objectWarning("ignoring attempt to erase out of bounds array item");
QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
}
} else {
typeWarning("array", "ignoring attempt to erase item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
}
}
QPDFObjectHandle
QPDFObjectHandle::eraseItemAndGetOld(int at)
{
auto array = as_array(strict);
auto result = (array && at < array.size() && at >= 0) ? array.at(at).second : newNull();
eraseItem(at);
return result;
}