Control font family, overriding changes to default fontset
I am trying to make a package that generates a type specimen for a font. In short:
- Prompt user for a font family (e.g. from
font-family-list) - Open temp buffer into which I generate strings, in the font, in various sizes
I can do this easily enough for ASCII characters by doing
(insert (propertize "Lorem ipsum" 'face `(:family ,font)))
And this does work for non-ASCII characters under emacs -Q.
However for my personal config I have customized the default fontset to use various fonts according to charset or script:
(set-fontset-font t 'han "MyCjkFont")
In the presence of this customization I find that no matter what I do, MyCjkFont is always used for characters in han. I've tried making custom fontsets, playing with faces, etc., but nothing has worked.
The type specimen should be ephemeral and self-contained, so I don't want to modify the default fontset when generating it (doing so would affect all other windows/buffers, and the changes would persist after killing the specimen).
Is there a way to do this?
Posted Jul 14, 2021, 12:24 AM UTC
I don't understand what you tried to do with
han. Can you show a self-contained example that shows the problem inemacs -Q?Sorry it wasn't clear.
First, I should say that
I can't reproduce this anymore so this simplifies things significantly.
I want to
han) characters(set-fontset-font t 'han "MyCjkFont")) because doing so makes the effect persistent and global(set-fontset-font t 'han "MyOtherCjkFont")with a different fontThe following fails to accomplish this under
emacs -Q:Expected behavior: The string
foobar東京特許許可局all appears in MisakiGothic.Actual behavior: Only the ASCII portion (
foobar) appears in MisakiGothic; the rest appears in PingFang SC, which appears to be a default on my system (macOS 11.4).Screenshot
This happens with both the Mac port and the NS port (both Emacs 28).
MisakiGothic can be downloaded here. The issue occurs with all fonts, but MisakiGothic is easy to distinguish.
That's because MisakiGothic doesn't have a good-enough coverage of the Chinese characters for Emacs to use it. Find a font with a better coverage (for example, one that covers the CJK Unified Ideographs Extension A as well), and Emacs will use it with your code.
The exact criteria for a font to be used by Emacs is complicated, especially for CJK languages. If a font doesn't pass those criteria, Emacs will use the font for the ASCII characters, but not for CJK.
Hmm. That's quite a problematic heuristic. I find that most of my CJK fonts, which work perfectly well and have perfectly adequate coverage, are not recognized as such.
Is there no way to override this behavior?
Which behavior would you like to override, exactly?
Basically, Emacs will only use a font for a script if that font covers (or declares it covers) the script "well enough". Which I think is reasonable -- you wouldn't want to have some of the characters from a script displayed using font A and some other characters from the same script using a different font B. Except that in large scripts like
hanthis means many fonts that support large subsets could still be rejected, and this could be a problem for casual users of that script.If it's this behavior that you want to override, the only way I know of is to customize the fontset, because it is all encoded there. I don't really understand why you artificially restrict yourself to not making any changes to fontsets; I think it's a mistake, because you make your job much harder for no good reason.