def buildTable( filenames=None, failureCallback=None ): """Build table mapping {family:(font:{modifiers:(name,file)})} filenames -- if provided, list of filenames to scan, otherwise the full set of system fonts provided by findsystem will be used. failureCallback -- if provided, a function taking three arguments, the failing filename, an error-type code, and the error object. If processing should stop, raise an error. codes: 0 -- couldn't open the font file 1 -- couldn't find modifiers in the font file 2 -- couldn't find font-name in the font file 3 -- couldn't find the generic family specifier for the font """ if filenames is None: filenames = findsystem.findFonts() table = {} for filename in filenames: try: font = describe.openFont(filename) except Exception as err: if failureCallback: failureCallback( filename, 0, err ) else: try: modifiers = describe.modifiers( font ) except (KeyError,AttributeError) as err: if failureCallback: failureCallback( filename, 1, err ) modifiers = (None,None) try: specificName, fontName = describe.shortName( font ) except (KeyError,AttributeError) as err: if failureCallback: failureCallback( filename, 2, err ) else: try: specifier = describe.family(font) except KeyError: if failureCallback: failureCallback( filename, 3, err ) else: table.setdefault( specifier, {} ).setdefault( fontName, {} )[modifiers] = (specificName,filename) return table
def scan( self, paths=None, printErrors=0, force = 0 ): """Scan the given paths registering each found font""" new, failed = [],[] for filename in findsystem.findFonts(paths): try: self.register( filename, force = force ) except Exception, err: log.info( 'Failure scanning %s', filename ) if printErrors: log.warn( "%s", traceback.format_exc()) failed.append( filename ) else: new.append( filename )
def scan(self, paths=None, printErrors=0, force=0): """Scan the given paths registering each found font""" new, failed = [], [] for filename in findsystem.findFonts(paths): try: self.register(filename, force=force) except Exception as err: if printErrors: traceback.print_exc() failed.append(filename) else: new.append(filename) return new, failed
def scan( self, paths=None, printErrors=0, force = 0 ): """Scan the given paths registering each found font""" new, failed = [],[] for filename in findsystem.findFonts(paths): try: self.register( filename, force = force ) except Exception as err: if printErrors: traceback.print_exc() failed.append( filename ) else: new.append( filename ) return new, failed
def scan(self, paths=None, printErrors=0, force=0): """Scan the given paths registering each found font""" new, failed = [], [] for filename in findsystem.findFonts(paths): try: self.register(filename, force=force) except Exception, err: log.info('Failure scanning %s', filename) if printErrors: log.warn("%s", traceback.format_exc()) failed.append(filename) else: new.append(filename)
def buildTable(filenames=None, failureCallback=None): """Build table mapping {family:(font:{modifiers:(name,file)})} filenames -- if provided, list of filenames to scan, otherwise the full set of system fonts provided by findsystem will be used. failureCallback -- if provided, a function taking three arguments, the failing filename, an error-type code, and the error object. If processing should stop, raise an error. codes: 0 -- couldn't open the font file 1 -- couldn't find modifiers in the font file 2 -- couldn't find font-name in the font file 3 -- couldn't find the generic family specifier for the font """ if filenames is None: filenames = findsystem.findFonts() table = {} for filename in filenames: try: font = describe.openFont(filename) except Exception as err: if failureCallback: failureCallback(filename, 0, err) else: try: modifiers = describe.modifiers(font) except (KeyError, AttributeError) as err: if failureCallback: failureCallback(filename, 1, err) modifiers = (None, None) try: specificName, fontName = describe.shortName(font) except (KeyError, AttributeError) as err: if failureCallback: failureCallback(filename, 2, err) else: try: specifier = describe.family(font) except KeyError: if failureCallback: failureCallback(filename, 3, err) else: table.setdefault(specifier, {}).setdefault( fontName, {})[modifiers] = (specificName, filename) return table
def main(): import time t = time.clock() if sys.argv[1:]: directories = sys.argv[1:] files = findsystem.findFonts(directories) else: files = None table = buildTable(files, failureCallback=interactiveCallback) t = time.clock()-t keys = table.keys() keys.sort() for fam in keys: print '_________________________' print fam fnts = table[fam].items() fnts.sort() for fnt,modset in fnts: mods = modset.keys() mods.sort() mods = ",".join([ '%s%s'%( w, ['','(I)'][i&1]) for (w,i) in mods]) print ' ',fnt.ljust(32), '--', mods log.info( 'Scan took %s seconds CPU time', t )
def main(): import time t = time.clock() if sys.argv[1:]: directories = sys.argv[1:] files = findsystem.findFonts(directories) else: files = None table = buildTable(files, failureCallback=interactiveCallback) t = time.clock() - t keys = table.keys() keys.sort() for fam in keys: print '_________________________' print fam fnts = table[fam].items() fnts.sort() for fnt, modset in fnts: mods = modset.keys() mods.sort() mods = ",".join( ['%s%s' % (w, ['', '(I)'][i & 1]) for (w, i) in mods]) print ' ', fnt.ljust(32), '--', mods log.info('Scan took %s seconds CPU time', t)
def interactiveCallback(file, code, err): """Simple error callback for interactive use""" sys.stderr.write('ERR on file %r (code %s):\n' % ( file, code, )) traceback.print_exc(file=sys.stderr) if __name__ == "__main__": import time t = time.clock() if sys.argv[1:]: directories = sys.argv[1:] files = findsystem.findFonts(directories) else: files = None table = buildTable(files, failureCallback=interactiveCallback) t = time.clock() - t keys = list(table.keys()) keys.sort() for fam in keys: print('_________________________') print(fam) fnts = list(table[fam].items()) fnts.sort() for fnt, modset in fnts: mods = list(modset.keys()) mods.sort() mods = ",".join(
"""Simple error callback for interactive use""" sys.stderr.write( 'ERR on file %r (code %s):\n'%( file, code, ) ) traceback.print_exc( file=sys.stderr ) if __name__ == "__main__": import time t = time.clock() if sys.argv[1:]: directories = sys.argv[1:] files = findsystem.findFonts(directories) else: files = None table = buildTable(files, failureCallback=interactiveCallback) t = time.clock()-t keys = list(table.keys()) keys.sort() for fam in keys: print('_________________________') print(fam) fnts = list(table[fam].items()) fnts.sort() for fnt,modset in fnts: mods = list(modset.keys()) mods.sort() mods = ",".join([ '%s%s'%( w, ['','(I)'][i&1]) for (w,i) in mods])
def main(): global verbosity parser = argparse.ArgumentParser(description='Convert images to ASCII art.') parser.add_argument('image', type=str, help='file name of image to be converted.') parser.add_argument('--out', type=str, help='name of file to write to.') parser.add_argument('--aspect', type=float, help='aspect ratio of terminal font (text only).') parser.add_argument('--font', type=str, help='file name of font to be used.') parser.add_argument('--paper', type=str, choices=Asciifier.PAPER_CHOICES, help='paper size (PDF only).') parser.add_argument('--orientation', type=str, choices=Asciifier.ORIENTATION_CHOICES, help='paper orientation (PDF only).') parser.add_argument('--resolution', type=int, help='number of characters per line.') parser.add_argument('--fontscale', type=float, help='factor to scale font by (PDF only).') parser.add_argument('--colorize', nargs='?', const=True, help='generate colored output instead of b/w (PDF only).') parser.add_argument('--cropmarks', nargs='?', const=True, help='draw crop marks (PDF only).') parser.add_argument('--logo', type=str, help='file name of a logo to place on page (PDF only).') parser.add_argument('-v', type=int, help='verbosity level.') args = parser.parse_args() asciifier = Asciifier() if args.v is not None: verbosity = args.v colorize = args.colorize font_scale = 1.0 if args.fontscale is not None: font_scale = float(args.fontscale) aspect_ratio = 2.0 if args.aspect is not None: aspect_ratio = args.aspect resolution = 80 if args.resolution is not None: resolution = args.resolution paper_format = 'a3' if args.paper is not None: paper_format = args.paper orientation = args.orientation cropmarks = args.cropmarks logo = args.logo font_name = args.font if font_name is not None: font_name = font_name.lower() font_paths = filter(lambda font_file: font_file.lower().find(font_name) >= 0, findsystem.findFonts()) if font_paths: font_name = font_paths[0] else: sys.stderr.write('Font "{}" not found\n'.format(font_name)) font_name = None if args.out is not None and args.out.endswith('.pdf'): asciifier.process(args.image, resolution=resolution) result = asciifier.to_pdf(paper_format=paper_format, orientation=orientation, font_name=font_name, font_scale=font_scale, colorize=colorize, cropmarks=cropmarks, logo=logo) else: asciifier.process(args.image, resolution=resolution, aspect_ratio=aspect_ratio) result = asciifier.to_plain_text() if args.out is None: print(result) else: with open(args.out, 'wb+') as f: f.write(result)
def main(): global verbosity parser = argparse.ArgumentParser( description='Convert images to ASCII art.') parser.add_argument('image', type=str, help='file name of image to be converted.') parser.add_argument('--out', type=str, help='name of file to write to.') parser.add_argument('--aspect', type=float, help='aspect ratio of terminal font (text only).') parser.add_argument('--font', type=str, help='file name of font to be used.') parser.add_argument('--paper', type=str, choices=Asciifier.PAPER_CHOICES, help='paper size (PDF only).') parser.add_argument('--orientation', type=str, choices=Asciifier.ORIENTATION_CHOICES, help='paper orientation (PDF only).') parser.add_argument('--resolution', type=int, help='number of characters per line.') parser.add_argument('--fontscale', type=float, help='factor to scale font by (PDF only).') parser.add_argument( '--colorize', nargs='?', const=True, help='generate colored output instead of b/w (PDF only).') parser.add_argument('--cropmarks', nargs='?', const=True, help='draw crop marks (PDF only).') parser.add_argument( '--logo', type=str, help='file name of a logo to place on page (PDF only).') parser.add_argument('-v', type=int, help='verbosity level.') args = parser.parse_args() asciifier = Asciifier() if args.v is not None: verbosity = args.v colorize = args.colorize font_scale = 1.0 if args.fontscale is not None: font_scale = float(args.fontscale) aspect_ratio = 2.0 if args.aspect is not None: aspect_ratio = args.aspect resolution = 80 if args.resolution is not None: resolution = args.resolution paper_format = 'a3' if args.paper is not None: paper_format = args.paper orientation = args.orientation cropmarks = args.cropmarks logo = args.logo font_name = args.font if font_name is not None: font_name = font_name.lower() font_paths = filter( lambda font_file: font_file.lower().find(font_name) >= 0, findsystem.findFonts()) if font_paths: font_name = font_paths[0] else: sys.stderr.write('Font "{}" not found\n'.format(font_name)) font_name = None if args.out is not None and args.out.endswith('.pdf'): asciifier.process(args.image, resolution=resolution) result = asciifier.to_pdf(paper_format=paper_format, orientation=orientation, font_name=font_name, font_scale=font_scale, colorize=colorize, cropmarks=cropmarks, logo=logo) else: asciifier.process(args.image, resolution=resolution, aspect_ratio=aspect_ratio) result = asciifier.to_plain_text() if args.out is None: print(result) else: with open(args.out, 'wb+') as f: f.write(result)