PoDoFo 1.2.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
41class PODOFO_API PdfFont : public PdfDictionaryElement
42{
43 friend class PdfFontSimple;
44 friend class PdfFontCID;
45 friend class PdfFontObject;
46 friend class PdfEncoding;
47 friend class PdfFontManager;
48 friend class PdfEncodingMapBase;
49 friend class PdfEncodingMapSimple;
50 friend class PdfFontBuiltinType1Encoding;
51
52private:
62 const PdfEncoding& encoding);
63
67 const PdfEncoding& encoding);
68
69public:
70 virtual ~PdfFont();
71
72public:
78 static bool TryCreateFromObject(PdfObject& obj, std::unique_ptr<PdfFont>& font);
79 static bool TryCreateFromObject(const PdfObject& obj, std::unique_ptr<const PdfFont>& font);
80
81private:
92 static std::unique_ptr<PdfFont> Create(PdfDocument& doc, PdfFontMetricsConstPtr&& metrics,
93 const PdfFontCreateParams& createParams, bool isProxy = false);
94
102 static std::unique_ptr<PdfFont> CreateStandard14(PdfDocument& doc, PdfStandard14FontType std14Font,
103 const PdfFontCreateParams& createParams);
104
105public:
109 bool TryCreateProxyFont(PdfFont*& proxyFont) const;
110 bool TryCreateProxyFont(PdfFontCreateFlags initFlags, PdfFont*& proxyFont) const;
111
120 void WriteStringToStream(OutputStream& stream, const std::string_view& str) const;
121
128 unsigned GetGID(char32_t codePoint, PdfGlyphAccess access) const;
129 bool TryGetGID(char32_t codePoint, PdfGlyphAccess access, unsigned& gid) const;
130
137 double GetStringLength(const std::string_view& str, const PdfTextState& state) const;
138
143 bool TryGetStringLength(const std::string_view& str, const PdfTextState& state, double& width) const;
144
151 double GetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state) const;
152
157 bool TryGetEncodedStringLength(const PdfString& encodedStr, const PdfTextState& state, double& length) const;
158
163 bool TryScanEncodedString(const PdfString& encodedStr, const PdfTextState& state, std::string& utf8str,
164 std::vector<double>& lengths, std::vector<unsigned>& positions) const;
165
170 double GetWordSpacingLength(const PdfTextState& state) const;
171
176 double GetHardSpacingLength(const PdfTextState& state) const;
177
182 double GetSpaceCharLength(const PdfTextState& state) const;
183
189 double GetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing = false) const;
190
196 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, bool ignoreCharSpacing, double& width) const;
197
202 bool TryGetCharLength(char32_t codePoint, const PdfTextState& state, double& width) const;
203
207 double GetDefaultCharLength(const PdfTextState& state, bool ignoreCharSpacing = false) const;
208
209 // TODO: Implement me
212 //std::vector<PdfSplittedString> SplitEncodedString(const PdfString& str) const;
213
217 void AddSubsetCIDs(const PdfString& encodedStr);
218
220 bool HasCIDSubset() const;
221
223 double GetCIDWidth(unsigned cid) const;
224
227 double GetLineSpacing(const PdfTextState& state) const;
228
232 double GetUnderlineThickness(const PdfTextState& state) const;
233
237 double GetUnderlinePosition(const PdfTextState& state) const;
238
242 double GetStrikeThroughPosition(const PdfTextState& state) const;
243
247 double GetStrikeThroughThickness(const PdfTextState& state) const;
248
255 double GetAscent(const PdfTextState& state) const;
256
264 double GetDescent(const PdfTextState& state) const;
265
266 virtual bool SupportsSubsetting() const;
267
268 bool IsStandard14Font() const;
269
270 bool IsStandard14Font(PdfStandard14FontType& std14Font) const;
271
272 static std::string_view GetStandard14FontName(PdfStandard14FontType stdFont);
273
278 static bool IsStandard14Font(const std::string_view& fontName, PdfStandard14FontType& stdFont);
279
282 static bool IsStandard14Font(const std::string_view& fontName, bool useAltNames, PdfStandard14FontType& stdFont);
283
284public:
286 bool IsCIDFont() const;
287
289 virtual bool IsObjectLoaded() const;
290
291 PdfFontType GetType() const { return m_Type; }
292
295 inline bool IsSubsettingEnabled() const { return m_SubsettingEnabled; }
296
297 inline bool IsEmbeddingEnabled() const { return m_EmbeddingEnabled; }
298
301 inline std::string_view GetSubsetPrefix() const;
302
305 inline const PdfEncoding& GetEncoding() const { return *m_Encoding; }
306
311 inline const PdfFontMetrics& GetMetrics() const { return *m_Metrics; }
312
317 inline const std::string& GetName() const { return m_Name; }
318
320 inline bool IsProxy() const { return m_IsProxy; }
321
322 PdfObject& GetDescendantFontObject();
323
324protected:
325 void EmbedFontFile(PdfDictionary& descriptor) const;
326 void EmbedFontFileType1(PdfDictionary& descriptor, const bufferview& data,
327 unsigned length1, unsigned length2, unsigned length3) const;
328 void EmbedFontFileCFF(PdfDictionary& descriptor, const bufferview& data, bool cidKeyed) const;
329 void EmbedFontFileTrueType(PdfDictionary& descriptor, const bufferview& data) const;
330 void EmbedFontFileOpenType(PdfDictionary& descriptor, const bufferview& data) const;
331
333 bool tryMapCIDToGIDLoadedMetrics(unsigned cid, unsigned& gid) const;
334
336 bool tryMapCIDToGIDNormal(unsigned cid, unsigned& gid) const;
337
338 void GetBoundingBox(PdfArray& arr) const;
339
341 void WriteDescriptors(PdfDictionary& fontDict, PdfDictionary& descriptorDict) const;
342
345 bool TryGetSubstituteCIDEncoding(std::unique_ptr<PdfEncodingMap>& cidEncodingMap) const;
346
347 PdfCIDSystemInfo GetCIDSystemInfo() const;
348
350 std::vector<PdfCharGIDInfo> GetCharGIDInfos() const;
351
352 virtual PdfObject* getDescendantFontObject();
353
355 virtual void initImported();
356
357 virtual void embedFont();
358
359 virtual void embedFontSubset();
360
361private:
362 PdfFont(const PdfFont& rhs) = delete;
363
364private:
366 void EmbedFont();
367
370 void InitImported(bool wantEmbed, bool wantSubset, bool isProxy);
371
381 bool TryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
382
386 PdfCharCode AddCharCodeSafe(unsigned gid, const unicodeview& codePoints);
387
389 std::unique_ptr<std::set<PdfCharCode>> GetCharCodeSubset() const;
390
391private:
392 bool tryConvertToGIDs(const std::string_view& utf8Str, PdfGlyphAccess access, std::vector<unsigned>& gids) const;
393 bool tryAddSubsetGID(unsigned gid, const unicodeview& codePoints, PdfCID& cid);
394
397 void mapCIDToGID(unsigned cid, PdfGID& gid) const;
398
401 bool tryMapCIDToGID(unsigned cid, PdfGlyphAccess access, unsigned& gid) const;
402
403 void initBase(const PdfEncoding& encoding);
404
405 double getStringLength(const std::vector<PdfCID>& cids, const PdfTextState& state) const;
406
407 void embedFontFileData(PdfDictionary& descriptor, const PdfName& fontFileName,
408 const std::function<void(PdfDictionary& dict)>& dictWriter, const bufferview& data) const;
409
410 static std::unique_ptr<PdfFont> createFontForType(PdfDocument& doc, PdfFontMetricsConstPtr&& metrics,
411 const PdfEncoding& encoding, bool preferNonCID);
412
413 void initSpacingDescriptors();
414
415 void initSpaceCharLength();
416
417 void pushSubsetInfo(unsigned cid, const PdfGID& gid, const PdfCharCode& code);
418
419 // TODO: Optimize me
420 using PdfCharCodeList = std::vector<PdfCharCode>;
421
422 struct PODOFO_API CIDSubsetInfo final
423 {
424 PdfGID Gid;
425 PdfCharCodeList Codes;
426 };
427
428 using CIDSubsetMap = std::map<unsigned, CIDSubsetInfo>;
429
430private:
431 std::string m_Name;
432 std::string m_SubsetPrefix;
433 PdfFontType m_Type;
434 bool m_EmbeddingEnabled;
435 bool m_IsEmbedded;
436 bool m_SubsettingEnabled;
437 bool m_IsProxy;
438 std::unique_ptr<CIDSubsetMap> m_SubsetCIDMap;
439 std::unique_ptr<std::unordered_map<unsigned, unsigned>> m_subsetGIDToCIDMap;
440 const PdfCIDToGIDMap* m_fontProgCIDToGIDMap;
441 double m_WordSpacingLengthRaw;
442 double m_SpaceCharLengthRaw;
443
444protected:
445 PdfFontMetricsConstPtr m_Metrics;
446 std::unique_ptr<PdfEncoding> m_Encoding;
447 PdfCharCodeMap* m_DynamicCIDMap;
448 PdfCharCodeMap* m_DynamicToUnicodeMap;
449};
450
451};
452
453#endif // PDF_FONT_H
454
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:15
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:76
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:77
PdfDocument is the core interface for working with PDF documents.
Definition PdfDocument.h:108
Basic PdfEncodingMap implementation using a PdfCharCodeMap.
Definition PdfEncodingMap.h:180
PdfEncodingMap used by legacy encodings like PdfBuiltInEncoding or PdfDifferenceEncoding that can def...
Definition PdfEncodingMap.h:217
A PdfEncoding is in PdfFont to transform a text string into a representation so that it can be displa...
Definition PdfEncoding.h:51
A PdfFont that represents a CID-keyed font.
Definition PdfFontCID.h:14
This class assists PdfDocument with caching font information.
Definition PdfFontManager.h:50
This abstract class provides access to font metrics information.
Definition PdfFontMetrics.h:31
This is a common base class for simple, non CID-keyed fonts like Type1, TrueType and Type3.
Definition PdfFontSimple.h:17
Before you can draw text on a PDF document, you have to create a font object first.
Definition PdfFont.h:42
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:317
const PdfFontMetrics & GetMetrics() const
Returns a handle to the fontmetrics object of this font.
Definition PdfFont.h:311
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:305
bool IsSubsettingEnabled() const
Check if this is a subsetting font.
Definition PdfFont.h:295
bool IsProxy() const
True if the font is substitute for embedding.
Definition PdfFont.h:320
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:21
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
A string that can be written to a PDF document.
Definition PdfString.h:21
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
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:21
PdfGlyphAccess
When accessing a glyph, there may be a difference in the glyph ID to retrieve the widths or to index ...
Definition PdfDeclarations.h:301
@ 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:15
PdfFontCreateFlags
Font init flags.
Definition PdfDeclarations.h:316
std::shared_ptr< const PdfFontMetrics > PdfFontMetricsConstPtr
Convenience typedef for a const PdfEncoding shared ptr.
Definition PdfFontMetrics.h:24
PdfFontType
Enum specifying the type of the font.
Definition PdfDeclarations.h:266
Represent a CID (Character ID) with full code unit information.
Definition PdfEncodingCommon.h:43
A character code unit.
Definition PdfEncodingCommon.h:16
Represents a GID (Glyph ID) with PDF metrics identifier.
Definition PdfEncodingCommon.h:60