font-reduce.py: Improve ranges/targets error handling and logging
authorW. Trevor King <wking@tremily.us>
Wed, 9 Jan 2013 18:20:36 +0000 (13:20 -0500)
committerW. Trevor King <wking@tremily.us>
Wed, 9 Jan 2013 18:21:03 +0000 (13:21 -0500)
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

index 26bad5985b86b5d54d4b5a3b1da6c75eb86d6304..f65d98323662fc0d9365db685229c20020554e54 100755 (executable)
@@ -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