fontconfig: Lift axes spec to named style

This commit is contained in:
Kovid Goyal
2024-05-15 11:16:13 +05:30
parent 31fe34810f
commit ea65ede572
2 changed files with 29 additions and 9 deletions

View File

@@ -180,6 +180,17 @@ def set_named_style(name: str, font: FontConfigPattern, vd: VariableData) -> boo
return False return False
def lift_axes_to_named_style_if_possible(font: FontConfigPattern, vd: VariableData) -> bool:
axes = font.get('axes', tuple(ax['default'] for ax in vd['axes']))
q = {vd['axes'][i]['tag']: val for i, val in enumerate(axes)}
for i, ns in enumerate(vd['named_styles']):
if ns['axis_values'] == q:
font.pop('axes', None)
font['named_style'] = i
return True
return False
def set_axis_values(tag_map: Dict[str, float], font: FontConfigPattern, vd: VariableData) -> bool: def set_axis_values(tag_map: Dict[str, float], font: FontConfigPattern, vd: VariableData) -> bool:
axes = list(font.get('axes', ())) or [ax['default'] for ax in vd['axes']] axes = list(font.get('axes', ())) or [ax['default'] for ax in vd['axes']]
changed = False changed = False
@@ -190,4 +201,5 @@ def set_axis_values(tag_map: Dict[str, float], font: FontConfigPattern, vd: Vari
axes[i] = val axes[i] = val
if changed: if changed:
font['axes'] = tuple(axes) font['axes'] = tuple(axes)
lift_axes_to_named_style_if_possible(font, vd)
return changed return changed

View File

@@ -26,18 +26,26 @@ class Selection(BaseTest):
fonts_map = all_fonts_map(monospaced=True) fonts_map = all_fonts_map(monospaced=True)
family_map = fonts_map['family_map'] family_map = fonts_map['family_map']
variable_map = fonts_map['variable_map'] variable_map = fonts_map['variable_map']
def a(family: str, *expected: str) -> None: has_source_code_pro = family_name_to_key('Source Code Pro') in family_map
has_source_code_vf = family_name_to_key('sourcecodeVf') in variable_map
del fonts_map, family_map, variable_map
def s(family: str, *expected: str) -> None:
opts.font_family = parse_font_spec(family) opts.font_family = parse_font_spec(family)
ff = get_font_files(opts) ff = get_font_files(opts)
actual = tuple(face_from_descriptor(ff[x]).postscript_name() for x in ('medium', 'bold', 'italic', 'bi')) # type: ignore actual = tuple(face_from_descriptor(ff[x]).postscript_name() for x in ('medium', 'bold', 'italic', 'bi')) # type: ignore
self.assertEqual(expected, actual) with self.subTest(spec=family):
def t(family: str, *expected: str) -> None: self.ae(expected, actual)
a(family, *expected)
a(f'family="{family}"', *expected) def both(family: str, *expected: str) -> None:
if family_name_to_key('Source Code Pro') in family_map: for family in (family, f'family="{family}"'):
t('Source Code Pro', 'SourceCodePro-Regular', 'SourceCodePro-Bold', 'SourceCodePro-It', 'SourceCodePro-BoldIt') s(family, *expected)
if family_name_to_key('sourcecodeVf') in variable_map:
a('sourcecodeVf', 'SourceCodeVF-Regular', 'SourceCodeVF-Bold', 'SourceCodeVF-Italic', 'SourceCodeVF-BoldItalic') if has_source_code_pro:
both('Source Code Pro', 'SourceCodePro-Regular', 'SourceCodePro-Bold', 'SourceCodePro-It', 'SourceCodePro-BoldIt')
if has_source_code_vf:
s('sourcecodeVf', 'SourceCodeVF-Regular', 'SourceCodeVF-Bold', 'SourceCodeVF-Italic', 'SourceCodeVF-BoldItalic')
s('family=sourcecodeVf', 'SourceCodeVF-Regular', 'SourceCodeVF-Semibold', 'SourceCodeVF-Italic', 'SourceCodeVF-SemiboldItalic')
class Rendering(BaseTest): class Rendering(BaseTest):