Skip to content

Commit 76b16cb

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

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
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

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

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()
87+
}
88+
8489
/// Returns the minimum content width of the layout. This is the width of the layout if _all_
8590
/// soft line-breaking opportunities are taken.
8691
pub fn min_content_width(&self) -> f32 {
87-
self.data.content_widths().0
92+
self.data.content_widths().min
8893
}
8994

9095
/// Returns the maximum content width of the layout. This is the width of the layout if _no_
9196
/// soft line-breaking opportunities are taken.
9297
pub fn max_content_width(&self) -> f32 {
93-
self.data.content_widths().1
98+
self.data.content_widths().max
9499
}
95100

96101
/// Returns the height of the layout.
@@ -445,3 +450,14 @@ impl LayoutAccessibility {
445450
}
446451
}
447452
}
453+
454+
/// Lower and upper bounds on layout width based on its contents.
455+
#[derive(Copy, Clone, Debug)]
456+
pub struct ContentWidths {
457+
/// The minimum content width. This is the width of the layout if _all_ soft line-breaking
458+
/// opportunities are taken.
459+
pub min: f32,
460+
/// The maximum content width. This is the width of the layout if _no_ soft line-breaking
461+
/// opportunities are taken.
462+
pub max: f32,
463+
}

0 commit comments

Comments
 (0)