@@ -346,23 +346,6 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
346
346
return [fname for fname in fontfiles if os .path .exists (fname )]
347
347
348
348
349
- def weight_as_number (weight ):
350
- """
351
- Return the weight property as a numeric value. String values
352
- are converted to their corresponding numeric value.
353
- """
354
- if isinstance (weight , six .string_types ):
355
- try :
356
- weight = weight_dict [weight .lower ()]
357
- except KeyError :
358
- weight = 400
359
- elif weight in range (100 , 1000 , 100 ):
360
- pass
361
- else :
362
- raise ValueError ('weight not a valid integer' )
363
- return weight
364
-
365
-
366
349
class FontEntry (object ):
367
350
"""
368
351
A class for storing Font properties. It is used when populating
@@ -435,17 +418,12 @@ def ttfFontProperty(font):
435
418
else :
436
419
variant = 'normal'
437
420
438
- # Weights are: 100, 200, 300, 400 (normal: default), 500 (medium),
439
- # 600 (semibold, demibold), 700 (bold), 800 (heavy), 900 (black)
440
- # lighter and bolder are also allowed.
441
-
442
421
weight = next ((w for w in weight_dict if sfnt4 .find (w ) >= 0 ), None )
443
422
if not weight :
444
423
if font .style_flags & ft2font .BOLD :
445
- weight = 700
424
+ weight = "bold"
446
425
else :
447
- weight = 400
448
- weight = weight_as_number (weight )
426
+ weight = "normal"
449
427
450
428
# Stretch can be absolute and relative
451
429
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -511,11 +489,7 @@ def afmFontProperty(fontpath, font):
511
489
else :
512
490
variant = 'normal'
513
491
514
- # Weights are: 100, 200, 300, 400 (normal: default), 500 (medium),
515
- # 600 (semibold, demibold), 700 (bold), 800 (heavy), 900 (black)
516
- # lighter and bolder are also allowed.
517
-
518
- weight = weight_as_number (font .get_weight ().lower ())
492
+ weight = font .get_weight ().lower ()
519
493
520
494
# Stretch can be absolute and relative
521
495
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -855,7 +829,6 @@ def set_weight(self, weight):
855
829
except ValueError :
856
830
if weight not in weight_dict :
857
831
raise ValueError ("weight is invalid" )
858
- weight = weight_dict [weight ]
859
832
self ._weight = weight
860
833
861
834
def set_stretch (self , stretch ):
@@ -1203,10 +1176,19 @@ def score_weight(self, weight1, weight2):
1203
1176
"""
1204
1177
Returns a match score between *weight1* and *weight2*.
1205
1178
1206
- The result is the absolute value of the difference between the
1179
+ The result is 0.0 if both weight1 and weight 2 are given as strings
1180
+ and have the same value.
1181
+
1182
+ Otherwise, the result is the absolute value of the difference between the
1207
1183
CSS numeric values of *weight1* and *weight2*, normalized
1208
- between 0.0 and 1.0.
1184
+ between 0.05 and 1.0.
1209
1185
"""
1186
+
1187
+ # exact match of the weight names (e.g. weight1 == weight2 == "regular")
1188
+ if (isinstance (weight1 , six .string_types ) and
1189
+ isinstance (weight2 , six .string_types ) and
1190
+ weight1 == weight2 ):
1191
+ return 0.0
1210
1192
try :
1211
1193
weightval1 = int (weight1 )
1212
1194
except ValueError :
@@ -1215,7 +1197,7 @@ def score_weight(self, weight1, weight2):
1215
1197
weightval2 = int (weight2 )
1216
1198
except ValueError :
1217
1199
weightval2 = weight_dict .get (weight2 , 500 )
1218
- return abs (weightval1 - weightval2 ) / 1000.0
1200
+ return 0.95 * ( abs (weightval1 - weightval2 ) / 1000.0 ) + 0.05
1219
1201
1220
1202
def score_size (self , size1 , size2 ):
1221
1203
"""
0 commit comments