Adding a per-language default font

After some struggles, I was able to add Hangul to fcitx and am able to type with it system-wide. However, for some reason the default font that is used is, well, quite ugly, and I’d like to change it. I’ve made changes to configuration.nix as
fonts.fontconfig.defaultFonts = { monospace = [ "D2Coding" ]; sansSerif = [ "Fira Sans Book" "Noto Sans" ]; };
and the monospace font changed for both hangul and latin characters, but sansSerif keeps using whatever NixOS/fcitx defaulted to in hangul although to my understanding it should use Noto Sans since Fira does not have hangul characters.
I’ve tried moving things around, making changes to the window manager (I’m using awesomewm), but the hangul font never changes.

Is there a way of forcing NixOS to use a different font for hangul/CJK? And if it’s not NixOS’s “job” to handle that, would I have to look somewhere in the wm or fcitx?

1 Like

I use zh-cn locale on my computer. I’ve spent a lot of hours and this is a very complex topic, and the problems are somewhat exacerbated by, frankly, the inconsistency of fontconfig itself.

In short, my observation is that if I have a latin locale, then specifying a font list like -

$nixos-option fonts.fontconfig.defaultFonts.sansSerif                                                                       [zx@Nix_Desk 2:48:00 ]
Value:
[
  "PT Sans"
  "PT Sans Regular"
  "Delicious"
  "Noto Sans CJK SC"
  "HanaMinA"
  "HanaMinB"
  "Noto Emoji"
  "Noto Color Emoji"
  "Gargi-1.2b"
]

would get you the correct order most of the times. (This is in particular is a NixOS thing). Otherwise (non-latin locale) use a per user fontconfig file.

Like this-

<?xml version='1.0'?>

<!--
IMPORTANT: None of this match target = pattern, name = family
stuff should be needed .

This seems like a hack. The only thing that should be required 
should be the version before this change. 
Just a list of alias -> family -> prefer -> family

We copied
https://wiki.archlinux.org/index.php/Font_configuration/Examples#Japanese
and replaced JP with SC (Simplified Chinese)
-->

<!-- Good link: https://eev.ee/blog/2015/05/20/i-stared-into-the-fontconfig-and-the-fontconfig-stared-back-at-me -->
<!-- Use `fc-match -s serif` or `fc-match -s "sans serif"` to list fallback fonts -->
<!-- 
  Search for Eric Sagnes fontconfig 
  to find the exact same problem as yours
-->


<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
  <!-- My own -->
  <match target="font">
    <edit mode="assign" name="antialias"> <bool>true</bool></edit>
  </match>
  
  <!-- 
    For Chinese fonts 
    automatically use a larger font size 
    until we are familiar
  -->
  
  <match target="font">
      <test name="family">
        <string>Noto Sans CJK SC</string>
      </test>
      <edit name="pixelsize" mode="assign">
          <times><name>pixelsize</name>, <double>1.3</double></times>
      </edit>
      <!--
      <edit name="embolden" mode="assign">
        <bool>true</bool>
      </edit>
      -->
  </match>

  <!-- Mostly copied from archwiki link -->


	<!-- Default font (no fc-match pattern) -->
	<match>
	 <edit mode="prepend" name="family">
		 <string>PT Sans</string>
	 </edit>
	</match>

	<!-- Default font for the zh locale -->
	<match>
		<test compare="contains" name="lang">
			<string>zh</string>
		</test>
		<edit mode="prepend" name="family">
			<string>PT Sans</string>
		</edit>
	</match>

  <!-- Default sans-serif font -->
  <match target="pattern">
    <test qual="any" name="family"><string>sans-serif</string></test>
    <edit name="family" mode="prepend" binding="same"><string>PT Sans</string>  </edit>
  </match>

  <!-- Default serif fonts -->
  <match target="pattern">
    <test qual="any" name="family"><string>serif</string></test>
    <edit name="family" mode="prepend" binding="same"><string>Bree Serif</string>  </edit>
  </match>

  <!-- Default monospace fonts -->
  <match target="pattern">
    <test qual="any" name="family"><string>monospace</string></test>
    <edit name="family" mode="append" binding="same"><string>Code New Roman</string></edit>
</match>

<!-- Fallback fonts preference order -->
  <alias>
    <family>serif</family>
    <prefer>
      <family>Bree Serif</family>
      <family>Noto Sans CJK SC</family>
      <family>Noto Emoji</family>
      <family>Noto Color Emoji</family>
    </prefer>
  </alias>
  <alias>
    <family>sans-serif</family>
    <prefer>
      <family>PT Sans</family>
      <family>Noto Sans CJK SC</family>
      <family>Noto Emoji</family>
      <family>Noto Color Emoji</family>
    </prefer>
  </alias>
  <alias>
    <family>monospace</family>
    <prefer>
      <family>Code New Roman</family>
      <family>Noto Sans CJK SC</family>
      <family>Noto Emoji</family>
      <family>Noto Color Emoji</family>
    </prefer>
  </alias>

</fontconfig>
<!-- vim:set ft=xml: -->

This is a file I created after a lot of research (though I don’t remember anymore how I exactly got where I was. Read the links I’ve mentioned for some more context.

3 Likes