Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_font.h
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#ifndef CORE_FPDFAPI_FONT_CPDF_FONT_H_
8#define CORE_FPDFAPI_FONT_CPDF_FONT_H_
9
10#include <stdint.h>
11
12#include <memory>
13#include <utility>
14#include <vector>
15
16#include "build/build_config.h"
17#include "core/fpdfapi/parser/cpdf_dictionary.h"
18#include "core/fpdfapi/parser/cpdf_stream_acc.h"
19#include "core/fxcrt/fx_coordinates.h"
20#include "core/fxcrt/fx_string.h"
21#include "core/fxcrt/observed_ptr.h"
22#include "core/fxcrt/retain_ptr.h"
23#include "core/fxcrt/unowned_ptr.h"
24#include "core/fxge/cfx_font.h"
25#include "core/fxge/freetype/fx_freetype.h"
26#include "third_party/abseil-cpp/absl/types/optional.h"
27
28class CFX_DIBitmap;
29class CPDF_CIDFont;
30class CPDF_Document;
31class CPDF_TrueTypeFont;
32class CPDF_Type1Font;
33class CPDF_Type3Char;
34class CPDF_Type3Font;
36enum class FontEncoding;
37
38class CPDF_Font : public Retainable, public Observable {
39 public:
40 // Callback mechanism for Type3 fonts to get pixels from forms.
41 class FormIface {
42 public:
43 virtual ~FormIface() = default;
44
45 virtual void ParseContentForType3Char(CPDF_Type3Char* pChar) = 0;
46 virtual bool HasPageObjects() const = 0;
47 virtual CFX_FloatRect CalcBoundingBox() const = 0;
50 };
51
52 // Callback mechanism for Type3 fonts to get new forms from upper layers.
54 public:
55 virtual ~FormFactoryIface() = default;
56
58 CPDF_Document* pDocument,
59 RetainPtr<CPDF_Dictionary> pPageResources,
60 RetainPtr<CPDF_Stream> pFormStream) = 0;
61 };
62
63 static constexpr uint32_t kInvalidCharCode = static_cast<uint32_t>(-1);
64
65 // |pFactory| only required for Type3 fonts.
67 RetainPtr<CPDF_Dictionary> pFontDict,
68 FormFactoryIface* pFactory);
70 ByteStringView fontname);
71
72 virtual bool IsType1Font() const;
73 virtual bool IsTrueTypeFont() const;
74 virtual bool IsType3Font() const;
75 virtual bool IsCIDFont() const;
76 virtual const CPDF_Type1Font* AsType1Font() const;
77 virtual CPDF_Type1Font* AsType1Font();
78 virtual const CPDF_TrueTypeFont* AsTrueTypeFont() const;
79 virtual CPDF_TrueTypeFont* AsTrueTypeFont();
80 virtual const CPDF_Type3Font* AsType3Font() const;
81 virtual CPDF_Type3Font* AsType3Font();
82 virtual const CPDF_CIDFont* AsCIDFont() const;
83 virtual CPDF_CIDFont* AsCIDFont();
84
85 virtual void WillBeDestroyed();
86 virtual bool IsVertWriting() const;
87 virtual bool IsUnicodeCompatible() const = 0;
88 virtual uint32_t GetNextChar(ByteStringView pString, size_t* pOffset) const;
89 virtual size_t CountChar(ByteStringView pString) const;
90 virtual int AppendChar(char* buf, uint32_t charcode) const;
91 virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0;
92#if BUILDFLAG(IS_APPLE)
94#endif
95 virtual WideString UnicodeFromCharCode(uint32_t charcode) const;
96 virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const;
97 virtual bool HasFontWidths() const;
98
99 ByteString GetBaseFontName() const { return m_BaseFontName; }
101 bool IsEmbedded() const { return IsType3Font() || m_pFontFile != nullptr; }
102 RetainPtr<CPDF_Dictionary> GetMutableFontDict() { return m_pFontDict; }
103 RetainPtr<const CPDF_Dictionary> GetFontDict() const { return m_pFontDict; }
104 uint32_t GetFontDictObjNum() const { return m_pFontDict->GetObjNum(); }
105 bool FontDictIs(const CPDF_Dictionary* pThat) const {
106 return m_pFontDict == pThat;
107 }
108 void ClearFontDict() { m_pFontDict = nullptr; }
109 bool IsStandardFont() const;
110 bool HasFace() const { return !!m_Font.GetFace(); }
111 void AppendChar(ByteString* str, uint32_t charcode) const;
112
113 const FX_RECT& GetFontBBox() const { return m_FontBBox; }
114 int GetTypeAscent() const { return m_Ascent; }
115 int GetTypeDescent() const { return m_Descent; }
116 int GetStringWidth(ByteStringView pString);
117 uint32_t FallbackFontFromCharcode(uint32_t charcode);
118 int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode);
119 int GetFontFlags() const { return m_Flags; }
120 int GetItalicAngle() const { return m_ItalicAngle; }
121 int GetFontWeight() const;
122
123 virtual int GetCharWidthF(uint32_t charcode) = 0;
124 virtual FX_RECT GetCharBBox(uint32_t charcode) = 0;
125
126 // Can return nullptr for stock Type1 fonts. Always returns non-null for other
127 // font types.
128 CPDF_Document* GetDocument() const { return m_pDocument; }
129
130 CFX_Font* GetFont() { return &m_Font; }
131 const CFX_Font* GetFont() const { return &m_Font; }
132
133 CFX_Font* GetFontFallback(int position);
134
135 const ByteString& GetResourceName() const { return m_ResourceName; }
136 void SetResourceName(const ByteString& name) { m_ResourceName = name; }
137
138 protected:
139 CPDF_Font(CPDF_Document* pDocument, RetainPtr<CPDF_Dictionary> pFontDict);
140 ~CPDF_Font() override;
141
142 static int TT2PDF(FT_Pos m, const RetainPtr<CFX_Face>& face);
143 static FX_RECT GetCharBBoxForFace(const RetainPtr<CFX_Face>& face);
144
145 // Commonly used wrappers for UseTTCharmap().
146 static bool UseTTCharmapMSUnicode(const RetainPtr<CFX_Face>& face) {
147 return UseTTCharmap(face, 3, 1);
148 }
149 static bool UseTTCharmapMSSymbol(const RetainPtr<CFX_Face>& face) {
150 return UseTTCharmap(face, 3, 0);
151 }
152 static bool UseTTCharmapMacRoman(const RetainPtr<CFX_Face>& face) {
153 return UseTTCharmap(face, 1, 0);
154 }
155 static bool UseTTCharmap(const RetainPtr<CFX_Face>& face,
156 int platform_id,
157 int encoding_id);
158
159 static const char* GetAdobeCharName(FontEncoding base_encoding,
160 const std::vector<ByteString>& charnames,
161 uint32_t charcode);
162
163 virtual bool Load() = 0;
164
165 void LoadUnicodeMap() const; // logically const only.
166 void LoadFontDescriptor(const CPDF_Dictionary* pFontDesc);
167 void CheckFontMetrics();
168
170 ByteString m_ResourceName; // The resource name for this font.
173 RetainPtr<CPDF_StreamAcc> m_pFontFile;
174 RetainPtr<CPDF_Dictionary> m_pFontDict;
175 ByteString m_BaseFontName;
177 mutable bool m_bToUnicodeLoaded = false;
178 int m_Flags = 0;
179 int m_StemV = 0;
180 int m_Ascent = 0;
181 int m_Descent = 0;
184};
185
186#endif // CORE_FPDFAPI_FONT_CPDF_FONT_H_
float GetLineDashPhase() const
float GetLineWidth() const
CFX_GraphStateData::LineCap GetLineCap() const
CFX_GraphStateData::LineJoin GetLineJoin() const
bool HasRef() const
const CPDF_Color * GetStrokeColor() const
const CPDF_Color * GetFillColor() const
bool GetRGB(int *R, int *G, int *B) const
bool IsColorSpaceRGB() const
const ByteString & GetName() const
const ByteString & GetPropertyName() const
ParamType GetParamType() const
virtual ~FormFactoryIface()=default
virtual std::unique_ptr< FormIface > CreateForm(CPDF_Document *pDocument, RetainPtr< CPDF_Dictionary > pPageResources, RetainPtr< CPDF_Stream > pFormStream)=0
virtual ~FormIface()=default
virtual CFX_FloatRect CalcBoundingBox() const =0
virtual void ParseContentForType3Char(CPDF_Type3Char *pChar)=0
virtual absl::optional< std::pair< RetainPtr< CFX_DIBitmap >, CFX_Matrix > > GetBitmapAndMatrixFromSoleImageOfForm() const =0
virtual bool HasPageObjects() const =0
int GetItalicAngle() const
Definition cpdf_font.h:120
virtual FX_RECT GetCharBBox(uint32_t charcode)=0
virtual bool IsTrueTypeFont() const
Definition cpdf_font.cpp:67
virtual const CPDF_CIDFont * AsCIDFont() const
UnownedPtr< CPDF_Document > const m_pDocument
Definition cpdf_font.h:169
int m_ItalicAngle
Definition cpdf_font.h:182
virtual int GetCharWidthF(uint32_t charcode)=0
const FX_RECT & GetFontBBox() const
Definition cpdf_font.h:113
virtual void WillBeDestroyed()
int GetTypeDescent() const
Definition cpdf_font.h:115
static bool UseTTCharmapMSSymbol(const RetainPtr< CFX_Face > &face)
Definition cpdf_font.h:149
int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode)
void LoadFontDescriptor(const CPDF_Dictionary *pFontDesc)
virtual CPDF_CIDFont * AsCIDFont()
RetainPtr< CPDF_Dictionary > m_pFontDict
Definition cpdf_font.h:174
static constexpr uint32_t kInvalidCharCode
Definition cpdf_font.h:63
static int TT2PDF(FT_Pos m, const RetainPtr< CFX_Face > &face)
void ClearFontDict()
Definition cpdf_font.h:108
absl::optional< FX_Charset > GetSubstFontCharset() const
virtual bool IsType1Font() const
Definition cpdf_font.cpp:63
virtual bool IsVertWriting() const
RetainPtr< CPDF_StreamAcc > m_pFontFile
Definition cpdf_font.h:173
CPDF_Font(CPDF_Document *pDocument, RetainPtr< CPDF_Dictionary > pFontDict)
Definition cpdf_font.cpp:52
~CPDF_Font() override
Definition cpdf_font.cpp:58
void LoadUnicodeMap() const
static RetainPtr< CPDF_Font > Create(CPDF_Document *pDoc, RetainPtr< CPDF_Dictionary > pFontDict, FormFactoryIface *pFactory)
virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const
virtual int GlyphFromCharCode(uint32_t charcode, bool *pVertGlyph)=0
static bool UseTTCharmapMacRoman(const RetainPtr< CFX_Face > &face)
Definition cpdf_font.h:152
const CFX_Font * GetFont() const
Definition cpdf_font.h:131
static bool UseTTCharmapMSUnicode(const RetainPtr< CFX_Face > &face)
Definition cpdf_font.h:146
virtual const CPDF_TrueTypeFont * AsTrueTypeFont() const
Definition cpdf_font.cpp:87
virtual const CPDF_Type1Font * AsType1Font() const
Definition cpdf_font.cpp:79
std::unique_ptr< CPDF_ToUnicodeMap > m_pToUnicodeMap
Definition cpdf_font.h:176
int m_Flags
Definition cpdf_font.h:178
RetainPtr< const CPDF_Dictionary > GetFontDict() const
Definition cpdf_font.h:103
void SetResourceName(const ByteString &name)
Definition cpdf_font.h:136
virtual bool IsCIDFont() const
Definition cpdf_font.cpp:75
std::vector< std::unique_ptr< CFX_Font > > m_FontFallbacks
Definition cpdf_font.h:172
int m_StemV
Definition cpdf_font.h:179
virtual WideString UnicodeFromCharCode(uint32_t charcode) const
CFX_Font * GetFont()
Definition cpdf_font.h:130
virtual const CPDF_Type3Font * AsType3Font() const
Definition cpdf_font.cpp:95
static RetainPtr< CPDF_Font > GetStockFont(CPDF_Document *pDoc, ByteStringView fontname)
static bool UseTTCharmap(const RetainPtr< CFX_Face > &face, int platform_id, int encoding_id)
virtual size_t CountChar(ByteStringView pString) const
void AppendChar(ByteString *str, uint32_t charcode) const
int GetStringWidth(ByteStringView pString)
RetainPtr< CPDF_Dictionary > GetMutableFontDict()
Definition cpdf_font.h:102
virtual CPDF_Type1Font * AsType1Font()
Definition cpdf_font.cpp:83
ByteString m_BaseFontName
Definition cpdf_font.h:175
ByteString GetBaseFontName() const
Definition cpdf_font.h:99
virtual bool HasFontWidths() const
virtual bool IsType3Font() const
Definition cpdf_font.cpp:71
virtual CPDF_Type3Font * AsType3Font()
Definition cpdf_font.cpp:99
bool FontDictIs(const CPDF_Dictionary *pThat) const
Definition cpdf_font.h:105
static const char * GetAdobeCharName(FontEncoding base_encoding, const std::vector< ByteString > &charnames, uint32_t charcode)
ByteString m_ResourceName
Definition cpdf_font.h:170
virtual CPDF_TrueTypeFont * AsTrueTypeFont()
Definition cpdf_font.cpp:91
const ByteString & GetResourceName() const
Definition cpdf_font.h:135
FX_RECT m_FontBBox
Definition cpdf_font.h:183
int GetFontWeight() const
bool HasFace() const
Definition cpdf_font.h:110
virtual int AppendChar(char *buf, uint32_t charcode) const
int m_Ascent
Definition cpdf_font.h:180
int m_Descent
Definition cpdf_font.h:181
uint32_t GetFontDictObjNum() const
Definition cpdf_font.h:104
virtual uint32_t GetNextChar(ByteStringView pString, size_t *pOffset) const
virtual bool Load()=0
bool m_bToUnicodeLoaded
Definition cpdf_font.h:177
uint32_t FallbackFontFromCharcode(uint32_t charcode)
virtual bool IsUnicodeCompatible() const =0
bool IsStandardFont() const
static FX_RECT GetCharBBoxForFace(const RetainPtr< CFX_Face > &face)
CPDF_Document * GetDocument() const
Definition cpdf_font.h:128
void CheckFontMetrics()
CFX_Font m_Font
Definition cpdf_font.h:171
bool IsEmbedded() const
Definition cpdf_font.h:101
int GetTypeAscent() const
Definition cpdf_font.h:114
int GetFontFlags() const
Definition cpdf_font.h:119
CFX_Font * GetFontFallback(int position)
const CFX_Matrix & form_matrix() const
float GetStrokeAlpha() const
BlendMode GetBlendType() const
ByteString GetBlendMode() const
void SetImage(RetainPtr< CPDF_Image > pImage)
const CFX_Matrix & matrix() const
bool ProcessPageObjects(fxcrt::ostringstream *buf)
CPDF_PageContentGenerator(CPDF_PageObjectHolder *pObjHolder)
void UpdateStream(size_t stream_index, fxcrt::ostringstream *buf)
size_t AddStream(fxcrt::ostringstream *buf)
CPDF_PageContentManager(CPDF_PageObjectHolder *page_obj_holder, CPDF_Document *document)
const CPDF_ColorState & color_state() const
void SetDirty(bool value)
const CPDF_GeneralState & general_state() const
const ByteString & GetResourceName() const
const CPDF_ClipPath & clip_path() const
virtual Type GetType() const =0
CPDF_GeneralState & mutable_general_state()
const CPDF_ContentMarks * GetContentMarks() const
void SetResourceName(const ByteString &resource_name)
const CFX_GraphState & graph_state() const
bool has_winding_filltype() const
bool stroke() const
const CFX_Matrix & matrix() const
bool has_alternate_filltype() const
bool has_no_filltype() const
bool IsRect() const
Definition cpdf_path.cpp:37
CPDF_SimpleFont(CPDF_Document *pDocument, RetainPtr< CPDF_Dictionary > pFontDict)
std::vector< ByteString > m_CharNames
~CPDF_SimpleFont() override
uint16_t m_GlyphIndex[kInternalTableSize]
int GlyphFromCharCode(uint32_t charcode, bool *pVertGlyph) override
static constexpr size_t kInternalTableSize
int GetCharWidthF(uint32_t charcode) override
void LoadCharMetrics(int charcode)
CPDF_FontEncoding m_Encoding
uint32_t CharCodeFromUnicode(wchar_t Unicode) const override
bool IsUnicodeCompatible() const override
void LoadCharWidths(const CPDF_Dictionary *font_desc)
FX_RECT m_CharBBox[kInternalTableSize]
FX_RECT GetCharBBox(uint32_t charcode) override
void LoadPDFEncoding(bool bEmbedded, bool bTrueType)
uint16_t m_CharWidth[kInternalTableSize]
FontEncoding m_BaseEncoding
bool HasFontWidths() const override
void LoadDifferences(const CPDF_Dictionary *encoding)
const CPDF_FontEncoding * GetEncoding() const
WideString UnicodeFromCharCode(uint32_t charcode) const override
virtual void LoadGlyphMap()=0
~CPDF_StringArchiveStream() override
CPDF_StringArchiveStream(fxcrt::ostringstream *stream)
FX_FILESIZE CurrentOffset() const override
bool WriteBlock(pdfium::span< const uint8_t > buffer) override
CFX_Matrix GetTextMatrix() const
float GetFontSize() const
TextRenderingMode GetTextRenderMode() const
bool IsTrueTypeFont() const override
void LoadGlyphMap() override
CPDF_TrueTypeFont * AsTrueTypeFont() override
const CPDF_TrueTypeFont * AsTrueTypeFont() const override
~CPDF_TrueTypeFont() override
static ByteString Format(const char *pFormat,...)
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition bytestring.h:119
CharType operator[](const size_t index) const
Definition bytestring.h:150
FontEncoding
ByteString PDF_NameEncode(const ByteString &orig)
BlendMode
Definition fx_dib.h:49
#define FX_FILESIZE
Definition fx_types.h:19
#define CHECK(cvref)
#define CONSTRUCT_VIA_MAKE_RETAIN
Definition retain_ptr.h:224