PoDoFo 1.1.0
Loading...
Searching...
No Matches
PdfFont.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_H
6#define PDF_FONT_H
7
8#include "PdfDeclarations.h"
9
10#include "PdfTextState.h"
11#include "PdfName.h"
12#include "PdfEncoding.h"
13#include "PdfElement.h"
14#include "PdfFontMetrics.h"
15
16namespace PoDoFo {
17
18class PdfCharCodeMap;
19
20struct PODOFO_API PdfFontCreateParams final
21{
22 PdfEncoding Encoding;
23 PdfFontCreateFlags Flags = PdfFontCreateFlags::None;
24};
25
26struct PODOFO_API PdfSplittedString final
27{
28 PdfString String;
29 bool IsSeparator = false;
30};
31
42class PODOFO_API PdfFont : public PdfDictionaryElement
43{
44 friend class PdfFontSimple;
45 friend class PdfFontCID;
46 friend class PdfFontObject;
47 friend class PdfEncoding;
48 friend class PdfFontManager;
49 friend class PdfEncodingMapBase;
50 friend class PdfEncodingMapSimple;
51 friend class PdfFontBuiltinType1Encoding;
52
53private:
64 const PdfEncoding& encoding);
65
70 const PdfEncoding& encoding);
71
72public:
73 virtual ~PdfFont();
74
75public:
82 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfFont>& font);
83 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfFont>& font);
84
85private:
99 static std::unique_ptr<PdfFont> Create(PdfDocument& doc, PdfFontMetricsConstPtr&& metrics,
100 const PdfFontCreateParams& createParams, bool isProxy = false);
101
111 static std::unique_ptr<PdfFont> CreateStandard14(PdfDocument& doc, PdfStandard14FontType std14Font,
112 const PdfFontCreateParams& createParams);
113
114public:
119 bool TryCreateProxyFont(PdfFont*& proxyFont) const;
120 bool TryCreateProxyFont(PdfFontCreateFlags initFlags, PdfFont*& proxyFont) const;
121
131 void WriteStringToStream(OutputStream& stream, const std::string_view& str) const;
132
139 unsigned GetGID(char32_t codePoint, PdfGlyphAccess access) const;
140 bool TryGetGID(char32_t codePoint, PdfGlyphAccess access, unsigned& gid) const;
141
148 double GetStringLength(const std::string_view& str, const PdfTextState& state) const;
149
154 bool TryGetStringLength(const std::string_view& str, const PdfTextState& state, double& width) const;
155
162 double GetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state) const;
163
167 bool TryGetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state, double& length) const;
168
174 bool TryScanEncodedString(const PdfString& encodedStr, const PdfTextState& state, std::string& utf8str,
175 std::vector<double>& lengths, std::vector<unsigned>& positions) const;
176
183 double GetWordSpacingLength(const PdfTextState& state) const;
184
191 double GetHardSpacingLength(const PdfTextState& state) const;
192
199 double GetSpaceCharLength(const PdfTextState& state) const;
200
204 double GetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing = false) const;
205
206 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing, double& width) const;
207
208 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, double& width) const;
209
210 double GetDefaultCharLength(const PdfTextState& state, bool ignoreCharSpacing = false) const;
211
212 // TODO: Implement me
216 //std::vector<PdfSplittedString> SplitEncodedString(const PdfString& str) const;
217
222 void AddSubsetCIDs(const PdfString& encodedStr);
223
226 bool HasCIDSubset() const;
227
231 double GetCIDWidth(unsigned cid) const;
232
236 double GetLineSpacing(const PdfTextState& state) const;
237
242 double GetUnderlineThickness(const PdfTextState& state) const;
243
248 double GetUnderlinePosition(const PdfTextState& state) const;
249
254 double GetStrikeThroughPosition(const PdfTextState& state) const;
255
260 double GetStrikeThroughThickness(const PdfTextState& state) const;
261
269 double GetAscent(const PdfTextState& state) const;
270
279 double GetDescent(const PdfTextState& state) const;
280
281 virtual bool SupportsSubsetting() const;
282
283 bool IsStandard14Font() const;
284
285 bool IsStandard14Font(PdfStandard14FontType& std14Font) const;
286
287 static std::string_view GetStandard14FontName(PdfStandard14FontType stdFont);
288
294 static bool IsStandard14Font(const std::string_view& fontName, PdfStandard14FontType& stdFont);
295
299 static bool IsStandard14Font(const std::string_view& fontName, bool useAltNames, PdfStandard14FontType& stdFont);
300
301public:
304 bool IsCIDFont() const;
305
309 virtual bool IsObjectLoaded() const;
310
311 PdfFontType GetType() const { return m_Type; }
312
316 inline bool IsSubsettingEnabled() const { return m_SubsettingEnabled; }
317
318 inline bool IsEmbeddingEnabled() const { return m_EmbeddingEnabled; }
319
324 inline std::string_view GetSubsetPrefix() const;
325
329 inline const PdfEncoding& GetEncoding() const { return *m_Encoding; }
330
336 inline const PdfFontMetrics& GetMetrics() const { return *m_Metrics; }
337
343 inline const std::string& GetName() const { return m_Name; }
344
347 inline bool IsProxy() const { return m_IsProxy; }
348
349 PdfObject& GetDescendantFontObject();
350
351protected:
352 void EmbedFontFile(PdfDictionary& descriptor) const;
353 void EmbedFontFileType1(PdfDictionary& descriptor, const bufferview& data,
354 unsigned length1, unsigned length2, unsigned length3) const;
355 void EmbedFontFileCFF(PdfDictionary& descriptor, const bufferview& data, bool cidKeyed) const;
356 void EmbedFontFileTrueType(PdfDictionary& descriptor, const bufferview& data) const;
357 void EmbedFontFileOpenType(PdfDictionary& descriptor, const bufferview& data) const;
358
361 bool tryMapCIDToGIDLoadedMetrics(unsigned cid, unsigned& gid) const;
362
365 bool tryMapCIDToGIDNormal(unsigned cid, unsigned& gid) const;
366
367 void GetBoundingBox(PdfArray& arr) const;
368
371 void WriteDescriptors(PdfDictionary& fontDict, PdfDictionary& descriptorDict) const;
372
376 bool TryGetSubstituteCIDEncoding(std::unique_ptr<PdfEncodingMap>& cidEncodingMap) const;
377
378 PdfCIDSystemInfo GetCIDSystemInfo() const;
379
382 std::vector<PdfCharGIDInfo> GetCharGIDInfos() const;
383
384 virtual PdfObject* getDescendantFontObject();
385
388 virtual void initImported();
389
390 virtual void embedFont();
391
392 virtual void embedFontSubset();
393
394private:
395 PdfFont(const PdfFont& rhs) = delete;
396
397private:
400 void EmbedFont();
401
406 void InitImported(bool wantEmbed, bool wantSubset, bool isProxy);
407
418 bool TryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
419
424 PdfCharCode AddCharCodeSafe(unsigned gid, const unicodeview& codePoints);
425
428 std::unique_ptr<std::set<PdfCharCode>> GetCharCodeSubset() const;
429
430private:
431 bool tryConvertToGIDs(const std::string_view& utf8Str, PdfGlyphAccess access, std::vector<unsigned>& gids) const;
432 bool tryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
433
437 void mapCIDToGID(unsigned cid, PdfGID& gid) const;
438
442 bool tryMapCIDToGID(unsigned cid, PdfGlyphAccess access, unsigned& gid) const;
443
444 void initBase(const PdfEncoding& encoding);
445
446 double getStringLength(const std::vector<PdfCID>& cids, const PdfTextState& state) const;
447
448 void embedFontFileData(PdfDictionary& descriptor, const PdfName& fontFileName,
449 const std::function<void(PdfDictionary& dict)>& dictWriter, const bufferview& data) const;
450
451 static std::unique_ptr<PdfFont> createFontForType(PdfDocument& doc, PdfFontMetricsConstPtr&& metrics,
452 const PdfEncoding& encoding, bool preferNonCID);
453
454 void initSpacingDescriptors();
455
456 void initSpaceCharLength();
457
458 void pushSubsetInfo(unsigned cid, const PdfGID& gid, const PdfCharCode& code);
459
460 // TODO: Optimize me
461 using PdfCharCodeList = std::vector<PdfCharCode>;
462
463 struct PODOFO_API CIDSubsetInfo final
464 {
465 PdfGID Gid;
466 PdfCharCodeList Codes;
467 };
468
469 using CIDSubsetMap = std::map<unsigned, CIDSubsetInfo>;
470
471private:
472 std::string m_Name;
473 std::string m_SubsetPrefix;
474 PdfFontType m_Type;
475 bool m_EmbeddingEnabled;
476 bool m_IsEmbedded;
477 bool m_SubsettingEnabled;
478 bool m_IsProxy;
479 std::unique_ptr<CIDSubsetMap> m_SubsetCIDMap;
480 std::unique_ptr<std::unordered_map<unsigned, unsigned>> m_subsetGIDToCIDMap;
481 const PdfCIDToGIDMap* m_fontProgCIDToGIDMap;
482 double m_WordSpacingLengthRaw;
483 double m_SpaceCharLengthRaw;
484
485protected:
486 PdfFontMetricsConstPtr m_Metrics;
487 std::unique_ptr<PdfEncoding> m_Encoding;
488 PdfCharCodeMap* m_DynamicCIDMap;
489 PdfCharCodeMap* m_DynamicToUnicodeMap;
490};
491
492};
493
494#endif // PDF_FONT_H
495
This file should be included as the FIRST file in every header of PoDoFo lib.
An interface for writing blocks of data to a data source.
Definition OutputStream.h:16
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:79
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:80
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:109
Basic PdfEncodingMap implementation using a PdfCharCodeMap.
Definition PdfEncodingMap.h:213
PdfEncodingMap used by legacy encodings like PdfBuiltInEncoding or PdfDifferenceEncoding that can def...
Definition PdfEncodingMap.h:252
A PdfEncoding is in PdfFont to transform a text string into a representation so that it can be displa...
Definition PdfEncoding.h:55
A PdfFont that represents a CID-keyed font.
Definition PdfFontCID.h:15
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
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
static bool IsStandard14Font(const std::string_view &fontName, bool useAltNames, PdfStandard14FontType &stdFont)
Determine if font name is a Standard14 font.
const std::string & GetName() const
Get the base font name of this font.
Definition PdfFont.h:343
const PdfFontMetrics & GetMetrics() const
Returns a handle to the fontmetrics object of this font.
Definition PdfFont.h:336
static bool TryCreateFromObject(PdfObject &obj, std::unique_ptr< PdfFont > &font)
Create a new PdfFont from an existing font in a PDF file.
const PdfEncoding & GetEncoding() const
Returns a reference to the fonts encoding.
Definition PdfFont.h:329
bool IsSubsettingEnabled() const
Check if this is a subsetting font.
Definition PdfFont.h:316
bool IsProxy() const
True if the font is substitute for embedding.
Definition PdfFont.h:347
static bool IsStandard14Font(const std::string_view &fontName, PdfStandard14FontType &stdFont)
Determine if font name is a Standard14 font.
This class represents a PdfName.
Definition PdfName.h:22
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:33
A string that can be written to a PDF document.
Definition PdfString.h:22
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
cspan< char32_t > unicodeview
Unicode code point view.
Definition basetypes.h:24
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
@ String
String datatype in PDF file. Strings have the form (Hallo World!) in PDF files.
@ 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
PdfFontCreateFlags
Font init flags.
Definition PdfDeclarations.h:338
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition PdfFontMetrics.h:25
PdfFontType
Enum specifying the type of the font.
Definition PdfDeclarations.h:284
Represent a CID (Character ID) with full code unit information.
Definition PdfEncodingCommon.h:46
A character code unit.
Definition PdfEncodingCommon.h:17
Represents a GID (Glyph ID) with PDF metrics identifier.
Definition PdfEncodingCommon.h:68