OLD | NEW |
---|---|
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "public/fpdf_sysfontinfo.h" | 7 #include "public/fpdf_sysfontinfo.h" |
8 | 8 |
9 #include "core/fxge/include/fx_font.h" | 9 #include "core/fxge/include/fx_font.h" |
10 #include "core/fxge/include/fx_ge.h" | 10 #include "core/fxge/include/fx_ge.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 } | 26 } |
27 return FALSE; | 27 return FALSE; |
28 } | 28 } |
29 | 29 |
30 void* MapFont(int weight, | 30 void* MapFont(int weight, |
31 FX_BOOL bItalic, | 31 FX_BOOL bItalic, |
32 int charset, | 32 int charset, |
33 int pitch_family, | 33 int pitch_family, |
34 const FX_CHAR* family, | 34 const FX_CHAR* family, |
35 int& iExact) override { | 35 int& iExact) override { |
36 if (m_pInfo->MapFont) | 36 if (!m_pInfo->MapFont) |
37 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family, | 37 return nullptr; |
38 family, &iExact); | 38 return m_pInfo->MapFont(m_pInfo, weight, bItalic, charset, pitch_family, |
39 return nullptr; | 39 family, &iExact); |
40 } | 40 } |
41 | 41 |
42 void* GetFont(const FX_CHAR* family) override { | 42 void* GetFont(const FX_CHAR* family) override { |
43 if (m_pInfo->GetFont) | 43 if (!m_pInfo->GetFont) |
44 return m_pInfo->GetFont(m_pInfo, family); | 44 return nullptr; |
45 return nullptr; | 45 return m_pInfo->GetFont(m_pInfo, family); |
46 } | 46 } |
47 | 47 |
48 uint32_t GetFontData(void* hFont, | 48 uint32_t GetFontData(void* hFont, |
49 uint32_t table, | 49 uint32_t table, |
50 uint8_t* buffer, | 50 uint8_t* buffer, |
51 uint32_t size) override { | 51 uint32_t size) override { |
52 if (m_pInfo->GetFontData) | 52 if (!m_pInfo->GetFontData) |
53 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size); | 53 return 0; |
54 return 0; | 54 return m_pInfo->GetFontData(m_pInfo, hFont, table, buffer, size); |
55 } | 55 } |
56 | 56 |
57 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override { | 57 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override { |
58 if (!m_pInfo->GetFaceName) | 58 if (!m_pInfo->GetFaceName) |
59 return FALSE; | 59 return FALSE; |
60 uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0); | 60 uint32_t size = m_pInfo->GetFaceName(m_pInfo, hFont, nullptr, 0); |
61 if (size == 0) | 61 if (size == 0) |
62 return FALSE; | 62 return FALSE; |
63 char* buffer = FX_Alloc(char, size); | 63 char* buffer = FX_Alloc(char, size); |
64 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size); | 64 size = m_pInfo->GetFaceName(m_pInfo, hFont, buffer, size); |
65 name = CFX_ByteString(buffer, size); | 65 name = CFX_ByteString(buffer, size); |
66 FX_Free(buffer); | 66 FX_Free(buffer); |
67 return TRUE; | 67 return TRUE; |
68 } | 68 } |
69 | 69 |
70 FX_BOOL GetFontCharset(void* hFont, int& charset) override { | 70 FX_BOOL GetFontCharset(void* hFont, int& charset) override { |
71 if (m_pInfo->GetFontCharset) { | 71 if (!m_pInfo->GetFontCharset) |
72 charset = m_pInfo->GetFontCharset(m_pInfo, hFont); | 72 return FALSE; |
73 return TRUE; | 73 |
74 } | 74 charset = m_pInfo->GetFontCharset(m_pInfo, hFont); |
75 return FALSE; | 75 return TRUE; |
76 } | 76 } |
77 | 77 |
78 void DeleteFont(void* hFont) override { | 78 void DeleteFont(void* hFont) override { |
79 if (m_pInfo->DeleteFont) | 79 if (m_pInfo->DeleteFont) |
80 m_pInfo->DeleteFont(m_pInfo, hFont); | 80 m_pInfo->DeleteFont(m_pInfo, hFont); |
81 } | 81 } |
82 | 82 |
83 private: | 83 private: |
84 FPDF_SYSFONTINFO* const m_pInfo; | 84 FPDF_SYSFONTINFO* const m_pInfo; |
85 }; | 85 }; |
86 | 86 |
87 DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper, | 87 DLLEXPORT void STDCALL FPDF_AddInstalledFont(void* mapper, |
dsinclair
2016/06/21 20:43:38
Would be be possible in a followup to change this
Lei Zhang
2016/06/21 20:46:36
No. FPDF_AddInstalledFont() is a public API, so it
| |
88 const char* name, | 88 const char* name, |
89 int charset) { | 89 int charset) { |
90 ((CFX_FontMapper*)mapper)->AddInstalledFont(name, charset); | 90 CFX_FontMapper* pMapper = reinterpret_cast<CFX_FontMapper*>(mapper); |
91 pMapper->AddInstalledFont(name, charset); | |
91 } | 92 } |
92 | 93 |
93 DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) { | 94 DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) { |
94 if (pFontInfoExt->version != 1) | 95 if (pFontInfoExt->version != 1) |
95 return; | 96 return; |
96 | 97 |
97 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo( | 98 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo( |
98 std::unique_ptr<IFX_SystemFontInfo>( | 99 std::unique_ptr<IFX_SystemFontInfo>( |
99 new CFX_ExternalFontInfo(pFontInfoExt))); | 100 new CFX_ExternalFontInfo(pFontInfoExt))); |
100 } | 101 } |
101 | 102 |
102 DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() { | 103 DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() { |
103 return CPWL_FontMap::defaultTTFMap; | 104 return CPWL_FontMap::defaultTTFMap; |
104 } | 105 } |
105 | 106 |
106 struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO { | 107 struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO { |
107 IFX_SystemFontInfo* m_pFontInfo; | 108 IFX_SystemFontInfo* m_pFontInfo; |
108 }; | 109 }; |
109 | 110 |
110 static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) { | 111 static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) { |
111 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); | 112 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
112 // TODO(thestig): Should this be set to nullptr too? | 113 // TODO(thestig): Should this be set to nullptr too? |
113 delete pDefault->m_pFontInfo; | 114 delete pDefault->m_pFontInfo; |
114 } | 115 } |
115 | 116 |
116 static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) { | 117 static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) { |
117 ((FPDF_SYSFONTINFO_DEFAULT*)pThis) | 118 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
118 ->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper); | 119 pDefault->m_pFontInfo->EnumFontList((CFX_FontMapper*)pMapper); |
119 } | 120 } |
120 | 121 |
121 static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis, | 122 static void* DefaultMapFont(struct _FPDF_SYSFONTINFO* pThis, |
122 int weight, | 123 int weight, |
123 int bItalic, | 124 int bItalic, |
124 int charset, | 125 int charset, |
125 int pitch_family, | 126 int pitch_family, |
126 const char* family, | 127 const char* family, |
127 int* bExact) { | 128 int* bExact) { |
128 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis) | 129 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
129 ->m_pFontInfo->MapFont(weight, bItalic, charset, pitch_family, family, | 130 return pDefault->m_pFontInfo->MapFont(weight, bItalic, charset, pitch_family, |
130 *bExact); | 131 family, *bExact); |
131 } | 132 } |
132 | 133 |
133 void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) { | 134 void* DefaultGetFont(struct _FPDF_SYSFONTINFO* pThis, const char* family) { |
134 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->GetFont(family); | 135 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
136 return pDefault->m_pFontInfo->GetFont(family); | |
135 } | 137 } |
136 | 138 |
137 static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis, | 139 static unsigned long DefaultGetFontData(struct _FPDF_SYSFONTINFO* pThis, |
138 void* hFont, | 140 void* hFont, |
139 unsigned int table, | 141 unsigned int table, |
140 unsigned char* buffer, | 142 unsigned char* buffer, |
141 unsigned long buf_size) { | 143 unsigned long buf_size) { |
142 return ((FPDF_SYSFONTINFO_DEFAULT*)pThis) | 144 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
143 ->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size); | 145 return pDefault->m_pFontInfo->GetFontData(hFont, table, buffer, buf_size); |
144 } | 146 } |
145 | 147 |
146 static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis, | 148 static unsigned long DefaultGetFaceName(struct _FPDF_SYSFONTINFO* pThis, |
147 void* hFont, | 149 void* hFont, |
148 char* buffer, | 150 char* buffer, |
149 unsigned long buf_size) { | 151 unsigned long buf_size) { |
150 CFX_ByteString name; | 152 CFX_ByteString name; |
151 if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis) | 153 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
152 ->m_pFontInfo->GetFaceName(hFont, name)) | 154 if (!pDefault->m_pFontInfo->GetFaceName(hFont, name)) |
153 return 0; | 155 return 0; |
154 if (name.GetLength() >= (long)buf_size) | 156 if (name.GetLength() >= (long)buf_size) |
155 return name.GetLength() + 1; | 157 return name.GetLength() + 1; |
156 FXSYS_strcpy(buffer, name.c_str()); | 158 FXSYS_strcpy(buffer, name.c_str()); |
157 return name.GetLength() + 1; | 159 return name.GetLength() + 1; |
158 } | 160 } |
159 | 161 |
160 static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { | 162 static int DefaultGetFontCharset(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { |
161 int charset; | 163 int charset; |
162 if (!((FPDF_SYSFONTINFO_DEFAULT*)pThis) | 164 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
163 ->m_pFontInfo->GetFontCharset(hFont, charset)) | 165 if (!pDefault->m_pFontInfo->GetFontCharset(hFont, charset)) |
164 return 0; | 166 return 0; |
165 return charset; | 167 return charset; |
166 } | 168 } |
167 | 169 |
168 static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { | 170 static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { |
169 ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->DeleteFont(hFont); | 171 auto* pDefault = static_cast<FPDF_SYSFONTINFO_DEFAULT*>(pThis); |
172 pDefault->m_pFontInfo->DeleteFont(hFont); | |
170 } | 173 } |
171 | 174 |
172 DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() { | 175 DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() { |
173 std::unique_ptr<IFX_SystemFontInfo> pFontInfo = | 176 std::unique_ptr<IFX_SystemFontInfo> pFontInfo = |
174 IFX_SystemFontInfo::CreateDefault(nullptr); | 177 IFX_SystemFontInfo::CreateDefault(nullptr); |
175 if (!pFontInfo) | 178 if (!pFontInfo) |
176 return nullptr; | 179 return nullptr; |
177 | 180 |
178 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt = | 181 FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt = |
179 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1); | 182 FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1); |
180 pFontInfoExt->DeleteFont = DefaultDeleteFont; | 183 pFontInfoExt->DeleteFont = DefaultDeleteFont; |
181 pFontInfoExt->EnumFonts = DefaultEnumFonts; | 184 pFontInfoExt->EnumFonts = DefaultEnumFonts; |
182 pFontInfoExt->GetFaceName = DefaultGetFaceName; | 185 pFontInfoExt->GetFaceName = DefaultGetFaceName; |
183 pFontInfoExt->GetFont = DefaultGetFont; | 186 pFontInfoExt->GetFont = DefaultGetFont; |
184 pFontInfoExt->GetFontCharset = DefaultGetFontCharset; | 187 pFontInfoExt->GetFontCharset = DefaultGetFontCharset; |
185 pFontInfoExt->GetFontData = DefaultGetFontData; | 188 pFontInfoExt->GetFontData = DefaultGetFontData; |
186 pFontInfoExt->MapFont = DefaultMapFont; | 189 pFontInfoExt->MapFont = DefaultMapFont; |
187 pFontInfoExt->Release = DefaultRelease; | 190 pFontInfoExt->Release = DefaultRelease; |
188 pFontInfoExt->version = 1; | 191 pFontInfoExt->version = 1; |
189 pFontInfoExt->m_pFontInfo = pFontInfo.release(); | 192 pFontInfoExt->m_pFontInfo = pFontInfo.release(); |
190 return pFontInfoExt; | 193 return pFontInfoExt; |
191 } | 194 } |
OLD | NEW |