Skip to content

Commit 1fb104a

Browse files
committed
Create named struct for content widths
1 parent 5311fc5 commit 1fb104a

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

parley/src/layout/data.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use crate::inline_box::InlineBox;
5-
use crate::layout::{Alignment, Glyph, LineMetrics, RunMetrics, Style};
5+
use crate::layout::{Alignment, ContentWidths, Glyph, LineMetrics, RunMetrics, Style};
66
use crate::style::Brush;
77
use crate::util::nearly_zero;
88
use crate::Font;
@@ -201,7 +201,7 @@ pub(crate) struct LayoutData<B: Brush> {
201201
pub(crate) coords: Vec<i16>,
202202

203203
// Lazily calculated values
204-
content_widths: OnceCell<(f32, f32)>,
204+
content_widths: OnceCell<ContentWidths>,
205205

206206
// Input (/ output of style resolution)
207207
pub(crate) styles: Vec<Style<B>>,
@@ -477,13 +477,13 @@ impl<B: Brush> LayoutData<B> {
477477
}
478478
}
479479

480-
pub(crate) fn content_widths(&self) -> (f32, f32) {
480+
pub(crate) fn content_widths(&self) -> ContentWidths {
481481
*self
482482
.content_widths
483483
.get_or_init(|| self.calculate_content_widths())
484484
}
485485

486-
fn calculate_content_widths(&self) -> (f32, f32) {
486+
fn calculate_content_widths(&self) -> ContentWidths {
487487
fn whitespace_advance(cluster: Option<&ClusterData>) -> f32 {
488488
cluster
489489
.filter(|cluster| cluster.info.whitespace().is_space_or_nbsp())
@@ -528,6 +528,9 @@ impl<B: Brush> LayoutData<B> {
528528
max_width = max_width.max(running_max_width - trailing_whitespace);
529529
}
530530

531-
(min_width, max_width)
531+
ContentWidths {
532+
min: min_width,
533+
max: max_width,
534+
}
532535
}
533536
}

parley/src/layout/mod.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,9 @@ impl<B: Brush> Layout<B> {
8181
self.data.full_width
8282
}
8383

84-
/// Returns the minimum content width of the layout. This is the width of the layout if _all_
85-
/// soft line-breaking opportunities are taken.
86-
pub fn min_content_width(&self) -> f32 {
87-
self.data.content_widths().0
88-
}
89-
90-
/// Returns the maximum content width of the layout. This is the width of the layout if _no_
91-
/// soft line-breaking opportunities are taken.
92-
pub fn max_content_width(&self) -> f32 {
93-
self.data.content_widths().1
84+
/// Returns the lower and upper bounds on the width of the layout.
85+
pub fn content_widths(&self) -> ContentWidths {
86+
self.data.content_widths()
9487
}
9588

9689
/// Returns the height of the layout.
@@ -445,3 +438,14 @@ impl LayoutAccessibility {
445438
}
446439
}
447440
}
441+
442+
/// Lower and upper bounds on layout width based on its contents.
443+
#[derive(Copy, Clone, Debug)]
444+
pub struct ContentWidths {
445+
/// The minimum content width. This is the width of the layout if _all_ soft line-breaking
446+
/// opportunities are taken.
447+
pub min: f32,
448+
/// The maximum content width. This is the width of the layout if _no_ soft line-breaking
449+
/// opportunities are taken.
450+
pub max: f32,
451+
}

parley/src/tests/test_basic.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ fn content_widths() {
137137

138138
let mut layout = builder.build(text);
139139

140-
layout.break_all_lines(Some(layout.min_content_width()));
140+
layout.break_all_lines(Some(layout.content_widths().min));
141141
layout.align(None, Alignment::Start, false);
142142
env.with_name("min").check_layout_snapshot(&layout);
143143

144-
layout.break_all_lines(Some(layout.max_content_width()));
144+
layout.break_all_lines(Some(layout.content_widths().max));
145145
layout.align(None, Alignment::Start, false);
146146
env.with_name("max").check_layout_snapshot(&layout);
147147
}
@@ -160,7 +160,7 @@ fn inbox_content_width() {
160160
height: 10.0,
161161
});
162162
let mut layout = builder.build(text);
163-
layout.break_all_lines(Some(layout.min_content_width()));
163+
layout.break_all_lines(Some(layout.content_widths().min));
164164
layout.align(None, Alignment::Start, false);
165165

166166
env.with_name("full_width").check_layout_snapshot(&layout);
@@ -176,11 +176,11 @@ fn inbox_content_width() {
176176
height: 10.0,
177177
});
178178
let mut layout = builder.build(text);
179-
layout.break_all_lines(Some(layout.max_content_width()));
179+
layout.break_all_lines(Some(layout.content_widths().max));
180180
layout.align(None, Alignment::Start, false);
181181

182182
assert!(
183-
nearly_eq(layout.width(), layout.max_content_width()),
183+
nearly_eq(layout.width(), layout.content_widths().max),
184184
"Layout should be as wide as the max content width"
185185
);
186186

0 commit comments

Comments
 (0)