Skip to content

Commit

Permalink
emit WARNs when xAvgCharWidth value differs slightly from
Browse files Browse the repository at this point in the history
the expected value.
(issue fonttools#1622)
  • Loading branch information
felipesanches committed Oct 26, 2017
1 parent bf5df19 commit 21c3df3
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions Lib/fontbakery/specifications/googlefonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,9 @@ def com_google_fonts_test_033(ttFont, monospace_stats):
)
def com_google_fonts_test_034(ttFont):
"""Check if OS/2 xAvgCharWidth is correct."""
current_value = ttFont['OS/2'].xAvgCharWidth
ACCEPTABLE_ERROR = 10 # This is how much we're willing to accept

if ttFont['OS/2'].version >= 3:
width_sum = 0
count = 0
Expand All @@ -1038,14 +1041,24 @@ def com_google_fonts_test_034(ttFont):
yield FAIL, "CRITICAL: Found no glyph width data!"
else:
expected_value = int(round(width_sum) / count)

if ttFont['OS/2'].xAvgCharWidth == expected_value:
if current_value == expected_value:
yield PASS, "OS/2 xAvgCharWidth is correct."
elif abs(current_value - expected_value) < ACCEPTABLE_ERROR:
yield WARN, ("OS/2 xAvgCharWidth is {} but should be"
" {} which corresponds to the"
" average of all glyph widths"
" in the font. These are similar values, which"
" may be a symptom of the slightly different"
" calculation of the xAvgCharWidth value in"
" font editors. There's further discussion on"
" this at https://github.com/googlefonts/fontbakery"
"/issues/1622").format(current_value,
expected_value)
else:
yield FAIL, ("OS/2 xAvgCharWidth is {} but should be "
"{} which corresponds to the "
"average of all glyph widths "
"in the font").format(ttFont['OS/2'].xAvgCharWidth,
"in the font").format(current_value,
expected_value)
else:
weightFactors = {'a': 64, 'b': 14, 'c': 27, 'd': 35,
Expand All @@ -1062,14 +1075,26 @@ def com_google_fonts_test_034(ttFont):
width_sum += (width*weightFactors[glyph_id])
expected_value = int(width_sum/1000.0 + 0.5) # round to closest int

if ttFont['OS/2'].xAvgCharWidth == expected_value:
if current_value == expected_value:
yield PASS, "OS/2 xAvgCharWidth value is correct."
elif abs(current_value - expected_value) < ACCEPTABLE_ERROR:
yield WARN, ("OS/2 xAvgCharWidth is {} but should be"
" {} which corresponds to the weighted"
" average of the widths of the latin"
" lowercase glyphs in the font."
" These are similar values, which"
" may be a symptom of the slightly different"
" calculation of the xAvgCharWidth value in"
" font editors. There's further discussion on"
" this at https://github.com/googlefonts/fontbakery"
"/issues/1622").format(current_value,
expected_value)
else:
yield FAIL, ("OS/2 xAvgCharWidth is {} but it should be "
"{} which corresponds to the weighted "
"average of the widths of the latin "
"lowercase glyphs in "
"the font").format(ttFont['OS/2'].xAvgCharWidth,
"the font").format(current_value,
expected_value)


Expand Down

0 comments on commit 21c3df3

Please sign in to comment.