diff --git a/weasyprint/text/fonts.py b/weasyprint/text/fonts.py
index 110166f7a..3c6d31701 100644
--- a/weasyprint/text/fonts.py
+++ b/weasyprint/text/fonts.py
@@ -117,12 +117,13 @@ def add_font_face(self, rule_descriptors, url_fetcher):
features_string = ''.join(
f'{key} {value}'
for key, value in font_features(**features).items())
- fontconfig_style = FONTCONFIG_STYLE[
- rule_descriptors.get('font_style', 'normal')]
- fontconfig_weight = FONTCONFIG_WEIGHT[
- rule_descriptors.get('font_weight', 'normal')]
- fontconfig_stretch = FONTCONFIG_STRETCH[
- rule_descriptors.get('font_stretch', 'normal')]
+ fontconfig_style = fontconfig_weight = fontconfig_stretch = None
+ if 'font_style' in rule_descriptors:
+ fontconfig_style = FONTCONFIG_STYLE[rule_descriptors['font_style']]
+ if 'font_weight' in rule_descriptors:
+ fontconfig_weight = FONTCONFIG_WEIGHT[rule_descriptors['font_weight']]
+ if 'font_stretch' in rule_descriptors:
+ fontconfig_stretch = FONTCONFIG_STRETCH[rule_descriptors['font_stretch']]
config_key = (
f'{rule_descriptors["font_family"]}-{fontconfig_style}-'
f'{fontconfig_weight}-{features_string}').encode()
@@ -213,7 +214,7 @@ def add_font_face(self, rule_descriptors, url_fetcher):
font_path.write_bytes(font)
xml_path = self._folder / f'{config_digest}.xml'
- xml_path.write_text(f'''
+ xml = ''.join((f'''
@@ -222,16 +223,23 @@ def add_font_face(self, rule_descriptors, url_fetcher):
{rule_descriptors['font_family']}
-
+ ''',
+ f'''
{fontconfig_style}
+ ''' if fontconfig_style else '',
+ f'''
{fontconfig_weight}
+ ''' if fontconfig_weight else '',
+ f'''
{fontconfig_stretch}
+ ''' if fontconfig_stretch else '',
+ f'''
@@ -240,7 +248,8 @@ def add_font_face(self, rule_descriptors, url_fetcher):
{features_string}
- ''')
+ '''))
+ xml_path.write_text(xml)
# TODO: We should mask local fonts with the same name
# too as explained in Behdad's blog entry.