Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.2.36"
version = "0.2.37"
authors = ["Raphael Amorim <rapha850@gmail.com>"]
edition = "2021"
license = "MIT"
Expand All @@ -28,15 +28,15 @@ readme = "README.md"
# Note: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#multiple-locations
# Sugarloaf example uses path when used locally, but uses
# version from crates.io when published.
teletypewriter = { path = "teletypewriter", version = "0.2.36" }
rio-backend = { path = "rio-backend", version = "0.2.36" }
rio-window = { path = "rio-window", version = "0.2.36", default-features = false }
sugarloaf = { path = "sugarloaf", version = "0.2.36" }
teletypewriter = { path = "teletypewriter", version = "0.2.37" }
rio-backend = { path = "rio-backend", version = "0.2.37" }
rio-window = { path = "rio-window", version = "0.2.37", default-features = false }
sugarloaf = { path = "sugarloaf", version = "0.2.37" }

# Own dependencies
copa = { path = "copa", default-features = true, version = "0.2.36" }
rio-proc-macros = { path = "rio-proc-macros", version = "0.2.36" }
corcovado = { path = "corcovado", version = "0.2.36" }
copa = { path = "copa", default-features = true, version = "0.2.37" }
rio-proc-macros = { path = "rio-proc-macros", version = "0.2.37" }
corcovado = { path = "corcovado", version = "0.2.37" }
raw-window-handle = { version = "0.6.2", features = ["std"] }
parking_lot = { version = "0.12.4", features = [
"nightly",
Expand Down
7 changes: 6 additions & 1 deletion docs/src/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ language: 'en'
- Kitty image protocol.
- Breaking: `Decorations` as `Transparent` is default on MacOS (instead of `Enabled`).

## 0.2.37

- Fix font loader for fallbacks and extra.
- Fix font size updating through config.

## 0.2.36

- Fix: handler should process two intermediate bytes in CSI sequences by [@aymanbagabas](https://github.com/aymanbagabas).
- Fix handler should process two intermediate bytes in CSI sequences by [@aymanbagabas](https://github.com/aymanbagabas).
- Fix DECSCUSR.

## 0.2.35
Expand Down
2 changes: 1 addition & 1 deletion sugarloaf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ twox-hash = { version = "2.1.1", default-features = false, features = [
memmap2 = { workspace = true }
font-kit = "0.14.3"
walkdir = "2.5.0"
ttf-parser = { version = "0.25.1", default-features = false, features = ["opentype-layout", "apple-layout", "variable-fonts", "glyph-names", "no-std-float"] }
ttf-parser = { version = "0.25.1", default-features = false, features = ["std", "opentype-layout", "apple-layout", "variable-fonts", "glyph-names"] }

[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "android"))))'.dependencies]
fontconfig-parser = { version = "0.5.8", default-features = false }
Expand Down
1 change: 0 additions & 1 deletion sugarloaf/src/font/fallbacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(target_os = "macos")]
pub fn external_fallbacks() -> Vec<String> {
vec![
String::from(".SF NS"),
String::from("Menlo"),
String::from("Geneva"),
String::from("Arial Unicode MS"),
Expand Down
146 changes: 146 additions & 0 deletions sugarloaf/src/font/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ impl Database {
bytes.len(),
font_index
);
// Try to find the actual file path for this font
if let Some(path) = find_font_path_from_data(&bytes) {
tracing::debug!(
"face_source: Found file path for memory font: {}",
path.display()
);
return Some((Source::File(path), font_index));
}
// Fallback to binary if path not found
return Some((
Source::Binary(SharedData::new(bytes.to_vec())),
font_index,
Expand All @@ -550,3 +559,140 @@ impl Default for Database {
Self::new()
}
}

#[cfg(target_os = "macos")]
const SYSTEM_FONT_DIRS: &[&str] = &[
"/Library/Fonts",
"/System/Library/Fonts",
"/System/Library/AssetsV2",
"/Network/Library/Fonts",
];

#[cfg(target_os = "windows")]
const SYSTEM_FONT_DIRS: &[&str] = &[
// Note: actual paths resolved at runtime using environment variables
];

#[cfg(all(unix, not(target_os = "macos")))]
const SYSTEM_FONT_DIRS: &[&str] = &[
"/usr/share/fonts",
"/usr/local/share/fonts",
];

#[cfg(not(target_arch = "wasm32"))]
fn get_font_name(face: &ttf_parser::Face) -> Option<String> {
face.names()
.into_iter()
.find(|n| n.name_id == ttf_parser::name_id::POST_SCRIPT_NAME && n.is_unicode())
.and_then(|n| n.to_string())
.or_else(|| {
face.names()
.into_iter()
.find(|n| n.name_id == ttf_parser::name_id::FAMILY && n.is_unicode())
.and_then(|n| n.to_string())
})
}

#[cfg(not(target_arch = "wasm32"))]
fn get_font_dirs() -> Vec<PathBuf> {
let mut dirs: Vec<PathBuf> = SYSTEM_FONT_DIRS.iter().map(PathBuf::from).collect();

#[cfg(target_os = "macos")]
if let Some(home) = std::env::var_os("HOME") {
dirs.push(PathBuf::from(home).join("Library/Fonts"));
}

#[cfg(target_os = "windows")]
{
// System fonts
if let Some(windir) = std::env::var_os("SYSTEMROOT") {
dirs.push(PathBuf::from(windir).join("Fonts"));
}
// User fonts
if let Some(profile) = std::env::var_os("USERPROFILE") {
let profile = PathBuf::from(profile);
dirs.push(profile.join("AppData/Local/Microsoft/Windows/Fonts"));
dirs.push(profile.join("AppData/Roaming/Microsoft/Windows/Fonts"));
}
}

#[cfg(all(unix, not(target_os = "macos")))]
if let Some(home) = std::env::var_os("HOME") {
let home = PathBuf::from(home);
dirs.push(home.join(".fonts"));
dirs.push(home.join(".local/share/fonts"));
}

dirs
}

// find the file path for a font given its binary data.
// parses the font to get its PostScript name, then searches system directories.
#[cfg(not(target_arch = "wasm32"))]
fn find_font_path_from_data(data: &[u8]) -> Option<PathBuf> {
use memmap2::Mmap;
use std::fs::File;
use walkdir::WalkDir;

// Parse font to get its name
let face = ttf_parser::Face::parse(data, 0).ok()?;
let target_name = get_font_name(&face)?;
let target_name_lower = target_name.to_lowercase();

tracing::debug!("find_font_path_from_data: searching for '{}'", target_name);

// Search each directory for the font file
for dir in get_font_dirs() {
if !dir.exists() {
continue;
}

for entry in WalkDir::new(&dir)
.into_iter()
.filter_map(|e| e.ok())
{
let path = entry.path();
if !path.is_file() {
continue;
}

// Check extension
let ext = path
.extension()
.and_then(|e| e.to_str())
.map(|e| e.to_lowercase());

match ext.as_deref() {
Some("ttf") | Some("otf") | Some("ttc") | Some("otc") => {}
_ => continue,
}

// Use memory mapping for efficient file access
let Ok(file) = File::open(path) else {
continue;
};
let Ok(mmap) = (unsafe { Mmap::map(&file) }) else {
continue;
};

// Handle font collections (TTC/OTC) - check all faces
let face_count = ttf_parser::fonts_in_collection(&mmap).unwrap_or(1);
for index in 0..face_count {
if let Ok(file_face) = ttf_parser::Face::parse(&mmap, index) {
if let Some(file_name) = get_font_name(&file_face) {
if file_name.to_lowercase() == target_name_lower {
tracing::debug!(
"find_font_path_from_data: found match at {}",
path.display()
);
return Some(path.to_path_buf());
}
}
}
}
}
}

tracing::debug!("find_font_path_from_data: no file found for '{}'", target_name);
None
}
Loading