Skip to content

Commit

Permalink
add HdcMeasureText() variant
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Oct 29, 2023
1 parent d3e33ad commit 8ef0a33
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,23 @@ static TempStr GetAppVersionTemp() {
return s;
}

static Size CalcSumatraVersionSize(HWND hwnd, HDC hdc) {
static Size CalcSumatraVersionSize(HDC hdc) {
Size result{};

HFONT fontSumatraTxt = CreateSimpleFont(hdc, kSumatraTxtFont, kSumatraTxtFontSize);
HFONT fontVersionTxt = CreateSimpleFont(hdc, kVersionTxtFont, kVersionTxtFontSize);

/* calculate minimal top box size */
Size txtSize = HwndMeasureText(hwnd, kAppName, fontSumatraTxt);
result.dy = txtSize.dy + DpiScale(hwnd, kAboutBoxMarginDy * 2);
Size txtSize = HdcMeasureText(hdc, kAppName, fontSumatraTxt);
result.dy = txtSize.dy + DpiScale(hdc, kAboutBoxMarginDy * 2);
result.dx = txtSize.dx;

/* consider version and version-sub strings */
TempStr ver = GetAppVersionTemp();
txtSize = HwndMeasureText(hwnd, ver, fontVersionTxt);
int minWidth = txtSize.dx + DpiScale(hwnd, 8);
txtSize = HdcMeasureText(hdc, ver, fontVersionTxt);
int minWidth = txtSize.dx + DpiScale(hdc, 8);
int dx = std::max(txtSize.dx, minWidth);
result.dx += 2 * (dx + DpiScale(hwnd, kInnerPadding));
result.dx += 2 * (dx + DpiScale(hdc, kInnerPadding));
return result;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ static void DrawAbout(HWND hwnd, HDC hdc, Rect rect, Vec<StaticLinkInfo*>& stati
FillRect(hdc, &rTmp, brushAboutBg);

/* render title */
Rect titleRect(rect.TL(), CalcSumatraVersionSize(hwnd, hdc));
Rect titleRect(rect.TL(), CalcSumatraVersionSize(hdc));

AutoDeleteBrush bgBrush = CreateSolidBrush(col);
ScopedSelectObject brush(hdc, bgBrush);
Expand Down Expand Up @@ -317,7 +317,7 @@ static void UpdateAboutLayoutInfo(HWND hwnd, HDC hdc, Rect* rect) {
HGDIOBJ origFont = SelectObject(hdc, fontLeftTxt);

/* calculate minimal top box size */
Size headerSize = CalcSumatraVersionSize(hwnd, hdc);
Size headerSize = CalcSumatraVersionSize(hdc);

/* calculate left text dimensions */
SelectObject(hdc, fontLeftTxt);
Expand Down Expand Up @@ -637,7 +637,7 @@ void DrawHomePage(MainWindow* win, HDC hdc, FileHistory& fileHistory, COLORREF t
bool isRtl = IsUIRightToLeft();

/* render title */
Rect titleBox = Rect(Point(0, 0), CalcSumatraVersionSize(win->hwndCanvas, hdc));
Rect titleBox = Rect(Point(0, 0), CalcSumatraVersionSize(hdc));
titleBox.x = rc.dx - titleBox.dx - 3;
DrawSumatraVersion(win->hwndCanvas, hdc, titleBox);
DrawLine(hdc, Rect(0, titleBox.dy, rc.dx, 0));
Expand Down
6 changes: 6 additions & 0 deletions src/utils/Dpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ void DpiScale(HWND hwnd, int& x1, int& x2) {
x1 = nx1;
x2 = nx2;
}

int DpiScale(HDC hdc, int x) {
int dpi = GetDeviceCaps(hdc, LOGPIXELSX);
int res = MulDiv(x, dpi, 96);
return res;
}
2 changes: 2 additions & 0 deletions src/utils/Dpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ int DpiGetForHwnd(HWND);
int DpiGet(HWND);
int DpiScale(HWND, int);
void DpiScale(HWND, int&, int&);

int DpiScale(HDC, int x);
22 changes: 15 additions & 7 deletions src/utils/WinUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2769,7 +2769,7 @@ bool DestroyIconSafe(HICON* h) {
return ToBool(res);
}

int HdcDrawText(HDC hdc, const char* s, RECT* r, uint format, HFONT font) {
int HdcDrawText(HDC hdc, const char* s, RECT* r, uint fmt, HFONT font) {
if (!s) {
return 0;
}
Expand All @@ -2779,17 +2779,17 @@ int HdcDrawText(HDC hdc, const char* s, RECT* r, uint format, HFONT font) {
}
int cch = (int)str::Len(ws);
ScopedSelectFont f(hdc, font);
return DrawTextW(hdc, ws, cch, r, format);
return DrawTextW(hdc, ws, cch, r, fmt);
}

int HdcDrawText(HDC hdc, const char* s, const Rect& r, uint format, HFONT font) {
int HdcDrawText(HDC hdc, const char* s, const Rect& r, uint fmt, HFONT font) {
RECT r2 = ToRECT(r);
return HdcDrawText(hdc, s, &r2, format, font);
return HdcDrawText(hdc, s, &r2, fmt, font);
}

// uses the same logic as HdcDrawText
Size HdcMeasureText(HDC hdc, const char* s, uint format, HFONT font) {
format |= DT_CALCRECT;
Size HdcMeasureText(HDC hdc, const char* s, uint fmt, HFONT font) {
fmt |= DT_CALCRECT;
WCHAR* ws = ToWStrTemp(s);
if (!ws) {
return {};
Expand All @@ -2800,7 +2800,7 @@ Size HdcMeasureText(HDC hdc, const char* s, uint format, HFONT font) {
// pick a very large area
// TODO: allow limiting by dx
RECT rc{0, 0, 4096, 4096};
int dy = DrawTextW(hdc, ws, sLen, &rc, format);
int dy = DrawTextW(hdc, ws, sLen, &rc, fmt);
if (0 == dy) {
return {};
}
Expand All @@ -2812,6 +2812,14 @@ Size HdcMeasureText(HDC hdc, const char* s, uint format, HFONT font) {
return Size(dx, dy);
}

Size HdcMeasureText(HDC hdc, const char* s, HFONT font) {
// DT_LEFT - left-aligned
// DT_NOCLIP - is faster, no clipping
// DT_NOPREFIX - doesn't process & to underline next char
uint fmt = DT_LEFT | DT_NOCLIP | DT_NOPREFIX;
return HdcMeasureText(hdc, s, fmt, font);
}

void DrawCenteredText(HDC hdc, const Rect r, const WCHAR* txt, bool isRTL) {
SetBkMode(hdc, TRANSPARENT);
RECT tmpRect = ToRECT(r);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/WinUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Size HwndMeasureText(HWND hwnd, const char* txt, HFONT font);
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);
Size HdcMeasureText(HDC hdc, const char* s, uint format, HFONT font = nullptr);

Size HdcMeasureText(HDC hdc, const char* s, HFONT font = nullptr);

bool IsFocused(HWND);
bool IsCursorOverWindow(HWND);
Expand Down

0 comments on commit 8ef0a33

Please sign in to comment.