Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions kiva/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = {}
Expand Down Expand Up @@ -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))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this embeds the font in the PDF file. I'm not sure if we want that or not.

PDF has a set of "safe" fonts which come with Acrobat Reader. I think maybe what we want to do in this method is try to map all requested fonts to the nearest match in this list.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this embeds the font in the PDF file.

Based off the link below, I believe this is true as well.

PDF has a set of "safe" fonts which come with Acrobat Reader. I think maybe what we want to do in this method is try to map all requested fonts to the nearest match in this list.

I am unsure about this / why it would be necessary, but my understanding of all this is very much not complete / still growing. When would embedding the font in the PDF file not work or be problematic?

ref: https://www.reportlab.com/docs/reportlab-userguide.pdf
pg. 28 and 49-52

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would embedding the font in the PDF file not work or be problematic?

You're right. It's probably OK.

self.gc.setFont(face_name, font.size)

def get_font(self):
""" Get the current font """
Expand Down
8 changes: 1 addition & 7 deletions kiva/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def default_filter(kw1):
</html>
"""

font_map = {"Arial": "Helvetica"}
try:
# expensive way of computing string widths
import reportlab.pdfbase.pdfmetrics as pdfmetrics
Expand All @@ -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):
Expand Down Expand Up @@ -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 =)
Expand Down