PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfFontManager.h
1// SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
2// SPDX-FileCopyrightText: 2020 Francesco Pretto <ceztko@gmail.com>
3// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
4
5#ifndef PDF_FONT_CACHE_H
6#define PDF_FONT_CACHE_H
7
8#include "PdfDeclarations.h"
9
10#include "PdfFont.h"
11#include "PdfEncodingFactory.h"
12
13#ifdef PODOFO_HAVE_FONTCONFIG
14#include "PdfFontConfigWrapper.h"
15#endif
16
17#if defined(_WIN32) && defined(PODOFO_HAVE_WIN32GDI)
18// To have LOGFONTW available
19typedef struct HFONT__* HFONT;
20#endif
21
22namespace PoDoFo {
23
24class PdfResources;
25
26struct PODOFO_API PdfFontSearchParams final
27{
28 nullable<PdfFontStyle> Style;
29 PdfFontAutoSelectBehavior AutoSelect = PdfFontAutoSelectBehavior::None;
30 PdfFontMatchBehaviorFlags MatchBehavior = PdfFontMatchBehaviorFlags::None;
32 std::string FontFamilyPattern;
33
35 std::function<PdfFont* (const std::vector<PdfFont*>)> FontSelector;
36};
37
49class PODOFO_API PdfFontManager final
50{
51 friend class PdfDocument;
52 friend class PdfMemDocument;
53 friend class PdfStreamedDocument;
54 friend class PdfFont;
55 friend class PdfCommon;
56 friend class PdfResources;
57
58public:
68 PdfFont* SearchFont(const std::string_view& fontPattern,
69 const PdfFontSearchParams& searchParams = { }, const PdfFontCreateParams& createParams = { });
70
79 PdfFont* SearchFont(const std::string_view& fontPattern, const PdfFontCreateParams& createParams);
80
85 PdfFont& GetStandard14Font(PdfStandard14FontType stdFont,
86 const PdfFontCreateParams& params = { });
87
93 PdfFont& GetOrCreateFont(const std::string_view& fontPath, unsigned faceIndex,
94 const PdfFontCreateParams& params = { });
95
101 PdfFont& GetOrCreateFontFromBuffer(const bufferview& buffer, unsigned faceIndex,
102 const PdfFontCreateParams& params = { });
103
108 PdfFont& GetOrCreateFont(const std::string_view& fontPath,
109 const PdfFontCreateParams& params = { });
110
115 PdfFont& GetOrCreateFontFromBuffer(const bufferview& buffer,
116 const PdfFontCreateParams& params = { });
117
122 PdfFont& GetOrCreateFont(PdfFontMetricsConstPtr metrics,
123 const PdfFontCreateParams& params = { });
124
128 PdfFont* GetCachedFont(const PdfReference& ref);
129
136 const PdfFontSearchParams& params = { });
137
138#if defined(_WIN32) && defined(PODOFO_HAVE_WIN32GDI)
139 PdfFont& GetOrCreateFont(HFONT font, const PdfFontCreateParams& params = { });
140#endif
141
142#ifdef PODOFO_HAVE_FONTCONFIG
148 static void SetFontConfigWrapper(const std::shared_ptr<PdfFontConfigWrapper>& fontConfig);
149
150 static PdfFontConfigWrapper& GetFontConfigWrapper();
151#endif // PODOFO_HAVE_FONTCONFIG
152
156 void EmbedFonts();
157
158 // These methods are reserved to use to selected friend classes
159private:
160 PdfFontManager(PdfDocument& doc);
161
162private:
163 const PdfFont* GetLoadedFont(const PdfResources& resources, const std::string_view& name);
164
168 void Clear();
169
170 PdfFont* AddImported(std::unique_ptr<PdfFont>&& font);
171
173 std::string GenerateSubsetPrefix();
174
175 static void AddFontDirectory(const std::string_view& path);
176
179 static PdfFontMetricsConstPtr SearchFontMetrics(const std::string_view& fontPattern,
180 const PdfFontSearchParams& params, const PdfFontMetrics& metrics, bool skipNormalization);
181
182private:
184 struct Descriptor
185 {
186 Descriptor(const std::string_view& name, PdfStandard14FontType stdType,
187 const PdfEncoding& encoding, bool hasFontStyle, PdfFontStyle style);
188
189 Descriptor(const Descriptor& rhs) = default;
190
191 const std::string Name;
192 const PdfStandard14FontType StdType;
193 const unsigned EncodingId;
194 const bool HasFontStyle;
195 const PdfFontStyle Style;
196 };
197
198 struct PathDescriptor
199 {
200 PathDescriptor(const std::string_view& filepath, unsigned faceIndex, const PdfEncoding& encoding);
201
202 PathDescriptor(const PathDescriptor& rhs) = default;
203
204 const std::string FilePath;
205 const unsigned FaceIndex;
206 const unsigned EncodingId;
207 };
208
209 struct HashElement
210 {
211 size_t operator()(const Descriptor& elem) const;
212 size_t operator()(const PathDescriptor& elem) const;
213 };
214
215 struct EqualElement
216 {
217 bool operator()(const Descriptor& lhs, const Descriptor& rhs) const;
218 bool operator()(const PathDescriptor& lhs, const PathDescriptor& rhs) const;
219 };
220
221 using CachedPaths = std::unordered_map<PathDescriptor, PdfFont*, HashElement, EqualElement>;
222 using CachedQueries = std::unordered_map<Descriptor, std::vector<PdfFont*>, HashElement, EqualElement>;
223
224 struct Storage
225 {
226 bool IsLoaded;
227 std::unique_ptr<PdfFont> Font;
228 };
229
230 using FontMap = std::unordered_map<PdfReference, Storage>;
231
232private:
233 static std::unique_ptr<const PdfFontMetrics> searchFontMetrics(const std::string_view& fontName,
234 const PdfFontSearchParams& params, const PdfFontMetrics* refMetrics, bool skipNormalization);
235 PdfFont* getImportedFont(const std::string_view& pattern,
236 const PdfFontSearchParams& searchParams, const PdfFontCreateParams& createParams);
237 PdfFont* addImported(std::vector<PdfFont*>& fonts, std::unique_ptr<PdfFont>&& font);
238 PdfFont& getOrCreateFontHashed(PdfFontMetricsConstPtr&& metrics, const PdfFontCreateParams& params);
239
240private:
241 PdfFontManager(const PdfFontManager&) = delete;
242 PdfFontManager& operator=(const PdfFontManager&) = delete;
243
244private:
245 PdfDocument* m_doc;
246 std::string m_currentPrefix;
247
248 // Map of cached font queries
249 CachedQueries m_cachedQueries;
250
251 // Map of cached font paths
252 CachedPaths m_cachedPaths;
253
254 // Map of all indirect fonts
255 FontMap m_fonts;
256
257 // Map of all invalid inline fonts
258 std::unordered_map<std::string, std::unique_ptr<PdfFont>> m_inlineFonts;
259
260#ifdef PODOFO_HAVE_FONTCONFIG
261 static std::shared_ptr<PdfFontConfigWrapper> m_fontConfig;
262#endif
263};
264
265};
266
267#endif // PDF_FONT_CACHE_H
This file should be included as the FIRST file in every header of PoDoFo lib.
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:108
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:50
PdfFont * SearchFont(const std::string_view &fontPattern, const PdfFontCreateParams &createParams)
Get a font from the cache.
PdfFont & GetOrCreateFont(const std::string_view &fontPath, const PdfFontCreateParams &params={ })
Get or create a font from a file path.
PdfFont * SearchFont(const std::string_view &fontPattern, const PdfFontSearchParams &searchParams={ }, const PdfFontCreateParams &createParams={ })
Get a font from the cache.
PdfFont & GetOrCreateFont(const std::string_view &fontPath, unsigned faceIndex, const PdfFontCreateParams &params={ })
Get or create a font from a file path.
static PdfFontMetricsConstPtr SearchFontMetrics(const std::string_view &fontPattern, const PdfFontSearchParams &params={ })
Try to search for fontmetrics from the given fontname and parameters.
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:42
PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk...
Definition PdfMemDocument.h:35
A interface that provides a wrapper around /Resources.
Definition PdfResources.h:21
PdfStreamedDocument is the preferred class for creating new PDF documents.
Definition PdfStreamedDocument.h:47
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfFontAutoSelectBehavior
Flags to control font creation.
Definition PdfDeclarations.h:308
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:15
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition PdfFontMetrics.h:24
PdfFontMatchBehaviorFlags
Definition PdfDeclarations.h:324