xref: /netbsd-src/external/mit/ctwm/libexec/ctwm_font_size (revision ad94c89804fc686c465e8f0f846ac29340f357ae)
1#!/bin/sh
2#	$NetBSD: ctwm_font_size,v 1.1 2020/10/12 11:07:24 nia Exp $
3#
4# Copyright (c) 2020 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Nia Alarie.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32width=$(xwininfo -root | awk '/Width:/{ print $2; }')
33height=$(xwininfo -root | awk '/Height:/{ print $2; }')
34dpi=$(xdpyinfo | awk '/resolution:/{ split($2, res, "x"); print res[1]; }' | head -1)
35
36# Attempt to select a reasonably sized font for the reported DPI.
37#
38# If the DPI is greater than 100, chances are it's specified and
39# not just a default.
40if [ "$dpi" -gt 100 ]; then
41	for size in 16 24 32 64; do
42		mm=$(printf '%d / (%d / 25)\n' "$size" "$dpi" | bc)
43		if [ "$mm" -ge 3 ]; then
44			printf '%d\n' "$size"
45			exit 0
46		fi
47	done
48fi
49
50# In case the reported DPI is lower than the actual DPI, allow at least 2x2
51# 80x24 xterms to be displayed in a grid.
52if [ "$width" -gt 6088 -a "$height" -gt 3656 ]; then
53	printf '64\n'
54elif [ "$width" -gt 3048 -a "$height" -gt 1832 ]; then
55	printf '32\n'
56elif [ "$width" -gt 2248 -a "$height" -gt 1400 ]; then
57	printf '24\n'
58elif [ "$width" -gt 640 -a "$height" -gt 480 ]; then
59	printf '16\n'
60else
61	# One 80x24 xterm with 16 pixel fonts does not fit into 640x480...
62	printf '12\n'
63fi
64