From bc1b2b03718b596506db2acedd6dad10f489d9fe Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Sat, 15 Jan 2022 11:30:42 +0300 Subject: [PATCH] Simplify error COM handling using exceptions Change-Id: I2514e39bba8debcbdbb60929d2ca2f1ba3aaa2e5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128456 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- vcl/skia/win/gdiimpl.cxx | 99 +++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 60 deletions(-) diff --git a/vcl/skia/win/gdiimpl.cxx b/vcl/skia/win/gdiimpl.cxx index e9074340e66e..f2f0d82a9047 100644 --- a/vcl/skia/win/gdiimpl.cxx +++ b/vcl/skia/win/gdiimpl.cxx @@ -16,7 +16,9 @@ #include #include #include +#include #include +#include #include #include @@ -119,39 +121,16 @@ bool WinSkiaSalGraphicsImpl::RenderAndCacheNativeControl(CompatibleDC& rWhite, C return true; } -#ifdef SAL_LOG_INFO -static HRESULT checkResult(HRESULT hr, const char* file, size_t line) -{ - if (FAILED(hr)) - { - OUString sLocationString - = OUString::createFromAscii(file) + ":" + OUString::number(line) + " "; - SAL_DETAIL_LOG_STREAM(SAL_DETAIL_ENABLE_LOG_INFO, ::SAL_DETAIL_LOG_LEVEL_INFO, "vcl.skia", - sLocationString.toUtf8().getStr(), - "HRESULT failed with: 0x" << OUString::number(hr, 16) << ": " - << WindowsErrorStringFromHRESULT(hr)); - } - return hr; -} - -#define CHECKHR(funct) checkResult(funct, __FILE__, __LINE__) -#else -#define CHECKHR(funct) (funct) -#endif - -sk_sp WinSkiaSalGraphicsImpl::createDirectWriteTypeface(HDC hdc, HFONT hfont) +sk_sp WinSkiaSalGraphicsImpl::createDirectWriteTypeface(HDC hdc, HFONT hfont) try { + using sal::systools::ThrowIfFailed; if (!dwriteDone) { - if (SUCCEEDED( - CHECKHR(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(&dwriteFactory))))) - { - if (SUCCEEDED(CHECKHR(dwriteFactory->GetGdiInterop(&dwriteGdiInterop)))) - dwriteFontMgr = SkFontMgr_New_DirectWrite(dwriteFactory.get()); - else - dwriteFactory.clear(); - } + ThrowIfFailed(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(&dwriteFactory)), + SAL_WHERE); + ThrowIfFailed(dwriteFactory->GetGdiInterop(&dwriteGdiInterop), SAL_WHERE); + dwriteFontMgr = SkFontMgr_New_DirectWrite(dwriteFactory.get()); dwriteDone = true; } if (!dwriteFontMgr) @@ -164,20 +143,17 @@ sk_sp WinSkiaSalGraphicsImpl::createDirectWriteTypeface(HDC hdc, HFO // For that CreateFromFaceFromHdc() is necessary. The simpler // CreateFontFromLOGFONT() seems to search for the best matching font, // which may not be the exact font. - HFONT oldFont = SelectFont(hdc, hfont); sal::systools::COMReference fontFace; - if (FAILED(CHECKHR(dwriteGdiInterop->CreateFontFaceFromHdc(hdc, &fontFace)))) { - SelectFont(hdc, oldFont); - return nullptr; + comphelper::ScopeGuard g( + [ hdc, oldFont(SelectFont(hdc, hfont)) ] { SelectFont(hdc, oldFont); }); + ThrowIfFailed(dwriteGdiInterop->CreateFontFaceFromHdc(hdc, &fontFace), SAL_WHERE); } - SelectFont(hdc, oldFont); sal::systools::COMReference collection; - if (FAILED(CHECKHR(dwriteFactory->GetSystemFontCollection(&collection)))) - return nullptr; + ThrowIfFailed(dwriteFactory->GetSystemFontCollection(&collection), SAL_WHERE); sal::systools::COMReference font; - // Do not use CHECKHR() here, as said above, this fails for our fonts. + // As said above, this fails for our fonts. if (FAILED(collection->GetFontFromFontFace(fontFace.get(), &font))) { // If not found in system collection, try our private font collection. @@ -191,26 +167,26 @@ sk_sp WinSkiaSalGraphicsImpl::createDirectWriteTypeface(HDC hdc, HFO // private fonts with this newly used font. sal::systools::COMReference dwriteFactory3; - if (FAILED(dwriteFactory->QueryInterface(&dwriteFactory3))) - return nullptr; + ThrowIfFailed(dwriteFactory->QueryInterface(&dwriteFactory3), SAL_WHERE); - if (!dwriteFontSetBuilder - && FAILED(dwriteFactory3->CreateFontSetBuilder(&dwriteFontSetBuilder))) - return nullptr; + if (!dwriteFontSetBuilder) + ThrowIfFailed(dwriteFactory3->CreateFontSetBuilder(&dwriteFontSetBuilder), + SAL_WHERE); UINT32 numberOfFiles; - if (FAILED(fontFace->GetFiles(&numberOfFiles, nullptr)) || numberOfFiles != 1) + ThrowIfFailed(fontFace->GetFiles(&numberOfFiles, nullptr), SAL_WHERE); + if (numberOfFiles != 1) return nullptr; sal::systools::COMReference fontFile; - if (FAILED(fontFace->GetFiles(&numberOfFiles, &fontFile))) - return nullptr; + ThrowIfFailed(fontFace->GetFiles(&numberOfFiles, &fontFile), SAL_WHERE); BOOL isSupported; DWRITE_FONT_FILE_TYPE fileType; UINT32 numberOfFonts; - if (FAILED(fontFile->Analyze(&isSupported, &fileType, nullptr, &numberOfFonts)) - || !isSupported) + ThrowIfFailed(fontFile->Analyze(&isSupported, &fileType, nullptr, &numberOfFonts), + SAL_WHERE); + if (!isSupported) return nullptr; // For each font within the font file, get a font face reference and add to the builder. @@ -226,25 +202,28 @@ sk_sp WinSkiaSalGraphicsImpl::createDirectWriteTypeface(HDC hdc, HFO dwriteFontSetBuilder->AddFontFaceReference(fontFaceReference.get()); } - dwritePrivateCollection.clear(); sal::systools::COMReference fontSet; - if (SUCCEEDED(CHECKHR(dwriteFontSetBuilder->CreateFontSet(&fontSet)))) - dwriteFactory3->CreateFontCollectionFromFontSet(fontSet.get(), - &dwritePrivateCollection); + ThrowIfFailed(dwriteFontSetBuilder->CreateFontSet(&fontSet), SAL_WHERE); + ThrowIfFailed(dwriteFactory3->CreateFontCollectionFromFontSet(fontSet.get(), + &dwritePrivateCollection), + SAL_WHERE); + ThrowIfFailed(dwritePrivateCollection->GetFontFromFontFace(fontFace.get(), &font), + SAL_WHERE); } - - if (!dwritePrivateCollection) - return nullptr; - // CHECKHR because we expect to succeed here - if (FAILED(CHECKHR(dwritePrivateCollection->GetFontFromFontFace(fontFace.get(), &font)))) - return nullptr; } sal::systools::COMReference fontFamily; - if (FAILED(CHECKHR(font->GetFontFamily(&fontFamily)))) - return nullptr; + ThrowIfFailed(font->GetFontFamily(&fontFamily), SAL_WHERE); return sk_sp( SkCreateTypefaceDirectWrite(dwriteFontMgr, fontFace.get(), font.get(), fontFamily.get())); } +catch (const sal::systools::ComError& e) +{ + SAL_DETAIL_LOG_STREAM(SAL_DETAIL_ENABLE_LOG_INFO, ::SAL_DETAIL_LOG_LEVEL_INFO, "vcl.skia", + e.what(), + "HRESULT 0x" << OUString::number(e.GetHresult(), 16) << ": " + << WindowsErrorStringFromHRESULT(e.GetHresult())); + return nullptr; +} bool WinSkiaSalGraphicsImpl::DrawTextLayout(const GenericSalLayout& rLayout) { -- 2.11.4.GIT