Ports the macOS-specific Zig code behind cfg(target_os = "macos"): - renderer: a GraphicsApi trait with generic::Renderer<G>; Metal backend (all renderer/metal/* files, shaders.metal verbatim, runtime library compile, IOSurface layer, CVDisplayLink), shadertoy -> MSL via naga; OpenGL remains the backend elsewhere. - font: CoreText face, shaper and discovery with build.rs backend selection (coretext default; coretext-freetype / coretext-harfbuzz / coretext-noshape features), no fontconfig/freetype/harfbuzz on Apple. - input: KeymapDarwin via UCKeyTranslate, option-as-alt, mac keycodes. - os: macos, mach, cf_release_thread, NSProcessInfo args and macOS branches; config: macOS defaults, keybinds, Application Support paths. - termio/core: login(1) wrapping, openpty/execvpe, Darwin child exit handling, os_log logging. Compile-checked for aarch64/x86_64-apple-darwin (clippy -D warnings clean) using the cross setup in docs/rewrite/notes/macos-M1.md; cannot run here. The embedded runtime/C API (embedded.zig) is the next step. Also includes the cross-platform integration edits shared by the crash, verification, Windows and macOS work (Cargo target-gating, thread crash-context hooks, apprt-gtk surface/IME refactor, termio/os cfg gates), a cfg fix so ghostty-config builds on wasm32, and an allow for the intentional recursion in the crash overflow test. Test Plan: - cargo test --workspace (3224 passed) - cargo clippy --workspace --all-targets -- -D warnings - cargo check --target aarch64-apple-darwin -p ghostty-renderer (with the documented cross env)
231 lines
6.9 KiB
Rust
231 lines
6.9 KiB
Rust
//! Font discovery. Port of `font/discovery.zig`: the fontconfig backend
|
|
//! (Linux, in this file) and the CoreText backend (Apple targets, in
|
|
//! `discovery_coretext.rs`).
|
|
|
|
#[cfg(font_fontconfig)]
|
|
pub use fc_impl::*;
|
|
|
|
#[cfg(font_coretext)]
|
|
#[path = "discovery_coretext.rs"]
|
|
mod coretext_impl;
|
|
#[cfg(font_coretext)]
|
|
pub use coretext_impl::*;
|
|
/// Descriptor is used to search for fonts (canonical definition in `ghostty-config`).
|
|
pub use ghostty_config::font_types::Descriptor;
|
|
|
|
#[cfg(font_fontconfig)]
|
|
mod fc_impl {
|
|
use std::sync::Arc;
|
|
|
|
use super::Descriptor;
|
|
use crate::{
|
|
collection::Collection,
|
|
deferred_face::{DeferredFace, Fontconfig as DeferredFontconfig},
|
|
face::Variation,
|
|
fontconfig as fc,
|
|
};
|
|
|
|
/// Discover implementation for the compile options.
|
|
pub type Discover = Fontconfig;
|
|
|
|
/// The error type of [`DiscoverIterator::next`].
|
|
pub type NextError = fc::Error;
|
|
|
|
/// Convert to Fontconfig pattern to use for lookup. The pattern does
|
|
/// not have defaults filled/substituted (Fontconfig thing) so callers
|
|
/// must still do this. (`Descriptor.toFcPattern`)
|
|
pub fn to_fc_pattern(desc: &Descriptor) -> fc::Pattern {
|
|
let mut pat = fc::Pattern::create();
|
|
if let Some(family) = &desc.family {
|
|
assert!(pat.add_string(fc::Property::Family, family));
|
|
}
|
|
if let Some(style) = &desc.style {
|
|
assert!(pat.add_string(fc::Property::Style, style));
|
|
}
|
|
if desc.codepoint > 0 {
|
|
let mut cs = fc::CharSet::create();
|
|
assert!(cs.add_char(desc.codepoint));
|
|
assert!(pat.add_charset(fc::Property::Charset, &cs));
|
|
}
|
|
if desc.size > 0.0 {
|
|
assert!(pat.add_integer(fc::Property::Size, desc.size.round() as i32));
|
|
}
|
|
if desc.bold {
|
|
assert!(pat.add_integer(fc::Property::Weight, fc::Weight::Bold as i32));
|
|
}
|
|
if desc.italic {
|
|
assert!(pat.add_integer(fc::Property::Slant, fc::Slant::Italic as i32));
|
|
}
|
|
|
|
// For fontconfig, we always add monospace in the pattern. Since
|
|
// fontconfig sorts by closeness to the pattern, this doesn't fully
|
|
// exclude non-monospace but helps prefer it.
|
|
assert!(pat.add_integer(fc::Property::Spacing, fc::Spacing::Mono as i32));
|
|
|
|
pat
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Fontconfig {
|
|
fc_config: Arc<fc::Config>,
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum DiscoverError {
|
|
#[error("fontconfig failed")]
|
|
FontConfigFailed,
|
|
#[error(transparent)]
|
|
Fontconfig(#[from] fc::Error),
|
|
}
|
|
|
|
impl Fontconfig {
|
|
pub fn init() -> Fontconfig {
|
|
// safe to call multiple times and concurrently
|
|
fc::init();
|
|
Fontconfig {
|
|
fc_config: Arc::new(
|
|
fc::Config::init_load_config_and_fonts()
|
|
.expect("FcInitLoadConfigAndFonts returned null"),
|
|
),
|
|
}
|
|
}
|
|
|
|
/// Discover fonts from a descriptor. This returns an iterator that can
|
|
/// be used to build up the deferred fonts.
|
|
pub fn discover(&self, desc: &Descriptor) -> Result<DiscoverIterator, DiscoverError> {
|
|
// Build our pattern that we'll search for
|
|
let mut pat = to_fc_pattern(desc);
|
|
assert!(self.fc_config.substitute_with_pat_pattern(&mut pat));
|
|
pat.default_substitute();
|
|
|
|
// Search
|
|
let set = self
|
|
.fc_config
|
|
.font_sort(&mut pat, false)
|
|
.map_err(|_| DiscoverError::FontConfigFailed)?;
|
|
|
|
Ok(DiscoverIterator {
|
|
config: Arc::clone(&self.fc_config),
|
|
pattern: pat,
|
|
set,
|
|
variations: desc.variations.clone(),
|
|
i: 0,
|
|
})
|
|
}
|
|
|
|
pub fn discover_fallback(
|
|
&self,
|
|
_collection: &mut Collection,
|
|
desc: &Descriptor,
|
|
) -> Result<DiscoverIterator, DiscoverError> {
|
|
self.discover(desc)
|
|
}
|
|
}
|
|
|
|
pub struct DiscoverIterator {
|
|
config: Arc<fc::Config>,
|
|
pattern: fc::Pattern,
|
|
set: fc::FontSet,
|
|
variations: Vec<Variation>,
|
|
i: usize,
|
|
}
|
|
|
|
impl DiscoverIterator {
|
|
#[allow(clippy::should_implement_trait)]
|
|
pub fn next(&mut self) -> Result<Option<DeferredFace>, fc::Error> {
|
|
if self.i >= self.set.len() {
|
|
return Ok(None);
|
|
}
|
|
|
|
// Get the copied pattern from our fontset that has the
|
|
// attributes configured for rendering.
|
|
let font_pattern =
|
|
self.config
|
|
.font_render_prepare(&mut self.pattern, &self.set, self.i)?;
|
|
|
|
// The charset and langset must exist (they are used for lookups).
|
|
font_pattern.charset_has_char(0)?;
|
|
font_pattern.langset_has_lang("")?;
|
|
|
|
// Increment after we return
|
|
self.i += 1;
|
|
|
|
Ok(Some(DeferredFace::from_fontconfig(DeferredFontconfig {
|
|
pattern: font_pattern,
|
|
variations: self.variations.clone(),
|
|
})))
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn descriptor_hash() {
|
|
let d = Descriptor::default();
|
|
assert_ne!(d.hashcode(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn descriptor_hash_family_names() {
|
|
let d1 = Descriptor {
|
|
family: Some("A".into()),
|
|
..Default::default()
|
|
};
|
|
let d2 = Descriptor {
|
|
family: Some("B".into()),
|
|
..Default::default()
|
|
};
|
|
assert_ne!(d1.hashcode(), d2.hashcode());
|
|
}
|
|
|
|
#[test]
|
|
fn descriptor_hash_is_deterministic_and_covers_fields() {
|
|
let a = Descriptor {
|
|
family: Some("A".into()),
|
|
bold: true,
|
|
..Default::default()
|
|
};
|
|
assert_eq!(a.hashcode(), a.clone().hashcode());
|
|
let mut b = a.clone();
|
|
b.italic = true;
|
|
assert_ne!(a.hashcode(), b.hashcode());
|
|
}
|
|
|
|
#[cfg(font_fontconfig)]
|
|
#[test]
|
|
fn fontconfig() {
|
|
let fc = Fontconfig::init();
|
|
let _it = fc
|
|
.discover(&Descriptor {
|
|
family: Some("monospace".into()),
|
|
size: 12.0,
|
|
..Default::default()
|
|
})
|
|
.unwrap();
|
|
}
|
|
|
|
#[cfg(font_fontconfig)]
|
|
#[test]
|
|
fn fontconfig_codepoint() {
|
|
let fc = Fontconfig::init();
|
|
let mut it = fc
|
|
.discover(&Descriptor {
|
|
codepoint: 'A' as u32,
|
|
size: 12.0,
|
|
..Default::default()
|
|
})
|
|
.unwrap();
|
|
|
|
// The first result should have the codepoint. Later ones may not
|
|
// because fontconfig returns all fonts sorted.
|
|
let face = it.next().unwrap().unwrap();
|
|
assert!(face.has_codepoint('A' as u32, None));
|
|
|
|
// Should have other codepoints too
|
|
assert!(face.has_codepoint('B' as u32, None));
|
|
}
|
|
}
|