|
59 | 59 | "com.google.fonts/check/cjk_chws_feature",
|
60 | 60 | "com.google.fonts/check/transformed_components",
|
61 | 61 | "com.google.fonts/check/gpos7",
|
| 62 | + "com.google.fonts/check/caps_vertically_centered", |
62 | 63 | "com.google.fonts/check/ots",
|
63 | 64 | "com.adobe.fonts/check/freetype_rasterizer",
|
64 | 65 | "com.adobe.fonts/check/sfnt_version",
|
@@ -278,6 +279,50 @@ def com_google_fonts_check_family_single_directory(fonts):
|
278 | 279 | )
|
279 | 280 |
|
280 | 281 |
|
| 282 | +@check( |
| 283 | + id="com.google.fonts/check/caps_vertically_centered", |
| 284 | + proposal="https://github.com/googlefonts/fontbakery/issues/4139", |
| 285 | + rationale=""" |
| 286 | + This check suggests one possible approach to designing vertical metrics, |
| 287 | + but can be ingnored if you follow a different approach. |
| 288 | + In order to center text in buttons, lists, and grid systems |
| 289 | + with minimal additional CSS work, the uppercase glyphs should be |
| 290 | + vertically centered in the em box. |
| 291 | + This check mainly applies to Latin, Greek, Cyrillic, and other similar scripts. |
| 292 | + For non-latin scripts like Arabic, this check might not be applicable. |
| 293 | + There is a detailed description of this subject at: |
| 294 | + https://x.com/romanshamin_en/status/1562801657691672576 |
| 295 | + """, |
| 296 | +) |
| 297 | +def com_google_fonts_check_caps_vertically_centered(ttFont): |
| 298 | + """Check if uppercase glyphs are vertically centered.""" |
| 299 | + from fontTools.pens.boundsPen import BoundsPen |
| 300 | + |
| 301 | + glyphSet = ttFont.getGlyphSet() |
| 302 | + highest_point_list = [] |
| 303 | + for glyphName in ["A", "B", "C", "D", "E", "H", "I", "M", "O", "S", "T", "X"]: |
| 304 | + pen = BoundsPen(glyphSet) |
| 305 | + glyphSet[glyphName].draw(pen) |
| 306 | + highest_point = pen.bounds[3] |
| 307 | + highest_point_list.append(highest_point) |
| 308 | + |
| 309 | + upm = ttFont["head"].unitsPerEm |
| 310 | + error_margin = upm * 0.05 |
| 311 | + average_cap_height = sum(highest_point_list) / len(highest_point_list) |
| 312 | + descender = ttFont["hhea"].descent |
| 313 | + top_margin = upm - average_cap_height |
| 314 | + difference = abs(top_margin - abs(descender)) |
| 315 | + vertically_centered = difference <= error_margin |
| 316 | + |
| 317 | + if not vertically_centered: |
| 318 | + yield WARN, Message( |
| 319 | + "vertical-metrics-not-centered", |
| 320 | + "Uppercase glyphs are not vertically centered in the em box.", |
| 321 | + ) |
| 322 | + else: |
| 323 | + yield PASS, "Uppercase glyphs are vertically centered in the em box." |
| 324 | + |
| 325 | + |
281 | 326 | @check(id="com.google.fonts/check/ots", proposal="legacy:check/036")
|
282 | 327 | def com_google_fonts_check_ots(font):
|
283 | 328 | """Checking with ots-sanitize."""
|
|
0 commit comments