From d3e33adf397ee3aac40f2b662c159c1c133500d2 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Sun, 29 Oct 2023 20:24:57 +0100 Subject: [PATCH] remove TextSizeInHwnd2(), HwndMeasureText(WCHAR), TextOutUtf8(), GetTextExtentPoint32Utf8() --- src/HomePage.cpp | 83 +++++++++++++++++++++++-------------------- src/utils/WinUtil.cpp | 77 ++++----------------------------------- src/utils/WinUtil.h | 4 --- src/wingui/WinGui.cpp | 2 +- 4 files changed, 53 insertions(+), 113 deletions(-) diff --git a/src/HomePage.cpp b/src/HomePage.cpp index 5ec373f3cc07..aa66d91e4ba5 100644 --- a/src/HomePage.cpp +++ b/src/HomePage.cpp @@ -112,17 +112,19 @@ static Vec gStaticLinks; #define COL4 RGB(69, 132, 190) #define COL5 RGB(112, 115, 207) -static void DrawAppName(HDC hdc, Point pt) { +static void DrawAppName(HDC hdc, Point pt, HFONT font) { const char* txt = kAppName; // colorful version COLORREF cols[] = {COL1, COL2, COL3, COL4, COL5, COL5, COL4, COL3, COL2, COL1}; + char buf[2] = {0}; + uint fmt = DT_LEFT | DT_NOCLIP; for (size_t i = 0; i < str::Len(txt); i++) { SetTextColor(hdc, cols[i % dimof(cols)]); - TextOutUtf8(hdc, pt.x, pt.y, txt + i, 1); - - SIZE txtSize; - GetTextExtentPoint32A(hdc, txt + i, 1, &txtSize); - pt.x += txtSize.cx; + Rect r{pt.x, pt.y, 1024, 1024}; + buf[0] = txt[i]; + HdcDrawText(hdc, buf, r, fmt, font); + Size txtSize = HdcMeasureText(hdc, buf, fmt, font); + pt.x += txtSize.dx; } } @@ -158,26 +160,26 @@ static Size CalcSumatraVersionSize(HWND hwnd, HDC hdc) { } static void DrawSumatraVersion(HWND hwnd, HDC hdc, Rect rect) { + uint fmt = DT_LEFT | DT_NOCLIP; HFONT fontSumatraTxt = CreateSimpleFont(hdc, kSumatraTxtFont, kSumatraTxtFontSize); HFONT fontVersionTxt = CreateSimpleFont(hdc, kVersionTxtFont, kVersionTxtFontSize); SetBkMode(hdc, TRANSPARENT); - ScopedSelectFont f(hdc, fontSumatraTxt); - SIZE txtSize; const char* txt = kAppName; - GetTextExtentPoint32Utf8(hdc, txt, (int)str::Len(txt), &txtSize); - Rect mainRect(rect.x + (rect.dx - txtSize.cx) / 2, rect.y + (rect.dy - txtSize.cy) / 2, txtSize.cx, txtSize.cy); - DrawAppName(hdc, mainRect.TL()); + Size txtSize = HdcMeasureText(hdc, txt, fmt, fontSumatraTxt); + Rect mainRect(rect.x + (rect.dx - txtSize.dx) / 2, rect.y + (rect.dy - txtSize.dy) / 2, txtSize.dx, txtSize.dy); + DrawAppName(hdc, mainRect.TL(), fontSumatraTxt); SetTextColor(hdc, gCurrentTheme->window.textColor); - ScopedSelectFont restoreFont(hdc, fontVersionTxt); - Point pt(mainRect.x + mainRect.dx + DpiScale(hwnd, kInnerPadding), mainRect.y); + int x = mainRect.x + mainRect.dx + DpiScale(hwnd, kInnerPadding); + int y = mainRect.y; char* ver = GetAppVersionTemp(); - TextOutUtf8(hdc, pt.x, pt.y, ver, (int)str::Len(ver)); - txt = VERSION_SUB_TXT; - TextOutUtf8(hdc, pt.x, pt.y + DpiScale(hwnd, 13), txt, (int)str::Len(txt)); + Rect r = {x, y, 1024, 1024}; + HdcDrawText(hdc, ver, r, fmt, fontVersionTxt); + r.y += DpiScale(hwnd, 13); + HdcDrawText(hdc, VERSION_SUB_TXT, r, fmt); } // draw on the bottom right @@ -208,6 +210,15 @@ static Rect DrawHideFrequentlyReadLink(HWND hwnd, HDC hdc, const char* txt) { return rect; } +static TempStr TrimGitTemp(char* s) { + if (gitCommidId && str::EndsWith(s, gitCommidId)) { + auto sLen = str::Len(s); + auto gitLen = str::Len(gitCommidId); + s = str::DupTemp(s, sLen - gitLen - 7); + } + return s; +} + /* Draws the about screen and remembers some state for hyperlinking. It transcribes the design I did in graphics software - hopeless to understand without seeing the design. */ @@ -260,8 +271,11 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec& stati /* render text on the left*/ SelectObject(hdc, fontLeftTxt); + uint fmt = DT_LEFT | DT_NOCLIP; for (AboutLayoutInfoEl* el = gAboutLayoutInfo; el->leftTxt; el++) { - TextOutUtf8(hdc, el->leftPos.x, el->leftPos.y, el->leftTxt, (int)str::Len(el->leftTxt)); + auto& pos = el->leftPos; + Rect r{pos.x, pos.y, 1024, 1024}; + HdcDrawText(hdc, el->leftTxt, r, fmt); } /* render text on the right */ @@ -276,13 +290,11 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec& stati col = gCurrentTheme->window.textColor; } SetTextColor(hdc, col); - size_t txtLen = str::Len(el->rightTxt); - if (gitCommidId) { - if (str::EndsWith(el->rightTxt, gitCommidId)) { - txtLen -= str::Len(gitCommidId) - 7; - } - } - TextOutUtf8(hdc, el->rightPos.x, el->rightPos.y, el->rightTxt, (int)txtLen); + char* s = (char*)el->rightTxt; + s = TrimGitTemp(s); + auto& pos = el->rightPos; + Rect r{pos.x, pos.y, 1024, 1024}; + HdcDrawText(hdc, s, r, fmt); if (hasUrl) { int underlineY = el->rightPos.y + el->rightPos.dy - 3; @@ -311,11 +323,11 @@ static void UpdateAboutLayoutInfo(HWND hwnd, HDC hdc, Rect* rect) { SelectObject(hdc, fontLeftTxt); int leftLargestDx = 0; int leftDy = 0; + uint fmt = DT_LEFT; for (AboutLayoutInfoEl* el = gAboutLayoutInfo; el->leftTxt; el++) { - SIZE txtSize; - GetTextExtentPoint32Utf8(hdc, el->leftTxt, (int)str::Len(el->leftTxt), &txtSize); - el->leftPos.dx = txtSize.cx; - el->leftPos.dy = txtSize.cy; + Size txtSize = HdcMeasureText(hdc, el->leftTxt, fmt); + el->leftPos.dx = txtSize.dx; + el->leftPos.dy = txtSize.dy; if (el == &gAboutLayoutInfo[0]) { leftDy = el->leftPos.dy; @@ -332,16 +344,11 @@ static void UpdateAboutLayoutInfo(HWND hwnd, HDC hdc, Rect* rect) { int rightLargestDx = 0; int rightDy = 0; for (AboutLayoutInfoEl* el = gAboutLayoutInfo; el->leftTxt; el++) { - SIZE txtSize; - size_t txtLen = str::Len(el->rightTxt); - if (gitCommidId) { - if (str::EndsWith(el->rightTxt, gitCommidId)) { - txtLen -= str::Len(gitCommidId) - 7; - } - } - GetTextExtentPoint32Utf8(hdc, el->rightTxt, (int)txtLen, &txtSize); - el->rightPos.dx = txtSize.cx; - el->rightPos.dy = txtSize.cy; + char* s = (char*)el->rightTxt; + s = TrimGitTemp(s); + Size txtSize = HdcMeasureText(hdc, s, fmt); + el->rightPos.dx = txtSize.dx; + el->rightPos.dy = txtSize.dy; if (el == &gAboutLayoutInfo[0]) { rightDy = el->rightPos.dy; diff --git a/src/utils/WinUtil.cpp b/src/utils/WinUtil.cpp index 50d247a2b834..96180eb2bc16 100755 --- a/src/utils/WinUtil.cpp +++ b/src/utils/WinUtil.cpp @@ -2769,54 +2769,6 @@ bool DestroyIconSafe(HICON* h) { return ToBool(res); } -bool TextOutUtf8(HDC hdc, int x, int y, const char* s, int sLen) { - if (!s) { - return false; - } - if (sLen <= 0) { - sLen = (int)str::Len(s); - } - WCHAR* ws = ToWStrTemp(s, (size_t)sLen); - if (!ws) { - return false; - } - sLen = (int)str::Len(ws); // TODO: can this be different after converting to WCHAR? - return TextOutW(hdc, x, y, ws, (int)sLen); -} - -bool GetTextExtentPoint32Utf8(HDC hdc, const char* s, int sLen, LPSIZE psizl) { - *psizl = SIZE{}; - if (!s) { - return true; - } - if (sLen <= 0) { - sLen = (int)str::Len(s); - } - WCHAR* ws = ToWStrTemp(s, sLen); - if (!ws) { - return false; - } - sLen = (int)str::Len(ws); // TODO: can this be different after converting to WCHAR? - return GetTextExtentPoint32W(hdc, ws, sLen, psizl); -} - -/* -format: -#define DT_TOP 0x00000000 -#define DT_LEFT 0x00000000 -#define DT_CENTER 0x00000001 -#define DT_RIGHT 0x00000002 -#define DT_VCENTER 0x00000004 -#define DT_BOTTOM 0x00000008 -#define DT_WORDBREAK 0x00000010 -#define DT_SINGLELINE 0x00000020 -#define DT_EXPANDTABS 0x00000040 -#define DT_TABSTOP 0x00000080 -#define DT_NOCLIP 0x00000100 -#define DT_EXTERNALLEADING 0x00000200 -#define DT_CALCRECT 0x00000400 -#define DT_NOPREFIX 0x00000800 -*/ int HdcDrawText(HDC hdc, const char* s, RECT* r, uint format, HFONT font) { if (!s) { return 0; @@ -2886,7 +2838,7 @@ Size TextSizeInHwnd(HWND hwnd, const WCHAR* txt, HFONT font) { return Size{}; } size_t txtLen = str::Len(txt); - HDC dc = GetWindowDC(hwnd); + AutoReleaseDC dc(hwnd); /* GetWindowDC() returns dc with default state, so we have to first set window's current font into dc */ if (font == nullptr) { @@ -2896,28 +2848,9 @@ Size TextSizeInHwnd(HWND hwnd, const WCHAR* txt, HFONT font) { SIZE sz{}; GetTextExtentPoint32W(dc, txt, (int)txtLen, &sz); SelectObject(dc, prev); - ReleaseDC(hwnd, dc); return Size(sz.cx, sz.cy); } -// TODO: unify with TextSizeInHwnd -/* Return size of a text in a given , taking into account its font */ -SIZE TextSizeInHwnd2(HWND hwnd, const WCHAR* txt, HFONT font) { - SIZE sz{}; - size_t txtLen = str::Len(txt); - HDC dc = GetWindowDC(hwnd); - /* GetWindowDC() returns dc with default state, so we have to first set - window's current font into dc */ - if (font == nullptr) { - font = (HFONT)SendMessageW(hwnd, WM_GETFONT, 0, 0); - } - HGDIOBJ prev = SelectObject(dc, font); - GetTextExtentPoint32W(dc, txt, (int)txtLen, &sz); - SelectObject(dc, prev); - ReleaseDC(hwnd, dc); - return sz; -} - // Return size of a text in a given , taking into account its font Size TextSizeInHwnd(HWND hwnd, const char* txt, HFONT font) { if (!txt || !*txt) { @@ -2929,8 +2862,9 @@ Size TextSizeInHwnd(HWND hwnd, const char* txt, HFONT font) { /* Return size of a text in a given , taking into account its font */ Size HwndMeasureText(HWND hwnd, const WCHAR* txt, HFONT font) { - SIZE sz{}; - + if (!txt || !*txt) { + return Size{}; + } AutoReleaseDC dc(hwnd); /* GetWindowDC() returns dc with default state, so we have to first set window's current font into dc */ @@ -2951,6 +2885,9 @@ Size HwndMeasureText(HWND hwnd, const WCHAR* txt, HFONT font) { /* Return size of a text in a given , taking into account its font */ Size HwndMeasureText(HWND hwnd, const char* txt, HFONT font) { + if (!txt || !*txt) { + return Size{}; + } TempWStr sw = ToWStrTemp(txt); return HwndMeasureText(hwnd, sw, font); } diff --git a/src/utils/WinUtil.h b/src/utils/WinUtil.h index 68af0aa096b5..c51a8bf460f4 100644 --- a/src/utils/WinUtil.h +++ b/src/utils/WinUtil.h @@ -114,11 +114,7 @@ void DrawCenteredText(HDC hdc, Rect r, const char* txt, bool isRTL = false); void DrawCenteredText(HDC, const RECT& r, const WCHAR* txt, bool isRTL = false); Size TextSizeInHwnd(HWND, const WCHAR*, HFONT = nullptr); Size TextSizeInHwnd(HWND, const char*, HFONT = nullptr); -SIZE TextSizeInHwnd2(HWND, const WCHAR*, HFONT); -Size HwndMeasureText(HWND hwnd, const WCHAR* txt, HFONT font); Size HwndMeasureText(HWND hwnd, const char* txt, HFONT font); -bool TextOutUtf8(HDC hdc, int x, int y, const char* s, int sLen = 0); -bool GetTextExtentPoint32Utf8(HDC hdc, const char* s, int sLen, LPSIZE psizl); int HdcDrawText(HDC hdc, const char* s, RECT* r, uint format, HFONT font = nullptr); int HdcDrawText(HDC hdc, const char* s, const Rect& r, uint format, HFONT font = nullptr); diff --git a/src/wingui/WinGui.cpp b/src/wingui/WinGui.cpp index 95c2877c38c8..5a7e4ed32ead 100644 --- a/src/wingui/WinGui.cpp +++ b/src/wingui/WinGui.cpp @@ -1857,7 +1857,7 @@ int ListBox::GetItemHeight(int idx) { // if failed for some reason, fallback to measuring text in default font // HFONT f = GetFont(); HFONT f = GetDefaultGuiFont(); - Size sz = HwndMeasureText(hwnd, L"A", f); + Size sz = HwndMeasureText(hwnd, "A", f); res = sz.dy; } return res;