PoDoFo  1.0.0-dev
PdfFontMetrics.h
1 
7 #ifndef PDF_FONT_METRICS_H
8 #define PDF_FONT_METRICS_H
9 
10 #include "PdfDeclarations.h"
11 
12 #include "PdfString.h"
13 #include "PdfCMapEncoding.h"
14 #include "PdfCIDToGIDMap.h"
15 
16 FORWARD_DECLARE_FREETYPE();
17 
18 namespace PoDoFo {
19 
20 class PdfFontMetrics;
21 
24 using PdfFontMetricsConstPtr = std::shared_ptr<const PdfFontMetrics>;
25 
32 class PODOFO_API PdfFontMetrics
33 {
34  friend class PdfFont;
35  friend class PdfFontManager;
36  friend class PdfFontMetricsBase;
37  friend class PdfFontMetricsFreetype;
38 
39 private:
41 
42 public:
43  virtual ~PdfFontMetrics();
44 
45  static std::unique_ptr<const PdfFontMetrics> Create(const std::string_view& filepath, unsigned faceIndex = 0);
46 
47  static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex = 0);
48 
49  virtual unsigned GetGlyphCount() const = 0;
50 
56  double GetGlyphWidth(unsigned gid) const;
57  virtual bool TryGetGlyphWidth(unsigned gid, double& width) const = 0;
58 
67  virtual void SubstituteGIDs(std::vector<unsigned>& gids,
68  std::vector<unsigned char>& backwardMap) const;
69 
73  virtual bool HasUnicodeMapping() const = 0;
74 
79  virtual bool TryGetGID(char32_t codePoint, unsigned& gid) const = 0;
80 
84  virtual double GetLineSpacing() const = 0;
85 
90  virtual double GetUnderlineThickness() const = 0;
91 
96  virtual double GetUnderlinePosition() const = 0;
97 
102  virtual double GetStrikeThroughPosition() const = 0;
103 
108  virtual double GetStrikeThroughThickness() const = 0;
109 
110  virtual PdfFontFileType GetFontFileType() const = 0;
111 
112  bool HasFontFileData() const;
113 
120  bufferview GetOrLoadFontFileData() const;
121 
127  virtual const PdfObject* GetFontFileObject() const;
128 
131  virtual unsigned GetFontFileLength1() const = 0;
132 
135  virtual unsigned GetFontFileLength2() const = 0;
136 
139  virtual unsigned GetFontFileLength3() const = 0;
140 
146  virtual std::string_view GetFontName() const = 0;
147 
151  virtual std::string_view GetFontNameRaw() const;
152 
155  virtual std::string_view GetFontFamilyName() const = 0;
156 
162  std::string_view GeFontFamilyNameSafe() const;
163 
169  virtual std::string_view GetPostScriptNameApprox() const;
170 
171  virtual PdfFontStretch GetFontStretch() const = 0;
172 
176  unsigned GetWeight() const;
177  virtual int GetWeightRaw() const = 0;
178 
179  virtual PdfFontDescriptorFlags GetFlags() const = 0;
180 
185  virtual void GetBoundingBox(std::vector<double>& bbox) const = 0;
186 
191  virtual double GetItalicAngle() const = 0;
192 
200  virtual double GetAscent() const = 0;
201 
210  virtual double GetDescent() const = 0;
211 
212  /* /Leading (optional, default 0)
213  */
214  double GetLeading() const;
215  virtual double GetLeadingRaw() const = 0;
216 
219  virtual double GetCapHeight() const = 0;
220 
225  double GetXHeight() const;
226  virtual double GetXHeightRaw() const = 0;
227 
230  virtual double GetStemV() const = 0;
231 
235  double GetStemH() const;
236  virtual double GetStemHRaw() const = 0;
237 
240  double GetAvgWidth() const;
241  virtual double GetAvgWidthRaw() const = 0;
242 
245  double GetMaxWidth() const;
246  virtual double GetMaxWidthRaw() const = 0;
247 
250  double GetDefaultWidth() const;
251  virtual double GetDefaultWidthRaw() const = 0;
252 
258  PdfFontStyle GetStyle() const;
259 
260  virtual bool IsObjectLoaded() const;
261 
262  bool IsStandard14FontMetrics() const;
263 
264  virtual bool IsStandard14FontMetrics(PdfStandard14FontType& std14Font) const;
265 
268  virtual const Matrix2D& GetMatrix() const;
269 
272  bool IsType1Kind() const;
273 
276  bool IsTrueTypeKind() const;
277 
282  bool IsPdfSymbolic() const;
283 
288  bool IsPdfNonSymbolic() const;
289 
298  virtual std::unique_ptr<PdfCMapEncoding> CreateToUnicodeMap(const PdfEncodingLimits& limitHints) const;
299 
303  bool TryGetImplicitEncoding(PdfEncodingMapConstPtr &encoding) const;
304 
305  PdfCIDToGIDMapConstPtr GetCIDToGIDMap() const;
306 
307 public:
308  const std::string& GetFilePath() const { return m_FilePath; }
309  unsigned GetFaceIndex() const { return m_FaceIndex; }
310 
311 protected:
317  virtual std::string_view GetBaseFontName() const = 0;
318 
319  virtual const PdfCIDToGIDMapConstPtr& getCIDToGIDMap() const;
320  virtual bool getIsBoldHint() const = 0;
321  virtual bool getIsItalicHint() const = 0;
322  virtual const datahandle& GetFontFileDataHandle() const = 0;
323  virtual FT_Face GetFaceHandle() const = 0;
324 
325 private:
326  void initFamilyFontNameSafe();
327  static PdfEncodingMapConstPtr getFontType1ImplicitEncoding(FT_Face face);
328 
329 private:
330  PdfFontMetrics(const PdfFontMetrics& rhs) = delete;
331  PdfFontMetrics& operator=(const PdfFontMetrics& rhs) = delete;
332 
333 private:
334  std::string m_FilePath;
335  nullable<PdfFontStyle> m_Style;
336  std::string m_FamilyFontNameSafe;
337  unsigned m_FaceIndex;
338 };
339 
340 class PODOFO_API PdfFontMetricsBase : public PdfFontMetrics
341 {
342  friend class PdfFontMetricsStandard14;
343  friend class PdfFontMetricsObject;
344 
345 private:
346  PdfFontMetricsBase();
347 
348 public:
349  ~PdfFontMetricsBase();
350 
351 protected:
352  const datahandle& GetFontFileDataHandle() const override final;
353  FT_Face GetFaceHandle() const override final;
354  virtual datahandle getFontFileDataHandle() const = 0;
355 
356 private:
357  bool m_dataInit;
358  datahandle m_Data;
359  bool m_faceInit;
360  FT_Face m_Face;
361 };
362 
363 
364 
365 };
366 
367 #endif // PDF_FONT_METRICS_H
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
This class assists PdfDocument with caching font information.
Definition: PdfFontManager.h:54
This is the main class to handle the Standard14 metric data.
Definition: PdfFontMetricsStandard14.h:23
This abstract class provides access to font metrics information.
Definition: PdfFontMetrics.h:33
virtual double GetItalicAngle() const =0
Get the italic angle of this font.
virtual unsigned GetFontFileLength3() const =0
Get /Length3 value for the font file, if available.
virtual double GetDescent() const =0
Get the descent of this font in PDF units for the current font size.
virtual double GetUnderlinePosition() const =0
Return the position of the underline for the current font size in PDF units.
virtual bool HasUnicodeMapping() const =0
Determines if the metrics has a valid Unicode code point to gid map.
virtual double GetStrikeThroughPosition() const =0
Return the position of the strikethrough for the current font size in PDF units.
virtual double GetAscent() const =0
Get the ascent of this font in PDF units for the current font size.
virtual double GetUnderlineThickness() const =0
Get the width of the underline for the current font size in PDF units.
virtual bool TryGetGID(char32_t codePoint, unsigned &gid) const =0
Try to retrieve the mapped gid from Unicode code point.
virtual unsigned GetFontFileLength2() const =0
Get /Length2 value for the font file, if available.
virtual std::string_view GetBaseFontName() const =0
Get a semantical base name for the font that can be used to compose the final name,...
virtual double GetLineSpacing() const =0
Retrieve the line spacing for this font.
virtual double GetCapHeight() const =0
The vertical coordinate of the top of flat capital letters, measured from the baseline.
virtual unsigned GetFontFileLength1() const =0
Get /Length1 value for the font file, if available.
virtual double GetStrikeThroughThickness() const =0
Get the width of the strikethrough for the current font size in PDF units.
virtual std::string_view GetFontName() const =0
Get the actual /FontName, eg.
virtual double GetStemV() const =0
The thickness, measured horizontally, of the dominant vertical stems of glyphs in the font.
virtual void GetBoundingBox(std::vector< double > &bbox) const =0
Create the bounding box vector in PDF units.
virtual std::string_view GetFontFamilyName() const =0
Get the actual /FontFamily, eg.
Before you can draw text on a PDF document, you have to create a font object first.
Definition: PdfFont.h:49
This class represents a PDF indirect Object in memory.
Definition: PdfObject.h:35
A const data provider that can hold a view to a static segments or a shared buffer.
Definition: basetypes.h:144
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
PdfFontFileType
Definition: PdfDeclarations.h:267
@ 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
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
PdfFontDescriptorFlags
Enum for the font descriptor flags.
Definition: PdfDeclarations.h:223
std::shared_ptr< const PdfEncodingMap > PdfEncodingMapConstPtr
Convenience typedef for a const /Encoding map entry shared ptr.
Definition: PdfEncodingMap.h:343