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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "../../public/fpdf_sysfontinfo.h"
8 #include "../include/fsdk_define.h"
9 #include "../include/pdfwindow/PWL_FontMap.h"
10 
11 class CSysFontInfo_Ext FX_FINAL : public IFX_SystemFontInfo
12 {
13 public:
14 	FPDF_SYSFONTINFO*	m_pInfo;
15 
Release()16 	virtual void		Release() FX_OVERRIDE
17 	{
18 		if (m_pInfo->Release)
19 			m_pInfo->Release(m_pInfo);
20 		delete this;
21 	}
22 
EnumFontList(CFX_FontMapper * pMapper)23 	virtual	FX_BOOL		EnumFontList(CFX_FontMapper* pMapper) FX_OVERRIDE
24 	{
25 		if (m_pInfo->EnumFonts) {
26 			m_pInfo->EnumFonts(m_pInfo, pMapper);
27 			return TRUE;
28 		}
29 		return FALSE;
30 	}
31 
MapFont(int weight,FX_BOOL bItalic,int charset,int pitch_family,FX_LPCSTR family,FX_BOOL & bExact)32 	virtual void*		MapFont(int weight, FX_BOOL bItalic, int charset, int pitch_family, FX_LPCSTR family, FX_BOOL& bExact)  FX_OVERRIDE
33 	{
34 		if (m_pInfo->MapFont)
35 			return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family, family, &bExact);
36 		return NULL;
37 	}
38 
GetFont(FX_LPCSTR family)39 	virtual void*		GetFont(FX_LPCSTR family)  FX_OVERRIDE
40 	{
41 		if (m_pInfo->GetFont)
42 			return m_pInfo->GetFont(m_pInfo, family);
43 		return NULL;
44 	}
45 
GetFontData(void * hFont,FX_DWORD table,FX_LPBYTE buffer,FX_DWORD size)46 	virtual FX_DWORD	GetFontData(void* hFont, FX_DWORD table, FX_LPBYTE buffer, FX_DWORD size)  FX_OVERRIDE
47 	{
48 		if (m_pInfo->GetFontData)
49 			return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size);
50 		return 0;
51 	}
52 
GetFaceName(void * hFont,CFX_ByteString & name)53 	virtual FX_BOOL		GetFaceName(void* hFont, CFX_ByteString& name)  FX_OVERRIDE
54 	{
55 		if (m_pInfo->GetFaceName == NULL) return FALSE;
56 		FX_DWORD size = m_pInfo->GetFaceName(m_pInfo, hFont, NULL, 0);
57 		if (size == 0) return FALSE;
58 		char* buffer = FX_Alloc(char, size);
59 		size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size);
60 		name = CFX_ByteString(buffer, size);
61 		FX_Free(buffer);
62 		return TRUE;
63 	}
64 
GetFontCharset(void * hFont,int & charset)65 	virtual FX_BOOL		GetFontCharset(void* hFont, int& charset)  FX_OVERRIDE
66 	{
67 		if (m_pInfo->GetFontCharset) {
68 			charset = m_pInfo->GetFontCharset(m_pInfo, hFont);
69 			return TRUE;
70 		}
71 		return FALSE;
72 	}
73 
DeleteFont(void * hFont)74 	virtual void		DeleteFont(void* hFont)  FX_OVERRIDE
75 	{
76 		if (m_pInfo->DeleteFont)
77 			m_pInfo->DeleteFont(m_pInfo, hFont);
78 	}
79 
80 private:
~CSysFontInfo_Ext()81         ~CSysFontInfo_Ext() { }
82 };
83 
FPDF_AddInstalledFont(void * mapper,const char * name,int charset)84 DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper, const char* name, int charset)
85 {
86 	((CFX_FontMapper*)mapper)->AddInstalledFont(name, charset);
87 }
88 
FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO * pFontInfoExt)89 DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt)
90 {
91 	if (pFontInfoExt->version != 1) return;
92 
93 	CSysFontInfo_Ext* pFontInfo = new CSysFontInfo_Ext;
94 	pFontInfo->m_pInfo = pFontInfoExt;
95 	CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(pFontInfo);
96 }
97 
FPDF_GetDefaultTTFMap()98 DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap()
99 {
100     return CPWL_FontMap::defaultTTFMap;
101 }
102 
103 struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO
104 {
105 	IFX_SystemFontInfo*	m_pFontInfo;
106 };
107 
DefaultRelease(struct _FPDF_SYSFONTINFO * pThis)108 static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis)
109 {
110 	((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->Release();
111 }
112 
DefaultEnumFonts(struct _FPDF_SYSFONTINFO * pThis,void * pMapper)113 static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper)
114 {
115 	((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper);
116 }
117 
DefaultMapFont(struct _FPDF_SYSFONTINFO * pThis,int weight,int bItalic,int charset,int pitch_family,const char * family,int * bExact)118 static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis, int weight, int bItalic, int charset, int pitch_family, const char* family, int* bExact)
119 {
120 	return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->MapFont(weight, bItalic, charset, pitch_family, family, *bExact);
121 }
122 
DefaultGetFont(struct _FPDF_SYSFONTINFO * pThis,const char * family)123 void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family)
124 {
125 	return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFont(family);
126 }
127 
DefaultGetFontData(struct _FPDF_SYSFONTINFO * pThis,void * hFont,unsigned int table,unsigned char * buffer,unsigned long buf_size)128 static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis, void* hFont,
129 			unsigned int table, unsigned char* buffer, unsigned long buf_size)
130 {
131 	return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size);
132 }
133 
DefaultGetFaceName(struct _FPDF_SYSFONTINFO * pThis,void * hFont,char * buffer,unsigned long buf_size)134 static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis, void* hFont, char* buffer, unsigned long buf_size)
135 {
136 	CFX_ByteString name;
137 	if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFaceName(hFont, name)) return 0;
138 	if (name.GetLength() >= (long)buf_size) return name.GetLength() + 1;
139 	FXSYS_strcpy(buffer, name);
140 	return name.GetLength() + 1;
141 }
142 
DefaultGetFontCharset(struct _FPDF_SYSFONTINFO * pThis,void * hFont)143 static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont)
144 {
145 	int charset;
146 	if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFontCharset(hFont, charset)) return 0;
147 	return charset;
148 }
149 
DefaultDeleteFont(struct _FPDF_SYSFONTINFO * pThis,void * hFont)150 static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont)
151 {
152 	((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->DeleteFont(hFont);
153 }
154 
FPDF_GetDefaultSystemFontInfo()155 DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo()
156 {
157 	IFX_SystemFontInfo* pFontInfo = IFX_SystemFontInfo::CreateDefault();
158 	if (pFontInfo == NULL) return NULL;
159 
160 	FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt = FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1);
161 	pFontInfoExt->DeleteFont = DefaultDeleteFont;
162 	pFontInfoExt->EnumFonts = DefaultEnumFonts;
163 	pFontInfoExt->GetFaceName = DefaultGetFaceName;
164 	pFontInfoExt->GetFont = DefaultGetFont;
165 	pFontInfoExt->GetFontCharset = DefaultGetFontCharset;
166 	pFontInfoExt->GetFontData = DefaultGetFontData;
167 	pFontInfoExt->MapFont = DefaultMapFont;
168 	pFontInfoExt->Release = DefaultRelease;
169 	pFontInfoExt->version = 1;
170 	pFontInfoExt->m_pFontInfo = pFontInfo;
171 	return pFontInfoExt;
172 }
173