PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfDictionary.h
1
7#ifndef PDF_DICTIONARY_H
8#define PDF_DICTIONARY_H
9
10#include "PdfDeclarations.h"
11#include "PdfDataContainer.h"
12
13namespace PoDoFo {
14
15class PdfDictionary;
16
20template <typename TObject, typename TMapIterator>
21class PdfDictionaryIndirectIterableBase final : public PdfIndirectIterableBase
22{
23 friend class PdfDictionary;
24
25public:
27
28private:
30
31public:
32 class Iterator final
33 {
35 public:
36 using difference_type = void;
37 using value_type = std::pair<PdfName, TObject*>;
38 using pointer = const value_type*;
39 using reference = const value_type&;
40 using iterator_category = std::forward_iterator_tag;
41 public:
42 Iterator();
43 private:
44 Iterator(TMapIterator&& iterator, PdfIndirectObjectList* objects);
45 public:
46 Iterator(const Iterator&) = default;
47 Iterator& operator=(const Iterator&) = default;
48 bool operator==(const Iterator& rhs) const;
49 bool operator!=(const Iterator& rhs) const;
50 Iterator& operator++();
51 Iterator operator++(int);
52 reference operator*();
53 pointer operator->();
54 private:
55 void resolve();
56 private:
57 TMapIterator m_iterator;
58 PdfIndirectObjectList* m_objects;
59 value_type m_pair;
60 };
61
62public:
63 Iterator begin() const;
64 Iterator end() const;
65
66private:
67 PdfDictionary* m_dict;
68};
69
72
81class PODOFO_API PdfDictionary final : public PdfDataContainer
82{
83 friend class PdfObject;
84 friend class PdfTokenizer;
85 friend class PdfSignature;
86 friend class PdfObjectStream;
87 friend class PdfObjectOutputStream;
88
89public:
93
99
111 PdfDictionary& operator=(const PdfDictionary& rhs);
112 PdfDictionary& operator=(PdfDictionary&& rhs) noexcept;
113
119 bool operator==(const PdfDictionary& rhs) const;
120
124 bool operator!=(const PdfDictionary& rhs) const;
125
128 void Clear();
129
140 PdfObject& AddKey(const PdfName& key, const PdfObject& obj);
141 PdfObject& AddKey(const PdfName& key, PdfObject&& obj);
142
156 void AddKeyIndirect(const PdfName& key, const PdfObject& obj);
157
171 PdfObject& AddKeyIndirectSafe(const PdfName& key, const PdfObject& obj);
172
182 const PdfObject* GetKey(const std::string_view& key) const;
183
195 PdfObject* GetKey(const std::string_view& key);
196
206 const PdfObject* FindKey(const std::string_view& key) const;
207 PdfObject* FindKey(const std::string_view& key);
208 const PdfObject& MustFindKey(const std::string_view& key) const;
209 PdfObject& MustFindKey(const std::string_view& key);
210
221 const PdfObject* FindKeyParent(const std::string_view& key) const;
222 PdfObject* FindKeyParent(const std::string_view& key);
223 const PdfObject& MustFindKeyParent(const std::string_view& key) const;
224 PdfObject& MustFindKeyParent(const std::string_view& key);
225
239 const PdfObject& MustGetKey(const std::string_view& key) const;
240 PdfObject& MustGetKey(const std::string_view& key);
241
242 template <typename T>
243 const typename ObjectAdapter<T>::TRet GetKeyAs(const std::string_view& key) const;
244
245 template <typename T>
246 typename ObjectAdapter<T>::TRet GetKeyAs(const std::string_view& key);
247
248 template <typename T>
249 const typename ObjectAdapter<T>::TRet FindKeyAs(const std::string_view& key) const;
250
251 template <typename T>
252 typename ObjectAdapter<T>::TRet FindKeyAs(const std::string_view& key);
253
254 template <typename T>
255 const typename ObjectAdapter<T>::TRet FindKeyParentAs(const std::string_view& key) const;
256
257 template <typename T>
258 typename ObjectAdapter<T>::TRet FindKeyParentAs(const std::string_view& key);
259
260 template <typename T>
261 const typename ObjectAdapter<T>::TRet GetKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback = { }) const;
262
263 template <typename T>
264 typename ObjectAdapter<T>::TRet GetKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback = { });
265
266 template <typename T>
267 const typename ObjectAdapter<T>::TRet FindKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback = { }) const;
268
269 template <typename T>
270 typename ObjectAdapter<T>::TRet FindKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback = { });
271
272 template <typename T>
273 const typename ObjectAdapter<T>::TRet FindKeyParentAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback = { }) const;
274
275 template <typename T>
276 typename ObjectAdapter<T>::TRet FindKeyParentAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback = { });
277
278 template <typename T>
279 bool TryFindKeyAs(const std::string_view& key, T& value) const;
280
281 template <typename T>
282 bool TryFindKeyAs(const std::string_view& key, T& value);
283
284 template <typename T>
285 bool TryFindKeyParentAs(const std::string_view& key, T& value) const;
286
287 template <typename T>
288 bool TryFindKeyParentAs(const std::string_view& key, T& value);
289
295 bool HasKey(const std::string_view& key) const;
296
308 bool RemoveKey(const std::string_view& key);
309
310 void Write(OutputStream& stream, PdfWriteFlags writeMode,
311 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const override;
312
316 unsigned GetSize() const;
317
318 PdfDictionaryIndirectIterable GetIndirectIterator();
319
320 PdfDictionaryConstIndirectIterable GetIndirectIterator() const;
321
322public:
323 using iterator = PdfNameMap<PdfObject>::iterator;
324 using const_iterator = PdfNameMap<PdfObject>::const_iterator;
325
326public:
327 iterator begin();
328 iterator end();
329 const_iterator begin() const;
330 const_iterator end() const;
331 size_t size() const;
332
333protected:
334 void resetDirty() override;
335 void setChildrenParent() override;
336
337private:
338 // NOTE: It also doesn't dirty set the moved "obj"
339 void AddKeyNoDirtySet(const PdfName& key, PdfObject&& obj);
340 void AddKeyNoDirtySet(const PdfName& key, PdfVariant&& var);
341 void RemoveKeyNoDirtySet(const std::string_view& key);
342 // Append a new "null" object with the given key
343 PdfObject& EmplaceNoDirtySet(const PdfName& key);
344
345private:
346 PdfObject& addKey(const PdfName& key, PdfObject&& obj);
347 PdfObject* getKey(const std::string_view& key) const;
348 PdfObject* findKey(const std::string_view& key) const;
349 PdfObject* findKeyParent(const std::string_view& key) const;
350 void write(OutputStream& stream, PdfWriteFlags writeMode, bool addDelimiters,
351 const PdfStatefulEncrypt* encrypt, charbuff& buffer) const;
352
353private:
354 PdfNameMap<PdfObject> m_Map;
355};
356
357template<typename T>
358const typename ObjectAdapter<T>::TRet PdfDictionary::GetKeyAs(const std::string_view& key) const
359{
360 return ObjectAdapter<T>::Get(MustGetKey(key));
361}
362
363template<typename T>
364typename ObjectAdapter<T>::TRet PdfDictionary::GetKeyAs(const std::string_view& key)
365{
366 return ObjectAdapter<T>::Get(MustGetKey(key));
367}
368
369template<typename T>
370const typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyAs(const std::string_view& key) const
371{
372 return ObjectAdapter<T>::Get(MustFindKey(key));
373}
374
375template<typename T>
376typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyAs(const std::string_view& key)
377{
378 return ObjectAdapter<T>::Get(MustFindKey(key));
379}
380
381template<typename T>
382const typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyParentAs(const std::string_view& key) const
383{
384 return ObjectAdapter<T>::Get(MustFindKeyParent(key));
385}
386
387template<typename T>
388typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyParentAs(const std::string_view& key)
389{
390 return ObjectAdapter<T>::Get(MustFindKeyParent(key));
391}
392
393template<typename T>
394const typename ObjectAdapter<T>::TRet PdfDictionary::GetKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback) const
395{
396 auto obj = getKey(key);
397 if (obj == nullptr)
398 return fallback;
399 else
400 return ObjectAdapter<T>::Get(const_cast<const PdfObject&>(*obj), fallback);
401}
402
403template<typename T>
404typename ObjectAdapter<T>::TRet PdfDictionary::GetKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback)
405{
406 auto obj = getKey(key);
407 if (obj == nullptr)
408 return fallback;
409 else
410 return ObjectAdapter<T>::Get(*obj, fallback);
411}
412
413template<typename T>
414const typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback) const
415{
416 auto obj = findKey(key);
417 if (obj == nullptr)
418 return fallback;
419 else
420 return ObjectAdapter<T>::Get(const_cast<const PdfObject&>(*obj), fallback);
421}
422
423template<typename T>
424typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback)
425{
426 auto obj = findKey(key);
427 if (obj == nullptr)
428 return fallback;
429 else
430 return ObjectAdapter<T>::Get(*obj, fallback);
431}
432
433template<typename T>
434const typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyParentAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback) const
435{
436 auto obj = findKeyParent(key);
437 if (obj == nullptr)
438 return fallback;
439 else
440 return ObjectAdapter<T>::Get(const_cast<const PdfObject&>(*obj), fallback);
441}
442
443template<typename T>
444typename ObjectAdapter<T>::TRet PdfDictionary::FindKeyParentAsSafe(const std::string_view& key, const std::common_type_t<T>& fallback)
445{
446 auto obj = findKeyParent(key);
447 if (obj == nullptr)
448 return fallback;
449 else
450 return ObjectAdapter<T>::Get(*obj, fallback);
451}
452
453template <typename T>
454bool PdfDictionary::TryFindKeyAs(const std::string_view& key, T& value) const
455{
456 auto obj = findKey(key);
457 if (obj != nullptr && ObjectAdapter<T>::TryGet(const_cast<const PdfObject&>(*obj), value))
458 {
459 return true;
460 }
461 else
462 {
463 value = { };
464 return false;
465 }
466}
467
468template<typename T>
469bool PdfDictionary::TryFindKeyAs(const std::string_view& key, T& value)
470{
471 auto obj = findKey(key);
472 if (obj != nullptr && ObjectAdapter<T>::TryGet(*obj, value))
473 {
474 return true;
475 }
476 else
477 {
478 value = { };
479 return false;
480 }
481}
482
483template <typename T>
484bool PdfDictionary::TryFindKeyParentAs(const std::string_view& key, T& value) const
485{
486 auto obj = findKeyParent(key);
487 if (obj != nullptr && ObjectAdapter<T>::TryGet(const_cast<const PdfObject&>(*obj), value))
488 {
489 return true;
490 }
491 else
492 {
493 value = { };
494 return false;
495 }
496}
497
498template<typename T>
499bool PdfDictionary::TryFindKeyParentAs(const std::string_view& key, T& value)
500{
501 auto obj = findKeyParent(key);
502 if (obj != nullptr && ObjectAdapter<T>::TryGet(*obj, value))
503 {
504 return true;
505 }
506 else
507 {
508 value = { };
509 return false;
510 }
511}
512
513template <typename TObject, typename TMapIterator>
514PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::PdfDictionaryIndirectIterableBase()
515 : m_dict(nullptr) { }
516
517template <typename TObject, typename TMapIterator>
518PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::PdfDictionaryIndirectIterableBase(PdfDictionary& dict)
519 : PdfIndirectIterableBase(dict), m_dict(&dict) { }
520
521template <typename TObject, typename TMapIterator>
522typename PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::begin() const
523{
524 if (m_dict == nullptr)
525 return Iterator();
526 else
527 return Iterator(m_dict->begin(), GetObjects());
528}
529
530template <typename TObject, typename TMapIterator>
531typename PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::end() const
532{
533 if (m_dict == nullptr)
534 return Iterator();
535 else
536 return Iterator(m_dict->end(), GetObjects());
537}
538
539template<typename TObject, typename TMapIterator>
540PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::Iterator() : m_objects(nullptr) { }
541
542template<typename TObject, typename TMapIterator>
543PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::Iterator(TMapIterator&& iterator, PdfIndirectObjectList* objects)
544 : m_iterator(std::move(iterator)), m_objects(objects) { }
545
546template<typename TObject, typename TMapIterator>
547bool PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::operator==(const Iterator& rhs) const
548{
549 return m_iterator == rhs.m_iterator;
550}
551
552template<typename TObject, typename TMapIterator>
553bool PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::operator!=(const Iterator& rhs) const
554{
555 return m_iterator != rhs.m_iterator;
556}
557
558template<typename TObject, typename TMapIterator>
559typename PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator& PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::operator++()
560{
561 m_iterator++;
562 return *this;
563}
564
565template<typename TObject, typename TMapIterator>
566typename PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::operator++(int)
567{
568 auto copy = *this;
569 m_iterator++;
570 return copy;
571}
572
573template<typename TObject, typename TMapIterator>
574typename PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::reference PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::operator*()
575{
576 resolve();
577 return m_pair;
578}
579
580template<typename TObject, typename TMapIterator>
581typename PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::pointer PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::operator->()
582{
583 resolve();
584 return &m_pair;
585}
586
587template<typename TObject, typename TMapIterator>
588void PdfDictionaryIndirectIterableBase<TObject, TMapIterator>::Iterator::resolve()
589{
590 TObject& robj = m_iterator->second;
591 TObject* indirectobj;
592 PdfReference ref;
593 if (m_objects != nullptr
594 && robj.TryGetReference(ref)
595 && ref.IsIndirect()
596 && (indirectobj = GetObject(*m_objects, ref)) != nullptr)
597 {
598 m_pair = value_type(m_iterator->first, indirectobj);
599 }
600 else
601 {
602 m_pair = value_type(m_iterator->first, &robj);
603 }
604}
605
606}
607
608#endif // PDF_DICTIONARY_H
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
A PdfDataProvider object with a PdfObject owner, specialized in holding objects.
Definition PdfDataContainer.h:22
Helper class to iterate through indirect objects.
Definition PdfDictionary.h:22
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
const PdfObject * FindKeyParent(const std::string_view &key) const
Get the keys value out of the dictionary.
const PdfObject * GetKey(const std::string_view &key) const
Get the key's value out of the dictionary.
const PdfObject * FindKey(const std::string_view &key) const
Get the keys value out of the dictionary.
PdfObject * GetKey(const std::string_view &key)
Get the key's value out of the dictionary.
const PdfObject & MustGetKey(const std::string_view &key) const
Get the key's value out of the dictionary.
A list of PdfObjects that constitutes the indirect object list of the document The PdfParser will rea...
Definition PdfIndirectObjectList.h:30
This class represents a PdfName.
Definition PdfName.h:24
A PDF stream can be appended to any PdfObject and can contain arbitrary data.
Definition PdfObjectStream.h:87
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
A simple tokenizer for PDF files and PDF content streams.
Definition PdfTokenizer.h:36
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16