about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-13 22:08:48 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-13 22:08:48 -0700
commitc8e87126b038a5f70b7c5a0eac018c0d12486f43 (patch)
treebb70aa2ed4dc12c02741b456473ff27b96e189ea /src
parent15207d15aef96e1d38bd5fd0ee6bd8b6686efb36 (diff)
reverse-engineer font ascent and descent, and rely on them
we now do the baseline computation "properly", during setup, rather than during text rendering. this allows us to be consistent about it; prior to this, the two spans of text we draw would often wind up with different baselines.

Force-Push: yes
Change-Id: Ic2f34c816fc0767d6e54c5415beb516f99cd3069
Diffstat (limited to 'src')
-rw-r--r--src/main.rs23
-rw-r--r--src/x/font.rs42
2 files changed, 53 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 61abb5c..aa0eb0c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -35,6 +35,7 @@ struct Dock {
   window: Window,
   bounds: Arc<Rectangle<u16>>,
   font: Arc<Font>,
+  baseline_position: i16,
 }
 
 impl HasWindow for Dock {
@@ -53,7 +54,7 @@ fn main() -> xcb::Result<()> {
 
   let dock = Dock::new(Arc::clone(&display), DockConfiguration {
     thickness: 34,
-    baseline_offset: 3,
+    baseline_offset: 2,
     edge_inset: 12,
     side: Side::Bottom,
     background, foreground,
@@ -112,13 +113,20 @@ impl Dock {
       ],
     })?;
 
-    let font = Arc::new(Font::new(Arc::clone(&display),
-        "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?);
+    let font = Font::new(Arc::clone(&display),
+        "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?;
+
+    let font_height = font.ascent() + font.descent();
+    let vertical_padding = (dock_bounds.height - font_height) / 2;
+    let baseline_position = font.ascent() as i16
+                            + vertical_padding as i16
+                            + configuration.baseline_offset as i16;
 
     let dock = Dock {
       window, configuration,
       bounds: Arc::new(dock_bounds),
-      font
+      font,
+      baseline_position
     };
 
     dock.set_string_property(&xcb::x::ATOM_WM_NAME, b"sandbar")?;
@@ -225,7 +233,7 @@ impl Dock {
     }
 
     self.draw_text("playful cool fic tech nonfic", Alignment::Left)?;
-    self.draw_text("abcy", Alignment::Right)?;
+    self.draw_text("2026", Alignment::Right)?;
 
     Ok(())
   }
@@ -314,9 +322,8 @@ impl Dock {
     let text = self.font.text_span(text)?;
 
     let mut location = Point::<i32>::zero();
-    location = location.add_offset(&text.draw_offset());
-    location.y += ((self.bounds.height - text.draw_size().height) / 2) as i32;
-    location.y += self.configuration.baseline_offset as i32;
+    location.x += text.draw_offset().x as i32;
+    location.y += self.baseline_position as i32;
 
     match alignment {
       Alignment::Left => {
diff --git a/src/x/font.rs b/src/x/font.rs
index dc84199..8d49e98 100644
--- a/src/x/font.rs
+++ b/src/x/font.rs
@@ -1,6 +1,7 @@
 use crate::x::*;
 use crate::geometry::{ Point, Offset, Size };
 
+use std::cell::OnceCell;
 use std::ffi::CString;
 use std::sync::Arc;
 
@@ -11,6 +12,7 @@ use x11::xrender::XGlyphInfo;
 pub struct Font {
   display: Arc<Display>,
   font_state: *mut x11::xft::XftFont,
+  metrics: OnceCell<(u16,u16)>,
 }
 
 pub struct TextSpan {
@@ -21,7 +23,7 @@ pub struct TextSpan {
 
 
 impl Font {
-  pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Font> {
+  pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Arc<Font>> {
     let xlib_display = display.connection().get_xlib_display();
     let screen_index = display.screen_index();
 
@@ -30,12 +32,44 @@ impl Font {
       XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast())
     };
 
-    Ok(Font { display, font_state })
+    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 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) -> xcb::Result<TextSpan> {
     TextSpan::new(Arc::clone(self), text)
   }
+
+  fn measure_character_heights(self: &Arc<Self>, repertoire: &str)
+    -> xcb::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)
+  }
 }
 
 
@@ -147,8 +181,8 @@ impl TextSpan {
    * immediately after it would start from.
    *
    *   This is rounded in some fashion, most likely to the nearest whole
-   * pixel, which causes it to often be smaller than logical_size(). See
-   * the note on draw() for some highly relevant philosophy.
+   * pixel, which causes it to often be smaller than draw_size(). See the note
+   * on draw() for some highly relevant philosophy.
    */
   pub fn logical_offset(&self) -> Offset<i16> {
     Offset {