def get_designsize(fontname): """ Function (tries to) determine designsize of given font. First checks a cache (loaded from setup.font_lookup), then seeks for corresponding TFM file and reads designsize. """ if fontname in config.fonts_lookup: encoding, designsize = config.fonts_lookup[fontname] log.debug("Getting designsize for '%s' from file cache" % fontname) return designsize # read TFM, if any log.debug('Checking TFM file...') filename = findfile.locate(fontname + '.tfm', setup.tex_paths) if filename: log.debug("Using file '%s'" % filename) file = binfile(filename, 'rb') try: _, designsize, _, _ = read_TFM(file) except TFMError, e: log.error("TFM error" + str(TFMError)) else: file.close() return designsize / (2.0**20)
def get_encoding_from_TFM(fontname): log.debug('checking TFM file') filename = findfile.locate(fontname + '.tfm', setup.tex_paths) if filename: log.debug("... using '%s'" % filename) file = binfile(filename, 'rb') try: _, _, encodingname, _ = read_TFM(file) except TFMError, e: log.error("... TFM error: %s" % str(TFMError)) else: file.close() try: encoding = config.encoding_lookup[encodingname] log.debug("... encoding %s" % encoding) return encoding except KeyError: log.error("... font %s: unknown TeX encoding: '%s'" % (fontname, encodingname))
def filefactory(path, log=None, **kwargs): # TODO: move this elsewhere and make it more easily configurable basename = os.path.basename(path) try: if basename.endswith('.txt'): with open(path, 'rb') as txt: if txt.readline().strip() == os.path.splitext(basename)[0]: txt.seek(0) return rawfile.rawfile(path=path, file=txt, **kwargs) elif basename in binnames: return binfile.binfile(path=path, **kwargs) elif basename in rawnames: return rawfile.rawfile(path=path, file=txt, **kwargs) except Exception as e: if log: log.warning('Failed to read file from path %s. Defauling to reading as a reffile, which should (hopefully) work despite.' % path) log.debug(traceback.format_exc()) return reffile.reffile(path=path, **kwargs)