1 // Copyright 2014 PDFium Authors. All rights reserved.
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 "fpdfsdk/include/fsdk_define.h"
10 #include "fpdfsdk/include/pdfwindow/PWL_FontMap.h"
11
12 class CFX_ExternalFontInfo final : public IFX_SystemFontInfo {
13 public:
CFX_ExternalFontInfo(FPDF_SYSFONTINFO * pInfo)14 explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
15
Release()16 void Release() override {
17 if (m_pInfo->Release)
18 m_pInfo->Release(m_pInfo);
19 delete this;
20 }
21
EnumFontList(CFX_FontMapper * pMapper)22 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override {
23 if (m_pInfo->EnumFonts) {
24 m_pInfo->EnumFonts(m_pInfo, pMapper);
25 return TRUE;
26 }
27 return FALSE;
28 }
29
MapFont(int weight,FX_BOOL bItalic,int charset,int pitch_family,const FX_CHAR * family,int & iExact)30 void* MapFont(int weight,
31 FX_BOOL bItalic,
32 int charset,
33 int pitch_family,
34 const FX_CHAR* family,
35 int& iExact) override {
36 if (m_pInfo->MapFont)
37 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family,
38 family, &iExact);
39 return NULL;
40 }
41
GetFont(const FX_CHAR * family)42 void* GetFont(const FX_CHAR* family) override {
43 if (m_pInfo->GetFont)
44 return m_pInfo->GetFont(m_pInfo, family);
45 return NULL;
46 }
47
GetFontData(void * hFont,FX_DWORD table,uint8_t * buffer,FX_DWORD size)48 FX_DWORD GetFontData(void* hFont,
49 FX_DWORD table,
50 uint8_t* buffer,
51 FX_DWORD size) override {
52 if (m_pInfo->GetFontData)
53 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size);
54 return 0;
55 }
56
GetFaceName(void * hFont,CFX_ByteString & name)57 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override {
58 if (!m_pInfo->GetFaceName)
59 return FALSE;
60 FX_DWORD size = m_pInfo->GetFaceName(m_pInfo, hFont, NULL, 0);
61 if (size == 0)
62 return FALSE;
63 char* buffer = FX_Alloc(char, size);
64 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size);
65 name = CFX_ByteString(buffer, size);
66 FX_Free(buffer);
67 return TRUE;
68 }
69
GetFontCharset(void * hFont,int & charset)70 FX_BOOL GetFontCharset(void* hFont, int& charset) override {
71 if (m_pInfo->GetFontCharset) {
72 charset = m_pInfo->GetFontCharset(m_pInfo, hFont);
73 return TRUE;
74 }
75 return FALSE;
76 }
77
DeleteFont(void * hFont)78 void DeleteFont(void* hFont) override {
79 if (m_pInfo->DeleteFont)
80 m_pInfo->DeleteFont(m_pInfo, hFont);
81 }
82
83 private:
~CFX_ExternalFontInfo()84 ~CFX_ExternalFontInfo() override {}
85
86 FPDF_SYSFONTINFO* const m_pInfo;
87 };
88
FPDF_AddInstalledFont(void * mapper,const char * name,int charset)89 DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper,
90 const char* name,
91 int charset) {
92 ((CFX_FontMapper*)mapper)->AddInstalledFont(name, charset);
93 }
94
FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO * pFontInfoExt)95 DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
96 if (pFontInfoExt->version != 1)
97 return;
98
99 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
100 new CFX_ExternalFontInfo(pFontInfoExt));
101 }
102
FPDF_GetDefaultTTFMap()103 DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() {
104 return CPWL_FontMap::defaultTTFMap;
105 }
106
107 struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO {
108 IFX_SystemFontInfo* m_pFontInfo;
109 };
110
DefaultRelease(struct _FPDF_SYSFONTINFO * pThis)111 static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
112 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->Release();
113 }
114
DefaultEnumFonts(struct _FPDF_SYSFONTINFO * pThis,void * pMapper)115 static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
116 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
117 ->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
118 }
119
DefaultMapFont(struct _FPDF_SYSFONTINFO * pThis,int weight,int bItalic,int charset,int pitch_family,const char * family,int * bExact)120 static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis,
121 int weight,
122 int bItalic,
123 int charset,
124 int pitch_family,
125 const char* family,
126 int* bExact) {
127 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
128 ->m_pFontInfo->MapFont(weight, bItalic, charset, pitch_family, family,
129 *bExact);
130 }
131
DefaultGetFont(struct _FPDF_SYSFONTINFO * pThis,const char * family)132 void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
133 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFont(family);
134 }
135
DefaultGetFontData(struct _FPDF_SYSFONTINFO * pThis,void * hFont,unsigned int table,unsigned char * buffer,unsigned long buf_size)136 static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis,
137 void* hFont,
138 unsigned int table,
139 unsigned char* buffer,
140 unsigned long buf_size) {
141 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
142 ->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size);
143 }
144
DefaultGetFaceName(struct _FPDF_SYSFONTINFO * pThis,void * hFont,char * buffer,unsigned long buf_size)145 static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
146 void* hFont,
147 char* buffer,
148 unsigned long buf_size) {
149 CFX_ByteString name;
150 if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis)
151 ->m_pFontInfo->GetFaceName(hFont, name))
152 return 0;
153 if (name.GetLength() >= (long)buf_size)
154 return name.GetLength() + 1;
155 FXSYS_strcpy(buffer, name);
156 return name.GetLength() + 1;
157 }
158
DefaultGetFontCharset(struct _FPDF_SYSFONTINFO * pThis,void * hFont)159 static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
160 int charset;
161 if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis)
162 ->m_pFontInfo->GetFontCharset(hFont, charset))
163 return 0;
164 return charset;
165 }
166
DefaultDeleteFont(struct _FPDF_SYSFONTINFO * pThis,void * hFont)167 static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
168 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->DeleteFont(hFont);
169 }
170
FPDF_GetDefaultSystemFontInfo()171 DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() {
172 IFX_SystemFontInfo* pFontInfo = IFX_SystemFontInfo::CreateDefault(nullptr);
173 if (!pFontInfo)
174 return NULL;
175
176 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt =
177 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
178 pFontInfoExt->DeleteFont = DefaultDeleteFont;
179 pFontInfoExt->EnumFonts = DefaultEnumFonts;
180 pFontInfoExt->GetFaceName = DefaultGetFaceName;
181 pFontInfoExt->GetFont = DefaultGetFont;
182 pFontInfoExt->GetFontCharset = DefaultGetFontCharset;
183 pFontInfoExt->GetFontData = DefaultGetFontData;
184 pFontInfoExt->MapFont = DefaultMapFont;
185 pFontInfoExt->Release = DefaultRelease;
186 pFontInfoExt->version = 1;
187 pFontInfoExt->m_pFontInfo = pFontInfo;
188 return pFontInfoExt;
189 }
190