From: W. Trevor King Date: Sat, 10 Mar 2012 23:55:59 +0000 (-0500) Subject: Add --range option to font-reduce.py. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3d77e31ed92dfabb07890db97fb8dc0f371fadfa;p=blog.git Add --range option to font-reduce.py. --- diff --git a/posts/font-reduce/font-reduce.py b/posts/font-reduce/font-reduce.py index d4d8c40..a58befa 100755 --- a/posts/font-reduce/font-reduce.py +++ b/posts/font-reduce/font-reduce.py @@ -30,16 +30,17 @@ import fontforge as _fontforge __version__ = '0.1' -def convert(input_file, output_file=None, **kwargs): +def convert(input_file, output_file=None, ranges=[(0x20,0x7e)], **kwargs): if output_file is None: base,ext = _os_path.splitext(input_file) output_file = '{}-reduced.woff'.format(base) i = _fontforge.open(input_file) - i.selection.select(("ranges",), 0x20, 0x7e) - i.copy() o = _fontforge.font() - o.selection.select(("ranges",), 0x20, 0x7e) - o.paste() + for start,stop in ranges: + i.selection.select(("ranges",), start, stop) + i.copy() + o.selection.select(("ranges",), start, stop) + o.paste() for attr in ['comment', 'copyright', 'encoding', 'familyname', 'fontname', 'fullname', 'sfntRevision', 'sfnt_names', 'userdata', 'version', 'woffMetadata']: @@ -110,6 +111,10 @@ if __name__ == '__main__': '-l', '--license', help='override font license text') parser.add_argument( '-s', '--source', help='override font source URL') + parser.add_argument( + '-r', '--range', action='append', default=['0x20,0x7e'], + help=('add an additional range of indices to copy to the reduced font ' + "(e.g '0x7f,0xff')")) parser.add_argument( '-o', '--output', help='override path to output file') parser.add_argument( @@ -117,6 +122,11 @@ if __name__ == '__main__': args = parser.parse_args() + ranges = [] + for r in args.range: + start,stop = [int(x, 0) for x in r.split(',')] + ranges.append((start,stop)) + for font in args.font: - convert(font, output_file=args.output, license=args.license, - source=args.source) + convert(font, output_file=args.output, ranges=ranges, + license=args.license, source=args.source)