PoDoFo 1.0.0-dev
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, const 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 GetSpaceCharLength(const PdfTextState& state) const;
195
199 double GetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing = false) const;
200
201 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing, double& width) const;
202
203 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, double& width) const;
204
205 double GetDefaultCharLength(const PdfTextState& state, bool ignoreCharSpacing = false) const;
206
207 // TODO: Implement me
211 //std::vector<PdfSplittedString> SplitEncodedString(const PdfString& str) const;
212
217 void AddSubsetCIDs(const PdfString& encodedStr);
218
221 bool HasCIDSubset() const;
222
226 double GetCIDWidth(unsigned cid) const;
227
231 double GetLineSpacing(const PdfTextState& state) const;
232
237 double GetUnderlineThickness(const PdfTextState& state) const;
238
243 double GetUnderlinePosition(const PdfTextState& state) const;
244
249 double GetStrikeThroughPosition(const PdfTextState& state) const;
250
255 double GetStrikeThroughThickness(const PdfTextState& state) const;
256
264 double GetAscent(const PdfTextState& state) const;
265
274 double GetDescent(const PdfTextState& state) const;
275
276 virtual bool SupportsSubsetting() const;
277
278 virtual PdfFontType GetType() const = 0;
279
280 bool IsStandard14Font() const;
281
282 bool IsStandard14Font(PdfStandard14FontType& std14Font) const;
283
284 static std::string_view GetStandard14FontName(PdfStandard14FontType stdFont);
285
291 static bool IsStandard14Font(const std::string_view& fontName, PdfStandard14FontType& stdFont);
292
296 static bool IsStandard14Font(const std::string_view& fontName, bool useAltNames, PdfStandard14FontType& stdFont);
297
298public:
301 bool IsCIDFont() const;
302
306 virtual bool IsObjectLoaded() const;
307
311 inline bool IsSubsettingEnabled() const { return m_SubsettingEnabled; }
312
313 inline bool IsEmbeddingEnabled() const { return m_EmbeddingEnabled; }
314
319 inline std::string_view GetSubsetPrefix() const;
320
324 inline const PdfEncoding& GetEncoding() const { return *m_Encoding; }
325
331 inline const PdfFontMetrics& GetMetrics() const { return *m_Metrics; }
332
338 inline const std::string& GetName() const { return m_Name; }
339
342 inline bool IsProxy() const { return m_IsProxy; }
343
344 PdfObject& GetDescendantFontObject();
345
346protected:
347 void EmbedFontFile(PdfObject& descriptor);
348 void EmbedFontFileType1(PdfObject& descriptor, const bufferview& data,
349 unsigned length1, unsigned length2, unsigned length3);
350 void EmbedFontFileCFF(PdfObject& descriptor, const bufferview& data, bool cidKeyed);
351 void EmbedFontFileTrueType(PdfObject& descriptor, const bufferview& data);
352 void EmbedFontFileOpenType(PdfObject& descriptor, const bufferview& data);
353
356 bool tryMapCIDToGIDLoadedMetrics(unsigned cid, unsigned& gid) const;
357
360 bool tryMapCIDToGIDUnicode(unsigned cid, unsigned& gid) const;
361
362 void GetBoundingBox(PdfArray& arr) const;
363
366 void FillDescriptor(PdfDictionary& dict) const;
367
371 bool TryGetSubstituteCIDEncoding(std::unique_ptr<PdfEncodingMap>& cidEncodingMap) const;
372
373 PdfCIDSystemInfo GetCIDSystemInfo() const;
374
377 std::vector<PdfCharGIDInfo> GetCharGIDInfos();
378
379 virtual PdfObject* getDescendantFontObject();
380
383 virtual void initImported();
384
385 virtual void embedFont();
386
387 virtual void embedFontSubset();
388
389private:
390 PdfFont(const PdfFont& rhs) = delete;
391
392private:
395 void EmbedFont();
396
401 void InitImported(bool wantEmbed, bool wantSubset, bool isProxy);
402
413 bool TryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
414
419 PdfCharCode AddCharCodeSafe(unsigned gid, const unicodeview& codePoints);
420
425 bool TryMapCIDToGID(unsigned cid, PdfGID& gid) const;
426 bool TryMapCIDToGID(unsigned cid, PdfGlyphAccess access, unsigned& gid) const;
427
428private:
429 struct CIDSubsetInfo
430 {
431 PdfGID Gid;
432 PdfCharCodeList Codes;
433 };
434
435 using CIDSubsetMap = std::map<unsigned, CIDSubsetInfo>;
436
437 bool tryConvertToGIDs(const std::string_view& utf8Str, PdfGlyphAccess access, std::vector<unsigned>& gids) const;
438 bool tryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
439
440 void initBase(const PdfEncoding& encoding);
441
442 double getStringLength(const std::vector<PdfCID>& cids, const PdfTextState& state) const;
443
444 void embedFontFileData(PdfObject& descriptor, const PdfName& fontFileName,
445 const std::function<void(PdfDictionary& dict)>& dictWriter, const bufferview& data);
446
447 static std::unique_ptr<PdfFont> createFontForType(PdfDocument& doc, const PdfFontMetricsConstPtr& metrics,
448 const PdfEncoding& encoding, PdfFontFileType type, bool preferNonCID);
449
450 void initSpaceDescriptors();
451
452 void pushSubsetInfo(unsigned cid, const PdfGID& gid, const PdfCharCode& code);
453
454private:
455 std::string m_Name;
456 std::string m_SubsetPrefix;
457 bool m_EmbeddingEnabled;
458 bool m_IsEmbedded;
459 bool m_SubsettingEnabled;
460 bool m_IsProxy;
461 std::unique_ptr<CIDSubsetMap> m_subsetCIDMap;
462 std::unique_ptr<std::unordered_map<unsigned, unsigned>> m_subsetGIDToCIDMap;
463 PdfCIDToGIDMapConstPtr m_fontProgCIDToGIDMap;
464 double m_WordSpacingLengthRaw;
465 double m_SpaceCharLengthRaw;
466
467protected:
468 PdfFontMetricsConstPtr m_Metrics;
469 std::unique_ptr<PdfEncoding> m_Encoding;
470 std::shared_ptr<PdfCharCodeMap> m_DynamicCIDMap;
471 std::shared_ptr<PdfCharCodeMap> m_DynamicToUnicodeMap;
472};
473
474};
475
476#endif // PDF_FONT_H
477
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:55
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:338
const PdfFontMetrics & GetMetrics() const
Returns a handle to the fontmetrics object of this font.
Definition PdfFont.h:331
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:324
bool IsSubsettingEnabled() const
Check if this is a subsetting font.
Definition PdfFont.h:311
bool IsProxy() const
True if the font is substitute for embedding.
Definition PdfFont.h:342
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