diff --git a/.changeset/system-fonts.md b/.changeset/system-fonts.md new file mode 100644 index 00000000..3de72b9f --- /dev/null +++ b/.changeset/system-fonts.md @@ -0,0 +1,6 @@ +### Changes +- **Fonts installed on your computer** - The Font menu in Settings > Appearance now lists every font installed on the machine, each name drawn in its own face. A monospace font sets the whole app; any other font sets the interface and leaves data and code in the pairing's code font. The Separate fonts pickers list them too. +- **Font pairings name their faces** - Each pairing in the Font menu now says which two fonts it sets, for example "Inter + Source Code Pro". + +### Fixes +- **The SQL editor and terminal follow the Font setting** - They were pinned to JetBrains Mono, so picking Source Code Pro or Fira Code changed the data grid but not the editor. diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 7a7260ad..2a3e717d 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -180,6 +180,7 @@ dependencies = [ "ed25519-dalek", "encoding_rs", "flate2", + "fontdb", "futures", "gdk", "getrandom 0.2.17", @@ -224,6 +225,7 @@ dependencies = [ "tokio-tungstenite", "tokio-util", "tower-http 0.5.2", + "ttf-parser", "urlencoding", "uuid", "webkit2gtk", @@ -2341,6 +2343,28 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" +[[package]] +name = "fontconfig-parser" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" +dependencies = [ + "roxmltree", +] + +[[package]] +name = "fontdb" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2660c5e9157bf76d2db1294e4a9feba604ef610819a3b591088d0d8392a3290f" +dependencies = [ + "fontconfig-parser", + "log", + "memmap2", + "slotmap", + "tinyvec", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -3910,6 +3934,15 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" +[[package]] +name = "memmap2" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" +dependencies = [ + "libc", +] + [[package]] name = "memoffset" version = "0.9.1" @@ -5754,6 +5787,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "roxmltree" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" + [[package]] name = "rsa" version = "0.9.10" @@ -6544,6 +6583,15 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +[[package]] +name = "slotmap" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdd58c3c93c3d278ca835519292445cb4b0d4dc59ccfdf7ceadaab3f8aeb4038" +dependencies = [ + "version_check", +] + [[package]] name = "smallvec" version = "1.15.1" @@ -8063,6 +8111,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "ttf-parser" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" + [[package]] name = "tungstenite" version = "0.24.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index e586bb28..1e98dba6 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -20,6 +20,9 @@ tauri-build = { version = "2.6.2", features = [] } [dependencies] serde_json = { version = "1.0", features = ["preserve_order", "raw_value"] } serde = { version = "1.0", features = ["derive"] } +# Lists the installed font families for the font pickers (system_fonts.rs). +fontdb = "0.24" +ttf-parser = "0.25" log = "0.4" tauri = { version = "2.11.2", features = ["macos-private-api", "image-png"] } tauri-plugin-opener = "2" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index b138e967..a7510fdb 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -38,6 +38,7 @@ mod proc; pub(crate) mod proxy; mod providers; mod secrets; +mod system_fonts; mod sync; mod updates; mod quit; @@ -966,6 +967,7 @@ pub fn run() { secrets::conn_secrets_write, secrets::conn_secrets_read_fresh, secrets::conn_secrets_merge, + system_fonts::system_fonts, metrics::get_app_metrics, metrics::set_process_title, commands::enable_autostart, diff --git a/src-tauri/src/system_fonts.rs b/src-tauri/src/system_fonts.rs new file mode 100644 index 00000000..0a1785d3 --- /dev/null +++ b/src-tauri/src/system_fonts.rs @@ -0,0 +1,118 @@ +//! The font families installed on this machine, for the font pickers in +//! Settings > Appearance. The webview can draw any installed family by name, but +//! WebKit has no API to list them (`queryLocalFonts` is Chromium-only), so the +//! list comes from here: fontdb finds the files in the OS font folders (and, +//! on Linux, the fontconfig setup), ttf-parser reads what each one can draw. + +use serde::Serialize; +use std::collections::BTreeMap; +use std::sync::OnceLock; + +#[derive(Clone, Serialize)] +pub struct SystemFont { + pub family: String, + /// Every face of the family is fixed-width. + pub mono: bool, +} + +static FONTS: OnceLock> = OnceLock::new(); + +/// What a face can draw, read from its tables: `None` when it cannot be read +/// or has no Unicode Latin letters (emoji, symbol, dingbat and CJK-only faces, +/// which would draw a font's name in pictures), else whether it is fixed-width. +/// The fixed-pitch flag alone is wrong both ways (FreeMono lacks it), so equal +/// advances for `i` and `M` count too. +fn latin_face(data: &[u8], index: u32) -> Option { + let face = ttf_parser::Face::parse(data, index).ok()?; + // glyph_index only reads Unicode cmap subtables: a symbol-encoded font + // (Standard Symbols, the URW dingbats) has none for these. + let glyph = |c: char| face.glyph_index(c); + let (i, m) = (glyph('i')?, glyph('M')?); + glyph('0')?; + // Some symbol fonts map ASCII too, to other shapes (Standard Symbols puts + // alpha at `a`). A named glyph for `a` must be named for it; a font without + // glyph names (post table version 3) is taken at its cmap's word. + let a = glyph('a')?; + if let Some(name) = face.glyph_name(a) { + if !(name == "a" || name.starts_with("a.") || name.eq_ignore_ascii_case("uni0061")) { + return None; + } + } + let advance = |g| face.glyph_hor_advance(g).unwrap_or(0); + Some(face.is_monospaced() || (advance(i) > 0 && advance(i) == advance(m))) +} + +fn scan() -> Vec { + let mut db = fontdb::Database::new(); + db.load_system_fonts(); + // Family name -> fixed-width, judged from its first readable face (the + // regular one in practice; a family's faces share their pitch). + let mut by_family: BTreeMap> = BTreeMap::new(); + for face in db.faces() { + // The first name is the English (or only) one, the one CSS matches. + let Some((family, _)) = face.families.first() else { continue }; + let family = family.trim(); + // Dot-prefixed families are macOS's private system faces (.SF NS, ...): + // WebKit refuses them by name, and SF Pro is offered as "System UI". + if family.is_empty() || family.starts_with('.') { + continue; + } + if matches!(by_family.get(family), Some(Some(_))) { + continue; + } + let judged = db.with_face_data(face.id, latin_face).flatten(); + by_family.insert(family.to_string(), judged); + } + by_family + .into_iter() + .filter_map(|(family, mono)| mono.map(|mono| SystemFont { family, mono })) + .collect() +} + +fn sorted(mut fonts: Vec) -> Vec { + fonts.sort_by_cached_key(|f| f.family.to_lowercase()); + fonts +} + +/// Installed families, sorted by name. Scanned once per launch: reading every +/// font file's header takes a few hundred ms on a machine with many fonts. +#[tauri::command] +pub async fn system_fonts() -> Result, String> { + if let Some(f) = FONTS.get() { + return Ok(f.clone()); + } + let fonts = tauri::async_runtime::spawn_blocking(|| sorted(scan())) + .await + .map_err(|e| format!("Could not list the installed fonts: {e}"))?; + Ok(FONTS.get_or_init(|| fonts).clone()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sorts_by_name_ignoring_case() { + let fonts = sorted(vec![ + SystemFont { family: "ubuntu".into(), mono: false }, + SystemFont { family: "Adwaita Mono".into(), mono: true }, + SystemFont { family: "Liberation Mono".into(), mono: true }, + ]); + let names: Vec<_> = fonts.iter().map(|f| f.family.as_str()).collect(); + assert_eq!(names, ["Adwaita Mono", "Liberation Mono", "ubuntu"]); + } + + #[test] + fn scan_lists_only_faces_that_draw_latin() { + let fonts = scan(); + assert!(fonts.iter().all(|f| !f.family.starts_with('.') && !f.family.is_empty())); + // Where this machine has them: symbol and emoji faces are left out, and + // FreeMono (no fixed-pitch flag) still counts as fixed-width. + for hidden in ["Standard Symbols PS", "D050000L", "Noto Color Emoji"] { + assert!(fonts.iter().all(|f| f.family != hidden), "{hidden} listed"); + } + if let Some(free) = fonts.iter().find(|f| f.family == "FreeMono") { + assert!(free.mono); + } + } +} diff --git a/src/app.css b/src/app.css index fb6c8d86..72a5e2c9 100644 --- a/src/app.css +++ b/src/app.css @@ -84,11 +84,11 @@ --editor-font-size: 13px; /* 1.5 ratio - matches VS Code / Cursor's default editor line height. */ --editor-line-height: 20px; - /* Dedicated code font for the code editors, kept independent of the app's UI / - data-grid mono (`--font-mono`) - same separation VS Code makes between its - "editor font" and everything else. JetBrains Mono is a true code typeface: - heavier, even rhythm, designed ligatures. */ - --editor-font-family: "JetBrains Mono Variable", "Geist Mono Variable", ui-monospace, monospace; + /* No --editor-font-family here: the SQL editor and the terminal read + `var(--editor-font-family, var(--font-mono))`, so they follow the font + picked in Settings like the data grid does. Separate fonts sets it when the + editor gets a face of its own. (It used to pin JetBrains Mono, so picking + Source Code Pro or Fira Code left the editor and terminal in another face.) */ /* ── Elevation system ────────────────────────────────────────────────────── Layered shadows for raised surfaces (menus, popovers, dialogs, cards). @@ -548,10 +548,10 @@ html[data-os="linux"] { --window-radius: 8px; } and Linux. System fonts (SF Pro, Segoe UI, desktop sans) and native OS smoothing already render at their designed weight, so never synthesize weights on them. */ - html[data-os="windows"][data-smoothing="grayscale"]:not([data-font="apple"]) { + html[data-os="windows"][data-smoothing="grayscale"]:not([data-font="apple"]):not([data-font="custom"]) { font-weight: 430; } - html[data-os="linux"][data-smoothing="grayscale"]:not([data-font="apple"]) { + html[data-os="linux"][data-smoothing="grayscale"]:not([data-font="apple"]):not([data-font="custom"]) { font-weight: 450; } /* Small UI text: let the face's own kerning and contextual forms apply on @@ -706,7 +706,7 @@ html[data-os="linux"] { --window-radius: 8px; } font-optical-sizing: auto; } - html[data-os='windows'][data-smoothing='grayscale']:not([data-font='apple']) :is( + html[data-os='windows'][data-smoothing='grayscale']:not([data-font='apple']):not([data-font='custom']) :is( [data-slot='context-menu-content'], [data-slot='dropdown-menu-content'], [data-slot='select-content'], @@ -719,7 +719,7 @@ html[data-os="linux"] { --window-radius: 8px; } font-weight: 430; } - html[data-os='linux'][data-smoothing='grayscale']:not([data-font='apple']) :is( + html[data-os='linux'][data-smoothing='grayscale']:not([data-font='apple']):not([data-font='custom']) :is( [data-slot='context-menu-content'], [data-slot='dropdown-menu-content'], [data-slot='select-content'], diff --git a/src/lib/components/SelectMenu.svelte b/src/lib/components/SelectMenu.svelte index 5ad2481d..7e4c1d98 100644 --- a/src/lib/components/SelectMenu.svelte +++ b/src/lib/components/SelectMenu.svelte @@ -4,7 +4,10 @@ // regardless of how many items it holds (see `searchThreshold` there). // Drop-in replacement for bits-ui