1

I am working on an ncurses application that creates some very blocky graphics using alphanum characters. I'd like to be able to detect the dimensions (specifically the aspect ratio) of the font glyphs so that I can correct for it by changing how I render my graphics (such that a square always looks like a square).

I am able to use the

void getmaxyx(WINDOW *win, int y, int x);

function in ncurses to get the size of the screen/window, but since many windows can be freely resized, it, it's not reliable. Is there a similar function (or external mechanism) for retrieving the font dimensions?

9
  • What you your terminal emulator? konsole? xterm? Something else?
    – aviro
    Jan 31, 2022 at 8:15
  • I use xterm and sometimes the console. I'd like a solution that can handle both, or two solutions I can combine together.
    – ttyj
    Feb 5, 2022 at 5:38
  • How would the font dimensions help you? What matters is the number of columns/lines in the terminal (that, as you said, could be obtained using getmaxyx). The font size could be larger or smaller, but it doesn't affect the display, only the lines and columns do. If you resize the window, the font will remain the same, but the number of lines and columns change so you'll need to redraw your graphics anyway.
    – aviro
    Feb 6, 2022 at 16:37
  • Because not all fonts are the same aspect ratio. Terminal dimensions are only helpful if the font aspect ratio remains the same.
    – ttyj
    Feb 7, 2022 at 4:49
  • It still doesn't matter. The terminal dimensions show you the number of columns and lines. Columns = the width of the terminal by characters. So if columns = 80, it means that the width of the terminal is exactly 80 characters, regardless of the size or the aspect ratio of the font. Same goes for the number of lines. A line is always at the height of a single character, the font has nothing to do with it.
    – aviro
    Feb 7, 2022 at 7:22

2 Answers 2

2

With xterm or compatible and provided font operation escape sequences have not been disabled:

$ xtermcontrol --get-font
DejaVu Sans Mono

That sends the <ESC>]50;?<ESC>\ escape sequence, and the terminal replies with <ESC>]50;font-name<ESC>\. See reference.

So you can also query it with zsh for instance with:

$ IFS=$';\e' read -rsd'\' -t0.2 $'font?\e]50;?\e\\' font font rest
$ typeset font
font='DejaVu Sans Mono'

allowFontOps is generally disabled by default though. You'd set the *allowFontOps: true resource to enable them or preferably keep it disabled but set *disallowedFontOps: SetFont to allow GetFont but not SetFont.

For the size, set the:

*disallowedWindowOps: 1,2,3,4,5,6,7,8,9,11,13,19,20,21,GetSelection,SetSelection,SetWinLines,SetXprop

resource, that is disallow all but GetWinSizeChars and GetWinSizePixels, then with zsh:

$ IFS=';t' read -rsdt -t0.2 $'x?\e[18t' cy cx rest
$ IFS=';t' read -rsdt -t0.2 $'x?\e[14t' y x rest
$ echo ${x}x${y} ${cx}x${cy}
1916x1012 147x38

\e[18t requests the window size in number of cells, \e[14t in number of pixels so you can get the size of a cell with:

$ printf '%gx%g\n' '1.*x/cx' '1.*y/cy'
13x26
-2

Hello from the sunny Athens. Unfortunately things like font_size, or various fonts, or things like that, have unfortunately to do with loading the graphical driver for the HW and applying it as a graphical environment, that is loaded ONLY, when you use the graphical environment from Linux and not from the console (alphanumeric mode only) or using CTRL+T for enabling a terminal during a Windows session in any case. The only possibility to use different font sizes, it could be through ncurses, but according to POSIX standards, this is not possible. If you want to apply in any case different font sizes, there are many environments like processing supported by java, which will give you a solution. Greetings from Athens Kostas Vergidis

2
  • 2
    No, the Linux console (without a graphical environment) is capable of using various fonts, and their size isn’t fixed. May 26 at 4:58
  • Hi kosta, and welcome to the site! As Stephen points out, I'm afraid this is wrong, which is why you're getting downvotes. Don't take it personally, it's about the answer, not you! Also, please avoid writing things that aren't directly relevant to the post. We try to keep things direct and to the point and regularly remove greetings, thanks etc. That's so we can help others find the information they need as efficiently as possible (δεν είμαστε απλώς στριμμένοι, δηλαδή, έχουμε λόγο!).
    – terdon
    May 27 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .