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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../public/fpdf_sysfontinfo.h"
8 #include "../include/fsdk_define.h"
9 #include "../include/pdfwindow/PWL_FontMap.h"
11 class CFX_ExternalFontInfo final : public IFX_SystemFontInfo {
13 explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {}
15 void Release() override {
17 m_pInfo->Release(m_pInfo);
21 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override {
22 if (m_pInfo->EnumFonts) {
23 m_pInfo->EnumFonts(m_pInfo, pMapper);
29 void* MapFont(int weight,
33 const FX_CHAR* family,
34 int& iExact) override {
36 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family,
41 void* GetFont(const FX_CHAR* family) override {
43 return m_pInfo->GetFont(m_pInfo, family);
47 FX_DWORD GetFontData(void* hFont,
50 FX_DWORD size) override {
51 if (m_pInfo->GetFontData)
52 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size);
56 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override {
57 if (m_pInfo->GetFaceName == NULL)
59 FX_DWORD size = m_pInfo->GetFaceName(m_pInfo, hFont, NULL, 0);
62 char* buffer = FX_Alloc(char, size);
63 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size);
64 name = CFX_ByteString(buffer, size);
69 FX_BOOL GetFontCharset(void* hFont, int& charset) override {
70 if (m_pInfo->GetFontCharset) {
71 charset = m_pInfo->GetFontCharset(m_pInfo, hFont);
77 void DeleteFont(void* hFont) override {
78 if (m_pInfo->DeleteFont)
79 m_pInfo->DeleteFont(m_pInfo, hFont);
83 ~CFX_ExternalFontInfo() override {}
85 FPDF_SYSFONTINFO* const m_pInfo;
88 DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper,
91 ((CFX_FontMapper*)mapper)->AddInstalledFont(name, charset);
94 DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) {
95 if (pFontInfoExt->version != 1)
98 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
99 new CFX_ExternalFontInfo(pFontInfoExt));
102 DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() {
103 return CPWL_FontMap::defaultTTFMap;
106 struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO {
107 IFX_SystemFontInfo* m_pFontInfo;
110 static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) {
111 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->Release();
114 static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) {
115 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
116 ->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
119 static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis,
126 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
127 ->m_pFontInfo->MapFont(weight, bItalic, charset, pitch_family, family,
131 void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) {
132 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFont(family);
135 static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis,
138 unsigned char* buffer,
139 unsigned long buf_size) {
140 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)
141 ->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size);
144 static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis,
147 unsigned long buf_size) {
149 if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis)
150 ->m_pFontInfo->GetFaceName(hFont, name))
152 if (name.GetLength() >= (long)buf_size)
153 return name.GetLength() + 1;
154 FXSYS_strcpy(buffer, name);
155 return name.GetLength() + 1;
158 static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
160 if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis)
161 ->m_pFontInfo->GetFontCharset(hFont, charset))
166 static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) {
167 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->DeleteFont(hFont);
170 DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() {
171 IFX_SystemFontInfo* pFontInfo = IFX_SystemFontInfo::CreateDefault(nullptr);
172 if (pFontInfo == NULL)
175 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt =
176 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
177 pFontInfoExt->DeleteFont = DefaultDeleteFont;
178 pFontInfoExt->EnumFonts = DefaultEnumFonts;
179 pFontInfoExt->GetFaceName = DefaultGetFaceName;
180 pFontInfoExt->GetFont = DefaultGetFont;
181 pFontInfoExt->GetFontCharset = DefaultGetFontCharset;
182 pFontInfoExt->GetFontData = DefaultGetFontData;
183 pFontInfoExt->MapFont = DefaultMapFont;
184 pFontInfoExt->Release = DefaultRelease;
185 pFontInfoExt->version = 1;
186 pFontInfoExt->m_pFontInfo = pFontInfo;