query-fonts.pl revision 20d2c4d2
1#!/usr/bin/perl -w
2# $XTermId: query-fonts.pl,v 1.5 2010/05/24 09:01:30 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2010 by Thomas E. Dickey
7#
8#                         All Rights Reserved
9#
10# Permission is hereby granted, free of charge, to any person obtaining a
11# copy of this software and associated documentation files (the
12# "Software"), to deal in the Software without restriction, including
13# without limitation the rights to use, copy, modify, merge, publish,
14# distribute, sublicense, and/or sell copies of the Software, and to
15# permit persons to whom the Software is furnished to do so, subject to
16# the following conditions:
17#
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28#
29# Except as contained in this notice, the name(s) of the above copyright
30# holders shall not be used in advertising or otherwise to promote the
31# sale, use or other dealings in this Software without prior written
32# authorization.
33# -----------------------------------------------------------------------------
34# Test the font-query features of xterm.
35
36# TODO:
37# test relative vs absolute font numbering
38# test all font-slots
39# test selection
40# test bold / wide / widebold
41# test actual fontname vs nominal
42# extend "CSI > Ps; Ps T" to query fontname in hex
43
44use strict;
45
46use Getopt::Std;
47use IO::Handle;
48
49our ( $opt_a, $opt_r, $opt_s );
50&getopts('ars') || die(
51    "Usage: $0 [options]\n
52Options:\n
53  -a      test using absolute numbers
54  -r      test using relative numbers
55  -s      use ^G rather than ST
56"
57);
58
59our $ST = $opt_s ? "\007" : "\x1b\\";
60
61sub no_reply($) {
62    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
63    autoflush TTY 1;
64    my $old = `stty -g`;
65    system "stty raw -echo min 0 time 5";
66
67    print TTY @_;
68    close TTY;
69    system "stty $old";
70}
71
72sub get_reply($) {
73    open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
74    autoflush TTY 1;
75    my $old = `stty -g`;
76    system "stty raw -echo min 0 time 5";
77
78    print TTY @_;
79    my $reply = <TTY>;
80    close TTY;
81    system "stty $old";
82    if ( defined $reply ) {
83        die("^C received\n") if ( "$reply" eq "\003" );
84    }
85    return $reply;
86}
87
88sub query_font($) {
89    my $param = $_[0];
90    my $reply;
91    my $n;
92    my $st    = $opt_s ? qr/\007/ : qr/\x1b\\/;
93    my $osc   = qr/\x1b]50/;
94    my $match = qr/${osc}.*${st}/;
95
96    $reply = get_reply( "\x1b]50;?" . $param . $ST );
97
98    printf "query{%s}%*s", $param, 3 - length($param), " ";
99
100    if ( defined $reply ) {
101        printf "%2d ", length($reply);
102        if ( $reply =~ /${match}/ ) {
103
104            $reply =~ s/^${osc}//;
105            $reply =~ s/^;//;
106            $reply =~ s/${st}$//;
107        }
108        else {
109            printf "? ";
110        }
111
112        my $result = "";
113        for ( $n = 0 ; $n < length($reply) ; ) {
114            my $c = substr( $reply, $n, 1 );
115            if ( $c =~ /[[:print:]]/ ) {
116                $result .= $c;
117            }
118            else {
119                my $k = ord substr( $reply, $n, 1 );
120                if ( ord $k == 0x1b ) {
121                    $result .= "\\E";
122                }
123                elsif ( $k == 0x7f ) {
124                    $result .= "^?";
125                }
126                elsif ( $k == 32 ) {
127                    $result .= "\\s";
128                }
129                elsif ( $k < 32 ) {
130                    $result .= sprintf( "^%c", $k + 64 );
131                }
132                elsif ( $k > 128 ) {
133                    $result .= sprintf( "\\%03o", $k );
134                }
135                else {
136                    $result .= chr($k);
137                }
138            }
139            $n += 1;
140        }
141
142        printf "{%s}", $result;
143    }
144    printf "\n";
145}
146
147if ($opt_r) {
148    my $n;
149    query_font("-");
150    foreach $n ( 0 .. 5 ) {
151        query_font( sprintf "-%d", $n );
152    }
153    query_font("+");
154    foreach $n ( 0 .. 5 ) {
155        query_font( sprintf "+%d", $n );
156    }
157}
158if ($opt_a) {
159    my $n;
160    foreach $n ( 0 .. 5 ) {
161        query_font( sprintf "%d", $n );
162    }
163}
164if ( not $opt_a and not $opt_r ) {
165    query_font("");
166}
167