PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfCharCodeMap.h
1// SPDX-FileCopyrightText: 2022 Francesco Pretto <ceztko@gmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
3
4#ifndef PDF_CHAR_CODE_MAP_H
5#define PDF_CHAR_CODE_MAP_H
6
7#include <atomic>
8#include "PdfDeclarations.h"
9#include "PdfEncodingCommon.h"
10
11namespace PoDoFo
12{
13 struct CodePointMapNode;
14
15 struct PODOFO_API CodeUnitRange final
16 {
17 PdfCharCode SrcCodeLo;
18 unsigned Size = 0;
19 CodePointSpan DstCodeLo;
20
21 CodeUnitRange();
22
23 CodeUnitRange(PdfCharCode srcCodeLo, unsigned size, CodePointSpan dstCodeLo);
24
25 PdfCharCode GetSrcCodeHi() const;
26 };
27
28 struct PODOFO_API CodeUnitRangeInequality
29 {
30 using is_transparent = std::true_type;
31
32 bool operator()(const CodeUnitRange& lhs, const PdfCharCode& rhs) const
33 {
34 return lhs.SrcCodeLo < rhs;
35 }
36 bool operator()(const PdfCharCode& lhs, const CodeUnitRange& rhs) const
37 {
38 return lhs < rhs.SrcCodeLo;
39 }
40 bool operator()(const CodeUnitRange& lhs, const CodeUnitRange& rhs) const
41 {
42 return lhs.SrcCodeLo < rhs.SrcCodeLo;
43 }
44 };
45
46 using CodeUnitRanges = std::set<CodeUnitRange, CodeUnitRangeInequality>;
47
52 struct PODOFO_API CodeSpaceRange final
53 {
54 CodeSpaceRange();
55 CodeSpaceRange(unsigned codeLo, unsigned codeHi, unsigned char codeSpaceSize);
56
57 unsigned CodeLo;
58 unsigned CodeHi;
59 unsigned char CodeSpaceSize;
60
61 PdfCharCode GetSrcCodeLo() const;
62 PdfCharCode GetSrcCodeHi() const;
63 };
64
72 class PODOFO_API PdfCharCodeMap final
73 {
74 PODOFO_PRIVATE_FRIEND(class PdfCMapEncodingFactory);
75
76 public:
77 PdfCharCodeMap();
78
79 PdfCharCodeMap(PdfCharCodeMap&& map) noexcept;
80
81 ~PdfCharCodeMap();
82
83 private:
84 PdfCharCodeMap(CodeUnitMap&& mapping, CodeUnitRanges&& ranges, const PdfEncodingLimits& limits);
85
86 public:
90 void PushMapping(const PdfCharCode& codeUnit, const codepointview& codePoints);
91
93 void PushMapping(const PdfCharCode& codeUnit, codepoint codePoint);
94
98 void PushRange(const PdfCharCode& srcCodeLo, unsigned size, codepoint dstCodeLo);
99
103 void PushRange(const PdfCharCode& srcCodeLo, unsigned size, const codepointview& dstCodeLo);
104
106 bool TryGetCodePoints(const PdfCharCode& codeUnit, CodePointSpan& codePoints) const;
107
111 bool TryGetNextCharCode(std::string_view::iterator& it,
112 const std::string_view::iterator& end, PdfCharCode& code) const;
113
116 bool TryGetCharCode(const codepointview& codePoints, PdfCharCode& code) const;
117
119 bool TryGetCharCode(codepoint codePoint, PdfCharCode& code) const;
120
121 PdfCharCodeMap& operator=(PdfCharCodeMap&& map) noexcept;
122
123 const PdfEncodingLimits& GetLimits() const { return m_Limits; }
124
125 bool IsEmpty() const;
126
128 bool IsTrivialIdentity() const;
129
130 std::vector<CodeSpaceRange> GetCodeSpaceRanges() const;
131
132 public:
134 const CodeUnitMap& GetMappings() const { return m_Mappings; }
135
137 const CodeUnitRanges& GetRanges() const { return m_Ranges; }
138
139 private:
140 void move(PdfCharCodeMap& map) noexcept;
141 void pushMapping(const PdfCharCode& codeUnit, const codepointview& codePoints);
142
143 private:
144 PdfCharCodeMap(const PdfCharCodeMap&) = delete;
145 PdfCharCodeMap& operator=(const PdfCharCodeMap&) = delete;
146
147 private:
148 void updateLimits(const PdfCharCode& codeUnit);
149 const CodePointMapNode* getCodePointMap() const;
150 void resetCodePointMap();
151 bool tryFixNextRanges(const CodeUnitRanges::iterator& it, unsigned prevRangeCodeUpper);
152
153 private:
154 PdfEncodingLimits m_Limits;
155 CodeUnitMap m_Mappings;
156 CodeUnitRanges m_Ranges;
157 // Head of a BST to lookup code points. NOTE: It's built lazily by the
158 // const lookups, which may run concurrently on shared maps such as the
159 // predefined CMaps, so it's stored atomically
160 mutable std::atomic<CodePointMapNode*> m_codePointMapHead;
161 };
162}
163
164#endif // PDF_CHAR_CODE_MAP_H
This file should be included as the FIRST file in every header of PoDoFo lib.
A memory owning immutable block of code points, optimized for small segments as up to 3 elements can ...
Definition PdfEncodingCommon.h:119
A bidirectional map from character code units to unspecified code points.
Definition PdfCharCodeMap.h:73
bool IsTrivialIdentity() const
Determines if the map is a trivial identity.
Definition PdfCharCodeMap.cpp:71
bool TryGetCharCode(const codepointview &codePoints, PdfCharCode &code) const
Try get char code from unicode code points.
Definition PdfCharCodeMap.cpp:327
bool TryGetCodePoints(const PdfCharCode &codeUnit, CodePointSpan &codePoints) const
Returns false when no mapped identifiers are not found in the map.
Definition PdfCharCodeMap.cpp:294
void PushMapping(const PdfCharCode &codeUnit, const codepointview &codePoints)
Method to push a mapping.
Definition PdfCharCodeMap.cpp:195
void PushRange(const PdfCharCode &srcCodeLo, unsigned size, codepoint dstCodeLo)
Push a range mapping in the form "srcCodeLo srcCodeHi dstCodeLo".
Definition PdfCharCodeMap.cpp:209
bool TryGetNextCharCode(std::string_view::iterator &it, const std::string_view::iterator &end, PdfCharCode &code) const
Try get char code from utf8 encoded range.
Definition PdfCharCodeMap.cpp:322
const CodeUnitMap & GetMappings() const
Provides direct mappings.
Definition PdfCharCodeMap.h:134
const CodeUnitRanges & GetRanges() const
Provides range mappings.
Definition PdfCharCodeMap.h:137
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
char32_t codepoint
A convenient typedef for an unspecified codepoint The underlying type is conveniently char32_t so it'...
Definition PdfEncodingCommon.h:113
A character code unit.
Definition PdfEncodingCommon.h:16