PoDoFo  1.0.0-dev
PdfFont.h
1 
7 #ifndef PDF_FONT_H
8 #define PDF_FONT_H
9 
10 #include "PdfDeclarations.h"
11 
12 #include <ostream>
13 
14 #include "PdfTextState.h"
15 #include "PdfName.h"
16 #include "PdfEncoding.h"
17 #include "PdfElement.h"
18 #include "PdfFontMetrics.h"
19 
20 namespace PoDoFo {
21 
22 class PdfCharCodeMap;
23 
24 using UsedGIDsMap = std::map<unsigned, PdfCID>;
25 
26 struct PODOFO_API PdfFontCreateParams final
27 {
28  PdfEncoding Encoding;
30 };
31 
32 struct PODOFO_API PdfSplittedString final
33 {
34  PdfString String;
35  bool IsSeparator = false;
36 };
37 
48 class PODOFO_API PdfFont : public PdfDictionaryElement
49 {
50  friend class PdfFontSimple;
51  friend class PdfFontCID;
52  friend class PdfFontFactory;
53  friend class PdfFontObject;
54  friend class PdfEncoding;
55  friend class PdfFontManager;
56 
57 private:
67  PdfFont(PdfDocument& doc, const PdfFontMetricsConstPtr& metrics,
68  const PdfEncoding& encoding);
69 
73  PdfFont(PdfObject& obj, const PdfFontMetricsConstPtr& metrics,
74  const PdfEncoding& encoding);
75 
76 public:
77  virtual ~PdfFont();
78 
79 public:
86  static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfFont>& font);
87  static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfFont>& font);
88 
89 private:
102  static std::unique_ptr<PdfFont> Create(PdfDocument& doc, const PdfFontMetricsConstPtr& metrics,
103  const PdfFontCreateParams& createParams);
104 
117  static std::unique_ptr<PdfFont> CreateStandard14(PdfDocument& doc, PdfStandard14FontType std14Font,
118  const PdfFontCreateParams& createParams);
119 
120 public:
124  bool TryCreateSubstituteFont(PdfFont*& substFont) const;
125  bool TryCreateSubstituteFont(PdfFontCreateFlags initFlags, PdfFont*& substFont) const;
126 
136  void WriteStringToStream(OutputStream& stream, const std::string_view& str) const;
137 
144  unsigned GetGID(char32_t codePoint, PdfGlyphAccess access) const;
145  bool TryGetGID(char32_t codePoint, PdfGlyphAccess access, unsigned& gid) const;
146 
153  double GetStringLength(const std::string_view& str, const PdfTextState& state) const;
154 
159  bool TryGetStringLength(const std::string_view& str, const PdfTextState& state, double& width) const;
160 
167  double GetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state) const;
168 
172  bool TryGetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state, double& length) const;
173 
179  bool TryScanEncodedString(const PdfString& encodedStr, const PdfTextState& state, std::string& utf8str,
180  std::vector<double>& lengths, std::vector<unsigned>& positions) const;
181 
188  double GetWordSpacingLength(const PdfTextState& state) const;
189 
196  double GetSpaceCharLength(const PdfTextState& state) const;
197 
201  double GetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing = false) const;
202 
203  bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing, double& width) const;
204 
205  bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, double& width) const;
206 
207  double GetDefaultCharLength(const PdfTextState& state, bool ignoreCharSpacing = false) const;
208 
209  // TODO: Implement me
213  //std::vector<PdfSplittedString> SplitEncodedString(const PdfString& str) const;
214 
219  void AddSubsetGIDs(const PdfString& encodedStr);
220 
224  double GetLineSpacing(const PdfTextState& state) const;
225 
230  double GetUnderlineThickness(const PdfTextState& state) const;
231 
236  double GetUnderlinePosition(const PdfTextState& state) const;
237 
242  double GetStrikeThroughPosition(const PdfTextState& state) const;
243 
248  double GetStrikeThroughThickness(const PdfTextState& state) const;
249 
257  double GetAscent(const PdfTextState& state) const;
258 
267  double GetDescent(const PdfTextState& state) const;
268 
269  virtual bool SupportsSubsetting() const;
270 
271  virtual PdfFontType GetType() const = 0;
272 
273  bool IsStandard14Font() const;
274 
275  bool IsStandard14Font(PdfStandard14FontType& std14Font) const;
276 
277  static std::string_view GetStandard14FontName(PdfStandard14FontType stdFont);
278 
284  static bool IsStandard14Font(const std::string_view& fontName, PdfStandard14FontType& stdFont);
285 
289  static bool IsStandard14Font(const std::string_view& fontName, bool useAltNames, PdfStandard14FontType& stdFont);
290 
291 public:
294  bool IsCIDKeyed() const;
295 
299  virtual bool IsObjectLoaded() const;
300 
304  inline bool IsSubsettingEnabled() const { return m_SubsettingEnabled; }
305 
306  inline bool IsEmbeddingEnabled() const { return m_EmbeddingEnabled; }
307 
312  inline const std::string& GetSubsetPrefix() const { return m_SubsetPrefix; }
313 
317  inline const PdfEncoding& GetEncoding() const { return *m_Encoding; }
318 
324  inline const PdfFontMetrics& GetMetrics() const { return *m_Metrics; }
325 
331  inline const std::string& GetName() const { return m_Name; }
332 
333  const UsedGIDsMap& GetUsedGIDs() const { return m_SubsetGIDs; }
334 
335  PdfObject& GetDescendantFontObject();
336 
337 protected:
338  void EmbedFontFile(PdfObject& descriptor);
339  void EmbedFontFileType1(PdfObject& descriptor, const bufferview& data,
340  unsigned length1, unsigned length2, unsigned length3);
341  void EmbedFontFileCFF(PdfObject& descriptor, const bufferview& data);
342  void EmbedFontFileTrueType(PdfObject& descriptor, const bufferview& data);
343  void EmbedFontFileOpenType(PdfObject& descriptor, const bufferview& data);
344 
345  virtual bool tryMapCIDToGID(unsigned cid, unsigned& gid) const;
346 
350  double GetCIDLengthRaw(unsigned cid) const;
351 
352  void GetBoundingBox(PdfArray& arr) const;
353 
356  void FillDescriptor(PdfDictionary& dict) const;
357 
358  virtual PdfObject* getDescendantFontObject();
359 
362  virtual void initImported();
363 
364  virtual void embedFont();
365 
366  virtual void embedFontSubset();
367 
368 private:
369  PdfFont(const PdfFont& rhs) = delete;
370 
371 private:
374  void EmbedFont();
375 
380  void InitImported(bool wantEmbed, bool wantSubset);
381 
390  PdfCID AddSubsetGIDSafe(unsigned gid, const unicodeview& codePoints);
391 
396  PdfCharCode AddCharCodeSafe(unsigned gid, const unicodeview& codePoints);
397 
402  bool TryMapCIDToGID(unsigned cid, PdfGlyphAccess access, unsigned& gid) const;
403 
404 private:
405  bool tryConvertToGIDs(const std::string_view& utf8Str, PdfGlyphAccess access, std::vector<unsigned>& gids) const;
406  bool tryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
407 
408  void initBase(const PdfEncoding& encoding);
409 
410  double getStringLength(const std::vector<PdfCID>& cids, const PdfTextState& state) const;
411 
412  void embedFontFileData(PdfObject& descriptor, const PdfName& fontFileName,
413  const std::function<void(PdfDictionary& dict)>& dictWriter, const bufferview& data);
414 
415  static std::unique_ptr<PdfFont> createFontForType(PdfDocument& doc, const PdfFontMetricsConstPtr& metrics,
416  const PdfEncoding& encoding, PdfFontFileType type, bool preferNonCID);
417 
418  void initSpaceDescriptors();
419 
420 private:
421  std::string m_Name;
422  std::string m_SubsetPrefix;
423  bool m_EmbeddingEnabled;
424  bool m_IsEmbedded;
425  bool m_SubsettingEnabled;
426  UsedGIDsMap m_SubsetGIDs;
427  PdfCIDToGIDMapConstPtr m_cidToGidMap;
428  double m_WordSpacingLengthRaw;
429  double m_SpaceCharLengthRaw;
430 
431 protected:
432  PdfFontMetricsConstPtr m_Metrics;
433  std::unique_ptr<PdfEncoding> m_Encoding;
434  std::shared_ptr<PdfCharCodeMap> m_DynamicCIDMap;
435  std::shared_ptr<PdfCharCodeMap> m_DynamicToUnicodeMap;
436 };
437 
438 };
439 
440 #endif // PDF_FONT_H
441 
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
An interface for writing blocks of data to a data source.
Definition: OutputStream.h:18
PdfDocument is the core interface for working with PDF documents.
Definition: PdfDocument.h:108
A PdfEncoding is in PdfFont to transform a text string into a representation so that it can be displa...
Definition: PdfEncoding.h:55
A PdfFont that represents a CID-keyed font.
Definition: PdfFontCID.h:17
This class assists PdfDocument with caching font information.
Definition: PdfFontManager.h:54
This abstract class provides access to font metrics information.
Definition: PdfFontMetrics.h:33
This is a common base class for simple, non CID-keyed fonts like Type1, TrueType and Type3.
Definition: PdfFontSimple.h:20
Before you can draw text on a PDF document, you have to create a font object first.
Definition: PdfFont.h:49
static bool IsStandard14Font(const std::string_view &fontName, bool useAltNames, PdfStandard14FontType &stdFont)
Determine if font name is a Standard14 font.
const std::string & GetName() const
Get the base font name of this font.
Definition: PdfFont.h:331
static bool TryCreateFromObject(PdfObject &obj, std::unique_ptr< PdfFont > &font)
Create a new PdfFont from an existing font in a PDF file.
bool IsSubsettingEnabled() const
Check if this is a subsetting font.
Definition: PdfFont.h:304
const PdfFontMetrics & GetMetrics() const
Returns a handle to the fontmetrics object of this font.
Definition: PdfFont.h:324
const std::string & GetSubsetPrefix() const
Definition: PdfFont.h:312
static bool IsStandard14Font(const std::string_view &fontName, PdfStandard14FontType &stdFont)
Determine if font name is a Standard14 font.
const PdfEncoding & GetEncoding() const
Returns a reference to the fonts encoding.
Definition: PdfFont.h:317
This class represents a PDF indirect Object in memory.
Definition: PdfObject.h:35
A string that can be written to a PDF document.
Definition: PdfString.h:24
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
cspan< char32_t > unicodeview
Unicode code point view.
Definition: basetypes.h:27
PdfGlyphAccess
When accessing a glyph, there may be a difference in the glyph ID to retrieve the width or to index i...
Definition: PdfDeclarations.h:294
PdfFontFileType
Definition: PdfDeclarations.h:267
@ String
String datatype in PDF file. Strings have the form (Hallo World!) in PDF files.
@ Create
Create a new file or truncate existing one for writing/reading.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition: basetypes.h:19
PdfFontCreateFlags
Font init flags.
Definition: PdfDeclarations.h:311
@ None
No special settings.
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition: PdfFontMetrics.h:24
PdfFontType
Enum specifying the type of the font.
Definition: PdfDeclarations.h:257