Last active
August 30, 2018 07:51
-
-
Save ph1ee/159bc03000fc2fd6b86ae20ba0f0626f to your computer and use it in GitHub Desktop.
Find the fonts containing the code point(s) with python-fontconfig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python2 | |
| # https://unix.stackexchange.com/a/268286/247080 | |
| import re | |
| import sys | |
| import fontconfig | |
| if len(sys.argv) < 1: | |
| print('''Usage: ''' + sys.argv[0] + '''CHARS [REGEX] | |
| Print the names of available fonts containing the code point(s) CHARS. | |
| If CHARS contains multiple characters, they must all be present. | |
| Alternatively you can use U+xxxx to search for a single character with | |
| code point xxxx (hexadecimal digits). | |
| If REGEX is specified, the font name must match this regular expression.''') | |
| sys.exit(0) | |
| characters = sys.argv[1] | |
| if characters.startswith('U+'): | |
| characters = unichr(int(characters[2:], 16)) | |
| else: | |
| characters = characters.decode(sys.stdout.encoding) | |
| regexp = re.compile(sys.argv[2] if len(sys.argv) > 2 else '') | |
| font_names = fontconfig.query() | |
| found = False | |
| for name in font_names: | |
| if not re.search(regexp, name): | |
| continue | |
| font = fontconfig.FcFont(name) | |
| if all(font.has_char(c) for c in characters): | |
| print(name) | |
| found = True | |
| sys.exit(0 if found else 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment