Apply RIBBI bits to all kinds of fonts

This commit is contained in:
Nikolaus Waxweiler 2022-07-05 15:45:44 +01:00
parent a3e14643a1
commit 16d899a2be
3 changed files with 1882 additions and 14 deletions

View File

@ -1271,19 +1271,18 @@ def instantiateVariableFont(
},
)
if "fvar" not in varfont:
# For statics, we should set various bits to mark them as
# Regular/Italic/Bold/BoldItalic as appropriate.
setRibbiBits(varfont)
# Set Regular/Italic/Bold/Bold Italic bits as appropriate, after the name
# table has been updated.
setRibbiBits(varfont)
return varfont
def setRibbiBits(staticFont):
def setRibbiBits(font):
"""Set the `head.macStyle` and `OS/2.fsSelection` style bits
appropriately."""
english_ribbi_style = staticFont["name"].getName(2, 3, 1, 0x409)
english_ribbi_style = font["name"].getName(2, 3, 1, 0x409)
if english_ribbi_style is None:
log.warning(
"Cannot set RIBBI bits because there is no Windows Unicode BMP name ID 2."
@ -1291,14 +1290,21 @@ def setRibbiBits(staticFont):
return
styleMapStyleName = english_ribbi_style.toStr().lower()
if styleMapStyleName == "bold":
staticFont["head"].macStyle = 0b01
elif styleMapStyleName == "bold italic":
staticFont["head"].macStyle = 0b11
elif styleMapStyleName == "italic":
staticFont["head"].macStyle = 0b10
if styleMapStyleName not in {"regular", "bold", "italic", "bold italic"}:
log.warning(
"Cannot set RIBBI bits because the Windows Unicode BMP name ID 2 is "
"something other than Regular/Italic/Bold/Bold Italic."
)
return
selection = staticFont["OS/2"].fsSelection
if styleMapStyleName == "bold":
font["head"].macStyle = 0b01
elif styleMapStyleName == "bold italic":
font["head"].macStyle = 0b11
elif styleMapStyleName == "italic":
font["head"].macStyle = 0b10
selection = font["OS/2"].fsSelection
# First clear...
selection &= ~(1 << 0)
selection &= ~(1 << 5)
@ -1313,7 +1319,7 @@ def setRibbiBits(staticFont):
elif styleMapStyleName == "bold italic":
selection |= 1 << 0
selection |= 1 << 5
staticFont["OS/2"].fsSelection = selection
font["OS/2"].fsSelection = selection
def splitAxisLocationAndRanges(axisLimits, rangeType=AxisRange):

File diff suppressed because it is too large Load Diff

View File

@ -1975,3 +1975,35 @@ def test_main_exit_multiple_limits(varfont, tmpdir, capsys):
captured = capsys.readouterr()
assert "Specified multiple limits for the same axis" in captured.err
def test_set_ribbi_bits():
varfont = ttLib.TTFont()
varfont.importXML(os.path.join(TESTDATA, "STATInstancerTest.ttx"))
for location in [instance.coordinates for instance in varfont["fvar"].instances]:
instance = instancer.instantiateVariableFont(
varfont, location, updateFontNames=True
)
name_id_2 = instance["name"].getDebugName(2)
mac_style = instance["head"].macStyle
fs_selection = instance["OS/2"].fsSelection & 0b1100001 # Just bits 0, 5, 6
if location["ital"] == 0:
if location["wght"] == 700:
assert name_id_2 == "Bold", location
assert mac_style == 0b01, location
assert fs_selection == 0b0100000, location
else:
assert name_id_2 == "Regular", location
assert mac_style == 0b00, location
assert fs_selection == 0b1000000, location
else:
if location["wght"] == 700:
assert name_id_2 == "Bold Italic", location
assert mac_style == 0b11, location
assert fs_selection == 0b0100001, location
else:
assert name_id_2 == "Italic", location
assert mac_style == 0b10, location
assert fs_selection == 0b0000001, location