fix: inspector font/formatting, FreeType-only backend on Apple
Resolves the parity items of docs/rewrite/notes/review-rv17-codex.md (final-Z20.md): - FreeType-only font backend is also available on Apple targets (discovery-free, embedded emoji fonts); ghostty_surface_quicklook_font returns null for every backend except plain CoreText like Zig. - The +list-fonts test keys off the font crate's selected backend (HAS_DISCOVERY), correct under feature unification and on Apple. - Inspector font: 32 px rasterized with FreeType (dear-imgui freetype) like Ghostty; a new `visual-parity.sh inspector` scenario shows 0.031% differing pixels (hover highlight) vs the original on Linux. Windows, Apple and wasm keep stb_truetype at 32 px (registered as a deliberate deviation). - Inspector DCS/APC descriptions follow Zig 0.15.2's formatting (checked by running Zig), incl. the terminal.Parser.Action prefix. - The ImGui demo window appears only in dev-profile builds, mirroring Zig's .Debug-only gate. Test Plan: - cargo test --workspace --no-fail-fast (3582 passed) - just check-macos (both, also with --features font-backend-freetype) - scripts/visual-parity.sh inspector
This commit is contained in:
1 parent
75fd25d1f1
commit
e536b603b2
18 files changed
+267
-83
No files matched your search
@@ -13,6 +13,13 @@ ghostty-terminal.workspace = true
|
||||
ghostty-vt.workspace = true
|
||||
log = "0.4"
|
||||
|
||||
# Zig builds Dear ImGui with `-DIMGUI_ENABLE_FREETYPE` (pkg/dcimgui), so the
|
||||
# inspector font is rasterized with FreeType. The `freetype` feature needs a
|
||||
# system FreeType (pkg-config); other targets keep stb_truetype (see
|
||||
# `docs/rewrite/DEVIANT_FIXES.md`).
|
||||
[target.'cfg(not(any(windows, target_vendor = "apple", target_arch = "wasm32")))'.dependencies]
|
||||
dear-imgui-rs = { version = "0.19", features = ["freetype"] }
|
||||
|
||||
[lints.rust]
|
||||
unsafe_code = "deny"
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
//! Emits `cfg(ghostty_debug_build)` for the Cargo `dev` profile (Zig
|
||||
//! `builtin.mode == .Debug`). `debug_assertions` is not equivalent: the
|
||||
//! repository's `release` profile enables it, like Zig's `ReleaseSafe`.
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-env-changed=PROFILE");
|
||||
println!("cargo:rustc-check-cfg=cfg(ghostty_debug_build)");
|
||||
let profile = std::env::var("PROFILE").unwrap_or_default();
|
||||
println!("cargo:rustc-env=GHOSTTY_BUILD_PROFILE={profile}");
|
||||
if profile == "debug" {
|
||||
println!("cargo:rustc-cfg=ghostty_debug_build");
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,19 @@
|
||||
|
||||
use std::sync::{Mutex, MutexGuard, PoisonError};
|
||||
|
||||
use dear_imgui_rs::{ConfigFlags, Context, FontSource, StbTrueTypeFontData, Ui};
|
||||
use dear_imgui_rs::{ConfigFlags, Context, FontSource, Ui};
|
||||
use ghostty_renderer::{overlay::Feature, state::InspectorHook};
|
||||
use ghostty_terminal::terminal::Terminal;
|
||||
|
||||
use crate::widgets;
|
||||
|
||||
/// The inspector font size in pixels. Zig hardcodes `16 * 2` ("This is
|
||||
/// currently hardcoded to a 2x content scale", with a TODO to recalculate it
|
||||
/// for different screen DPIs); the style is scaled separately by the apprt.
|
||||
pub const FONT_SIZE: f32 = 16.0 * 2.0;
|
||||
|
||||
/// Setup the ImGui state. This requires an ImGui context.
|
||||
///
|
||||
/// `scale_factor` is the content scale of the display; Zig hardcodes a 2x
|
||||
/// scale here (with a TODO) and sizes the font as `16 * 2`, this uses
|
||||
/// `16 * scale_factor` instead.
|
||||
pub fn setup(ctx: &mut Context, scale_factor: f64) {
|
||||
pub fn setup(ctx: &mut Context) {
|
||||
let io = ctx.io_mut();
|
||||
|
||||
// Enable docking, which we use heavily for the UI. Our colorspace is
|
||||
@@ -28,14 +29,28 @@ pub fn setup(ctx: &mut Context, scale_factor: f64) {
|
||||
log::warn!("failed to disable the imgui log file err={err}");
|
||||
}
|
||||
|
||||
// Use our own embedded font
|
||||
let font_size = (16.0 * scale_factor.max(1.0)) as f32;
|
||||
match StbTrueTypeFontData::from_slice(ghostty_font::embedded::REGULAR) {
|
||||
Ok(data) => {
|
||||
ctx.font_atlas()
|
||||
.add_font(&[FontSource::stb_truetype_with_size(data, font_size)]);
|
||||
// Use our own embedded font. The atlas rasterizes it with FreeType (the
|
||||
// default loader of the `freetype` build of Dear ImGui, as in Zig) where
|
||||
// that build is available, otherwise with stb_truetype.
|
||||
ctx.font_atlas().add_font(&[embedded_font_source()]);
|
||||
}
|
||||
|
||||
#[cfg(not(any(windows, target_vendor = "apple", target_arch = "wasm32")))]
|
||||
#[allow(unsafe_code)]
|
||||
fn embedded_font_source() -> FontSource<'static> {
|
||||
// SAFETY: the embedded font is a complete, valid TrueType font that
|
||||
// lives for the whole program; Dear ImGui copies it on add.
|
||||
unsafe { FontSource::ttf_data_with_size(ghostty_font::embedded::REGULAR, FONT_SIZE) }
|
||||
}
|
||||
|
||||
#[cfg(any(windows, target_vendor = "apple", target_arch = "wasm32"))]
|
||||
fn embedded_font_source() -> FontSource<'static> {
|
||||
match dear_imgui_rs::StbTrueTypeFontData::from_slice(ghostty_font::embedded::REGULAR) {
|
||||
Ok(data) => FontSource::stb_truetype_with_size(data, FONT_SIZE),
|
||||
Err(err) => {
|
||||
log::warn!("failed to load the embedded inspector font err={err}");
|
||||
FontSource::default_font_with_size(FONT_SIZE)
|
||||
}
|
||||
Err(err) => log::warn!("failed to load the embedded inspector font err={err}"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +125,18 @@ impl InspectorHook for Shared {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// Zig sizes the font as `16 * 2` and rasterizes with FreeType.
|
||||
#[test]
|
||||
fn font_size_and_rasterizer_match_zig() {
|
||||
assert!((super::FONT_SIZE - 32.0).abs() < f32::EPSILON);
|
||||
let freetype_target = cfg!(not(any(
|
||||
windows,
|
||||
target_vendor = "apple",
|
||||
target_arch = "wasm32"
|
||||
)));
|
||||
assert_eq!(dear_imgui_rs::HAS_FREETYPE, freetype_target);
|
||||
}
|
||||
|
||||
use ghostty_input::{Action, Key, KeyEvent};
|
||||
use ghostty_renderer::size::{CellSize, Padding, ScreenSize, Size};
|
||||
use ghostty_terminal::terminal::Options;
|
||||
@@ -146,7 +173,7 @@ mod tests {
|
||||
#[test]
|
||||
fn draws_frames_headless() {
|
||||
let mut ctx = Context::create();
|
||||
setup(&mut ctx, 1.0);
|
||||
setup(&mut ctx);
|
||||
ctx.io_mut().set_display_size([1000.0, 700.0]);
|
||||
ctx.io_mut().set_delta_time(1.0 / 60.0);
|
||||
ctx.font_atlas()
|
||||
|
||||
@@ -13,6 +13,10 @@ use super::{key, renderer, terminal, termio};
|
||||
use crate::dock::begin_window;
|
||||
|
||||
/// This is discovered via the hardcoded string in the ImGui demo window.
|
||||
/// True for the Cargo `dev` profile only (Zig `builtin.mode == .Debug`);
|
||||
/// set by `build.rs` from `PROFILE`, deliberately not `debug_assertions`.
|
||||
pub const DEBUG_BUILD: bool = cfg!(ghostty_debug_build);
|
||||
|
||||
const WINDOW_IMGUI_DEMO: &str = "Dear ImGui Demo";
|
||||
const WINDOW_KEYBOARD: &str = "Keyboard";
|
||||
const WINDOW_TERMINAL: &str = "Terminal";
|
||||
@@ -133,8 +137,10 @@ impl Inspector {
|
||||
crate::dock::flush_splits(ui);
|
||||
|
||||
// In debug we show the ImGui demo window so we can easily view
|
||||
// available widgets and such.
|
||||
if cfg!(debug_assertions) && self.show_demo_window {
|
||||
// available widgets and such. Zig checks `builtin.mode == .Debug`
|
||||
// (not `ReleaseSafe`), which is the Cargo `dev` profile; assertions
|
||||
// enabled in a release profile do not count.
|
||||
if DEBUG_BUILD && self.show_demo_window {
|
||||
ui.show_demo_window(&mut self.show_demo_window);
|
||||
}
|
||||
|
||||
@@ -399,6 +405,14 @@ fn mouse_button_label(button: usize) -> &'static str {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn demo_window_gate_follows_the_dev_profile() {
|
||||
// The `release` profile keeps debug assertions on (like Zig's
|
||||
// ReleaseSafe) but must not show the demo.
|
||||
let debug_build = DEBUG_BUILD;
|
||||
assert_eq!(debug_build, env!("GHOSTTY_BUILD_PROFILE") == "debug");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn button_labels() {
|
||||
assert_eq!(mouse_button_label(0), "?");
|
||||
|
||||
@@ -259,16 +259,20 @@ fn encode_action(md: &mut Vec<(String, String)>, action: &ParserAction) -> Strin
|
||||
}
|
||||
}
|
||||
|
||||
/// Mirrors `Parser.Action.format` for the actions that are not specially
|
||||
/// encoded.
|
||||
/// Mirrors Zig 0.15.2 `Parser.Action.format` for the actions that are not
|
||||
/// specially encoded: `@typeName(Action)`, then `{ .<tag> = <value> }` with
|
||||
/// `u8` payloads as `0x{x}` and every other payload printed with
|
||||
/// `printValue("any", .., depth 3)`: structs as `.{ .a = .., .b = .. }`,
|
||||
/// slices as `{ a, b }` (`{ }` when empty), integers in decimal and `void`
|
||||
/// as `void`.
|
||||
fn format_action(action: &ParserAction) -> String {
|
||||
let (tag, value) = match action {
|
||||
ParserAction::DcsHook(d) => (
|
||||
"dcs_hook",
|
||||
format!(
|
||||
"{{ intermediates = {:?}, params = {:?}, final = {} }}",
|
||||
d.intermediates(),
|
||||
d.params(),
|
||||
".{{ .intermediates = {}, .params = {}, .final = {} }}",
|
||||
zig_any_slice(d.intermediates()),
|
||||
zig_any_slice(d.params()),
|
||||
d.final_
|
||||
),
|
||||
),
|
||||
@@ -280,7 +284,21 @@ fn format_action(action: &ParserAction) -> String {
|
||||
// Handled by the specific encoders.
|
||||
_ => ("unknown", String::new()),
|
||||
};
|
||||
format!("parser.Parser.Action{{ .{tag} = {value} }}")
|
||||
format!("terminal.Parser.Action{{ .{tag} = {value} }}")
|
||||
}
|
||||
|
||||
/// A slice of integers as Zig's `{any}` prints it: `{ 1, 2 }`, or `{ }`
|
||||
/// when empty.
|
||||
fn zig_any_slice<T: std::fmt::Display>(items: &[T]) -> String {
|
||||
let mut out = String::from("{ ");
|
||||
for (i, v) in items.iter().enumerate() {
|
||||
if i != 0 {
|
||||
out.push_str(", ");
|
||||
}
|
||||
let _ = write!(out, "{v}");
|
||||
}
|
||||
out.push_str(" }");
|
||||
out
|
||||
}
|
||||
|
||||
fn encode_print(ch: u32) -> String {
|
||||
@@ -927,6 +945,35 @@ mod tests {
|
||||
assert!(filter("-").pass_filter("abc"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dcs_and_apc_descriptions_match_zig_format() {
|
||||
// Expected strings produced by Zig 0.15.2 `Parser.Action.format`.
|
||||
let hook = ParserAction::DcsHook(ghostty_vt::parser::Dcs::new(b"$ ", &[1, 2, 300], b'q'));
|
||||
assert_eq!(
|
||||
format_action(&hook),
|
||||
"terminal.Parser.Action{ .dcs_hook = .{ .intermediates = { 36, 32 }, \
|
||||
.params = { 1, 2, 300 }, .final = 113 } }"
|
||||
);
|
||||
let empty = ParserAction::DcsHook(ghostty_vt::parser::Dcs::new(b"", &[], b'q'));
|
||||
assert_eq!(
|
||||
format_action(&empty),
|
||||
"terminal.Parser.Action{ .dcs_hook = .{ .intermediates = { }, \
|
||||
.params = { }, .final = 113 } }"
|
||||
);
|
||||
assert_eq!(
|
||||
format_action(&ParserAction::DcsUnhook),
|
||||
"terminal.Parser.Action{ .dcs_unhook = void }"
|
||||
);
|
||||
assert_eq!(
|
||||
format_action(&ParserAction::DcsPut(0x1b)),
|
||||
"terminal.Parser.Action{ .dcs_put = 0x1b }"
|
||||
);
|
||||
assert_eq!(
|
||||
format_action(&ParserAction::ApcPut(0x41)),
|
||||
"terminal.Parser.Action{ .apc_put = 0x41 }"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_execute_names() {
|
||||
assert_eq!(encode_execute(0x07), "BEL (0x7)");
|
||||
|
||||
Reference in new issue
Block a user