Skip to content

Commit 4617b94

Browse files
committed
Don’t set default Fontconfig values for unset properties
1 parent d9d7f62 commit 4617b94

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

weasyprint/text/fonts.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ def add_font_face(self, rule_descriptors, url_fetcher):
117117
features_string = ''.join(
118118
f'<string>{key} {value}</string>'
119119
for key, value in font_features(**features).items())
120-
fontconfig_style = FONTCONFIG_STYLE[
121-
rule_descriptors.get('font_style', 'normal')]
122-
fontconfig_weight = FONTCONFIG_WEIGHT[
123-
rule_descriptors.get('font_weight', 'normal')]
124-
fontconfig_stretch = FONTCONFIG_STRETCH[
125-
rule_descriptors.get('font_stretch', 'normal')]
120+
fontconfig_style = fontconfig_weight = fontconfig_stretch = None
121+
if 'font_style' in rule_descriptors:
122+
fontconfig_style = FONTCONFIG_STYLE[rule_descriptors['font_style']]
123+
if 'font_weight' in rule_descriptors:
124+
fontconfig_weight = FONTCONFIG_WEIGHT[rule_descriptors['font_weight']]
125+
if 'font_stretch' in rule_descriptors:
126+
fontconfig_stretch = FONTCONFIG_STRETCH[rule_descriptors['font_stretch']]
126127
config_key = (
127128
f'{rule_descriptors["font_family"]}-{fontconfig_style}-'
128129
f'{fontconfig_weight}-{features_string}').encode()
@@ -213,7 +214,7 @@ def add_font_face(self, rule_descriptors, url_fetcher):
213214
font_path.write_bytes(font)
214215

215216
xml_path = self._folder / f'{config_digest}.xml'
216-
xml_path.write_text(f'''<?xml version="1.0"?>
217+
xml = ''.join((f'''<?xml version="1.0"?>
217218
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
218219
<fontconfig>
219220
<match target="scan">
@@ -222,16 +223,23 @@ def add_font_face(self, rule_descriptors, url_fetcher):
222223
</test>
223224
<edit name="family" mode="assign_replace">
224225
<string>{rule_descriptors['font_family']}</string>
225-
</edit>
226+
</edit>''',
227+
f'''
226228
<edit name="slant" mode="assign_replace">
227229
<const>{fontconfig_style}</const>
228230
</edit>
231+
''' if fontconfig_style else '',
232+
f'''
229233
<edit name="weight" mode="assign_replace">
230234
<int>{fontconfig_weight}</int>
231235
</edit>
236+
''' if fontconfig_weight else '',
237+
f'''
232238
<edit name="width" mode="assign_replace">
233239
<const>{fontconfig_stretch}</const>
234240
</edit>
241+
''' if fontconfig_stretch else '',
242+
f'''
235243
</match>
236244
<match target="font">
237245
<test name="file" compare="eq">
@@ -240,7 +248,8 @@ def add_font_face(self, rule_descriptors, url_fetcher):
240248
<edit name="fontfeatures"
241249
mode="assign_replace">{features_string}</edit>
242250
</match>
243-
</fontconfig>''')
251+
</fontconfig>'''))
252+
xml_path.write_text(xml)
244253

245254
# TODO: We should mask local fonts with the same name
246255
# too as explained in Behdad's blog entry.

0 commit comments

Comments
 (0)