From 564c42933275fe8c8863381e0c7d12c2da15a4ff Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Sun, 20 Nov 2016 15:40:20 +0100 Subject: [PATCH] put get_fonts() in font_helpers.py --- brother_ql_web.py | 50 +++++------------------------------------------ font_helpers.py | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 45 deletions(-) create mode 100755 font_helpers.py diff --git a/brother_ql_web.py b/brother_ql_web.py index 876fcfb..d6e7a79 100755 --- a/brother_ql_web.py +++ b/brother_ql_web.py @@ -15,19 +15,16 @@ from brother_ql.devicedependent import models from brother_ql import BrotherQLRaster, create_label from brother_ql.backends import backend_factory, guess_backend -MODEL = None -BACKEND_CLASS = None -BACKEND_STRING_DESCR = None +from font_helpers import get_fonts logger = logging.getLogger(__name__) -# globals: DEBUG = False - +MODEL = None +BACKEND_CLASS = None +BACKEND_STRING_DESCR = None FONTS = None - DEFAULT_FONT = None - DEFAULT_FONTS = [ {'family': 'Minion Pro', 'style': 'Semibold'}, {'family': 'Linux Libertine', 'style': 'Regular'}, @@ -42,43 +39,6 @@ def index(): """ return markdown.markdown(INDEX_MD) -def get_fonts(folder=None): - """ - Scan a folder (or the system) for .ttf / .otf fonts and - return a dictionary of the structure family -> style -> file path - """ - fonts = {} - if folder: - cmd = ['fc-scan', '--format', '"%{file}:%{family}:style=%{style}\n"', folder] - else: - cmd = ['fc-list', ':', 'file', 'family', 'style'] - for line in subprocess.check_output(cmd).decode('utf-8').split("\n"): - logger.debug(line) - line.strip() - if not line: continue - if 'otf' not in line and 'ttf' not in line: continue - parts = line.split(':') - path = parts[0] - families = parts[1].strip().split(',') - styles = parts[2].split('=')[1].split(',') - if len(families) == 1 and len(styles) > 1: - families = [families[0]] * len(styles) - elif len(families) > 1 and len(styles) == 1: - styles = [styles[0]] * len(families) - if len(families) != len(styles): - if DEBUG: - print("Problem with this font:") - print(line) - continue - for i in range(len(families)): - try: fonts[families[i]] - except: fonts[families[i]] = dict() - fonts[families[i]][styles[i]] = path - if DEBUG: - print("Added this font:") - print(families[i], styles[i], path) - return fonts - @route('/api/print/text') @route('/api/print/text/') @route('/api/print/text/') @@ -187,7 +147,7 @@ def main(): sys.stderr.write('Could not find any of the default fonts') sys.exit() - run(host='', port=args.port, debug=args.loglevel==logging.DEBUG) + run(host='', port=args.port, debug=DEBUG) if __name__ == "__main__": main() diff --git a/font_helpers.py b/font_helpers.py new file mode 100755 index 0000000..a1bdbdd --- /dev/null +++ b/font_helpers.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import logging + +logger = logging.getLogger(__name__) + +def get_fonts(folder=None): + """ + Scan a folder (or the system) for .ttf / .otf fonts and + return a dictionary of the structure family -> style -> file path + """ + fonts = {} + if folder: + cmd = ['fc-scan', '--format', '"%{file}:%{family}:style=%{style}\n"', folder] + else: + cmd = ['fc-list', ':', 'file', 'family', 'style'] + for line in subprocess.check_output(cmd).decode('utf-8').split("\n"): + logger.debug(line) + line.strip() + if not line: continue + if 'otf' not in line and 'ttf' not in line: continue + parts = line.split(':') + path = parts[0] + families = parts[1].strip().split(',') + styles = parts[2].split('=')[1].split(',') + if len(families) == 1 and len(styles) > 1: + families = [families[0]] * len(styles) + elif len(families) > 1 and len(styles) == 1: + styles = [styles[0]] * len(families) + if len(families) != len(styles): + logger.debug("Problem with this font: " + line) + continue + for i in range(len(families)): + try: fonts[families[i]] + except: fonts[families[i]] = dict() + fonts[families[i]][styles[i]] = path + logger.debug("Added this font: " + str((families[i], styles[i], path))) + return fonts