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
fpdf_sysfontinfo.cpp
Go to the documentation of this file.
1// Copyright 2014 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#include "public/fpdf_sysfontinfo.h"
8
9#include <stddef.h>
10
11#include <memory>
12
13#include "core/fxcrt/fx_codepage.h"
14#include "core/fxcrt/stl_util.h"
15#include "core/fxcrt/unowned_ptr.h"
16#include "core/fxge/cfx_font.h"
17#include "core/fxge/cfx_fontmapper.h"
18#include "core/fxge/cfx_fontmgr.h"
19#include "core/fxge/cfx_gemodule.h"
20#include "core/fxge/fx_font.h"
21#include "core/fxge/systemfontinfo_iface.h"
22#include "third_party/base/numerics/safe_conversions.h"
23
24#ifdef PDF_ENABLE_XFA
25#include "xfa/fgas/font/cfgas_fontmgr.h"
26#include "xfa/fgas/font/cfgas_gemodule.h"
27#endif
28
29static_assert(FXFONT_ANSI_CHARSET == static_cast<int>(FX_Charset::kANSI),
30 "Charset must match");
31static_assert(FXFONT_DEFAULT_CHARSET == static_cast<int>(FX_Charset::kDefault),
32 "Charset must match");
33static_assert(FXFONT_SYMBOL_CHARSET == static_cast<int>(FX_Charset::kSymbol),
34 "Charset must match");
35static_assert(FXFONT_SHIFTJIS_CHARSET ==
36 static_cast<int>(FX_Charset::kShiftJIS),
37 "Charset must match");
38static_assert(FXFONT_HANGEUL_CHARSET == static_cast<int>(FX_Charset::kHangul),
39 "Charset must match");
40static_assert(FXFONT_GB2312_CHARSET ==
41 static_cast<int>(FX_Charset::kChineseSimplified),
42 "Charset must match");
43static_assert(FXFONT_CHINESEBIG5_CHARSET ==
44 static_cast<int>(FX_Charset::kChineseTraditional),
45 "Charset must match");
46static_assert(FXFONT_GREEK_CHARSET ==
47 static_cast<int>(FX_Charset::kMSWin_Greek),
48 "Charset must match");
49static_assert(FXFONT_VIETNAMESE_CHARSET ==
50 static_cast<int>(FX_Charset::kMSWin_Vietnamese),
51 "Charset must match");
52static_assert(FXFONT_HEBREW_CHARSET ==
53 static_cast<int>(FX_Charset::kMSWin_Hebrew),
54 "Charset must match");
55static_assert(FXFONT_ARABIC_CHARSET ==
56 static_cast<int>(FX_Charset::kMSWin_Arabic),
57 "Charset must match");
58static_assert(FXFONT_CYRILLIC_CHARSET ==
59 static_cast<int>(FX_Charset::kMSWin_Cyrillic),
60 "Charset must match");
61static_assert(FXFONT_THAI_CHARSET == static_cast<int>(FX_Charset::kThai),
62 "Charset must match");
63static_assert(FXFONT_EASTERNEUROPEAN_CHARSET ==
64 static_cast<int>(FX_Charset::kMSWin_EasternEuropean),
65 "Charset must match");
66static_assert(offsetof(CFX_Font::CharsetFontMap, charset) ==
67 offsetof(FPDF_CharsetFontMap, charset),
68 "CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap");
69static_assert(offsetof(CFX_Font::CharsetFontMap, fontname) ==
70 offsetof(FPDF_CharsetFontMap, fontname),
71 "CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap");
72static_assert(sizeof(CFX_Font::CharsetFontMap) == sizeof(FPDF_CharsetFontMap),
73 "CFX_Font::CharsetFontMap must be same as FPDF_CharsetFontMap");
74
75class CFX_ExternalFontInfo final : public SystemFontInfoIface {
76 public:
77 explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
79 if (m_pInfo->Release)
80 m_pInfo->Release(m_pInfo);
81 }
82
83 bool EnumFontList(CFX_FontMapper* pMapper) override {
84 if (m_pInfo->EnumFonts) {
85 m_pInfo->EnumFonts(m_pInfo, pMapper);
86 return true;
87 }
88 return false;
89 }
90
91 void* MapFont(int weight,
92 bool bItalic,
93 FX_Charset charset,
94 int pitch_family,
95 const ByteString& face) override {
96 if (!m_pInfo->MapFont)
97 return nullptr;
98
99 int iExact;
100 return m_pInfo->MapFont(m_pInfo, weight, bItalic, static_cast<int>(charset),
101 pitch_family, face.c_str(), &iExact);
102 }
103
104 void* GetFont(const ByteString& family) override {
105 if (!m_pInfo->GetFont)
106 return nullptr;
107 return m_pInfo->GetFont(m_pInfo, family.c_str());
108 }
109
110 size_t GetFontData(void* hFont,
111 uint32_t table,
112 pdfium::span<uint8_t> buffer) override {
113 if (!m_pInfo->GetFontData)
114 return 0;
115 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer.data(),
116 fxcrt::CollectionSize<unsigned long>(buffer));
117 }
118
119 bool GetFaceName(void* hFont, ByteString* name) override {
120 if (!m_pInfo->GetFaceName)
121 return false;
122 unsigned long size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0);
123 if (size == 0)
124 return false;
125 char* buffer = FX_Alloc(char, size);
126 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size);
127 *name = ByteString(buffer, size);
128 FX_Free(buffer);
129 return true;
130 }
131
132 bool GetFontCharset(void* hFont, FX_Charset* charset) override {
133 if (!m_pInfo->GetFontCharset)
134 return false;
135
136 *charset = FX_GetCharsetFromInt(m_pInfo->GetFontCharset(m_pInfo, hFont));
137 return true;
138 }
139
140 void DeleteFont(void* hFont) override {
141 if (m_pInfo->DeleteFont)
142 m_pInfo->DeleteFont(m_pInfo, hFont);
143 }
144
145 private:
146 UnownedPtr<FPDF_SYSFONTINFO> const m_pInfo;
147};
148
150 const char* face,
151 int charset) {
152 CFX_FontMapper* pMapper = static_cast<CFX_FontMapper*>(mapper);
154}
155
157FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
158 if (pFontInfoExt->version != 1)
159 return;
160
162 std::make_unique<CFX_ExternalFontInfo>(pFontInfoExt));
163
164#ifdef PDF_ENABLE_XFA
165 CFGAS_GEModule::Get()->GetFontMgr()->EnumFonts();
166#endif
167}
168
169FPDF_EXPORT const FPDF_CharsetFontMap* FPDF_CALLCONV FPDF_GetDefaultTTFMap() {
170 return reinterpret_cast<const FPDF_CharsetFontMap*>(CFX_Font::kDefaultTTFMap);
171}
172
173struct FPDF_SYSFONTINFO_DEFAULT final : public FPDF_SYSFONTINFO {
175};
176
177static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
178 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
179 pDefault->m_pFontInfo.ClearAndDelete();
180}
181
182static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
183 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
184 pDefault->m_pFontInfo->EnumFontList(static_cast<CFX_FontMapper*>(pMapper));
185}
186
187static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis,
188 int weight,
189 FPDF_BOOL use_italic,
190 int charset,
191 int pitch_family,
192 const char* family,
193 FPDF_BOOL* /*exact*/) {
194 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
195 return pDefault->m_pFontInfo->MapFont(weight, !!use_italic,
197 pitch_family, family);
198}
199
200void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
201 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
202 return pDefault->m_pFontInfo->GetFont(family);
203}
204
205static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis,
206 void* hFont,
207 unsigned int table,
208 unsigned char* buffer,
209 unsigned long buf_size) {
210 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
211 return pdfium::base::checked_cast<unsigned long>(
212 pDefault->m_pFontInfo->GetFontData(hFont, table, {buffer, buf_size}));
213}
214
215static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
216 void* hFont,
217 char* buffer,
218 unsigned long buf_size) {
219 ByteString name;
220 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
221 if (!pDefault->m_pFontInfo->GetFaceName(hFont, &name))
222 return 0;
223
224 const unsigned long copy_length =
225 pdfium::base::checked_cast<unsigned long>(name.GetLength() + 1);
226 if (copy_length <= buf_size)
227 strncpy(buffer, name.c_str(), copy_length * sizeof(ByteString::CharType));
228
229 return copy_length;
230}
231
232static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
233 FX_Charset charset;
234 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
235 if (!pDefault->m_pFontInfo->GetFontCharset(hFont, &charset))
236 return 0;
237 return static_cast<int>(charset);
238}
239
240static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
241 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
242 pDefault->m_pFontInfo->DeleteFont(hFont);
243}
244
246 std::unique_ptr<SystemFontInfoIface> pFontInfo =
247 CFX_GEModule::Get()->GetPlatform()->CreateDefaultSystemFontInfo();
248 if (!pFontInfo)
249 return nullptr;
250
251 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt =
252 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
253 pFontInfoExt->DeleteFont = DefaultDeleteFont;
254 pFontInfoExt->EnumFonts = DefaultEnumFonts;
255 pFontInfoExt->GetFaceName = DefaultGetFaceName;
256 pFontInfoExt->GetFont = DefaultGetFont;
258 pFontInfoExt->GetFontData = DefaultGetFontData;
259 pFontInfoExt->MapFont = DefaultMapFont;
260 pFontInfoExt->Release = DefaultRelease;
261 pFontInfoExt->version = 1;
262 pFontInfoExt->m_pFontInfo = pFontInfo.release();
263 return pFontInfoExt;
264}
265
267FPDF_FreeDefaultSystemFontInfo(FPDF_SYSFONTINFO* pFontInfo) {
268 FX_Free(static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pFontInfo));
269}
void * GetFont(const ByteString &family) override
bool GetFontCharset(void *hFont, FX_Charset *charset) override
size_t GetFontData(void *hFont, uint32_t table, pdfium::span< uint8_t > buffer) override
CFX_ExternalFontInfo(FPDF_SYSFONTINFO *pInfo)
bool EnumFontList(CFX_FontMapper *pMapper) override
void DeleteFont(void *hFont) override
bool GetFaceName(void *hFont, ByteString *name) override
void * MapFont(int weight, bool bItalic, FX_Charset charset, int pitch_family, const ByteString &face) override
void AddInstalledFont(const ByteString &name, FX_Charset charset)
CFX_FontMapper * GetBuiltinMapper() const
Definition cfx_fontmgr.h:71
static const CharsetFontMap kDefaultTTFMap[]
Definition cfx_font.h:78
static CFX_GEModule * Get()
CFX_FontMgr * GetFontMgr() const
const char * c_str() const
Definition bytestring.h:76
ByteString & operator=(ByteString &&that) noexcept
static void * DefaultMapFont(struct _FPDF_SYSFONTINFO *pThis, int weight, FPDF_BOOL use_italic, int charset, int pitch_family, const char *family, FPDF_BOOL *)
void * DefaultGetFont(struct _FPDF_SYSFONTINFO *pThis, const char *family)
static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO *pThis, void *hFont, unsigned int table, unsigned char *buffer, unsigned long buf_size)
FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO *pFontInfoExt)
static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO *pThis, void *hFont)
static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO *pThis, void *hFont)
FPDF_EXPORT const FPDF_CharsetFontMap *FPDF_CALLCONV FPDF_GetDefaultTTFMap()
FPDF_EXPORT FPDF_SYSFONTINFO *FPDF_CALLCONV FPDF_GetDefaultSystemFontInfo()
static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO *pThis, void *hFont, char *buffer, unsigned long buf_size)
FPDF_EXPORT void FPDF_CALLCONV FPDF_AddInstalledFont(void *mapper, const char *face, int charset)
FPDF_EXPORT void FPDF_CALLCONV FPDF_FreeDefaultSystemFontInfo(FPDF_SYSFONTINFO *pFontInfo)
static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO *pThis, void *pMapper)
static void DefaultRelease(struct _FPDF_SYSFONTINFO *pThis)
#define FXFONT_DEFAULT_CHARSET
#define FXFONT_HANGEUL_CHARSET
#define FXFONT_EASTERNEUROPEAN_CHARSET
#define FXFONT_SHIFTJIS_CHARSET
#define FXFONT_SYMBOL_CHARSET
#define FXFONT_GREEK_CHARSET
#define FXFONT_CYRILLIC_CHARSET
#define FXFONT_HEBREW_CHARSET
#define FXFONT_ANSI_CHARSET
#define FXFONT_ARABIC_CHARSET
#define FXFONT_GB2312_CHARSET
#define FXFONT_CHINESEBIG5_CHARSET
#define FXFONT_VIETNAMESE_CHARSET
#define FXFONT_THAI_CHARSET
#define FPDF_CALLCONV
Definition fpdfview.h:227
#define FPDF_EXPORT
Definition fpdfview.h:221
FX_Charset
Definition fx_codepage.h:70
@ kChineseTraditional
@ kMSWin_EasternEuropean
@ kChineseSimplified
FX_Charset FX_GetCharsetFromInt(int value)
UnownedPtr< SystemFontInfoIface > m_pFontInfo
void(* Release)(struct _FPDF_SYSFONTINFO *pThis)
void(* DeleteFont)(struct _FPDF_SYSFONTINFO *pThis, void *hFont)
unsigned long(* GetFontData)(struct _FPDF_SYSFONTINFO *pThis, void *hFont, unsigned int table, unsigned char *buffer, unsigned long buf_size)
void *(* GetFont)(struct _FPDF_SYSFONTINFO *pThis, const char *face)
unsigned long(* GetFaceName)(struct _FPDF_SYSFONTINFO *pThis, void *hFont, char *buffer, unsigned long buf_size)
int(* GetFontCharset)(struct _FPDF_SYSFONTINFO *pThis, void *hFont)
void(* EnumFonts)(struct _FPDF_SYSFONTINFO *pThis, void *pMapper)
void *(* MapFont)(struct _FPDF_SYSFONTINFO *pThis, int weight, FPDF_BOOL bItalic, int charset, int pitch_family, const char *face, FPDF_BOOL *bExact)