about summary refs log tree commit diff
path: root/src/x/text/font.rs
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-22 23:18:13 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-22 23:18:13 -0700
commitb7319af1ed9e5dc3c29d05d6f5de9f325fa64191 (patch)
treed3ce5e3b0f6835e4e8179ab8caf2921c7e4ad295 /src/x/text/font.rs
parent1b24796bb97f64ab2771a884632d4b6f64d86f0f (diff)
new subdirectory src/x/text/
Force-Push: yes
Change-Id: I1c428e1f7b42de263534965422dffe007300082c
Diffstat (limited to 'src/x/text/font.rs')
-rw-r--r--src/x/text/font.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/x/text/font.rs b/src/x/text/font.rs
new file mode 100644
index 0000000..fe3429c
--- /dev/null
+++ b/src/x/text/font.rs
@@ -0,0 +1,106 @@
+use crate::error::Result;
+use crate::layout::Parcel;
+use crate::geometry::{ Alignment, Point, Offset, Size };
+use crate::x::*;
+use crate::x::text::Span;
+
+use std::cell::OnceCell;
+use std::ffi::CString;
+use std::sync::Arc;
+
+use x11::xft::{
+  XftCharExists, XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName
+};
+use x11::xrender::XGlyphInfo;
+
+
+pub struct Font {
+  display: Arc<Display>,
+  font_state: *mut x11::xft::XftFont,
+  metrics: OnceCell<(u16,u16)>,
+}
+
+impl PartialEq for Font {
+  fn eq(&self, other: &Font) -> bool {
+    self.display == other.display
+    && self.font_state == other.font_state
+  }
+}
+
+impl HasConnection for Font {
+  fn connection(&self) -> &Connection { self.display.connection() }
+}
+
+impl HasDisplay for Font {
+  fn display(&self) -> &Arc<Display> { &self.display }
+}
+
+impl Font {
+  pub fn new(display: Arc<Display>, name: &str) -> Result<Arc<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())
+    };
+
+    let font = Arc::new(Font {
+      display, font_state,
+      metrics: OnceCell::new(),
+    });
+
+    let x_height = font.measure_character_heights("acemnorsuvwxz")?;
+    let ascent = font.text_span("bdfhiklt")?.draw_size().height;
+    let descent = font.text_span("gpqy")?.draw_size().height - x_height;
+
+    let _ = font.metrics.set((ascent, descent));
+
+    Ok(font)
+  }
+
+  pub fn font_state(&self) -> *mut x11::xft::XftFont {
+    self.font_state
+  }
+
+  pub fn ascent(self: &Arc<Self>) -> u16 {
+    let (ascent, _) = self.metrics.get().unwrap();
+
+    *ascent
+  }
+
+  pub fn descent(self: &Arc<Self>) -> u16 {
+    let (_, descent) = self.metrics.get().unwrap();
+
+    *descent
+  }
+
+  pub fn text_span(self: &Arc<Self>, text: &str) -> Result<Span> {
+    Span::new(Arc::clone(self), text)
+  }
+
+  fn measure_character_heights(self: &Arc<Self>, repertoire: &str)
+    -> Result<u16>
+  {
+    let mut max_height = 0;
+    for c in repertoire.chars() {
+      let span = self.text_span(&format!("{}", c))?;
+      max_height = max_height.max(span.draw_size().height);
+    }
+
+    Ok(max_height)
+  }
+
+  pub fn check_char_exists(self: &Arc<Self>, codepoint: u32) -> Result<bool> {
+    let xlib_display = self.display.connection().get_xlib_display();
+
+    let result = unsafe {
+      XftCharExists(xlib_display,
+		    self.font_state,
+		    codepoint)
+    };
+
+    Ok(result != 0)
+  }
+}
+