Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: core/src/fxge/win32/fx_win32_device.cpp

Issue 1306883002: Added a fallback Win32 font information class for win32k lockdown. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Added font fallback class. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "../../../include/fxge/fx_ge.h" 7 #include "../../../include/fxge/fx_ge.h"
8 8
9 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ 9 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_
10 #include <crtdbg.h> 10 #include <crtdbg.h>
11 11
12 #include "../../../include/fxcodec/fx_codec.h" 12 #include "../../../include/fxcodec/fx_codec.h"
13 #include "../../../include/fxge/fx_freetype.h" 13 #include "../../../include/fxge/fx_freetype.h"
14 #include "../../../include/fxge/fx_ge_win32.h" 14 #include "../../../include/fxge/fx_ge_win32.h"
15 #include "../agg/include/fx_agg_driver.h" 15 #include "../agg/include/fx_agg_driver.h"
16 #include "../dib/dib_int.h" 16 #include "../dib/dib_int.h"
17 #include "../ge/text_int.h" 17 #include "../ge/text_int.h"
18 #include "dwrite_int.h" 18 #include "dwrite_int.h"
19 #include "win32_int.h" 19 #include "win32_int.h"
20 20
21 class CFX_Win32FallbackFontInfo final : public CFX_FolderFontInfo {
22 public:
23 void* MapFont(int weight,
24 FX_BOOL bItalic,
25 int charset,
26 int pitch_family,
27 const FX_CHAR* family,
28 int& iExact) override;
29
30 void* FindFont(int weight,
31 FX_BOOL bItalic,
32 int charset,
33 int pitch_family,
34 const FX_CHAR* family,
35 FX_BOOL bMatchName);
36 };
21 class CFX_Win32FontInfo final : public IFX_SystemFontInfo { 37 class CFX_Win32FontInfo final : public IFX_SystemFontInfo {
22 public: 38 public:
23 CFX_Win32FontInfo(); 39 CFX_Win32FontInfo();
24 ~CFX_Win32FontInfo() override; 40 ~CFX_Win32FontInfo() override;
25 41
26 // IFX_SystemFontInfo 42 // IFX_SystemFontInfo
27 void Release() override; 43 void Release() override;
28 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override; 44 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override;
29 void* MapFont(int weight, 45 void* MapFont(int weight,
30 FX_BOOL bItalic, 46 FX_BOOL bItalic,
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 if (thisname[0] == ' ') { 208 if (thisname[0] == ' ') {
193 if (thisname.Mid(1, name.GetLength()) == name) { 209 if (thisname.Mid(1, name.GetLength()) == name) {
194 return m_pMapper->m_InstalledTTFonts[i + 1]; 210 return m_pMapper->m_InstalledTTFonts[i + 1];
195 } 211 }
196 } else if (thisname.Left(name.GetLength()) == name) { 212 } else if (thisname.Left(name.GetLength()) == name) {
197 return m_pMapper->m_InstalledTTFonts[i]; 213 return m_pMapper->m_InstalledTTFonts[i];
198 } 214 }
199 } 215 }
200 return CFX_ByteString(); 216 return CFX_ByteString();
201 } 217 }
218 void* CFX_Win32FallbackFontInfo::MapFont(int weight,
219 FX_BOOL bItalic,
220 int charset,
221 int pitch_family,
222 const FX_CHAR* cstr_face,
223 int& iExact) {
224 CFX_ByteString face = cstr_face;
Tom Sepez 2015/09/29 16:26:03 nit: prefer CFX_ByteString face(cstr_face);
forshaw 2015/09/29 16:46:23 Acknowledged.
225 int iBaseFont;
Tom Sepez 2015/09/29 16:26:03 nit: move this into the for(), eg for (int iBaseFo
forshaw 2015/09/29 16:46:23 Acknowledged.
226 for (iBaseFont = 0; iBaseFont < 12; iBaseFont++)
Tom Sepez 2015/09/29 16:26:03 use FX_ArraySize(Base14Substs) in place of hard-co
forshaw 2015/09/29 16:46:23 Acknowledged.
227 if (face == CFX_ByteStringC(Base14Substs[iBaseFont].m_pName)) {
Tom Sepez 2015/09/29 16:26:03 I thought CFX_Bytestring had an operator== method
forshaw 2015/09/29 16:46:23 Acknowledged.
228 face = Base14Substs[iBaseFont].m_pWinName;
229 iExact = 1;
230 break;
231 }
232 if (iBaseFont < 12) {
Tom Sepez 2015/09/29 16:26:03 move this into the loop in place of the break, eg
forshaw 2015/09/29 16:46:23 Acknowledged.
233 return GetFont(face);
234 }
235
236 FX_BOOL bCJK = TRUE;
237 switch (charset) {
238 case FXFONT_SHIFTJIS_CHARSET:
239 case FXFONT_GB2312_CHARSET:
240 case FXFONT_CHINESEBIG5_CHARSET:
241 case FXFONT_HANGEUL_CHARSET:
242 default:
243 bCJK = FALSE;
244 break;
245 }
246 if (charset == FXFONT_ANSI_CHARSET && (pitch_family & FXFONT_FF_FIXEDPITCH)) {
247 return GetFont("Courier New");
248 }
249 return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK);
250 }
251 static FX_DWORD _Win32GetCharset(int charset) {
Tom Sepez 2015/09/29 16:26:03 These are copied from linux or some such? Rather
forshaw 2015/09/29 16:46:23 Yeah ideally it would be nicer to just have a sing
252 switch (charset) {
253 case FXFONT_SHIFTJIS_CHARSET:
254 return CHARSET_FLAG_SHIFTJIS;
255 case FXFONT_GB2312_CHARSET:
256 return CHARSET_FLAG_GB;
257 case FXFONT_CHINESEBIG5_CHARSET:
258 return CHARSET_FLAG_BIG5;
259 case FXFONT_HANGEUL_CHARSET:
260 return CHARSET_FLAG_KOREAN;
261 case FXFONT_SYMBOL_CHARSET:
262 return CHARSET_FLAG_SYMBOL;
263 case FXFONT_ANSI_CHARSET:
264 return CHARSET_FLAG_ANSI;
265 default:
266 break;
267 }
268 return 0;
269 }
270 static int32_t _Win32GetSimilarValue(int weight,
271 FX_BOOL bItalic,
272 int pitch_family,
273 FX_DWORD style) {
274 int32_t iSimilarValue = 0;
275 if ((style & FXFONT_BOLD) == (weight > 400)) {
276 iSimilarValue += 16;
277 }
278 if ((style & FXFONT_ITALIC) == bItalic) {
279 iSimilarValue += 16;
280 }
281 if ((style & FXFONT_SERIF) == (pitch_family & FXFONT_FF_ROMAN)) {
282 iSimilarValue += 16;
283 }
284 if ((style & FXFONT_SCRIPT) == (pitch_family & FXFONT_FF_SCRIPT)) {
285 iSimilarValue += 8;
286 }
287 if ((style & FXFONT_FIXED_PITCH) == (pitch_family & FXFONT_FF_FIXEDPITCH)) {
288 iSimilarValue += 8;
289 }
290 return iSimilarValue;
291 }
292
293 void* CFX_Win32FallbackFontInfo::FindFont(int weight,
294 FX_BOOL bItalic,
295 int charset,
296 int pitch_family,
297 const FX_CHAR* family,
298 FX_BOOL bMatchName) {
299 CFX_FontFaceInfo* pFind = NULL;
300 FX_DWORD charset_flag = _Win32GetCharset(charset);
301 int32_t iBestSimilar = 0;
302 for (const auto& it : m_FontList) {
303 const CFX_ByteString& bsName = it.first;
304 CFX_FontFaceInfo* pFont = it.second;
305 if (!(pFont->m_Charsets & charset_flag) &&
306 charset != FXFONT_DEFAULT_CHARSET) {
307 continue;
308 }
309 int32_t iSimilarValue = 0;
310 int32_t index = bsName.Find(family);
311 if (bMatchName && index < 0) {
312 continue;
313 }
314 if (!bMatchName && index > 0) {
315 iSimilarValue += 64;
Tom Sepez 2015/09/29 16:26:03 Doesn't this increment get lost when we make the a
forshaw 2015/09/29 16:46:23 Looks that way, this was copied from the Linux ver
316 }
317 iSimilarValue =
318 _Win32GetSimilarValue(weight, bItalic, pitch_family, pFont->m_Styles);
319 if (iSimilarValue > iBestSimilar) {
320 iBestSimilar = iSimilarValue;
321 pFind = pFont;
322 }
323 }
324 return pFind;
325 }
326
202 struct _FontNameMap { 327 struct _FontNameMap {
203 const FX_CHAR* m_pSubFontName; 328 const FX_CHAR* m_pSubFontName;
204 const FX_CHAR* m_pSrcFontName; 329 const FX_CHAR* m_pSrcFontName;
205 }; 330 };
206 const _FontNameMap g_JpFontNameMap[] = { 331 const _FontNameMap g_JpFontNameMap[] = {
207 {"MS Mincho", "Heiseimin-W3"}, 332 {"MS Mincho", "Heiseimin-W3"},
208 {"MS Gothic", "Jun101-Light"}, 333 {"MS Gothic", "Jun101-Light"},
209 }; 334 };
210 extern "C" { 335 extern "C" {
211 static int compareString(const void* key, const void* element) { 336 static int compareString(const void* key, const void* element) {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 } 524 }
400 FX_BOOL CFX_Win32FontInfo::GetFontCharset(void* hFont, int& charset) { 525 FX_BOOL CFX_Win32FontInfo::GetFontCharset(void* hFont, int& charset) {
401 TEXTMETRIC tm; 526 TEXTMETRIC tm;
402 HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont); 527 HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont);
403 ::GetTextMetrics(m_hDC, &tm); 528 ::GetTextMetrics(m_hDC, &tm);
404 ::SelectObject(m_hDC, hOldFont); 529 ::SelectObject(m_hDC, hOldFont);
405 charset = tm.tmCharSet; 530 charset = tm.tmCharSet;
406 return TRUE; 531 return TRUE;
407 } 532 }
408 IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) { 533 IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) {
409 return new CFX_Win32FontInfo; 534 // If GDI disabled then GetDC for the desktop will fail. Select the fallback
Tom Sepez 2015/09/29 16:26:03 nit: is disabled, then
forshaw 2015/09/29 16:46:23 Acknowledged.
535 // font information class if GDI is disabled.
536 HDC hdc = GetDC(NULL);
537 if (!hdc) {
538 CFX_Win32FallbackFontInfo* pInfoFallback = new CFX_Win32FallbackFontInfo;
539 // Construct the font path manually, SHGetKnownFolderPath won't work under
540 // a restrictive sandbox.
541 CHAR windows_path[MAX_PATH] = {0};
542 DWORD path_len = GetWindowsDirectoryA(windows_path, MAX_PATH);
543 if (path_len > 0 && path_len < MAX_PATH) {
544 CFX_ByteString fonts_path(windows_path);
545 fonts_path += "\\Fonts";
546 pInfoFallback->AddPath(fonts_path);
547 }
548 return pInfoFallback;
549 } else {
Tom Sepez 2015/09/29 16:26:03 nit: else after return not neeed.
forshaw 2015/09/29 16:46:23 Acknowledged.
550 ReleaseDC(NULL, hdc);
551 return new CFX_Win32FontInfo;
552 }
410 } 553 }
411 void CFX_GEModule::InitPlatform() { 554 void CFX_GEModule::InitPlatform() {
412 CWin32Platform* pPlatformData = new CWin32Platform; 555 CWin32Platform* pPlatformData = new CWin32Platform;
413 OSVERSIONINFO ver; 556 OSVERSIONINFO ver;
414 ver.dwOSVersionInfoSize = sizeof(ver); 557 ver.dwOSVersionInfoSize = sizeof(ver);
415 GetVersionEx(&ver); 558 GetVersionEx(&ver);
416 pPlatformData->m_bHalfTone = ver.dwMajorVersion >= 5; 559 pPlatformData->m_bHalfTone = ver.dwMajorVersion >= 5;
417 pPlatformData->m_GdiplusExt.Load(); 560 pPlatformData->m_GdiplusExt.Load();
418 m_pPlatformData = pPlatformData; 561 m_pPlatformData = pPlatformData;
419 m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr)); 562 m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr));
(...skipping 889 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 SelectObject(m_hDC, m_hOldBitmap); 1452 SelectObject(m_hDC, m_hOldBitmap);
1310 DeleteDC(m_hDC); 1453 DeleteDC(m_hDC);
1311 } 1454 }
1312 if (m_hBitmap) { 1455 if (m_hBitmap) {
1313 DeleteObject(m_hBitmap); 1456 DeleteObject(m_hBitmap);
1314 } 1457 }
1315 delete GetBitmap(); 1458 delete GetBitmap();
1316 } 1459 }
1317 1460
1318 #endif // _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ 1461 #endif // _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698