blob: d5ee7d186fa396bb7e43841db0e6c9e675972c72 [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
Lei Zhangbde53d22015-11-12 22:21:30 -08009#include "fpdfsdk/include/fsdk_define.h"
dan sinclair89e904b2016-03-23 19:29:15 -040010#include "fpdfsdk/pdfwindow/PWL_FontMap.h"
John Abd-El-Malek5110c472014-05-17 22:33:34 -070011
Nico Weber9d8ec5a2015-08-04 13:00:21 -070012class CFX_ExternalFontInfo final : public IFX_SystemFontInfo {
13 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070014 explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
John Abd-El-Malek5110c472014-05-17 22:33:34 -070015
Lei Zhang2b1a2d52015-08-14 22:16:22 -070016 void Release() override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017 if (m_pInfo->Release)
18 m_pInfo->Release(m_pInfo);
19 delete this;
20 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070021
Lei Zhang2b1a2d52015-08-14 22:16:22 -070022 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 if (m_pInfo->EnumFonts) {
24 m_pInfo->EnumFonts(m_pInfo, pMapper);
25 return TRUE;
26 }
27 return FALSE;
28 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070029
Lei Zhang2b1a2d52015-08-14 22:16:22 -070030 void* MapFont(int weight,
31 FX_BOOL bItalic,
32 int charset,
33 int pitch_family,
34 const FX_CHAR* family,
35 int& iExact) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 if (m_pInfo->MapFont)
37 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family,
38 family, &iExact);
39 return NULL;
40 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070041
Lei Zhang2b1a2d52015-08-14 22:16:22 -070042 void* GetFont(const FX_CHAR* family) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 if (m_pInfo->GetFont)
44 return m_pInfo->GetFont(m_pInfo, family);
45 return NULL;
46 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070047
tsepezc3255f52016-03-25 14:52:27 -070048 uint32_t GetFontData(void* hFont,
49 uint32_t table,
Lei Zhang2b1a2d52015-08-14 22:16:22 -070050 uint8_t* buffer,
tsepezc3255f52016-03-25 14:52:27 -070051 uint32_t size) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 if (m_pInfo->GetFontData)
53 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size);
54 return 0;
55 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070056
Lei Zhang2b1a2d52015-08-14 22:16:22 -070057 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override {
Lei Zhang412e9082015-12-14 18:34:00 -080058 if (!m_pInfo->GetFaceName)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 return FALSE;
tsepezc3255f52016-03-25 14:52:27 -070060 uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, NULL, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 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 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070069
Lei Zhang2b1a2d52015-08-14 22:16:22 -070070 FX_BOOL GetFontCharset(void* hFont, int& charset) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (m_pInfo->GetFontCharset) {
72 charset = m_pInfo->GetFontCharset(m_pInfo, hFont);
73 return TRUE;
74 }
75 return FALSE;
76 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -070077
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 void DeleteFont(void* hFont) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 if (m_pInfo->DeleteFont)
80 m_pInfo->DeleteFont(m_pInfo, hFont);
81 }
Tom Sepeze5b59ca2015-01-09 11:46:17 -080082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 private:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070084 ~CFX_ExternalFontInfo() override {}
Tom Sepez58b2a2c2015-07-31 14:39:22 -070085
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) {
92 ((CFX_FontMapper*)mapper)->AddInstalledFont(name, charset);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
96 if (pFontInfoExt->version != 1)
97 return;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070098
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
100 new CFX_ExternalFontInfo(pFontInfoExt));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700101}
102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() {
104 return CPWL_FontMap::defaultTTFMap;
Tom Sepez2a0bb3b2015-05-12 12:37:14 -0700105}
106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO {
108 IFX_SystemFontInfo* m_pFontInfo;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700109};
110
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
112 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->Release();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700113}
114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
116 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
117 ->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700118}
119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120static 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);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
133 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFont(family);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136static 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);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145static 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;
tsepezb4c9f3f2016-04-13 15:41:21 -0700155 FXSYS_strcpy(buffer, name.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 return name.GetLength() + 1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700157}
158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159static 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;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700165}
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
168 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->DeleteFont(hFont);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700169}
170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() {
Lei Zhang6f62d532015-09-23 15:31:44 -0700172 IFX_SystemFontInfo* pFontInfo = IFX_SystemFontInfo::CreateDefault(nullptr);
Lei Zhang412e9082015-12-14 18:34:00 -0800173 if (!pFontInfo)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 return NULL;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 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;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700189}