about summary refs log tree commit diff
path: root/src/main.rs
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/main.rs
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/main.rs')
-rw-r--r--src/main.rs23
1 files changed, 15 insertions, 8 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 => {