diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/geometry.rs | 89 | ||||
| -rw-r--r-- | src/main.rs | 34 | ||||
| -rw-r--r-- | src/x.rs | 2 | ||||
| -rw-r--r-- | src/x/font.rs | 132 |
4 files changed, 239 insertions, 18 deletions
diff --git a/src/geometry.rs b/src/geometry.rs index 3d25b5d..77f7a61 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -1,5 +1,12 @@ -use std::ops::Sub; +/* It is the nature of mathematical objects to have many small features + * which are not all needed in practice. + */ +#![allow(dead_code)] +use std::ops::{ Add, Sub }; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Alignment { Left, Right } #[derive(Clone, Copy, PartialEq, Eq)] pub enum Side { Top, Bottom, Left, Right } @@ -39,8 +46,11 @@ impl Corner { } -pub trait Coordinate: Copy + From<u8> + Ord + Sub<Output=Self> { } -impl<T: Copy + From<u8> + Ord + Sub<Output=T>> Coordinate for T { } +pub trait Coordinate: Copy + From<u8> + Ord + + Add<Output=Self> + Sub<Output=Self> { } +impl<T: Copy + From<u8> + Ord + + Add<Output=T> + Sub<Output=T>> + Coordinate for T { } #[derive(Debug)] @@ -50,6 +60,18 @@ pub struct Point<T: Coordinate> { } #[derive(Debug)] +pub struct Offset<T: Coordinate> { + pub x: T, + pub y: T, +} + +#[derive(Debug)] +pub struct Size<T: Coordinate> { + pub width: T, + pub height: T, +} + +#[derive(Debug)] pub struct Rectangle<T: Coordinate> { pub left: T, pub top: T, @@ -57,6 +79,67 @@ pub struct Rectangle<T: Coordinate> { pub height: T, } +impl<T: Coordinate> Point<T> { + pub fn new(x: T, y: T) -> Point<T> { + Point { x, y } + } + + pub fn zero() -> Point<T> { + Point { + x: 0.into(), + y: 0.into(), + } + } + + pub fn add_offset<U: Coordinate + Into<T>>(&self, offset: &Offset<U>) + -> Point<T> + { + Point { + x: self.x + offset.x.into(), + y: self.y + offset.y.into(), + } + } + + pub fn sub_offset<U: Coordinate + Into<T>>(&self, offset: &Offset<U>) + -> Point<T> + { + Point { + x: self.x - offset.x.into(), + y: self.y - offset.y.into(), + } + } + + pub fn add_size<U: Coordinate + Into<T>>(&self, size: &Size<U>) + -> Point<T> + { + Point { + x: self.x + size.width.into(), + y: self.y + size.height.into(), + } + } + + pub fn sub_size<U: Coordinate + Into<T>>(&self, size: &Size<T>) + -> Point<T> + { + Point { + x: self.x - size.width.into(), + y: self.y - size.height.into(), + } + } +} + +impl<T: Coordinate> Offset<T> { + pub fn new(x: T, y: T) -> Offset<T> { + Offset { x, y } + } +} + +impl<T: Coordinate> Size<T> { + pub fn new(width: T, height: T) -> Size<T> { + Size { width, height } + } +} + impl<T: Coordinate> Rectangle<T> { pub fn corner_point(&self, corner: Corner) -> Point<T> { match corner { diff --git a/src/main.rs b/src/main.rs index 9decfbd..61abb5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,8 @@ mod x; struct DockConfiguration { thickness: u16, + baseline_offset: i8, + edge_inset: i8, side: Side, background: Color, foreground: Color, @@ -32,7 +34,7 @@ struct Dock { configuration: DockConfiguration, window: Window, bounds: Arc<Rectangle<u16>>, - font: Font, + font: Arc<Font>, } impl HasWindow for Dock { @@ -51,6 +53,8 @@ fn main() -> xcb::Result<()> { let dock = Dock::new(Arc::clone(&display), DockConfiguration { thickness: 34, + baseline_offset: 3, + edge_inset: 12, side: Side::Bottom, background, foreground, use_rounded_inside_corners: false, @@ -108,8 +112,8 @@ impl Dock { ], })?; - let font = Font::new(Arc::clone(&display), - "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?; + let font = Arc::new(Font::new(Arc::clone(&display), + "FantasqueSansM Nerd Font Mono:style=Regular:size=18")?); let dock = Dock { window, configuration, @@ -220,7 +224,8 @@ impl Dock { self.draw_corners(drawable, &context)?; } - self.draw_text()?; + self.draw_text("playful cool fic tech nonfic", Alignment::Left)?; + self.draw_text("abcy", Alignment::Right)?; Ok(()) } @@ -303,12 +308,27 @@ impl Dock { } - fn draw_text(&self) -> xcb::Result<()> { + fn draw_text(&self, text: &str, alignment: Alignment) -> xcb::Result<()> { let context = FontGraphicContext::new(self.window())?; let color = &self.configuration.foreground; - let text = "playful cool fic tech nonfic"; + let text = self.font.text_span(text)?; - self.font.draw(&context, color, 12, 25, 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; + + match alignment { + Alignment::Left => { + location.x += self.configuration.edge_inset as i32; + } + Alignment::Right => { + location.x += self.bounds.width as i32 - text.draw_size().width as i32; + location.x -= self.configuration.edge_inset as i32; + } + } + + text.draw(&context, color, location)?; Ok(()) } diff --git a/src/x.rs b/src/x.rs index 60a24bc..24eadc1 100644 --- a/src/x.rs +++ b/src/x.rs @@ -2,7 +2,7 @@ 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; +pub use font::{ Font, TextSpan }; pub use font_context::FontGraphicContext; pub use geometry::{ X_CLOCKWISE_RIGHT_ANGLE, SideExt, RectangleExt }; pub use window::{ Window, HasWindow, WindowExt }; diff --git a/src/x/font.rs b/src/x/font.rs index e0e3602..dc84199 100644 --- a/src/x/font.rs +++ b/src/x/font.rs @@ -1,16 +1,24 @@ use crate::x::*; +use crate::geometry::{ Point, Offset, Size }; use std::ffi::CString; use std::sync::Arc; -use x11::xft::{ XftDrawStringUtf8, XftFontOpenName }; +use x11::xft::{ XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName }; +use x11::xrender::XGlyphInfo; pub struct Font { - _display: Arc<Display>, + display: Arc<Display>, font_state: *mut x11::xft::XftFont, } +pub struct TextSpan { + font: Arc<Font>, + text: CString, + extent: XGlyphInfo, +} + impl Font { pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Font> { @@ -22,21 +30,131 @@ impl Font { XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast()) }; - Ok(Font { _display: display, font_state }) + Ok(Font { display, font_state }) + } + + pub fn text_span(self: &Arc<Self>, text: &str) -> xcb::Result<TextSpan> { + TextSpan::new(Arc::clone(self), text) } +} + +impl TextSpan { + pub fn new(font: Arc<Font>, text: &str) -> xcb::Result<Self> { + let xlib_display = font.display.connection().get_xlib_display(); + + let text = CString::new(text).unwrap(); + + let mut extent = XGlyphInfo { + width: 0, + height: 0, + x: 0, + y: 0, + xOff: 0, + yOff: 0, + }; + unsafe { + XftTextExtentsUtf8(xlib_display, + font.font_state, + text.as_ptr().cast(), + text.count_bytes() as i32, + &mut extent) + }; + + Ok(TextSpan { font, text, extent }) + } + + /* The coordinates given to draw() are those of the left-side baseline of + * the span's as-drawn pixel boundaries, in window coordinate space. Recall + * that window coordinate space puts the origin at the top-left. + * + * As-drawn boundaries are about what pixels actually get marked on the + * screen, which include the descender and any marks that are outside the + * font's bounds as described by its metrics. + * + * Though this can be counterintuitive to programmers, it is normal for + * fonts to draw outside their bounds. Font metrics are primarily intended + * to advise about graphic-design topics, which are socially defined and do + * not necessarily obey mathematical identities, even when such identities + * are offered as explanations of their meaning. + * + * Instead, the mathematical meaning of these metrics serves as a first + * layer, a literal meaning which provides a reference point for additional + * layers of meaning to be added atop, in the same fashion as how the + * duration of a musical measure is not actually fixed during a live + * performance. + * + * There are very good reasons that programmers find this state of affairs + * deeply saddening, but there is nothing to be done but deal with it. + */ pub fn draw(&self, context: &FontGraphicContext, color: &Color, - x: i32, y: i32, text: &str) + location: Point<i32>) -> xcb::Result<()> { let xft_draw = context.get_xft_draw(); - let text = CString::new(text).unwrap(); + unsafe { - XftDrawStringUtf8(xft_draw, color.as_xft_color(), self.font_state, x, y, - text.as_ptr().cast(), text.count_bytes() as i32) + XftDrawStringUtf8(xft_draw, + color.as_xft_color(), + self.font.font_state, + location.x, location.y, + self.text.as_ptr().cast(), + self.text.count_bytes() as i32) }; Ok(()) } + + /* This returns the offset from the top-left of the span's logical parcel to the + * left-side baseline of its as-drawn pixel boundaries. See the note on draw() for + * more context on what these as-drawn boundaries mean and why. + */ + pub fn draw_offset(&self) -> Offset<i16> { + Offset { + x: self.extent.x, + y: self.extent.y + } + } + + /* This is the as-drawn pixel size, rounded up to the next whole pixel, + * so that it is safe to use as a bounding box. It is computed only with + * respect to characters present in the span, and only considers the marks + * they actually draw. + * + * There are two important implications. First, due to rounding, + * pixel_size() is likely to be wider than logical_offset(). Second, trailing + * spaces are not included, since they do not draw any marks. + * + * In the vertical dimension, this size is conceptually similar to the + * maximum ascent plus the maximum descent of any character present in the + * span. Since there is no way within xft to obtain the font's declared + * ascent or descent, this measurement may be the best way to approximate + * those measurements. Note that there is nothing that even approximates line + * height; just guess. + * + * If you're thinking this all sounds highly inconvenient, you are correct. + * See the note on draw() for some highly relevant philosophy. + */ + pub fn draw_size(&self) -> Size<u16> { + Size { + width: self.extent.width, + height: self.extent.height, + } + } + + /* This returns the offset from the left-side baseline of the span's + * logical parcel to the new left-side baseline that a span occurring + * 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. + */ + pub fn logical_offset(&self) -> Offset<i16> { + Offset { + x: self.extent.xOff, + y: self.extent.yOff, + } + } } |