Skip to content

Commit da5da65

Browse files
k-takatachrisbra
authored andcommitted
patch 9.0.1987: win32: font-size calculation can be improved
Problem: win32: font-size calculation can be improved Solution: calculate font size before the window size Support calculating the new size even if a bitmap font is used. Calculate the new font size before actually change the Window size. closes: #13280 related: #11812, #13252 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: Ken Takata <[email protected]>
1 parent c661e11 commit da5da65

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

src/gui_w32.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ GetAverageFontSize(HDC hdc, SIZE *size)
15881588
* Get the character size of a font.
15891589
*/
15901590
static void
1591-
GetFontSize(GuiFont font)
1591+
GetFontSize(GuiFont font, int *char_width, int *char_height)
15921592
{
15931593
HWND hwnd = GetDesktopWindow();
15941594
HDC hdc = GetWindowDC(hwnd);
@@ -1599,21 +1599,32 @@ GetFontSize(GuiFont font)
15991599
GetTextMetrics(hdc, &tm);
16001600
GetAverageFontSize(hdc, &size);
16011601

1602-
gui.char_width = size.cx + tm.tmOverhang;
1603-
gui.char_height = tm.tmHeight + p_linespace;
1602+
if (char_width)
1603+
*char_width = size.cx + tm.tmOverhang;
1604+
if (char_height)
1605+
*char_height = tm.tmHeight + p_linespace;
16041606

16051607
SelectFont(hdc, hfntOld);
16061608

16071609
ReleaseDC(hwnd, hdc);
16081610
}
16091611

1612+
/*
1613+
* Update the character size in "gui" structure with the specified font.
1614+
*/
1615+
static void
1616+
UpdateFontSize(GuiFont font)
1617+
{
1618+
GetFontSize(font, &gui.char_width, &gui.char_height);
1619+
}
1620+
16101621
/*
16111622
* Adjust gui.char_height (after 'linespace' was changed).
16121623
*/
16131624
int
16141625
gui_mch_adjust_charheight(void)
16151626
{
1616-
GetFontSize(gui.norm_font);
1627+
UpdateFontSize(gui.norm_font);
16171628
return OK;
16181629
}
16191630

@@ -3517,7 +3528,7 @@ gui_mch_init_font(char_u *font_name, int fontset UNUSED)
35173528
gui_mch_free_font(gui.norm_font);
35183529
gui.norm_font = font;
35193530
current_font_height = lfOrig.lfHeight;
3520-
GetFontSize(font);
3531+
UpdateFontSize(font);
35213532

35223533
p = logfont2name(lfOrig);
35233534
if (p != NULL)
@@ -4742,16 +4753,33 @@ _OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
47424753
static BOOL
47434754
_OnGetDpiScaledSize(HWND hwnd, UINT dpi, SIZE *size)
47444755
{
4756+
int old_width, old_height;
4757+
int new_width, new_height;
4758+
LOGFONTW lf;
4759+
HFONT font;
4760+
47454761
//TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
47464762

47474763
// Calculate new approximate size.
4748-
// FIXME: If a bitmap font (e.g. FixedSys) is used, the font size may not
4749-
// be changed. In that case, the calculated size can be wrong.
4750-
size->cx = size->cx * dpi / s_dpi;
4751-
size->cy = size->cy * dpi / s_dpi;
4764+
GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
4765+
GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
4766+
lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
4767+
font = CreateFontIndirectW(&lf);
4768+
if (font)
4769+
{
4770+
GetFontSize((GuiFont)font, &new_width, &new_height); // New size
4771+
DeleteFont(font);
4772+
}
4773+
else
4774+
{
4775+
new_width = old_width;
4776+
new_height = old_height;
4777+
}
4778+
size->cx = size->cx * new_width / old_width;
4779+
size->cy = size->cy * new_height / old_height;
47524780
//TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
47534781

4754-
return FALSE;
4782+
return TRUE;
47554783
}
47564784

47574785
static LRESULT

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1987,
707709
/**/
708710
1986,
709711
/**/

0 commit comments

Comments
 (0)