PoDoFo 1.1.0
Loading...
Searching...
No Matches
PdfFont.h
1
7#ifndef PDF_FONT_H
8#define PDF_FONT_H
9
10#include "PdfDeclarations.h"
11
12#include "PdfTextState.h"
13#include "PdfName.h"
14#include "PdfEncoding.h"
15#include "PdfElement.h"
16#include "PdfFontMetrics.h"
17
18namespace PoDoFo {
19
20class PdfCharCodeMap;
21
22struct PODOFO_API PdfFontCreateParams final
23{
24 PdfEncoding Encoding;
25 PdfFontCreateFlags Flags = PdfFontCreateFlags::None;
26};
27
28struct PODOFO_API PdfSplittedString final
29{
30 PdfString String;
31 bool IsSeparator = false;
32};
33
44class PODOFO_API PdfFont : public PdfDictionaryElement
45{
46 friend class PdfFontSimple;
47 friend class PdfFontCID;
48 friend class PdfFontFactory;
49 friend class PdfFontObject;
50 friend class PdfEncoding;
51 friend class PdfFontManager;
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
114 static std::unique_ptr<PdfFont> CreateStandard14(PdfDocument& doc, PdfStandard14FontType std14Font,
115 const PdfFontCreateParams& createParams);
116
117public:
122 bool TryCreateProxyFont(PdfFont*& proxyFont) const;
123 bool TryCreateProxyFont(PdfFontCreateFlags initFlags, PdfFont*& proxyFont) const;
124
134 void WriteStringToStream(OutputStream& stream, const std::string_view& str) const;
135
142 unsigned GetGID(char32_t codePoint, PdfGlyphAccess access) const;
143 bool TryGetGID(char32_t codePoint, PdfGlyphAccess access, unsigned& gid) const;
144
151 double GetStringLength(const std::string_view& str, const PdfTextState& state) const;
152
157 bool TryGetStringLength(const std::string_view& str, const PdfTextState& state, double& width) const;
158
165 double GetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state) const;
166
170 bool TryGetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state, double& length) const;
171
177 bool TryScanEncodedString(const PdfString& encodedStr, const PdfTextState& state, std::string& utf8str,
178 std::vector<double>& lengths, std::vector<unsigned>& positions) const;
179
186 double GetWordSpacingLength(const PdfTextState& state) const;
187
194 double GetHardSpacingLength(const PdfTextState& state) const;
195
202 double GetSpaceCharLength(const PdfTextState& state) const;
203
207 double GetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing = false) const;
208
209 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing, double& width) const;
210
211 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, double& width) const;
212
213 double GetDefaultCharLength(const PdfTextState& state, bool ignoreCharSpacing = false) const;
214
215 // TODO: Implement me
219 //std::vector<PdfSplittedString> SplitEncodedString(const PdfString& str) const;
220
225 void AddSubsetCIDs(const PdfString& encodedStr);
226
229 bool HasCIDSubset() const;
230
234 double GetCIDWidth(unsigned cid) const;
235
239 double GetLineSpacing(const PdfTextState& state) const;
240
245 double GetUnderlineThickness(const PdfTextState& state) const;
246
251 double GetUnderlinePosition(const PdfTextState& state) const;
252
257 double GetStrikeThroughPosition(const PdfTextState& state) const;
258
263 double GetStrikeThroughThickness(const PdfTextState& state) const;
264
272 double GetAscent(const PdfTextState& state) const;
273
282 double GetDescent(const PdfTextState& state) const;
283
284 virtual bool SupportsSubsetting() const;
285
286 bool IsStandard14Font() const;
287
288 bool IsStandard14Font(PdfStandard14FontType& std14Font) const;
289
290 static std::string_view GetStandard14FontName(PdfStandard14FontType stdFont);
291
297 static bool IsStandard14Font(const std::string_view& fontName, PdfStandard14FontType& stdFont);
298
302 static bool IsStandard14Font(const std::string_view& fontName, bool useAltNames, PdfStandard14FontType& stdFont);
303
304public:
307 bool IsCIDFont() const;
308
312 virtual bool IsObjectLoaded() const;
313
314 PdfFontType GetType() const { return m_Type; }
315
319 inline bool IsSubsettingEnabled() const { return m_SubsettingEnabled; }
320
321 inline bool IsEmbeddingEnabled() const { return m_EmbeddingEnabled; }
322
327 inline std::string_view GetSubsetPrefix() const;
328
332 inline const PdfEncoding& GetEncoding() const { return *m_Encoding; }
333
339 inline const PdfFontMetrics& GetMetrics() const { return *m_Metrics; }
340
346 inline const std::string& GetName() const { return m_Name; }
347
350 inline bool IsProxy() const { return m_IsProxy; }
351
352 PdfObject& GetDescendantFontObject();
353
354protected:
355 void EmbedFontFile(PdfDictionary& descriptor) const;
356 void EmbedFontFileType1(PdfDictionary& descriptor, const bufferview& data,
357 unsigned length1, unsigned length2, unsigned length3) const;
358 void EmbedFontFileCFF(PdfDictionary& descriptor, const bufferview& data, bool cidKeyed) const;
359 void EmbedFontFileTrueType(PdfDictionary& descriptor, const bufferview& data) const;
360 void EmbedFontFileOpenType(PdfDictionary& descriptor, const bufferview& data) const;
361
364 bool tryMapCIDToGIDLoadedMetrics(unsigned cid, unsigned& gid) const;
365
368 bool tryMapCIDToGIDNormal(unsigned cid, unsigned& gid) const;
369
370 void GetBoundingBox(PdfArray& arr) const;
371
374 void WriteDescriptors(PdfDictionary& fontDict, PdfDictionary& descriptorDict) const;
375
379 bool TryGetSubstituteCIDEncoding(std::unique_ptr<PdfEncodingMap>& cidEncodingMap) const;
380
381 PdfCIDSystemInfo GetCIDSystemInfo() const;
382
385 std::vector<PdfCharGIDInfo> GetCharGIDInfos() const;
386
387 virtual PdfObject* getDescendantFontObject();
388
391 virtual void initImported();
392
393 virtual void embedFont();
394
395 virtual void embedFontSubset();
396
397private:
398 PdfFont(const PdfFont& rhs) = delete;
399
400private:
403 void EmbedFont();
404
409 void InitImported(bool wantEmbed, bool wantSubset, bool isProxy);
410
421 bool TryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
422
427 PdfCharCode AddCharCodeSafe(unsigned gid, const unicodeview& codePoints);
428
433 bool TryMapCIDToGID(unsigned cid, PdfGID& gid) const;
434 bool TryMapCIDToGID(unsigned cid, PdfGlyphAccess access, unsigned& gid) const;
435
436private:
437 struct CIDSubsetInfo
438 {
439 PdfGID Gid;
440 PdfCharCodeList Codes;
441 };
442
443 using CIDSubsetMap = std::map<unsigned, CIDSubsetInfo>;
444
445 bool tryConvertToGIDs(const std::string_view& utf8Str, PdfGlyphAccess access, std::vector<unsigned>& gids) const;
446 bool tryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
447
448 void initBase(const PdfEncoding& encoding);
449
450 double getStringLength(const std::vector<PdfCID>& cids, const PdfTextState& state) const;
451
452 void embedFontFileData(PdfDictionary& descriptor, const PdfName& fontFileName,
453 const std::function<void(PdfDictionary& dict)>& dictWriter, const bufferview& data) const;
454
455 static std::unique_ptr<PdfFont> createFontForType(PdfDocument& doc, PdfFontMetricsConstPtr&& metrics,
456 const PdfEncoding& encoding, bool preferNonCID);
457
458 void initSpacingDescriptors();
459
460 void initSpaceCharLength();
461
462 void pushSubsetInfo(unsigned cid, const PdfGID& gid, const PdfCharCode& code);
463
464private:
465 std::string m_Name;
466 std::string m_SubsetPrefix;
467 PdfFontType m_Type;
468 bool m_EmbeddingEnabled;
469 bool m_IsEmbedded;
470 bool m_SubsettingEnabled;
471 bool m_IsProxy;
472 std::unique_ptr<CIDSubsetMap> m_subsetCIDMap;
473 std::unique_ptr<std::unordered_map<unsigned, unsigned>> m_subsetGIDToCIDMap;
474 const PdfCIDToGIDMap* m_fontProgCIDToGIDMap;
475 double m_WordSpacingLengthRaw;
476 double m_SpaceCharLengthRaw;
477
478protected:
479 PdfFontMetricsConstPtr m_Metrics;
480 std::unique_ptr<PdfEncoding> m_Encoding;
481 std::shared_ptr<PdfCharCodeMap> m_DynamicCIDMap;
482 std::shared_ptr<PdfCharCodeMap> m_DynamicToUnicodeMap;
483};
484
485};
486
487#endif // PDF_FONT_H
488
SPDX-FileCopyrightText: (C) 2005 Dominik Seichter domseichter@web.de SPDX-FileCopyrightText: (C) 2020...
An interface for writing blocks of data to a data source.
Definition OutputStream.h:18
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:81
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:111
A PdfEncoding is in PdfFont to transform a text string into a representation so that it can be displa...
Definition PdfEncoding.h:58
A PdfFont that represents a CID-keyed font.
Definition PdfFontCID.h:17
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
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
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:346
const PdfFontMetrics & GetMetrics() const
Returns a handle to the fontmetrics object of this font.
Definition PdfFont.h:339
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:332
bool IsSubsettingEnabled() const
Check if this is a subsetting font.
Definition PdfFont.h:319
bool IsProxy() const
True if the font is substitute for embedding.
Definition PdfFont.h:350
static bool IsStandard14Font(const std::string_view &fontName, PdfStandard14FontType &stdFont)
Determine if font name is a Standard14 font.
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
A string that can be written to a PDF document.
Definition PdfString.h:24
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
cspan< char32_t > unicodeview
Unicode code point view.
Definition basetypes.h:27
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
@ 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:19
PdfFontCreateFlags
Font init flags.
Definition PdfDeclarations.h:340
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition PdfFontMetrics.h:27
PdfFontType
Enum specifying the type of the font.
Definition PdfDeclarations.h:286
Represent a CID (Character ID) with full code unit information.
Definition PdfEncodingCommon.h:52
A character code unit.
Definition PdfEncodingCommon.h:20
Represents a GID (Glyph ID) with PDF metrics identifier.
Definition PdfEncodingCommon.h:74