diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-22 22:11:08 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-22 22:11:08 -0700 |
| commit | 057c89dbfa4a3f1d70648ebb37fc34e48112d5a9 (patch) | |
| tree | 5af196a33863e47e780dc52642402dd988ba2b9d | |
| parent | 33a0aea1fa5b12c817cc299b10f8a359d670c645 (diff) | |
okay, TextLine is fully in use
Force-Push: yes Change-Id: Iddd05f95b0db3703e0c25e55bc7d55bb6adfbc5b
| -rw-r--r-- | src/layout.rs | 5 | ||||
| -rw-r--r-- | src/main.rs | 78 | ||||
| -rw-r--r-- | src/x/font.rs | 49 | ||||
| -rw-r--r-- | src/x/text_line.rs | 40 |
4 files changed, 95 insertions, 77 deletions
diff --git a/src/layout.rs b/src/layout.rs new file mode 100644 index 0000000..44b747a --- /dev/null +++ b/src/layout.rs @@ -0,0 +1,5 @@ +pub struct Parcel { + pub left: u32, + pub right: u32, +} + diff --git a/src/main.rs b/src/main.rs index 49ee13d..87de67a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use crate::error::*; use crate::facility::*; use crate::geometry::*; +use crate::layout::*; use crate::module::*; use crate::x::*; @@ -31,6 +32,7 @@ use xcb::x::{ mod error; mod facility; mod geometry; +mod layout; mod module; mod property_list; mod x; @@ -62,11 +64,6 @@ struct Dock { modules: Vec<(Arc<Mutex<Box<dyn Module>>>, ModuleFraming)>, } -struct Parcel { - left: u32, - right: u32, -} - struct ModuleFraming { alignment: Alignment, } @@ -343,7 +340,8 @@ impl Dock { - self.configuration.edge_inset as u32, }; - self.draw_text(&mut open_parcel, "playful cool fic tech nonfic", Alignment::Left)?; + self.draw_text("playful cool fic tech nonfic", + &mut open_parcel, Alignment::Left)?; { let mut facilities = self.facilities.lock()?; @@ -374,7 +372,7 @@ impl Dock { text }; - self.draw_text(&mut open_parcel, &text, framing.alignment)?; + self.draw_text(&text, &mut open_parcel, framing.alignment)?; } } @@ -480,8 +478,8 @@ impl Dock { } - fn draw_text(&self, open_parcel: &mut Parcel, text: &str, - alignment: Alignment) + fn draw_text(&self, text: &str, + open_parcel: &mut Parcel, alignment: Alignment) -> Result<()> { let context = FontGraphicContext::new(self.window())?; @@ -490,33 +488,8 @@ impl Dock { let mut line = TextLine::new(Arc::clone(&self.fonts))?; line.append_text(text)?; - let mut location = Point::<i32>::zero(); - location.y += self.baseline_position as i32; - - match alignment { - Alignment::Left => { - location.x += open_parcel.left as i32; - } - Alignment::Right => { - location.x += open_parcel.right as i32; - } - } - - /* A reversed iterator is a different type, so we need two of this loop. */ - match alignment { - Alignment::Left => { - for span in line.spans() { - self.layout_and_draw_span(open_parcel, &mut location, span, - &context, color, alignment)?; - } - } - Alignment::Right => { - for span in line.spans().rev() { - self.layout_and_draw_span(open_parcel, &mut location, span, - &context, color, alignment)?; - } - } - } + line.layout_and_draw(open_parcel, alignment, &context, color, + self.baseline_position)?; match alignment { Alignment::Left => { @@ -529,39 +502,6 @@ impl Dock { Ok(()) } - - fn layout_and_draw_span(&self, open_parcel: &mut Parcel, - location: &mut Point<i32>, span: &TextSpan, - context: &FontGraphicContext, color: &Color, - alignment: Alignment) - -> Result<()> - { - match alignment { - Alignment::Left => { - location.x += span.draw_offset().x as i32; - } - Alignment::Right => { - location.x -= span.draw_size().width as i32; - } - } - - span.draw(&context, color, &location)?; - - match alignment { - Alignment::Left => { - location.x += span.draw_size().width as i32; - - open_parcel.left += span.draw_size().width as u32; - } - Alignment::Right => { - location.x -= span.draw_offset().x as i32; - - open_parcel.right -= span.draw_size().width as u32; - } - } - - Ok(()) - } } impl HasDisplay for Dock { diff --git a/src/x/font.rs b/src/x/font.rs index 3bd641e..2dbfb75 100644 --- a/src/x/font.rs +++ b/src/x/font.rs @@ -1,5 +1,7 @@ +use crate::error::Result; +use crate::layout::Parcel; +use crate::geometry::{ Alignment, Point, Offset, Size }; use crate::x::*; -use crate::geometry::{ Point, Offset, Size }; use std::cell::OnceCell; use std::ffi::CString; @@ -32,7 +34,7 @@ impl PartialEq for Font { } impl Font { - pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Arc<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(); @@ -67,12 +69,12 @@ impl Font { *descent } - pub fn text_span(self: &Arc<Self>, text: &str) -> xcb::Result<TextSpan> { + pub fn text_span(self: &Arc<Self>, text: &str) -> Result<TextSpan> { TextSpan::new(Arc::clone(self), text) } fn measure_character_heights(self: &Arc<Self>, repertoire: &str) - -> xcb::Result<u16> + -> Result<u16> { let mut max_height = 0; for c in repertoire.chars() { @@ -83,7 +85,7 @@ impl Font { Ok(max_height) } - pub fn check_char_exists(self: &Arc<Self>, codepoint: u32) -> xcb::Result<bool> { + pub fn check_char_exists(self: &Arc<Self>, codepoint: u32) -> Result<bool> { let xlib_display = self.display.connection().get_xlib_display(); let result = unsafe { @@ -98,7 +100,7 @@ impl Font { impl TextSpan { - pub fn new(font: Arc<Font>, text: &str) -> xcb::Result<Self> { + pub fn new(font: Arc<Font>, text: &str) -> Result<Self> { let xlib_display = font.display.connection().get_xlib_display(); let text = CString::new(text).unwrap(); @@ -147,7 +149,7 @@ impl TextSpan { */ pub fn draw(&self, context: &FontGraphicContext, color: &Color, location: &Point<i32>) - -> xcb::Result<()> + -> Result<()> { let xft_draw = context.get_xft_draw(); @@ -215,5 +217,38 @@ impl TextSpan { y: self.extent.yOff, } } + + pub fn layout_and_draw(&self, + open_parcel: &mut Parcel, location: &mut Point<i32>, + alignment: Alignment, context: &FontGraphicContext, + color: &Color) + -> Result<()> + { + match alignment { + Alignment::Left => { + location.x += self.draw_offset().x as i32; + } + Alignment::Right => { + location.x -= self.draw_size().width as i32; + } + } + + self.draw(&context, color, &location)?; + + match alignment { + Alignment::Left => { + location.x += self.draw_size().width as i32; + + open_parcel.left += self.draw_size().width as u32; + } + Alignment::Right => { + location.x -= self.draw_offset().x as i32; + + open_parcel.right -= self.draw_size().width as u32; + } + } + + Ok(()) + } } diff --git a/src/x/text_line.rs b/src/x/text_line.rs index 579f96f..26ee8bf 100644 --- a/src/x/text_line.rs +++ b/src/x/text_line.rs @@ -1,5 +1,7 @@ use crate::error::Result; -use crate::x::{ Font, FontList, TextSpan }; +use crate::geometry::{ Alignment, Point }; +use crate::layout::Parcel; +use crate::x::{ Color, Font, FontGraphicContext, FontList, TextSpan }; use std::sync::{ Arc, Mutex }; @@ -53,5 +55,41 @@ impl TextLine { pub fn spans(&self) -> impl Iterator<Item=&TextSpan> + DoubleEndedIterator { self.spans.iter() } + + pub fn layout_and_draw(&self, open_parcel: &mut Parcel, + alignment: Alignment, context: &FontGraphicContext, + color: &Color, baseline_position: i16) + -> Result<()> + { + let mut location = Point::<i32>::zero(); + location.y += baseline_position as i32; + + match alignment { + Alignment::Left => { + location.x += open_parcel.left as i32; + } + Alignment::Right => { + location.x += open_parcel.right as i32; + } + } + + /* A reversed iterator is a different type, so we need two of this loop. */ + match alignment { + Alignment::Left => { + for span in self.spans() { + span.layout_and_draw(open_parcel, &mut location, + alignment, context, color)?; + } + } + Alignment::Right => { + for span in self.spans().rev() { + span.layout_and_draw(open_parcel, &mut location, + alignment, context, color)?; + } + } + } + + Ok(()) + } } |