Truly independent web browser
LibGfx: Take a font’s ascent and descent from OS/2 when hhea has none
sideshowbarker2238abd62a17
diff --git a/Libraries/LibGfx/Font/FaceDescription.cpp b/Libraries/LibGfx/Font/FaceDescription.cppindex 7e3f04ee66d..6f029605d80 100644--- a/Libraries/LibGfx/Font/FaceDescription.cpp+++ b/Libraries/LibGfx/Font/FaceDescription.cpp@@ -111,6 +111,20 @@ static FaceVerticalMetrics read_vertical_metrics(hb_face_t* face)metrics.ascender = position;hb_ot_metrics_get_position_with_fallback(font, HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER, &position);metrics.descender = position;++ // https://learn.microsoft.com/en-us/typography/opentype/spec/os2+ // NB: Some fonts leave the ascender and descender of their hhea table at 0, and give them only in their OS/2 table.+ // Such a font takes them from the OS/2 typo metrics instead, or from its win metrics where those are 0 too.+ if (metrics.ascender == 0 && metrics.descender == 0) {+ FontTable os2 { face, FourCC { "OS/2" } };+ if (os2.contains(68, 4) && (os2.i16_at(68) != 0 || os2.i16_at(70) != 0)) {+ metrics.ascender = os2.i16_at(68);+ metrics.descender = os2.i16_at(70);+ } else if (os2.contains(74, 4)) {+ metrics.ascender = os2.u16_at(74);+ metrics.descender = -static_cast<i32>(os2.u16_at(76));+ }+ }if (hb_ot_metrics_get_position(font, HB_OT_METRICS_TAG_X_HEIGHT, &position))metrics.x_height = position;diff --git a/Tests/LibWeb/Text/expected/css/font-with-zero-hhea-metrics-uses-os2.txt b/Tests/LibWeb/Text/expected/css/font-with-zero-hhea-metrics-uses-os2.txtnew file mode 100644index 00000000000..6a3ecdc786e--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/font-with-zero-hhea-metrics-uses-os2.txt@@ -0,0 +1,2 @@+span height: 15+two lines with line-height: normal take up two lines: truediff --git a/Tests/LibWeb/Text/input/css/font-with-zero-hhea-metrics-uses-os2.html b/Tests/LibWeb/Text/input/css/font-with-zero-hhea-metrics-uses-os2.htmlnew file mode 100644index 00000000000..a977ceedd68--- /dev/null+++ b/Tests/LibWeb/Text/input/css/font-with-zero-hhea-metrics-uses-os2.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ /* A subset of Liberation Sans (SIL OFL) whose hhea ascender, descender and line gap are 0, with its OS/2 table as is. */+ @font-face { font-family: HheaZero; src: url(data:font/woff2;base64,d09GMgABAAAAAAQUAAoAAAAABiwAAAPKAAIZmQAAAAAAAAAAAAAAAAAAAAAAAAAABmAAbAqGUIUuATYCJAM4Cx4ABCAFcAcgG7sEIC4H7GbZT/ARBpYRqrRVSyy5nY8OU0IQ4Xv/P5umR8V9EvlFKNPOekjSxDqPntdq/ws4muEYNjEuX0hG/fbpnSSkT9aIJKXVVSewtMXddHIih8MzTCmV5oUWBFkT4/JlJTgNUBKTFWmk3afdEqJvMDWR9ef/cno4Nti2IzjxExrTPt7prLB32Is2J5hgNtagNKIJ6um2CON8xcjpoj4tgKVXyiI22AtsAUjC1dm6RBsVI2QrJ6AVYI1SWCx78Zo+AIPQz77yvzrMGSwiO5Dtgy5gDweSkQlQgAXEwkIKo6m3TwyiYaFIRi1KGnomdm5jIMwU1OzIxvULzCOzxeybPbNjts2GWUMA6gMnAo5C9hGkI4DUtBMU4kDy8FDMYJz4WuuEjpsxdmx3ICISdiUmbYe9oyc34hxf7vg2uHMHPdOAOtevo8ZXoyuwSDAijgy/gZ6UBnzRIgAOA29EMtcAOmIPV2rkFQPRathVSohDv7ATcgai+2Py5cHhSZzZi8dj0Ef9ge2vXnIb3PMmt+JbMuMNfkXzjYbXY13fDQBbEzh/YNweX4ux/0jd48x7h1cSbyzQiDh10ThE9LBL6EldMqqJc+gtGTUA3XjLEVfh6PWi0du0QnXMolun0O0yWsizZ7fP/nbHDAFifj8w/76rd2eAoVrMb2bq7ZbArc/+6p6c+9twZ1OHWhr6VqCbxJm5u7u129fQV97VqmsOso1uxV5Y0SjjcyQxrwzP335FtGrtaOXl/WZLibLY1OwY2nfdXb0FsM/uGuEIDA8DQrOTbdpaQivYol1WJqQ0+1/JvyVXWW5o9oxYWx8p3bIdexYgtHRSBVy46wp/2cbB2tM7oPjlJPoV+/2732TRsz37+UrxHo46Pbyk8+X3Y3y9e0X7K6Bg84xb/9MXH366ySaN8OLUmWquHVVHRbvjwjKYTeEC+YjggPzwrpeB05uXq2jjcbJftq0vKC8NCNL9N6brkiniwYXU5eVhaLAeKXDz2+U6Xcunfn+fXKyik6cq6THn0tHflOs6pev2Xt+wV2DHjj+Td0ko76dsePzQc23zJmBeEyQgeYqDkoMXtRm7eCe8A+fgVAF6dGYAEEDFn5/l920hfKFvoaQhAE4W+wPwIinqmX9m7Gf7GQihDhoAAQSw+ZbIJ+gagHNKCPoJWXRPgKVeYrfwyh3jN28C9R4rGA350+OT2ifMPw1xEcAn42UVMEyTBwvRYYRoQCWFT7RmnbpfgEQkoRw1BZFAwcqEoKDioCeywmlII4jslJsYkZmRDQzUzGHmAAAA) format("woff2"); }+ body { margin: 8px; font: 16px HheaZero; }+ #lines { width: 120px; line-height: normal; }+</style>+<p style="line-height: 30px"><span id="span">Text in the font</span></p>+<div id="lines">Two lines of text in the font</div>+<script>+ asyncTest(async done => {+ await document.fonts.ready;+ // A font whose hhea table gives no ascent or descent takes them from its OS/2 table.+ println(`span height: ${Math.round(span.getBoundingClientRect().height)}`);+ // With line-height: normal, each line is as tall as the ascent and descent, so the two lines don't overlap.+ const lineHeight = Math.round(span.getBoundingClientRect().height);+ const linesHeight = Math.round(lines.getBoundingClientRect().height);+ println(`two lines with line-height: normal take up two lines: ${lineHeight > 0 && linesHeight >= 2 * lineHeight}`);+ done();+ });+</script>