PoDoFo 1.0.0-dev
Loading...
Searching...
No Matches
PdfFontMetrics.h
1
7#ifndef PDF_FONT_METRICS_H
8#define PDF_FONT_METRICS_H
9
10#include "PdfString.h"
11#include "PdfCMapEncoding.h"
12#include "PdfCIDToGIDMap.h"
13#include <podofo/auxiliary/Matrix.h>
14#include <podofo/auxiliary/Corners.h>
15
16FORWARD_DECLARE_FREETYPE();
17
18namespace PoDoFo {
19
20class PdfFontMetrics;
21
22// NOTE: Underlying type may may change in the future
23using GlyphMetricsListConstPtr = std::shared_ptr<const std::vector<double>>;
24
27using PdfFontMetricsConstPtr = std::shared_ptr<const PdfFontMetrics>;
28
35class PODOFO_API PdfFontMetrics
36{
37 friend class PdfFont;
38 friend class PdfFontSimple;
39 friend class PdfFontObject;
40 friend class PdfFontManager;
41 friend class PdfFontMetricsBase;
42 friend class PdfFontMetricsFreetype;
43 friend class PdfEncodingFactory;
44 friend class PdfEncodingMapSimple;
45 friend class PdfDifferenceEncoding;
46 PODOFO_PRIVATE_FRIEND(class FontTrueTypeSubset);
47
48private:
50
51public:
52 virtual ~PdfFontMetrics();
53
54 static std::unique_ptr<const PdfFontMetrics> Create(const std::string_view& filepath, unsigned faceIndex = 0);
55
56 static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex = 0);
57
61 unsigned GetGlyphCount() const;
62
66 unsigned GetGlyphCount(PdfGlyphAccess access) const;
67
74 double GetGlyphWidth(unsigned gid) const;
75 bool TryGetGlyphWidth(unsigned gid, double& width) const;
76
83 double GetGlyphWidth(unsigned gid, PdfGlyphAccess access) const;
84 bool TryGetGlyphWidth(unsigned gid, PdfGlyphAccess access, double& width) const;
85
94 virtual void SubstituteGIDs(std::vector<unsigned>& gids,
95 std::vector<unsigned char>& backwardMap) const;
96
100 virtual bool HasUnicodeMapping() const = 0;
101
106 virtual bool TryGetGID(char32_t codePoint, unsigned& gid) const = 0;
107
111 virtual double GetLineSpacing() const = 0;
112
117 virtual double GetUnderlineThickness() const = 0;
118
123 virtual double GetUnderlinePosition() const = 0;
124
129 virtual double GetStrikeThroughPosition() const = 0;
130
135 virtual double GetStrikeThroughThickness() const = 0;
136
137 virtual PdfFontFileType GetFontFileType() const = 0;
138
139 bool HasFontFileData() const;
140
147 bufferview GetOrLoadFontFileData() const;
148
154 virtual const PdfObject* GetFontFileObject() const;
155
158 virtual unsigned GetFontFileLength1() const = 0;
159
162 virtual unsigned GetFontFileLength2() const = 0;
163
166 virtual unsigned GetFontFileLength3() const = 0;
167
173 virtual std::string_view GetFontName() const = 0;
174
178 virtual std::string_view GetFontNameRaw() const;
179
182 virtual std::string_view GetFontFamilyName() const = 0;
183
189 std::string_view GeFontFamilyNameSafe() const;
190
193 virtual unsigned char GetSubsetPrefixLength() const;
194
200 std::string_view GetPostScriptNameRough() const;
201
202 virtual PdfFontStretch GetFontStretch() const = 0;
203
207 unsigned GetWeight() const;
208 virtual int GetWeightRaw() const = 0;
209
210 virtual bool TryGetFlags(PdfFontDescriptorFlags& value) const = 0;
211 PdfFontDescriptorFlags GetFlags() const;
212
217 virtual bool TryGetBoundingBox(Corners& value) const = 0;
218 Corners GetBoundingBox() const;
219
224 virtual bool TryGetItalicAngle(double& value) const = 0;
225 double GetItalicAngle() const;
226
234 virtual bool TryGetAscent(double& value) const = 0;
235 double GetAscent() const;
236
245 virtual bool TryGetDescent(double& value) const = 0;
246 double GetDescent() const;
247
250 virtual bool TryGetCapHeight(double& value) const = 0;
251 double GetCapHeight() const;
252
255 virtual bool TryGetStemV(double& value) const = 0;
256 double GetStemV() const;
257
258 /* /Leading (optional, default 0)
259 */
260 double GetLeading() const;
261 virtual double GetLeadingRaw() const = 0;
262
267 double GetXHeight() const;
268 virtual double GetXHeightRaw() const = 0;
269
273 double GetStemH() const;
274 virtual double GetStemHRaw() const = 0;
275
278 double GetAvgWidth() const;
279 virtual double GetAvgWidthRaw() const = 0;
280
283 double GetMaxWidth() const;
284 virtual double GetMaxWidthRaw() const = 0;
285
288 double GetDefaultWidth() const;
289 virtual double GetDefaultWidthRaw() const = 0;
290
296 PdfFontStyle GetStyle() const;
297
298 virtual bool IsObjectLoaded() const;
299
300 bool IsStandard14FontMetrics() const;
301
302 virtual bool IsStandard14FontMetrics(PdfStandard14FontType& std14Font) const;
303
306 virtual const Matrix& GetMatrix() const;
307
310 bool IsType1Kind() const;
311
314 bool IsTrueTypeKind() const;
315
320 bool IsPdfSymbolic() const;
321
326 bool IsPdfNonSymbolic() const;
327
336 virtual std::unique_ptr<PdfCMapEncoding> CreateToUnicodeMap(const PdfEncodingLimits& limitHints) const;
337
338public:
339 const std::string& GetFilePath() const { return m_FilePath; }
340 unsigned GetFaceIndex() const { return m_FaceIndex; }
341
342protected:
343 virtual PdfFontType GetFontType() const;
344
350 virtual std::string_view GetBaseFontName() const = 0;
351
352 virtual bool getIsBoldHint() const = 0;
353 virtual bool getIsItalicHint() const = 0;
354 virtual const datahandle& GetFontFileDataHandle() const = 0;
355 virtual FT_Face GetFaceHandle() const = 0;
356
357 virtual unsigned GetGlyphCountFontProgram() const;
358 virtual bool TryGetGlyphWidthFontProgram(unsigned gid, double& width) const;
359 virtual void ExportType3GlyphData(PdfDictionary& fontDict, cspan<std::string_view> glyphs) const;
360
361 bool HasParsedWidths() const;
362
363 unsigned GetParsedWidthsCount() const;
364
367 GlyphMetricsListConstPtr GetParsedWidths() const { return m_ParsedWidths; }
368
369 void SetParsedWidths(GlyphMetricsListConstPtr&& parsedWidths);
370
371private:
372 static std::unique_ptr<const PdfFontMetrics> CreateFromFile(const std::string_view& filepath, unsigned faceIndex,
374
375 static std::unique_ptr<const PdfFontMetrics> CreateFromBuffer(const bufferview& buffer, unsigned faceIndex,
377
378 static std::unique_ptr<PdfFontMetrics> CreateFromFace(FT_Face face, std::unique_ptr<charbuff>&& buffer,
380
383 std::unique_ptr<const PdfFontMetrics> CreateMergedMetrics(bool skipNormalization) const;
384
388 PdfEncodingMapConstPtr GetDefaultEncoding(PdfCIDToGIDMapConstPtr& cidToGidMap) const;
389 PdfEncodingMapConstPtr GetDefaultEncoding() const;
390
393 PdfCIDToGIDMapConstPtr GetTrueTypeBuiltinCIDToGIDMap() const;
394
395 PdfEncodingMapConstPtr getFontType1BuiltInEncoding(FT_Face face) const;
396 void initFamilyFontNameSafe();
397 PdfEncodingMapConstPtr getDefaultEncoding(bool tryFetchCidToGidMap, PdfCIDToGIDMapConstPtr& cidToGidMap) const;
398
399private:
400 PdfFontMetrics(const PdfFontMetrics& rhs) = delete;
401 PdfFontMetrics& operator=(const PdfFontMetrics& rhs) = delete;
402
403private:
404 std::string m_FilePath;
405 std::string m_FamilyFontNameSafe;
406 GlyphMetricsListConstPtr m_ParsedWidths;
408 unsigned m_FaceIndex;
409};
410
411class PODOFO_API PdfFontMetricsBase : public PdfFontMetrics
412{
413 friend class PdfFontMetricsStandard14;
414 friend class PdfFontMetricsObject;
415
416private:
417 PdfFontMetricsBase();
418
419public:
420 ~PdfFontMetricsBase();
421
422protected:
423 const datahandle& GetFontFileDataHandle() const override final;
424 FT_Face GetFaceHandle() const override final;
425 virtual datahandle getFontFileDataHandle() const = 0;
426
427private:
428 bool m_dataInit;
429 bool m_faceInit;
430 datahandle m_Data;
431 FT_Face m_Face;
432};
433
434
435
436};
437
438#endif // PDF_FONT_METRICS_H
An unoriented rectangle defined by 2 points.
Definition Corners.h:20
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
PdfDifferenceEncoding is an encoding, which is based on either the fonts encoding or a predefined enc...
Definition PdfDifferenceEncoding.h:118
This factory creates a PdfEncoding from an existing object in the PDF.
Definition PdfEncodingFactory.h:20
PdfEncodingMap used by legacy encodings like PdfBuiltInEncoding or PdfDifferenceEncoding that can def...
Definition PdfEncodingMap.h:254
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:54
This abstract class provides access to font metrics information.
Definition PdfFontMetrics.h:36
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:367
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:20
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:45
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
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
PdfGlyphAccess
When accessing a glyph, there may be a difference in the glyph ID to retrieve the widths or to index ...
Definition PdfDeclarations.h:323
PdfFontFileType
Definition PdfDeclarations.h:296
@ 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:27
PdfFontStyle
Font style flags used during searches.
Definition PdfDeclarations.h:310
PdfFontDescriptorFlags
Enum for the font descriptor flags.
Definition PdfDeclarations.h:252
std::shared_ptr< const PdfEncodingMap > PdfEncodingMapConstPtr
Convenience typedef for a const /Encoding map entry shared ptr.
Definition PdfEncodingMap.h:355