blob: 5f80ca0860fdbbe5005e3adf80f7ae889e22554c [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek5110c472014-05-17 22:33:34 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_sysfontinfo.h"
8
dsinclair74a34fc2016-09-29 16:41:42 -07009#include "core/fxge/cfx_fontmapper.h"
10#include "core/fxge/cfx_gemodule.h"
11#include "core/fxge/fx_font.h"
12#include "core/fxge/ifx_systemfontinfo.h"
dsinclair114e46a2016-09-29 17:18:21 -070013#include "fpdfsdk/fsdk_define.h"
dan sinclair89e904b2016-03-23 19:29:15 -040014#include "fpdfsdk/pdfwindow/PWL_FontMap.h"
John Abd-El-Malek5110c472014-05-17 22:33:34 -070015
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016class CFX_ExternalFontInfo final : public IFX_SystemFontInfo {
17 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070018 explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
thestig24508df2016-05-27 15:14:20 -070019 ~CFX_ExternalFontInfo() override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020 if (m_pInfo->Release)
21 m_pInfo->Release(m_pInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070023
Lei Zhang2b1a2d52015-08-14 22:16:22 -070024 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 if (m_pInfo->EnumFonts) {
26 m_pInfo->EnumFonts(m_pInfo, pMapper);
27 return TRUE;
28 }
29 return FALSE;
30 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070031
Lei Zhang2b1a2d52015-08-14 22:16:22 -070032 void* MapFont(int weight,
33 FX_BOOL bItalic,
34 int charset,
35 int pitch_family,
36 const FX_CHAR* family,
37 int& iExact) override {
thestig907a5222016-06-21 14:38:27 -070038 if (!m_pInfo->MapFont)
39 return nullptr;
40 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family,
41 family, &iExact);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070043
Lei Zhang2b1a2d52015-08-14 22:16:22 -070044 void* GetFont(const FX_CHAR* family) override {
thestig907a5222016-06-21 14:38:27 -070045 if (!m_pInfo->GetFont)
46 return nullptr;
47 return m_pInfo->GetFont(m_pInfo, family);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070049
tsepezc3255f52016-03-25 14:52:27 -070050 uint32_t GetFontData(void* hFont,
51 uint32_t table,
Lei Zhang2b1a2d52015-08-14 22:16:22 -070052 uint8_t* buffer,
tsepezc3255f52016-03-25 14:52:27 -070053 uint32_t size) override {
thestig907a5222016-06-21 14:38:27 -070054 if (!m_pInfo->GetFontData)
55 return 0;
56 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070058
Lei Zhang2b1a2d52015-08-14 22:16:22 -070059 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override {
Lei Zhang412e9082015-12-14 18:34:00 -080060 if (!m_pInfo->GetFaceName)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 return FALSE;
thestig1cd352e2016-06-07 17:53:06 -070062 uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 if (size == 0)
64 return FALSE;
65 char* buffer = FX_Alloc(char, size);
66 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size);
67 name = CFX_ByteString(buffer, size);
68 FX_Free(buffer);
69 return TRUE;
70 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070071
Lei Zhang2b1a2d52015-08-14 22:16:22 -070072 FX_BOOL GetFontCharset(void* hFont, int& charset) override {
thestig907a5222016-06-21 14:38:27 -070073 if (!m_pInfo->GetFontCharset)
74 return FALSE;
75
76 charset = m_pInfo->GetFontCharset(m_pInfo, hFont);
77 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070079
Lei Zhang2b1a2d52015-08-14 22:16:22 -070080 void DeleteFont(void* hFont) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 if (m_pInfo->DeleteFont)
82 m_pInfo->DeleteFont(m_pInfo, hFont);
83 }
Tom Sepeze5b59ca2015-01-09 11:46:17 -080084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 private:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 FPDF_SYSFONTINFO* const m_pInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070087};
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper,
90 const char* name,
91 int charset) {
thestig907a5222016-06-21 14:38:27 -070092 CFX_FontMapper* pMapper = reinterpret_cast<CFX_FontMapper*>(mapper);
93 pMapper->AddInstalledFont(name, charset);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
97 if (pFontInfoExt->version != 1)
98 return;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
thestig24508df2016-05-27 15:14:20 -0700101 std::unique_ptr<IFX_SystemFontInfo>(
102 new CFX_ExternalFontInfo(pFontInfoExt)));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700103}
104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() {
106 return CPWL_FontMap::defaultTTFMap;
Tom Sepez2a0bb3b2015-05-12 12:37:14 -0700107}
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO {
110 IFX_SystemFontInfo* m_pFontInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700111};
112
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
thestig24508df2016-05-27 15:14:20 -0700114 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
115 // TODO(thestig): Should this be set to nullptr too?
116 delete pDefault->m_pFontInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700117}
118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
thestig907a5222016-06-21 14:38:27 -0700120 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
121 pDefault->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700122}
123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis,
125 int weight,
126 int bItalic,
127 int charset,
128 int pitch_family,
129 const char* family,
130 int* bExact) {
thestig907a5222016-06-21 14:38:27 -0700131 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
132 return pDefault->m_pFontInfo->MapFont(weight, bItalic, charset, pitch_family,
133 family, *bExact);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
thestig907a5222016-06-21 14:38:27 -0700137 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
138 return pDefault->m_pFontInfo->GetFont(family);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700139}
140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis,
142 void* hFont,
143 unsigned int table,
144 unsigned char* buffer,
145 unsigned long buf_size) {
thestig907a5222016-06-21 14:38:27 -0700146 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
147 return pDefault->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700148}
149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
151 void* hFont,
152 char* buffer,
153 unsigned long buf_size) {
154 CFX_ByteString name;
thestig907a5222016-06-21 14:38:27 -0700155 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
156 if (!pDefault->m_pFontInfo->GetFaceName(hFont, name))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 return 0;
158 if (name.GetLength() >= (long)buf_size)
159 return name.GetLength() + 1;
tsepezb4c9f3f2016-04-13 15:41:21 -0700160 FXSYS_strcpy(buffer, name.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 return name.GetLength() + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700162}
163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
165 int charset;
thestig907a5222016-06-21 14:38:27 -0700166 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
167 if (!pDefault->m_pFontInfo->GetFontCharset(hFont, charset))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 return 0;
169 return charset;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700170}
171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
thestig907a5222016-06-21 14:38:27 -0700173 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
174 pDefault->m_pFontInfo->DeleteFont(hFont);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700175}
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() {
thestig24508df2016-05-27 15:14:20 -0700178 std::unique_ptr<IFX_SystemFontInfo> pFontInfo =
179 IFX_SystemFontInfo::CreateDefault(nullptr);
Lei Zhang412e9082015-12-14 18:34:00 -0800180 if (!pFontInfo)
thestig24508df2016-05-27 15:14:20 -0700181 return nullptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt =
184 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
185 pFontInfoExt->DeleteFont = DefaultDeleteFont;
186 pFontInfoExt->EnumFonts = DefaultEnumFonts;
187 pFontInfoExt->GetFaceName = DefaultGetFaceName;
188 pFontInfoExt->GetFont = DefaultGetFont;
189 pFontInfoExt->GetFontCharset = DefaultGetFontCharset;
190 pFontInfoExt->GetFontData = DefaultGetFontData;
191 pFontInfoExt->MapFont = DefaultMapFont;
192 pFontInfoExt->Release = DefaultRelease;
193 pFontInfoExt->version = 1;
thestig24508df2016-05-27 15:14:20 -0700194 pFontInfoExt->m_pFontInfo = pFontInfo.release();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 return pFontInfoExt;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700196}