If you can dynamically change the font in the terminal like you can with CSS

浪子不回头ぞ 提交于 2019-12-11 15:16:14

问题


I have seen these two questions:

  • Can I change the font of terminal?
  • Is it possible to show mathematical symbols in the terminal?

The answer to both is yes.

I am wondering if it is possible to have a CLI print out mathematical symbols into the terminal from a custom font only it is personally using, while at the same time I never have to change the font on my terminal (I can keep using the default font for everything else). That is, say I have my default font for my terminal set to x. The CLI program would have downloaded and have a local copy of font y. Then the CLI sets the font type to font y for the terminal, draws some stuff in font y, and then sets it back to font x before closing. That way the terminal still has its original font but it was also able to display symbols (like math symbols) from another font. Wondering if this is in any way possible.

I'm imagining this sort of like how you have font-families in CSS. In CSS, you can provide a downloaded font to the end user's browser to display the text using a different font than what they have as the default in the browser. Wondering if you can do this same sort of thing with the terminal. Specifically I am on the Mac using iTerm2, but knowing if it's possible there or on any other terminal would be useful to know.


回答1:


Not really. In principle you can switch fonts with the ANSI escape code ESC [ 11m where 11 can be any of the “fonts” between 10 and 20 inclusive with 10 being the default. You would have to load an appropriate font first (setfont?).

In practice no terminal emulator actually supports this (I've heard that pangoterm does).

The usual workaround is to create a patched Unicode font that encodes additional symbols in private-use areas, and configure your terminal emulator to use that font. However, your programs would then have to output the correct codepoints, you can't locally switch the font for ASCII. Font patching is e.g. suggested by the Powerline vim extension.




回答2:


In X applications, font substitution already does what you describe. By default, when a font does not have a certain glyph, it is substituted from a different font. The details are customisable in /etc/fonts/fonts.conf, there are several GUI configuration tools for that.

Screenshot: Konsole 18.04, configured font for application is DejaVu Sans Mono size 16, beer mugs glyph substituted from Symbola



来源:https://stackoverflow.com/questions/51469342/if-you-can-dynamically-change-the-font-in-the-terminal-like-you-can-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!