Skip to content

Commit

Permalink
fix: underline not aligned
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin271 committed Jan 22, 2024
1 parent 5bca55d commit fc2b687
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
6 changes: 5 additions & 1 deletion src/interpreter/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::utils::Align;

use html5ever::{local_name, Attribute};

/// Finds whether an alignment exists within an attribute slice.
///
/// Returns the first alignment found, if any.
pub fn find_align(attrs: &[Attribute]) -> Option<Align> {
AttrIter::new(attrs).find_map(|attr| {
if let Attr::Align(align) = attr {
Expand Down Expand Up @@ -64,6 +67,7 @@ impl<'attrs> Iterator for AttrIter<'attrs> {
}
}

/// Attributes that can be held by elements
pub enum Attr {
Align(Align),
Href(String),
Expand Down Expand Up @@ -201,7 +205,7 @@ pub struct List {
pub list_type: ListType,
}

// Represents the number of parent text option tags the current element is a child of
/// Represents the number of parent text option tags the current element is a child of
#[derive(Default)]
pub struct TextOptions {
pub underline: usize,
Expand Down
31 changes: 11 additions & 20 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl TextBox {
}

Key {
align: self.align.into(),
lines,
size: self.font_size * self.hidpi_scale * zoom,
line_height: self.line_height(zoom),
Expand Down Expand Up @@ -262,28 +263,14 @@ impl TextBox {
) -> CachedTextArea {
let cache = text_system.text_cache.borrow_mut();

let (key, max_width) = {
let mut cache = cache.lock().unwrap();
let (key, paragraph) = cache.allocate(
text_system.font_system.lock().unwrap().borrow_mut(),
self.key(bounds, zoom),
);

let max_width = paragraph
.layout_runs()
.fold(0., |max, buffer| buffer.line_w.max(max));
(key, max_width)
};

let left = match self.align {
Align::Left => screen_position.0,
Align::Center => screen_position.0 + (bounds.0 - max_width) / 2.,
Align::Right => screen_position.0 + bounds.0 - max_width,
};
let (key, _) = cache.lock().unwrap().allocate(
text_system.font_system.lock().unwrap().borrow_mut(),
self.key(bounds, zoom),
);

CachedTextArea {
key,
left,
left: screen_position.0,
top: (screen_position.1 - scroll_y),
bounds: TextBounds::default(),
default_color: Color::rgb(255, 255, 255),
Expand Down Expand Up @@ -586,8 +573,10 @@ pub struct SectionKey<'a> {
index: usize,
}

/// The key of a [`TextCache`] entry
#[derive(Clone)]
pub struct Key<'a> {
align: glyphon::cosmic_text::Align,
lines: Vec<Vec<SectionKey<'a>>>,
size: f32,
line_height: f32,
Expand Down Expand Up @@ -652,7 +641,9 @@ impl TextCache {
.metadata(section.index),
)
}
let buffer_line = BufferLine::new(line_str, attrs_list, Shaping::Advanced);
let mut buffer_line = BufferLine::new(line_str, attrs_list, Shaping::Advanced);
buffer_line.set_align(Some(key.align));

buffer.lines.push(buffer_line);
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ impl Align {
}
}

impl From<Align> for glyphon::cosmic_text::Align {
fn from(value: Align) -> Self {
match value {
Align::Left => glyphon::cosmic_text::Align::Left,
Align::Center => glyphon::cosmic_text::Align::Center,
Align::Right => glyphon::cosmic_text::Align::Right,
}
}
}

#[derive(Default)]
pub struct HoverInfo {
pub cursor_icon: CursorIcon,
Expand Down

0 comments on commit fc2b687

Please sign in to comment.