Beispiel #1
0
def register_font_path(font_path):
    # Caching seems to cause problems. Disable for now
    ff = fontfinder.FontFinder(useCache=False)
    ff.addDirectory(font_path, recur=True)

    try:
        ff.search()
    except (KeyError, Exception) as fferror:
        logging.warning("Problem parsing font: {}".format(fferror))

    for family_name in ff.getFamilyNames():
        fonts_in_family = ff.getFontsInFamily(family_name)
        for font in fonts_in_family:
            if len(fonts_in_family) == 1:
                try:
                    ttfont = TTFont(family_name.decode("utf-8"), font.fileName)
                    pdfmetrics.registerFont(ttfont)
                    pdfmetrics.registerFontFamily(family_name)
                except TTFError as e:
                    logging.warning("Could not register font {}, {}".format(
                        family_name, e))
                    continue
            elif len(fonts_in_family) > 1:
                '''If font family has multiple weights/styles'''
                font_name = family_name + "-".encode() + font.styleName
                font_name = font_name.decode("utf-8")
                try:
                    ttfont = TTFont(font_name, font.fileName)
                    pdfmetrics.registerFont(ttfont)
                    addMapping(font.familyName, font.isBold, font.isItalic,
                               font_name)
                except TTFError as e:
                    logging.warning("Could not register font {}, {}".format(
                        family_name, e))
                    continue
Beispiel #2
0
 def test16(self):
     from reportlab.lib import fontfinder
     ff = fontfinder.FontFinder(useCache=False, recur=True)
     ff.addDirectories(rl_config.T1SearchPath + rl_config.TTFSearchPath)
     ff.search()
     ff.getFamilyNames()