Skip to content

Instantly share code, notes, and snippets.

@mwgamera
Last active January 5, 2021 12:00
Show Gist options
  • Select an option

  • Save mwgamera/364c4193fb54225b446a to your computer and use it in GitHub Desktop.

Select an option

Save mwgamera/364c4193fb54225b446a to your computer and use it in GitHub Desktop.
List all fonts known to fontconfig that provide glyph for given characters.
#!/usr/bin/env python3
# klg, May 2014
import fontconfig
import sys
chars = list(''.join(sys.argv[1:]))
if not len(chars):
sys.exit(0)
fonts = fontconfig.query()
def progress(i):
message = '\rSearching fonts ... {0} / {1} \033[K'
sys.stderr.write(message.format(i, len(fonts)))
sys.stderr.flush()
def clear():
sys.stderr.write('\r\033[K')
sys.stderr.flush()
for i in range(len(fonts)):
if i % 17 == 0:
progress(i)
x = [ch for ch in chars if fonts[i].has_char(ch)]
if len(x):
x = ' '.join(['{:04X}'.format(ord(ch)) for ch in x])
path = fonts[i].file
clear()
print('{}: {}'.format(x, path))
progress(i)
clear()
@mwgamera
Copy link
Copy Markdown
Author

mwgamera commented Jan 5, 2021

I stopped using this script long time ago when I realized that fontconfig cache can be queried much more effectively using fc-list ':charset=XXXX' where XXXX is a hexadecimal Unicode code point, or a comma-separated list of such.

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