instancer: reuse varLib.set_default_weight_width_slant function

This commit is contained in:
Cosimo Lupo 2019-06-20 15:09:17 +01:00
parent 1726a4a1fc
commit e6b8897f18
No known key found for this signature in database
GPG Key ID: 179A8F0895A02F4F
2 changed files with 1 additions and 105 deletions

View File

@ -752,37 +752,6 @@ def setMacOverlapFlags(glyfTable):
glyph.flags[0] |= flagOverlapSimple
def setDefaultWeightWidthSlant(ttFont, location):
if "wght" in location and "OS/2" in ttFont:
weightClass = otRound(max(1, min(location["wght"], 1000)))
log.info("Setting OS/2.usWidthClass = %s", weightClass)
ttFont["OS/2"].usWeightClass = weightClass
if "wdth" in location:
# map 'wdth' axis (1..200) to OS/2.usWidthClass (1..9), rounding to closest
steps = [50.0, 62.5, 75.0, 87.5, 100.0, 112.5, 125.0, 150.0, 200.0]
n = len(steps)
os2WidthClasses = {
(prev + curr) / 2: widthClass
for widthClass, (prev, curr) in enumerate(
zip(islice(steps, 0, n - 1), islice(steps, 1, n)), start=1
)
}
wdth = location["wdth"]
for percent, widthClass in sorted(os2WidthClasses.items()):
if wdth < percent:
break
else:
widthClass = 9
log.info("Setting OS/2.usWidthClass = %s", widthClass)
ttFont["OS/2"].usWidthClass = widthClass
if "slnt" in location and "post" in ttFont:
italicAngle = max(-90, min(location["slnt"], 90))
log.info("Setting post.italicAngle = %s", italicAngle)
ttFont["post"].italicAngle = italicAngle
def normalize(value, triple, avarMapping):
value = normalizeValue(value, triple)
if avarMapping:
@ -919,7 +888,7 @@ def instantiateVariableFont(
if "glyf" in varfont and overlap:
setMacOverlapFlags(varfont["glyf"])
setDefaultWeightWidthSlant(
varLib.set_default_weight_width_slant(
varfont,
location={
axisTag: limit

View File

@ -1072,79 +1072,6 @@ def test_setMacOverlapFlags():
assert b.components[0].flags & flagOverlapCompound != 0
@pytest.fixture
def ttFont():
f = ttLib.TTFont()
f["OS/2"] = ttLib.newTable("OS/2")
f["post"] = ttLib.newTable("post")
return f
class SetDefaultWeightWidthSlantTest(object):
@pytest.mark.parametrize(
"location, expected",
[
({"wght": 0}, 1),
({"wght": 1}, 1),
({"wght": 100}, 100),
({"wght": 1000}, 1000),
({"wght": 1001}, 1000),
],
)
def test_wght(self, ttFont, location, expected):
instancer.setDefaultWeightWidthSlant(ttFont, location)
assert ttFont["OS/2"].usWeightClass == expected
@pytest.mark.parametrize(
"location, expected",
[
({"wdth": 0}, 1),
({"wdth": 56}, 1),
({"wdth": 57}, 2),
({"wdth": 62.5}, 2),
({"wdth": 75}, 3),
({"wdth": 87.5}, 4),
({"wdth": 100}, 5),
({"wdth": 112.5}, 6),
({"wdth": 125}, 7),
({"wdth": 150}, 8),
({"wdth": 200}, 9),
({"wdth": 201}, 9),
({"wdth": 1000}, 9),
],
)
def test_wdth(self, ttFont, location, expected):
instancer.setDefaultWeightWidthSlant(ttFont, location)
assert ttFont["OS/2"].usWidthClass == expected
@pytest.mark.parametrize(
"location, expected",
[
({"slnt": -91}, -90),
({"slnt": -90}, -90),
({"slnt": 0}, 0),
({"slnt": 11.5}, 11.5),
({"slnt": 90}, 90),
({"slnt": 91}, 90),
],
)
def test_slnt(self, ttFont, location, expected):
instancer.setDefaultWeightWidthSlant(ttFont, location)
assert ttFont["post"].italicAngle == expected
def test_all(self, ttFont):
instancer.setDefaultWeightWidthSlant(
ttFont, {"wght": 500, "wdth": 150, "slnt": -12.0}
)
assert ttFont["OS/2"].usWeightClass == 500
assert ttFont["OS/2"].usWidthClass == 8
assert ttFont["post"].italicAngle == -12.0
def _strip_ttLibVersion(string):
return re.sub(' ttLibVersion=".*"', "", string)