PoDoFo 1.0.0-dev
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 private:
40 std::string_view::iterator m_it;
41 std::string_view::iterator m_end;
42 const PdfEncodingMap* m_encoding;
43 PdfEncodingLimits m_limits;
44 const PdfEncodingMap* m_toUnicode;
45 };
46
55 class PODOFO_API PdfEncoding final
56 {
57 friend class PdfEncodingFactory;
58 friend class PdfFont;
59 friend class PdfFontCID;
60 friend class PdfFontCIDTrueType;
61 friend class PdfFontSimple;
62
63 public:
69 PdfEncoding(const PdfEncoding&) = default;
70
71 private:
74 PdfEncoding(unsigned id, bool isObjectLoaded, const PdfEncodingLimits& limits, PdfFont* font,
76 PdfCIDToGIDMapConstPtr&& cidToGidMap);
77
80 static PdfEncoding Create(const PdfEncodingLimits& parsedLimits, PdfEncodingMapConstPtr&& encoding,
81 PdfEncodingMapConstPtr&& toUnicode, PdfCIDToGIDMapConstPtr&& cidToGidMap);
82
86
89 static std::unique_ptr<PdfEncoding> CreateSchim(const PdfEncoding& encoding, PdfFont& font);
90
94 static std::unique_ptr<PdfEncoding> CreateDynamicEncoding(const std::shared_ptr<PdfCharCodeMap>& cidMap,
95 const std::shared_ptr<PdfCharCodeMap>& toUnicodeMap, PdfFont& font);
96
97 public:
101 std::string ConvertToUtf8(const PdfString& encodedStr) const;
102
106 bool TryConvertToUtf8(const PdfString& encodedStr, std::string& str) const;
107
111 charbuff ConvertToEncoded(const std::string_view& str) const;
112
113 bool TryConvertToEncoded(const std::string_view& str, charbuff& encoded) const;
114
118 std::vector<PdfCID> ConvertToCIDs(const PdfString& encodedStr) const;
119
123 bool TryConvertToCIDs(const PdfString& encodedStr, std::vector<PdfCID>& cids) const;
124
130 char32_t GetCodePoint(const PdfCharCode& codeUnit) const;
131
138 char32_t GetCodePoint(unsigned charCode) const;
139
140 PdfStringScanContext StartStringScan(const PdfString& encodedStr);
141
142 public:
146 const PdfCharCode& GetFirstChar() const;
147
151 const PdfCharCode& GetLastChar() const;
152
155 bool IsNull() const;
156
159 bool HasCIDMapping() const;
160
164 bool IsSimpleEncoding() const;
165
168 bool HasParsedLimits() const;
169
172 bool IsDynamicEncoding() const;
173
177 unsigned GetId() const { return m_Id; }
178
182 bool IsObjectLoaded() const { return m_IsObjectLoaded; }
183
188 const PdfEncodingLimits& GetLimits() const;
189
190 bool HasValidToUnicodeMap() const;
191
194 const PdfEncodingMap& GetToUnicodeMap() const;
195
201 bool GetToUnicodeMapSafe(const PdfEncodingMap*& toUnicode) const;
202
208 const PdfEncodingMap& GetToUnicodeMapSafe() const;
209
210 const PdfEncodingMap& GetEncodingMap() const { return *m_Encoding; }
211
212 PdfEncodingMapConstPtr GetEncodingMapPtr() const { return m_Encoding; }
213
214 PdfEncodingMapConstPtr GetToUnicodeMapPtr() const;
215
216 public:
217 PdfEncoding& operator=(const PdfEncoding&) = default;
218
219 private:
220 // These methods will be called by PdfFont
221 void ExportToFont(PdfFont& font, const PdfCIDSystemInfo& cidInfo) const;
222 void ExportToFont(PdfFont& font) const;
223 bool TryGetCIDId(const PdfCharCode& codeUnit, unsigned& cid) const;
224 const PdfCIDToGIDMap* GetCIDToGIDMap() const { return m_CIDToGIDMap.get(); }
225
226 static unsigned GetNextId();
227
228 private:
229 void exportToFont(PdfFont& font, const PdfCIDSystemInfo* cidInfo) const;
230 bool tryExportEncodingTo(PdfDictionary& dictionary, bool wantCidMapping) const;
231 bool tryConvertEncodedToUtf8(const std::string_view& encoded, std::string& str) const;
232 bool tryConvertEncodedToCIDs(const std::string_view& encoded, std::vector<PdfCID>& cids) const;
233 void writeCIDMapping(PdfObject& cmapObj, const PdfFont& font, const PdfCIDSystemInfo& info) const;
234 void writeToUnicodeCMap(PdfObject& cmapObj, const PdfFont& font) const;
235 bool tryGetCharCode(PdfFont& font, unsigned gid, const unicodeview& codePoints, PdfCharCode& unit) const;
236
237 private:
238 unsigned m_Id;
239 bool m_IsObjectLoaded;
240 PdfEncodingLimits m_ParsedLimits;
241 PdfFont* m_Font;
242 PdfEncodingMapConstPtr m_Encoding;
243 PdfEncodingMapConstPtr m_ToUnicode;
244 PdfCIDToGIDMapConstPtr m_CIDToGIDMap;
245 };
246}
247
248#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:56
bool IsObjectLoaded() const
True if the encoding is constructed from object loaded information.
Definition PdfEncoding.h:182
unsigned GetId() const
Return an Id to be used in hashed containers.
Definition PdfEncoding.h:177
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