PoDoFo  1.0.0-dev
PdfDifferenceEncoding.h
1 
7 #ifndef PDF_DIFFERENCE_ENCODING_H
8 #define PDF_DIFFERENCE_ENCODING_H
9 
10 #include "PdfEncodingMap.h"
11 #include "PdfArray.h"
12 
13 namespace PoDoFo {
14 
15 class PdfFontMetrics;
16 
20 class PODOFO_API PdfDifferenceList final
21 {
22  struct Difference
23  {
24  unsigned char Code = 0;
25  PdfName Name;
26  char32_t MappedCodePoint = U'\0';
27  };
28 
29  using List = std::vector<Difference>;
30  using iterator = std::vector<Difference>::iterator;
31  using const_iterator = std::vector<Difference>::const_iterator;
32 
33 public:
37 
38  PdfDifferenceList(const PdfDifferenceList& rhs) = default;
39  PdfDifferenceList& operator=(const PdfDifferenceList& rhs) = default;
40 
49  void AddDifference(unsigned char code, char32_t codePoint);
50 
57  void AddDifference(unsigned char code, const PdfName& name, bool explicitNames = false);
58 
66  bool TryGetMappedName(unsigned char code, const PdfName*& name) const;
67  bool TryGetMappedName(unsigned char code, const PdfName*& name, char32_t& codePoint) const;
68 
73  void ToArray(PdfArray& arr) const;
74 
81  size_t GetCount() const;
82 
83  const_iterator begin() const { return m_differences.begin(); }
84 
85  const_iterator end() const { return m_differences.end(); }
86 
87 private:
88  void addDifference(unsigned char code, char32_t codePoint, const PdfName& name);
89  bool contains(unsigned char code, const PdfName*& name, char32_t& codePoint);
90 
91  struct DifferenceComparatorPredicate
92  {
93  public:
94  inline bool operator()(const Difference& diff1, const Difference& diff2) const
95  {
96  return diff1.Code < diff2.Code;
97  }
98  };
99 
100  List m_differences;
101 };
102 
107 class PODOFO_API PdfDifferenceEncoding final : public PdfEncodingMapOneByte
108 {
109 public:
116  PdfDifferenceEncoding(const PdfEncodingMapConstPtr& baseEncoding,
117  const PdfDifferenceList& differences);
118 
119 public:
125  static bool TryCreateFromObject(const PdfObject& obj, const PdfFontMetrics& metrics,
126  std::unique_ptr<PdfDifferenceEncoding>& encoding);
127 
135  static std::unique_ptr<PdfDifferenceEncoding> CreateFromObject(const PdfObject& obj, const PdfFontMetrics& metrics);
136 
142  static char32_t NameToCodePoint(const std::string_view& name);
143 
149  static PdfName CodePointToName(char32_t codePoint);
150 
157  inline const PdfDifferenceList& GetDifferences() const { return m_differences; }
158 
159 protected:
160  void getExportObject(PdfIndirectObjectList& objects, PdfName& name, PdfObject*& obj) const override;
161  bool tryGetCharCode(char32_t codePoint, PdfCharCode& codeUnit) const override;
162  bool tryGetCodePoints(const PdfCharCode& codeUnit, const unsigned* cidId, CodePointSpan& codePoints) const override;
163 
164 private:
165  void buildReverseMap();
166 
167 private:
168  PdfEncodingMapConstPtr m_baseEncoding;
169  PdfDifferenceList m_differences;
170  bool m_reverseMapBuilt;
171  std::unordered_map<char32_t, unsigned char> m_reverseMap;
172 };
173 
174 };
175 
176 #endif // PDF_DIFFERENCE_ENCODING_H
A memory owning immutable block of code points, optimized for small segments as up to 3 elements can ...
Definition: PdfCharCodeMap.h:29
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition: PdfArray.h:81
PdfDifferenceEncoding is an encoding, which is based on either the fonts encoding or a predefined enc...
Definition: PdfDifferenceEncoding.h:108
const PdfDifferenceList & GetDifferences() const
Get read-only access to the object containing the actual differences.
Definition: PdfDifferenceEncoding.h:157
A helper class for PdfDifferenceEncoding that can be used to create a differences array.
Definition: PdfDifferenceEncoding.h:21
PdfEncodingMap used by encodings like PdfBuiltInEncoding or PdfDifferenceEncoding that can define all...
Definition: PdfEncodingMap.h:247
This abstract class provides access to font metrics information.
Definition: PdfFontMetrics.h:33
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
This class represents a PDF indirect Object in memory.
Definition: PdfObject.h:35
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
std::shared_ptr< const PdfEncodingMap > PdfEncodingMapConstPtr
Convenience typedef for a const /Encoding map entry shared ptr.
Definition: PdfEncodingMap.h:343
A character code unit.
Definition: PdfEncodingCommon.h:20