Skip to content

Commit 99b3000

Browse files
committed
Implement text-wrap-mode
1 parent d6af4ea commit 99b3000

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ taffy = { version = "0.8", default-features = false, features = ["std", "flexbox
5252
color = "0.3"
5353
peniko = "0.4"
5454
kurbo = "0.11"
55-
parley = { version = "0.4", default-features = false, features = ["std"] }
55+
parley = { path = "../parley/parley", version = "0.4", default-features = false, features = ["std"] }
5656
wgpu = "24"
5757
softbuffer = "0.4"
5858
vello = { version = "0.5", features = [ "wgpu" ] }

packages/blitz-dom/src/layout/construct.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ fn push_children_and_pseudos(layout_children: &mut Vec<usize>, node: &Node) {
4444
}
4545
}
4646

47+
/// Convert a relative line height to an absolute one
48+
fn resolve_line_height(line_height: parley::LineHeight, font_size: f32) -> f32 {
49+
let size = match line_height {
50+
parley::LineHeight::FontSizeRelative(relative) => relative * font_size,
51+
parley::LineHeight::Absolute(absolute) => absolute,
52+
parley::LineHeight::MetricsRelative(_) => unreachable!(),
53+
};
54+
size
55+
}
56+
4757
pub(crate) fn collect_layout_children(
4858
doc: &mut BaseDocument,
4959
container_node_id: usize,
@@ -446,7 +456,7 @@ fn node_list_item_child(
446456
};
447457

448458
let mut layout = builder.build().0;
449-
let width = layout.max_content_width();
459+
let width = layout.calculate_content_widths().max;
450460
layout.break_all_lines(Some(width));
451461

452462
ListItemLayoutPosition::Outside(Box::new(layout))
@@ -731,7 +741,7 @@ pub(crate) fn build_inline_layout(
731741

732742
// dbg!(&parley_style);
733743

734-
let root_line_height = parley_style.line_height;
744+
let root_line_height = resolve_line_height(parley_style.line_height, parley_style.font_size);
735745

736746
// Create a parley tree builder
737747
let mut builder =
@@ -898,9 +908,14 @@ pub(crate) fn build_inline_layout(
898908

899909
// style.brush = peniko::Brush::Solid(peniko::Color::WHITE);
900910

911+
let font_size = style.font_size;
912+
901913
// Floor the line-height of the span by the line-height of the inline context
902914
// See https://www.w3.org/TR/CSS21/visudet.html#line-height
903-
style.line_height = style.line_height.max(root_line_height);
915+
style.line_height = parley::LineHeight::Absolute(
916+
resolve_line_height(style.line_height, font_size)
917+
.max(root_line_height),
918+
);
904919

905920
// dbg!(node_id);
906921
// dbg!(&style);

packages/blitz-dom/src/layout/inline.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ impl BaseDocument {
7474
.width
7575
.map(|w| (w * scale) - pbw)
7676
.unwrap_or_else(|| {
77-
let content_sizes = inline_layout.layout.content_widths();
77+
// TODO: cache content widths
78+
let content_sizes = inline_layout.layout.calculate_content_widths();
7879
let computed_width = match available_space.width {
7980
AvailableSpace::MinContent => content_sizes.min,
8081
AvailableSpace::MaxContent => content_sizes.max,

packages/blitz-dom/src/stylo_to_parley.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::util::ToColorColor;
88

99
// Module of type aliases so we can refer to stylo types with nicer names
1010
pub(crate) mod stylo {
11+
pub(crate) use style::computed_values::text_wrap_mode::T as TextWrapMode;
1112
pub(crate) use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
1213
pub(crate) use style::properties::ComputedValues;
1314
pub(crate) use style::values::computed::OverflowWrap;
@@ -43,13 +44,11 @@ pub(crate) fn style(
4344

4445
// Convert font size and line height
4546
let font_size = font_styles.font_size.used_size.0.px();
46-
let line_height: f32 = match font_styles.line_height {
47-
stylo::LineHeight::Normal => font_size * 1.2,
48-
stylo::LineHeight::Number(num) => font_size * num.0,
49-
stylo::LineHeight::Length(value) => value.0.px(),
47+
let line_height = match font_styles.line_height {
48+
stylo::LineHeight::Normal => parley::LineHeight::FontSizeRelative(1.2),
49+
stylo::LineHeight::Number(num) => parley::LineHeight::FontSizeRelative(num.0),
50+
stylo::LineHeight::Length(value) => parley::LineHeight::Absolute(value.0.px()),
5051
};
51-
// Parley expects line height as a multiple of font size!
52-
let line_height = line_height / font_size;
5352

5453
let letter_spacing = itext_styles
5554
.letter_spacing
@@ -126,6 +125,10 @@ pub(crate) fn style(
126125
.map(TextBrush::from_color);
127126

128127
// Wrapping and breaking
128+
let text_wrap_mode = match itext_styles.text_wrap_mode {
129+
stylo::TextWrapMode::Wrap => parley::TextWrapMode::Wrap,
130+
stylo::TextWrapMode::Nowrap => parley::TextWrapMode::NoWrap,
131+
};
129132
let word_break = match itext_styles.word_break {
130133
stylo::WordBreak::Normal => parley::WordBreakStrength::Normal,
131134
stylo::WordBreak::BreakAll => parley::WordBreakStrength::BreakAll,
@@ -161,5 +164,6 @@ pub(crate) fn style(
161164
letter_spacing,
162165
overflow_wrap,
163166
word_break,
167+
text_wrap_mode,
164168
}
165169
}

0 commit comments

Comments
 (0)