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