r/emacs•
amake

Control font family, overriding changes to default fontset

Question

I am trying to make a package that generates a type specimen for a font. In short:

  1. Prompt user for a font family (e.g. from font-family-list)
  2. 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

410
eli-zaretskiiGNU Emacs maintainer•

I don't understand what you tried to do with han. Can you show a self-contained example that shows the problem in emacs -Q?

2
amakeOP•

Sorry it wasn't clear.

First, I should say that

And this does work for non-ASCII characters under emacs -Q.

I can't reproduce this anymore so this simplifies things significantly.

I want to

  • set the font family for a particular region of a buffer via some temporary, local mechanism such as string properties
  • have the family take effect for non-ASCII (e.g. han) characters
  • do this without modifying the default fontset (i.e. doing (set-fontset-font t 'han "MyCjkFont")) because doing so makes the effect persistent and global
  • have the family take effect even if I had done (set-fontset-font t 'han "MyOtherCjkFont") with a different font

The following fails to accomplish this under emacs -Q:

(with-output-to-temp-buffer "*Font Test*"
  (with-current-buffer standard-output
    (insert (propertize "lorem ipsum\n" 'face '(:family "Arial" :height 200)))
    (insert (propertize "foobar東京特許許可局\n" 'face '(:family "MisakiGothic" :height 400)))))

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.

1
eli-zaretskiiGNU Emacs maintainer•

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.

1
amakeOP•

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?

1
eli-zaretskiiGNU Emacs maintainer•

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 han this 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.

1