Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 different 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!

like image 824
navr91 Avatar asked Aug 16 '11 18:08

navr91


1 Answers

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() );

like image 194
Keith Thompson Avatar answered Oct 02 '22 02:10

Keith Thompson