Skip to content

Instantly share code, notes, and snippets.

@ernstki
Last active April 30, 2024 10:19
Show Gist options
  • Save ernstki/2ae279ad89888e4099c9852bf0ba5d11 to your computer and use it in GitHub Desktop.
Save ernstki/2ae279ad89888e4099c9852bf0ba5d11 to your computer and use it in GitHub Desktop.
Easily disable fonts that aren't in your language, for Debian and derivatives (and possibly other Linux distros)
#!/usr/bin/env perl
##
## Usage: fc-reject.pl > ~/.config/fontconfig/conf.d/88-reject.conf
## Author: Kevin Ernst <ernstki -at- mail.uc.edu>
## License: MIT or CC-BY-SA-4.0, at your option
## Source: https://gist.github.com/ernstki/2ae279ad89888e4099c9852bf0ba5d11
##
use v5.12;
use warnings;
use autodie;
my $mylang = 'en'; # use '(en|lang1|lang2)' for other languages
my $rejectfam = 'Noto'; # use '(Noto|Other Family|Another Family)' for others
my $rejectlist = {};
open my $fh, '-|', 'fc-cat';
while (<$fh>) {
if (/.* "([^:,]+).*:lang=([^:]*):.*/) {
my ($name, $lang) = ($1, $2);
if ($name =~ /$rejectfam/ and (!$lang or $lang !~ /$mylang/)) {
$rejectlist->{$name} = 1;
}
}
}
close $fh;
print <<EOF;
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<selectfont>
<rejectfont>
EOF
foreach my $name (keys %$rejectlist) {
print <<EOF;
<pattern>
<patelt name="family">
<string>$name</string>
</patelt>
</pattern>
EOF
}
print <<EOF;
</rejectfont>
</selectfont>
</fontconfig>
EOF
@wdoekes
Copy link

wdoekes commented Apr 30, 2024

On Ubuntu 22.04 you might want to re-enable the emoji by removing Noto Color Emoji from the output. Otherwise you won't even see the 👍 in GitHub.

And maybe Noto Music and Noto Sans Symbols and Noto Sans Symbols2 -- although I'm not sure how often you run into those.

As for the harmless warning, I suggest:

fc-reject.pl > ~/.config/fontconfig/conf.d/88-reject.conf.new &&
  mv ~/.config/fontconfig/conf.d/88-reject.conf{.new,}

The atomic mv ensures that only the complete file is read.

Thanks for this gist, @ernstki !

My changes:

--- fc-reject.pl	2024-04-30 12:15:16.573580587 +0200
+++ /usr/local/bin/remove-noto-fonts	2024-04-30 12:18:46.764030790 +0200
@@ -1,6 +1,6 @@
 #!/usr/bin/env perl
 ##
-##  Usage:    fc-reject.pl > ~/.config/fontconfig/conf.d/88-reject.conf
+##  Usage:    fc-reject.pl > ~/.config/fontconfig/conf.d/88-reject.conf.new && mv ~/.config/fontconfig/conf.d/88-reject.conf{.new,}
 ##  Author:   Kevin Ernst <ernstki -at- mail.uc.edu>
 ##  License:  MIT or CC-BY-SA-4.0, at your option
 ##  Source:   https://gist.github.com/ernstki/2ae279ad89888e4099c9852bf0ba5d11
@@ -9,17 +9,30 @@ use v5.12;
 use warnings;
 use autodie;
 
-my $mylang = 'en';       # use '(en|lang1|lang2)' for other languages
-my $rejectfam = 'Noto';  # use '(Noto|Other Family|Another Family)' for others
+my $mylang = 'en|nl|und-zsye';  # use 'en|lang1|lang2' for other languages
+                                # 'und-zsye' is for Emoji
+my $rejectfam = '^Noto';        # use '^(Noto|Other Family|Another Family)' for others
+my $keepfam = '^Noto Music';	# use '^(Noto Music|Others to keep)' for others
 my $rejectlist = {};
 
 open my $fh, '-|', 'fc-cat';
 while (<$fh>) {
-    if (/.* "([^:,]+).*:lang=([^:]*):.*/) {
-        my ($name, $lang) = ($1, $2);
-        if ($name =~ /$rejectfam/ and (!$lang or $lang !~ /$mylang/)) {
-            $rejectlist->{$name} = 1;
-        }
+    if (/.* "([^:,]+).*:lang=([^:]*)/) {
+        my ($name, @langs) = ($1, split(/\|/, $2));
+        if ($name =~ /$rejectfam/) {
+	    my $keep = 0;
+	    foreach my $lang (@langs) {
+		if ($lang =~ /^($mylang)$/) {
+		    $keep = 1;
+		}
+	    }
+	    if ($name =~ /$keepfam/) {
+		$keep = 1;
+	    }
+	    if ($keep == 0) {
+		$rejectlist->{$name} = 1;
+	    }
+	}
     }
 }
 close $fh;
@@ -32,7 +45,7 @@ print <<EOF;
     <rejectfont>
 EOF
 
-foreach my $name (keys %$rejectlist) {
+foreach my $name (sort keys %$rejectlist) {
     print <<EOF;
       <pattern>
         <patelt name="family">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment