From 9f0e7a0c121e1393de74b058d1e0a754931f3b65 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 17 May 2022 03:14:19 +0000 Subject: [PATCH] Xdefaults, xinitrc: dynamically calculate font size Recent changes in OpenBSD made my laptops share different (althought correctly calculated) DPIs. Add an script and rework X resources loading so: 1. load ~/.Xdefaults into xrdb for baseline 2. run the script to compute a DPI and xterm's faceSize so it respects some values previously defined in this repo As an upside, now XTerm.VT100.faceSize can be queried, so use that in hlwm theme. --- dotfiles/Xdefaults | 21 ++++-------- dotfiles/xinitrc | 6 +++- utils/scripts/x-dpi-facesizes.sh | 53 +++++++++++++++++++++++++++++++ xdg-config-dir/herbstluftwm/theme | 6 ++-- 4 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 utils/scripts/x-dpi-facesizes.sh diff --git a/dotfiles/Xdefaults b/dotfiles/Xdefaults index e7ca201..3f8414d 100644 --- a/dotfiles/Xdefaults +++ b/dotfiles/Xdefaults @@ -38,20 +38,13 @@ XTerm.VT100.scrollKey: true !XTerm.VT100.wideFont: -misc-fixed-medium-r-normal--14-130-75-75-c-140-jisx0208.1983-0 XTerm.VT100.faceName: monospace !XTerm.VT100.faceNameDoublesize: M+ 1m -!XTerm.VT100.faceSize: 10.5 -!XTerm.VT100.faceSize1: 6.0 -!XTerm.VT100.faceSize2: 8.0 -!XTerm.VT100.faceSize3: 9.0 -!XTerm.VT100.faceSize4: 10.5 -!XTerm.VT100.faceSize5: 12.0 -!XTerm.VT100.faceSize6: 18.0 -XTerm.VT100.faceSize: 8.0 -XTerm.VT100.faceSize1: 4.0 -XTerm.VT100.faceSize2: 6.0 -XTerm.VT100.faceSize3: 7.0 -XTerm.VT100.faceSize4: 8.0 -XTerm.VT100.faceSize5: 10.0 -XTerm.VT100.faceSize6: 12.0 +XTerm.VT100.faceSize: 10.5 +XTerm.VT100.faceSize1: 6.0 +XTerm.VT100.faceSize2: 8.0 +XTerm.VT100.faceSize3: 9.0 +XTerm.VT100.faceSize4: 10.5 +XTerm.VT100.faceSize5: 12.0 +XTerm.VT100.faceSize6: 18.0 XTerm.VT100.color0: #000000 XTerm.VT100.color1: #872011 diff --git a/dotfiles/xinitrc b/dotfiles/xinitrc index 868caf2..06e643c 100755 --- a/dotfiles/xinitrc +++ b/dotfiles/xinitrc @@ -1,6 +1,6 @@ #!/bin/sh # env -# Written in 2019,2021 by Lucas +# Written in 2019,2021-2022 by Lucas # CC0 1.0 Universal/Public domain - No rights reserved # # To the extent possible under law, the author(s) have dedicated all @@ -21,9 +21,13 @@ xset r rate 300 75 xset b off +xrdb -load ~/.Xdefaults if [ -r ~/.Xresources ]; then xrdb -merge ~/.Xresources fi +if [ -r ~/code/lucas/env/utils/scripts/x-dpi-facesizes.sh ]; then + sh ~/code/lucas/env/utils/scripts/x-dpi-facesizes.sh +fi : ${logdir:=~/local/var/log} exec herbstluftwm -l >"$logdir/hlwm.out" 2>"$logdir/hlwm.err" diff --git a/utils/scripts/x-dpi-facesizes.sh b/utils/scripts/x-dpi-facesizes.sh new file mode 100644 index 0000000..0b35687 --- /dev/null +++ b/utils/scripts/x-dpi-facesizes.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# env +# Written in 2022 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . + +FACESIZES="10.5 6.0 8.0 9.0 10.5 12.0 18.0" +BASEDPI=96 + +targetdpi=96 +if command -v xdpyinfo >/dev/null 2>&1; then + targetdpi=$(xdpyinfo | grep '^ resolution:' | { + min= + while read -r line; do + set -- $line + dpi_x=${2%%x*} + dpi_y=${2##*x} + dpi_sum=$(( $dpi_x + $dpi_y )) + if [ -z "$min" ]; then + min=$dpi_sum + else + min=$(( $dpi_sum < $min ? $dpi_sum : $dpi )) + fi + done + + if [ -z "$min" ]; then + echo 96 + else + echo "$min / 2" | bc -l + fi + }) +fi + +for facesize in $FACESIZES; do + echo "$BASEDPI * $facesize / $targetdpi" +done | bc -l | { + i=0 + printf "Xft.dpi:\t%.1f\n" "$targetdpi" + while read -r v; do + if [ $i -eq 0 ]; then + printf "XTerm.VT100.faceSize:\t%.1f\n" "$v" + else + printf "XTerm.VT100.faceSize%d:\t%.1f\n" $i "$v" + fi + i=$(($i + 1)) + done +} | xrdb -merge diff --git a/xdg-config-dir/herbstluftwm/theme b/xdg-config-dir/herbstluftwm/theme index 47cb8aa..aded0a4 100644 --- a/xdg-config-dir/herbstluftwm/theme +++ b/xdg-config-dir/herbstluftwm/theme @@ -29,7 +29,9 @@ : ${hlwm_foreground="#fff8f0"} : ${hlwm_font="-misc-fixed-medium-r-normal--14-130-75-75-c-70-iso10646-1"} -#: ${hlwm_face="monospace:size=10.5"} -: ${hlwm_face="monospace:size=8.0"} + +_hlwm_faceSize=$(xrdb -get XTerm.VT100.faceSize) +: ${hlwm_face="monospace:size=${_hlwm_faceSize:-10.5}"} +unset _hlwm_faceSize : ${hlwm_font_height=17}