mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
The `FontContextHandle` was really only used on FreeType platforms to store the `FT_Library` handle to use for creating faces. Each `FontContext` and `FontCacheThread` would create its own `FontContextHandle`. This change removes this data structure in favor of a mutex-protected shared `FontContextHandle` for an entire Servo process. The handle is initialized using a `OnceLock` to ensure that it only happens once and also that it stays alive for the entire process lifetime. In addition to greatly simplifying the code, this will make it possible for different threads to share platform-specific `FontHandle`s, avoiding multiple allocations for a single font. The only downside to all of this is that memory usage of FreeType fonts isn't measured (though the mechanism is still there). This is because the `FontCacheThread` currently doesn't do any memory measurement. Eventually this *will* happen though, during the font system redesign. In exchange, this should reduce the memory usage since there is only a single FreeType library loaded into memory now. This is part of #32033.
75 lines
2.2 KiB
Rust
75 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 gfx::font_cache_thread::FontIdentifier;
|
|
use gfx::font_template::{FontTemplate, FontTemplateDescriptor};
|
|
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 file = File::open(path.clone()).unwrap();
|
|
let mut template = FontTemplate::new(
|
|
FontIdentifier::Web(ServoUrl::from_file_path(path).unwrap()),
|
|
Some(file.bytes().map(|b| b.unwrap()).collect()),
|
|
)
|
|
.unwrap();
|
|
|
|
template.descriptor().unwrap()
|
|
}
|
|
|
|
assert_eq!(
|
|
descriptor("DejaVuSans"),
|
|
FontTemplateDescriptor {
|
|
weight: FontWeight::NORMAL,
|
|
stretch: FontStretch::hundred(),
|
|
style: FontStyle::NORMAL,
|
|
}
|
|
);
|
|
|
|
assert_eq!(
|
|
descriptor("DejaVuSans-Bold"),
|
|
FontTemplateDescriptor {
|
|
weight: FontWeight::BOLD,
|
|
stretch: FontStretch::hundred(),
|
|
style: FontStyle::NORMAL,
|
|
}
|
|
);
|
|
|
|
assert_eq!(
|
|
descriptor("DejaVuSans-Oblique"),
|
|
FontTemplateDescriptor {
|
|
weight: FontWeight::NORMAL,
|
|
stretch: FontStretch::hundred(),
|
|
style: FontStyle::ITALIC,
|
|
}
|
|
);
|
|
|
|
assert_eq!(
|
|
descriptor("DejaVuSansCondensed-BoldOblique"),
|
|
FontTemplateDescriptor {
|
|
weight: FontWeight::BOLD,
|
|
stretch: FontStretch::from_percentage(0.875),
|
|
style: FontStyle::ITALIC,
|
|
}
|
|
);
|
|
}
|