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