Skip to content

Commit

Permalink
remove TextSizeInHwnd2(), HwndMeasureText(WCHAR), TextOutUtf8(), GetT…
Browse files Browse the repository at this point in the history
…extExtentPoint32Utf8()
  • Loading branch information
kjk committed Oct 29, 2023
1 parent d63554d commit d3e33ad
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 113 deletions.
83 changes: 45 additions & 38 deletions src/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,19 @@ static Vec<StaticLinkInfo*> 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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -260,8 +271,11 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& 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 */
Expand All @@ -276,13 +290,11 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& 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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
77 changes: 7 additions & 70 deletions src/utils/WinUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 <txt> in a given <hwnd>, 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 <txt> in a given <hwnd>, taking into account its font
Size TextSizeInHwnd(HWND hwnd, const char* txt, HFONT font) {
if (!txt || !*txt) {
Expand All @@ -2929,8 +2862,9 @@ Size TextSizeInHwnd(HWND hwnd, const char* txt, HFONT font) {

/* Return size of a text <txt> in a given <hwnd>, 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 */
Expand All @@ -2951,6 +2885,9 @@ Size HwndMeasureText(HWND hwnd, const WCHAR* txt, HFONT font) {

/* Return size of a text <txt> in a given <hwnd>, 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);
}
Expand Down
4 changes: 0 additions & 4 deletions src/utils/WinUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/wingui/WinGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d3e33ad

Please sign in to comment.