From 24508dff1636d80be01497e30fccd21533fc0bde Mon Sep 17 00:00:00 2001 From: thestig Date: Fri, 27 May 2016 15:14:20 -0700 Subject: Clean up some Android/Windows code. Review-Url: https://codereview.chromium.org/2004313007 --- core/fxge/android/fx_android_font.h | 41 +- core/fxge/android/fx_android_imp.cpp | 7 +- core/fxge/apple/fx_mac_imp.cpp | 9 +- core/fxge/ge/fx_ge_fontmap.cpp | 68 +-- core/fxge/ge/fx_ge_linux.cpp | 15 +- core/fxge/include/fx_font.h | 19 +- core/fxge/win32/fx_win32_device.cpp | 964 +++++++++++++++++------------------ core/fxge/win32/fx_win32_print.cpp | 319 ++++++------ core/fxge/win32/win32_int.h | 28 +- fpdfsdk/fpdf_sysfontinfo.cpp | 20 +- 10 files changed, 739 insertions(+), 751 deletions(-) diff --git a/core/fxge/android/fx_android_font.h b/core/fxge/android/fx_android_font.h index 07d5f5852b..c9c68e347a 100644 --- a/core/fxge/android/fx_android_font.h +++ b/core/fxge/android/fx_android_font.h @@ -18,29 +18,28 @@ class CFPF_SkiaFontMgr; class CFX_AndroidFontInfo : public IFX_SystemFontInfo { public: CFX_AndroidFontInfo(); - virtual void Release() { delete this; } - - 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& bExact); - - virtual void* GetFont(const FX_CHAR* face); - virtual uint32_t GetFontData(void* hFont, - uint32_t table, - uint8_t* buffer, - uint32_t size); - virtual FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name); - virtual FX_BOOL GetFontCharset(void* hFont, int& charset); - - virtual void DeleteFont(void* hFont); - virtual void* RetainFont(void* hFont); + ~CFX_AndroidFontInfo() override; + FX_BOOL Init(CFPF_SkiaFontMgr* pFontMgr); + // IFX_SystemFontInfo: + FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override; + void* MapFont(int weight, + FX_BOOL bItalic, + int charset, + int pitch_family, + const FX_CHAR* face, + int& bExact) override; + void* GetFont(const FX_CHAR* face) override; + uint32_t GetFontData(void* hFont, + uint32_t table, + uint8_t* buffer, + uint32_t size) override; + FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override; + FX_BOOL GetFontCharset(void* hFont, int& charset) override; + void DeleteFont(void* hFont) override; + void* RetainFont(void* hFont) override; + protected: CFPF_SkiaFontMgr* m_pFontMgr; }; diff --git a/core/fxge/android/fx_android_imp.cpp b/core/fxge/android/fx_android_imp.cpp index bbaa2ba27a..08fdea3d89 100644 --- a/core/fxge/android/fx_android_imp.cpp +++ b/core/fxge/android/fx_android_imp.cpp @@ -8,6 +8,9 @@ #if _FX_OS_ == _FX_ANDROID_ +#include +#include + #include "core/fxge/android/fpf_skiamodule.h" #include "core/fxge/android/fx_android_font.h" #include "core/fxge/include/fx_ge.h" @@ -19,9 +22,9 @@ void CFX_GEModule::InitPlatform() { CFPF_SkiaFontMgr* pFontMgr = pDeviceModule->GetFontMgr(); if (pFontMgr) { - CFX_AndroidFontInfo* pFontInfo = new CFX_AndroidFontInfo; + std::unique_ptr pFontInfo(new CFX_AndroidFontInfo); pFontInfo->Init(pFontMgr); - m_pFontMgr->SetSystemFontInfo(pFontInfo); + m_pFontMgr->SetSystemFontInfo(std::move(pFontInfo)); } m_pPlatformData = pDeviceModule; } diff --git a/core/fxge/apple/fx_mac_imp.cpp b/core/fxge/apple/fx_mac_imp.cpp index 82b5078565..16dd4aef66 100644 --- a/core/fxge/apple/fx_mac_imp.cpp +++ b/core/fxge/apple/fx_mac_imp.cpp @@ -95,13 +95,16 @@ void* CFX_MacFontInfo::MapFont(int weight, return NULL; } -IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) { - CFX_MacFontInfo* pInfo = new CFX_MacFontInfo; + +std::unique_ptr IFX_SystemFontInfo::CreateDefault( + const char** pUnused) { + CFX_MacFontInfo* pInfo(new CFX_MacFontInfo); pInfo->AddPath("~/Library/Fonts"); pInfo->AddPath("/Library/Fonts"); pInfo->AddPath("/System/Library/Fonts"); - return pInfo; + return std::unique_ptr(pInfo); } + void CFX_GEModule::InitPlatform() { m_pPlatformData = new CApplePlatform; m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr)); diff --git a/core/fxge/ge/fx_ge_fontmap.cpp b/core/fxge/ge/fx_ge_fontmap.cpp index 7938e10984..83690c7181 100644 --- a/core/fxge/ge/fx_ge_fontmap.cpp +++ b/core/fxge/ge/fx_ge_fontmap.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include "core/fxge/fontdata/chromefontdata/chromefontdata.h" @@ -478,8 +479,9 @@ void CFX_FontMgr::InitFTLibrary() { FT_Err_Unimplemented_Feature; } -void CFX_FontMgr::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { - m_pBuiltinMapper->SetSystemFontInfo(pFontInfo); +void CFX_FontMgr::SetSystemFontInfo( + std::unique_ptr pFontInfo) { + m_pBuiltinMapper->SetSystemFontInfo(std::move(pFontInfo)); } FXFT_Face CFX_FontMgr::FindSubstFont(const CFX_ByteString& face_name, @@ -654,35 +656,29 @@ bool CFX_FontMgr::GetBuiltinFont(size_t index, CFX_FontMapper::CFX_FontMapper(CFX_FontMgr* mgr) : m_bListLoaded(FALSE), - m_pFontInfo(nullptr), m_pFontMgr(mgr) { m_MMFaces[0] = nullptr; m_MMFaces[1] = nullptr; FXSYS_memset(m_FoxitFaces, 0, sizeof(m_FoxitFaces)); } + CFX_FontMapper::~CFX_FontMapper() { for (size_t i = 0; i < FX_ArraySize(m_FoxitFaces); ++i) { if (m_FoxitFaces[i]) FXFT_Done_Face(m_FoxitFaces[i]); } - if (m_MMFaces[0]) { + if (m_MMFaces[0]) FXFT_Done_Face(m_MMFaces[0]); - } - if (m_MMFaces[1]) { + if (m_MMFaces[1]) FXFT_Done_Face(m_MMFaces[1]); - } - if (m_pFontInfo) { - m_pFontInfo->Release(); - } } -void CFX_FontMapper::SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo) { - if (!pFontInfo) { + +void CFX_FontMapper::SetSystemFontInfo( + std::unique_ptr pFontInfo) { + if (!pFontInfo) return; - } - if (m_pFontInfo) { - m_pFontInfo->Release(); - } - m_pFontInfo = pFontInfo; + + m_pFontInfo = std::move(pFontInfo); } static CFX_ByteString GetStringFromTable(const uint8_t* string_ptr, @@ -783,18 +779,13 @@ void CFX_FontMapper::AddInstalledFont(const CFX_ByteString& name, int charset) { } void CFX_FontMapper::LoadInstalledFonts() { - if (!m_pFontInfo) { - return; - } - if (m_bListLoaded) { - return; - } - if (m_bListLoaded) { + if (!m_pFontInfo || m_bListLoaded) return; - } + m_pFontInfo->EnumFontList(this); m_bListLoaded = TRUE; } + CFX_ByteString CFX_FontMapper::MatchInstalledFonts( const CFX_ByteString& norm_name) { LoadInstalledFonts(); @@ -1253,9 +1244,9 @@ FXFT_Face CFX_FontMapper::FindSubstFontByUnicode(uint32_t dwUnicode, uint32_t flags, int weight, int italic_angle) { - if (m_pFontInfo == NULL) { - return NULL; - } + if (!m_pFontInfo) + return nullptr; + FX_BOOL bItalic = (flags & FXFONT_ITALIC) != 0; int PitchFamily = 0; if (flags & FXFONT_SERIF) { @@ -1269,9 +1260,9 @@ FXFT_Face CFX_FontMapper::FindSubstFontByUnicode(uint32_t dwUnicode, } void* hFont = m_pFontInfo->MapFontByUnicode(dwUnicode, weight, bItalic, PitchFamily); - if (hFont == NULL) { - return NULL; - } + if (!hFont) + return nullptr; + uint32_t ttc_size = m_pFontInfo->GetFontData(hFont, 0x74746366, NULL, 0); uint32_t font_size = m_pFontInfo->GetFontData(hFont, 0, NULL, 0); if (font_size == 0 && ttc_size == 0) { @@ -1289,7 +1280,7 @@ FXFT_Face CFX_FontMapper::FindSubstFontByUnicode(uint32_t dwUnicode, uint8_t* pFontData; face = m_pFontMgr->GetCachedTTCFace(ttc_size, checksum, ttc_size - font_size, pFontData); - if (face == NULL) { + if (!face) { pFontData = FX_Alloc(uint8_t, ttc_size); if (pFontData) { m_pFontInfo->GetFontData(hFont, 0x74746366, pFontData, ttc_size); @@ -1302,11 +1293,11 @@ FXFT_Face CFX_FontMapper::FindSubstFontByUnicode(uint32_t dwUnicode, m_pFontInfo->GetFaceName(hFont, SubstName); uint8_t* pFontData; face = m_pFontMgr->GetCachedFace(SubstName, weight, bItalic, pFontData); - if (face == NULL) { + if (!face) { pFontData = FX_Alloc(uint8_t, font_size); if (!pFontData) { m_pFontInfo->DeleteFont(hFont); - return NULL; + return nullptr; } m_pFontInfo->GetFontData(hFont, 0, pFontData, font_size); face = m_pFontMgr->AddCachedFace(SubstName, weight, bItalic, pFontData, @@ -1361,8 +1352,9 @@ void _FTStreamClose(FXFT_Stream stream); }; #if _FX_OS_ == _FX_ANDROID_ -IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) { - return NULL; +std::unique_ptr IFX_SystemFontInfo::CreateDefault( + const char** pUnused) { + return nullptr; } #endif @@ -1377,10 +1369,6 @@ void CFX_FolderFontInfo::AddPath(const CFX_ByteStringC& path) { m_PathList.push_back(CFX_ByteString(path)); } -void CFX_FolderFontInfo::Release() { - delete this; -} - FX_BOOL CFX_FolderFontInfo::EnumFontList(CFX_FontMapper* pMapper) { m_pMapper = pMapper; for (const auto& path : m_PathList) diff --git a/core/fxge/ge/fx_ge_linux.cpp b/core/fxge/ge/fx_ge_linux.cpp index 0d5aff75fa..46d7146026 100644 --- a/core/fxge/ge/fx_ge_linux.cpp +++ b/core/fxge/ge/fx_ge_linux.cpp @@ -123,7 +123,9 @@ void* CFX_LinuxFontInfo::MapFont(int weight, } return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK); } -IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUserPaths) { + +std::unique_ptr IFX_SystemFontInfo::CreateDefault( + const char** pUserPaths) { CFX_LinuxFontInfo* pInfo = new CFX_LinuxFontInfo; if (!pInfo->ParseFontCfg(pUserPaths)) { pInfo->AddPath("/usr/share/fonts"); @@ -131,17 +133,18 @@ IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUserPaths) { pInfo->AddPath("/usr/share/X11/fonts/TTF"); pInfo->AddPath("/usr/local/share/fonts"); } - return pInfo; + return std::unique_ptr(pInfo); } + FX_BOOL CFX_LinuxFontInfo::ParseFontCfg(const char** pUserPaths) { - if (!pUserPaths) { + if (!pUserPaths) return FALSE; - } - for (const char** pPath = pUserPaths; *pPath; ++pPath) { + + for (const char** pPath = pUserPaths; *pPath; ++pPath) AddPath(*pPath); - } return TRUE; } + void CFX_GEModule::InitPlatform() { m_pFontMgr->SetSystemFontInfo( IFX_SystemFontInfo::CreateDefault(m_pUserFontPaths)); diff --git a/core/fxge/include/fx_font.h b/core/fxge/include/fx_font.h index 762cdfaf87..a8eaecec59 100644 --- a/core/fxge/include/fx_font.h +++ b/core/fxge/include/fx_font.h @@ -269,7 +269,7 @@ class CFX_FontMgr { FXFT_Face GetFileFace(const FX_CHAR* filename, int face_index); FXFT_Face GetFixedFace(const uint8_t* pData, uint32_t size, int face_index); void ReleaseFace(FXFT_Face face); - void SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo); + void SetSystemFontInfo(std::unique_ptr pFontInfo); FXFT_Face FindSubstFont(const CFX_ByteString& face_name, FX_BOOL bTrueType, uint32_t flags, @@ -294,8 +294,8 @@ class CFX_FontMapper { explicit CFX_FontMapper(CFX_FontMgr* mgr); ~CFX_FontMapper(); - void SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo); - IFX_SystemFontInfo* GetSystemFontInfo() { return m_pFontInfo; } + void SetSystemFontInfo(std::unique_ptr pFontInfo); + IFX_SystemFontInfo* GetSystemFontInfo() { return m_pFontInfo.get(); } void AddInstalledFont(const CFX_ByteString& name, int charset); void LoadInstalledFonts(); @@ -341,15 +341,17 @@ class CFX_FontMapper { FXFT_Face m_MMFaces[MM_FACE_COUNT]; CFX_ByteString m_LastFamily; std::vector m_FaceArray; - IFX_SystemFontInfo* m_pFontInfo; + std::unique_ptr m_pFontInfo; FXFT_Face m_FoxitFaces[FOXIT_FACE_COUNT]; CFX_FontMgr* const m_pFontMgr; }; class IFX_SystemFontInfo { public: - static IFX_SystemFontInfo* CreateDefault(const char** pUserPaths); - virtual void Release() = 0; + static std::unique_ptr CreateDefault( + const char** pUserPaths); + + virtual ~IFX_SystemFontInfo() {} virtual FX_BOOL EnumFontList(CFX_FontMapper* pMapper) = 0; virtual void* MapFont(int weight, @@ -376,19 +378,16 @@ class IFX_SystemFontInfo { virtual int GetFaceIndex(void* hFont); virtual void DeleteFont(void* hFont) = 0; virtual void* RetainFont(void* hFont); - - protected: - virtual ~IFX_SystemFontInfo() {} }; class CFX_FolderFontInfo : public IFX_SystemFontInfo { public: CFX_FolderFontInfo(); ~CFX_FolderFontInfo() override; + void AddPath(const CFX_ByteStringC& path); // IFX_SytemFontInfo: - void Release() override; FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override; void* MapFont(int weight, FX_BOOL bItalic, diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp index b71f3ae97a..c5b32bdbfc 100644 --- a/core/fxge/win32/fx_win32_device.cpp +++ b/core/fxge/win32/fx_win32_device.cpp @@ -6,6 +6,10 @@ #include "core/fxge/include/fx_ge.h" +#include +#include +#include + #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ #include @@ -24,8 +28,303 @@ #include "core/fxge/win32/win32_int.h" #include "third_party/base/stl_util.h" +namespace { + +const struct { + const FX_CHAR* m_pFaceName; + const FX_CHAR* m_pVariantName; +} g_VariantNames[] = { + {"DFKai-SB", "\x19\x6A\x77\x69\xD4\x9A"}, +}; + +const struct { + const FX_CHAR* m_pName; + const FX_CHAR* m_pWinName; + bool m_bBold; + bool m_bItalic; +} g_Base14Substs[] = { + {"Courier", "Courier New", false, false}, + {"Courier-Bold", "Courier New", true, false}, + {"Courier-BoldOblique", "Courier New", true, true}, + {"Courier-Oblique", "Courier New", false, true}, + {"Helvetica", "Arial", false, false}, + {"Helvetica-Bold", "Arial", true, false}, + {"Helvetica-BoldOblique", "Arial", true, true}, + {"Helvetica-Oblique", "Arial", false, true}, + {"Times-Roman", "Times New Roman", false, false}, + {"Times-Bold", "Times New Roman", true, false}, + {"Times-BoldItalic", "Times New Roman", true, true}, + {"Times-Italic", "Times New Roman", false, true}, +}; + +struct FontNameMap { + const FX_CHAR* m_pSubFontName; + const FX_CHAR* m_pSrcFontName; +}; +const FontNameMap g_JpFontNameMap[] = { + {"MS Mincho", "Heiseimin-W3"}, + {"MS Gothic", "Jun101-Light"}, +}; + +bool GetSubFontName(CFX_ByteString* name) { + for (size_t i = 0; i < FX_ArraySize(g_JpFontNameMap); ++i) { + if (!FXSYS_stricmp(name->c_str(), g_JpFontNameMap[i].m_pSrcFontName)) { + *name = g_JpFontNameMap[i].m_pSubFontName; + return true; + } + } + return false; +} + +FX_BOOL IsGDIEnabled() { + // If GDI is disabled then GetDC for the desktop will fail. + HDC hdc = ::GetDC(nullptr); + if (hdc) { + ::ReleaseDC(nullptr, hdc); + return TRUE; + } + return FALSE; +} + +HPEN CreatePen(const CFX_GraphStateData* pGraphState, + const CFX_Matrix* pMatrix, + uint32_t argb) { + FX_FLOAT width; + FX_FLOAT scale = 1.f; + if (pMatrix) + scale = FXSYS_fabs(pMatrix->a) > FXSYS_fabs(pMatrix->b) + ? FXSYS_fabs(pMatrix->a) + : FXSYS_fabs(pMatrix->b); + if (pGraphState) { + width = scale * pGraphState->m_LineWidth; + } else { + width = 1.0f; + } + uint32_t PenStyle = PS_GEOMETRIC; + if (width < 1) { + width = 1; + } + if (pGraphState->m_DashCount) { + PenStyle |= PS_USERSTYLE; + } else { + PenStyle |= PS_SOLID; + } + switch (pGraphState->m_LineCap) { + case 0: + PenStyle |= PS_ENDCAP_FLAT; + break; + case 1: + PenStyle |= PS_ENDCAP_ROUND; + break; + case 2: + PenStyle |= PS_ENDCAP_SQUARE; + break; + } + switch (pGraphState->m_LineJoin) { + case 0: + PenStyle |= PS_JOIN_MITER; + break; + case 1: + PenStyle |= PS_JOIN_ROUND; + break; + case 2: + PenStyle |= PS_JOIN_BEVEL; + break; + } + int a; + FX_COLORREF rgb; + ArgbDecode(argb, a, rgb); + LOGBRUSH lb; + lb.lbColor = rgb; + lb.lbStyle = BS_SOLID; + lb.lbHatch = 0; + std::vector dashes; + if (pGraphState->m_DashCount) { + dashes.resize(pGraphState->m_DashCount); + for (int i = 0; i < pGraphState->m_DashCount; i++) { + dashes[i] = FXSYS_round( + pMatrix ? pMatrix->TransformDistance(pGraphState->m_DashArray[i]) + : pGraphState->m_DashArray[i]); + dashes[i] = std::max(dashes[i], 1U); + } + } + return ExtCreatePen(PenStyle, (DWORD)FXSYS_ceil(width), &lb, + pGraphState->m_DashCount, + reinterpret_cast(dashes.data())); +} + +HBRUSH CreateBrush(uint32_t argb) { + int a; + FX_COLORREF rgb; + ArgbDecode(argb, a, rgb); + return CreateSolidBrush(rgb); +} + +void SetPathToDC(HDC hDC, + const CFX_PathData* pPathData, + const CFX_Matrix* pMatrix) { + BeginPath(hDC); + int nPoints = pPathData->GetPointCount(); + FX_PATHPOINT* pPoints = pPathData->GetPoints(); + for (int i = 0; i < nPoints; i++) { + FX_FLOAT posx = pPoints[i].m_PointX, posy = pPoints[i].m_PointY; + if (pMatrix) { + pMatrix->Transform(posx, posy); + } + int screen_x = FXSYS_round(posx), screen_y = FXSYS_round(posy); + int point_type = pPoints[i].m_Flag & FXPT_TYPE; + if (point_type == PT_MOVETO) { + MoveToEx(hDC, screen_x, screen_y, nullptr); + } else if (point_type == PT_LINETO) { + if (pPoints[i].m_PointY == pPoints[i - 1].m_PointY && + pPoints[i].m_PointX == pPoints[i - 1].m_PointX) { + screen_x++; + } + LineTo(hDC, screen_x, screen_y); + } else if (point_type == PT_BEZIERTO) { + POINT lppt[3]; + lppt[0].x = screen_x; + lppt[0].y = screen_y; + posx = pPoints[i + 1].m_PointX; + posy = pPoints[i + 1].m_PointY; + if (pMatrix) { + pMatrix->Transform(posx, posy); + } + lppt[1].x = FXSYS_round(posx); + lppt[1].y = FXSYS_round(posy); + posx = pPoints[i + 2].m_PointX; + posy = pPoints[i + 2].m_PointY; + if (pMatrix) { + pMatrix->Transform(posx, posy); + } + lppt[2].x = FXSYS_round(posx); + lppt[2].y = FXSYS_round(posy); + PolyBezierTo(hDC, lppt, 3); + i += 2; + } + if (pPoints[i].m_Flag & PT_CLOSEFIGURE) { + CloseFigure(hDC); + } + } + EndPath(hDC); +} + +#ifdef _SKIA_SUPPORT_ +// TODO(caryclark) This antigrain function is duplicated here to permit +// removing the last remaining dependency. Eventually, this will be elminiated +// altogether and replace by Skia code. + +struct rect_base { + FX_FLOAT x1; + FX_FLOAT y1; + FX_FLOAT x2; + FX_FLOAT y2; +}; + +unsigned clip_liang_barsky(FX_FLOAT x1, + FX_FLOAT y1, + FX_FLOAT x2, + FX_FLOAT y2, + const rect_base& clip_box, + FX_FLOAT* x, + FX_FLOAT* y) { + const FX_FLOAT nearzero = 1e-30f; + FX_FLOAT deltax = x2 - x1; + FX_FLOAT deltay = y2 - y1; + unsigned np = 0; + if (deltax == 0) + deltax = (x1 > clip_box.x1) ? -nearzero : nearzero; + FX_FLOAT xin, xout; + if (deltax > 0) { + xin = clip_box.x1; + xout = clip_box.x2; + } else { + xin = clip_box.x2; + xout = clip_box.x1; + } + FX_FLOAT tinx = (xin - x1) / deltax; + if (deltay == 0) + deltay = (y1 > clip_box.y1) ? -nearzero : nearzero; + FX_FLOAT yin, yout; + if (deltay > 0) { + yin = clip_box.y1; + yout = clip_box.y2; + } else { + yin = clip_box.y2; + yout = clip_box.y1; + } + FX_FLOAT tiny = (yin - y1) / deltay; + FX_FLOAT tin1, tin2; + if (tinx < tiny) { + tin1 = tinx; + tin2 = tiny; + } else { + tin1 = tiny; + tin2 = tinx; + } + if (tin1 <= 1.0f) { + if (0 < tin1) { + *x++ = xin; + *y++ = yin; + ++np; + } + if (tin2 <= 1.0f) { + FX_FLOAT toutx = (xout - x1) / deltax; + FX_FLOAT touty = (yout - y1) / deltay; + FX_FLOAT tout1 = (toutx < touty) ? toutx : touty; + if (tin2 > 0 || tout1 > 0) { + if (tin2 <= tout1) { + if (tin2 > 0) { + if (tinx > tiny) { + *x++ = xin; + *y++ = y1 + (deltay * tinx); + } else { + *x++ = x1 + (deltax * tiny); + *y++ = yin; + } + ++np; + } + if (tout1 < 1.0f) { + if (toutx < touty) { + *x++ = xout; + *y++ = y1 + (deltay * toutx); + } else { + *x++ = x1 + (deltax * touty); + *y++ = yout; + } + } else { + *x++ = x2; + *y++ = y2; + } + ++np; + } else { + if (tinx > tiny) { + *x++ = xin; + *y++ = yout; + } else { + *x++ = xout; + *y++ = yin; + } + ++np; + } + } + } + } + return np; +} +#endif // _SKIA_SUPPORT_ + +FX_BOOL MatrixNoScaled(const CFX_Matrix* pMatrix) { + return pMatrix->GetA() == 1.0f && pMatrix->GetB() == 0 && + pMatrix->GetC() == 0 && pMatrix->GetD() == 1.0f; +} + class CFX_Win32FallbackFontInfo final : public CFX_FolderFontInfo { public: + CFX_Win32FallbackFontInfo() {} + ~CFX_Win32FallbackFontInfo() override {} + + // CFX_FolderFontInfo: void* MapFont(int weight, FX_BOOL bItalic, int charset, @@ -33,13 +332,13 @@ class CFX_Win32FallbackFontInfo final : public CFX_FolderFontInfo { const FX_CHAR* family, int& iExact) override; }; + class CFX_Win32FontInfo final : public IFX_SystemFontInfo { public: CFX_Win32FontInfo(); ~CFX_Win32FontInfo() override; // IFX_SystemFontInfo - void Release() override; FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override; void* MapFont(int weight, FX_BOOL bItalic, @@ -47,7 +346,7 @@ class CFX_Win32FontInfo final : public IFX_SystemFontInfo { int pitch_family, const FX_CHAR* face, int& iExact) override; - void* GetFont(const FX_CHAR* face) override { return NULL; } + void* GetFont(const FX_CHAR* face) override { return nullptr; } uint32_t GetFontData(void* hFont, uint32_t table, uint8_t* buffer, @@ -64,29 +363,32 @@ class CFX_Win32FontInfo final : public IFX_SystemFontInfo { int weight, int picth_family); CFX_ByteString FindFont(const CFX_ByteString& name); + HDC m_hDC; CFX_FontMapper* m_pMapper; CFX_ByteString m_LastFamily; CFX_ByteString m_KaiTi, m_FangSong; }; -CFX_Win32FontInfo::CFX_Win32FontInfo() { - m_hDC = CreateCompatibleDC(NULL); +int CALLBACK FontEnumProc(const LOGFONTA* plf, + const TEXTMETRICA* lpntme, + uint32_t FontType, + LPARAM lParam) { + CFX_Win32FontInfo* pFontInfo = reinterpret_cast(lParam); + pFontInfo->AddInstalledFont(plf, FontType); + return 1; } + +CFX_Win32FontInfo::CFX_Win32FontInfo() : m_hDC(CreateCompatibleDC(nullptr)) {} + CFX_Win32FontInfo::~CFX_Win32FontInfo() { - m_pMapper = NULL; -} -void CFX_Win32FontInfo::Release() { DeleteDC(m_hDC); - delete this; } -#define TT_MAKE_TAG(x1, x2, x3, x4) \ - (((uint32_t)x1 << 24) | ((uint32_t)x2 << 16) | ((uint32_t)x3 << 8) | \ - (uint32_t)x4) + FX_BOOL CFX_Win32FontInfo::IsOpenTypeFromDiv(const LOGFONTA* plf) { HFONT hFont = CreateFontIndirectA(plf); FX_BOOL ret = FALSE; - uint32_t font_size = GetFontData(hFont, 0, NULL, 0); + uint32_t font_size = GetFontData(hFont, 0, nullptr, 0); if (font_size != GDI_ERROR && font_size >= sizeof(uint32_t)) { uint32_t lVersion = 0; GetFontData(hFont, 0, (uint8_t*)(&lVersion), sizeof(uint32_t)); @@ -94,19 +396,20 @@ FX_BOOL CFX_Win32FontInfo::IsOpenTypeFromDiv(const LOGFONTA* plf) { ((uint32_t)((uint8_t)(lVersion >> 8))) << 16 | ((uint32_t)((uint8_t)(lVersion >> 16))) << 8 | ((uint8_t)(lVersion >> 24)); - if (lVersion == TT_MAKE_TAG('O', 'T', 'T', 'O') || lVersion == 0x00010000 || - lVersion == TT_MAKE_TAG('t', 't', 'c', 'f') || - lVersion == TT_MAKE_TAG('t', 'r', 'u', 'e') || lVersion == 0x00020000) { + if (lVersion == FXBSTR_ID('O', 'T', 'T', 'O') || lVersion == 0x00010000 || + lVersion == FXBSTR_ID('t', 't', 'c', 'f') || + lVersion == FXBSTR_ID('t', 'r', 'u', 'e') || lVersion == 0x00020000) { ret = TRUE; } } DeleteFont(hFont); return ret; } + FX_BOOL CFX_Win32FontInfo::IsSupportFontFormDiv(const LOGFONTA* plf) { HFONT hFont = CreateFontIndirectA(plf); FX_BOOL ret = FALSE; - uint32_t font_size = GetFontData(hFont, 0, NULL, 0); + uint32_t font_size = GetFontData(hFont, 0, nullptr, 0); if (font_size != GDI_ERROR && font_size >= sizeof(uint32_t)) { uint32_t lVersion = 0; GetFontData(hFont, 0, (uint8_t*)(&lVersion), sizeof(uint32_t)); @@ -114,47 +417,37 @@ FX_BOOL CFX_Win32FontInfo::IsSupportFontFormDiv(const LOGFONTA* plf) { ((uint32_t)((uint8_t)(lVersion >> 8))) << 16 | ((uint32_t)((uint8_t)(lVersion >> 16))) << 8 | ((uint8_t)(lVersion >> 24)); - if (lVersion == TT_MAKE_TAG('O', 'T', 'T', 'O') || lVersion == 0x00010000 || - lVersion == TT_MAKE_TAG('t', 't', 'c', 'f') || - lVersion == TT_MAKE_TAG('t', 'r', 'u', 'e') || lVersion == 0x00020000) { - ret = TRUE; - } else if ((lVersion & 0xFFFF0000) == TT_MAKE_TAG(0x80, 0x01, 0x00, 0x00) || - (lVersion & 0xFFFF0000) == TT_MAKE_TAG('%', '!', 0, 0)) { + if (lVersion == FXBSTR_ID('O', 'T', 'T', 'O') || lVersion == 0x00010000 || + lVersion == FXBSTR_ID('t', 't', 'c', 'f') || + lVersion == FXBSTR_ID('t', 'r', 'u', 'e') || lVersion == 0x00020000 || + (lVersion & 0xFFFF0000) == FXBSTR_ID(0x80, 0x01, 0x00, 0x00) || + (lVersion & 0xFFFF0000) == FXBSTR_ID('%', '!', 0, 0)) { ret = TRUE; } } DeleteFont(hFont); return ret; } + void CFX_Win32FontInfo::AddInstalledFont(const LOGFONTA* plf, uint32_t FontType) { CFX_ByteString name(plf->lfFaceName); - if (name[0] == '@') { + if (name[0] == '@') return; - } + if (name == m_LastFamily) { m_pMapper->AddInstalledFont(name, plf->lfCharSet); return; } - if (!(FontType & TRUETYPE_FONTTYPE) && !(FontType & DEVICE_FONTTYPE)) { - return; - } if (!(FontType & TRUETYPE_FONTTYPE)) { - if (!IsSupportFontFormDiv(plf)) { + if (!(FontType & DEVICE_FONTTYPE) || !IsSupportFontFormDiv(plf)) return; - } } + m_pMapper->AddInstalledFont(name, plf->lfCharSet); m_LastFamily = name; } -static int CALLBACK FontEnumProc(const LOGFONTA* plf, - const TEXTMETRICA* lpntme, - uint32_t FontType, - LPARAM lParam) { - CFX_Win32FontInfo* pFontInfo = (CFX_Win32FontInfo*)lParam; - pFontInfo->AddInstalledFont(plf, FontType); - return 1; -} + FX_BOOL CFX_Win32FontInfo::EnumFontList(CFX_FontMapper* pMapper) { m_pMapper = pMapper; LOGFONTA lf; @@ -166,37 +459,12 @@ FX_BOOL CFX_Win32FontInfo::EnumFontList(CFX_FontMapper* pMapper) { 0); return TRUE; } -static const struct { - const FX_CHAR* m_pFaceName; - const FX_CHAR* m_pVariantName; -} VariantNames[] = { - {"DFKai-SB", "\x19\x6A\x77\x69\xD4\x9A"}, -}; -static const struct { - const FX_CHAR* m_pName; - const FX_CHAR* m_pWinName; - FX_BOOL m_bBold; - FX_BOOL m_bItalic; -} Base14Substs[] = { - {"Courier", "Courier New", FALSE, FALSE}, - {"Courier-Bold", "Courier New", TRUE, FALSE}, - {"Courier-BoldOblique", "Courier New", TRUE, TRUE}, - {"Courier-Oblique", "Courier New", FALSE, TRUE}, - {"Helvetica", "Arial", FALSE, FALSE}, - {"Helvetica-Bold", "Arial", TRUE, FALSE}, - {"Helvetica-BoldOblique", "Arial", TRUE, TRUE}, - {"Helvetica-Oblique", "Arial", FALSE, TRUE}, - {"Times-Roman", "Times New Roman", FALSE, FALSE}, - {"Times-Bold", "Times New Roman", TRUE, FALSE}, - {"Times-BoldItalic", "Times New Roman", TRUE, TRUE}, - {"Times-Italic", "Times New Roman", FALSE, TRUE}, -}; + CFX_ByteString CFX_Win32FontInfo::FindFont(const CFX_ByteString& name) { - if (!m_pMapper) { + if (!m_pMapper) return name; - } - int nFonts = pdfium::CollectionSize(m_pMapper->m_InstalledTTFonts); - for (int i = 0; i < nFonts; i++) { + + for (size_t i = 0; i < m_pMapper->m_InstalledTTFonts.size(); ++i) { CFX_ByteString thisname = m_pMapper->m_InstalledTTFonts[i]; if (thisname[0] == ' ') { if (thisname.Mid(1, name.GetLength()) == name) { @@ -208,6 +476,7 @@ CFX_ByteString CFX_Win32FontInfo::FindFont(const CFX_ByteString& name) { } return CFX_ByteString(); } + void* CFX_Win32FallbackFontInfo::MapFont(int weight, FX_BOOL bItalic, int charset, @@ -231,32 +500,7 @@ void* CFX_Win32FallbackFontInfo::MapFont(int weight, } return FindFont(weight, bItalic, charset, pitch_family, cstr_face, !bCJK); } -struct _FontNameMap { - const FX_CHAR* m_pSubFontName; - const FX_CHAR* m_pSrcFontName; -}; -const _FontNameMap g_JpFontNameMap[] = { - {"MS Mincho", "Heiseimin-W3"}, - {"MS Gothic", "Jun101-Light"}, -}; -extern "C" { -static int compareString(const void* key, const void* element) { - return FXSYS_stricmp((const FX_CHAR*)key, - ((_FontNameMap*)element)->m_pSrcFontName); -} -} -FX_BOOL _GetSubFontName(CFX_ByteString& name) { - int size = sizeof g_JpFontNameMap; - void* pFontnameMap = (void*)g_JpFontNameMap; - _FontNameMap* found = (_FontNameMap*)FXSYS_bsearch( - name.c_str(), pFontnameMap, size / sizeof(_FontNameMap), - sizeof(_FontNameMap), compareString); - if (!found) { - return FALSE; - } - name = found->m_pSubFontName; - return TRUE; -} + void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, int weight, int picth_family) { @@ -286,6 +530,7 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, face = "SimSun"; } } + void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, int weight, int picth_family) { @@ -314,15 +559,16 @@ void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, } return; } - if (_GetSubFontName(face)) { + if (GetSubFontName(&face)) return; - } + if (!(picth_family & FF_ROMAN) && weight > 400) { face = "MS PGothic"; } else { face = "MS PMincho"; } } + void* CFX_Win32FontInfo::MapFont(int weight, FX_BOOL bItalic, int charset, @@ -332,10 +578,10 @@ void* CFX_Win32FontInfo::MapFont(int weight, CFX_ByteString face = cstr_face; int iBaseFont; for (iBaseFont = 0; iBaseFont < 12; iBaseFont++) - if (face == CFX_ByteStringC(Base14Substs[iBaseFont].m_pName)) { - face = Base14Substs[iBaseFont].m_pWinName; - weight = Base14Substs[iBaseFont].m_bBold ? FW_BOLD : FW_NORMAL; - bItalic = Base14Substs[iBaseFont].m_bItalic; + if (face == CFX_ByteStringC(g_Base14Substs[iBaseFont].m_pName)) { + face = g_Base14Substs[iBaseFont].m_pWinName; + weight = g_Base14Substs[iBaseFont].m_bBold ? FW_BOLD : FW_NORMAL; + bItalic = g_Base14Substs[iBaseFont].m_bItalic; iExact = TRUE; break; } @@ -360,26 +606,25 @@ void* CFX_Win32FontInfo::MapFont(int weight, HFONT hOldFont = (HFONT)::SelectObject(m_hDC, hFont); ::GetTextFaceA(m_hDC, 100, facebuf); ::SelectObject(m_hDC, hOldFont); - if (face.EqualNoCase(facebuf)) { + if (face.EqualNoCase(facebuf)) return hFont; - } - int iCount = sizeof(VariantNames) / sizeof(VariantNames[0]); - for (int i = 0; i < iCount; ++i) { - if (face == VariantNames[i].m_pFaceName) { - CFX_WideString wsFace = CFX_WideString::FromLocal(facebuf); - const unsigned short* pName = - (const unsigned short*)VariantNames[i].m_pVariantName; - FX_STRSIZE len = CFX_WideString::WStringLength(pName); - CFX_WideString wsName = CFX_WideString::FromUTF16LE(pName, len); - if (wsFace == wsName) { - return hFont; - } - } + + CFX_WideString wsFace = CFX_WideString::FromLocal(facebuf); + for (size_t i = 0; i < FX_ArraySize(g_VariantNames); ++i) { + if (face != g_VariantNames[i].m_pFaceName) + continue; + + const unsigned short* pName = reinterpret_cast( + g_VariantNames[i].m_pVariantName); + FX_STRSIZE len = CFX_WideString::WStringLength(pName); + CFX_WideString wsName = CFX_WideString::FromUTF16LE(pName, len); + if (wsFace == wsName) + return hFont; } ::DeleteObject(hFont); - if (charset == DEFAULT_CHARSET) { - return NULL; - } + if (charset == DEFAULT_CHARSET) + return nullptr; + switch (charset) { case SHIFTJIS_CHARSET: GetJapanesePreference(face, weight, pitch_family); @@ -403,9 +648,11 @@ void* CFX_Win32FontInfo::MapFont(int weight, OUT_TT_ONLY_PRECIS, 0, 0, subst_pitch_family, face.c_str()); return hFont; } + void CFX_Win32FontInfo::DeleteFont(void* hFont) { ::DeleteObject(hFont); } + uint32_t CFX_Win32FontInfo::GetFontData(void* hFont, uint32_t table, uint8_t* buffer, @@ -419,6 +666,7 @@ uint32_t CFX_Win32FontInfo::GetFontData(void* hFont, } return size; } + FX_BOOL CFX_Win32FontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { char facebuf[100]; HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont); @@ -430,6 +678,7 @@ FX_BOOL CFX_Win32FontInfo::GetFaceName(void* hFont, CFX_ByteString& name) { name = facebuf; return TRUE; } + FX_BOOL CFX_Win32FontInfo::GetFontCharset(void* hFont, int& charset) { TEXTMETRIC tm; HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont); @@ -438,19 +687,14 @@ FX_BOOL CFX_Win32FontInfo::GetFontCharset(void* hFont, int& charset) { charset = tm.tmCharSet; return TRUE; } -static FX_BOOL IsGDIEnabled() { - // If GDI is disabled then GetDC for the desktop will fail. - HDC hdc = ::GetDC(NULL); - if (hdc) { - ::ReleaseDC(NULL, hdc); - return TRUE; - } - return FALSE; -} -IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) { - if (IsGDIEnabled()) { - return new CFX_Win32FontInfo; - } + +} // namespace + +std::unique_ptr IFX_SystemFontInfo::CreateDefault( + const char** pUnused) { + if (IsGDIEnabled()) + return std::unique_ptr(new CFX_Win32FontInfo); + // 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 @@ -462,8 +706,9 @@ IFX_SystemFontInfo* IFX_SystemFontInfo::CreateDefault(const char** pUnused) { fonts_path += "\\Fonts"; pInfoFallback->AddPath(fonts_path.AsStringC()); } - return pInfoFallback; + return std::unique_ptr(pInfoFallback); } + void CFX_GEModule::InitPlatform() { CWin32Platform* pPlatformData = new CWin32Platform; OSVERSIONINFO ver; @@ -476,10 +721,12 @@ void CFX_GEModule::InitPlatform() { m_pPlatformData = pPlatformData; m_pFontMgr->SetSystemFontInfo(IFX_SystemFontInfo::CreateDefault(nullptr)); } + void CFX_GEModule::DestroyPlatform() { delete (CWin32Platform*)m_pPlatformData; - m_pPlatformData = NULL; + m_pPlatformData = nullptr; } + CGdiDeviceDriver::CGdiDeviceDriver(HDC hDC, int device_class) { m_hDC = hDC; m_DeviceClass = device_class; @@ -487,7 +734,7 @@ CGdiDeviceDriver::CGdiDeviceDriver(HDC hDC, int device_class) { (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); SetStretchBltMode(hDC, pPlatform->m_bHalfTone ? HALFTONE : COLORONCOLOR); if (GetObjectType(m_hDC) == OBJ_MEMDC) { - HBITMAP hBitmap = CreateBitmap(1, 1, 1, 1, NULL); + HBITMAP hBitmap = CreateBitmap(1, 1, 1, 1, nullptr); hBitmap = (HBITMAP)SelectObject(m_hDC, hBitmap); BITMAP bitmap; GetObject(hBitmap, sizeof bitmap, &bitmap); @@ -538,27 +785,29 @@ void* CGdiDeviceDriver::GetClipRgn() { HRGN hClipRgn = CreateRectRgn(0, 0, 1, 1); if (::GetClipRgn(m_hDC, hClipRgn) == 0) { DeleteObject(hClipRgn); - hClipRgn = NULL; + hClipRgn = nullptr; } return (void*)hClipRgn; } -FX_BOOL CGdiDeviceDriver::GDI_SetDIBits(const CFX_DIBitmap* pBitmap1, + +FX_BOOL CGdiDeviceDriver::GDI_SetDIBits(CFX_DIBitmap* pBitmap1, const FX_RECT* pSrcRect, int left, int top, void* pIccTransform) { if (m_DeviceClass == FXDC_PRINTER) { - CFX_DIBitmap* pBitmap = pBitmap1->FlipImage(FALSE, TRUE); - if (!pBitmap) { + std::unique_ptr pBitmap(pBitmap1->FlipImage(FALSE, TRUE)); + if (!pBitmap) return FALSE; - } + if ((pBitmap->IsCmykImage() || pIccTransform) && !pBitmap->ConvertFormat(FXDIB_Rgb, pIccTransform)) { return FALSE; } + int width = pSrcRect->Width(), height = pSrcRect->Height(); LPBYTE pBuffer = pBitmap->GetBuffer(); - CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap); + CFX_ByteString info = CFX_WindowsDIB::GetBitmapInfo(pBitmap.get()); ((BITMAPINFOHEADER*)info.c_str())->biHeight *= -1; FX_RECT dst_rect(0, 0, width, height); dst_rect.Intersect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight()); @@ -567,13 +816,12 @@ FX_BOOL CGdiDeviceDriver::GDI_SetDIBits(const CFX_DIBitmap* pBitmap1, ::StretchDIBits(m_hDC, left, top, dst_width, dst_height, 0, 0, dst_width, dst_height, pBuffer, (BITMAPINFO*)info.c_str(), DIB_RGB_COLORS, SRCCOPY); - delete pBitmap; } else { - CFX_DIBitmap* pBitmap = (CFX_DIBitmap*)pBitmap1; - if ((pBitmap->IsCmykImage() || pIccTransform) && - (pBitmap = pBitmap->CloneConvert(FXDIB_Rgb, NULL, pIccTransform)) == - NULL) { - return FALSE; + CFX_DIBitmap* pBitmap = pBitmap1; + if (pBitmap->IsCmykImage() || pIccTransform) { + pBitmap = pBitmap->CloneConvert(FXDIB_Rgb, nullptr, pIccTransform); + if (!pBitmap) + return FALSE; } int width = pSrcRect->Width(), height = pSrcRect->Height(); LPBYTE pBuffer = pBitmap->GetBuffer(); @@ -588,17 +836,18 @@ FX_BOOL CGdiDeviceDriver::GDI_SetDIBits(const CFX_DIBitmap* pBitmap1, } return TRUE; } -FX_BOOL CGdiDeviceDriver::GDI_StretchDIBits(const CFX_DIBitmap* pBitmap1, + +FX_BOOL CGdiDeviceDriver::GDI_StretchDIBits(CFX_DIBitmap* pBitmap1, int dest_left, int dest_top, int dest_width, int dest_height, uint32_t flags, void* pIccTransform) { - CFX_DIBitmap* pBitmap = (CFX_DIBitmap*)pBitmap1; - if (!pBitmap || dest_width == 0 || dest_height == 0) { + CFX_DIBitmap* pBitmap = pBitmap1; + if (!pBitmap || dest_width == 0 || dest_height == 0) return FALSE; - } + if ((pBitmap->IsCmykImage() || pIccTransform) && !pBitmap->ConvertFormat(FXDIB_Rgb, pIccTransform)) { return FALSE; @@ -631,7 +880,8 @@ FX_BOOL CGdiDeviceDriver::GDI_StretchDIBits(const CFX_DIBitmap* pBitmap1, } return TRUE; } -FX_BOOL CGdiDeviceDriver::GDI_StretchBitMask(const CFX_DIBitmap* pBitmap1, + +FX_BOOL CGdiDeviceDriver::GDI_StretchBitMask(CFX_DIBitmap* pBitmap1, int dest_left, int dest_top, int dest_width, @@ -640,10 +890,10 @@ FX_BOOL CGdiDeviceDriver::GDI_StretchBitMask(const CFX_DIBitmap* pBitmap1, uint32_t flags, int alpha_flag, void* pIccTransform) { - CFX_DIBitmap* pBitmap = (CFX_DIBitmap*)pBitmap1; - if (!pBitmap || dest_width == 0 || dest_height == 0) { + CFX_DIBitmap* pBitmap = pBitmap1; + if (!pBitmap || dest_width == 0 || dest_height == 0) return FALSE; - } + Color2Argb(bitmap_color, bitmap_color, alpha_flag | (1 << 24), pIccTransform); int width = pBitmap->GetWidth(), height = pBitmap->GetHeight(); struct { @@ -693,241 +943,15 @@ FX_BOOL CGdiDeviceDriver::GDI_StretchBitMask(const CFX_DIBitmap* pBitmap1, return TRUE; } + FX_BOOL CGdiDeviceDriver::GetClipBox(FX_RECT* pRect) { return ::GetClipBox(m_hDC, (RECT*)pRect); } + FX_BOOL CGdiDeviceDriver::SetClipRgn(void* hRgn) { ::SelectClipRgn(m_hDC, (HRGN)hRgn); return TRUE; } -static HPEN _CreatePen(const CFX_GraphStateData* pGraphState, - const CFX_Matrix* pMatrix, - uint32_t argb) { - FX_FLOAT width; - FX_FLOAT scale = 1.f; - if (pMatrix) - scale = FXSYS_fabs(pMatrix->a) > FXSYS_fabs(pMatrix->b) - ? FXSYS_fabs(pMatrix->a) - : FXSYS_fabs(pMatrix->b); - if (pGraphState) { - width = scale * pGraphState->m_LineWidth; - } else { - width = 1.0f; - } - uint32_t PenStyle = PS_GEOMETRIC; - if (width < 1) { - width = 1; - } - if (pGraphState->m_DashCount) { - PenStyle |= PS_USERSTYLE; - } else { - PenStyle |= PS_SOLID; - } - switch (pGraphState->m_LineCap) { - case 0: - PenStyle |= PS_ENDCAP_FLAT; - break; - case 1: - PenStyle |= PS_ENDCAP_ROUND; - break; - case 2: - PenStyle |= PS_ENDCAP_SQUARE; - break; - } - switch (pGraphState->m_LineJoin) { - case 0: - PenStyle |= PS_JOIN_MITER; - break; - case 1: - PenStyle |= PS_JOIN_ROUND; - break; - case 2: - PenStyle |= PS_JOIN_BEVEL; - break; - } - int a; - FX_COLORREF rgb; - ArgbDecode(argb, a, rgb); - LOGBRUSH lb; - lb.lbColor = rgb; - lb.lbStyle = BS_SOLID; - lb.lbHatch = 0; - uint32_t* pDash = NULL; - if (pGraphState->m_DashCount) { - pDash = FX_Alloc(uint32_t, pGraphState->m_DashCount); - for (int i = 0; i < pGraphState->m_DashCount; i++) { - pDash[i] = FXSYS_round( - pMatrix ? pMatrix->TransformDistance(pGraphState->m_DashArray[i]) - : pGraphState->m_DashArray[i]); - if (pDash[i] < 1) { - pDash[i] = 1; - } - } - } - HPEN hPen = ExtCreatePen(PenStyle, (DWORD)FXSYS_ceil(width), &lb, - pGraphState->m_DashCount, (const DWORD*)pDash); - FX_Free(pDash); - return hPen; -} -static HBRUSH _CreateBrush(uint32_t argb) { - int a; - FX_COLORREF rgb; - ArgbDecode(argb, a, rgb); - return CreateSolidBrush(rgb); -} -static void _SetPathToDC(HDC hDC, - const CFX_PathData* pPathData, - const CFX_Matrix* pMatrix) { - BeginPath(hDC); - int nPoints = pPathData->GetPointCount(); - FX_PATHPOINT* pPoints = pPathData->GetPoints(); - for (int i = 0; i < nPoints; i++) { - FX_FLOAT posx = pPoints[i].m_PointX, posy = pPoints[i].m_PointY; - if (pMatrix) { - pMatrix->Transform(posx, posy); - } - int screen_x = FXSYS_round(posx), screen_y = FXSYS_round(posy); - int point_type = pPoints[i].m_Flag & FXPT_TYPE; - if (point_type == PT_MOVETO) { - MoveToEx(hDC, screen_x, screen_y, NULL); - } else if (point_type == PT_LINETO) { - if (pPoints[i].m_PointY == pPoints[i - 1].m_PointY && - pPoints[i].m_PointX == pPoints[i - 1].m_PointX) { - screen_x++; - } - LineTo(hDC, screen_x, screen_y); - } else if (point_type == PT_BEZIERTO) { - POINT lppt[3]; - lppt[0].x = screen_x; - lppt[0].y = screen_y; - posx = pPoints[i + 1].m_PointX; - posy = pPoints[i + 1].m_PointY; - if (pMatrix) { - pMatrix->Transform(posx, posy); - } - lppt[1].x = FXSYS_round(posx); - lppt[1].y = FXSYS_round(posy); - posx = pPoints[i + 2].m_PointX; - posy = pPoints[i + 2].m_PointY; - if (pMatrix) { - pMatrix->Transform(posx, posy); - } - lppt[2].x = FXSYS_round(posx); - lppt[2].y = FXSYS_round(posy); - PolyBezierTo(hDC, lppt, 3); - i += 2; - } - if (pPoints[i].m_Flag & PT_CLOSEFIGURE) { - CloseFigure(hDC); - } - } - EndPath(hDC); -} - -#ifdef _SKIA_SUPPORT_ -// TODO(caryclark) This antigrain function is duplicated here to permit -// removing the last remaining dependency. Eventually, this will be elminiated -// altogether and replace by Skia code. - -struct rect_base { - FX_FLOAT x1; - FX_FLOAT y1; - FX_FLOAT x2; - FX_FLOAT y2; -}; - -static unsigned clip_liang_barsky(FX_FLOAT x1, - FX_FLOAT y1, - FX_FLOAT x2, - FX_FLOAT y2, - const rect_base& clip_box, - FX_FLOAT* x, - FX_FLOAT* y) { - const FX_FLOAT nearzero = 1e-30f; - FX_FLOAT deltax = x2 - x1; - FX_FLOAT deltay = y2 - y1; - unsigned np = 0; - if (deltax == 0) - deltax = (x1 > clip_box.x1) ? -nearzero : nearzero; - FX_FLOAT xin, xout; - if (deltax > 0) { - xin = clip_box.x1; - xout = clip_box.x2; - } else { - xin = clip_box.x2; - xout = clip_box.x1; - } - FX_FLOAT tinx = (xin - x1) / deltax; - if (deltay == 0) - deltay = (y1 > clip_box.y1) ? -nearzero : nearzero; - FX_FLOAT yin, yout; - if (deltay > 0) { - yin = clip_box.y1; - yout = clip_box.y2; - } else { - yin = clip_box.y2; - yout = clip_box.y1; - } - FX_FLOAT tiny = (yin - y1) / deltay; - FX_FLOAT tin1, tin2; - if (tinx < tiny) { - tin1 = tinx; - tin2 = tiny; - } else { - tin1 = tiny; - tin2 = tinx; - } - if (tin1 <= 1.0f) { - if (0 < tin1) { - *x++ = xin; - *y++ = yin; - ++np; - } - if (tin2 <= 1.0f) { - FX_FLOAT toutx = (xout - x1) / deltax; - FX_FLOAT touty = (yout - y1) / deltay; - FX_FLOAT tout1 = (toutx < touty) ? toutx : touty; - if (tin2 > 0 || tout1 > 0) { - if (tin2 <= tout1) { - if (tin2 > 0) { - if (tinx > tiny) { - *x++ = xin; - *y++ = y1 + (deltay * tinx); - } else { - *x++ = x1 + (deltax * tiny); - *y++ = yin; - } - ++np; - } - if (tout1 < 1.0f) { - if (toutx < touty) { - *x++ = xout; - *y++ = y1 + (deltay * toutx); - } else { - *x++ = x1 + (deltax * touty); - *y++ = yout; - } - } else { - *x++ = x2; - *y++ = y2; - } - ++np; - } else { - if (tinx > tiny) { - *x++ = xin; - *y++ = yout; - } else { - *x++ = xout; - *y++ = yin; - } - ++np; - } - } - } - } - return np; -} -#endif void CGdiDeviceDriver::DrawLine(FX_FLOAT x1, FX_FLOAT y1, @@ -966,13 +990,10 @@ void CGdiDeviceDriver::DrawLine(FX_FLOAT x1, y2 = y[np - 1]; } } - MoveToEx(m_hDC, FXSYS_round(x1), FXSYS_round(y1), NULL); + MoveToEx(m_hDC, FXSYS_round(x1), FXSYS_round(y1), nullptr); LineTo(m_hDC, FXSYS_round(x2), FXSYS_round(y2)); } -static FX_BOOL _MatrixNoScaled(const CFX_Matrix* pMatrix) { - return pMatrix->GetA() == 1.0f && pMatrix->GetB() == 0 && - pMatrix->GetC() == 0 && pMatrix->GetD() == 1.0f; -} + FX_BOOL CGdiDeviceDriver::DrawPath(const CFX_PathData* pPathData, const CFX_Matrix* pMatrix, const CFX_GraphStateData* pGraphState, @@ -1019,7 +1040,7 @@ FX_BOOL CGdiDeviceDriver::DrawPath(const CFX_PathData* pPathData, if (bDrawAlpha || ((m_DeviceClass != FXDC_PRINTER && !(fill_mode & FXFILL_FULLCOVER)) || (pGraphState && pGraphState->m_DashCount))) { - if (!((NULL == pMatrix || _MatrixNoScaled(pMatrix)) && pGraphState && + if (!((!pMatrix || MatrixNoScaled(pMatrix)) && pGraphState && pGraphState->m_LineWidth == 1.f && (pPathData->GetPointCount() == 5 || pPathData->GetPointCount() == 4) && @@ -1034,16 +1055,16 @@ FX_BOOL CGdiDeviceDriver::DrawPath(const CFX_PathData* pPathData, } int old_fill_mode = fill_mode; fill_mode &= 3; - HPEN hPen = NULL; - HBRUSH hBrush = NULL; + HPEN hPen = nullptr; + HBRUSH hBrush = nullptr; if (pGraphState && stroke_alpha) { - SetMiterLimit(m_hDC, pGraphState->m_MiterLimit, NULL); - hPen = _CreatePen(pGraphState, pMatrix, stroke_color); + SetMiterLimit(m_hDC, pGraphState->m_MiterLimit, nullptr); + hPen = CreatePen(pGraphState, pMatrix, stroke_color); hPen = (HPEN)SelectObject(m_hDC, hPen); } if (fill_mode && fill_alpha) { SetPolyFillMode(m_hDC, fill_mode); - hBrush = _CreateBrush(fill_color); + hBrush = CreateBrush(fill_color); hBrush = (HBRUSH)SelectObject(m_hDC, hBrush); } if (pPathData->GetPointCount() == 2 && pGraphState && @@ -1058,14 +1079,14 @@ FX_BOOL CGdiDeviceDriver::DrawPath(const CFX_PathData* pPathData, } DrawLine(x1, y1, x2, y2); } else { - _SetPathToDC(m_hDC, pPathData, pMatrix); + SetPathToDC(m_hDC, pPathData, pMatrix); if (pGraphState && stroke_alpha) { if (fill_mode && fill_alpha) { if (old_fill_mode & FX_FILL_TEXT_MODE) { StrokeAndFillPath(m_hDC); } else { FillPath(m_hDC); - _SetPathToDC(m_hDC, pPathData, pMatrix); + SetPathToDC(m_hDC, pPathData, pMatrix); StrokePath(m_hDC); } } else { @@ -1085,6 +1106,7 @@ FX_BOOL CGdiDeviceDriver::DrawPath(const CFX_PathData* pPathData, } return TRUE; } + FX_BOOL CGdiDeviceDriver::FillRect(const FX_RECT* pRect, uint32_t fill_color, int alpha_flag, @@ -1108,6 +1130,7 @@ FX_BOOL CGdiDeviceDriver::FillRect(const FX_RECT* pRect, DeleteObject(hBrush); return TRUE; } + FX_BOOL CGdiDeviceDriver::SetClip_PathFill(const CFX_PathData* pPathData, const CFX_Matrix* pMatrix, int fill_mode) { @@ -1119,18 +1142,19 @@ FX_BOOL CGdiDeviceDriver::SetClip_PathFill(const CFX_PathData* pPathData, return TRUE; } } - _SetPathToDC(m_hDC, pPathData, pMatrix); + SetPathToDC(m_hDC, pPathData, pMatrix); SetPolyFillMode(m_hDC, fill_mode & 3); SelectClipPath(m_hDC, RGN_AND); return TRUE; } + FX_BOOL CGdiDeviceDriver::SetClip_PathStroke( const CFX_PathData* pPathData, const CFX_Matrix* pMatrix, const CFX_GraphStateData* pGraphState) { - HPEN hPen = _CreatePen(pGraphState, pMatrix, 0xff000000); + HPEN hPen = CreatePen(pGraphState, pMatrix, 0xff000000); hPen = (HPEN)SelectObject(m_hDC, hPen); - _SetPathToDC(m_hDC, pPathData, pMatrix); + SetPathToDC(m_hDC, pPathData, pMatrix); WidenPath(m_hDC); SetPolyFillMode(m_hDC, WINDING); FX_BOOL ret = SelectClipPath(m_hDC, RGN_AND); @@ -1138,6 +1162,7 @@ FX_BOOL CGdiDeviceDriver::SetClip_PathStroke( DeleteObject(hPen); return ret; } + FX_BOOL CGdiDeviceDriver::DrawCosmeticLine(FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, @@ -1158,16 +1183,18 @@ FX_BOOL CGdiDeviceDriver::DrawCosmeticLine(FX_FLOAT x1, } HPEN hPen = CreatePen(PS_SOLID, 1, rgb); hPen = (HPEN)SelectObject(m_hDC, hPen); - MoveToEx(m_hDC, FXSYS_round(x1), FXSYS_round(y1), NULL); + MoveToEx(m_hDC, FXSYS_round(x1), FXSYS_round(y1), nullptr); LineTo(m_hDC, FXSYS_round(x2), FXSYS_round(y2)); hPen = (HPEN)SelectObject(m_hDC, hPen); DeleteObject(hPen); return TRUE; } + FX_BOOL CGdiDeviceDriver::DeleteDeviceRgn(void* pRgn) { DeleteObject((HGDIOBJ)pRgn); return TRUE; } + CGdiDisplayDriver::CGdiDisplayDriver(HDC hDC) : CGdiDeviceDriver(hDC, FXDC_DISPLAY) { CWin32Platform* pPlatform = @@ -1176,6 +1203,7 @@ CGdiDisplayDriver::CGdiDisplayDriver(HDC hDC) m_RenderCaps |= FXRC_ALPHA_PATH | FXRC_ALPHA_IMAGE; } } + FX_BOOL CGdiDisplayDriver::GetDIBits(CFX_DIBitmap* pBitmap, int left, int top, @@ -1198,7 +1226,7 @@ FX_BOOL CGdiDisplayDriver::GetDIBits(CFX_DIBitmap* pBitmap, bmi.bmiHeader.biWidth = width; if (!CFX_GEModule::Get()->GetCodecModule() || !CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) { - pIccTransform = NULL; + pIccTransform = nullptr; } if (pBitmap->GetBPP() > 8 && !pBitmap->IsCmykImage() && !pIccTransform) { ret = ::GetDIBits(hDCMemory, hbmp, 0, height, pBitmap->GetBuffer(), &bmi, @@ -1222,6 +1250,7 @@ FX_BOOL CGdiDisplayDriver::GetDIBits(CFX_DIBitmap* pBitmap, DeleteObject(hDCMemory); return ret; } + FX_BOOL CGdiDisplayDriver::SetDIBits(const CFX_DIBSource* pSource, uint32_t color, const FX_RECT* pSrcRect, @@ -1239,15 +1268,15 @@ FX_BOOL CGdiDisplayDriver::SetDIBits(const CFX_DIBSource* pSource, if (pSource->GetBPP() != 1 || alpha != 255) { CFX_DIBitmap background; if (!background.Create(width, height, FXDIB_Rgb32) || - !GetDIBits(&background, left, top, NULL) || + !GetDIBits(&background, left, top, nullptr) || !background.CompositeMask(0, 0, width, height, pSource, color, 0, 0, - FXDIB_BLEND_NORMAL, NULL, FALSE, alpha_flag, - pIccTransform)) { + FXDIB_BLEND_NORMAL, nullptr, FALSE, + alpha_flag, pIccTransform)) { return FALSE; } FX_RECT src_rect(0, 0, width, height); return SetDIBits(&background, 0, &src_rect, left, top, FXDIB_BLEND_NORMAL, - 0, NULL); + 0, nullptr); } FX_RECT clip_rect(left, top, left + pSrcRect->Width(), top + pSrcRect->Height()); @@ -1259,15 +1288,15 @@ FX_BOOL CGdiDisplayDriver::SetDIBits(const CFX_DIBSource* pSource, if (pSource->HasAlpha()) { CFX_DIBitmap bitmap; if (!bitmap.Create(width, height, FXDIB_Rgb) || - !GetDIBits(&bitmap, left, top, NULL) || + !GetDIBits(&bitmap, left, top, nullptr) || !bitmap.CompositeBitmap(0, 0, width, height, pSource, pSrcRect->left, - pSrcRect->top, FXDIB_BLEND_NORMAL, NULL, FALSE, - pIccTransform)) { + pSrcRect->top, FXDIB_BLEND_NORMAL, nullptr, + FALSE, pIccTransform)) { return FALSE; } FX_RECT src_rect(0, 0, width, height); return SetDIBits(&bitmap, 0, &src_rect, left, top, FXDIB_BLEND_NORMAL, 0, - NULL); + nullptr); } CFX_DIBExtractor temp(pSource); CFX_DIBitmap* pBitmap = temp; @@ -1276,6 +1305,7 @@ FX_BOOL CGdiDisplayDriver::SetDIBits(const CFX_DIBSource* pSource, } return FALSE; } + FX_BOOL CGdiDisplayDriver::UseFoxitStretchEngine(const CFX_DIBSource* pSource, uint32_t color, int dest_left, @@ -1295,18 +1325,17 @@ FX_BOOL CGdiDisplayDriver::UseFoxitStretchEngine(const CFX_DIBSource* pSource, dest_top += dest_height; } bitmap_clip.Offset(-dest_left, -dest_top); - CFX_DIBitmap* pStretched = - pSource->StretchTo(dest_width, dest_height, render_flags, &bitmap_clip); - if (!pStretched) { + std::unique_ptr pStretched( + pSource->StretchTo(dest_width, dest_height, render_flags, &bitmap_clip)); + if (!pStretched) return TRUE; - } + FX_RECT src_rect(0, 0, pStretched->GetWidth(), pStretched->GetHeight()); - FX_BOOL ret = - SetDIBits(pStretched, color, &src_rect, pClipRect->left, pClipRect->top, - FXDIB_BLEND_NORMAL, alpha_flag, pIccTransform); - delete pStretched; - return ret; + return SetDIBits(pStretched.get(), color, &src_rect, pClipRect->left, + pClipRect->top, FXDIB_BLEND_NORMAL, alpha_flag, + pIccTransform); } + FX_BOOL CGdiDisplayDriver::StretchDIBits(const CFX_DIBSource* pSource, uint32_t color, int dest_left, @@ -1335,27 +1364,25 @@ FX_BOOL CGdiDisplayDriver::StretchDIBits(const CFX_DIBSource* pSource, clip_rect.Intersect(*pClipRect); clip_rect.Offset(-image_rect.left, -image_rect.top); int clip_width = clip_rect.Width(), clip_height = clip_rect.Height(); - CFX_DIBitmap* pStretched = - pSource->StretchTo(dest_width, dest_height, flags, &clip_rect); - if (!pStretched) { + std::unique_ptr pStretched( + pSource->StretchTo(dest_width, dest_height, flags, &clip_rect)); + if (!pStretched) return TRUE; - } + CFX_DIBitmap background; if (!background.Create(clip_width, clip_height, FXDIB_Rgb32) || !GetDIBits(&background, image_rect.left + clip_rect.left, - image_rect.top + clip_rect.top, NULL) || - !background.CompositeMask(0, 0, clip_width, clip_height, pStretched, - color, 0, 0, FXDIB_BLEND_NORMAL, NULL, FALSE, - alpha_flag, pIccTransform)) { - delete pStretched; + image_rect.top + clip_rect.top, nullptr) || + !background.CompositeMask( + 0, 0, clip_width, clip_height, pStretched.get(), color, 0, 0, + FXDIB_BLEND_NORMAL, nullptr, FALSE, alpha_flag, pIccTransform)) { return FALSE; } + FX_RECT src_rect(0, 0, clip_width, clip_height); - FX_BOOL ret = - SetDIBits(&background, 0, &src_rect, image_rect.left + clip_rect.left, - image_rect.top + clip_rect.top, FXDIB_BLEND_NORMAL, 0, NULL); - delete pStretched; - return ret; + return SetDIBits( + &background, 0, &src_rect, image_rect.left + clip_rect.left, + image_rect.top + clip_rect.top, FXDIB_BLEND_NORMAL, 0, nullptr); } if (pSource->HasAlpha()) { CWin32Platform* pPlatform = @@ -1364,9 +1391,8 @@ FX_BOOL CGdiDisplayDriver::StretchDIBits(const CFX_DIBSource* pSource, !pSource->IsCmykImage()) { CFX_DIBExtractor temp(pSource); CFX_DIBitmap* pBitmap = temp; - if (!pBitmap) { + if (!pBitmap) return FALSE; - } return pPlatform->m_GdiplusExt.StretchDIBits( m_hDC, pBitmap, dest_left, dest_top, dest_width, dest_height, pClipRect, flags); @@ -1383,45 +1409,10 @@ FX_BOOL CGdiDisplayDriver::StretchDIBits(const CFX_DIBSource* pSource, } return FALSE; } -#define GET_PS_FEATURESETTING 4121 -#define FEATURESETTING_PSLEVEL 2 -int GetPSLevel(HDC hDC) { - int device_type = ::GetDeviceCaps(hDC, TECHNOLOGY); - if (device_type != DT_RASPRINTER) { - return 0; - } - uint32_t esc = GET_PS_FEATURESETTING; - if (ExtEscape(hDC, QUERYESCSUPPORT, sizeof esc, (char*)&esc, 0, NULL)) { - int param = FEATURESETTING_PSLEVEL; - if (ExtEscape(hDC, GET_PS_FEATURESETTING, sizeof(int), (char*)¶m, - sizeof(int), (char*)¶m) > 0) { - return param; - } - } - esc = POSTSCRIPT_IDENTIFY; - if (ExtEscape(hDC, QUERYESCSUPPORT, sizeof esc, (char*)&esc, 0, NULL) == 0) { - esc = POSTSCRIPT_DATA; - if (ExtEscape(hDC, QUERYESCSUPPORT, sizeof esc, (char*)&esc, 0, NULL)) { - return 2; - } - return 0; - } - esc = PSIDENT_GDICENTRIC; - if (ExtEscape(hDC, POSTSCRIPT_IDENTIFY, sizeof(uint32_t), (char*)&esc, 0, - NULL) <= 0) { - return 2; - } - esc = GET_PS_FEATURESETTING; - if (ExtEscape(hDC, QUERYESCSUPPORT, sizeof esc, (char*)&esc, 0, NULL)) { - int param = FEATURESETTING_PSLEVEL; - if (ExtEscape(hDC, GET_PS_FEATURESETTING, sizeof(int), (char*)¶m, - sizeof(int), (char*)¶m) > 0) { - return param; - } - } - return 2; -} + +// static int CFX_WindowsDevice::m_psLevel = 2; + CFX_WindowsDevice::CFX_WindowsDevice(HDC hDC, FX_BOOL bCmykOutput, FX_BOOL bForcePSOutput, @@ -1436,29 +1427,23 @@ CFX_WindowsDevice::CFX_WindowsDevice(HDC hDC, } SetDeviceDriver(CreateDriver(hDC, bCmykOutput)); } + HDC CFX_WindowsDevice::GetDC() const { IFX_RenderDeviceDriver* pRDD = GetDeviceDriver(); - if (!pRDD) { - return NULL; - } - return (HDC)pRDD->GetPlatformSurface(); + return pRDD ? reinterpret_cast(pRDD->GetPlatformSurface()) : nullptr; } + IFX_RenderDeviceDriver* CFX_WindowsDevice::CreateDriver(HDC hDC, FX_BOOL bCmykOutput) { int device_type = ::GetDeviceCaps(hDC, TECHNOLOGY); int obj_type = ::GetObjectType(hDC); - int device_class; - if (device_type == DT_RASPRINTER || device_type == DT_PLOTTER || - obj_type == OBJ_ENHMETADC) { - device_class = FXDC_PRINTER; - } else { - device_class = FXDC_DISPLAY; - } - if (device_class == FXDC_PRINTER) { + bool use_printer = device_type == DT_RASPRINTER || + device_type == DT_PLOTTER || obj_type == OBJ_ENHMETADC; + if (use_printer) return new CGdiPrinterDriver(hDC); - } return new CGdiDisplayDriver(hDC); } + CFX_WinBitmapDevice::CFX_WinBitmapDevice(int width, int height, FXDIB_Format format) { @@ -1469,28 +1454,29 @@ CFX_WinBitmapDevice::CFX_WinBitmapDevice(int width, bmih.biHeight = -height; bmih.biPlanes = 1; bmih.biWidth = width; - uint8_t* pBuffer; - m_hBitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, - (void**)&pBuffer, NULL, 0); - if (!m_hBitmap) { + void* pBufferPtr; + m_hBitmap = CreateDIBSection(nullptr, reinterpret_cast(&bmih), + DIB_RGB_COLORS, &pBufferPtr, nullptr, 0); + if (!m_hBitmap) return; - } + + uint8_t* pBuffer = static_cast(pBufferPtr); CFX_DIBitmap* pBitmap = new CFX_DIBitmap; pBitmap->Create(width, height, format, pBuffer); SetBitmap(pBitmap); - m_hDC = ::CreateCompatibleDC(NULL); + m_hDC = ::CreateCompatibleDC(nullptr); m_hOldBitmap = (HBITMAP)SelectObject(m_hDC, m_hBitmap); IFX_RenderDeviceDriver* pDriver = new CGdiDisplayDriver(m_hDC); SetDeviceDriver(pDriver); } + CFX_WinBitmapDevice::~CFX_WinBitmapDevice() { if (m_hDC) { SelectObject(m_hDC, m_hOldBitmap); DeleteDC(m_hDC); } - if (m_hBitmap) { + if (m_hBitmap) DeleteObject(m_hBitmap); - } delete GetBitmap(); } diff --git a/core/fxge/win32/fx_win32_print.cpp b/core/fxge/win32/fx_win32_print.cpp index 9fd1683a80..e46beeefbd 100644 --- a/core/fxge/win32/fx_win32_print.cpp +++ b/core/fxge/win32/fx_win32_print.cpp @@ -16,135 +16,18 @@ #include "core/fxge/include/fx_ge_win32.h" #include "core/fxge/win32/win32_int.h" -#define SIZETHRESHOLD 1000 -#define OUTPUTPSLEN 4096 -CGdiPrinterDriver::CGdiPrinterDriver(HDC hDC) - : CGdiDeviceDriver(hDC, FXDC_PRINTER) { - m_HorzSize = ::GetDeviceCaps(m_hDC, HORZSIZE); - m_VertSize = ::GetDeviceCaps(m_hDC, VERTSIZE); - m_bSupportROP = TRUE; -} -int CGdiPrinterDriver::GetDeviceCaps(int caps_id) { - if (caps_id == FXDC_HORZ_SIZE) { - return m_HorzSize; - } - if (caps_id == FXDC_VERT_SIZE) { - return m_VertSize; - } - return CGdiDeviceDriver::GetDeviceCaps(caps_id); -} -FX_BOOL CGdiPrinterDriver::SetDIBits(const CFX_DIBSource* pSource, - uint32_t color, - const FX_RECT* pSrcRect, - int left, - int top, - int blend_type, - int alpha_flag, - void* pIccTransform) { - if (pSource->IsAlphaMask()) { - FX_RECT clip_rect(left, top, left + pSrcRect->Width(), - top + pSrcRect->Height()); - return StretchDIBits(pSource, color, left - pSrcRect->left, - top - pSrcRect->top, pSource->GetWidth(), - pSource->GetHeight(), &clip_rect, 0, alpha_flag, - pIccTransform, FXDIB_BLEND_NORMAL); - } - ASSERT(pSource && !pSource->IsAlphaMask() && pSrcRect); - ASSERT(blend_type == FXDIB_BLEND_NORMAL); - if (pSource->HasAlpha()) { - return FALSE; - } - CFX_DIBExtractor temp(pSource); - CFX_DIBitmap* pBitmap = temp; - if (!pBitmap) { - return FALSE; - } - return GDI_SetDIBits(pBitmap, pSrcRect, left, top, pIccTransform); -} -FX_BOOL CGdiPrinterDriver::StretchDIBits(const CFX_DIBSource* pSource, - uint32_t color, - int dest_left, - int dest_top, - int dest_width, - int dest_height, - const FX_RECT* pClipRect, - uint32_t flags, - int alpha_flag, - void* pIccTransform, - int blend_type) { - if (pSource->IsAlphaMask()) { - int alpha = FXGETFLAG_COLORTYPE(alpha_flag) - ? FXGETFLAG_ALPHA_FILL(alpha_flag) - : FXARGB_A(color); - if (pSource->GetBPP() != 1 || alpha != 255 || !m_bSupportROP) { - return FALSE; - } - if (dest_width < 0 || dest_height < 0) { - CFX_DIBitmap* pFlipped = - pSource->FlipImage(dest_width < 0, dest_height < 0); - if (!pFlipped) { - return FALSE; - } - if (dest_width < 0) { - dest_left += dest_width; - } - if (dest_height < 0) { - dest_top += dest_height; - } - FX_BOOL ret = GDI_StretchBitMask(pFlipped, dest_left, dest_top, - abs(dest_width), abs(dest_height), color, - flags, alpha_flag, pIccTransform); - delete pFlipped; - return ret; - } - CFX_DIBExtractor temp(pSource); - CFX_DIBitmap* pBitmap = temp; - if (!pBitmap) { - return FALSE; - } - return GDI_StretchBitMask(pBitmap, dest_left, dest_top, dest_width, - dest_height, color, flags, alpha_flag, - pIccTransform); - } - if (pSource->HasAlpha()) { - return FALSE; - } - if (dest_width < 0 || dest_height < 0) { - CFX_DIBitmap* pFlipped = - pSource->FlipImage(dest_width < 0, dest_height < 0); - if (!pFlipped) { - return FALSE; - } - if (dest_width < 0) { - dest_left += dest_width; - } - if (dest_height < 0) { - dest_top += dest_height; - } - FX_BOOL ret = - GDI_StretchDIBits(pFlipped, dest_left, dest_top, abs(dest_width), - abs(dest_height), flags, pIccTransform); - delete pFlipped; - return ret; - } - CFX_DIBExtractor temp(pSource); - CFX_DIBitmap* pBitmap = temp; - if (!pBitmap) { - return FALSE; - } - return GDI_StretchDIBits(pBitmap, dest_left, dest_top, dest_width, - dest_height, flags, pIccTransform); -} -static CFX_DIBitmap* Transform1bppBitmap(const CFX_DIBSource* pSrc, - const CFX_Matrix* pDestMatrix) { +namespace { + +CFX_DIBitmap* Transform1bppBitmap(const CFX_DIBSource* pSrc, + const CFX_Matrix* pDestMatrix) { ASSERT(pSrc->GetFormat() == FXDIB_1bppRgb || pSrc->GetFormat() == FXDIB_1bppMask || pSrc->GetFormat() == FXDIB_1bppCmyk); CFX_DIBExtractor src_bitmap(pSrc); CFX_DIBitmap* pSrcBitmap = src_bitmap; - if (!pSrcBitmap) { - return NULL; - } + if (!pSrcBitmap) + return nullptr; + int src_width = pSrcBitmap->GetWidth(), src_height = pSrcBitmap->GetHeight(); uint8_t* src_buf = pSrcBitmap->GetBuffer(); uint32_t src_pitch = pSrcBitmap->GetPitch(); @@ -168,14 +51,13 @@ static CFX_DIBitmap* Transform1bppBitmap(const CFX_DIBSource* pSrc, CPDF_FixedMatrix result2src_fix(result2src, 8); int result_width = result_rect.Width(); int result_height = result_rect.Height(); - CFX_DIBitmap* pTempBitmap = new CFX_DIBitmap; + std::unique_ptr pTempBitmap(new CFX_DIBitmap); if (!pTempBitmap->Create(result_width, result_height, pSrc->GetFormat())) { - delete pTempBitmap; - if (pSrcBitmap != src_bitmap) { + if (pSrcBitmap != src_bitmap) delete pSrcBitmap; - } - return NULL; + return nullptr; } + pTempBitmap->CopyPalette(pSrc->GetPalette()); uint8_t* dest_buf = pTempBitmap->GetBuffer(); int dest_pitch = pTempBitmap->GetPitch(); @@ -215,11 +97,128 @@ static CFX_DIBitmap* Transform1bppBitmap(const CFX_DIBSource* pSrc, } } } - if (pSrcBitmap != src_bitmap) { + if (pSrcBitmap != src_bitmap) delete pSrcBitmap; + + return pTempBitmap.release(); +} + +} // namespace + +CGdiPrinterDriver::CGdiPrinterDriver(HDC hDC) + : CGdiDeviceDriver(hDC, FXDC_PRINTER), + m_HorzSize(::GetDeviceCaps(m_hDC, HORZSIZE)), + m_VertSize(::GetDeviceCaps(m_hDC, VERTSIZE)) {} + +CGdiPrinterDriver::~CGdiPrinterDriver() {} + +int CGdiPrinterDriver::GetDeviceCaps(int caps_id) { + if (caps_id == FXDC_HORZ_SIZE) + return m_HorzSize; + if (caps_id == FXDC_VERT_SIZE) + return m_VertSize; + return CGdiDeviceDriver::GetDeviceCaps(caps_id); +} + +FX_BOOL CGdiPrinterDriver::SetDIBits(const CFX_DIBSource* pSource, + uint32_t color, + const FX_RECT* pSrcRect, + int left, + int top, + int blend_type, + int alpha_flag, + void* pIccTransform) { + if (pSource->IsAlphaMask()) { + FX_RECT clip_rect(left, top, left + pSrcRect->Width(), + top + pSrcRect->Height()); + return StretchDIBits(pSource, color, left - pSrcRect->left, + top - pSrcRect->top, pSource->GetWidth(), + pSource->GetHeight(), &clip_rect, 0, alpha_flag, + pIccTransform, FXDIB_BLEND_NORMAL); } - return pTempBitmap; + ASSERT(pSource && !pSource->IsAlphaMask() && pSrcRect); + ASSERT(blend_type == FXDIB_BLEND_NORMAL); + if (pSource->HasAlpha()) + return FALSE; + + CFX_DIBExtractor temp(pSource); + CFX_DIBitmap* pBitmap = temp; + if (!pBitmap) + return FALSE; + + return GDI_SetDIBits(pBitmap, pSrcRect, left, top, pIccTransform); } + +FX_BOOL CGdiPrinterDriver::StretchDIBits(const CFX_DIBSource* pSource, + uint32_t color, + int dest_left, + int dest_top, + int dest_width, + int dest_height, + const FX_RECT* pClipRect, + uint32_t flags, + int alpha_flag, + void* pIccTransform, + int blend_type) { + if (pSource->IsAlphaMask()) { + int alpha = FXGETFLAG_COLORTYPE(alpha_flag) + ? FXGETFLAG_ALPHA_FILL(alpha_flag) + : FXARGB_A(color); + if (pSource->GetBPP() != 1 || alpha != 255) + return FALSE; + + if (dest_width < 0 || dest_height < 0) { + std::unique_ptr pFlipped( + pSource->FlipImage(dest_width < 0, dest_height < 0)); + if (!pFlipped) + return FALSE; + + if (dest_width < 0) + dest_left += dest_width; + if (dest_height < 0) + dest_top += dest_height; + + return GDI_StretchBitMask(pFlipped.get(), dest_left, dest_top, + abs(dest_width), abs(dest_height), color, flags, + alpha_flag, pIccTransform); + } + + CFX_DIBExtractor temp(pSource); + CFX_DIBitmap* pBitmap = temp; + if (!pBitmap) + return FALSE; + return GDI_StretchBitMask(pBitmap, dest_left, dest_top, dest_width, + dest_height, color, flags, alpha_flag, + pIccTransform); + } + + if (pSource->HasAlpha()) + return FALSE; + + if (dest_width < 0 || dest_height < 0) { + std::unique_ptr pFlipped( + pSource->FlipImage(dest_width < 0, dest_height < 0)); + if (!pFlipped) + return FALSE; + + if (dest_width < 0) + dest_left += dest_width; + if (dest_height < 0) + dest_top += dest_height; + + return GDI_StretchDIBits(pFlipped.get(), dest_left, dest_top, + abs(dest_width), abs(dest_height), flags, + pIccTransform); + } + + CFX_DIBExtractor temp(pSource); + CFX_DIBitmap* pBitmap = temp; + if (!pBitmap) + return FALSE; + return GDI_StretchDIBits(pBitmap, dest_left, dest_top, dest_width, + dest_height, flags, pIccTransform); +} + FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource, int bitmap_alpha, uint32_t color, @@ -230,7 +229,7 @@ FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource, void* pIccTransform, int blend_type) { if (bitmap_alpha < 255 || pSource->HasAlpha() || - (pSource->IsAlphaMask() && (pSource->GetBPP() != 1 || !m_bSupportROP))) { + (pSource->IsAlphaMask() && (pSource->GetBPP() != 1))) { return FALSE; } CFX_FloatRect unit_rect = pMatrix->GetUnitRect(); @@ -244,39 +243,39 @@ FX_BOOL CGdiPrinterDriver::StartDIBits(const CFX_DIBSource* pSource, bFlipY ? full_rect.bottom : full_rect.top, bFlipX ? -full_rect.Width() : full_rect.Width(), bFlipY ? -full_rect.Height() : full_rect.Height(), - NULL, 0, alpha_flag, pIccTransform, blend_type); + nullptr, 0, alpha_flag, pIccTransform, blend_type); } if (FXSYS_fabs(pMatrix->a) < 0.5f && FXSYS_fabs(pMatrix->d) < 0.5f) { - CFX_DIBitmap* pTransformed = - pSource->SwapXY(pMatrix->c > 0, pMatrix->b < 0); - if (!pTransformed) { - return FALSE; - } - FX_BOOL ret = StretchDIBits( - pTransformed, color, full_rect.left, full_rect.top, full_rect.Width(), - full_rect.Height(), NULL, 0, alpha_flag, pIccTransform, blend_type); - delete pTransformed; - return ret; - } - if (pSource->GetBPP() == 1) { - CFX_DIBitmap* pTransformed = Transform1bppBitmap(pSource, pMatrix); - if (!pIccTransform) { + std::unique_ptr pTransformed( + pSource->SwapXY(pMatrix->c > 0, pMatrix->b < 0)); + if (!pTransformed) return FALSE; - } - SaveState(); - CFX_PathData path; - path.AppendRect(0, 0, 1.0f, 1.0f); - SetClip_PathFill(&path, pMatrix, WINDING); - FX_BOOL ret = StretchDIBits( - pTransformed, color, full_rect.left, full_rect.top, full_rect.Width(), - full_rect.Height(), NULL, 0, alpha_flag, pIccTransform, blend_type); - RestoreState(false); - delete pTransformed; - handle = NULL; - return ret; + + return StretchDIBits(pTransformed.get(), color, full_rect.left, + full_rect.top, full_rect.Width(), full_rect.Height(), + nullptr, 0, alpha_flag, pIccTransform, blend_type); } - return FALSE; + if (pSource->GetBPP() != 1) + return FALSE; + + std::unique_ptr pTransformed( + Transform1bppBitmap(pSource, pMatrix)); + if (!pIccTransform) + return FALSE; + + SaveState(); + CFX_PathData path; + path.AppendRect(0, 0, 1.0f, 1.0f); + SetClip_PathFill(&path, pMatrix, WINDING); + FX_BOOL ret = + StretchDIBits(pTransformed.get(), color, full_rect.left, full_rect.top, + full_rect.Width(), full_rect.Height(), nullptr, 0, + alpha_flag, pIccTransform, blend_type); + RestoreState(false); + handle = nullptr; + return ret; } + CPSOutput::CPSOutput(HDC hDC) { m_hDC = hDC; m_pBuf = NULL; diff --git a/core/fxge/win32/win32_int.h b/core/fxge/win32/win32_int.h index d2d091bc19..9d62b41a8e 100644 --- a/core/fxge/win32/win32_int.h +++ b/core/fxge/win32/win32_int.h @@ -107,6 +107,9 @@ class CWin32Platform { class CGdiDeviceDriver : public IFX_RenderDeviceDriver { protected: + CGdiDeviceDriver(HDC hDC, int device_class); + ~CGdiDeviceDriver() override {} + // IFX_RenderDeviceDriver int GetDeviceCaps(int caps_id) override; void SaveState() override; @@ -147,19 +150,19 @@ class CGdiDeviceDriver : public IFX_RenderDeviceDriver { virtual FX_BOOL DeleteDeviceRgn(void* pRgn); virtual void DrawLine(FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, FX_FLOAT y2); - FX_BOOL GDI_SetDIBits(const CFX_DIBitmap* pBitmap, + FX_BOOL GDI_SetDIBits(CFX_DIBitmap* pBitmap, const FX_RECT* pSrcRect, int left, int top, void* pIccTransform); - FX_BOOL GDI_StretchDIBits(const CFX_DIBitmap* pBitmap, + FX_BOOL GDI_StretchDIBits(CFX_DIBitmap* pBitmap, int dest_left, int dest_top, int dest_width, int dest_height, uint32_t flags, void* pIccTransform); - FX_BOOL GDI_StretchBitMask(const CFX_DIBitmap* pBitmap, + FX_BOOL GDI_StretchBitMask(CFX_DIBitmap* pBitmap, int dest_left, int dest_top, int dest_width, @@ -168,11 +171,13 @@ class CGdiDeviceDriver : public IFX_RenderDeviceDriver { uint32_t flags, int alpha_flag, void* pIccTransform); + HDC m_hDC; - int m_Width, m_Height, m_nBitsPerPixel; - int m_DeviceClass, m_RenderCaps; - CGdiDeviceDriver(HDC hDC, int device_class); - ~CGdiDeviceDriver() override {} + int m_Width; + int m_Height; + int m_nBitsPerPixel; + int m_DeviceClass; + int m_RenderCaps; }; class CGdiDisplayDriver : public CGdiDeviceDriver { @@ -227,9 +232,11 @@ class CGdiDisplayDriver : public CGdiDeviceDriver { void* pIccTransform = NULL, int blend_type = FXDIB_BLEND_NORMAL); }; + class CGdiPrinterDriver : public CGdiDeviceDriver { public: - CGdiPrinterDriver(HDC hDC); + explicit CGdiPrinterDriver(HDC hDC); + ~CGdiPrinterDriver() override; protected: int GetDeviceCaps(int caps_id) override; @@ -261,8 +268,9 @@ class CGdiPrinterDriver : public CGdiDeviceDriver { int alpha_flag, void* pIccTransform, int blend_type) override; - int m_HorzSize, m_VertSize; - FX_BOOL m_bSupportROP; + + const int m_HorzSize; + const int m_VertSize; }; class CPSOutput : public IFX_PSOutput { diff --git a/fpdfsdk/fpdf_sysfontinfo.cpp b/fpdfsdk/fpdf_sysfontinfo.cpp index 18b01341ee..9d05903a61 100644 --- a/fpdfsdk/fpdf_sysfontinfo.cpp +++ b/fpdfsdk/fpdf_sysfontinfo.cpp @@ -14,11 +14,9 @@ class CFX_ExternalFontInfo final : public IFX_SystemFontInfo { public: explicit CFX_ExternalFontInfo(FPDF_SYSFONTINFO* pInfo) : m_pInfo(pInfo) {} - - void Release() override { + ~CFX_ExternalFontInfo() override { if (m_pInfo->Release) m_pInfo->Release(m_pInfo); - delete this; } FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override { @@ -83,8 +81,6 @@ class CFX_ExternalFontInfo final : public IFX_SystemFontInfo { } private: - ~CFX_ExternalFontInfo() override {} - FPDF_SYSFONTINFO* const m_pInfo; }; @@ -99,7 +95,8 @@ DLLEXPORT void STDCALL FPDF_SetSystemFontInfo(FPDF_SYSFONTINFO* pFontInfoExt) { return; CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo( - new CFX_ExternalFontInfo(pFontInfoExt)); + std::unique_ptr( + new CFX_ExternalFontInfo(pFontInfoExt))); } DLLEXPORT const FPDF_CharsetFontMap* STDCALL FPDF_GetDefaultTTFMap() { @@ -111,7 +108,9 @@ struct FPDF_SYSFONTINFO_DEFAULT : public FPDF_SYSFONTINFO { }; static void DefaultRelease(struct _FPDF_SYSFONTINFO* pThis) { - ((FPDF_SYSFONTINFO_DEFAULT*)pThis)->m_pFontInfo->Release(); + auto* pDefault = static_cast(pThis); + // TODO(thestig): Should this be set to nullptr too? + delete pDefault->m_pFontInfo; } static void DefaultEnumFonts(struct _FPDF_SYSFONTINFO* pThis, void* pMapper) { @@ -171,9 +170,10 @@ static void DefaultDeleteFont(struct _FPDF_SYSFONTINFO* pThis, void* hFont) { } DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() { - IFX_SystemFontInfo* pFontInfo = IFX_SystemFontInfo::CreateDefault(nullptr); + std::unique_ptr pFontInfo = + IFX_SystemFontInfo::CreateDefault(nullptr); if (!pFontInfo) - return NULL; + return nullptr; FPDF_SYSFONTINFO_DEFAULT* pFontInfoExt = FX_Alloc(FPDF_SYSFONTINFO_DEFAULT, 1); @@ -186,6 +186,6 @@ DLLEXPORT FPDF_SYSFONTINFO* STDCALL FPDF_GetDefaultSystemFontInfo() { pFontInfoExt->MapFont = DefaultMapFont; pFontInfoExt->Release = DefaultRelease; pFontInfoExt->version = 1; - pFontInfoExt->m_pFontInfo = pFontInfo; + pFontInfoExt->m_pFontInfo = pFontInfo.release(); return pFontInfoExt; } -- cgit v1.2.3