colrv1_postproc.py: handle missing regional indicators in noflags font

don't bother to do textwrap.dedent, as whitespace doesn't matter in FEA code
pull/378/head
Cosimo Lupo 2022-02-03 19:07:42 +00:00
parent ec10159d1f
commit 083b7fcad7
1 changed files with 29 additions and 27 deletions

View File

@ -5,7 +5,6 @@ For now substantially based on copying from a correct bitmap build.
""" """
from absl import app from absl import app
import functools import functools
from textwrap import dedent
from fontTools.feaLib.builder import addOpenTypeFeaturesFromString from fontTools.feaLib.builder import addOpenTypeFeaturesFromString
from fontTools import ttLib from fontTools import ttLib
from fontTools.ttLib.tables import _g_l_y_f as glyf from fontTools.ttLib.tables import _g_l_y_f as glyf
@ -199,55 +198,58 @@ def _add_fallback_subs_for_unknown_flags(colr_font):
black_flag = cmap[BLACK_FLAG] black_flag = cmap[BLACK_FLAG]
cancel_tag = cmap[CANCEL_TAG] cancel_tag = cmap[CANCEL_TAG]
flag_tags = sorted(cmap[cp] for cp in FLAG_TAGS) flag_tags = sorted(cmap[cp] for cp in FLAG_TAGS)
regional_indicators = sorted(cmap[cp] for cp in REGIONAL_INDICATORS) # in the *-noflags.ttf font there are no region flags thus this list is empty
regional_indicators = sorted(cmap[cp] for cp in REGIONAL_INDICATORS if cp in cmap)
classes = dedent( classes = f'@FLAG_TAGS = [{" ".join(flag_tags)}];\n'
f"""\ if regional_indicators:
@FLAG_TAGS = [{" ".join(flag_tags)}]; classes += f"""
@REGIONAL_INDICATORS = [{" ".join(regional_indicators)}]; @REGIONAL_INDICATORS = [{" ".join(regional_indicators)}];
@UNKNOWN_FLAG = [{" ".join([unknown_flag] * len(regional_indicators))}]; @UNKNOWN_FLAG = [{" ".join([unknown_flag] * len(regional_indicators))}];
""" """
)
lookups = ( lookups = (
# the first lookup is a dummy that stands for the emoji sequences ligatures # the first lookup is a dummy that stands for the emoji sequences ligatures
# from the destination font; we only use it to ensure the lookup indices match. # from the destination font; we only use it to ensure the lookup indices match.
# We can't leave it empty otherwise feaLib optimizes it away. # We can't leave it empty otherwise feaLib optimizes it away.
dedent( f"""
f"""\ lookup placeholder {{
lookup placeholder {{ sub {unknown_flag} {unknown_flag} by {unknown_flag};
sub {unknown_flag} {unknown_flag} by {unknown_flag}; }} placeholder;
}} placeholder; """
"""
)
+ "\n".join( + "\n".join(
["lookup delete_glyph {"] ["lookup delete_glyph {"]
+ [f" sub {g} by NULL;" for g in sorted(regional_indicators + flag_tags)] + [f" sub {g} by NULL;" for g in sorted(regional_indicators + flag_tags)]
+ ["} delete_glyph;"] + ["} delete_glyph;"]
) )
+ "\n" + (
+ dedent( """
"""\
lookup replace_with_unknown_flag { lookup replace_with_unknown_flag {
sub @REGIONAL_INDICATORS by @UNKNOWN_FLAG; sub @REGIONAL_INDICATORS by @UNKNOWN_FLAG;
} replace_with_unknown_flag; } replace_with_unknown_flag;
""" """
if regional_indicators
else "\n"
) )
) )
features = ( features = (
"languagesystem DFLT dflt;\n" "languagesystem DFLT dflt;\n"
+ classes + classes
+ lookups + lookups
+ dedent( + "feature ccmp {"
f"""\ + f"""
feature ccmp {{ lookup placeholder;
lookup placeholder; sub {black_flag} @FLAG_TAGS' lookup delete_glyph;
sub {black_flag} @FLAG_TAGS' lookup delete_glyph; sub {black_flag} {cancel_tag} by {unknown_flag};
sub {black_flag} {cancel_tag} by {unknown_flag}; """
sub @REGIONAL_INDICATORS' lookup replace_with_unknown_flag + (
@REGIONAL_INDICATORS' lookup delete_glyph;
}} ccmp;
""" """
sub @REGIONAL_INDICATORS' lookup replace_with_unknown_flag
@REGIONAL_INDICATORS' lookup delete_glyph;
"""
if regional_indicators
else ""
) )
+ "} ccmp;"
) )
# feaLib always builds a new GSUB table (can't update one in place) so we have to # feaLib always builds a new GSUB table (can't update one in place) so we have to
# use an empty TTFont and then update our GSUB with the newly built lookups # use an empty TTFont and then update our GSUB with the newly built lookups