Skip to content

Instantly share code, notes, and snippets.

@wzyboy
Last active April 28, 2017 21:02
Show Gist options
  • Save wzyboy/5b387fdac30937d5ea776ab6623758aa to your computer and use it in GitHub Desktop.
Save wzyboy/5b387fdac30937d5ea776ab6623758aa to your computer and use it in GitHub Desktop.
mpv fc-match: A quick and dirty way to determine whether you have all the fonts required in an ass file
#!/usr/bin/env python
import os
import argparse
import subprocess
def get_fonts_from_ass_file(ass_file):
try:
with open(ass_file) as f:
ass = f.readlines()
except UnicodeDecodeError:
with open(ass_file, encoding='utf-16') as f:
ass = f.readlines()
# Format: Name, Fontname, Fontsize, ...
fonts = set()
for line in ass:
line = line.strip()
if not line.startswith('Style:'):
continue
fontname = line.split(',')[1]
fontname = fontname.strip('@')
fonts.add(fontname)
return fonts
def fc_match(fontname, fonts_conf=None):
# FONTCONFIG_FILE could only be absolute path
if not fonts_conf.startswith('/'):
fonts_conf = os.path.abspath(os.path.expanduser(fonts_conf))
# fc-match escapes all '-' chars internally,
# we also need to escape them, or no results would return
fontname = fontname.replace('-', r'\-')
cmd = ['fc-match', fontname, 'file', 'fullname']
env = os.environ.copy()
env['FONTCONFIG_FILE'] = fonts_conf
try:
_output = subprocess.check_output(cmd, env=env)
except subprocess.CalledProcessError:
_output = ':ERROR:ERROR'
# :fullname=Noto Sans:file=/usr/share/fonts/noto/NotoSans-Regular.ttc
output = _output.decode('utf-8').strip().split(':')
pretty_output = '{} => \t{}, {}'.format(fontname, output[2], output[1]).replace('\\', '')
print(pretty_output)
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument('-c', '--fonts-conf', default='~/.config/mpv/fonts.conf')
argparser.add_argument('ass', nargs='+')
args = argparser.parse_args()
fonts = set()
for a in args.ass:
_fonts = get_fonts_from_ass_file(a)
fonts = fonts | _fonts
for font in fonts:
fc_match(font, args.fonts_conf)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment