From 8c597b53755c95a7aa4903747f8228075ba56598 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Tue, 19 Jun 2018 14:16:17 +0100 Subject: [PATCH] mutator: round to closest width class this is the mapping between the half of each percentage intervals and the associated usWidthClass: {56.25: 1, 68.75: 2, 81.25: 3, 93.75: 4, 106.25: 5, 118.75: 6, 137.5: 7, 175.0: 8} Notice how wdth=80 (in the adjusted test case) will fall in width class 3, instead of 4, because it is < 81.25, thus closer to the nominal 75 than to 87.5. --- Lib/fontTools/varLib/mutator.py | 20 +++++++------------ .../test_results/Mutator_IUP-instance.ttx | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Lib/fontTools/varLib/mutator.py b/Lib/fontTools/varLib/mutator.py index 821adaec0..ac771e8a2 100644 --- a/Lib/fontTools/varLib/mutator.py +++ b/Lib/fontTools/varLib/mutator.py @@ -20,18 +20,12 @@ import logging log = logging.getLogger("fontTools.varlib.mutator") - -OS2_WIDTH_CLASS_VALUES = { - 50.0: 1, # Ultra-condensed - 62.5: 2, # Extra-condensed - 75.0: 3, # Condensed - 87.5: 4, # Semi-condensed - 100.0: 5, # Medium (normal) - 112.5: 6, # Semi-expanded - 125.0: 7, # Expanded - 150.0: 8, # Extra-expanded - 200.0: 9, # Ultra-expanded -} +# map 'wdth' axis (1..200) to OS/2.usWidthClass (1..9), rounding to closest +OS2_WIDTH_CLASS_VALUES = {} +percents = [50.0, 62.5, 75.0, 87.5, 100.0, 112.5, 125.0, 150.0, 200.0] +for i, (prev, curr) in enumerate(zip(percents[:-1], percents[1:]), start=1): + half = (prev + curr) / 2 + OS2_WIDTH_CLASS_VALUES[half] = i def instantiateVariableFont(varfont, location, inplace=False): @@ -143,7 +137,7 @@ def instantiateVariableFont(varfont, location, inplace=False): if "wdth" in location: wdth = location["wdth"] for percent, widthClass in sorted(OS2_WIDTH_CLASS_VALUES.items()): - if wdth <= percent: + if wdth < percent: varfont["OS/2"].usWidthClass = widthClass break else: diff --git a/Tests/varLib/data/test_results/Mutator_IUP-instance.ttx b/Tests/varLib/data/test_results/Mutator_IUP-instance.ttx index 91a59358c..1800479b1 100755 --- a/Tests/varLib/data/test_results/Mutator_IUP-instance.ttx +++ b/Tests/varLib/data/test_results/Mutator_IUP-instance.ttx @@ -57,7 +57,7 @@ - +