From 758eb197ce0ef2b4f8ef59640ae1115e4bba3803 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 9 Jan 2013 13:20:36 -0500 Subject: [PATCH] font-reduce.py: Improve ranges/targets error handling and logging Now the ranges in the fontlog reflect the actual ranges (instead of being hardcoded to match the defaults). Also raise an explicit error if the number of ranges does not match the number of targets. --- posts/font-reduce/font-reduce.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/posts/font-reduce/font-reduce.py b/posts/font-reduce/font-reduce.py index 26bad59..f65d983 100755 --- a/posts/font-reduce/font-reduce.py +++ b/posts/font-reduce/font-reduce.py @@ -35,13 +35,19 @@ def convert(input_file, output_file=None, ranges=[(0x20,0x7e)], if output_file is None: base,ext = _os_path.splitext(input_file) output_file = '{}-reduced.woff'.format(base) + if len(ranges) != len(targets): + raise ValueError( + 'range/target missmatch: ranges: {}, targets: {}.'.format( + ranges, targets)) i = _fontforge.open(input_file) o = _fontforge.font() + mappings = [] for ((istart,istop),(ostart,ostop)) in zip(ranges, targets): i.selection.select(('ranges',), istart, istop) i.copy() o.selection.select(('ranges',), ostart, ostop) o.paste() + mappings.append((istart,istop,ostart,ostop)) for attr in ['comment', 'copyright', 'encoding', 'familyname', 'fontname', 'fullname', 'sfntRevision', 'sfnt_names', 'userdata', 'version', 'woffMetadata']: @@ -49,10 +55,14 @@ def convert(input_file, output_file=None, ranges=[(0x20,0x7e)], if attr in ['fontname', 'fullname'] and value: value = 'Reduced{}'.format(value) setattr(o, attr, value) - fontlog = ('Reduced to Unicode characters 0x20 through 0x7e by ' + fontlog = ('Reduced to Unicode characters 0x{:x} through 0x{:x} by ' 'font-reduce.py version {}, ' '`http://blog.tremily.us/posts/font-reduce/`.' - ).format(__version__) + ).format(ranges[0][0], ranges[0][1], __version__) + if len(mappings) > 1: + fontlog += '\n\nAdditional ranges: {}'.format(', '.join( + '0x{:x}-0x{:x} -> 0x{:x}-0x{:x}'.format(*mapping) + for mapping in mappings[1:])) if i.fontlog: fontlog = '{}\n\n{}'.format(i.fontlog.rstrip('\n'), fontlog) o.fontlog = fontlog -- 2.26.2