servo/components/gfx/tests/font_template.rs
Martin Robinson 4732da3477
fonts: Add support for more @font-face features (#32164)
There are a couple major changes here:

1. Support is added for the `weight`, `style`, `stretch` and
   `unicode-range` declarations in `@font-face`.
2. Font matching in the font cache can return templates and
   `FontGroupFamily` can own mulitple templates. This is due to needing
   support for "composite fonts". These are `@font-face` declarations
   that only differ in their `unicode-range` definition.

This fixes a lot of non-determinism in font selection especially when
dealing with pages that define "composite faces." A notable example of
such a page is servo.org, which now consistently displays the correct
web font.

One test starts to fail due to an uncovered bug, but this will be fixed
in a followup change.

Fixes #20686.
Fixes #20684.

Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
2024-04-29 17:02:07 +00:00

71 lines
2.2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// Test doesn't yet run on Mac, see https://github.com/servo/servo/pull/19928 for explanation.
#[cfg(not(target_os = "macos"))]
#[test]
fn test_font_template_descriptor() {
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::sync::Arc;
use gfx::font::PlatformFontMethods;
use gfx::font_cache_thread::FontIdentifier;
use gfx::font_template::FontTemplateDescriptor;
use gfx::platform::font::PlatformFont;
use servo_url::ServoUrl;
use style::values::computed::font::{FontStretch, FontStyle, FontWeight};
fn descriptor(filename: &str) -> FontTemplateDescriptor {
let mut path: PathBuf = [
env!("CARGO_MANIFEST_DIR"),
"tests",
"support",
"dejavu-fonts-ttf-2.37",
"ttf",
]
.iter()
.collect();
path.push(format!("{}.ttf", filename));
let identifier = FontIdentifier::Web(ServoUrl::from_file_path(path.clone()).unwrap());
let file = File::open(path.clone()).unwrap();
let data = file.bytes().map(|b| b.unwrap()).collect();
let handle = PlatformFont::new_from_data(identifier, Arc::new(data), 0, None).unwrap();
handle.descriptor()
}
assert_eq!(
descriptor("DejaVuSans"),
FontTemplateDescriptor::new(
FontWeight::NORMAL,
FontStretch::hundred(),
FontStyle::NORMAL,
)
);
assert_eq!(
descriptor("DejaVuSans-Bold"),
FontTemplateDescriptor::new(FontWeight::BOLD, FontStretch::hundred(), FontStyle::NORMAL,)
);
assert_eq!(
descriptor("DejaVuSans-Oblique"),
FontTemplateDescriptor::new(
FontWeight::NORMAL,
FontStretch::hundred(),
FontStyle::ITALIC,
)
);
assert_eq!(
descriptor("DejaVuSansCondensed-BoldOblique"),
FontTemplateDescriptor::new(
FontWeight::BOLD,
FontStretch::from_percentage(0.875),
FontStyle::ITALIC,
)
);
}