John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_sysfontinfo.h" |
| 8 | |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 9 | #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" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/fsdk_define.h" |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 14 | #include "fpdfsdk/pdfwindow/PWL_FontMap.h" |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 15 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 16 | class CFX_ExternalFontInfo final : public IFX_SystemFontInfo { |
| 17 | public: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 18 | explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {} |
thestig | 24508df | 2016-05-27 15:14:20 -0700 | [diff] [blame] | 19 | ~CFX_ExternalFontInfo() override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 20 | if (m_pInfo->Release) |
| 21 | m_pInfo->Release(m_pInfo); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 22 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 23 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 24 | FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 25 | if (m_pInfo->EnumFonts) { |
| 26 | m_pInfo->EnumFonts(m_pInfo, pMapper); |
| 27 | return TRUE; |
| 28 | } |
| 29 | return FALSE; |
| 30 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 31 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 32 | void* MapFont(int weight, |
| 33 | FX_BOOL bItalic, |
| 34 | int charset, |
| 35 | int pitch_family, |
| 36 | const FX_CHAR* family, |
| 37 | int& iExact) override { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 38 | if (!m_pInfo->MapFont) |
| 39 | return nullptr; |
| 40 | return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family, |
| 41 | family, &iExact); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 43 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 44 | void* GetFont(const FX_CHAR* family) override { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 45 | if (!m_pInfo->GetFont) |
| 46 | return nullptr; |
| 47 | return m_pInfo->GetFont(m_pInfo, family); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 48 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 49 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 50 | uint32_t GetFontData(void* hFont, |
| 51 | uint32_t table, |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 52 | uint8_t* buffer, |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 53 | uint32_t size) override { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 54 | if (!m_pInfo->GetFontData) |
| 55 | return 0; |
| 56 | return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 58 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 59 | FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 60 | if (!m_pInfo->GetFaceName) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | return FALSE; |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 62 | uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 71 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 72 | FX_BOOL GetFontCharset(void* hFont, int& charset) override { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 73 | if (!m_pInfo->GetFontCharset) |
| 74 | return FALSE; |
| 75 | |
| 76 | charset = m_pInfo->GetFontCharset(m_pInfo, hFont); |
| 77 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 79 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 80 | void DeleteFont(void* hFont) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | if (m_pInfo->DeleteFont) |
| 82 | m_pInfo->DeleteFont(m_pInfo, hFont); |
| 83 | } |
Tom Sepez | e5b59ca | 2015-01-09 11:46:17 -0800 | [diff] [blame] | 84 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | private: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | FPDF_SYSFONTINFO* const m_pInfo; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper, |
| 90 | const char* name, |
| 91 | int charset) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 92 | CFX_FontMapper* pMapper = reinterpret_cast<CFX_FontMapper*>(mapper); |
| 93 | pMapper->AddInstalledFont(name, charset); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) { |
| 97 | if (pFontInfoExt->version != 1) |
| 98 | return; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 99 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo( |
thestig | 24508df | 2016-05-27 15:14:20 -0700 | [diff] [blame] | 101 | std::unique_ptr<IFX_SystemFontInfo>( |
| 102 | new CFX_ExternalFontInfo(pFontInfoExt))); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() { |
| 106 | return CPWL_FontMap::defaultTTFMap; |
Tom Sepez | 2a0bb3b | 2015-05-12 12:37:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO { |
| 110 | IFX_SystemFontInfo* m_pFontInfo; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) { |
thestig | 24508df | 2016-05-27 15:14:20 -0700 | [diff] [blame] | 114 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 119 | static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 120 | auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
| 121 | pDefault->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | static 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) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 131 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 137 | auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
| 138 | return pDefault->m_pFontInfo->GetFont(family); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 141 | static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis, |
| 142 | void* hFont, |
| 143 | unsigned int table, |
| 144 | unsigned char* buffer, |
| 145 | unsigned long buf_size) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 146 | auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
| 147 | return pDefault->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis, |
| 151 | void* hFont, |
| 152 | char* buffer, |
| 153 | unsigned long buf_size) { |
| 154 | CFX_ByteString name; |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 155 | auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
| 156 | if (!pDefault->m_pFontInfo->GetFaceName(hFont, name)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | if (name.GetLength() >= (long)buf_size) |
| 159 | return name.GetLength() + 1; |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 160 | FXSYS_strcpy(buffer, name.c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | return name.GetLength() + 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { |
| 165 | int charset; |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 166 | auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
| 167 | if (!pDefault->m_pFontInfo->GetFontCharset(hFont, charset)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | return charset; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { |
thestig | 907a522 | 2016-06-21 14:38:27 -0700 | [diff] [blame] | 173 | auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
| 174 | pDefault->m_pFontInfo->DeleteFont(hFont); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() { |
thestig | 24508df | 2016-05-27 15:14:20 -0700 | [diff] [blame] | 178 | std::unique_ptr<IFX_SystemFontInfo> pFontInfo = |
| 179 | IFX_SystemFontInfo::CreateDefault(nullptr); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 180 | if (!pFontInfo) |
thestig | 24508df | 2016-05-27 15:14:20 -0700 | [diff] [blame] | 181 | return nullptr; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 182 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | 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; |
thestig | 24508df | 2016-05-27 15:14:20 -0700 | [diff] [blame] | 194 | pFontInfoExt->m_pFontInfo = pFontInfo.release(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | return pFontInfoExt; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 196 | } |