Skip to content

Commit 9b6a0b7

Browse files
yanonefelipesanches
authored andcommitted
New check: com.google.fonts/check/color_cpal_brightness
(PR #3908)
1 parent 34b929a commit 9b6a0b7

4 files changed

Lines changed: 78 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A more detailed list of changes is available in the corresponding milestones for
88
- **[com.google.fonts/check/colorfont_tables]:** Fonts must have neither or both the tables `COLR` and `SVG`. (issue #3886)
99
- **[com.google.fonts/check/description/noto_has_article]:** Noto fonts must have an ARTICLE.en_us.html file. (issue #3841)
1010
- **[com.google.fonts/check/slant_direction]:** Check slant direction of outline to match values of slnt axis extrema. (PR #3910)
11+
- **[com.google.fonts/check/color_cpal_brightness]:** Warn if COLRv0 layers are colored too dark or too bright instead of foreground color. (PR #3908)
1112

1213
### BugFixes
1314
- **[com.adobe.fonts/check/varfont/valid_default_instance_nameids]:** The check did not account for nameID 17. (issue #3895)

Lib/fontbakery/profiles/googlefonts.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
'com.google.fonts/check/render_own_name',
202202
'com.google.fonts/check/STAT',
203203
'com.google.fonts/check/colorfont_tables',
204+
'com.google.fonts/check/color_cpal_brightness',
204205
]
205206

206207
GOOGLEFONTS_PROFILE_CHECKS = \
@@ -6248,6 +6249,61 @@ def com_google_fonts_check_colorfont_tables(ttFont):
62486249
else:
62496250
yield PASS, "Looks good!"
62506251

6252+
6253+
@check(
6254+
id = "com.google.fonts/check/color_cpal_brightness",
6255+
rationale = """
6256+
Layers of a COLRv0 font should not be too dark or too bright. When layer colors
6257+
are set explicitly, they can't be changed and they may turn out illegible
6258+
against dark or bright backgrounds.
6259+
6260+
While traditional color-less fonts can be colored in design apps or CSS, a
6261+
black color definition in a COLRv0 font actually means that that layer will be
6262+
rendered in black regardless of the background color. This leads to text
6263+
becoming invisible against a dark background, for instance when using a dark
6264+
theme in a web browser or operating system.
6265+
6266+
This check ensures that layer colors are at least 10% bright and at most 90%
6267+
bright, when not already set to the current color (0xFFFF).
6268+
""",
6269+
proposal = 'https://github.com/googlefonts/fontbakery/pull/3908'
6270+
)
6271+
def com_google_fonts_check_color_cpal_brightness(config, ttFont):
6272+
"""Color layers should have a minimum brightness"""
6273+
from fontbakery.utils import pretty_print_list
6274+
6275+
def color_brightness(hex_value):
6276+
'''Generic color brightness formula'''
6277+
return (hex_value[0] * 299 + hex_value[1] * 587 + hex_value[2] * 114) / 1000
6278+
6279+
minimum_brightness = 256 * .1
6280+
FOREGROUND_COLOR = 0xFFFF
6281+
dark_glyphs = []
6282+
if 'COLR' in ttFont.keys() and ttFont['COLR'].version == 0:
6283+
for key in ttFont['COLR'].ColorLayers:
6284+
for layer in ttFont['COLR'].ColorLayers[key]:
6285+
# 0xFFFF is the foreground color, ignore
6286+
if layer.colorID != FOREGROUND_COLOR:
6287+
hex_value = ttFont["CPAL"].palettes[0][layer.colorID]
6288+
layer_brightness = color_brightness(hex_value)
6289+
if (layer_brightness < minimum_brightness
6290+
or layer_brightness > 256 - minimum_brightness):
6291+
if key not in dark_glyphs:
6292+
dark_glyphs.append(key)
6293+
if dark_glyphs:
6294+
dark_glyphs = pretty_print_list(config, sorted(dark_glyphs))
6295+
yield WARN,\
6296+
Message('glyphs-too-dark-or-too-bright',
6297+
f"The following glyphs have layers that are too bright or"
6298+
f" too dark: {dark_glyphs}.\n"
6299+
f"\n"
6300+
f" To fix this, please either set the color definitions of all"
6301+
f" layers in question to current color (0xFFFF), or alter"
6302+
f" the brightness of these layers significantly.")
6303+
else:
6304+
yield PASS, "Looks good!"
6305+
6306+
62516307
@check(
62526308
id = 'com.google.fonts/check/description/noto_has_article',
62536309
conditions = ['is_noto'],
200 KB
Binary file not shown.

tests/profiles/googlefonts_test.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,10 +4218,8 @@ def test_check_STAT(fps, new_stat, result):
42184218
@patch("freetype.Face", side_effect=ImportError)
42194219
def test_check_override_freetype_rasterizer(mock_import_error):
42204220
"""Check that overridden test yields FAIL rather than SKIP."""
4221-
check = CheckTester(
4222-
googlefonts_profile,
4223-
f"com.adobe.fonts/check/freetype_rasterizer{OVERRIDE_SUFFIX}",
4224-
)
4221+
check = CheckTester(googlefonts_profile,
4222+
f"com.adobe.fonts/check/freetype_rasterizer{OVERRIDE_SUFFIX}")
42254223

42264224
font = TEST_FILE("cabin/Cabin-Regular.ttf")
42274225
msg = assert_results_contain(check(font), FAIL, "freetype-not-installed")
@@ -4263,15 +4261,30 @@ def test_check_colorfont_tables():
42634261
f'with a good font without SVG or COLR tables.')
42644262

42654263

4264+
def test_check_color_cpal_brightness():
4265+
"""Color layers should have a minimum brightness"""
4266+
check = CheckTester(googlefonts_profile,
4267+
"com.google.fonts/check/color_cpal_brightness")
4268+
4269+
font = TEST_FILE("color_fonts/AmiriQuranColored_too_dark.ttf")
4270+
assert_results_contain(check(font),
4271+
WARN, 'glyphs-too-dark-or-too-bright',
4272+
'with a colrv0 font with doo dark layers')
4273+
4274+
font = TEST_FILE("color_fonts/AmiriQuranColored.ttf")
4275+
assert_PASS(check(font),
4276+
'with a colrv0 font with good layer colors')
4277+
4278+
42664279
def test_check_noto_has_article():
42674280
"""Noto fonts must have an ARTICLE.en_us.html file"""
42684281
check = CheckTester(googlefonts_profile,
42694282
"com.google.fonts/check/description/noto_has_article")
42704283

4271-
ttFont = TTFont(TEST_FILE("notosanskhudawadi/NotoSansKhudawadi-Regular.ttf"))
4272-
assert_PASS(check(ttFont), "with a good font")
4284+
font = TEST_FILE("notosanskhudawadi/NotoSansKhudawadi-Regular.ttf")
4285+
assert_PASS(check(font), "with a good font")
42734286

4274-
ttFont = TTFont(TEST_FILE("noto_sans_tamil_supplement/NotoSansTamilSupplement-Regular.ttf"))
4275-
assert_results_contain(check(ttFont),
4287+
font = TEST_FILE("noto_sans_tamil_supplement/NotoSansTamilSupplement-Regular.ttf")
4288+
assert_results_contain(check(font),
42764289
FAIL, 'missing-article',
42774290
"with a bad font")

0 commit comments

Comments
 (0)