From 667a9bfba9c61e75e39cfb9a5b442c4a189cf006 Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Sat, 21 Dec 2019 10:51:39 +0100 Subject: [PATCH] copy fonts to a dir scanned by fontconfig instead of manually injecting configs --- screenless/bureau/bureau.py | 28 +++++++++++++--------------- screenless/mgmt.py | 3 +++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/screenless/bureau/bureau.py b/screenless/bureau/bureau.py index f89ec2c..0b7f882 100644 --- a/screenless/bureau/bureau.py +++ b/screenless/bureau/bureau.py @@ -7,6 +7,7 @@ import json import logging import os.path import random +import shutil import string import subprocess import tempfile @@ -166,7 +167,6 @@ class Bureau(object): self.commands = {} self.api = {} - modpath = os.path.dirname(__file__) mypath = inspect.getfile(self.__class__) self.mdir = os.path.dirname(mypath) @@ -175,6 +175,17 @@ class Bureau(object): os.mkdir(basepath) os.chdir(basepath) + # make sure fonts are copied + fontpath = os.path.join(basepath, "fonts") + fontsrc = os.path.join(self.mdir, "fonts") + if os.path.exists(fontsrc): + fontlist = glob.glob(os.path.join(fontsrc, "*.otf")) + fontlist += glob.glob(os.path.join(fontsrc, "*.ttf")) + for fontfile in fontlist: + fontbase = os.path.basename(fontfile) + if not os.path.exists(os.path.join(fontpath, fontbase)): + shutil.copy(fontfile, fontpath) + self.load_config() # setup log file @@ -355,20 +366,7 @@ class Bureau(object): pdfpath = tempfile.mkstemp(".pdf")[1] self.log.debug("rendering to: " + pdfpath) - # manually add local fonts - fontconfig = FontConfiguration() - fontsdir = os.path.join(self.mdir, "fonts") - fontlist = [] - for font in glob.glob(fontsdir + "/*.otf"): - fontlist.append(("local", "file://" + font)) - self.log.debug("adding font " + font) - for font in glob.glob(fontsdir + "/*.ttf"): - fontlist.append(("local", "file://" + font)) - self.log.debug("adding font " + font) - fdict = { "src": fontlist } - fontconfig.add_font_face(fdict, - weasyprint.default_url_fetcher) - weasyprint.HTML(string=templ.render_unicode(**kwargs)).write_pdf(pdfpath, font_config=fontconfig) + weasyprint.HTML(string=templ.render_unicode(**kwargs)).write_pdf(pdfpath) subprocess.call("lpr -o sides=two-sided-long-edge -o InputSlot=Upper " + pdfpath, shell=True) # TODO: make this asynchronous diff --git a/screenless/mgmt.py b/screenless/mgmt.py index 496a116..41af9ab 100644 --- a/screenless/mgmt.py +++ b/screenless/mgmt.py @@ -23,6 +23,9 @@ class Management: if not os.path.exists(basepath): os.mkdir(basepath) os.chdir(basepath) + fontpath = os.path.join(basepath, "fonts") + if not os.path.exists(fontpath): + os.mkdir(fontpath) self.procs = {} self.org_chart = []