PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfFontMetrics.h
1// SPDX-FileCopyrightText: 2005 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_METRICS_H
6#define PDF_FONT_METRICS_H
7
8#include "PdfString.h"
9#include "PdfCMapEncoding.h"
10#include "PdfCIDToGIDMap.h"
11#include <podofo/auxiliary/Matrix.h>
12#include <podofo/auxiliary/Corners.h>
13
14FORWARD_DECLARE_FREETYPE();
15
16namespace PoDoFo {
17
18class PdfFontMetrics;
19
20// NOTE: Underlying type may change in the future
21using GlyphMetricsListConstPtr = std::shared_ptr<const std::vector<double>>;
22
25using PdfFontMetricsConstPtr = std::shared_ptr<const PdfFontMetrics>;
26
33class PODOFO_API PdfFontMetrics
34{
35 friend class PdfFont;
36 friend class PdfFontSimple;
37 friend class PdfFontObject;
38 friend class PdfFontManager;
39 friend class PdfFontMetricsBase;
40 friend class PdfFontMetricsFreetype;
41 friend class PdfEncodingFactory;
42 friend class PdfEncodingMapSimple;
43 friend class PdfDifferenceEncoding;
44 PODOFO_PRIVATE_FRIEND(class FontTrueTypeSubset);
45
46private:
48
49public:
50 virtual ~PdfFontMetrics();
51
52 static std::unique_ptr<const PdfFontMetrics> Create(const std::string_view& filepath, unsigned faceIndex = 0);
53
54 static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex = 0);
55
59 unsigned GetGlyphCount() const;
60
64 unsigned GetGlyphCount(PdfGlyphAccess access) const;
65
72 double GetGlyphWidth(unsigned gid) const;
73 bool TryGetGlyphWidth(unsigned gid, double& width) const;
74
81 double GetGlyphWidth(unsigned gid, PdfGlyphAccess access) const;
82 bool TryGetGlyphWidth(unsigned gid, PdfGlyphAccess access, double& width) const;
83
92 virtual void SubstituteGIDs(std::vector<unsigned>& gids,
93 std::vector<unsigned char>& backwardMap) const;
94
98 virtual bool HasUnicodeMapping() const = 0;
99
104 virtual bool TryGetGID(char32_t codePoint, unsigned& gid) const = 0;
105
109 virtual double GetLineSpacing() const = 0;
110
115 virtual double GetUnderlineThickness() const = 0;
116
121 virtual double GetUnderlinePosition() const = 0;
122
127 virtual double GetStrikeThroughPosition() const = 0;
128
133 virtual double GetStrikeThroughThickness() const = 0;
134
135 virtual PdfFontFileType GetFontFileType() const = 0;
136
137 bool HasFontFileData() const;
138
145 bufferview GetOrLoadFontFileData() const;
146
152 virtual const PdfObject* GetFontFileObject() const;
153
156 virtual unsigned GetFontFileLength1() const = 0;
157
160 virtual unsigned GetFontFileLength2() const = 0;
161
164 virtual unsigned GetFontFileLength3() const = 0;
165
171 virtual std::string_view GetFontName() const = 0;
172
176 virtual std::string_view GetFontNameRaw() const;
177
180 virtual std::string_view GetFontFamilyName() const = 0;
181
187 std::string_view GeFontFamilyNameSafe() const;
188
191 virtual unsigned char GetSubsetPrefixLength() const;
192
198 std::string_view GetPostScriptNameRough() const;
199
200 virtual PdfFontStretch GetFontStretch() const = 0;
201
205 unsigned GetWeight() const;
206 virtual int GetWeightRaw() const = 0;
207
208 virtual bool TryGetFlags(PdfFontDescriptorFlags& value) const = 0;
209 PdfFontDescriptorFlags GetFlags() const;
210
215 virtual bool TryGetBoundingBox(Corners& value) const = 0;
216 Corners GetBoundingBox() const;
217
222 virtual bool TryGetItalicAngle(double& value) const = 0;
223 double GetItalicAngle() const;
224
232 virtual bool TryGetAscent(double& value) const = 0;
233 double GetAscent() const;
234
243 virtual bool TryGetDescent(double& value) const = 0;
244 double GetDescent() const;
245
248 virtual bool TryGetCapHeight(double& value) const = 0;
249 double GetCapHeight() const;
250
253 virtual bool TryGetStemV(double& value) const = 0;
254 double GetStemV() const;
255
256 /* /Leading (optional, default 0)
257 */
258 double GetLeading() const;
259 virtual double GetLeadingRaw() const = 0;
260
265 double GetXHeight() const;
266 virtual double GetXHeightRaw() const = 0;
267
271 double GetStemH() const;
272 virtual double GetStemHRaw() const = 0;
273
276 double GetAvgWidth() const;
277 virtual double GetAvgWidthRaw() const = 0;
278
281 double GetMaxWidth() const;
282 virtual double GetMaxWidthRaw() const = 0;
283
286 double GetDefaultWidth() const;
287 virtual double GetDefaultWidthRaw() const = 0;
288
294 PdfFontStyle GetStyle() const;
295
296 virtual bool IsObjectLoaded() const;
297
298 bool IsStandard14FontMetrics() const;
299
300 virtual bool IsStandard14FontMetrics(PdfStandard14FontType& std14Font) const;
301
304 virtual const Matrix& GetMatrix() const;
305
308 bool IsType1Kind() const;
309
312 bool IsTrueTypeKind() const;
313
318 bool IsPdfSymbolic() const;
319
324 bool IsPdfNonSymbolic() const;
325
334 virtual std::unique_ptr<PdfCMapEncoding> CreateToUnicodeMap(const PdfEncodingLimits& limitHints) const;
335
336#ifdef PODOFO_3RDPARTY_INTEROP_ENABLED
337 virtual FT_Face GetFaceHandle() const = 0;
338#endif // PODOFO_3RDPARTY_INTEROP_ENABLED
339
340public:
341 const std::string& GetFilePath() const { return m_FilePath; }
342 unsigned GetFaceIndex() const { return m_FaceIndex; }
343
344protected:
345 virtual PdfFontType GetFontType() const;
346
352 virtual std::string_view GetBaseFontName() const = 0;
353
354 virtual bool getIsBoldHint() const = 0;
355 virtual bool getIsItalicHint() const = 0;
356 virtual const datahandle& GetFontFileDataHandle() const = 0;
357
358 virtual unsigned GetGlyphCountFontProgram() const;
359 virtual bool TryGetGlyphWidthFontProgram(unsigned gid, double& width) const;
360 virtual void ExportType3GlyphData(PdfDictionary& fontDict, cspan<std::string_view> glyphs) const;
361
362 bool HasParsedWidths() const;
363
364 unsigned GetParsedWidthsCount() const;
365
368 GlyphMetricsListConstPtr GetParsedWidths() const { return m_ParsedWidths; }
369
370 void SetParsedWidths(GlyphMetricsListConstPtr&& parsedWidths);
371
372private:
373 static std::unique_ptr<const PdfFontMetrics> CreateFromFile(const std::string_view& filepath, unsigned faceIndex,
375
376 static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex,
378
379 static std::unique_ptr<PdfFontMetrics> CreateFromFace(FT_Face face, std::unique_ptr<charbuff>&& buffer,
381
384 std::unique_ptr<const PdfFontMetrics> CreateMergedMetrics(bool skipNormalization) const;
385
389 PdfEncodingMapConstPtr GetDefaultEncoding(PdfCIDToGIDMapConstPtr& cidToGidMap) const;
390 PdfEncodingMapConstPtr GetDefaultEncoding() const;
391
394 PdfCIDToGIDMapConstPtr GetTrueTypeBuiltinCIDToGIDMap() const;
395
396 PdfEncodingMapConstPtr getFontType1BuiltInEncoding(FT_Face face) const;
397 void initFamilyFontNameSafe();
398 PdfEncodingMapConstPtr getDefaultEncoding(bool tryFetchCidToGidMap, PdfCIDToGIDMapConstPtr& cidToGidMap) const;
399
400private:
401 PdfFontMetrics(const PdfFontMetrics& rhs) = delete;
402 PdfFontMetrics& operator=(const PdfFontMetrics& rhs) = delete;
403
404private:
405 std::string m_FilePath;
406 std::string m_FamilyFontNameSafe;
407 GlyphMetricsListConstPtr m_ParsedWidths;
409 unsigned m_FaceIndex;
410};
411
412class PODOFO_API PdfFontMetricsBase : public PdfFontMetrics
413{
414 friend class PdfFontMetricsStandard14;
415 friend class PdfFontMetricsObject;
416
417private:
418 PdfFontMetricsBase();
419
420public:
421 ~PdfFontMetricsBase();
422
423protected:
424 const datahandle& GetFontFileDataHandle() const override final;
425 FT_Face GetFaceHandle() const override final;
426 virtual datahandle getFontFileDataHandle() const = 0;
427
428private:
429 bool m_dataInit;
430 bool m_faceInit;
431 datahandle m_Data;
432 FT_Face m_Face;
433};
434
435
436
437};
438
439#endif // PDF_FONT_METRICS_H
An unoriented rectangle defined by 2 points.
Definition Corners.h:17
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:80
PdfDifferenceEncoding is an encoding, which is based on either the fonts encoding or a predefined enc...
Definition PdfDifferenceEncoding.h:113
This factory creates a PdfEncoding from an existing object in the PDF.
Definition PdfEncodingFactory.h:17
PdfEncodingMap used by legacy encodings like PdfBuiltInEncoding or PdfDifferenceEncoding that can def...
Definition PdfEncodingMap.h:252
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:52
This abstract class provides access to font metrics information.
Definition PdfFontMetrics.h:34
virtual bool TryGetItalicAngle(double &value) const =0
Get the italic angle of this font.
virtual bool TryGetBoundingBox(Corners &value) const =0
Create the bounding box vector in PDF units.
virtual bool TryGetStemV(double &value) const =0
The thickness, measured horizontally, of the dominant vertical stems of glyphs in the font.
virtual unsigned GetFontFileLength3() const =0
Get /Length3 value for the font file, if available.
virtual double GetUnderlinePosition() const =0
Return the position of the underline for the current font size in PDF units.
GlyphMetricsListConstPtr GetParsedWidths() const
Retrieve the parsed width from a /W or /Widths entry, if available.
Definition PdfFontMetrics.h:368
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 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 unsigned GetFontFileLength1() const =0
Get /Length1 value for the font file, if available.
virtual bool TryGetCapHeight(double &value) const =0
The vertical coordinate of the top of flat capital letters, measured from the baseline.
virtual bool TryGetDescent(double &value) const =0
Get the descent of this font in PDF units for the current font size.
virtual double GetStrikeThroughThickness() const =0
Get the width of the strikethrough for the current font size in PDF units.
virtual bool TryGetAscent(double &value) const =0
Get the ascent of this font in PDF units for the current font size.
virtual std::string_view GetFontName() const =0
Get the actual /FontName, eg.
virtual std::string_view GetFontFamilyName() const =0
Get the actual /FontFamily, eg.
This is a common base class for simple, non CID-keyed fonts like Type1, TrueType and Type3.
Definition PdfFontSimple.h:18
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:43
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:33
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
A const data provider that can hold a view to a static segments or a shared buffer.
Definition basetypes.h:141
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfGlyphAccess
When accessing a glyph, there may be a difference in the glyph ID to retrieve the widths or to index ...
Definition PdfDeclarations.h:321
PdfFontFileType
Definition PdfDeclarations.h:294
@ Create
Create a new file or truncate existing one for writing/reading.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:16
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition PdfFontMetrics.h:25
PdfFontStyle
Font style flags used during searches.
Definition PdfDeclarations.h:308
PdfFontDescriptorFlags
Enum for the font descriptor flags.
Definition PdfDeclarations.h:250
std::shared_ptr< const PdfEncodingMap > PdfEncodingMapConstPtr
Convenience typedef for a const /Encoding map entry shared ptr.
Definition PdfEncodingMap.h:353