PoDoFo  1.0.0-dev
PdfFontManager.h
1 
7 #ifndef PDF_FONT_CACHE_H
8 #define PDF_FONT_CACHE_H
9 
10 #include "PdfDeclarations.h"
11 
12 #include "PdfFont.h"
13 #include "PdfEncodingFactory.h"
14 
15 #ifdef PODOFO_HAVE_FONTCONFIG
16 #include "PdfFontConfigWrapper.h"
17 #endif
18 
19 #if defined(_WIN32) && defined(PODOFO_HAVE_WIN32GDI)
20 // To have LOGFONTW available
21 typedef struct HFONT__* HFONT;
22 #endif
23 
24 namespace PoDoFo {
25 
26 class PdfResources;
27 
28 struct PODOFO_API PdfFontSearchParams final
29 {
30  nullable<PdfFontStyle> Style;
32  PdfFontMatchBehaviorFlags MatchBehavior = PdfFontMatchBehaviorFlags::None;
34  std::string FontFamilyPattern;
35 
37  std::function<PdfFont* (const std::vector<PdfFont*>)> FontSelector;
38 };
39 
53 class PODOFO_API PdfFontManager final
54 {
55  friend class PdfDocument;
56  friend class PdfMemDocument;
57  friend class PdfStreamedDocument;
58  friend class PdfFont;
59  friend class PdfCommon;
60  friend class PdfResources;
61 
62 public:
72  PdfFont* SearchFont(const std::string_view& fontPattern,
73  const PdfFontSearchParams& searchParams = { }, const PdfFontCreateParams& createParams = { });
74 
75  PdfFont* SearchFont(const std::string_view& fontPattern, const PdfFontCreateParams& createParams);
76 
77  PdfFont& GetStandard14Font(PdfStandard14FontType stdFont,
78  const PdfFontCreateParams& params = { });
79 
80  PdfFont& GetOrCreateFont(const std::string_view& fontPath, unsigned faceIndex,
81  const PdfFontCreateParams& params = { });
82 
83  PdfFont& GetOrCreateFontFromBuffer(const bufferview& buffer, unsigned faceIndex,
84  const PdfFontCreateParams& params = { });
85 
86  PdfFont& GetOrCreateFont(const std::string_view& fontPath,
87  const PdfFontCreateParams& params = { });
88 
89  PdfFont& GetOrCreateFontFromBuffer(const bufferview& buffer,
90  const PdfFontCreateParams& params = { });
91 
92  PdfFont& GetOrCreateFont(const PdfFontMetricsConstPtr& metrics,
93  const PdfFontCreateParams& params = { });
94 
98  PdfFont* GetCachedFont(const PdfReference& ref);
99 
104  static PdfFontMetricsConstPtr SearchFontMetrics(const std::string_view& fontPattern,
105  const PdfFontSearchParams& params = { });
106 
107 #if defined(_WIN32) && defined(PODOFO_HAVE_WIN32GDI)
108  PdfFont& GetOrCreateFont(HFONT font, const PdfFontCreateParams& params = { });
109 #endif
110 
111 #ifdef PODOFO_HAVE_FONTCONFIG
119  static void SetFontConfigWrapper(const std::shared_ptr<PdfFontConfigWrapper>& fontConfig);
120 
121  static PdfFontConfigWrapper& GetFontConfigWrapper();
122 #endif // PODOFO_HAVE_FONTCONFIG
123 
126  void EmbedFonts();
127 
128  // These methods are reserved to use to selected friend classes
129 private:
130  PdfFontManager(PdfDocument& doc);
131 
132 private:
133  const PdfFont* GetLoadedFont(const PdfResources& resources, const std::string_view& name);
134 
140  void Clear();
141 
142  PdfFont* AddImported(std::unique_ptr<PdfFont>&& font);
143 
146  std::string GenerateSubsetPrefix();
147 
148  static void AddFontDirectory(const std::string_view& path);
149 
150 private:
153  struct Descriptor
154  {
155  Descriptor(const std::string_view& name, PdfStandard14FontType stdType,
156  const PdfEncoding& encoding, bool hasFontStyle, PdfFontStyle style);
157 
158  Descriptor(const Descriptor& rhs) = default;
159 
160  const std::string Name;
161  const PdfStandard14FontType StdType;
162  const unsigned EncodingId;
163  const bool HasFontStyle;
164  const PdfFontStyle Style;
165  };
166 
167  struct PathDescriptor
168  {
169  PathDescriptor(const std::string_view& filepath, unsigned faceIndex, const PdfEncoding& encoding);
170 
171  PathDescriptor(const PathDescriptor& rhs) = default;
172 
173  const std::string FilePath;
174  const unsigned FaceIndex;
175  const unsigned EncodingId;
176  };
177 
178  struct HashElement
179  {
180  size_t operator()(const Descriptor& elem) const;
181  size_t operator()(const PathDescriptor& elem) const;
182  };
183 
184  struct EqualElement
185  {
186  bool operator()(const Descriptor& lhs, const Descriptor& rhs) const;
187  bool operator()(const PathDescriptor& lhs, const PathDescriptor& rhs) const;
188  };
189 
190  using CachedPaths = std::unordered_map<PathDescriptor, PdfFont*, HashElement, EqualElement>;
191  using CachedQueries = std::unordered_map<Descriptor, std::vector<PdfFont*>, HashElement, EqualElement>;
192 
193  struct Storage
194  {
195  bool IsLoaded;
196  std::unique_ptr<PdfFont> Font;
197  };
198 
199  using FontMap = std::unordered_map<PdfReference, Storage>;
200 
201 private:
202  static std::unique_ptr<const PdfFontMetrics> getFontMetrics(const std::string_view& fontName,
203  const PdfFontSearchParams& params);
204  PdfFont* getImportedFont(const std::string_view& pattern,
205  const PdfFontSearchParams& searchParams, const PdfFontCreateParams& createParams);
206  PdfFont* addImported(std::vector<PdfFont*>& fonts, std::unique_ptr<PdfFont>&& font);
207  PdfFont& getOrCreateFontHashed(const PdfFontMetricsConstPtr& metrics, const PdfFontCreateParams& params);
208 
209 #if defined(_WIN32) && defined(PODOFO_HAVE_WIN32GDI)
210  static std::unique_ptr<charbuff> getWin32FontData(const std::string_view& fontName,
211  const PdfFontSearchParams& params);
212 #endif
213 
214 private:
215  PdfFontManager(const PdfFontManager&) = delete;
216  PdfFontManager& operator=(const PdfFontManager&) = delete;
217 
218 private:
219  PdfDocument* m_doc;
220  std::string m_currentPrefix;
221 
222  // Map of cached font queries
223  CachedQueries m_cachedQueries;
224 
225  // Map of cached font paths
226  CachedPaths m_cachedPaths;
227 
228  // Map of all indirect fonts
229  FontMap m_fonts;
230 
231  // Map of all invalid inline fonts
232  std::unordered_map<std::string, std::unique_ptr<PdfFont>> m_inlineFonts;
233 
234 #ifdef PODOFO_HAVE_FONTCONFIG
235  static std::shared_ptr<PdfFontConfigWrapper> m_fontConfig;
236 #endif
237 };
238 
239 };
240 
241 #endif // PDF_FONT_CACHE_H
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
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:54
PdfFont * SearchFont(const std::string_view &fontPattern, const PdfFontSearchParams &searchParams={ }, const PdfFontCreateParams &createParams={ })
Get a font from the cache.
Before you can draw text on a PDF document, you have to create a font object first.
Definition: PdfFont.h:49
PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk...
Definition: PdfMemDocument.h:39
A interface that provides a wrapper around /Resources.
Definition: PdfResources.h:25
PdfStreamedDocument is the preferred class for creating new PDF documents.
Definition: PdfStreamedDocument.h:50
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
@ Name
Name datatype. Names are used as keys in dictionary to reference values.
PdfFontAutoSelectBehavior
Flags to control font creation.
Definition: PdfDeclarations.h:302
cspan< char > bufferview
Convenient read-only char buffer span.
Definition: basetypes.h:19
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition: PdfFontMetrics.h:24
PdfFontStyle
Font style flags used during searches.
Definition: PdfDeclarations.h:281
PdfFontMatchBehaviorFlags
Definition: PdfDeclarations.h:319