TL;DR: What's a simple way to reliably say: use Consolas as the default font, FreeMono for the characters unsupported by Consolas, and Symbola for characters unsupported by both?
Since my main programming font does not cover all the mathematical symbols I need, I initially set up font fallback as shown below:
(set-fontset-font t 'unicode (font-spec :name "FreeMono") nil 'append)
(set-fontset-font t 'unicode (font-spec :name "Symbola") nil 'append)
Unfortunately this also changed the font for some of the character that my main font supports, so I changed it to
(set-fontset-font t 'unicode (font-spec :name "Consolas") nil)
(set-fontset-font t 'unicode (font-spec :name "FreeMono") nil 'append)
(set-fontset-font t 'unicode (font-spec :name "Symbola") nil 'append)
If my understanding is correct, this should ensure that characters that Consolas cannot handle are handled by FreeMono, unless FreeMono doesn't have them, in which case they should be displayed using Symbola. It is also my understanding that t
does the same as "fontset-default"
above.
Unfortunately, there were still cases where the right font wasn't selected; I found that changing to
(set-fontset-font t 'unicode (font-spec :name "Consolas") nil)
(set-fontset-font t 'unicode (font-spec :name "FreeMono") nil 'append)
(set-fontset-font t 'unicode (font-spec :name "Symbola") nil 'append)
(set-fontset-font "fontset-startup" 'unicode (font-spec :name "Consolas") nil)
(set-fontset-font "fontset-startup" 'unicode (font-spec :name "FreeMono") nil 'append)
(set-fontset-font "fontset-startup" 'unicode (font-spec :name "Symbola") nil 'append)
worked better, but not always: changing the font size using
(set-face-attribute 'default nil :height some-size)
caused the fallbacks to be ignored, due to new fontsets being created.
My current solution is to do
(set-fontset-font fontset 'unicode (font-spec :name "Consolas") nil)
(set-fontset-font fontset 'unicode (font-spec :name "FreeMono") nil 'append)
(set-fontset-font fontset 'unicode (font-spec :name "Symbola") nil 'append)
on each fontset (fontset-list
), after each font size change.
What's the proper way to set font fallback?
Note: for testing purposes, here are a few math characters: ℕ𝓟⧺×≠≥≤±¬∨∧∃∀λ⟿⟹⊥⊤⊢
References: Emacs manual on fontsets and on modifying fontsets
fontset
in the wrong way. Emacs does not actually check for every character it displays whether a font knows how to display it or not. That would be too computationally intensive. So there is no "fallback" mechanism per se. You should set your default font and then modify the default fontset manually in certain ranges to display using different fonts. This procedure is manual or maybe the packageunicode
can help.