@@ -117,12 +117,13 @@ def add_font_face(self, rule_descriptors, url_fetcher):
117
117
features_string = '' .join (
118
118
f'<string>{ key } { value } </string>'
119
119
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' ]]
126
127
config_key = (
127
128
f'{ rule_descriptors ["font_family" ]} -{ fontconfig_style } -'
128
129
f'{ fontconfig_weight } -{ features_string } ' ).encode ()
@@ -213,7 +214,7 @@ def add_font_face(self, rule_descriptors, url_fetcher):
213
214
font_path .write_bytes (font )
214
215
215
216
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"?>
217
218
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
218
219
<fontconfig>
219
220
<match target="scan">
@@ -222,16 +223,23 @@ def add_font_face(self, rule_descriptors, url_fetcher):
222
223
</test>
223
224
<edit name="family" mode="assign_replace">
224
225
<string>{ rule_descriptors ['font_family' ]} </string>
225
- </edit>
226
+ </edit>''' ,
227
+ f'''
226
228
<edit name="slant" mode="assign_replace">
227
229
<const>{ fontconfig_style } </const>
228
230
</edit>
231
+ ''' if fontconfig_style else '' ,
232
+ f'''
229
233
<edit name="weight" mode="assign_replace">
230
234
<int>{ fontconfig_weight } </int>
231
235
</edit>
236
+ ''' if fontconfig_weight else '' ,
237
+ f'''
232
238
<edit name="width" mode="assign_replace">
233
239
<const>{ fontconfig_stretch } </const>
234
240
</edit>
241
+ ''' if fontconfig_stretch else '' ,
242
+ f'''
235
243
</match>
236
244
<match target="font">
237
245
<test name="file" compare="eq">
@@ -240,7 +248,8 @@ def add_font_face(self, rule_descriptors, url_fetcher):
240
248
<edit name="fontfeatures"
241
249
mode="assign_replace">{ features_string } </edit>
242
250
</match>
243
- </fontconfig>''' )
251
+ </fontconfig>''' ))
252
+ xml_path .write_text (xml )
244
253
245
254
# TODO: We should mask local fonts with the same name
246
255
# too as explained in Behdad's blog entry.
0 commit comments