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 | |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 9 | #include "fpdfsdk/include/fsdk_define.h" |
dan sinclair | 89e904b | 2016-03-23 19:29:15 -0400 | [diff] [blame] | 10 | #include "fpdfsdk/pdfwindow/PWL_FontMap.h" |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 11 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 12 | class CFX_ExternalFontInfo final : public IFX_SystemFontInfo { |
| 13 | public: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 14 | explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {} |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 15 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 16 | void Release() override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | if (m_pInfo->Release) |
| 18 | m_pInfo->Release(m_pInfo); |
| 19 | delete this; |
| 20 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 21 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 22 | FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 23 | if (m_pInfo->EnumFonts) { |
| 24 | m_pInfo->EnumFonts(m_pInfo, pMapper); |
| 25 | return TRUE; |
| 26 | } |
| 27 | return FALSE; |
| 28 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 29 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 30 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 41 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 42 | void* GetFont(const FX_CHAR* family) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 43 | if (m_pInfo->GetFont) |
| 44 | return m_pInfo->GetFont(m_pInfo, family); |
| 45 | return NULL; |
| 46 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 47 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 48 | uint32_t GetFontData(void* hFont, |
| 49 | uint32_t table, |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 50 | uint8_t* buffer, |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 51 | uint32_t size) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 52 | if (m_pInfo->GetFontData) |
| 53 | return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size); |
| 54 | return 0; |
| 55 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 56 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 57 | FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override { |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 58 | if (!m_pInfo->GetFaceName) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | return FALSE; |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 60 | uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, NULL, 0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 69 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 70 | FX_BOOL GetFontCharset(void* hFont, int& charset) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | if (m_pInfo->GetFontCharset) { |
| 72 | charset = m_pInfo->GetFontCharset(m_pInfo, hFont); |
| 73 | return TRUE; |
| 74 | } |
| 75 | return FALSE; |
| 76 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 77 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 78 | void DeleteFont(void* hFont) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | if (m_pInfo->DeleteFont) |
| 80 | m_pInfo->DeleteFont(m_pInfo, hFont); |
| 81 | } |
Tom Sepez | e5b59ca | 2015-01-09 11:46:17 -0800 | [diff] [blame] | 82 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | private: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 84 | ~CFX_ExternalFontInfo() override {} |
Tom Sepez | 58b2a2c | 2015-07-31 14:39:22 -0700 | [diff] [blame] | 85 | |
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) { |
| 92 | ((CFX_FontMapper*)mapper)->AddInstalledFont(name, charset); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) { |
| 96 | if (pFontInfoExt->version != 1) |
| 97 | return; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 98 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo( |
| 100 | new CFX_ExternalFontInfo(pFontInfoExt)); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() { |
| 104 | return CPWL_FontMap::defaultTTFMap; |
Tom Sepez | 2a0bb3b | 2015-05-12 12:37:14 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO { |
| 108 | IFX_SystemFontInfo* m_pFontInfo; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) { |
| 112 | ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->Release(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) { |
| 116 | ((FPDF_SYSFONTINFO_DEFAULT*)pThis) |
| 117 | ->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) { |
| 133 | return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFont(family); |
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 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | static 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; |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 155 | FXSYS_strcpy(buffer, name.c_str()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | return name.GetLength() + 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | static 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { |
| 168 | ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->DeleteFont(hFont); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 172 | IFX_SystemFontInfo* pFontInfo = IFX_SystemFontInfo::CreateDefault(nullptr); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 173 | if (!pFontInfo) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 174 | return NULL; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 175 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 176 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 189 | } |