def createFontDict(fontfiles, fontext='ttf'): """A function to create a dictionary of font file paths. The default is to create a dictionary for TrueType fonts. An AFM font dictionary can optionally be created. """ fontdict = {} # Add fonts from list of known font files. seen = {} for fpath in fontfiles: verbose.report('createFontDict: %s' % (fpath), 'debug') fname = fpath.split('/')[-1] if seen.has_key(fname): continue else: seen[fname] = 1 if fontext == 'afm': try: font = afm.AFM(file(fpath)) except RuntimeError: warnings.warn("Could not open font file %s" % fpath) continue prop = afmFontProperty(font) else: try: font = ft2font.FT2Font(str(fpath)) except RuntimeError: warnings.warn("Could not open font file %s" % fpath) continue except UnicodeError: warnings.warn("Cannot handle unicode filenames") #print >> sys.stderr, 'Bad file is', fpath continue try: prop = ttfFontProperty(font) except: continue add_filename(fontdict, prop, fpath) # !!!! Default font algorithm needs improvement if prop.name.lower() in ['bitstream vera serif', 'times']: prop.name = 'serif' add_filename(fontdict, prop, fpath) elif prop.name.lower() in ['bitstream vera sans', 'helvetica']: prop.name = 'sans-serif' add_filename(fontdict, prop, fpath) elif prop.name.lower() in ['zapf chancery', 'itc zapf chancery']: prop.name = 'cursive' add_filename(fontdict, prop, fpath) elif prop.name.lower() in ['western', 'itc avant garde gothic']: prop.name = 'fantasy' add_filename(fontdict, prop, fpath) elif prop.name.lower() in ['bitstream vera sans mono', 'courier']: prop.name = 'monospace' add_filename(fontdict, prop, fpath) return fontdict
def createFontList(fontfiles, fontext='ttf'): """ A function to create a font lookup list. The default is to create a list of TrueType fonts. An AFM font list can optionally be created. """ fontlist = [] # Add fonts from list of known font files. seen = {} for fpath in fontfiles: verbose.report('createFontDict: %s' % (fpath), 'debug') fname = os.path.split(fpath)[1] if fname in seen: continue else: seen[fname] = 1 if fontext == 'afm': try: fh = open(fpath, 'rb') except: verbose.report("Could not open font file %s" % fpath) continue try: try: font = afm.AFM(fh) finally: fh.close() except RuntimeError: verbose.report("Could not parse font file %s" % fpath) continue try: prop = afmFontProperty(fpath, font) except KeyError: continue else: try: font = ft2font.FT2Font(fpath) except RuntimeError: verbose.report("Could not open font file %s" % fpath) continue except UnicodeError: verbose.report("Cannot handle unicode filenames") # print >> sys.stderr, 'Bad file is', fpath continue except IOError: verbose.report("IO error - cannot open font file %s" % fpath) continue try: prop = ttfFontProperty(font) except (KeyError, RuntimeError, ValueError): continue fontlist.append(prop) return fontlist
def createFontList(fontfiles, fontext='ttf'): """ A function to create a font lookup list. The default is to create a list of TrueType fonts. An AFM font list can optionally be created. """ fontlist = [] # Add fonts from list of known font files. seen = set() for fpath in fontfiles: _log.debug('createFontDict: %s' % (fpath)) fname = os.path.split(fpath)[1] if fname in seen: continue else: seen.add(fname) if fontext == 'afm': try: fh = open(fpath, 'rb') except EnvironmentError: _log.info("Could not open font file %s", fpath) continue try: font = afm.AFM(fh) except RuntimeError: _log.info("Could not parse font file %s", fpath) continue finally: fh.close() try: prop = afmFontProperty(fpath, font) except KeyError: continue else: try: font = ft2font.FT2Font(fpath) except RuntimeError: _log.info("Could not open font file %s", fpath) continue except UnicodeError: _log.info("Cannot handle unicode filenames") continue except IOError: _log.info("IO error - cannot open font file %s", fpath) continue try: prop = ttfFontProperty(font) except (KeyError, RuntimeError, ValueError): continue fontlist.append(prop) return fontlist
def createFontList(fontfiles, fontext='ttf'): """ Create a font lookup list. The default is to create a list of TrueType fonts. An AFM font list can optionally be created. """ fontlist = [] # Add fonts from list of known font files. seen = set() for fpath in fontfiles: _log.debug('createFontDict: %s', fpath) fname = os.path.split(fpath)[1] if fname in seen: continue if fontext == 'afm': try: with open(fpath, 'rb') as fh: font = afm.AFM(fh) except EnvironmentError: _log.info("Could not open font file %s", fpath) continue except RuntimeError: _log.info("Could not parse font file %s", fpath) continue try: prop = afmFontProperty(fpath, font) except KeyError as exc: _log.info("Could not extract properties for %s: %s", fpath, exc) continue else: try: font = ft2font.FT2Font(fpath) except (OSError, RuntimeError) as exc: _log.info("Could not open font file %s: %s", fpath, exc) continue except UnicodeError: _log.info("Cannot handle unicode filenames") continue try: prop = ttfFontProperty(font) except (KeyError, RuntimeError, ValueError, NotImplementedError) as exc: _log.info("Could not extract properties for %s: %s", fpath, exc) continue fontlist.append(prop) seen.add(fname) return fontlist
def createFontDict(fontfiles, fontext='ttf'): """ A function to create a dictionary of font file paths. The default is to create a dictionary for TrueType fonts. An AFM font dictionary can optionally be created. """ fontdict = {} # Add fonts from list of known font files. seen = {} for fpath in fontfiles: verbose.report('createFontDict: %s' % (fpath), 'debug') fname = os.path.split(fpath)[1] if seen.has_key(fname): continue else: seen[fname] = 1 if fontext == 'afm': try: fh = open(fpath, 'r') except: verbose.report("Could not open font file %s" % fpath) continue try: try: font = afm.AFM(fh) finally: fh.close() except RuntimeError: verbose.report("Could not parse font file %s" % fpath) continue prop = afmFontProperty(font) else: try: font = ft2font.FT2Font(str(fpath)) except RuntimeError: verbose.report("Could not open font file %s" % fpath) continue except UnicodeError: verbose.report("Cannot handle unicode filenames") #print >> sys.stderr, 'Bad file is', fpath continue try: prop = ttfFontProperty(font) except: continue add_filename(fontdict, prop, fpath) return fontdict
def addfont(self, path): """ Cache the properties of the font at *path* to make it available to the `FontManager`. The type of font is inferred from the path suffix. Parameters ---------- path : str or path-like """ if Path(path).suffix.lower() == ".afm": with open(path, "rb") as fh: font = afm.AFM(fh) prop = afmFontProperty(path, font) self.afmlist.append(prop) else: font = ft2font.FT2Font(path) prop = ttfFontProperty(font) self.ttflist.append(prop)
def addfont(self, path): """ Cache the properties of the font at *path* to make it available to the `FontManager`. The type of font is inferred from the path suffix. Parameters ---------- path : str or path-like """ if Path(path).suffix.lower() == ".afm": try: with open(path, "rb") as fh: font = afm.AFM(fh) except EnvironmentError: _log.info("Could not open font file %s", path) return except RuntimeError: _log.info("Could not parse font file %s", path) return try: prop = afmFontProperty(path, font) except KeyError as exc: _log.info("Could not extract properties for %s: %s", path, exc) return self.afmlist.append(prop) else: try: font = ft2font.FT2Font(path) except (OSError, RuntimeError) as exc: _log.info("Could not open font file %s: %s", path, exc) return except UnicodeError: _log.info("Cannot handle unicode filenames") return try: prop = ttfFontProperty(font) except (KeyError, RuntimeError, ValueError, NotImplementedError) as exc: _log.info("Could not extract properties for %s: %s", path, exc) return self.ttflist.append(prop)
def test_get_familyname_guessed(): fh = BytesIO(AFM_TEST_DATA) fm = afm.AFM(fh) del fm._header[b'FamilyName'] # remove FamilyName, so we have to guess assert fm.get_familyname() == 'My Font'
def test_font_manager_weight_normalization(): font = afm.AFM( BytesIO(AFM_TEST_DATA.replace(b"Weight Bold\n", b"Weight Custom\n"))) assert fm.afmFontProperty("", font).weight == "normal"