1#!/bin/bash
2#
3# Copyright (c) 2019-2020 Paul Mattes.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#     * Redistributions of source code must retain the above copyright
10#       notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above copyright
12#       notice, this list of conditions and the following disclaimer in the
13#       documentation and/or other materials provided with the distribution.
14#     * Neither the names of Paul Mattes, Jeff Sparkes, GTRC nor their
15#       contributors may be used to endorse or promote products derived
16#       from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
19# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21# EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29# Run x3270 with cheesy automatic font scaling.
30#  x3270a [-d] [x3270-options]
31
32# Figure out the pathname of the directory this was run in.
33d=`dirname $0`
34case "$d" in
35    .)	d=`pwd`
36	;;
37    /*)	;;
38    *)	d=`pwd`/$d
39esac
40
41if [ "$1" = "-d" ]
42then	debug=1
43	shift
44fi
45
46# Debug echo function.
47function decho
48{
49    [ -n "$debug" ] && echo $*
50}
51
52# Figure out the display dpi and the scale relative to 96.
53if [ -n "$DPI" ]
54then	dpi=$DPI
55    	dpi_xrm="-xrm"
56    	dpi_opt="Xft.dpi: $DPI"
57else	dpi=$(xrdb -query | awk '/^Xft\.dpi:/ { print $2 }')
58	if [ -z "$dpi" ]
59	then	echo >&2 "Display DPI is unknown"
60		exit 1
61	fi
62fi
63typeset -i scale
64let "scale = ($dpi*100)/96"
65decho "display is ${dpi}dpi, scale is $scale"
66
67# 96dpi needs no mods
68if [ $scale -eq 100 ]
69then	set -x
70    	exec $d/x3270 "$@"
71fi
72
73# These are hand-tuned.
74typeset -i s20
75let "s20 = ($scale * 10) / 130"
76decho "s20 is $s20"
77typeset -i s24
78let "s24 = ($scale * 10) / 108"
79decho "s24 is $s24"
80typeset -i s32
81let "s32 = ($scale * 10) / 81"
82decho "s32 is $s32"
83typeset -i s40
84let "s40 = ($scale * 10) / 65"
85decho "s40 is $s40"
86
87# x3270 fonts only come in discrete sizes (12, 16, 20, 24, 32).
88# Pick something big enough.
89if [ $s32 -ge 29 ]
90then	efont=3270gt32
91elif [ $s32 -ge 23 ]
92then	efont=3270gt24
93elif [ $s32 -ge 19 ]
94then	efont=3270-20
95elif [ $s32 -ge 15 ]
96then	efont=3270gt16
97else	efont=3270
98fi
99decho "efont is $efont"
100
101set -x
102exec $d/x3270 \
103 -xrm "x3270*keyPad*large*font: -misc-fixed-medium-r-normal--$s24-*-*-*-*-*-iso8859-1" \
104 -xrm "x3270*keyPad*small*font: -misc-fixed-medium-r-normal--$s20-*-*-*-*-*-iso8859-1" \
105 -xrm "x3270*value*font: -misc-fixed-medium-r-normal--$s20-*-*-*-*-*-iso8859-1" \
106 -xrm "x3270*filename*font: -misc-fixed-medium-r-normal--$s20-*-*-*-*-*-iso8859-1" \
107 -xrm "x3270*kmPopup*text*font: -misc-fixed-medium-r-normal--$s20-*-*-*-*-*-iso8859-1" \
108 -xrm "x3270*smallLabel.font: -misc-fixed-medium-r-normal--$s24-*-*-*-*-*-iso8859-1" \
109 -xrm "x3270*dataLabel.font: -misc-fixed-medium-r-normal--$s40-*-*-*-*-*-iso8859-1" \
110 -xrm "x3270*dialog*value*font: -misc-fixed-medium-r-normal--$s40-*-*-*-*-*-iso8859-1" \
111 -xrm "x3270*font: -*-helvetica-bold-r-normal--$s32-*-*-*-p-*-iso8859-1" \
112 -efont $efont \
113 $dpi_xrm "$dpi_opt" "$@"
114