I had ligatures working in Emacs with ligature.el, and JetBrains Mono drew !== as one long bar. It looked great until I tried to tell != from !== at a glance. JetBrains Mono ships a few optional extras, and one of them, ss19, puts small gaps between the equals signs. Next to it was zero, a slashed zero instead of the dotted one. I wanted both.
Plenty of editors expose these as a setting. In Emacs there is no place to put it. I ended up hacking Emacs and went down a rabbit hole. At the end, on my own build it looks like this:
In Part 1 I got to know a little bit more about font composition in Emacs. That turned out to be the first wall. Passing the features along was the easy part. Understanding when Emacs decides to shape text at all, and how to make it shape mine, was not.
“Probably” out of luck
The ligature.el README has a limitations section. Emacs doesn’t support stylistic sets, you can’t set them from Elisp, and you are “(probably) out of luck”.
Probably. If the only thing missing is a way to pass a setting from Elisp, how hard can it be?
How does everyone else do it
I had no idea where to start, so I looked at a program that already does this. WezTerm takes a harfbuzz_features option in its config, and you list exactly the tags I wanted: zero, ss19.
HarfBuzz. I’d seen that word before. When I build Emacs from source, the configure summary at the end has a line Does Emacs use HarfBuzz? yes. I never knew what it was, but now there was a hint: in WezTerm it’s the thing that takes the feature tags. If my Emacs is built with it, the support should be somewhere in there already.
grep harfbuzz
So I grepped the source for harfbuzz. I expected one file and got more than a dozen in src/: ftfont.c, ftcrfont.c, xftfont.c, a few Windows ones, and hbfont.c.
ftcrfont.c was the confusing one, so I opened it. Near the bottom, inside #ifdef HAVE_HARFBUZZ, there’s this:
ftcrhbfont_driver = ftcrfont_driver;
ftcrhbfont_driver.type = Qftcrhb;
...
ftcrhbfont_driver.shape = hbfont_shape;
...
register_font_driver (&ftcrhbfont_driver, NULL);
It copies a whole struct font_driver, swaps a few function pointers, and registers the copy under a new name, ftcrhb. Looking up struct font_driver in font.h explained the rest. It’s a table of functions: how to list fonts, open them, measure them, draw them. Emacs calls each of these a font backend. ftcr is FreeType plus Cairo, and ftcrhb is the same backend with HarfBuzz doing the shape step. The other files are the same idea for Xft, Windows and so on, and (frame-parameter nil 'font-backend) shows which ones a frame is using.
So all roads lead to hbfont_shape, which lives in src/hbfont.c. It’s a small file, and the copyright line starts at 2019, which makes it a baby by Emacs standards. Most of it is plumbing: wrap the font, fill a buffer with characters. Then, near the bottom:
hb_bool_t success = hb_shape_full (hb_font, hb_buffer, NULL, 0, NULL);
I didn’t know what hb_shape_full did, so I went to the HarfBuzz docs. HarfBuzz calls its job shaping: you give it a run of characters and a font, and it tells you which glyphs to draw and where. Characters and glyphs aren’t the same thing. With ligatures on, the three characters !== come back as one glyph. In Arabic, the same letter comes back as a different glyph depending on what’s next to it.
The third argument is an array of hb_feature_t, and the fourth is its length. A feature is a four-letter tag, the same zero and ss19 from the WezTerm config, plus a value and the range of the buffer it applies to. So the font carries all these optional rules, and it’s up to whoever calls the shaper to say which ones to turn on.
Emacs passes NULL and 0. Nothing wrong with that, it’s just not wired up to anything. HarfBuzz arrived in 2019, replacing older shaping engines so that scripts like Arabic would render properly, and for that the font’s defaults are enough. So the slot for features is there, waiting for someone to fill it.
Fine. Hard-code it and see:
static hb_feature_t features[] = {
{HB_TAG('z','e','r','o'), 1, 0, -1}, // slashed zero
{HB_TAG('c','a','l','t'), 1, 0, -1}, // contextual alternates
{HB_TAG('s','s','1','9'), 1, 0, -1}, // stylistic set 19
};
fprintf (stderr, "Enabling features for JetBrains Mono\n");
for (int i = 0; i < 3; i++)
{
char tag_str[5];
hb_tag_to_string (features[i].tag, tag_str);
tag_str[4] = '\0';
fprintf (stderr, "Feature %d: %s (value=%u, start=%u, end=%d)\n",
i, tag_str, features[i].value, features[i].start,
features[i].end);
}
hb_bool_t success = hb_shape_full (hb_font, hb_buffer, features, 3, NULL);
1 means on, and 0 to -1 means the whole buffer. The print loop is there so I’d at least see it working.
Rebuild. Start Emacs from a terminal. Open a file full of zeros.
The zeros were still dotted. The terminal was empty.
Why isn’t this running
An empty terminal is worse than a wrong glyph. A wrong glyph means my features are wrong. An empty terminal means the function I edited was never called.
That made no sense to me. Emacs was obviously drawing JetBrains Mono. Was my frame even on the ftcrhb backend? Was the binary I started even the one I built? I searched, asked on forums, and pulled my hair out for a while.
The useful thing that came out of it was one sentence, repeated in a few places: Emacs doesn’t call hbfont_shape unless it has to.
“Has to” was the clue. What text does Emacs have to shape? The HarfBuzz docs kept using Arabic as the example, where a letter can’t be drawn without looking at its neighbors. So I pasted in some Arabic, العربية, and the terminal filled up with my feature list.
So the code worked, and HarfBuzz worked. Emacs had just decided my zeros didn’t need HarfBuzz.
Who decides
The name I needed came from an old Reddit thread where Eli Zaretskii, the Emacs maintainer, explains composition-function-table. C-h v on it says it’s defined in composite.c, so that was the next file to open.
composite.c is a different kind of file. git log says it was created on 1999-12-15 by Kenichi Handa, and the copyright header still credits AIST, the Japanese national research institute, with registration numbers. Reading through the comments and docstrings, the file is about scripts like Thai, Tibetan and the Indic ones, where characters merge, stack and change shape. Normally Emacs puts one glyph per character and moves on. This file is how it says “these ones go together”.
composition-function-table is how it says that. It’s a char-table, so every character has a slot. Most slots are empty. A filled slot holds rules of the form [PATTERN PREV-CHARS FUNC]. When Emacs draws a character with an empty slot, it just draws the glyph from the font. That’s the fast path my zeros were taking. When the slot has a rule, Emacs collects text into a glyph string and calls FUNC, which is usually font-shape-gstring, which ends up in hbfont_shape.
My first reading was that Emacs shapes one character at a time, if it shapes at all. But Arabic worked without me setting anything, so somewhere there had to be a rule for it. Grepping lisp/ for composition-function-table found it in lisp/language/misc-lang.el. The rule sits on the whole Arabic block, and its pattern is one character class covering the Arabic letters and the zero-width joiners, followed by a +. That + is the important part. The character is only the trigger. The regexp grabs the whole run of Arabic letters, and the whole run goes to HarfBuzz together. It has to, because a letter’s shape depends on its neighbors. PREV-CHARS even lets the match start a few characters before the trigger.
So a character with an empty slot never gets near HarfBuzz, whatever is around it. And a Latin 0 has an empty slot.
Why gate it like this? I only got the answer later, when I sent my patch to the bug tracker and Eli explained it: “This is an optimization: using the shaping engine slows down redisplay”. Shaping goes from C into Lisp and back into C again. Characters without a rule skip all of that and get their glyphs straight from the font.
Filling the slot
The rest was almost an anticlimax:
(set-char-table-range composition-function-table
?0 ; character '0'
'(["." 0 font-shape-gstring]))
Evaluate, and the zero on screen got its slash. My hard-coded features from 2019 finally reached a character, through a door from 1999.
ss19 needed one more thought. It changes the !== ligature, and a ligature is a sequence, so a rule that only grabbed the ! would hand HarfBuzz a lone ! with nothing to join. The pattern has to cover all three characters:
(set-char-table-range composition-function-table
?! ; character '!'
'(["\\(!==\\)" 0 font-shape-gstring]))
Gaps between the equals signs. Both switches flipped, as long as you don’t mind recompiling Emacs to change your mind.
This is also when I understood what ligature.el actually does. Emacs can’t ask a font which sequences it has ligatures for and fill in this table by itself. ligature.el fills it in for you, one sequence at a time, using the same mechanism Handa built for Devanagari to draw -> as an arrow. I’d been using the package without knowing it was mostly a list of regexps.
Two halves that don’t line up
Looking back at what it took, two separate parts of Emacs have to agree before a feature shows up. composite.c decides whether text gets shaped and how much of it goes together, and it decides by looking at characters. hbfont.c decides how it gets shaped, and the obvious place to say “slashed zero, please” is on a face, next to the font family and the height.
A face is attached to whatever text it is on, but the gate in front of the shaper only looks at which character it is, not which face it has. Right now I’m bridging that by hand with set-char-table-range, which is fine for me and useless for anyone else. Eli wasn’t a fan of that either, but that’s for a later part.
Next
The features are still hard-coded in C. Next step is to allow user to configure in Elisp and pass it to font backend.