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
14namespace PoDoFo
15{
16 class PdfFont;
17 class PdfEncoding;
18 class PdfFontSimple;
19
23 class PODOFO_API PdfStringScanContext
24 {
25 friend class PdfEncoding;
26
27 private:
28 PdfStringScanContext(const std::string_view& encodedstr, const PdfEncoding& encoding);
29
30 public:
31 bool IsEndOfString() const;
32
36 bool TryScan(PdfCID& cid, std::string& utf8str, CodePointSpan& codepoints);
37
38 private:
39 std::string_view::iterator m_it;
40 std::string_view::iterator m_end;
41 const PdfEncodingMap* m_encoding;
42 PdfEncodingLimits m_limits;
43 const PdfEncodingMap* m_toUnicode;
44 };
45
54 class PODOFO_API PdfEncoding final
55 {
56 friend class PdfEncodingFactory;
57 friend class PdfFont;
58 friend class PdfFontCID;
59 friend class PdfFontCIDTrueType;
60 friend class PdfFontSimple;
61
62 public:
68 PdfEncoding(const PdfEncoding&) = default;
69
70 private:
72 const PdfEncodingMapConstPtr& toUnicode = nullptr);
73 PdfEncoding(unsigned id, bool isObjectLoaded, const PdfEncodingLimits& limits, PdfFont* font,
75
78 static PdfEncoding Create(const PdfEncodingLimits& parsedLimits, const PdfEncodingMapConstPtr& encoding,
80
83 static std::unique_ptr<PdfEncoding> CreateSchim(const PdfEncoding& encoding, PdfFont& font);
84
88 static std::unique_ptr<PdfEncoding> CreateDynamicEncoding(const std::shared_ptr<PdfCharCodeMap>& cidMap,
89 const std::shared_ptr<PdfCharCodeMap>& toUnicodeMap, PdfFont& font);
90
91 public:
95 std::string ConvertToUtf8(const PdfString& encodedStr) const;
96
100 bool TryConvertToUtf8(const PdfString& encodedStr, std::string& str) const;
101
105 charbuff ConvertToEncoded(const std::string_view& str) const;
106
107 bool TryConvertToEncoded(const std::string_view& str, charbuff& encoded) const;
108
112 std::vector<PdfCID> ConvertToCIDs(const PdfString& encodedStr) const;
113
117 bool TryConvertToCIDs(const PdfString& encodedStr, std::vector<PdfCID>& cids) const;
118
124 char32_t GetCodePoint(const PdfCharCode& codeUnit) const;
125
132 char32_t GetCodePoint(unsigned charCode) const;
133
134 PdfStringScanContext StartStringScan(const PdfString& encodedStr);
135
136 public:
140 const PdfCharCode& GetFirstChar() const;
141
145 const PdfCharCode& GetLastChar() const;
146
149 bool IsNull() const;
150
153 bool HasCIDMapping() const;
154
158 bool IsSimpleEncoding() const;
159
162 bool HasParsedLimits() const;
163
166 bool IsDynamicEncoding() const;
167
171 unsigned GetId() const { return m_Id; }
172
176 bool IsObjectLoaded() const { return m_IsObjectLoaded; }
177
182 const PdfEncodingLimits& GetLimits() const;
183
184 bool HasValidToUnicodeMap() const;
185
188 const PdfEncodingMap& GetToUnicodeMap() const;
189
195 bool GetToUnicodeMapSafe(const PdfEncodingMap*& toUnicode) const;
196
202 const PdfEncodingMap& GetToUnicodeMapSafe() const;
203
204 inline const PdfEncodingMap& GetEncodingMap() const { return *m_Encoding; }
205
206 inline const PdfEncodingMapConstPtr GetEncodingMapPtr() const { return m_Encoding; }
207
208 const PdfEncodingMapConstPtr GetToUnicodeMapPtr() const;
209
210 public:
211 PdfEncoding& operator=(const PdfEncoding&) = default;
212
213 private:
214 // These methods will be called by PdfFont
215 void ExportToFont(PdfFont& font, const PdfCIDSystemInfo& cidInfo) const;
216 void ExportToFont(PdfFont& font) const;
217 bool TryGetCIDId(const PdfCharCode& codeUnit, unsigned& cid) const;
218
219 static unsigned GetNextId();
220
221 private:
222 void exportToFont(PdfFont& font, const PdfCIDSystemInfo* cidInfo) const;
223 bool tryExportEncodingTo(PdfDictionary& dictionary, bool wantCidMapping) const;
224 bool tryConvertEncodedToUtf8(const std::string_view& encoded, std::string& str) const;
225 bool tryConvertEncodedToCIDs(const std::string_view& encoded, std::vector<PdfCID>& cids) const;
226 void writeCIDMapping(PdfObject& cmapObj, const PdfFont& font, const PdfCIDSystemInfo& info) const;
227 void writeToUnicodeCMap(PdfObject& cmapObj, const PdfFont& font) const;
228 bool tryGetCharCode(PdfFont& font, unsigned gid, const unicodeview& codePoints, PdfCharCode& unit) const;
229
230 private:
231 unsigned m_Id;
232 bool m_IsObjectLoaded;
233 PdfEncodingLimits m_ParsedLimits;
234 PdfFont* m_Font;
235 PdfEncodingMapConstPtr m_Encoding;
236 PdfEncodingMapConstPtr m_ToUnicode;
237 };
238}
239
240#endif // PDF_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 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:28
A PdfEncoding is in PdfFont to transform a text string into a representation so that it can be displa...
Definition PdfEncoding.h:55
bool IsObjectLoaded() const
True if the encoding is constructed from object loaded information.
Definition PdfEncoding.h:176
unsigned GetId() const
Return an Id to be used in hashed containers.
Definition PdfEncoding.h:171
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:24
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:353
@ 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:345
Represent a CID (Character ID) with full code unit information.
Definition PdfEncodingCommon.h:52
A character code unit.
Definition PdfEncodingCommon.h:20