Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
#58 renamed textfragment wrap to word wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
bennobuilder committed Mar 25, 2024
1 parent 0bdfde8 commit 14bcd06
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
18 changes: 10 additions & 8 deletions crates/attributed_string/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use dyn_utils::{
units::{abs::Abs, em::Em},
};
use glam::Vec2;
use line_wrap::{no_wrap::NoLineWrap, text_fragment_wrap::TextFragmentWrap, LineWrapStrategy};
use line_wrap::{no_wrap::NoLineWrap, word_wrap::WordWrap, LineWrapStrategy};
use rust_lapper::Lapper;
use tokens::{line::LineToken, span::SpanToken};
use utils::is_range_within;
Expand Down Expand Up @@ -113,7 +113,7 @@ impl AttributedString {
pub fn layout(&mut self) {
let mut line_wrap_strategy: Box<dyn LineWrapStrategy> = match self.config.line_wrap {
LineWrap::None => Box::new(NoLineWrap),
LineWrap::Word => Box::new(TextFragmentWrap),
LineWrap::Word => Box::new(WordWrap),
_ => Box::new(NoLineWrap),
};
let lines =
Expand All @@ -128,12 +128,14 @@ impl AttributedString {
let line_range = line.get_range();

current_pos.x = 0.0;
current_pos.y += line.height(&self.spans, &self.attrs_intervals);

// To mirror figmas behavior
if index == 0 {
current_pos.y *= 0.78;
}
current_pos.y += if index == 0 {
// TODO: for the first line apply the glyph height
// starting from the baseline not the decsent
// so basically the plain ascent?
line.height(&self.spans, &self.attrs_intervals) * 0.78
} else {
line.height(&self.spans, &self.attrs_intervals)
};

let mut max_ascent = Em::zero();
let mut max_descent = Em::zero();
Expand Down
2 changes: 1 addition & 1 deletion crates/attributed_string/src/line_wrap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod no_wrap;
pub mod text_fragment_wrap;
pub mod word_wrap;

use crate::{
attrs::AttrsIntervals,
Expand Down
4 changes: 3 additions & 1 deletion crates/attributed_string/src/line_wrap/no_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl LineWrapStrategy for NoLineWrap {
_: &Size,
) -> Vec<LineToken> {
let mut lines: Vec<LineToken> = Vec::new();

let mut current_span_ranges: Vec<SpanRange> = Vec::new();

for (index, span) in spans.iter().enumerate() {
let mut span_range_start = span.get_range().start;

Expand All @@ -39,6 +39,7 @@ impl LineWrapStrategy for NoLineWrap {
}
}

// Handle any remaining span range at the end
if span_range_start < span.get_range().end {
current_span_ranges.push(SpanRange::new(
index,
Expand All @@ -47,6 +48,7 @@ impl LineWrapStrategy for NoLineWrap {
}
}

// Handle any remaining span ranges at the end
if !current_span_ranges.is_empty() {
lines.push(LineToken::new(current_span_ranges));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use dyn_utils::{properties::size::Size, units::abs::Abs};

/// Line wrap strategy that wraps text at word boundaries
/// or line breaks based on specified width constraints.
pub struct TextFragmentWrap;
pub struct WordWrap;

impl LineWrapStrategy for TextFragmentWrap {
impl LineWrapStrategy for WordWrap {
fn compute_lines(
&mut self,
spans: &[SpanToken],
Expand Down
12 changes: 12 additions & 0 deletions crates/attributed_string/src/tokens/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ pub enum ShapeTokenVariant {
TextFragment(TextFragmentToken),
}

impl ShapeTokenVariant {
pub fn get_shape_token(&self) -> &dyn ShapeToken {
match self {
ShapeTokenVariant::Glyph(token) => token,
ShapeTokenVariant::WordSeparator(token) => token,
ShapeTokenVariant::Linebreak(token) => token,
ShapeTokenVariant::Bitmap(token) => token,
ShapeTokenVariant::TextFragment(token) => token,
}
}
}

#[derive(Debug)]
pub struct ShapeBuffer {
pub buffer: Option<rustybuzz::UnicodeBuffer>,
Expand Down

0 comments on commit 14bcd06

Please sign in to comment.