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 PdfFontManager;
39 friend class PdfFontMetricsBase;
40 friend class PdfFontMetricsFreetype;
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
56 unsigned GetGlyphCount() const;
57
61 unsigned GetGlyphCount(PdfGlyphAccess access) const;
62
69 double GetGlyphWidth(unsigned gid) const;
70 bool TryGetGlyphWidth(unsigned gid, double& width) const;
71
78 double GetGlyphWidth(unsigned gid, PdfGlyphAccess access) const;
79 bool TryGetGlyphWidth(unsigned gid, PdfGlyphAccess access, double& width) const;
80
89 virtual void SubstituteGIDs(std::vector<unsigned>& gids,
90 std::vector<unsigned char>& backwardMap) const;
91
95 virtual bool HasUnicodeMapping() const = 0;
96
101 virtual bool TryGetGID(char32_t codePoint, unsigned& gid) const = 0;
102
106 virtual double GetLineSpacing() const = 0;
107
112 virtual double GetUnderlineThickness() const = 0;
113
118 virtual double GetUnderlinePosition() const = 0;
119
124 virtual double GetStrikeThroughPosition() const = 0;
125
130 virtual double GetStrikeThroughThickness() const = 0;
131
132 virtual PdfFontFileType GetFontFileType() const = 0;
133
134 bool HasFontFileData() const;
135
142 bufferview GetOrLoadFontFileData() const;
143
149 virtual const PdfObject* GetFontFileObject() const;
150
153 virtual unsigned GetFontFileLength1() const = 0;
154
157 virtual unsigned GetFontFileLength2() const = 0;
158
161 virtual unsigned GetFontFileLength3() const = 0;
162
168 virtual std::string_view GetFontName() const = 0;
169
173 virtual std::string_view GetFontNameRaw() const;
174
177 virtual std::string_view GetFontFamilyName() const = 0;
178
184 std::string_view GeFontFamilyNameSafe() const;
185
188 virtual unsigned char GetSubsetPrefixLength() const;
189
195 std::string_view GetPostScriptNameRough() const;
196
197 virtual PdfFontStretch GetFontStretch() const = 0;
198
202 unsigned GetWeight() const;
203 virtual int GetWeightRaw() const = 0;
204
205 virtual bool TryGetFlags(PdfFontDescriptorFlags& value) const = 0;
206 PdfFontDescriptorFlags GetFlags() const;
207
212 virtual bool TryGetBoundingBox(Corners& value) const = 0;
213 Corners GetBoundingBox() const;
214
219 virtual bool TryGetItalicAngle(double& value) const = 0;
220 double GetItalicAngle() const;
221
229 virtual bool TryGetAscent(double& value) const = 0;
230 double GetAscent() const;
231
240 virtual bool TryGetDescent(double& value) const = 0;
241 double GetDescent() const;
242
245 virtual bool TryGetCapHeight(double& value) const = 0;
246 double GetCapHeight() const;
247
250 virtual bool TryGetStemV(double& value) const = 0;
251 double GetStemV() const;
252
253 /* /Leading (optional, default 0)
254 */
255 double GetLeading() const;
256 virtual double GetLeadingRaw() const = 0;
257
262 double GetXHeight() const;
263 virtual double GetXHeightRaw() const = 0;
264
268 double GetStemH() const;
269 virtual double GetStemHRaw() const = 0;
270
273 double GetAvgWidth() const;
274 virtual double GetAvgWidthRaw() const = 0;
275
278 double GetMaxWidth() const;
279 virtual double GetMaxWidthRaw() const = 0;
280
283 double GetDefaultWidth() const;
284 virtual double GetDefaultWidthRaw() const = 0;
285
291 PdfFontStyle GetStyle() const;
292
293 virtual bool IsObjectLoaded() const;
294
295 bool IsStandard14FontMetrics() const;
296
297 virtual bool IsStandard14FontMetrics(PdfStandard14FontType& std14Font) const;
298
301 virtual const Matrix& GetMatrix() const;
302
305 bool IsType1Kind() const;
306
309 bool IsTrueTypeKind() const;
310
315 bool IsPdfSymbolic() const;
316
321 bool IsPdfNonSymbolic() const;
322
331 virtual std::unique_ptr<PdfCMapEncoding> CreateToUnicodeMap(const PdfEncodingLimits& limitHints) const;
332
336 bool TryGetImplicitEncoding(PdfEncodingMapConstPtr &encoding) const;
337
340 PdfCIDToGIDMapConstPtr GetCIDToGIDMap() const;
341
342public:
343 const std::string& GetFilePath() const { return m_FilePath; }
344 unsigned GetFaceIndex() const { return m_FaceIndex; }
345
346protected:
352 virtual std::string_view GetBaseFontName() const = 0;
353
354 virtual const PdfCIDToGIDMapConstPtr& getCIDToGIDMap() const;
355 virtual bool getIsBoldHint() const = 0;
356 virtual bool getIsItalicHint() const = 0;
357 virtual const datahandle& GetFontFileDataHandle() const = 0;
358 virtual FT_Face GetFaceHandle() const = 0;
359
360 virtual unsigned GetGlyphCountFontProgram() const;
361 virtual bool TryGetGlyphWidthFontProgram(unsigned gid, double& width) const;
362
363 bool HasParsedWidths() 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
385 void initFamilyFontNameSafe();
386 static PdfEncodingMapConstPtr getFontType1ImplicitEncoding(FT_Face face);
387
388private:
389 PdfFontMetrics(const PdfFontMetrics& rhs) = delete;
390 PdfFontMetrics& operator=(const PdfFontMetrics& rhs) = delete;
391
392private:
393 std::string m_FilePath;
394 std::string m_FamilyFontNameSafe;
395 GlyphMetricsListConstPtr m_ParsedWidths;
397 unsigned m_FaceIndex;
398};
399
400class PODOFO_API PdfFontMetricsBase : public PdfFontMetrics
401{
402 friend class PdfFontMetricsStandard14;
403 friend class PdfFontMetricsObject;
404
405private:
406 PdfFontMetricsBase();
407
408public:
409 ~PdfFontMetricsBase();
410
411protected:
412 const datahandle& GetFontFileDataHandle() const override final;
413 FT_Face GetFaceHandle() const override final;
414 virtual datahandle getFontFileDataHandle() const = 0;
415
416private:
417 bool m_dataInit;
418 bool m_faceInit;
419 datahandle m_Data;
420 FT_Face m_Face;
421};
422
423
424
425};
426
427#endif // PDF_FONT_METRICS_H
An unoriented rectangle defined by 2 points.
Definition Corners.h:20
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.
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:345