fonts: Add support for generic font families and font size configuration (#32673)

This adds support for generic font families in Servo and allows for
configuration of them as well as their default font sizes. One
interesting fix here is that now monospace default to 13px, like it does
in other browsers.

In addition to that, this exposes a new interface in Stylo which allows
setting a default style. This is quite useful for fonts, but also for
other kinds of default style settings -- like text zoom.

Fixes #8371.
Fixes #14773.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Martin Robinson 2024-07-08 19:17:48 +02:00 committed by GitHub
parent 956b7f62e0
commit 77e9e3deba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 397 additions and 300 deletions

View file

@ -9,19 +9,15 @@ use base::text::{unicode_plane, UnicodeBlock, UnicodeBlockMethod};
use dwrote::{Font, FontCollection, FontDescriptor, FontStretch, FontStyle};
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
use style::values::computed::font::GenericFontFamily;
use style::values::computed::{FontStyle as StyleFontStyle, FontWeight as StyleFontWeight};
use style::values::specified::font::FontStretchKeyword;
use crate::{
EmojiPresentationPreference, FallbackFontSelectionOptions, FontTemplate, FontTemplateDescriptor,
EmojiPresentationPreference, FallbackFontSelectionOptions, FontTemplate,
FontTemplateDescriptor, LowercaseFontFamilyName,
};
pub static SANS_SERIF_FONT_FAMILY: &str = "Arial";
pub fn system_default_family(_: &str) -> Option<String> {
Some("Verdana".to_owned())
}
pub fn for_each_available_family<F>(mut callback: F)
where
F: FnMut(String),
@ -377,3 +373,15 @@ impl From<&Font> for FontTemplateDescriptor {
FontTemplateDescriptor::new(weight, stretch, style)
}
}
pub fn default_system_generic_font_family(generic: GenericFontFamily) -> LowercaseFontFamilyName {
match generic {
GenericFontFamily::None | GenericFontFamily::Serif => "Times New Roman",
GenericFontFamily::SansSerif => "Arial",
GenericFontFamily::Monospace => "Courier New",
GenericFontFamily::Cursive => "Comic Sans MS",
GenericFontFamily::Fantasy => "Impact",
GenericFontFamily::SystemUi => "Segoe UI",
}
.into()
}