开发者

Change Terminal Font Size with C++

I'm doing a small project for fun in C++ (in Ubuntu 11.04) and the program is text-based (all in the Gnome terminal). I'm using the ncurses library to change the font color, but I also want to print d开发者_如何转开发ifferent sized text to the terminal, and can't figure out how to do that with ncurses. Is there a way to do this (perhaps with ncurses, or with a different library)? Ideally, I'd want it to be terminal-independent,, but if it's a solution that only works in Gnome, or only works in Ubuntu, or some other restriction like that then that's better than nothing!

Thanks for your help as always.


I've tried the suggestion from Keith Thompson but couldn't get it to work. Here's my code:

cout << "\x1b]50;" << "10x20" << "\a" << flush;
cout << "test";

It just shows up as the same font size specified in the terminal preferences. I'm using: GNOME Terminal 2.32.1 if that helps!


At least for xterm, you can change the current font by printing an escape sequence. The syntax is ESCAPE ] 50 ; FONTNAME BEL.

Here's (an abbreviated version of) a script I use for this; I call it xfont (the real one has more error checking):

#!/usr/bin/perl

use strict;
use warnings;

print "\e]50;@ARGV\a";

I don't know which other terminal emulators recognize this sequence. In particular, I find that it doesn't work under screen, even if the screen session is in an xterm window.

Note that you have to specify the name of the font ("10x20", "9x15"), not its size.

EDIT: I should pay more attention to tags. In C++, it would be something like:

std::cout << "\x1b]50;" << font_name << "\a" << std::flush;

UPDATE: With xterm, this won't work if you're using TrueType fonts. Also, Dúthomhas suggests in a comment:

I know this is old, but all terminfo strings should be printed using putp() [or tputs()], even in C++.

putp( (std::string{ "\33]50;" } + font_name + "\a").c_str() );


The best you can do is to use bold font. Terminal emulates real text-based terminal so it doesn't support different fonts at once.


ncurses does not change font sizes, but does manipulate styles within a font using the video attributes (bold, italic). Some terminals have a documented way to change their font size, which affects the whole window (making it "invisible" to ncurses).

As noted, xterm has a feature for setting a font which is documented in XTerm Control Sequences, in the Operating System Commands section:

        Ps = 5 0  ⇒  Set Font to Pt.  These controls may be disabled
      using the allowFontOps resource.  If Pt begins with a "#",
      index in the font menu, relative (if the next character is a
      plus or minus sign) or absolute.  A number is expected but not
      required after the sign (the default is the current entry for
      relative, zero for absolute indexing).

      The same rule (plus or minus sign, optional number) is used
      when querying the font.  The remainder of Pt is ignored.

      A font can be specified after a "#" index expression, by
      adding a space and then the font specifier.

      If the TrueType Fonts menu entry is set (the renderFont
      resource), then this control sets/queries the faceName
      resource.

The manual page documents faceName as the font-family name. While one could try adding the pixelsize property to that, it might not work. But changing the faceName works (assuming that you have selected TrueType fonts), e.g., using this script on my current machine:

#!/bin/bash
setfont() {
read -p "$1:"
printf '\033]50;%s\007' "$1"
read -p "done!"
}
setfont "Bitstream Charter"
setfont "URW Gothic L"

I got those names using fc-list. A similar script would work for bitmap fonts, using the names found with xlsfonts. For bitmap fonts, there are font aliases such as 9x15, 10x20, 12x24, but those are only defined for a few combinations of perhaps a thousand results from xlsfonts. For that, read the X Logical Font Description Conventions documentation. Those useful font aliases, by the way, are not systematically documented anywhere.

That only changes the appearance of the characters and might not change the size. But reading the documentation, it is possible (using a feature adapted from rxvt) to use a number to step through the settings on the font menu. This script works for me, changing the font up one, twice, down two (back to the default), up four (to the end of the settings), and back:

#!/bin/bash
changesize() {
read -p "$1:"
printf '\033]50;#%s\007' "$1"
read -p "done!"
}
changesize +1
changesize +1
changesize -2
changesize +4
changesize -4

Because this uses a control sequence that is not from the terminal database, a cout or printf in your program is suitable. You should use putp for strings which come from the terminal database because they can contain padding information which is interpreted by putp (and which would not be useful when printed directly to the terminal). Conversely, some random control sequence could look like something that could confuse putp.

Padding is documented in terminfo(5).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