mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
System fonts used to be instantiated using the system font loader and this change restores that behavior. In addition, on macOS and FreeType platforms font data for system fonts is loaded using memory mapping. The benefit is that system font loaders typically are able to cache fonts in system memory (using memory mapping, for instance) and we'd like to load them in a the way most compatible with other applications. On my Linux system, this manages to get the overhead of loading a very large font down from 10ms to approximately 1ms. Subsequent runs show even less overhead. We've measured similar gains on macOS systems. Currently, system font data must be loaded into memory manually for canvas and this is unlikely to change even with a switch to `vello`. The use of explicit memmory mapping should help in this case -- though it probably won't be possible to use this properly on macOS and Windows if we ever want to load fonts from TTCs properly. Signed-off-by: Martin Robinson <mrobinson@igalia.com> Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
74 lines
2.2 KiB
Rust
74 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 fonts::platform::font::PlatformFont;
|
|
use fonts::{FontData, FontIdentifier, FontTemplateDescriptor, PlatformFontMethods};
|
|
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 mut bytes = Vec::new();
|
|
File::open(path.clone())
|
|
.expect("Couldn't open font file!")
|
|
.read_to_end(&mut bytes)
|
|
.unwrap();
|
|
let data = FontData::from_bytes(&bytes);
|
|
|
|
let handle = PlatformFont::new_from_data(identifier, &data, 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,
|
|
)
|
|
);
|
|
}
|