tFigure out DPI while loading font - xmenu - drop-down menu for X11
 (HTM) git clone git://git.z3bra.org/xmenu.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit cee4923f600997aed880b33c886e4300288169e6
 (DIR) parent 505a6b3bd489b7d60ead607b4e4755134301b55b
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Wed, 28 Oct 2020 11:04:52 +0100
       
       Figure out DPI while loading font
       
       Diffstat:
         M font.c                              |      42 +++++++++++++++++++++++++++++--
         M font.h                              |       3 ++-
         M xmenu.c                             |       6 +-----
       
       3 files changed, 43 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/font.c b/font.c
       t@@ -4,9 +4,42 @@
        static FT_Face face;
        static FT_Library ft;
        
       +double
       +xft_forcedpi(xcb_connection_t *dpy)
       +{
       +        xcb_xrm_database_t *db;
       +        char *r;
       +
       +        /* try to set DPI from "Xft.dpi" resource */
       +        db = xcb_xrm_database_from_default(dpy);
       +        if (db) {
       +                xcb_xrm_resource_get_string(db, "Xft.dpi", NULL, &r);
       +                if (r)
       +                        return strtod(r, NULL);
       +        }
       +
       +        return -1;
       +}
       +
       +double
       +xft_getxdpi(xcb_screen_t *screen)
       +{
       +        /* return actual screen DPI */
       +        return 25.4 * screen->width_in_pixels/screen->width_in_millimeters;
       +}
       +
       +double
       +xft_getydpi(xcb_screen_t *screen)
       +{
       +        /* return actual screen DPI */
       +        return 25.4 * screen->height_in_pixels/screen->height_in_millimeters;
       +}
       +
       +
        int
       -xft_loadfont(char *query, double dpi)
       +xft_loadfont(xcb_connection_t *dpy, xcb_screen_t *screen, char *query)
        {
       +        double dpi[2];
                FcResult result;
                FcPattern *pattern, *match;
                FcValue index, file, pxsz;
       t@@ -49,7 +82,12 @@ xft_loadfont(char *query, double dpi)
                if (FT_New_Face(ft, (const char *)file.u.s, index.u.i, &face))
                        return -1;
        
       -        FT_Set_Char_Size(face, 0, 64 * pxsz.u.d/(dpi/72.0), dpi, dpi);
       +        dpi[0] = dpi[1] = xft_forcedpi(dpy);
       +        if (dpi[0] < 0) {
       +                dpi[0] = xft_getxdpi(screen);
       +                dpi[1] = xft_getydpi(screen);
       +        }
       +        FT_Set_Char_Size(face, 0, 64 * pxsz.u.d, dpi[0], dpi[1]);
        
                FcPatternDestroy(match);
        
 (DIR) diff --git a/font.h b/font.h
       t@@ -9,8 +9,9 @@
        
        #include <xcb/render.h>
        #include <xcb/xcb_renderutil.h>
       +#include <xcb/xcb_xrm.h>
        
       -int xft_loadfont(char *, double);
       +int xft_loadfont(xcb_connection_t *, xcb_screen_t *, char *);
        int xft_drawtext(xcb_connection_t *, xcb_drawable_t, int, int, int, char *);
        size_t xft_txtw(char *);
        size_t xft_txth(char *);
 (DIR) diff --git a/xmenu.c b/xmenu.c
       t@@ -247,7 +247,6 @@ int
        main(int argc, char *argv[])
        {
                int dflag = 0;
       -        long dpi;
                char *argv0;
        
                ARGBEGIN {
       t@@ -280,10 +279,7 @@ main(int argc, char *argv[])
                if (!screen)
                        return -1;
        
       -        /* 1 inch = 25.4 millimeters */
       -        dpi = screen->height_in_pixels/screen->height_in_millimeters * 25.4;
       -
       -        xft_loadfont(font, dpi);
       +        xft_loadfont(dpy, screen, font);
        
                entries = argv;
                for (nent = 0; nent < (size_t)argc; nent++) {