diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-22 03:31:51 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-22 21:40:21 -0700 |
| commit | 33a0aea1fa5b12c817cc299b10f8a359d670c645 (patch) | |
| tree | 1f29b867d49a6158edc679c3cc1521a77f1175d7 /src | |
| parent | efa17e7eb566d81ce821bde459e900d7398a9977 (diff) | |
there are now concepts of font lists and text lines
a "line" is a list of spans much of the layout logic is moved into TextLine, but not yet all of it Change-Id: I2edba34cd9be5a50ac6dc646cd6e2ab2369fca13 Force-Push: yes
Diffstat (limited to 'src')
| -rw-r--r-- | src/facility.rs | 1 | ||||
| -rw-r--r-- | src/geometry.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 79 | ||||
| -rw-r--r-- | src/module/battery.rs | 7 | ||||
| -rw-r--r-- | src/property_list.rs | 1 | ||||
| -rw-r--r-- | src/x.rs | 5 | ||||
| -rw-r--r-- | src/x/connection.rs | 6 | ||||
| -rw-r--r-- | src/x/display.rs | 5 | ||||
| -rw-r--r-- | src/x/font.rs | 33 | ||||
| -rw-r--r-- | src/x/font_list.rs | 61 | ||||
| -rw-r--r-- | src/x/text_line.rs | 57 |
11 files changed, 238 insertions, 23 deletions
diff --git a/src/facility.rs b/src/facility.rs index dcc252c..0c34bf9 100644 --- a/src/facility.rs +++ b/src/facility.rs @@ -112,6 +112,7 @@ impl FacilityMap { } else { None } } + #[allow(unused)] pub fn iter(&self) -> impl Iterator<Item=&Box<dyn Facility>> { let btree_iterator = self.facilities.iter(); diff --git a/src/geometry.rs b/src/geometry.rs index 77f7a61..a256bcb 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -5,13 +5,13 @@ use std::ops::{ Add, Sub }; -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Alignment { Left, Right } -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Side { Top, Bottom, Left, Right } -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Corner { TopLeft, TopRight, BottomRight, BottomLeft } impl Side { diff --git a/src/main.rs b/src/main.rs index fa97c35..49ee13d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,7 +56,7 @@ struct Dock { window: Window, is_visible: bool, bounds: Arc<Rectangle<u16>>, - font: Arc<Font>, + fonts: Arc<Mutex<FontList>>, baseline_position: i16, facilities: Arc<Mutex<FacilityMap>>, modules: Vec<(Arc<Mutex<Box<dyn Module>>>, ModuleFraming)>, @@ -176,12 +176,16 @@ impl Dock { ], })?; - let font = Font::new(Arc::clone(&display), + let fonts = Arc::new(Mutex::new(FontList::new(Arc::clone(&display))?)); + fonts.lock()?.append( "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?; + fonts.lock()?.append("Symbola:size=18")?; + fonts.lock()?.append("Sazanami Mincho:size=18")?; - let font_height = font.ascent() + font.descent(); + let primary_font = fonts.lock()?.primary_font()?; + let font_height = primary_font.ascent() + primary_font.descent(); let vertical_padding = (dock_bounds.height - font_height) / 2; - let baseline_position = font.ascent() as i16 + let baseline_position = primary_font.ascent() as i16 + vertical_padding as i16 + configuration.baseline_offset as i16; @@ -221,7 +225,7 @@ impl Dock { window, configuration, is_visible: false, bounds: Arc::new(dock_bounds), - font, + fonts, baseline_position, facilities, modules, @@ -482,28 +486,79 @@ impl Dock { { let context = FontGraphicContext::new(self.window())?; let color = &self.configuration.foreground; - let text = self.font.text_span(text)?; + + let mut line = TextLine::new(Arc::clone(&self.fonts))?; + line.append_text(text)?; let mut location = Point::<i32>::zero(); - location.x += text.draw_offset().x as i32; 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; + } + } - open_parcel.left += text.draw_size().width as u32; - open_parcel.left += self.configuration.intermodule_padding as u32; + /* 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 => { - location.x += open_parcel.right as i32 - text.draw_size().width as i32; + for span in line.spans().rev() { + self.layout_and_draw_span(open_parcel, &mut location, span, + &context, color, alignment)?; + } + } + } - open_parcel.right -= text.draw_size().width as u32; + match alignment { + Alignment::Left => { + open_parcel.left += self.configuration.intermodule_padding as u32; + } + Alignment::Right => { open_parcel.right -= self.configuration.intermodule_padding as u32; } } - text.draw(&context, color, location)?; + 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(()) } diff --git a/src/module/battery.rs b/src/module/battery.rs index 0b87b12..154016a 100644 --- a/src/module/battery.rs +++ b/src/module/battery.rs @@ -2,7 +2,6 @@ use crate::error::{ Error, Result }; use crate::facility::{ FacilityMap, FacilityType }; use crate::facility::envsys::EnvSys; use crate::module::{ CreatableModule, Module }; -use crate::property_list::{ Array, Value }; use std::time::Duration; @@ -69,14 +68,14 @@ impl Module for BatteryModule { let is_full = ratio >= self.full_threshold; if is_full && ! is_charging { - text.push_str("100%"); + text.push_str("\u{1F333}100"); } else { let percentage = (ratio * 100.0).round() as i32; if is_charging { - text.push_str(&format!("^{}%", percentage)); + text.push_str(&format!("\u{2668}{}%", percentage)); } else { - text.push_str(&format!("v{}%", percentage)); + text.push_str(&format!("\u{1f343}{}%", percentage)); } } } diff --git a/src/property_list.rs b/src/property_list.rs index b096b35..b5cb4d6 100644 --- a/src/property_list.rs +++ b/src/property_list.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use xml::reader::{ EventReader, XmlEvent }; +#[allow(unused)] #[derive(Debug)] pub enum Value { Dictionary(Dictionary), diff --git a/src/x.rs b/src/x.rs index 24eadc1..0782649 100644 --- a/src/x.rs +++ b/src/x.rs @@ -1,10 +1,13 @@ +#![allow(unused_imports)] pub use color::{ Color, DisplayColorExt }; pub use context::{ GraphicContext, HasGraphicContext, GraphicContextExt }; pub use connection::{ Connection, HasConnection, ConnectionExt }; pub use display::{ Display, HasDisplay, DisplayExt }; pub use font::{ Font, TextSpan }; +pub use font_list::FontList; pub use font_context::FontGraphicContext; pub use geometry::{ X_CLOCKWISE_RIGHT_ANGLE, SideExt, RectangleExt }; +pub use text_line::TextLine; pub use window::{ Window, HasWindow, WindowExt }; pub mod atoms; @@ -14,6 +17,8 @@ pub mod context; pub mod font_context; pub mod display; pub mod font; +pub mod font_list; pub mod geometry; +pub mod text_line; pub mod window; diff --git a/src/x/connection.rs b/src/x/connection.rs index c4e879f..f37f665 100644 --- a/src/x/connection.rs +++ b/src/x/connection.rs @@ -7,6 +7,12 @@ use xcb::x::{ Atom, InternAtom }; pub struct Connection(xcb::Connection); +impl PartialEq for Connection { + fn eq(&self, other: &Connection) -> bool { + self.0.get_raw_conn() == other.0.get_raw_conn() + } +} + impl Connection { pub fn connect() -> xcb::ConnResult<(Connection, i32)> { let (connection, screen_index) diff --git a/src/x/display.rs b/src/x/display.rs index 7aa7e2e..e950484 100644 --- a/src/x/display.rs +++ b/src/x/display.rs @@ -16,6 +16,11 @@ impl HasConnection for Arc<Display> { fn connection(&self) -> &Connection { &self.connection } } +impl PartialEq for Display { + fn eq(&self, other: &Display) -> bool { + self.connection == other.connection + } +} impl Display { pub fn new() -> xcb::Result<Display> { diff --git a/src/x/font.rs b/src/x/font.rs index 8d49e98..3bd641e 100644 --- a/src/x/font.rs +++ b/src/x/font.rs @@ -5,7 +5,9 @@ use std::cell::OnceCell; use std::ffi::CString; use std::sync::Arc; -use x11::xft::{ XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName }; +use x11::xft::{ + XftCharExists, XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName +}; use x11::xrender::XGlyphInfo; @@ -22,6 +24,13 @@ pub struct TextSpan { } +impl PartialEq for Font { + fn eq(&self, other: &Font) -> bool { + self.display == other.display + && self.font_state == other.font_state + } +} + impl Font { pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Arc<Font>> { let xlib_display = display.connection().get_xlib_display(); @@ -32,7 +41,10 @@ impl Font { XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast()) }; - let font = Arc::new(Font { display, font_state, metrics: OnceCell::new() }); + 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; @@ -70,6 +82,18 @@ impl Font { Ok(max_height) } + + pub fn check_char_exists(self: &Arc<Self>, codepoint: u32) -> xcb::Result<bool> { + let xlib_display = self.display.connection().get_xlib_display(); + + let result = unsafe { + XftCharExists(xlib_display, + self.font_state, + codepoint) + }; + + Ok(result != 0) + } } @@ -122,7 +146,7 @@ impl TextSpan { * deeply saddening, but there is nothing to be done but deal with it. */ pub fn draw(&self, context: &FontGraphicContext, color: &Color, - location: Point<i32>) + location: &Point<i32>) -> xcb::Result<()> { let xft_draw = context.get_xft_draw(); @@ -146,7 +170,7 @@ impl TextSpan { pub fn draw_offset(&self) -> Offset<i16> { Offset { x: self.extent.x, - y: self.extent.y + y: self.extent.y, } } @@ -184,6 +208,7 @@ impl TextSpan { * pixel, which causes it to often be smaller than draw_size(). See the note * on draw() for some highly relevant philosophy. */ + #[allow(unused)] pub fn logical_offset(&self) -> Offset<i16> { Offset { x: self.extent.xOff, diff --git a/src/x/font_list.rs b/src/x/font_list.rs new file mode 100644 index 0000000..d21d8c4 --- /dev/null +++ b/src/x/font_list.rs @@ -0,0 +1,61 @@ +use crate::error::{ Error, Result }; +use crate::x::{ Display, Font }; + +use std::sync::Arc; + + +pub struct FontList { + display: Arc<Display>, + fonts: Vec<Arc<Font>>, +} + +impl FontList { + pub fn new(display: Arc<Display>) -> Result<FontList> { + let fonts = Vec::new(); + + Ok(FontList { display, fonts }) + } + + pub fn append(&mut self, name: &str) -> Result<()> { + let font = Font::new(Arc::clone(&self.display), name)?; + + self.append_font(font); + + Ok(()) + } + + pub fn append_font(&mut self, font: Arc<Font>) { + self.fonts.push(font); + } + + /* The primary font is the first one in the list. Ascribing meaning to this + * is up to the caller. + */ + pub fn primary_font(&self) -> Result<Arc<Font>> { + if self.fonts.len() > 0 { + return Ok(Arc::clone(&self.fonts[0])); + } + + Err(Error::custom("There are no fonts in the list.")) + } + + /* This accepts a Unicode codepoint, and returns a font containing it. At + * least, that's the hope. The xft docs are thin on the topic of what + * encoding the codepoint is in, and it's entirely possible something weird + * happens in fonts for which ISO-10646 is not an explicitly provided + * encoding, or for which it's provided in an unusual way. + * + * If the character isn't found in any font, this returns the primary font, + * on the assumption that's the one whose missing-glyph glyph we want. + */ + pub fn find_codepoint(&self, codepoint: u32) -> Result<Arc<Font>> { + for font in &self.fonts { + if font.check_char_exists(codepoint)? { + return Ok(Arc::clone(&font)); + } + } + + self.primary_font() + } +} + diff --git a/src/x/text_line.rs b/src/x/text_line.rs new file mode 100644 index 0000000..579f96f --- /dev/null +++ b/src/x/text_line.rs @@ -0,0 +1,57 @@ +use crate::error::Result; +use crate::x::{ Font, FontList, TextSpan }; + +use std::sync::{ Arc, Mutex }; + + +pub struct TextLine { + fonts: Arc<Mutex<FontList>>, + spans: Vec<TextSpan>, +} + + +impl TextLine { + pub fn new(fonts: Arc<Mutex<FontList>>) -> Result<TextLine> { + let spans = Vec::new(); + + Ok(TextLine { fonts, spans }) + } + + pub fn append_text(&mut self, text: &str) -> Result<()> { + let mut current_span_font: Option<Arc<Font>> = None; + let mut current_span_text = String::new(); + + for char in text.chars() { + let codepoint = char as u32; + + let new_font = &self.fonts.lock()?.find_codepoint(codepoint)?; + if let Some(old_font) = ¤t_span_font { + if old_font != new_font { + let span = old_font.text_span(¤t_span_text)?; + self.spans.push(span); + + current_span_font = Some(Arc::clone(new_font)); + current_span_text = String::new(); + } + } else { + current_span_font = Some(Arc::clone(new_font)); + } + + current_span_text.push(char); + } + + if let Some(font) = current_span_font + && !current_span_text.is_empty() + { + let span = font.text_span(¤t_span_text)?; + self.spans.push(span); + } + + Ok(()) + } + + pub fn spans(&self) -> impl Iterator<Item=&TextSpan> + DoubleEndedIterator { + self.spans.iter() + } +} + |