blob: 5dca4814d4d528e548f6884ec8ad49f612f64152 [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
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <memory>
10
Dan Sinclairf51a02a2017-04-19 12:46:53 -040011#include "core/fxcrt/fx_codepage.h"
dsinclair74a34fc2016-09-29 16:41:42 -070012#include "core/fxge/cfx_fontmapper.h"
13#include "core/fxge/cfx_gemodule.h"
14#include "core/fxge/fx_font.h"
15#include "core/fxge/ifx_systemfontinfo.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/fsdk_define.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040017#include "fpdfsdk/pwl/cpwl_font_map.h"
Tom Sepezfe91c6c2017-05-16 15:33:20 -070018#include "third_party/base/ptr_util.h"
John Abd-El-Malek5110c472014-05-17 22:33:34 -070019
Dan Sinclairf51a02a2017-04-19 12:46:53 -040020static_assert(FXFONT_ANSI_CHARSET == FX_CHARSET_ANSI, "Charset must match");
21static_assert(FXFONT_DEFAULT_CHARSET == FX_CHARSET_Default,
22 "Charset must match");
23static_assert(FXFONT_SYMBOL_CHARSET == FX_CHARSET_Symbol, "Charset must match");
24static_assert(FXFONT_SHIFTJIS_CHARSET == FX_CHARSET_ShiftJIS,
25 "Charset must match");
26static_assert(FXFONT_HANGEUL_CHARSET == FX_CHARSET_Hangul,
27 "Charset must match");
28static_assert(FXFONT_GB2312_CHARSET == FX_CHARSET_ChineseSimplified,
29 "Charset must match");
30static_assert(FXFONT_CHINESEBIG5_CHARSET == FX_CHARSET_ChineseTraditional,
31 "Charset must match");
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033class CFX_ExternalFontInfo final : public IFX_SystemFontInfo {
34 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070035 explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
thestig24508df2016-05-27 15:14:20 -070036 ~CFX_ExternalFontInfo() override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 if (m_pInfo->Release)
38 m_pInfo->Release(m_pInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070040
tsepez4cf55152016-11-02 14:37:54 -070041 bool EnumFontList(CFX_FontMapper* pMapper) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 if (m_pInfo->EnumFonts) {
43 m_pInfo->EnumFonts(m_pInfo, pMapper);
tsepez4cf55152016-11-02 14:37:54 -070044 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 }
tsepez4cf55152016-11-02 14:37:54 -070046 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070048
Lei Zhang2b1a2d52015-08-14 22:16:22 -070049 void* MapFont(int weight,
tsepez4cf55152016-11-02 14:37:54 -070050 bool bItalic,
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051 int charset,
52 int pitch_family,
Dan Sinclair812e96c2017-03-13 16:43:37 -040053 const char* family,
Lei Zhang2b1a2d52015-08-14 22:16:22 -070054 int& iExact) override {
thestig907a5222016-06-21 14:38:27 -070055 if (!m_pInfo->MapFont)
56 return nullptr;
57 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family,
58 family, &iExact);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070060
Dan Sinclair812e96c2017-03-13 16:43:37 -040061 void* GetFont(const char* family) override {
thestig907a5222016-06-21 14:38:27 -070062 if (!m_pInfo->GetFont)
63 return nullptr;
64 return m_pInfo->GetFont(m_pInfo, family);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070066
tsepezc3255f52016-03-25 14:52:27 -070067 uint32_t GetFontData(void* hFont,
68 uint32_t table,
Lei Zhang2b1a2d52015-08-14 22:16:22 -070069 uint8_t* buffer,
tsepezc3255f52016-03-25 14:52:27 -070070 uint32_t size) override {
thestig907a5222016-06-21 14:38:27 -070071 if (!m_pInfo->GetFontData)
72 return 0;
73 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070075
Tom Sepezec8ff7d2017-04-07 16:58:00 -070076 bool GetFaceName(void* hFont, CFX_ByteString* name) override {
Lei Zhang412e9082015-12-14 18:34:00 -080077 if (!m_pInfo->GetFaceName)
tsepez4cf55152016-11-02 14:37:54 -070078 return false;
thestig1cd352e2016-06-07 17:53:06 -070079 uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (size == 0)
tsepez4cf55152016-11-02 14:37:54 -070081 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 char* buffer = FX_Alloc(char, size);
83 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size);
Tom Sepezec8ff7d2017-04-07 16:58:00 -070084 *name = CFX_ByteString(buffer, size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 FX_Free(buffer);
tsepez4cf55152016-11-02 14:37:54 -070086 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070088
Tom Sepezec8ff7d2017-04-07 16:58:00 -070089 bool GetFontCharset(void* hFont, int* charset) override {
thestig907a5222016-06-21 14:38:27 -070090 if (!m_pInfo->GetFontCharset)
tsepez4cf55152016-11-02 14:37:54 -070091 return false;
thestig907a5222016-06-21 14:38:27 -070092
Tom Sepezec8ff7d2017-04-07 16:58:00 -070093 *charset = m_pInfo->GetFontCharset(m_pInfo, hFont);
tsepez4cf55152016-11-02 14:37:54 -070094 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070096
Lei Zhang2b1a2d52015-08-14 22:16:22 -070097 void DeleteFont(void* hFont) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 if (m_pInfo->DeleteFont)
99 m_pInfo->DeleteFont(m_pInfo, hFont);
100 }
Tom Sepeze5b59ca2015-01-09 11:46:17 -0800101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 private:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 FPDF_SYSFONTINFO* const m_pInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700104};
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper,
107 const char* name,
108 int charset) {
Nicolas Pena46abb662017-05-17 17:23:22 -0400109 CFX_FontMapper* pMapper = static_cast<CFX_FontMapper*>(mapper);
thestig907a5222016-06-21 14:38:27 -0700110 pMapper->AddInstalledFont(name, charset);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700111}
112
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
114 if (pFontInfoExt->version != 1)
115 return;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700118 pdfium::MakeUnique<CFX_ExternalFontInfo>(pFontInfoExt));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700119}
120
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() {
122 return CPWL_FontMap::defaultTTFMap;
Tom Sepez2a0bb3b2015-05-12 12:37:14 -0700123}
124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO {
Tom Sepezd0409af2017-05-25 15:53:57 -0700126 CFX_UnownedPtr<IFX_SystemFontInfo> m_pFontInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700127};
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
thestig24508df2016-05-27 15:14:20 -0700130 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
Tom Sepezd0409af2017-05-25 15:53:57 -0700131 delete pDefault->m_pFontInfo.Release();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
thestig907a5222016-06-21 14:38:27 -0700135 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
136 pDefault->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700137}
138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis,
140 int weight,
141 int bItalic,
142 int charset,
143 int pitch_family,
144 const char* family,
145 int* bExact) {
thestig907a5222016-06-21 14:38:27 -0700146 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
tsepez95e58342016-10-28 11:34:42 -0700147 return pDefault->m_pFontInfo->MapFont(weight, !!bItalic, charset,
148 pitch_family, family, *bExact);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700149}
150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
thestig907a5222016-06-21 14:38:27 -0700152 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
153 return pDefault->m_pFontInfo->GetFont(family);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700154}
155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis,
157 void* hFont,
158 unsigned int table,
159 unsigned char* buffer,
160 unsigned long buf_size) {
thestig907a5222016-06-21 14:38:27 -0700161 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
162 return pDefault->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700163}
164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
166 void* hFont,
167 char* buffer,
168 unsigned long buf_size) {
169 CFX_ByteString name;
thestig907a5222016-06-21 14:38:27 -0700170 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700171 if (!pDefault->m_pFontInfo->GetFaceName(hFont, &name))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 return 0;
173 if (name.GetLength() >= (long)buf_size)
174 return name.GetLength() + 1;
Dan Sinclair7e7c6492017-04-03 14:50:05 -0400175
176 strncpy(buffer, name.c_str(),
177 (name.GetLength() + 1) * sizeof(CFX_ByteString::CharType));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 return name.GetLength() + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700179}
180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
182 int charset;
thestig907a5222016-06-21 14:38:27 -0700183 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700184 if (!pDefault->m_pFontInfo->GetFontCharset(hFont, &charset))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 return 0;
186 return charset;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700187}
188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
thestig907a5222016-06-21 14:38:27 -0700190 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
191 pDefault->m_pFontInfo->DeleteFont(hFont);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700192}
193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() {
thestig24508df2016-05-27 15:14:20 -0700195 std::unique_ptr<IFX_SystemFontInfo> pFontInfo =
196 IFX_SystemFontInfo::CreateDefault(nullptr);
Lei Zhang412e9082015-12-14 18:34:00 -0800197 if (!pFontInfo)
thestig24508df2016-05-27 15:14:20 -0700198 return nullptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt =
201 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
202 pFontInfoExt->DeleteFont = DefaultDeleteFont;
203 pFontInfoExt->EnumFonts = DefaultEnumFonts;
204 pFontInfoExt->GetFaceName = DefaultGetFaceName;
205 pFontInfoExt->GetFont = DefaultGetFont;
206 pFontInfoExt->GetFontCharset = DefaultGetFontCharset;
207 pFontInfoExt->GetFontData = DefaultGetFontData;
208 pFontInfoExt->MapFont = DefaultMapFont;
209 pFontInfoExt->Release = DefaultRelease;
210 pFontInfoExt->version = 1;
thestig24508df2016-05-27 15:14:20 -0700211 pFontInfoExt->m_pFontInfo = pFontInfo.release();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 return pFontInfoExt;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700213}
npm788217d2016-11-08 16:48:36 -0800214
Dan Sinclair4bb4e842017-07-18 09:58:58 -0400215DLLEXPORT void STDCALL
216FPDF_FreeDefaultSystemFontInfo(FPDF_SYSFONTINFO* pDefaultFontInfo) {
npmbe6b1482016-11-10 10:12:30 -0800217 FX_Free(static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pDefaultFontInfo));
npm788217d2016-11-08 16:48:36 -0800218}