From 97794fff629f47482222b897bff463e14cdb9e02 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 25 Nov 2026 19:19:14 +0000 Subject: [PATCH 01/10] ui-contract: DocRun::coords draws a run at variation coordinates of its face; FontSample takes them --- crates/font-manager/Cargo.toml | 1 + crates/font-manager/src/panel/faces.rs | 48 ++++++++++++------- crates/font-manager/src/panel/fixtures.rs | 3 ++ crates/font-manager/src/panel/specimen.rs | 13 +++-- .../src/panel/specimen/stories.rs | 20 +++++++- crates/font-manager/src/picker/banner.rs | 7 +-- crates/ui-contract/src/primitives/content.rs | 30 ++++++++++-- docs/contract-api.md | 5 +- 8 files changed, 97 insertions(+), 30 deletions(-) diff --git a/crates/font-manager/Cargo.toml b/crates/font-manager/Cargo.toml index fbaa419991..dcc2db8025 100644 --- a/crates/font-manager/Cargo.toml +++ b/crates/font-manager/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true reads = [ "crates/ui-contract/fonts/inter/Inter-Regular.ttf", "crates/ui-contract/fonts/inter/Inter-SemiBold.ttf", + "crates/font-subset/tests/data/orbitron/Orbitron[wght].ttf", "crates/font-subset/tests/data/sarabun/Sarabun-Regular.ttf", "crates/ui-contract/tests/fonts/NotoSansCJKsc-test.otf", ] diff --git a/crates/font-manager/src/panel/faces.rs b/crates/font-manager/src/panel/faces.rs index ec3f0c9ac1..c2732db705 100644 --- a/crates/font-manager/src/panel/faces.rs +++ b/crates/font-manager/src/panel/faces.rs @@ -82,25 +82,41 @@ pub(crate) struct SampleLine { pub(crate) baseline: f32, } -/// `text` in `font` for a band `band` high, laid out by `layout` (a measure's or a painter's) and cut to `room` as +/// A sample's text and the face, size and colour it is drawn in. +#[derive(Clone, Copy)] +pub(crate) struct Sample<'a> { + pub(crate) text: &'a str, + pub(crate) font: &'a FontRef, + /// The variation coordinates (axis tag, value) the face is drawn at; empty for the face as registered. + pub(crate) coords: &'a [([u8; 4], f32)], + pub(crate) points: f32, + pub(crate) color: DataColor, +} + +impl<'a> Sample<'a> { + /// `text` in `font` as it is registered, at `points` in `color`. + pub(crate) fn new(text: &'a str, font: &'a FontRef, points: f32, color: DataColor) -> Self { + Sample { text, font, coords: &[], points, color } + } + + /// The sample drawn at the coordinates `coords` of its face. + pub(crate) fn at(self, coords: &'a [([u8; 4], f32)]) -> Self { + Sample { coords, ..self } + } +} + +/// `sample` for a band `band` high, laid out by `layout` (a measure's or a painter's) and cut to `room` as /// [`sample_text`] cuts it. Every face sits on one baseline: that of the interface face's line at `points` centred in /// the band, as a control's line sets its texts. The sample is laid out at `points`, or, where its ink (what the /// layout draws, as `Galley::mesh_bounds` holds it) would cross the band's top or bottom from that baseline, at the /// largest size whose ink stays inside the band. -pub(crate) fn sample_line( - layout: impl Fn(&DocText<'_>) -> TextLayout, - font: &FontRef, - text: &str, - points: f32, - band: f32, - room: Option, - color: DataColor, -) -> SampleLine { +pub(crate) fn sample_line(layout: impl Fn(&DocText<'_>) -> TextLayout, sample: Sample<'_>, band: f32, room: Option) -> SampleLine { + let Sample { text, font, coords, points, color } = sample; let probe = [DocRun::new(BASELINE_PROBE, FontRef::proportional(), points, color)]; let line = layout(&DocText::new(&probe)); let baseline = (band - line.size().y) / 2.0 + line.first_baseline; let at = |size: f32| { - let runs = [DocRun::new(text, font.clone(), size, color)]; + let runs = [DocRun::new(text, font.clone(), size, color).coords(coords)]; layout(&sample_text(&runs, room)) }; let mut size = points; @@ -127,8 +143,6 @@ fn share(room: f32, ink: f32) -> f32 { if ink > room { room / ink } else { 1.0 } } -/// `text` on one line in `font` at `points` in the band `rect` ([`sample_line`]), at the reading start of `rect`, -/// clipped to `rect`. The sample keeps its own direction whatever the interface's. /// The ink of a sample over `rect`: black on a white paper it fills, in every theme, when `on_white`; else `ink`. pub(crate) fn sample_ink(painter: &ContentPainterCx<'_>, rect: Rect, on_white: bool, ink: Color32) -> DataColor { if !on_white { @@ -138,10 +152,12 @@ pub(crate) fn sample_ink(painter: &ContentPainterCx<'_>, rect: Rect, on_white: b DataColor::new(Color32::from(Srgba::BLACK)) } -pub(crate) fn paint_sample(painter: &ContentPainterCx<'_>, rect: Rect, font: &FontRef, text: &str, points: f32, color: DataColor) { - let line = sample_line(|doc| painter.layout(doc), font, text, points, rect.height(), Some(rect.width()), color); +/// `sample` on one line in the band `rect` ([`sample_line`]), at the reading start of `rect`, clipped to `rect`. The +/// sample keeps its own direction whatever the interface's. +pub(crate) fn paint_sample(painter: &ContentPainterCx<'_>, rect: Rect, sample: Sample<'_>) { + let line = sample_line(|doc| painter.layout(doc), sample, rect.height(), Some(rect.width())); let size = line.layout.size(); - let x = match base_direction(text) { + let x = match base_direction(sample.text) { Some(Direction::Rtl) => rect.right() - size.x, _ => rect.left(), }; diff --git a/crates/font-manager/src/panel/fixtures.rs b/crates/font-manager/src/panel/fixtures.rs index 4026ce878b..1cff9dbd13 100644 --- a/crates/font-manager/src/panel/fixtures.rs +++ b/crates/font-manager/src/panel/fixtures.rs @@ -47,6 +47,9 @@ pub(crate) fn story_catalog() -> Arc { catalog } +/// A variable font (`wght`), for the samples drawn at variation coordinates. +pub(crate) const VARIABLE: &[u8] = include_bytes!("../../../font-subset/tests/data/orbitron/Orbitron[wght].ttf"); + /// The panel over the story catalog and index; its store is a fake that refuses downloads. pub(crate) fn story_panel() -> FontsPanel { let store = FakeStore::new(std::env::temp_dir().join("craft-fonts-panel-story"), HashMap::new()); diff --git a/crates/font-manager/src/panel/specimen.rs b/crates/font-manager/src/panel/specimen.rs index c19e68d939..424382fbd1 100644 --- a/crates/font-manager/src/panel/specimen.rs +++ b/crates/font-manager/src/panel/specimen.rs @@ -12,7 +12,7 @@ use egui::{Color32, Vec2}; use crate::Catalog; use crate::FaceInfo; -use crate::panel::faces::{self, Owner, Shown}; +use crate::panel::faces::{self, Owner, Sample, Shown}; #[cfg(feature = "suite")] pub(crate) mod stories; @@ -81,12 +81,16 @@ pub(crate) struct FontSample<'a> { /// [`LINE_EM`] of its size. #[prop(quantity = "band height the caller's layout gives, in points")] band: Option, + /// The variation coordinates (axis tag, value) the face is drawn at, over its own: one registered variable face + /// draws every named instance and every axis value. + #[prop] + coords: &'a [([u8; 4], f32)], } impl<'a> FontSample<'a> { /// A sample of `text` in `face` at `points`, named `label`. pub(crate) fn new(label: impl Into