]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0026: win32: Windows default font lacking v9.1.0026
authorKen Takata <kentkt@csc.jp>
Fri, 12 Jan 2024 17:09:43 +0000 (18:09 +0100)
committerChristian Brabandt <cb@256bit.org>
Fri, 12 Jan 2024 17:12:04 +0000 (18:12 +0100)
Problem:  win32: Windows default font lacking (@clach04)
Solution: Improve default font (Ken Takata)

win32: Improve default font

Currently, Fixedsys is the default font on Windows.
It is not suitable for recent High DPI environments.

* Change the default font to Consolas.
* Allow to change the default font by the translation message. E.g.:
  ```
  msgid "DefaultFontNameForWindows"
  msgstr "Courier New"
  ```

fixes: #12919
closes: #13266

Signed-off-by: Ken Takata <kentkt@csc.jp>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/gui_w32.c
src/os_mswin.c
src/proto/os_mswin.pro
src/testdir/test_mswin_event.vim
src/version.c

index eb4c841388e9a3a8c7b1464fea35328e5ed77940..0935ecfd12233e74e652c964868f0e27cac21da7 100644 (file)
@@ -392,16 +392,6 @@ static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
 static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
 static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
 
-    static UINT WINAPI
-stubGetDpiForSystem(void)
-{
-    HWND hwnd = GetDesktopWindow();
-    HDC hdc = GetWindowDC(hwnd);
-    UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
-    ReleaseDC(hwnd, hdc);
-    return dpi;
-}
-
     static int WINAPI
 stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
 {
@@ -5286,7 +5276,7 @@ load_dpi_func(void)
 
 fail:
     // Disable PerMonitorV2 APIs.
-    pGetDpiForSystem = stubGetDpiForSystem;
+    pGetDpiForSystem = vimGetDpiForSystem;
     pGetDpiForWindow = NULL;
     pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
     pSetThreadDpiAwarenessContext = NULL;
index 21b7db31f3daebba5963201186592721dd37d23e..512fa408960dc7471854e90c1debcbd469acbadc 100644 (file)
@@ -2747,19 +2747,22 @@ quality_id2name(DWORD id)
     return qp->name;
 }
 
+// The default font height in 100% scaling (96dpi).
+// (-12 in 96dpi equates to roughly 9pt)
+#define DEFAULT_FONT_HEIGHT    (-12)
+
 static const LOGFONTW s_lfDefault =
 {
-    -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
+    DEFAULT_FONT_HEIGHT,
+    0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
     OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
     PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
-    L"Fixedsys"        // see _ReadVimIni
+    L""        // Default font name will be set later based on current language.
 };
 
-// Initialise the "current height" to -12 (same as s_lfDefault) just
-// in case the user specifies a font in "guifont" with no size before a font
-// with an explicit size has been set. This defaults the size to this value
-// (-12 equates to roughly 9pt).
-int current_font_height = -12;         // also used in gui_w32.c
+// This will be initialized when set_default_logfont() is called first time.
+// The value will be based on the system DPI.
+int current_font_height = 0;           // also used in gui_w32.c
 
 /*
  * Convert a string representing a point size into pixels. The string should
@@ -3026,6 +3029,47 @@ utf16ascncmp(const WCHAR *w, const char *p, size_t n)
     return 0;
 }
 
+/*
+ * Equivalent of GetDpiForSystem().
+ */
+    UINT WINAPI
+vimGetDpiForSystem(void)
+{
+    HWND hwnd = GetDesktopWindow();
+    HDC hdc = GetWindowDC(hwnd);
+    UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
+    ReleaseDC(hwnd, hdc);
+    return dpi;
+}
+
+/*
+ * Set default logfont based on current language.
+ */
+    static void
+set_default_logfont(LOGFONTW *lf)
+{
+    // Default font name for current language on MS-Windows.
+    // If not translated, falls back to "Consolas".
+    // This must be a fixed-pitch font.
+    const char *defaultfontname = N_("DefaultFontNameForWindows");
+    char *fontname = _(defaultfontname);
+
+    if (strcmp(fontname, defaultfontname) == 0)
+       fontname = "Consolas";
+
+    *lf = s_lfDefault;
+    lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
+    if (current_font_height == 0)
+       current_font_height = lf->lfHeight;
+
+    WCHAR *wfontname = enc_to_utf16((char_u*)fontname, NULL);
+    if (wfontname != NULL)
+    {
+       wcscpy_s(lf->lfFaceName, LF_FACESIZE, wfontname);
+       vim_free(wfontname);
+    }
+}
+
 /*
  * Get font info from "name" into logfont "lf".
  * Return OK for a valid name, FAIL otherwise.
@@ -3043,7 +3087,7 @@ get_logfont(
     static LOGFONTW *lastlf = NULL;
     WCHAR      *wname;
 
-    *lf = s_lfDefault;
+    set_default_logfont(lf);
     if (name == NULL)
        return OK;
 
@@ -3083,7 +3127,7 @@ get_logfont(
        lf->lfFaceName[p - wname] = NUL;
 
     // First set defaults
-    lf->lfHeight = -12;
+    lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
     lf->lfWidth = 0;
     lf->lfWeight = FW_NORMAL;
     lf->lfItalic = FALSE;
index 17a9ba25ee5e00d45de0e712ff46cd2321375647..47310104b8ec6ca3acdccaff9e57c558dd6e2230 100644 (file)
@@ -51,6 +51,7 @@ void serverProcessPendingMessages(void);
 char *charset_id2name(int id);
 char *quality_id2name(DWORD id);
 void gui_mch_expand_font(optexpand_T *args, void *param, int (*add_match)(char_u *val));
+UINT WINAPI vimGetDpiForSystem(void);
 int get_logfont(LOGFONTW *lf, char_u *name, HDC printer_dc, int verbose);
 void channel_init_winsock(void);
 /* vim: set ft=c : */
index 4de016953e104ab9fed2fce042c0930ecbb87e21..b6609937c04741465fde485834d77c8d9503fea1 100644 (file)
@@ -727,14 +727,14 @@ func Test_mswin_event_mouse()
   if has('gui_running')
     let args = { }
     let args.row = 9
-    let args.col = 7
+    let args.col = 5
     let args.move = 1
     let args.cell = 1
     call test_mswin_event("mouse", args)
     call feedkeys("\<Esc>", 'Lx!')
     let pos = getmousepos()
     call assert_equal(9, pos.screenrow)
-    call assert_equal(7, pos.screencol)
+    call assert_equal(5, pos.screencol)
 
     let args.cell = 0
     call test_mswin_event("mouse", args)
index c8f0bb9c3833f26c4ffb819f0456f08f06b4141c..6216b2f033bb4b616a6bdd7cb158674891332e2b 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    26,
 /**/
     25,
 /**/