mirror of
https://github.com/servo/servo.git
synced 2025-09-29 16:19:14 +01:00
fonts: Implement CSS font-variation-settings
property for FreeType platforms (#38642)
This change adds support for variable fonts via the [`font-variation-settings`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-variation-settings) property. There are three areas where we need to set the variation values: * Webrender (`compositor.rs`), for drawing the glyphs * Harfbuzz (`shaper.rs`), for most shaping tasks * PlatformFont (`fonts/platform/`), for horizontal advances and kerning For now, freetype is the only platform shaper that supports variable fonts. I can't easily test the fonts with non-freetype shapers. Thats why variable fonts are behind the `layout_variable_fonts_enabled` pref, which is disabled by default. <img width="1250" height="710" alt="image" src="https://github.com/user-attachments/assets/1aee1407-f3a2-42f6-a106-af0443fcd588" /> <details><summary>HTML test file</summary> ```html <style> @font-face { font-family: "Amstelvar VF"; src: url("https://mdn.github.io/shared-assets/fonts/variable-fonts/AmstelvarAlpha-VF.woff2") format("woff2-variations"); font-weight: 300 900; font-stretch: 35% 100%; font-style: normal; font-display: swap; } p { font: 1.2em "Amstelvar VF", Georgia, serif; font-size: 4rem; margin: 1rem; display: inline-block; } .p1 { font-variation-settings: "wght" 300; } .p2 { font-variation-settings: "wght" 625; } .p3 { font-variation-settings: "wght" 900; } </style> <div> <p class="p1">Weight</p> <span>(font-variation-settings: "wght" 300)</span> </div> <div> <p class="p2">Weight</p> <span>(font-variation-settings: "wght" 625)</span> </div> <div> <p class="p3">Weight</p> <span>(font-variation-settings: "wght" 900)</span> </div> </div> ``` </details> https://github.com/user-attachments/assets/9e21101a-796a-49fe-b82c-8999d8fa9ee1 Testing: Needs decision on whether we want to enable the pref in CI Works towards https://github.com/servo/servo/issues/37236 Depends on https://github.com/servo/stylo/pull/230 --------- Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
parent
ce16fbce75
commit
7471ad7730
19 changed files with 274 additions and 79 deletions
|
@ -21,7 +21,7 @@ use style::computed_values::font_weight::T as StyleFontWeight;
|
|||
use style::values::computed::font::FontStyle as StyleFontStyle;
|
||||
use truetype::tables::WindowsMetrics;
|
||||
use truetype::value::Read;
|
||||
use webrender_api::FontInstanceFlags;
|
||||
use webrender_api::{FontInstanceFlags, FontVariation};
|
||||
|
||||
use super::font_list::LocalFontIdentifier;
|
||||
use crate::{
|
||||
|
@ -116,6 +116,7 @@ impl PlatformFontMethods for PlatformFont {
|
|||
_font_identifier: FontIdentifier,
|
||||
data: &FontData,
|
||||
pt_size: Option<Au>,
|
||||
_variations: &[FontVariation],
|
||||
) -> Result<Self, &'static str> {
|
||||
let font_face = FontFile::new_from_buffer(Arc::new(data.clone()))
|
||||
.ok_or("Could not create FontFile")?
|
||||
|
@ -130,6 +131,7 @@ impl PlatformFontMethods for PlatformFont {
|
|||
fn new_from_local_font_identifier(
|
||||
font_identifier: LocalFontIdentifier,
|
||||
pt_size: Option<Au>,
|
||||
_variations: &[FontVariation],
|
||||
) -> Result<PlatformFont, &'static str> {
|
||||
let font_face = FontCollection::system()
|
||||
.font_from_descriptor(&font_identifier.font_descriptor)
|
||||
|
@ -320,4 +322,9 @@ impl PlatformFontMethods for PlatformFont {
|
|||
Size2D::new(width, height),
|
||||
)
|
||||
}
|
||||
|
||||
fn variations(&self) -> &[FontVariation] {
|
||||
// FIXME: implement this for windows
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue