blob: f1f165afcb3a3333029cf6f228728da88f4409d7 [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
Ryan Harrison275e2602017-09-18 14:23:18 -040076 bool GetFaceName(void* hFont, 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);
Ryan Harrison275e2602017-09-18 14:23:18 -040084 *name = 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
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400106FPDF_EXPORT void FPDF_CALLCONV 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
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400113FPDF_EXPORT void FPDF_CALLCONV
114FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 if (pFontInfoExt->version != 1)
116 return;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700119 pdfium::MakeUnique<CFX_ExternalFontInfo>(pFontInfoExt));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700120}
121
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400122FPDF_EXPORT const FPDF_CharsetFontMap* FPDF_CALLCONV FPDF_GetDefaultTTFMap() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 return CPWL_FontMap::defaultTTFMap;
Tom Sepez2a0bb3b2015-05-12 12:37:14 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO {
Dan Sinclairaee0db02017-09-21 16:53:58 -0400127 UnownedPtr<IFX_SystemFontInfo> m_pFontInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700128};
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
thestig24508df2016-05-27 15:14:20 -0700131 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
Tom Sepezd0409af2017-05-25 15:53:57 -0700132 delete pDefault->m_pFontInfo.Release();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
thestig907a5222016-06-21 14:38:27 -0700136 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
137 pDefault->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700138}
139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis,
141 int weight,
142 int bItalic,
143 int charset,
144 int pitch_family,
145 const char* family,
146 int* bExact) {
thestig907a5222016-06-21 14:38:27 -0700147 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
tsepez95e58342016-10-28 11:34:42 -0700148 return pDefault->m_pFontInfo->MapFont(weight, !!bItalic, charset,
149 pitch_family, family, *bExact);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700150}
151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
thestig907a5222016-06-21 14:38:27 -0700153 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
154 return pDefault->m_pFontInfo->GetFont(family);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700155}
156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis,
158 void* hFont,
159 unsigned int table,
160 unsigned char* buffer,
161 unsigned long buf_size) {
thestig907a5222016-06-21 14:38:27 -0700162 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
163 return pDefault->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700164}
165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
167 void* hFont,
168 char* buffer,
169 unsigned long buf_size) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400170 ByteString name;
thestig907a5222016-06-21 14:38:27 -0700171 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700172 if (!pDefault->m_pFontInfo->GetFaceName(hFont, &name))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 return 0;
Ryan Harrison875e98c2017-09-27 10:53:11 -0400174 if (name.GetLength() >= static_cast<size_t>(buf_size))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 return name.GetLength() + 1;
Dan Sinclair7e7c6492017-04-03 14:50:05 -0400176
177 strncpy(buffer, name.c_str(),
Ryan Harrison275e2602017-09-18 14:23:18 -0400178 (name.GetLength() + 1) * sizeof(ByteString::CharType));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 return name.GetLength() + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700180}
181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
183 int charset;
thestig907a5222016-06-21 14:38:27 -0700184 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
Tom Sepezec8ff7d2017-04-07 16:58:00 -0700185 if (!pDefault->m_pFontInfo->GetFontCharset(hFont, &charset))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 return 0;
187 return charset;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700188}
189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
thestig907a5222016-06-21 14:38:27 -0700191 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis);
192 pDefault->m_pFontInfo->DeleteFont(hFont);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700193}
194
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400195FPDF_EXPORT FPDF_SYSFONTINFO* FPDF_CALLCONV FPDF_GetDefaultSystemFontInfo() {
thestig24508df2016-05-27 15:14:20 -0700196 std::unique_ptr<IFX_SystemFontInfo> pFontInfo =
197 IFX_SystemFontInfo::CreateDefault(nullptr);
Lei Zhang412e9082015-12-14 18:34:00 -0800198 if (!pFontInfo)
thestig24508df2016-05-27 15:14:20 -0700199 return nullptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt =
202 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
203 pFontInfoExt->DeleteFont = DefaultDeleteFont;
204 pFontInfoExt->EnumFonts = DefaultEnumFonts;
205 pFontInfoExt->GetFaceName = DefaultGetFaceName;
206 pFontInfoExt->GetFont = DefaultGetFont;
207 pFontInfoExt->GetFontCharset = DefaultGetFontCharset;
208 pFontInfoExt->GetFontData = DefaultGetFontData;
209 pFontInfoExt->MapFont = DefaultMapFont;
210 pFontInfoExt->Release = DefaultRelease;
211 pFontInfoExt->version = 1;
thestig24508df2016-05-27 15:14:20 -0700212 pFontInfoExt->m_pFontInfo = pFontInfo.release();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 return pFontInfoExt;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700214}
npm788217d2016-11-08 16:48:36 -0800215
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400216FPDF_EXPORT void FPDF_CALLCONV
Dan Sinclair4bb4e842017-07-18 09:58:58 -0400217FPDF_FreeDefaultSystemFontInfo(FPDF_SYSFONTINFO* pDefaultFontInfo) {
npmbe6b1482016-11-10 10:12:30 -0800218 FX_Free(static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pDefaultFontInfo));
npm788217d2016-11-08 16:48:36 -0800219}