1e0a2b6dfSmrg#!/usr/bin/env perl
204b94745Smrg# $XTermId: query-fonts.pl,v 1.8 2019/05/19 08:57:31 tom Exp $
320d2c4d2Smrg# -----------------------------------------------------------------------------
420d2c4d2Smrg# this file is part of xterm
520d2c4d2Smrg#
6f2e35a3aSmrg# Copyright 2010-2018,2019 by Thomas E. Dickey
720d2c4d2Smrg#
820d2c4d2Smrg#                         All Rights Reserved
920d2c4d2Smrg#
1020d2c4d2Smrg# Permission is hereby granted, free of charge, to any person obtaining a
1120d2c4d2Smrg# copy of this software and associated documentation files (the
1220d2c4d2Smrg# "Software"), to deal in the Software without restriction, including
1320d2c4d2Smrg# without limitation the rights to use, copy, modify, merge, publish,
1420d2c4d2Smrg# distribute, sublicense, and/or sell copies of the Software, and to
1520d2c4d2Smrg# permit persons to whom the Software is furnished to do so, subject to
1620d2c4d2Smrg# the following conditions:
1720d2c4d2Smrg#
1820d2c4d2Smrg# The above copyright notice and this permission notice shall be included
1920d2c4d2Smrg# in all copies or substantial portions of the Software.
2020d2c4d2Smrg#
2120d2c4d2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
2220d2c4d2Smrg# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2320d2c4d2Smrg# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2420d2c4d2Smrg# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2520d2c4d2Smrg# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2620d2c4d2Smrg# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2720d2c4d2Smrg# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2820d2c4d2Smrg#
2920d2c4d2Smrg# Except as contained in this notice, the name(s) of the above copyright
3020d2c4d2Smrg# holders shall not be used in advertising or otherwise to promote the
3120d2c4d2Smrg# sale, use or other dealings in this Software without prior written
3220d2c4d2Smrg# authorization.
3320d2c4d2Smrg# -----------------------------------------------------------------------------
3420d2c4d2Smrg# Test the font-query features of xterm.
3520d2c4d2Smrg
3620d2c4d2Smrg# TODO:
3720d2c4d2Smrg# test relative vs absolute font numbering
3820d2c4d2Smrg# test all font-slots
3920d2c4d2Smrg# test selection
4020d2c4d2Smrg# test bold / wide / widebold
4120d2c4d2Smrg# test actual fontname vs nominal
4220d2c4d2Smrg# extend "CSI > Ps; Ps T" to query fontname in hex
4320d2c4d2Smrg
4420d2c4d2Smrguse strict;
45e0a2b6dfSmrguse warnings;
4620d2c4d2Smrg
4720d2c4d2Smrguse Getopt::Std;
4820d2c4d2Smrguse IO::Handle;
4920d2c4d2Smrg
5020d2c4d2Smrgour ( $opt_a, $opt_r, $opt_s );
51f2e35a3aSmrg
52f2e35a3aSmrg$Getopt::Std::STANDARD_HELP_VERSION = 1;
5320d2c4d2Smrg&getopts('ars') || die(
5420d2c4d2Smrg    "Usage: $0 [options]\n
5520d2c4d2SmrgOptions:\n
5620d2c4d2Smrg  -a      test using absolute numbers
5720d2c4d2Smrg  -r      test using relative numbers
5820d2c4d2Smrg  -s      use ^G rather than ST
5920d2c4d2Smrg"
6020d2c4d2Smrg);
6120d2c4d2Smrg
6220d2c4d2Smrgour $ST = $opt_s ? "\007" : "\x1b\\";
6320d2c4d2Smrg
6420d2c4d2Smrgsub get_reply($) {
6520d2c4d2Smrg    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
6620d2c4d2Smrg    autoflush TTY 1;
6720d2c4d2Smrg    my $old = `stty -g`;
6820d2c4d2Smrg    system "stty raw -echo min 0 time 5";
6920d2c4d2Smrg
7020d2c4d2Smrg    print TTY @_;
7120d2c4d2Smrg    my $reply = <TTY>;
7220d2c4d2Smrg    close TTY;
7320d2c4d2Smrg    system "stty $old";
7420d2c4d2Smrg    if ( defined $reply ) {
7520d2c4d2Smrg        die("^C received\n") if ( "$reply" eq "\003" );
7620d2c4d2Smrg    }
7720d2c4d2Smrg    return $reply;
7820d2c4d2Smrg}
7920d2c4d2Smrg
8020d2c4d2Smrgsub query_font($) {
8120d2c4d2Smrg    my $param = $_[0];
8220d2c4d2Smrg    my $reply;
8320d2c4d2Smrg    my $n;
8420d2c4d2Smrg    my $st    = $opt_s ? qr/\007/ : qr/\x1b\\/;
8520d2c4d2Smrg    my $osc   = qr/\x1b]50/;
8620d2c4d2Smrg    my $match = qr/${osc}.*${st}/;
8720d2c4d2Smrg
8820d2c4d2Smrg    $reply = get_reply( "\x1b]50;?" . $param . $ST );
8920d2c4d2Smrg
9020d2c4d2Smrg    printf "query{%s}%*s", $param, 3 - length($param), " ";
9120d2c4d2Smrg
9220d2c4d2Smrg    if ( defined $reply ) {
9320d2c4d2Smrg        printf "%2d ", length($reply);
9420d2c4d2Smrg        if ( $reply =~ /${match}/ ) {
9520d2c4d2Smrg
9620d2c4d2Smrg            $reply =~ s/^${osc}//;
9720d2c4d2Smrg            $reply =~ s/^;//;
9820d2c4d2Smrg            $reply =~ s/${st}$//;
9920d2c4d2Smrg        }
10020d2c4d2Smrg        else {
10120d2c4d2Smrg            printf "? ";
10220d2c4d2Smrg        }
10320d2c4d2Smrg
10420d2c4d2Smrg        my $result = "";
10520d2c4d2Smrg        for ( $n = 0 ; $n < length($reply) ; ) {
10620d2c4d2Smrg            my $c = substr( $reply, $n, 1 );
10720d2c4d2Smrg            if ( $c =~ /[[:print:]]/ ) {
10820d2c4d2Smrg                $result .= $c;
10920d2c4d2Smrg            }
11020d2c4d2Smrg            else {
11120d2c4d2Smrg                my $k = ord substr( $reply, $n, 1 );
11220d2c4d2Smrg                if ( ord $k == 0x1b ) {
11320d2c4d2Smrg                    $result .= "\\E";
11420d2c4d2Smrg                }
11520d2c4d2Smrg                elsif ( $k == 0x7f ) {
11620d2c4d2Smrg                    $result .= "^?";
11720d2c4d2Smrg                }
11820d2c4d2Smrg                elsif ( $k == 32 ) {
11920d2c4d2Smrg                    $result .= "\\s";
12020d2c4d2Smrg                }
12120d2c4d2Smrg                elsif ( $k < 32 ) {
12220d2c4d2Smrg                    $result .= sprintf( "^%c", $k + 64 );
12320d2c4d2Smrg                }
12420d2c4d2Smrg                elsif ( $k > 128 ) {
12520d2c4d2Smrg                    $result .= sprintf( "\\%03o", $k );
12620d2c4d2Smrg                }
12720d2c4d2Smrg                else {
12820d2c4d2Smrg                    $result .= chr($k);
12920d2c4d2Smrg                }
13020d2c4d2Smrg            }
13120d2c4d2Smrg            $n += 1;
13220d2c4d2Smrg        }
13320d2c4d2Smrg
13420d2c4d2Smrg        printf "{%s}", $result;
13520d2c4d2Smrg    }
13620d2c4d2Smrg    printf "\n";
13720d2c4d2Smrg}
13820d2c4d2Smrg
13920d2c4d2Smrgif ($opt_r) {
14020d2c4d2Smrg    my $n;
14120d2c4d2Smrg    query_font("-");
14220d2c4d2Smrg    foreach $n ( 0 .. 5 ) {
14320d2c4d2Smrg        query_font( sprintf "-%d", $n );
14420d2c4d2Smrg    }
14520d2c4d2Smrg    query_font("+");
14620d2c4d2Smrg    foreach $n ( 0 .. 5 ) {
14720d2c4d2Smrg        query_font( sprintf "+%d", $n );
14820d2c4d2Smrg    }
14920d2c4d2Smrg}
15020d2c4d2Smrgif ($opt_a) {
15120d2c4d2Smrg    my $n;
15220d2c4d2Smrg    foreach $n ( 0 .. 5 ) {
15320d2c4d2Smrg        query_font( sprintf "%d", $n );
15420d2c4d2Smrg    }
15520d2c4d2Smrg}
15620d2c4d2Smrgif ( not $opt_a and not $opt_r ) {
15720d2c4d2Smrg    query_font("");
15820d2c4d2Smrg}
159