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/x/font.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/x/font.rs (limited to 'src/x/font.rs') 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