More chromium_code whack-a-mole.
[pdfium.git] / core / src / fxge / win32 / fx_win32_device.cpp
index 4cfc6cb..a32f9ad 100644 (file)
 #include "dwrite_int.h"
 #include "win32_int.h"
 
+class CFX_Win32FallbackFontInfo final : public CFX_FolderFontInfo {
+ public:
+  void* MapFont(int weight,
+                FX_BOOL bItalic,
+                int charset,
+                int pitch_family,
+                const FX_CHAR* family,
+                int& iExact) override;
+};
 class CFX_Win32FontInfo final : public IFX_SystemFontInfo {
  public:
   CFX_Win32FontInfo();
-  ~CFX_Win32FontInfo();
-  virtual void Release();
-  virtual FX_BOOL EnumFontList(CFX_FontMapper* pMapper);
-  virtual void* MapFont(int weight,
-                        FX_BOOL bItalic,
-                        int charset,
-                        int pitch_family,
-                        const FX_CHAR* face,
-                        int& iExact);
-  virtual void* GetFont(const FX_CHAR* face) { return NULL; }
-  virtual FX_DWORD GetFontData(void* hFont,
-                               FX_DWORD table,
-                               uint8_t* buffer,
-                               FX_DWORD size);
-  virtual void DeleteFont(void* hFont);
-  virtual FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name);
-  virtual FX_BOOL GetFontCharset(void* hFont, int& charset);
+  ~CFX_Win32FontInfo() override;
+
+  // IFX_SystemFontInfo
+  void Release() override;
+  FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override;
+  void* MapFont(int weight,
+                FX_BOOL bItalic,
+                int charset,
+                int pitch_family,
+                const FX_CHAR* face,
+                int& iExact) override;
+  void* GetFont(const FX_CHAR* face) override { return NULL; }
+  FX_DWORD GetFontData(void* hFont,
+                       FX_DWORD table,
+                       uint8_t* buffer,
+                       FX_DWORD size) override;
+  FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override;
+  FX_BOOL GetFontCharset(void* hFont, int& charset) override;
+  void DeleteFont(void* hFont) override;
+
   FX_BOOL IsOpenTypeFromDiv(const LOGFONTA* plf);
   FX_BOOL IsSupportFontFormDiv(const LOGFONTA* plf);
   void AddInstalledFont(const LOGFONTA* plf, FX_DWORD FontType);
@@ -51,6 +63,7 @@ class CFX_Win32FontInfo final : public IFX_SystemFontInfo {
   CFX_ByteString m_LastFamily;
   CFX_ByteString m_KaiTi, m_FangSong;
 };
+
 CFX_Win32FontInfo::CFX_Win32FontInfo() {
   m_hDC = CreateCompatibleDC(NULL);
 }
@@ -109,7 +122,7 @@ FX_BOOL CFX_Win32FontInfo::IsSupportFontFormDiv(const LOGFONTA* plf) {
 }
 void CFX_Win32FontInfo::AddInstalledFont(const LOGFONTA* plf,
                                          FX_DWORD FontType) {
-  CFX_ByteString name(plf->lfFaceName, -1);
+  CFX_ByteString name(plf->lfFaceName);
   if (name[0] == '@') {
     return;
   }
@@ -195,6 +208,29 @@ CFX_ByteString CFX_Win32FontInfo::FindFont(const CFX_ByteString& name) {
   }
   return CFX_ByteString();
 }
+void* CFX_Win32FallbackFontInfo::MapFont(int weight,
+                                         FX_BOOL bItalic,
+                                         int charset,
+                                         int pitch_family,
+                                         const FX_CHAR* cstr_face,
+                                         int& iExact) {
+  void* font = GetSubstFont(cstr_face);
+  if (font) {
+    iExact = 1;
+    return font;
+  }
+  FX_BOOL bCJK = TRUE;
+  switch (charset) {
+    case FXFONT_SHIFTJIS_CHARSET:
+    case FXFONT_GB2312_CHARSET:
+    case FXFONT_CHINESEBIG5_CHARSET:
+    case FXFONT_HANGEUL_CHARSET:
+    default:
+      bCJK = FALSE;
+      break;
+  }
+  return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK);
+}
 struct _FontNameMap {
   const FX_CHAR* m_pSubFontName;
   const FX_CHAR* m_pSrcFontName;
@@ -322,7 +358,7 @@ void* CFX_Win32FontInfo::MapFont(int weight,
                     OUT_TT_ONLY_PRECIS, 0, 0, subst_pitch_family, face);
   char facebuf[100];
   HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont);
