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-2023 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//
33// To see a description of the changes please see the Changelog file that
34// came with your tarball or type make ChangeLog if you are building from git
35//
36//========================================================================
37
38#ifndef GLOBALPARAMS_H
39#define GLOBALPARAMS_H
40
41#include <cassert>
42#include "poppler-config.h"
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 <vector>
53
54class GooString;
55class NameToCharCode;
56class CharCodeToUnicode;
57class CharCodeToUnicodeCache;
58class UnicodeMapCache;
59class CMap;
60class CMapCache;
61class GlobalParams;
62class GfxFont;
63class Stream;
64class SysFontList;
65
66//------------------------------------------------------------------------
67
68// The global parameters object.
69extern std::unique_ptr<GlobalParams> POPPLER_PRIVATE_EXPORT globalParams;
70
71//------------------------------------------------------------------------
72
73enum SysFontType
74{
75 sysFontPFA,
76 sysFontPFB,
77 sysFontTTF,
78 sysFontTTC
79};
80
81//------------------------------------------------------------------------
82
83struct FamilyStyleFontSearchResult
84{
85 FamilyStyleFontSearchResult() = default;
86
87 FamilyStyleFontSearchResult(const std::string &filepathA, int faceIndexA) : filepath(filepathA), faceIndex(faceIndexA) { }
88
89 std::string filepath;
90 int faceIndex = 0;
91};
92
93//------------------------------------------------------------------------
94
95struct UCharFontSearchResult
96{
97 UCharFontSearchResult() = default;
98
99 UCharFontSearchResult(const std::string &filepathA, int faceIndexA, const std::string &familyA, const std::string &styleA) : filepath(filepathA), faceIndex(faceIndexA), family(familyA), style(styleA) { }
100
101 const std::string filepath;
102 const int faceIndex = 0;
103 const std::string family;
104 const std::string style;
105};
106
107//------------------------------------------------------------------------
108
109class POPPLER_PRIVATE_EXPORT GlobalParams
110{
111public:
112 // Initialize the global parameters
113 explicit GlobalParams(const char *customPopplerDataDir = nullptr);
114
115 ~GlobalParams();
116
117 GlobalParams(const GlobalParams &) = delete;
118 GlobalParams &operator=(const GlobalParams &) = delete;
119
120 void setupBaseFonts(const char *dir);
121
122 //----- accessors
123
124 CharCode getMacRomanCharCode(const char *charName);
125
126 // Return Unicode values for character names. Used for general text
127 // extraction.
128 Unicode mapNameToUnicodeText(const char *charName);
129
130 // Return Unicode values for character names. Used for glyph
131 // lookups or text extraction with ZapfDingbats fonts.
132 Unicode mapNameToUnicodeAll(const char *charName);
133
134 UnicodeMap *getResidentUnicodeMap(const std::string &encodingName);
135 FILE *getUnicodeMapFile(const std::string &encodingName);
136 FILE *findCMapFile(const GooString *collection, const GooString *cMapName);
137 FILE *findToUnicodeFile(const GooString *name);
138 GooString *findFontFile(const std::string &fontName);
139 GooString *findBase14FontFile(const GooString *base14Name, const GfxFont *font, GooString *substituteFontName = nullptr);
140 GooString *findSystemFontFile(const GfxFont *font, SysFontType *type, int *fontNum, GooString *substituteFontName = nullptr, const GooString *base14Name = nullptr);
141 FamilyStyleFontSearchResult findSystemFontFileForFamilyAndStyle(const std::string &fontFamily, const std::string &fontStyle, const std::vector<std::string> &filesToIgnore = {});
142 UCharFontSearchResult findSystemFontFileForUChar(Unicode uChar, const GfxFont &fontToEmulate);
143 std::string getTextEncodingName() const;
144 bool getPrintCommands();
145 bool getProfileCommands();
146 bool getErrQuiet();
147
148 CharCodeToUnicode *getCIDToUnicode(const GooString *collection);
149 const UnicodeMap *getUnicodeMap(const std::string &encodingName);
150 std::shared_ptr<CMap> getCMap(const GooString *collection, const GooString *cMapName);
151 const UnicodeMap *getTextEncoding();
152
153 const UnicodeMap *getUtf8Map();
154
155 std::vector<std::string> getEncodingNames();
156
157 //----- functions to set parameters
158 void addFontFile(const std::string &fontName, const std::string &path);
159 void setTextEncoding(const char *encodingName);
160 void setPrintCommands(bool printCommandsA);
161 void setProfileCommands(bool profileCommandsA);
162 void setErrQuiet(bool errQuietA);
163#ifdef ANDROID
164 static void setFontDir(const std::string &fontDir);
165#endif
166 static bool parseYesNo2(const char *token, bool *flag);
167
168private:
169 void parseNameToUnicode(const GooString *name);
170
171 void scanEncodingDirs();
172 void addCIDToUnicode(const GooString *collection, const GooString *fileName);
173 void addUnicodeMap(const GooString *encodingName, const GooString *fileName);
174 void addCMapDir(const GooString *collection, const GooString *dir);
175
176 //----- static tables
177
178 NameToCharCode * // mapping from char name to
179 macRomanReverseMap; // MacRomanEncoding index
180
181 //----- user-modifiable settings
182
183 NameToCharCode * // mapping from char name to Unicode for ZapfDingbats
184 nameToUnicodeZapfDingbats;
185 NameToCharCode * // mapping from char name to Unicode for text
186 nameToUnicodeText; // extraction
187 // files for mappings from char collections
188 // to Unicode, indexed by collection name
189 std::unordered_map<std::string, std::string> cidToUnicodes;
190 // mappings from Unicode to char codes,
191 // indexed by encoding name
192 std::unordered_map<std::string, UnicodeMap> residentUnicodeMaps;
193 // files for mappings from Unicode to char
194 // codes, indexed by encoding name
195 std::unordered_map<std::string, std::string> unicodeMaps;
196 // list of CMap dirs, indexed by collection
197 std::unordered_multimap<std::string, std::string> cMapDirs;
198 std::vector<GooString *> toUnicodeDirs; // list of ToUnicode CMap dirs
199 bool baseFontsInitialized;
200#ifdef _WIN32
201 // windows font substitutes (for CID fonts)
202 std::unordered_map<std::string, std::string> substFiles;
203#endif
204 // font files: font name mapped to path
205 std::unordered_map<std::string, std::string> fontFiles;
206 SysFontList *sysFonts; // system fonts
207 GooString *textEncoding; // encoding (unicodeMap) to use for text
208 // output
209 bool printCommands; // print the drawing commands
210 bool profileCommands; // profile the drawing commands
211 bool errQuiet; // suppress error messages?
212
213 CharCodeToUnicodeCache *cidToUnicodeCache;
214 CharCodeToUnicodeCache *unicodeToUnicodeCache;
215 UnicodeMapCache *unicodeMapCache;
216 CMapCache *cMapCache;
217
218 const UnicodeMap *utf8Map;
219
220 mutable std::recursive_mutex mutex;
221 mutable std::recursive_mutex unicodeMapCacheMutex;
222 mutable std::recursive_mutex cMapCacheMutex;
223
224 const char *popplerDataDir;
225};
226
227class POPPLER_PRIVATE_EXPORT GlobalParamsIniter
228{
229public:
230 explicit GlobalParamsIniter(ErrorCallback errorCallback);
231 ~GlobalParamsIniter();
232
233 GlobalParamsIniter(const GlobalParamsIniter &) = delete;
234 GlobalParamsIniter &operator=(const GlobalParamsIniter &) = delete;
235
236 static bool setCustomDataDir(const std::string &dir);
237
238private:
239 static std::mutex mutex;
240 static int count;
241
242 static std::string customDataDir;
243};
244
245#endif
246

source code of poppler/poppler/GlobalParams.h