Skip to content

Commit a644377

Browse files
authored
New check: Caps vertical centering
If possible, the uppercase glyphs should be vertically centered in the em box. **com.google.fonts/check/caps_vertically_centered** Added to the Universal profile. (issue #4139)
1 parent 6a19237 commit a644377

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ A more detailed list of changes is available in the corresponding milestones for
1818
#### Added to the Google Fonts Profile
1919
- **[com.google.fonts/check/metadata/primary_script]:** New check that guesses the primary script and compares to METADATA.pb (issue #4109)
2020

21+
#### Added to the Universal Profile
22+
- **[com.google.fonts/check/caps_vertically_centered]:** If possible, the uppercase glyphs should be vertically centered in the em box. (issue #4139)
2123

2224
## 0.9.0a2 (2023-Aug-04)
2325
### Changes to existing checks

Lib/fontbakery/profiles/universal.py

+45
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"com.google.fonts/check/cjk_chws_feature",
6060
"com.google.fonts/check/transformed_components",
6161
"com.google.fonts/check/gpos7",
62+
"com.google.fonts/check/caps_vertically_centered",
6263
"com.google.fonts/check/ots",
6364
"com.adobe.fonts/check/freetype_rasterizer",
6465
"com.adobe.fonts/check/sfnt_version",
@@ -278,6 +279,50 @@ def com_google_fonts_check_family_single_directory(fonts):
278279
)
279280

280281

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+
281326
@check(id="com.google.fonts/check/ots", proposal="legacy:check/036")
282327
def com_google_fonts_check_ots(font):
283328
"""Checking with ots-sanitize."""

tests/profiles/universal_test.py

+14
Original file line numberDiff line numberDiff line change
@@ -1367,3 +1367,17 @@ def test_check_alt_caron():
13671367

13681368
ttFont = TTFont(TEST_FILE("merriweather/Merriweather-Regular.ttf"))
13691369
assert_PASS(check(ttFont))
1370+
1371+
1372+
def test_check_caps_vertically_centered():
1373+
"""Check if uppercase glyphs are vertically centered."""
1374+
1375+
check = CheckTester(
1376+
universal_profile, "com.google.fonts/check/caps_vertically_centered"
1377+
)
1378+
1379+
test_font = TTFont(TEST_FILE("shantell/ShantellSans[BNCE,INFM,SPAC,wght].ttf"))
1380+
assert_PASS(check(test_font))
1381+
1382+
test_font = TTFont(TEST_FILE("cairo/CairoPlay-Italic.leftslanted.ttf"))
1383+
assert_results_contain(check(test_font), WARN, "vertical-metrics-not-centered")

0 commit comments

Comments
 (0)