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
24using PdfFontMetricsConstPtr = std::shared_ptr<const PdfFontMetrics>;
25
30class PODOFO_API PdfFontMetrics
31{
32 friend class PdfFont;
33 friend class PdfFontSimple;
34 friend class PdfFontObject;
35 friend class PdfFontManager;
36 friend class PdfFontMetricsBase;
37 friend class PdfFontMetricsFreetype;
38 friend class PdfEncodingFactory;
39 friend class PdfEncodingMapSimple;
40 friend class PdfDifferenceEncoding;
41 PODOFO_PRIVATE_FRIEND(class FontTrueTypeSubset);
42
43private:
45
46public:
47 virtual ~PdfFontMetrics();
48
49 static std::unique_ptr<const PdfFontMetrics> Create(const std::string_view& filepath, unsigned faceIndex = 0);
50
51 static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex = 0);
52
55 unsigned GetGlyphCount() const;
56
60 unsigned GetGlyphCount(PdfGlyphAccess access) const;
61
67 double GetGlyphWidth(unsigned gid) const;
74 bool TryGetGlyphWidth(unsigned gid, double& width) const;
75
81 double GetGlyphWidth(unsigned gid, PdfGlyphAccess access) const;
89 bool TryGetGlyphWidth(unsigned gid, PdfGlyphAccess access, double& width) const;
90
97 virtual void SubstituteGIDs(std::vector<unsigned>& gids,
98 std::vector<unsigned char>& backwardMap) const;
99
102 virtual bool HasUnicodeMapping() const = 0;
103
107 virtual bool TryGetGID(char32_t codePoint, unsigned& gid) const = 0;
108
111 virtual double GetLineSpacing() const = 0;
112
116 virtual double GetUnderlineThickness() const = 0;
117
121 virtual double GetUnderlinePosition() const = 0;
122
126 virtual double GetStrikeThroughPosition() const = 0;
127
131 virtual double GetStrikeThroughThickness() const = 0;
132
133 virtual PdfFontFileType GetFontFileType() const = 0;
134
135 bool HasFontFileData() const;
136
142 bufferview GetOrLoadFontFileData() const;
143
148 virtual const PdfObject* GetFontFileObject() const;
149
151 virtual unsigned GetFontFileLength1() const = 0;
152
154 virtual unsigned GetFontFileLength2() const = 0;
155
157 virtual unsigned GetFontFileLength3() const = 0;
158
163 virtual std::string_view GetFontName() const = 0;
164
167 virtual std::string_view GetFontNameRaw() const;
168
170 virtual std::string_view GetFontFamilyName() const = 0;
171
176 std::string_view GetFontFamilyNameSafe() const;
177
178 [[deprecated("Use GetFontFamilyNameSafe() instead")]]
179 std::string_view GeFontFamilyNameSafe() const;
180
182 virtual unsigned char GetSubsetPrefixLength() const;
183
187 std::string_view GetPostScriptNameRough() const;
188
189 virtual PdfFontStretch GetFontStretch() const = 0;
190
193 unsigned GetWeight() const;
194 virtual int GetWeightRaw() const = 0;
195
196 virtual bool TryGetFlags(PdfFontDescriptorFlags& value) const = 0;
197 PdfFontDescriptorFlags GetFlags() const;
198
202 virtual bool TryGetBoundingBox(Corners& value) const = 0;
203 Corners GetBoundingBox() const;
204
208 virtual bool TryGetItalicAngle(double& value) const = 0;
209 double GetItalicAngle() const;
210
217 virtual bool TryGetAscent(double& value) const = 0;
218 double GetAscent() const;
219
227 virtual bool TryGetDescent(double& value) const = 0;
228 double GetDescent() const;
229
231 virtual bool TryGetCapHeight(double& value) const = 0;
232 double GetCapHeight() const;
233
235 virtual bool TryGetStemV(double& value) const = 0;
236 double GetStemV() const;
237
238 /* /Leading (optional, default 0)
239 */
240 double GetLeading() const;
241 virtual double GetLeadingRaw() const = 0;
242
246 double GetXHeight() const;
247 virtual double GetXHeightRaw() const = 0;
248
251 double GetStemH() const;
252 virtual double GetStemHRaw() const = 0;
253
255 double GetAvgWidth() const;
256 virtual double GetAvgWidthRaw() const = 0;
257
259 double GetMaxWidth() const;
260 virtual double GetMaxWidthRaw() const = 0;
261
263 double GetDefaultWidth() const;
264 virtual double GetDefaultWidthRaw() const = 0;
265
270 PdfFontStyle GetStyle() const;
271
272 virtual bool IsObjectLoaded() const;
273
274 bool IsStandard14FontMetrics() const;
275
276 virtual bool IsStandard14FontMetrics(PdfStandard14FontType& std14Font) const;
277
279 virtual const Matrix& GetMatrix() const;
280
282 bool IsType1Kind() const;
283
285 bool IsTrueTypeKind() const;
286
290 bool IsPdfSymbolic() const;
291
295 bool IsPdfNonSymbolic() const;
296
304 virtual std::unique_ptr<PdfCMapEncoding> CreateToUnicodeMap(const PdfEncodingLimits& limitHints) const;
305
306#ifdef PODOFO_3RDPARTY_INTEROP_ENABLED
307 virtual FT_Face GetFaceHandle() const = 0;
308#endif // PODOFO_3RDPARTY_INTEROP_ENABLED
309
310public:
311 const std::string& GetFilePath() const { return m_FilePath; }
312 unsigned GetFaceIndex() const { return m_FaceIndex; }
313
314protected:
315 virtual PdfFontType GetFontType() const;
316
321 virtual std::string_view GetBaseFontName() const = 0;
322
323 virtual bool getIsBoldHint() const = 0;
324 virtual bool getIsItalicHint() const = 0;
325 virtual const datahandle& GetFontFileDataHandle() const = 0;
326
327 virtual unsigned GetGlyphCountFontProgram() const;
328 virtual bool TryGetGlyphWidthFontProgram(unsigned gid, double& width) const;
329 virtual void ExportType3GlyphData(PdfDictionary& fontDict, cspan<std::string_view> glyphs) const;
330
331 bool HasParsedWidths() const;
332
333 unsigned GetParsedWidthsCount() const;
334
336 GlyphMetricsListConstPtr GetParsedWidths() const { return m_ParsedWidths; }
337
338 void SetParsedWidths(GlyphMetricsListConstPtr&& parsedWidths);
339
340private:
341 static std::unique_ptr<const PdfFontMetrics> CreateFromFile(const std::string_view& filepath, unsigned faceIndex,
343
344 static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex,
346
347 static std::unique_ptr<PdfFontMetrics> CreateFromFace(FT_Face face, std::unique_ptr<charbuff>&& buffer,
349
351 std::unique_ptr<const PdfFontMetrics> CreateMergedMetrics(bool skipNormalization) const;
352
355 PdfEncodingMapConstPtr GetDefaultEncoding(PdfCIDToGIDMapConstPtr& cidToGidMap) const;
356 PdfEncodingMapConstPtr GetDefaultEncoding() const;
357
359 PdfCIDToGIDMapConstPtr GetTrueTypeBuiltinCIDToGIDMap() const;
360
361 PdfEncodingMapConstPtr getFontType1BuiltInEncoding(FT_Face face) const;
362 void initFamilyFontNameSafe();
363 PdfEncodingMapConstPtr getDefaultEncoding(bool tryFetchCidToGidMap, PdfCIDToGIDMapConstPtr& cidToGidMap) const;
364
365private:
366 PdfFontMetrics(const PdfFontMetrics& rhs) = delete;
367 PdfFontMetrics& operator=(const PdfFontMetrics& rhs) = delete;
368
369private:
370 std::string m_FilePath;
371 std::string m_FamilyFontNameSafe;
372 GlyphMetricsListConstPtr m_ParsedWidths;
374 unsigned m_FaceIndex;
375};
376
377class PODOFO_API PdfFontMetricsBase : public PdfFontMetrics
378{
379 friend class PdfFontMetricsStandard14;
380 friend class PdfFontMetricsObject;
381
382private:
383 PdfFontMetricsBase();
384
385public:
386 ~PdfFontMetricsBase();
387
388protected:
389 const datahandle& GetFontFileDataHandle() const override final;
390 FT_Face GetFaceHandle() const override final;
391 virtual datahandle getFontFileDataHandle() const = 0;
392
393private:
394 bool m_dataInit;
395 bool m_faceInit;
396 datahandle m_Data;
397 FT_Face m_Face;
398};
399
400
401
402};
403
404#endif // PDF_FONT_METRICS_H
An unoriented rectangle defined by 2 points.
Definition Corners.h:16
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:77
PdfDifferenceEncoding is an encoding, which is based on either the fonts encoding or a predefined enc...
Definition PdfDifferenceEncoding.h:105
This factory creates a PdfEncoding from an existing object in the PDF.
Definition PdfEncodingFactory.h:16
PdfEncodingMap used by legacy encodings like PdfBuiltInEncoding or PdfDifferenceEncoding that can def...
Definition PdfEncodingMap.h:217
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:50
This abstract class provides access to font metrics information.
Definition PdfFontMetrics.h:31
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:336
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. "Times", if available.
This is a common base class for simple, non CID-keyed fonts like Type1, TrueType and Type3.
Definition PdfFontSimple.h:17
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:42
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
A const data provider that can hold a view to a static segments or a shared buffer.
Definition basetypes.h:135
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:301
PdfFontFileType
Definition PdfDeclarations.h:276
@ Create
Create a new file or truncate existing one for writing/reading.
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
PdfFontStyle
Font style flags used during searches.
Definition PdfDeclarations.h:289
PdfFontDescriptorFlags
Enum for the font descriptor flags.
Definition PdfDeclarations.h:233
std::shared_ptr< const PdfEncodingMap > PdfEncodingMapConstPtr
Convenience typedef for a const /Encoding map entry shared ptr.
Definition PdfEncodingMap.h:311