From c2991d893d4e97b3dfd46d8bc01ff7ddb024abc8 Mon Sep 17 00:00:00 2001 From: John Wiggins Date: Wed, 3 Mar 2021 17:11:12 +0100 Subject: [PATCH] Fix set_font() for PDF and SVG backends --- kiva/pdf.py | 18 ++++++++++++++++-- kiva/svg.py | 8 +------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/kiva/pdf.py b/kiva/pdf.py index 01b11564e..df87bc415 100644 --- a/kiva/pdf.py +++ b/kiva/pdf.py @@ -19,12 +19,14 @@ # standard library imports import copy +import os import warnings from numpy import ndarray, pi # ReportLab PDF imports -import reportlab.pdfbase.pdfmetrics +from reportlab.pdfbase import pdfmetrics +from reportlab.pdfbase.ttfonts import TTFont import reportlab.pdfbase._fontdata from reportlab.pdfgen import canvas @@ -35,6 +37,7 @@ from .constants import FILL, STROKE, EOF_FILL import kiva.constants as constants import kiva.affine as affine +from kiva.fonttools.font import Font cap_style = {} @@ -621,7 +624,18 @@ def set_font(self, font): face_name = font.face_name if face_name == "": face_name = "Helvetica" - self.gc.setFont(face_name, font.size) + + try: + self.gc.setFont(face_name, font.size) + except KeyError: + # Face name is not recognized. + # Register the TTF font that kiva's font_manager located + filename = font.findfont() + if not os.path.splitext(filename)[-1] in (".ttf", ".ttc"): + raise + + pdfmetrics.registerFont(TTFont(face_name, filename)) + self.gc.setFont(face_name, font.size) def get_font(self): """ Get the current font """ diff --git a/kiva/svg.py b/kiva/svg.py index 575fd1321..cc44d7050 100644 --- a/kiva/svg.py +++ b/kiva/svg.py @@ -112,7 +112,6 @@ def default_filter(kw1): """ -font_map = {"Arial": "Helvetica"} try: # expensive way of computing string widths import reportlab.pdfbase.pdfmetrics as pdfmetrics @@ -125,8 +124,6 @@ def default_filter(kw1): _reportlab_loaded = 0 -font_face_map = {"Arial": "Helvetica", "": "Helvetica"} - # This backend has no compiled path object, yet. class CompiledPath(object): @@ -185,10 +182,7 @@ def save(self, filename): # Text handling code def set_font(self, font): - self.face_name = font_face_map.get(font.face_name, font.face_name) - self.font = pdfmetrics.Font( - self.face_name, self.face_name, pdfmetrics.defaultEncoding - ) + self.face_name = font.face_name self.font_size = font.size # actual implementation =)