Skip to content

Commit 1197ebc

Browse files
committed
Add word and letter spacing to text layout based on style properties
- Updated `shape_text` function to include `word_spacing` and `letter_spacing` from the style. - Added a new test to verify that spacing changes are correctly applied per style run in the text layout.
1 parent c355468 commit 1197ebc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

parley/src/shape/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ pub(crate) fn shape_text<'a, B: Brush>(
174174
item.locale = style.locale;
175175
item.variations = style.font_variations;
176176
item.features = style.font_features;
177+
item.word_spacing = style.word_spacing;
178+
item.letter_spacing = style.letter_spacing;
177179
text_range.start = text_range.end;
178180
char_range.start = char_range.end;
179181
}

parley/src/tests/test_basic.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,34 @@ fn realign_all() {
734734
}
735735
}
736736

737+
#[test]
738+
fn spacing_changes_per_style_run() {
739+
let mut env = TestEnv::new(test_name!(), None);
740+
741+
let text = "foo bar";
742+
let mut builder = env.ranged_builder(text);
743+
builder.push(StyleProperty::WordSpacing(2.0), 3..text.len());
744+
builder.push(StyleProperty::LetterSpacing(1.5), 3..text.len());
745+
746+
let layout = builder.build(text);
747+
assert_eq!(
748+
layout.data.runs.len(),
749+
2,
750+
"expected two runs after style break"
751+
);
752+
753+
let first_run = &layout.data.runs[0];
754+
let second_run = &layout.data.runs[1];
755+
756+
assert_eq!(&text[first_run.text_range.clone()], "foo");
757+
assert_eq!(first_run.word_spacing, 0.0);
758+
assert_eq!(first_run.letter_spacing, 0.0);
759+
760+
assert_eq!(&text[second_run.text_range.clone()], " bar");
761+
assert_eq!(second_run.word_spacing, 2.0);
762+
assert_eq!(second_run.letter_spacing, 1.5);
763+
}
764+
737765
#[test]
738766
fn layout_impl_send_sync() {
739767
fn assert_send_sync<T: Send + Sync>() {}

0 commit comments

Comments
 (0)