Truly independent web browser
LibWeb: Resolve the math generic family to a math font
sideshowbarkerbac2ea0f2398
diff --git a/Libraries/LibGfx/Font/SystemFontMatcherDirectWrite.cpp b/Libraries/LibGfx/Font/SystemFontMatcherDirectWrite.cppindex c6ba87d522f..c91f6e5bfc9 100644--- a/Libraries/LibGfx/Font/SystemFontMatcherDirectWrite.cpp+++ b/Libraries/LibGfx/Font/SystemFontMatcherDirectWrite.cpp@@ -275,6 +275,8 @@ static StringView windows_family_for_generic_family(StringView family)return "Comic Sans MS"sv;if (family == "fantasy"sv)return "Impact"sv;+ if (family == "math"sv)+ return "Cambria Math"sv;return family;}diff --git a/Libraries/LibWeb/CSS/FontResolution.cpp b/Libraries/LibWeb/CSS/FontResolution.cppindex d2a66b9f39f..5e178e21f00 100644--- a/Libraries/LibWeb/CSS/FontResolution.cpp+++ b/Libraries/LibWeb/CSS/FontResolution.cpp@@ -498,6 +498,9 @@ NonnullRefPtr<Gfx::FontCascadeList const> resolve_font_cascade(FontFaceSnapshotcase Keyword::Fantasy:generic_font = Platform::GenericFont::Fantasy;break;+ case Keyword::Math:+ generic_font = Platform::GenericFont::Math;+ break;case Keyword::SansSerif:generic_font = Platform::GenericFont::SansSerif;break;diff --git a/Libraries/LibWeb/Platform/FontPlugin.cpp b/Libraries/LibWeb/Platform/FontPlugin.cppindex 4af0feb6e6b..f148c6d847f 100644--- a/Libraries/LibWeb/Platform/FontPlugin.cpp+++ b/Libraries/LibWeb/Platform/FontPlugin.cpp@@ -88,6 +88,10 @@ void FontPlugin::update_generic_fonts()// (We also add Android-specific font names to the list from W3 where required.)Vector<FlyString> cursive_fallbacks { "Comic Sans MS"_fly_string, "Comic Sans"_fly_string, "Apple Chancery"_fly_string, "Bradley Hand"_fly_string, "Brush Script MT"_fly_string, "Snell Roundhand"_fly_string, "URW Chancery L"_fly_string, "Dancing Script"_fly_string };Vector<FlyString> fantasy_fallbacks { "Impact"_fly_string, "Luminari"_fly_string, "Chalkduster"_fly_string, "Jazz LET"_fly_string, "Blippo"_fly_string, "Stencil Std"_fly_string, "Marker Felt"_fly_string, "Trattatello"_fly_string, "Coming Soon"_fly_string };+ // NB: Fonts with an OpenType MATH table: the one macOS users most often have first, then the ones TeX and Linux+ // distributions ship, then the one Windows ships. STIXGeneral, which macOS ships, has no MATH table, but was+ // made for mathematical text.+ Vector<FlyString> math_fallbacks { "STIX Two Math"_fly_string, "Latin Modern Math"_fly_string, "XITS Math"_fly_string, "Libertinus Math"_fly_string, "TeX Gyre Termes Math"_fly_string, "DejaVu Math TeX Gyre"_fly_string, "Cambria Math"_fly_string, "STIXGeneral"_fly_string };Vector<FlyString> monospace_fallbacks { "Andale Mono"_fly_string, "Courier New"_fly_string, "Courier"_fly_string, "FreeMono"_fly_string, "OCR A Std"_fly_string, "Noto Sans Mono"_fly_string, "DejaVu Sans Mono"_fly_string, "Droid Sans Mono"_fly_string, "Liberation Mono"_fly_string };Vector<FlyString> sans_serif_fallbacks { "Arial"_fly_string, "Helvetica"_fly_string, "Verdana"_fly_string, "Trebuchet MS"_fly_string, "Gill Sans"_fly_string, "Noto Sans"_fly_string, "Avantgarde"_fly_string, "Optima"_fly_string, "Arial Narrow"_fly_string, "Liberation Sans"_fly_string, "Roboto"_fly_string };Vector<FlyString> serif_fallbacks { "Times"_fly_string, "Times New Roman"_fly_string, "Didot"_fly_string, "Georgia"_fly_string, "Palatino"_fly_string, "Bookman"_fly_string, "New Century Schoolbook"_fly_string, "American Typewriter"_fly_string, "Liberation Serif"_fly_string, "Roman"_fly_string, "Noto Serif"_fly_string };@@ -98,6 +102,7 @@ void FontPlugin::update_generic_fonts()fallback_set(GenericFont::Cursive) = move(cursive_fallbacks);fallback_set(GenericFont::Fantasy) = move(fantasy_fallbacks);+ fallback_set(GenericFont::Math) = move(math_fallbacks);fallback_set(GenericFont::Monospace) = move(monospace_fallbacks);fallback_set(GenericFont::SansSerif) = move(sans_serif_fallbacks);fallback_set(GenericFont::Serif) = move(serif_fallbacks);@@ -136,6 +141,9 @@ FlyString FontPlugin::compute_generic_font_name(GenericFont generic_font)case GenericFont::Fantasy:generic_family_name = "fantasy"sv;break;+ case GenericFont::Math:+ generic_family_name = "math"sv;+ break;case GenericFont::Monospace:case GenericFont::UiMonospace:generic_family_name = "monospace"sv;@@ -177,6 +185,10 @@ FlyString FontPlugin::compute_generic_font_name(GenericFont generic_font)return family;}+ // NB: Without a math font, math text is drawn in the serif font, rather than in the family listed after math.+ if (generic_font == GenericFont::Math)+ return compute_generic_font_name(GenericFont::Serif);+return fallbacks.first();}diff --git a/Libraries/LibWeb/Platform/FontPlugin.h b/Libraries/LibWeb/Platform/FontPlugin.hindex e7687b6493e..81558265cb4 100644--- a/Libraries/LibWeb/Platform/FontPlugin.h+++ b/Libraries/LibWeb/Platform/FontPlugin.h@@ -22,6 +22,7 @@ namespace Web::Platform {enum class GenericFont {Cursive,Fantasy,+ Math,Monospace,SansSerif,Serif,diff --git a/Tests/LibWeb/Text/expected/css/generic-family-math-resolves-to-a-font.txt b/Tests/LibWeb/Text/expected/css/generic-family-math-resolves-to-a-font.txtnew file mode 100644index 00000000000..a527a17330e--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/generic-family-math-resolves-to-a-font.txt@@ -0,0 +1 @@+math draws the text, not the font after it: truediff --git a/Tests/LibWeb/Text/input/css/generic-family-math-resolves-to-a-font.html b/Tests/LibWeb/Text/input/css/generic-family-math-resolves-to-a-font.htmlnew file mode 100644index 00000000000..bb57d287329--- /dev/null+++ b/Tests/LibWeb/Text/input/css/generic-family-math-resolves-to-a-font.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<style>+ @font-face { font-family: AhemFallback; src: url(../wpt-import/fonts/Ahem.ttf); }+ div { font-size: 20px; white-space: pre; }+</style>+<div style="font-family: math, AhemFallback"><span id="math">xxxxx</span></div>+<div style="font-family: AhemFallback"><span id="ahem">xxxxx</span></div>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ await document.fonts.load("20px AhemFallback");+ // The math generic family resolves to a font of its own, so the family after it in the list draws nothing here.+ const width = id => document.getElementById(id).getBoundingClientRect().width;+ println(`math draws the text, not the font after it: ${width("math") !== width("ahem")}`);+ done();+ });+</script>