/src/poppler/poppler/GlobalParams.h
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // GlobalParams.h |
4 | | // |
5 | | // Copyright 2001-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | //======================================================================== |
10 | | // |
11 | | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | | // |
13 | | // All changes made under the Poppler project to this file are licensed |
14 | | // under GPL version 2 or later |
15 | | // |
16 | | // Copyright (C) 2005, 2007-2010, 2012, 2015, 2017-2026 Albert Astals Cid <aacid@kde.org> |
17 | | // Copyright (C) 2005 Jonathan Blandford <jrb@redhat.com> |
18 | | // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de> |
19 | | // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com> |
20 | | // Copyright (C) 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com> |
21 | | // Copyright (C) 2009 Jonathan Kew <jonathan_kew@sil.org> |
22 | | // Copyright (C) 2009 Petr Gajdos <pgajdos@novell.com> |
23 | | // Copyright (C) 2009, 2011, 2012, 2014, 2015 William Bader <williambader@hotmail.com> |
24 | | // Copyright (C) 2010 Hib Eris <hib@hiberis.nl> |
25 | | // Copyright (C) 2011 Pino Toscano <pino@kde.org> |
26 | | // Copyright (C) 2012, 2017 Adrian Johnson <ajohnson@redneon.com> |
27 | | // Copyright (C) 2012 Thomas Freitag <Thomas.Freitag@alfa.de> |
28 | | // Copyright (C) 2013 Jason Crain <jason@aquaticape.us> |
29 | | // Copyright (C) 2018, 2020 Adam Reichold <adam.reichold@t-online.de> |
30 | | // Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de> |
31 | | // Copyright (C) 2023 Shivodit Gill <shivodit.gill@gmail.com> |
32 | | // Copyright (C) 2024-2026 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk> |
33 | | // |
34 | | // To see a description of the changes please see the Changelog file that |
35 | | // came with your tarball or type make ChangeLog if you are building from git |
36 | | // |
37 | | //======================================================================== |
38 | | |
39 | | #ifndef GLOBALPARAMS_H |
40 | | #define GLOBALPARAMS_H |
41 | | |
42 | | #include <cassert> |
43 | | #include "poppler_private_export.h" |
44 | | #include <cstdio> |
45 | | #include "CharTypes.h" |
46 | | #include "UnicodeMap.h" |
47 | | #include "Error.h" |
48 | | #include <unordered_map> |
49 | | #include <string> |
50 | | #include <memory> |
51 | | #include <mutex> |
52 | | #include <optional> |
53 | | #include <utility> |
54 | | #include <vector> |
55 | | #include <filesystem> |
56 | | |
57 | | class GooString; |
58 | | class NameToCharCode; |
59 | | class CharCodeToUnicode; |
60 | | class CharCodeToUnicodeCache; |
61 | | class UnicodeMapCache; |
62 | | class CMap; |
63 | | class CMapCache; |
64 | | class GlobalParams; |
65 | | class GfxFont; |
66 | | class Stream; |
67 | | class SysFontList; |
68 | | |
69 | | //------------------------------------------------------------------------ |
70 | | |
71 | | // The global parameters object. |
72 | | extern std::unique_ptr<GlobalParams> POPPLER_PRIVATE_EXPORT globalParams; |
73 | | |
74 | | //------------------------------------------------------------------------ |
75 | | |
76 | | enum SysFontType |
77 | | { |
78 | | sysFontPFA, |
79 | | sysFontPFB, |
80 | | sysFontTTF, |
81 | | sysFontTTC |
82 | | }; |
83 | | |
84 | | //------------------------------------------------------------------------ |
85 | | |
86 | | struct FamilyStyleFontSearchResult |
87 | | { |
88 | 0 | FamilyStyleFontSearchResult() = default; |
89 | | |
90 | 0 | FamilyStyleFontSearchResult(std::string filepathA, int faceIndexA, bool substitutedA) : filepath(std::move(filepathA)), faceIndex(faceIndexA), substituted(substitutedA) { } |
91 | | |
92 | | std::string filepath; |
93 | | int faceIndex = 0; |
94 | | bool substituted = false; /**True if you got something close but different*/ |
95 | | }; |
96 | | |
97 | | //------------------------------------------------------------------------ |
98 | | |
99 | | struct UCharFontSearchResult |
100 | | { |
101 | 0 | UCharFontSearchResult() = default; |
102 | | |
103 | 0 | UCharFontSearchResult(std::string filepathA, int faceIndexA, std::string familyA, std::string styleA) : filepath(std::move(filepathA)), faceIndex(faceIndexA), family(std::move(familyA)), style(std::move(styleA)) { } |
104 | | |
105 | | const std::string filepath; |
106 | | const int faceIndex = 0; |
107 | | const std::string family; |
108 | | const std::string style; |
109 | | }; |
110 | | |
111 | | //------------------------------------------------------------------------ |
112 | | |
113 | | class POPPLER_PRIVATE_EXPORT GlobalParams |
114 | | { |
115 | | public: |
116 | | // Initialize the global parameters |
117 | | explicit GlobalParams(std::string customPopplerDataDir = {}); |
118 | | |
119 | | ~GlobalParams(); |
120 | | |
121 | | GlobalParams(const GlobalParams &) = delete; |
122 | | GlobalParams &operator=(const GlobalParams &) = delete; |
123 | | |
124 | | void setupBaseFonts(const char *dir); |
125 | | |
126 | | //----- accessors |
127 | | |
128 | | CharCode getMacRomanCharCode(const char *charName); |
129 | | |
130 | | // Return Unicode values for character names. Used for general text |
131 | | // extraction. |
132 | | Unicode mapNameToUnicodeText(const char *charName); |
133 | | |
134 | | // Return Unicode values for character names. Used for glyph |
135 | | // lookups or text extraction with ZapfDingbats fonts. |
136 | | Unicode mapNameToUnicodeAll(const char *charName); |
137 | | |
138 | | UnicodeMap *getResidentUnicodeMap(const std::string &encodingName); |
139 | | FILE *getUnicodeMapFile(const std::string &encodingName); |
140 | | FILE *findCMapFile(const std::string &collection, const std::string &cMapName); |
141 | | FILE *findToUnicodeFile(const std::string &name); |
142 | | std::optional<std::string> findFontFile(const std::string &fontName); |
143 | | std::optional<std::string> findBase14FontFile(const std::string &base14Name, const GfxFont &font, GooString *substituteFontName = nullptr); |
144 | | std::optional<std::string> findSystemFontFile(const GfxFont &font, SysFontType *type, int *fontNum, GooString *substituteFontName = nullptr, const std::string *base14Name = nullptr); |
145 | | FamilyStyleFontSearchResult findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle, const std::vector<std::string> &filesToIgnore = {}); |
146 | | UCharFontSearchResult findSystemFontFileForUChar(Unicode uChar, const GfxFont &fontToEmulate); |
147 | | std::string getTextEncodingName() const; |
148 | | bool getPrintCommands(); |
149 | | bool getProfileCommands(); |
150 | | bool getErrQuiet() const; |
151 | | |
152 | | std::shared_ptr<CharCodeToUnicode> getCIDToUnicode(const std::string &collection); |
153 | | const UnicodeMap *getUnicodeMap(const std::string &encodingName); |
154 | | std::shared_ptr<CMap> getCMap(const std::string &collection, const std::string &cMapName); |
155 | | const UnicodeMap *getTextEncoding(); |
156 | | |
157 | | const UnicodeMap *getUtf8Map(); |
158 | | |
159 | | std::vector<std::string> getEncodingNames(); |
160 | | |
161 | | //----- functions to set parameters |
162 | | void addFontFile(const std::string &fontName, const std::string &path); |
163 | | void setTextEncoding(const std::string &encodingName); |
164 | | void setPrintCommands(bool printCommandsA); |
165 | | void setProfileCommands(bool profileCommandsA); |
166 | | void setErrQuiet(bool errQuietA); |
167 | | #ifdef ANDROID |
168 | | static void setFontDir(const std::string &fontDir); |
169 | | #endif |
170 | | static bool parseYesNo2(const char *token, bool *flag); |
171 | | |
172 | | private: |
173 | | static std::string appendToPath(const std::string &path, const std::string &fileName); |
174 | | void parseNameToUnicode(const std::filesystem::path &name); |
175 | | |
176 | | void scanEncodingDirs(); |
177 | | void addCIDToUnicode(std::string &&collection, std::string &&fileName); |
178 | | void addUnicodeMap(std::string &&encodingName, std::string &&fileName); |
179 | | void addCMapDir(std::string &&collection, std::string &&dir); |
180 | | |
181 | | //----- static tables |
182 | | |
183 | | NameToCharCode * // mapping from char name to |
184 | | macRomanReverseMap; // MacRomanEncoding index |
185 | | |
186 | | //----- user-modifiable settings |
187 | | |
188 | | NameToCharCode * // mapping from char name to Unicode for ZapfDingbats |
189 | | nameToUnicodeZapfDingbats; |
190 | | NameToCharCode * // mapping from char name to Unicode for text |
191 | | nameToUnicodeText; // extraction |
192 | | // files for mappings from char collections |
193 | | // to Unicode, indexed by collection name |
194 | | std::unordered_map<std::string, std::string> cidToUnicodes; |
195 | | // mappings from Unicode to char codes, |
196 | | // indexed by encoding name |
197 | | std::unordered_map<std::string, UnicodeMap> residentUnicodeMaps; |
198 | | // files for mappings from Unicode to char |
199 | | // codes, indexed by encoding name |
200 | | std::unordered_map<std::string, std::string> unicodeMaps; |
201 | | // list of CMap dirs, indexed by collection |
202 | | std::unordered_multimap<std::string, std::string> cMapDirs; |
203 | | std::vector<std::string> toUnicodeDirs; // list of ToUnicode CMap dirs |
204 | | bool baseFontsInitialized; |
205 | | #ifdef _WIN32 |
206 | | // windows font substitutes (for CID fonts) |
207 | | std::unordered_map<std::string, std::string> substFiles; |
208 | | #endif |
209 | | // font files: font name mapped to path |
210 | | std::unordered_map<std::string, std::string> fontFiles; |
211 | | SysFontList *sysFonts; // system fonts |
212 | | std::string textEncoding; // encoding (unicodeMap) to use for text |
213 | | // output |
214 | | bool printCommands; // print the drawing commands |
215 | | bool profileCommands; // profile the drawing commands |
216 | | bool errQuiet; // suppress error messages? |
217 | | |
218 | | std::unique_ptr<CharCodeToUnicodeCache> cidToUnicodeCache; |
219 | | std::unique_ptr<CharCodeToUnicodeCache> unicodeToUnicodeCache; |
220 | | UnicodeMapCache *unicodeMapCache; |
221 | | CMapCache *cMapCache; |
222 | | |
223 | | const UnicodeMap *utf8Map; |
224 | | |
225 | | mutable std::recursive_mutex mutex; |
226 | | mutable std::recursive_mutex unicodeMapCacheMutex; |
227 | | mutable std::recursive_mutex cMapCacheMutex; |
228 | | |
229 | | std::string popplerDataDir; |
230 | | }; |
231 | | |
232 | | class POPPLER_PRIVATE_EXPORT GlobalParamsIniter |
233 | | { |
234 | | public: |
235 | | explicit GlobalParamsIniter(ErrorCallback errorCallback); |
236 | | ~GlobalParamsIniter(); |
237 | | |
238 | | GlobalParamsIniter(const GlobalParamsIniter &) = delete; |
239 | | GlobalParamsIniter &operator=(const GlobalParamsIniter &) = delete; |
240 | | |
241 | | static bool setCustomDataDir(const std::string &dir); |
242 | | |
243 | | private: |
244 | | static std::mutex mutex; |
245 | | static int count; |
246 | | |
247 | | static std::string customDataDir; |
248 | | }; |
249 | | |
250 | | #endif |