From 4c7e73d492757813957fef810c8e43e6cb89588a Mon Sep 17 00:00:00 2001 From: "Pedro M. de Echanove Pasquin" Date: Sun, 18 Oct 2026 18:01:18 +0200 Subject: [PATCH] Text and RichText take a font face the host loaded itself, for hosts that resolve their own system families `Text` and `RichText` could only draw in a theme family, named by `(family, weight, style)` and resolved through the active theme's font registry, or in the canvas default. A host that finds its fonts elsewhere could not use them: rustdroid draws Android's sans-serif, which is Roboto looked up through fontconfig, so that an app's text keeps the widths it was designed with, and those faces are not in any ltk theme. Both widgets gain `face( Arc )`. A face given this way takes precedence over `font` in `resolve_font`, so it is used for drawing and for measuring alike, and `RichText::map` carries it over. Without it nothing changes. .gitignore lists what a Debian package build leaves under debian/: the debhelper directories and logs, the staged package trees of libltk-dev, libltk-doc and ltk-theme-default, the build stamp, debian/files and the substvars. --- .gitignore | 9 +++++++++ src/widget/rich_text/mod.rs | 14 ++++++++++++++ src/widget/text/mod.rs | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/.gitignore b/.gitignore index 1e7caa9..3795acf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,11 @@ Cargo.lock target/ +/debian/.debhelper/ +/debian/libltk-dev/ +/debian/libltk-doc/ +/debian/ltk-theme-default/ +/debian/debhelper-build-stamp +/debian/files +/debian/*.substvars +/debian/*.debhelper +/debian/*.debhelper.log diff --git a/src/widget/rich_text/mod.rs b/src/widget/rich_text/mod.rs index f438140..9cb0058 100644 --- a/src/widget/rich_text/mod.rs +++ b/src/widget/rich_text/mod.rs @@ -39,6 +39,7 @@ pub struct RichText pub( crate ) color: Color, pub( crate ) link_color: Color, pub( crate ) font: Option<( String, u16, FontStyle )>, + pub( crate ) face: Option>, pub( crate ) align: TextAlign, pub( crate ) links: Vec>, } @@ -56,6 +57,7 @@ impl RichText color: Color::WHITE, link_color: Color::rgb( 0.20, 0.50, 0.95 ), font: None, + face: None, align: TextAlign::Left, links: Vec::new(), } @@ -120,8 +122,19 @@ impl RichText canvas.resolve_font( self.size ) } + /// Draw and measure with `face`, a font the caller loaded itself, instead of a theme family. + pub fn face( mut self, face: Arc ) -> Self + { + self.face = Some( face ); + self + } + fn resolve_font( &self, canvas: &Canvas ) -> Option> { + if let Some( face ) = &self.face + { + return Some( face.clone() ); + } self.font.as_ref().map( |( family, weight, style )| canvas.font_for( family, *weight, *style ) ) } @@ -213,6 +226,7 @@ impl RichText color: self.color, link_color: self.link_color, font: self.font, + face: self.face, align: self.align, links: self.links.into_iter().map( |l| LinkSpan { start: l.start, end: l.end, msg: f( l.msg ) } ).collect(), } diff --git a/src/widget/text/mod.rs b/src/widget/text/mod.rs index 8ecde1a..dd7e94b 100644 --- a/src/widget/text/mod.rs +++ b/src/widget/text/mod.rs @@ -51,6 +51,8 @@ pub struct Text /// the active theme's font registry on every draw. `None` keeps /// the canvas default font (Sora Regular in `ltk-theme-default`). pub( crate ) font: Option<( String, u16, FontStyle )>, + /// A caller-supplied face that takes precedence over [`Self::font`]. + pub( crate ) face: Option>, /// Multiplier applied to the font's natural line height when wrapping /// onto multiple lines. `1.0` (default) uses the font-declared leading; /// values above `1.0` open up the gap between wrapped lines. @@ -72,6 +74,7 @@ impl Text wrap: false, truncate: true, font: None, + face: None, line_height: 1.0, } } @@ -103,6 +106,14 @@ impl Text self } + /// Draw and measure with `face`, a font the caller loaded itself (a host + /// that resolves its own system families), instead of a theme family. + pub fn face( mut self, face: Arc ) -> Self + { + self.face = Some( face ); + self + } + /// Shorthand for [`Self::font`] when the desired override is just /// a weight on the default Sora family (the family `ltk-theme- /// default` declares). @@ -162,6 +173,10 @@ impl Text fn resolve_font( &self, canvas: &Canvas ) -> Option> { + if let Some( face ) = &self.face + { + return Some( face.clone() ); + } self.font.as_ref().map( |( family, weight, style )| { canvas.font_for( family, *weight, *style )