-  int ret = ::GetTextFaceA(m_hDC, 100, facebuf);
+  ::GetTextFaceA(m_hDC, 100, facebuf);
   ::SelectObject(m_hDC, hOldFont);
   if (face.EqualNoCase(facebuf)) {
     return hFont;
@@ -401,8 +437,25 @@ FX_BOOL CFX_Win32FontInfo::GetFontCharset(void* hFont, int& charset) {
   charset = tm.tmCharSet;
   return TRUE;
 }
-IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault() {
-  return new CFX_Win32FontInfo;
+IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) {
+  HDC hdc = ::GetDC(NULL);
+  if (hdc) {
+    ::ReleaseDC(NULL, hdc);
+    return new CFX_Win32FontInfo;
+  }
+  // If GDI is disabled then GetDC for the desktop will fail. Select the
+  // fallback font information class if GDI is disabled.
+  CFX_Win32FallbackFontInfo* pInfoFallback = new CFX_Win32FallbackFontInfo;
+  // Construct the font path manually, SHGetKnownFolderPath won't work under
+  // a restrictive sandbox.
+  CHAR windows_path[MAX_PATH] = {};
+  DWORD path_len = ::GetWindowsDirectoryA(windows_path, MAX_PATH);
+  if (path_len > 0 && path_len < MAX_PATH) {
+    CFX_ByteString fonts_path(windows_path);
+    fonts_path += "\\Fonts";
+    pInfoFallback->AddPath(fonts_path);
+  }
+  return pInfoFallback;
 }
 void CFX_GEModule::InitPlatform() {
   CWin32Platform* pPlatformData = new CWin32Platform;
@@ -412,7 +465,7 @@ void CFX_GEModule::InitPlatform() {
   pPlatformData->m_bHalfTone = ver.dwMajorVersion >= 5;
   pPlatformData->m_GdiplusExt.Load();
   m_pPlatformData = pPlatformData;
-  m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault());
+  m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr));
 }
 void CFX_GEModule::DestroyPlatform() {
   delete (CWin32Platform*)m_pPlatformData;
@@ -483,7 +536,6 @@ FX_BOOL CGdiDeviceDriver::GDI_SetDIBits(const CFX_DIBitmap* pBitmap1,
       return FALSE;
     }
     int width = pSrcRect->Width(), height = pSrcRect->Height();
-    int pitch = pBitmap->GetPitch();
     LPBYTE pBuffer = pBitmap->GetBuffer();
     CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap);
     ((BITMAPINFOHEADER*)info.c_str())->biHeight *= -1;
@@ -503,7 +555,6 @@ FX_BOOL CGdiDeviceDriver::GDI_SetDIBits(const CFX_DIBitmap* pBitmap1,
       return FALSE;
     }
     int width = pSrcRect->Width(), height = pSrcRect->Height();
-    int pitch = pBitmap->GetPitch();
     LPBYTE pBuffer = pBitmap->GetBuffer();
     CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap);
     ::SetDIBitsToDevice(m_hDC, left, top, width, height, pSrcRect->left,
@@ -695,9 +746,7 @@ static HPEN _CreatePen(const CFX_GraphStateData* pGraphState,
   }
   HPEN hPen = ExtCreatePen(PenStyle, (DWORD)FXSYS_ceil(width), &lb,
                            pGraphState->m_DashCount, (const DWORD*)pDash);
-  if (pDash) {
-    FX_Free(pDash);
-  }
+  FX_Free(pDash);
   return hPen;
 }
 static HBRUSH _CreateBrush(FX_DWORD argb) {