Score:1

How do I configure fontconfig to map a font name with a hyphen?

cn flag

I'm having trouble with fontconfig recognizing the ui-monospace font that several websites (including Github and Stack Exchange sites) have recently started using. Firefox doesn't yet have that special CSS reference to system fonts (see FF bug 1226042, the draft spec, and its Can I Use entry), and FF is also fooled by fontconfig, which will satisfy any font with its default assignment for sans-serif.

As a workaround, I'd like to explicitly alias this font name to my preferred monospace font (which is Panic Sans, a tweaked DejaVu Sans Mono variant that nudges the underscore up a little), but since that's so obscure, this question uses DejaVu Sans Mono.

My first attempt at this workaround doesn't work:

  <alias>
    <family>ui-monospace</family>
    <prefer><family>DejaVu Sans Mono</family></prefer>
  </alias>

Another attempt that has had more success for other fonts:

  <match>
    <test name="family"><string>ui-monospace</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>DejaVu Sans Mono</string>
    </edit>
  </match>

When I save that and flush my cache, it doesn't work:

$ grep ui.monospace ~/.config/fontconfig/fonts.conf
    <test name="family"><string>ui-monospace</string></test>
$ fc-cache -f
$ fc-match 'ui-monospace'
Carlito-Regular.ttf: "Carlito" "Regular"
$ fc-match sans-serif
Carlito-Regular.ttf: "Carlito" "Regular"

So that doesn't work. Here's the curious part: it works when I use a space instead of a hyphen:

$ sed -i 's/ui-monospace/ui monospace/' ~/.config/fontconfig/fonts.conf
$ grep ui.monospace ~/.config/fontconfig/fonts.conf
    <test name="family"><string>ui monospace</string></test>
$ fc-cache -f
$ fc-match 'ui monospace'
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"

So it appears the issue is the hyphen. I don't see any documentation (or bug reports) suggesting this issue; the fontconfig docs for <string> make no such note of illegal characters or how to escape them. I've tried defining it as <string>ui\-monospace</string> and <string>ui&#45;monospace</string> (and &#x2d;) but none of those worked.

How do I configure fontconfig to map a font name with a hyphen?

eg flag
I think it is an issue with the shell, try again with `fc-match "ui\-monospace"`it works for me with fish shell.
cn flag
Hyphen is not a special character for shells (if it were, `fc-match 'ui-monospace'` would have worked). That escape needs to be seen by _fc-match_, not the shell, as demonstrated by `fc-match "ui\\-monospace"` working.
eg flag
Yes, the first part of my comment is wrong. I meant "command line interface" in opposition to API like pango... Anyway, it seems that `fc-match` needs the escaping. But the configuration should work with hypen in config file.
user.dz avatar
ng flag
@NicolasGoy, it would be nice if you copy that as an answer.
Score:0
co flag

I put this snippet to the top part of my ~/.config/fontconfig/fonts.conf and it works:

  <match target="pattern">
    <test qual="any" name="family">
      <string>ui-monospace</string>
    </test>
    <edit name="family" mode="assign" binding="same">
      <string>monospace</string>
    </edit>
  </match>

It has to be at the top, before the part that specifies which monospace I prefer.

Verified on GitHub and by fc-match 'ui\-monospace'.

cn flag
Yes, but that sends a literal backslash in the match call. If you do `fc-match 'ui-monospace'` or `fc-match ui-monospace` or `fc-match "ui-monospace"`, it doesn't work. As noted by Nicolas Goy in a [comment to the question](https://askubuntu.com/q/1355091/260416#comment2347914_1355091), this is actually a bug in the `fc-match` command line. I additionally had some other font-rendering bug in my GUI apps that was solved by a reboot.
co flag
@AdamKatz several characters like `-` or `:` needs to be escaped because it has special meaning to fontconfig. The part after `-` is interpreted as `size` (but non-digits are ignored). However in the config file, it's already specified to test against `family`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.