|
200 | 200 | 'com.google.fonts/check/render_own_name', |
201 | 201 | 'com.google.fonts/check/STAT', |
202 | 202 | 'com.google.fonts/check/colorfont_tables', |
| 203 | + 'com.google.fonts/check/color_cpal_brightness', |
203 | 204 | ] |
204 | 205 |
|
205 | 206 | GOOGLEFONTS_PROFILE_CHECKS = \ |
@@ -6188,6 +6189,50 @@ def com_google_fonts_check_colorfont_tables(ttFont): |
6188 | 6189 | else: |
6189 | 6190 | yield PASS, "Looks good!" |
6190 | 6191 |
|
| 6192 | +@check( |
| 6193 | + id = "com.google.fonts/check/color_cpal_brightness", |
| 6194 | + rationale = """ |
| 6195 | + Layers of a COLRv0 font should not be too dark or too bright. When layer colors are |
| 6196 | + set explicitly, they can't be changed and they may turn out illegible against dark or bright backgrounds. |
| 6197 | + While traditional color-less fonts |
| 6198 | + can be colored in design apps or CSS, a black color definition in a COLRv0 |
| 6199 | + font actually means that that layer will be rendered in black regardless of the |
| 6200 | + background color. This leads to text becoming invisible against a dark background, |
| 6201 | + for instance when using a dark theme in a web browser or operating system. |
| 6202 | +
|
| 6203 | + This check ensures that layer colors are at least 10% bright and at most 90% bright, when not |
| 6204 | + already set to the current color (0xFFFF). |
| 6205 | + """, |
| 6206 | + proposal = 'https://github.com/googlefonts/fontbakery/issues/3886' |
| 6207 | +) |
| 6208 | +def com_google_fonts_check_color_cpal_brightness(ttFont): |
| 6209 | + """Color layers should have a minimum brightness""" |
| 6210 | + SUGGESTED_FIX = ("To fix this, please either set the color definitions of all layers " |
| 6211 | + "in question to current color (0xFFFF), " |
| 6212 | + "or alter the brightness of these layers significantly.") |
| 6213 | + minimum_brightness = 256 * .1 |
| 6214 | + FOREGROUND_COLOR = 0xFFFF |
| 6215 | + # Generic color brightness formula |
| 6216 | + def color_brightness(hex): |
| 6217 | + return (hex[0] * 299 + hex[1] * 587 + hex[2] * 114) / 1000 |
| 6218 | + dark_glyphs = [] |
| 6219 | + if 'COLR' in ttFont.keys() and ttFont['COLR'].version == 0: |
| 6220 | + for key in ttFont['COLR'].ColorLayers: |
| 6221 | + for layer in ttFont['COLR'].ColorLayers[key]: |
| 6222 | + # 0xFFFF is the foreground color, ignore |
| 6223 | + if layer.colorID != FOREGROUND_COLOR: |
| 6224 | + layer_brightness = color_brightness(ttFont["CPAL"].palettes[0][layer.colorID]) |
| 6225 | + if layer_brightness < minimum_brightness or layer_brightness > 256 - minimum_brightness: |
| 6226 | + if key not in dark_glyphs: |
| 6227 | + dark_glyphs.append(key) |
| 6228 | + if dark_glyphs: |
| 6229 | + yield WARN,\ |
| 6230 | + Message('glyphs-too-dark-or-too-bright', |
| 6231 | + f"The following glyphs have layers that are too bright or too dark: {dark_glyphs}. " + SUGGESTED_FIX) |
| 6232 | + |
| 6233 | + else: |
| 6234 | + yield PASS, "Looks good!" |
| 6235 | + |
6191 | 6236 | @check( |
6192 | 6237 | id = 'com.google.fonts/check/description/noto_has_article', |
6193 | 6238 | conditions = ['is_noto'], |
|
0 commit comments