patch 9.1.0854: cannot get terminal cell size

Problem:  cannot get terminal cell size
Solution: add getcellpixels() function to return xpixel * ypixel
          cell size on terminal Unix (mikoto2000)

closes: #16004

Signed-off-by: mikoto2000 <mikoto2000@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 2d88668..04967ef 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -2077,6 +2077,14 @@
 			ret_string,	    f_getbufoneline},
     {"getbufvar",	2, 3, FEARG_1,	    arg3_buffer_string_any,
 			ret_any,	    f_getbufvar},
+    {"getcellpixels",	0, 0, 0,	    NULL,
+			ret_list_any,
+#if (defined(UNIX) || defined(VMS)) && (defined(FEAT_EVAL) || defined(PROTO))
+	    f_getcellpixels
+#else
+	    NULL
+#endif
+			},
     {"getcellwidths",	0, 0, 0,	    NULL,
 			ret_list_any,	    f_getcellwidths},
     {"getchangelist",	0, 1, FEARG_1,	    arg1_buffer,
diff --git a/src/os_unix.c b/src/os_unix.c
index 0aa6f3c..c69798d 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -4348,6 +4348,68 @@
     return OK;
 }
 
+#if defined(FEAT_EVAL) || defined(PROTO)
+    void
+f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
+{
+    struct cellsize cs;
+    mch_calc_cell_size(&cs);
+
+    if (rettv_list_alloc(rettv) == FAIL)
+        return;
+
+    list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
+    list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
+}
+#endif
+
+/*
+ * Try to get the current terminal cell size.
+ * If faile get cell size, fallback 5x10 pixel.
+ */
+    void
+mch_calc_cell_size(struct cellsize *cs_out)
+{
+#if defined(FEAT_GUI)
+    if (!gui.in_use)
+    {
+#endif
+        // get current tty size.
+        struct winsize ws;
+        int fd = 1;
+        int retval = -1;
+        retval = ioctl(fd, TIOCGWINSZ, &ws);
+#  ifdef FEAT_EVAL
+        ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
+#  endif
+        if (retval == -1)
+        {
+            cs_out->cs_xpixel = -1;
+            cs_out->cs_ypixel = -1;
+            return;
+        }
+
+        // calculate parent tty's pixel per cell.
+        int x_cell_size = ws.ws_xpixel / ws.ws_col;
+        int y_cell_size = ws.ws_ypixel / ws.ws_row;
+
+        // calculate current tty's pixel
+        cs_out->cs_xpixel = x_cell_size;
+        cs_out->cs_ypixel = y_cell_size;
+
+#  ifdef FEAT_EVAL
+        ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
+#  endif
+#if defined(FEAT_GUI)
+    }
+    else
+    {
+        cs_out->cs_xpixel = -1;
+        cs_out->cs_ypixel = -1;
+    }
+#endif
+}
+
 #if defined(FEAT_TERMINAL) || defined(PROTO)
 /*
  * Report the windows size "rows" and "cols" to tty "fd".
@@ -4367,8 +4429,13 @@
 
     ws.ws_col = cols;
     ws.ws_row = rows;
-    ws.ws_xpixel = cols * 5;
-    ws.ws_ypixel = rows * 10;
+
+    // calcurate and set tty pixel size
+    struct cellsize cs;
+    mch_calc_cell_size(&cs);
+    ws.ws_xpixel = cols * cs.cs_xpixel;
+    ws.ws_ypixel = rows * cs.cs_ypixel;
+
     retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
     ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
 # elif defined(TIOCSSIZE)
diff --git a/src/os_unix.h b/src/os_unix.h
index 99184ab..fac2c0f 100644
--- a/src/os_unix.h
+++ b/src/os_unix.h
@@ -488,3 +488,9 @@
 
 // We have three kinds of ACL support.
 #define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)
+
+struct cellsize {
+    unsigned int cs_xpixel;
+    unsigned int cs_ypixel;
+};
+
diff --git a/src/proto/os_unix.pro b/src/proto/os_unix.pro
index d9dc8d9..be4942d 100644
--- a/src/proto/os_unix.pro
+++ b/src/proto/os_unix.pro
@@ -91,4 +91,6 @@
 void stop_timeout(void);
 volatile sig_atomic_t *start_timeout(long msec);
 void delete_timer(void);
+void f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv);
+void mch_calc_cell_size(struct cellsize *cs_out);
 /* vim: set ft=c : */
diff --git a/src/testdir/test_utf8.vim b/src/testdir/test_utf8.vim
index 991a095..d820e35 100644
--- a/src/testdir/test_utf8.vim
+++ b/src/testdir/test_utf8.vim
@@ -273,6 +273,14 @@
   bwipe!
 endfunc
 
+" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
+func Test_getcellpixels()
+  " Not yet Windows-compatible
+  CheckNotMSWindows
+  let cellpixels = getcellpixels()
+  call assert_equal(2, len(cellpixels))
+endfunc
+
 func Test_getcellwidths()
   call setcellwidths([])
   call assert_equal([], getcellwidths())
diff --git a/src/version.c b/src/version.c
index 4e5864f..5337b1a 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    854,
+/**/
     853,
 /**/
     852,