summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-12 18:38:43 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-12 18:38:43 -0700
commit7138ace1c1f97f85509278e50b72eb62ed0fd315 (patch)
treeb9afd2669631846080f215ac88f3dc0d0857a125 /src
parent7c457902864fd3e54832dd22bf33f324b6ec3cdb (diff)
move some of the font rendering into its own file
Force-Push: yes
Change-Id: I628d0d05bf75ac781cf6eb6524972b4e61d25e61
Diffstat (limited to 'src')
-rw-r--r--src/main.rs23
-rw-r--r--src/x.rs2
-rw-r--r--src/x/font.rs46
3 files changed, 54 insertions, 17 deletions
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<Rectangle<u16>>,
-  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<Display>,
+  font_state: *mut x11::xft::XftFont,
+}
+
+
+impl Font {
+  pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Font> {
+    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(())
+  }
+}
+