1 | //======================================================================== |
2 | // |
3 | // UnicodeMap.h |
4 | // |
5 | // Mapping from Unicode to an encoding. |
6 | // |
7 | // Copyright 2001-2003 Glyph & Cog, LLC |
8 | // |
9 | //======================================================================== |
10 | |
11 | //======================================================================== |
12 | // |
13 | // Modified under the Poppler project - http://poppler.freedesktop.org |
14 | // |
15 | // All changes made under the Poppler project to this file are licensed |
16 | // under GPL version 2 or later |
17 | // |
18 | // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com> |
19 | // Copyright (C) 2018-2022 Albert Astals Cid <aacid@kde.org> |
20 | // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de> |
21 | // Copyright (C) 2019 Volker Krause <vkrause@kde.org> |
22 | // |
23 | // To see a description of the changes please see the Changelog file that |
24 | // came with your tarball or type make ChangeLog if you are building from git |
25 | // |
26 | //======================================================================== |
27 | |
28 | #ifndef UNICODEMAP_H |
29 | #define UNICODEMAP_H |
30 | |
31 | #include "poppler-config.h" |
32 | #include "poppler_private_export.h" |
33 | #include "CharTypes.h" |
34 | |
35 | #include <atomic> |
36 | #include <memory> |
37 | #include <string> |
38 | #include <vector> |
39 | |
40 | //------------------------------------------------------------------------ |
41 | |
42 | enum UnicodeMapKind |
43 | { |
44 | unicodeMapUser, // read from a file |
45 | unicodeMapResident, // static list of ranges |
46 | unicodeMapFunc // function pointer |
47 | }; |
48 | |
49 | typedef int (*UnicodeMapFunc)(Unicode u, char *buf, int bufSize); |
50 | |
51 | struct UnicodeMapRange |
52 | { |
53 | Unicode start, end; // range of Unicode chars |
54 | unsigned int code, nBytes; // first output code |
55 | }; |
56 | |
57 | struct UnicodeMapExt; |
58 | |
59 | //------------------------------------------------------------------------ |
60 | |
61 | class POPPLER_PRIVATE_EXPORT UnicodeMap |
62 | { |
63 | public: |
64 | // Create the UnicodeMap specified by <encodingName>. Sets the |
65 | // initial reference count to 1. Returns NULL on failure. |
66 | static std::unique_ptr<UnicodeMap> parse(const std::string &encodingNameA); |
67 | |
68 | // Create a resident UnicodeMap. |
69 | UnicodeMap(const char *encodingNameA, bool unicodeOutA, const UnicodeMapRange *rangesA, int lenA); |
70 | |
71 | // Create a resident UnicodeMap that uses a function instead of a |
72 | // list of ranges. |
73 | UnicodeMap(const char *encodingNameA, bool unicodeOutA, UnicodeMapFunc funcA); |
74 | |
75 | UnicodeMap(UnicodeMap &&other) noexcept; |
76 | UnicodeMap &operator=(UnicodeMap &&other) noexcept; |
77 | |
78 | void swap(UnicodeMap &other) noexcept; |
79 | |
80 | ~UnicodeMap(); |
81 | |
82 | UnicodeMap(const UnicodeMap &) = delete; |
83 | UnicodeMap &operator=(const UnicodeMap &) = delete; |
84 | |
85 | std::string getEncodingName() const { return encodingName; } |
86 | |
87 | bool isUnicode() const { return unicodeOut; } |
88 | |
89 | // Return true if this UnicodeMap matches the specified |
90 | // <encodingNameA>. |
91 | bool match(const std::string &encodingNameA) const; |
92 | |
93 | // Map Unicode to the target encoding. Fills in <buf> with the |
94 | // output and returns the number of bytes used. Output will be |
95 | // truncated at <bufSize> bytes. No string terminator is written. |
96 | // Returns 0 if no mapping is found. |
97 | int mapUnicode(Unicode u, char *buf, int bufSize) const; |
98 | |
99 | private: |
100 | explicit UnicodeMap(const std::string &encodingNameA); |
101 | |
102 | std::string encodingName; |
103 | UnicodeMapKind kind; |
104 | bool unicodeOut; |
105 | union { |
106 | const UnicodeMapRange *ranges; // (user, resident) |
107 | UnicodeMapFunc func; // (func) |
108 | }; |
109 | int len; // (user, resident) |
110 | UnicodeMapExt *eMaps; // (user) |
111 | int eMapsLen; // (user) |
112 | }; |
113 | |
114 | //------------------------------------------------------------------------ |
115 | |
116 | class UnicodeMapCache |
117 | { |
118 | public: |
119 | UnicodeMapCache(); |
120 | |
121 | // Get the UnicodeMap for <encodingName>. Returns NULL on failure. |
122 | const UnicodeMap *getUnicodeMap(const std::string &encodingName); |
123 | |
124 | private: |
125 | std::vector<std::unique_ptr<UnicodeMap>> cache; |
126 | }; |
127 | |
128 | #endif |
129 | |