Skip to content

Commit

Permalink
Fix font scaling bug on high DPI display. See rizonesoft/Notepad3#608
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Aug 20, 2018
1 parent 8e3d2dc commit 3b835c7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ BOOL IniSectionSetString(LPWSTR lpCachedIniSection, LPCWSTR lpName, LPCWSTR lpSt
return FALSE;
}

UINT GetCurrentPPI(HWND hwnd) {
HDC hDC = GetDC(hwnd);
UINT ppi = GetDeviceCaps(hDC, LOGPIXELSY);
ReleaseDC(hwnd, hDC);
ppi = max_u(ppi, USER_DEFAULT_SCREEN_DPI);
return ppi;
}

UINT GetCurrentDPI(HWND hwnd) {
UINT dpi = 0;
if (IsWin10AndAbove()) {
Expand All @@ -167,9 +175,8 @@ UINT GetCurrentDPI(HWND hwnd) {
}

if (dpi == 0) {
// FIXME: seems always get 96
HDC hDC = GetDC(hwnd);
dpi = GetDeviceCaps(hDC, LOGPIXELSX);
dpi = GetDeviceCaps(hDC, LOGPIXELSY);
ReleaseDC(hwnd, hDC);
}

Expand Down Expand Up @@ -488,6 +495,11 @@ BOOL SetWindowTitle(HWND hwnd, UINT uIDAppName, BOOL bIsElevated, UINT uIDUntitl
lstrcat(szTitle, pszSep);
lstrcat(szTitle, szAppName);

#if 0
wsprintf(szAppName, L"; dpi=%u, %u", g_uCurrentDPI, g_uCurrentPPI);
lstrcat(szTitle, szAppName);
#endif

return SetWindowText(hwnd, szTitle);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ extern HINSTANCE g_hInstance;
extern HANDLE g_hDefaultHeap;
extern UINT16 g_uWinVer;
extern UINT g_uCurrentDPI;
extern UINT g_uCurrentPPI;
extern WCHAR szIniFile[MAX_PATH];

// MSDN: Operating System Version
Expand Down Expand Up @@ -100,6 +101,7 @@ extern WCHAR szIniFile[MAX_PATH];
#endif

#define RoundToCurrentDPI(value) ((g_uCurrentDPI*(value) + USER_DEFAULT_SCREEN_DPI/2) / USER_DEFAULT_SCREEN_DPI)
#define ScaleFontSize(value) MulDiv(g_uCurrentDPI, (value), g_uCurrentPPI)

#define NP2HeapAlloc(size) HeapAlloc(g_hDefaultHeap, HEAP_ZERO_MEMORY, (size))
#define NP2HeapFree(hMem) HeapFree(g_hDefaultHeap, 0, hMem)
Expand Down Expand Up @@ -153,6 +155,7 @@ static inline void EndWaitCursor(void) {
SetCursorPos(pt.x, pt.y);
}

UINT GetCurrentPPI(HWND hwnd);
UINT GetCurrentDPI(HWND hwnd);
BOOL PrivateIsAppThemed(void);
HRESULT PrivateSetCurrentProcessExplicitAppUserModelID(PCWSTR AppID);
Expand Down
11 changes: 7 additions & 4 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ HANDLE g_hDefaultHeap;
HANDLE g_hScintilla;
UINT16 g_uWinVer;
UINT g_uCurrentDPI = USER_DEFAULT_SCREEN_DPI;
UINT g_uCurrentPPI = USER_DEFAULT_SCREEN_DPI;
WCHAR g_wchAppUserModelID[38] = L"";
WCHAR g_wchWorkingDirectory[MAX_PATH] = L"";

Expand Down Expand Up @@ -1405,12 +1406,12 @@ LRESULT CALLBACK MainWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
//
//
static inline void UpdateSelectionMarginWidth() {
int width = bShowSelectionMargin ? RoundToCurrentDPI(16) : 0;
int width = bShowSelectionMargin ? ScaleFontSize(16) : 0;
SendMessage(hwndEdit, SCI_SETMARGINWIDTHN, 1, width);
}

static inline void UpdateFoldMarginWidth() {
int width = bShowCodeFolding ? RoundToCurrentDPI(13) : 0;
int width = bShowCodeFolding ? ScaleFontSize(13) : 0;
SciCall_SetMarginWidth(MARGIN_FOLD_INDEX, width);
}

Expand Down Expand Up @@ -1486,6 +1487,7 @@ void SetWrapVisualFlags(void) {
LRESULT MsgCreate(HWND hwnd, WPARAM wParam, LPARAM lParam) {
HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
g_uCurrentDPI = GetCurrentDPI(hwnd);
g_uCurrentPPI = GetCurrentPPI(hwnd);

// Setup edit control
hwndEdit = EditCreate(hwnd);
Expand Down Expand Up @@ -1827,11 +1829,12 @@ void CreateBars(HWND hwnd, HINSTANCE hInstance) {
//
void MsgDPIChanged(HWND hwnd, WPARAM wParam, LPARAM lParam) {
g_uCurrentDPI = HIWORD(wParam);
g_uCurrentPPI = GetCurrentPPI(hwnd);
RECT* const rc = (RECT *)lParam;
Sci_Position pos = SciCall_GetCurrentPos();
#if 0
char buf[64];
sprintf(buf, "WM_DPICHANGED: dpi=%u\n", g_uCurrentDPI);
char buf[128];
sprintf(buf, "WM_DPICHANGED: dpi=%u, %u\n", g_uCurrentDPI, g_uCurrentPPI);
SendMessage(hwndEdit, SCI_INSERTTEXT, 0, (LPARAM)buf);
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/Styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -2578,7 +2578,7 @@ void Style_SetStyles(HWND hwnd, int iStyle, LPCWSTR lpszStyle) {

// Size
if (Style_StrGetSizeEx(lpszStyle, &iValue)) {
iValue = RoundToCurrentDPI(iValue);
iValue = ScaleFontSize(iValue);
SendMessage(hwnd, SCI_STYLESETSIZEFRACTIONAL, iStyle, (LPARAM)iValue);
}

Expand Down

0 comments on commit 3b835c7

Please sign in to comment.