Skip to content

Commit

Permalink
fix: text wrapping logic with leading empty space
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed Aug 31, 2023
1 parent abb4115 commit 436ee47
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fontkit"
version = "0.4.1"
version = "0.4.4"
edition = "2021"
authors = ["Zimon Dai <daizhuoxian@gmail.com>"]
description = "A simple library for font loading and indexing"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fontkit-rs",
"version": "0.0.5",
"version": "0.0.6",
"description": "Toolkit used to load, match, measure, and render texts",
"main": "pkg/bundler/index.js",
"directories": {
Expand Down
32 changes: 27 additions & 5 deletions src/metrics/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub struct Span<T> {

impl<T> Span<T> {
fn width(&self) -> f32 {
if self.metrics.positions.is_empty() {
return 0.0;
}
let mut width = self.metrics.width(self.size, self.letter_spacing);
if self.swallow_leading_space && self.metrics.positions[0].metrics.c == ' ' {
let c = &self.metrics.positions[0];
Expand Down Expand Up @@ -291,7 +294,7 @@ where
let mut current_real_index = real_index;
while span.metrics.positions()[current_real_index].metrics.c == ' '
&& span_values
.get(current_real_index)
.get(current_real_index - real_index)
.map(|c| *c != ' ')
.unwrap_or(true)
{
Expand All @@ -303,9 +306,9 @@ where
}
let factor = span.size / span.metrics.units() as f32;
let range = if rtl {
(current_real_index + 1 - count)..total_count
(current_real_index + 1 - count)..current_real_index
} else {
0..(current_real_index + count)
current_real_index..(current_real_index + count)
};
let acc_seg_width = range
.map(|index| span.metrics.positions.get(index).unwrap())
Expand All @@ -315,20 +318,39 @@ where
+ p.metrics.advanced_x as f32 * factor
+ span.letter_spacing
});
if current_line_width + acc_seg_width <= width {
let acc_seg_width_with_space = if current_real_index == real_index {
acc_seg_width
} else {
let range = if rtl {
(current_real_index + 1 - count)..real_index
} else {
real_index..(current_real_index + count)
};
range
.map(|index| span.metrics.positions.get(index).unwrap())
.fold(0.0, |current, p| {
current
+ p.kerning as f32 * factor
+ p.metrics.advanced_x as f32 * factor
+ span.letter_spacing
})
};
if current_line_width + acc_seg_width_with_space <= width {
if rtl {
real_index = current_real_index - count;
} else {
real_index = current_real_index + count;
}
current_line_width += acc_seg_width;
} else {
real_index = current_real_index;
break;
}
}
if (real_index == 0 && !rtl)
|| (rtl && real_index == span.metrics.positions.len() - 1)
{
real_index = naive_break_index
real_index = naive_break_index;
}

// Split here, create a new span
Expand Down

0 comments on commit 436ee47

Please sign in to comment.