PoDoFo 1.1.0
Loading...
Searching...
No Matches
PdfEncoding.h
1
7#ifndef PDF_ENCODING_H
8#define PDF_ENCODING_H
9
10#include "PdfEncodingMap.h"
11#include "PdfString.h"
12#include "PdfObject.h"
13#include "PdfCIDToGIDMap.h"
14
15namespace PoDoFo
16{
17 class PdfFont;
18 class PdfEncoding;
19 class PdfFontSimple;
20
24 class PODOFO_API PdfStringScanContext
25 {
26 friend class PdfEncoding;
27
28 private:
29 PdfStringScanContext(const std::string_view& encodedstr, const PdfEncoding& encoding);
30
31 public:
32 bool IsEndOfString() const;
33
37 bool TryScan(PdfCID& cid, std::string& utf8str, CodePointSpan& codepoints);
38
39 bool TryScan(PdfCID& cid, std::string& utf8str, std::vector<unsigned>& positions, CodePointSpan& codepoints);
40
41 private:
42 std::string_view::iterator m_it;
43 std::string_view::iterator m_end;
44 const PdfEncodingMap* m_encoding;
45 PdfEncodingLimits m_limits;
46 const PdfEncodingMap* m_toUnicode;
47 };
48
57 class PODOFO_API PdfEncoding final
58 {
59 friend class PdfEncodingFactory;
60 friend class PdfFont;
61 friend class PdfFontCID;
62 friend class PdfFontCIDTrueType;
63 friend class PdfFontSimple;
64
65 public:
71 PdfEncoding(const PdfEncoding&) = default;
72
73 private:
76 PdfEncoding(unsigned id, bool isObjectLoaded, const PdfEncodingLimits& limits, PdfFont* font,
78 PdfCIDToGIDMapConstPtr&& cidToGidMap);
79
82 static PdfEncoding Create(const PdfEncodingLimits& parsedLimits, PdfEncodingMapConstPtr&& encoding,
83 PdfEncodingMapConstPtr&& toUnicode, PdfCIDToGIDMapConstPtr&& cidToGidMap);
84
88
91 static std::unique_ptr<PdfEncoding> CreateSchim(const PdfEncoding& encoding, PdfFont& font);
92
96 static std::unique_ptr<PdfEncoding> CreateDynamicEncoding(const std::shared_ptr<PdfCharCodeMap>& cidMap,
97 const std::shared_ptr<PdfCharCodeMap>& toUnicodeMap, PdfFont& font);
98
99 public:
103 std::string ConvertToUtf8(const PdfString& encodedStr) const;
104
108 bool TryConvertToUtf8(const PdfString& encodedStr, std::string& str) const;
109
113 charbuff ConvertToEncoded(const std::string_view& str) const;
114
115 bool TryConvertToEncoded(const std::string_view& str, charbuff& encoded) const;
116
120 std::vector<PdfCID> ConvertToCIDs(const PdfString& encodedStr) const;
121
125 bool TryConvertToCIDs(const PdfString& encodedStr, std::vector<PdfCID>& cids) const;
126
132 char32_t GetCodePoint(const PdfCharCode& codeUnit) const;
133
140 char32_t GetCodePoint(unsigned charCode) const;
141
142 PdfStringScanContext StartStringScan(const PdfString& encodedStr);
143
144 public:
148 const PdfCharCode& GetFirstChar() const;
149
153 const PdfCharCode& GetLastChar() const;
154
157 bool IsNull() const;
158
161 bool HasCIDMapping() const;
162
166 bool IsSimpleEncoding() const;
167
170 bool HasParsedLimits() const;
171
174 bool IsDynamicEncoding() const;
175
179 unsigned GetId() const { return m_Id; }
180
184 bool IsObjectLoaded() const { return m_IsObjectLoaded; }
185
190 const PdfEncodingLimits& GetLimits() const;
191
192 bool HasValidToUnicodeMap() const;
193
196 const PdfEncodingMap& GetToUnicodeMap() const;
197
203 bool GetToUnicodeMapSafe(const PdfEncodingMap*& toUnicode) const;
204
210 const PdfEncodingMap& GetToUnicodeMapSafe() const;
211
212 const PdfEncodingMap& GetEncodingMap() const { return *m_Encoding; }
213
214 PdfEncodingMapConstPtr GetEncodingMapPtr() const { return m_Encoding; }
215
216 PdfEncodingMapConstPtr GetToUnicodeMapPtr() const;
217
218 public:
219 PdfEncoding& operator=(const PdfEncoding&) = default;
220
221 private:
222 // These methods will be called by PdfFont
223 void ExportToFont(PdfFont& font, const PdfCIDSystemInfo& cidInfo) const;
224 void ExportToFont(PdfFont& font) const;
225 bool TryGetCIDId(const PdfCharCode& codeUnit, unsigned& cid) const;
226 const PdfCIDToGIDMap* GetCIDToGIDMap() const { return m_CIDToGIDMap.get(); }
227
228 static unsigned GetNextId();
229
230 private:
231 void exportToFont(PdfFont& font, const PdfCIDSystemInfo* cidInfo) const;
232 bool tryExportEncodingTo(PdfDictionary& dictionary, bool wantCidMapping) const;
233 bool tryConvertEncodedToUtf8(const std::string_view& encoded, std::string& str) const;
234 bool tryConvertEncodedToCIDs(const std::string_view& encoded, std::vector<PdfCID>& cids) const;
235 void writeCIDMapping(PdfObject& cmapObj, const PdfFont& font, const PdfCIDSystemInfo& info) const;
236 void writeToUnicodeCMap(PdfObject& cmapObj, const PdfFont& font) const;
237 bool tryGetCharCode(PdfFont& font, unsigned gid, const unicodeview& codePoints, PdfCharCode& unit) const;
238
239 private:
240 unsigned m_Id;
241 bool m_IsObjectLoaded;
242 PdfEncodingLimits m_ParsedLimits;
243 PdfFont* m_Font;
244 PdfEncodingMapConstPtr m_Encoding;
245 PdfEncodingMapConstPtr m_ToUnicode;
246 PdfCIDToGIDMapConstPtr m_CIDToGIDMap;
247 };
248}
249
250#endif // PDF_ENCODING_H
A memory owning immutable block of code points, optimized for small segments as up to 3 elements can ...
Definition PdfEncodingCommon.h:141
This factory creates a PdfEncoding from an existing object in the PDF.
Definition PdfEncodingFactory.h:20
A PdfEncodingMap is a low level interface to convert between utf8 and encoded strings in and to deter...
Definition PdfEncodingMap.h:32
A PdfEncoding is in PdfFont to transform a text string into a representation so that it can be displa...
Definition PdfEncoding.h:58
bool IsObjectLoaded() const
True if the encoding is constructed from object loaded information.
Definition PdfEncoding.h:184
unsigned GetId() const
Return an Id to be used in hashed containers.
Definition PdfEncoding.h:179
A PdfFont that represents a CID-keyed font that has a TrueType/OpenType font backend (aka "CIDFontTyp...
Definition PdfFontCIDTrueType.h:17
A PdfFont that represents a CID-keyed font.
Definition PdfFontCID.h:17
This is a common base class for simple, non CID-keyed fonts like Type1, TrueType and Type3.
Definition PdfFontSimple.h:20
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:45
A PDF string context to iteratively scan a string and collect both CID and unicode codepoints.
Definition PdfEncoding.h:25
A string that can be written to a PDF document.
Definition PdfString.h:24
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
std::shared_ptr< const PdfEncodingMap > PdfToUnicodeMapConstPtr
Convenience alias for a const /ToUnicode CMap entry shared ptr.
Definition PdfEncodingMap.h:363
@ Create
Create a new file or truncate existing one for writing/reading.
std::shared_ptr< const PdfEncodingMap > PdfEncodingMapConstPtr
Convenience typedef for a const /Encoding map entry shared ptr.
Definition PdfEncodingMap.h:355
Represent a CID (Character ID) with full code unit information.
Definition PdfEncodingCommon.h:52
A character code unit.
Definition PdfEncodingCommon.h:20