From 7138ace1c1f97f85509278e50b72eb62ed0fd315 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Thu, 12 Nov 2026 18:38:43 -0700 Subject: move some of the font rendering into its own file Force-Push: yes Change-Id: I628d0d05bf75ac781cf6eb6524972b4e61d25e61 --- src/main.rs | 23 ++++++----------------- src/x.rs | 2 ++ src/x/font.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 src/x/font.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 523ec45..ddb58cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,7 @@ use x11::xrender::XRenderColor; use x11::xft::{ XftFontOpenName, XftDrawCreate, XftDrawDestroy, - XftColor, - XftDrawStringUtf8 + XftColor }; use xcb::{ Xid }; use xcb::x::{ @@ -40,7 +39,7 @@ struct Dock { configuration: DockConfiguration, window: Window, bounds: Arc>, - font: *mut x11::xft::XftFont, + font: Font, } impl HasWindow for Dock { @@ -120,14 +119,8 @@ impl Dock { ], })?; - let xlib_display = display.connection().get_xlib_display(); - let screen_index = display.screen_index(); - - let font = unsafe { - XftFontOpenName(xlib_display, screen_index, - c"FantasqueSansM Nerd Font Mono:style=Regular:size=18" - .as_ptr().cast()) - }; + let font = Font::new(Arc::clone(&display), + "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?; let dock = Dock { window, configuration, @@ -339,12 +332,8 @@ impl Dock { pixel: self.configuration.foreground_pixel as u64, color: self.configuration.foreground_render_color, }; - let text = c"amazing cool fiction technically reads playful unknown telephonic works from-post"; - unsafe { - XftDrawStringUtf8(xft_context, &xft_color, self.font, 12, 25, - text.as_ptr().cast(), - text.count_bytes() as i32) - }; + let text = "playful cool fic tech nonfic"; + self.font.draw(xft_context, &xft_color, 12, 25, text)?; unsafe { XftDrawDestroy(xft_context) }; diff --git a/src/x.rs b/src/x.rs index 09297d8..231ccd3 100644 --- a/src/x.rs +++ b/src/x.rs @@ -2,6 +2,7 @@ pub use color::DisplayColorExt; pub use context::{ GraphicContext, HasGraphicContext, GraphicContextExt }; pub use connection::{ Connection, HasConnection, ConnectionExt }; pub use display::{ Display, HasDisplay, DisplayExt }; +pub use font::Font; pub use geometry::{ X_CLOCKWISE_RIGHT_ANGLE, SideExt, RectangleExt }; pub use window::{ Window, HasWindow, WindowExt }; @@ -10,6 +11,7 @@ pub mod color; pub mod connection; pub mod context; pub mod display; +pub mod font; pub mod geometry; pub mod window; diff --git a/src/x/font.rs b/src/x/font.rs new file mode 100644 index 0000000..20ae7e4 --- /dev/null +++ b/src/x/font.rs @@ -0,0 +1,46 @@ +use crate::x::*; + +use std::ffi::CString; +use std::sync::Arc; + +use x11::xft::{ + XftColor, + XftDraw, + XftDrawStringUtf8, + XftFontOpenName, +}; + + +pub struct Font { + display: Arc, + font_state: *mut x11::xft::XftFont, +} + + +impl Font { + pub fn new(display: Arc, name: &str) -> xcb::Result { + let xlib_display = display.connection().get_xlib_display(); + let screen_index = display.screen_index(); + + let name = CString::new(name).unwrap(); + let font_state = unsafe { + XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast()) + }; + + Ok(Font { display, font_state }) + } + + pub fn draw(&self, xft_context: *mut XftDraw, xft_color: &XftColor, + x: i32, y: i32, text: &str) + -> xcb::Result<()> + { + let text = CString::new(text).unwrap(); + unsafe { + XftDrawStringUtf8(xft_context, xft_color, self.font_state, x, y, + text.as_ptr().cast(), text.count_bytes() as i32) + }; + + Ok(()) + } +} + -- cgit 1.4.1