#!/bin/true
# this script is to be sourced, not run
# Small library to calculate hidpi scale based on Xft.dpi, and to apply them.

### constants
HIDPI_REF_DPI=${REF_DPI:-96} # this is the DPI at which the scale is 100%
HIDPI_FACTOR=100 # more zeros for more precision

# currently used Xft.dpi
hidpi_get_xft_dpi() {
	local dpi=$(xrdb -get Xft.dpi)
	[ "$dpi" ] && echo "$dpi" || echo $HIDPI_REF_DPI
}

# get scale in $FACTOR (shell doesn't do floating point)
hidpi_get_scale() {
	echo $(( ($(hidpi_get_xft_dpi) * $HIDPI_FACTOR) / $HIDPI_REF_DPI ))
}

# apply to given numbers (supposedly widths and lengths)
hidpi_apply_scale() {
	local p scale=$(hidpi_get_scale) output=""
	for p; do
		output="$output $(( ($p * $scale) / $HIDPI_FACTOR ))"
	done
	echo ${output# }
}

