Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions internal/core/textlayout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod shaping;
/// cbindgen:ignore
pub mod sharedparley;
use shaping::ShapeBuffer;
pub use shaping::{AbstractFont, FontMetrics, Glyph, TextShaper};
pub use shaping::{AbstractFont, FontMetrics, Glyph, LengthDiv, TextShaper};

mod linebreaker;
pub use linebreaker::TextLine;
Expand Down Expand Up @@ -422,13 +422,7 @@ impl<Font: AbstractFont> TextParagraphLayout<'_, Font> {
if line_height <= Font::Length::zero() {
return usize::MAX;
}
let mut lines = 0usize;
let mut height = Font::Length::zero();
while height + line_height <= self.max_height {
height += line_height;
lines += 1;
}
lines
self.max_height.div_count(line_height)
}

/// Returns the leading edge of the glyph at the given byte offset
Expand Down
44 changes: 44 additions & 0 deletions internal/core/textlayout/shaping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub trait TextShaper {
+ core::cmp::PartialOrd
+ core::ops::Mul<Self::LengthPrimitive, Output = Self::Length>
+ core::ops::Div<Self::LengthPrimitive, Output = Self::Length>
+ LengthDiv
+ core::fmt::Debug;
// Shapes the given string and emits the result into the given glyphs buffer.
fn shape_text<GlyphStorage: core::iter::Extend<Glyph<Self::Length>>>(
Expand All @@ -67,6 +68,35 @@ pub trait TextShaper {
fn glyph_for_char(&self, ch: char) -> Option<Glyph<Self::Length>>;
}

/// How many times one length fits into another of the same unit.
/// euclid's `Length / Length` produces a `Scale` rather than a plain number, so the division the
/// paragraph layout needs is spelled out here instead of as a `core::ops::Div` bound.
pub trait LengthDiv {
/// The number of whole `divisor`s in `self`, truncated, and zero when that count is negative.
/// `divisor` must not be zero.
fn div_count(self, divisor: Self) -> usize;
}

impl LengthDiv for f32 {
fn div_count(self, divisor: Self) -> usize {
// Casts from float saturate, so a negative or NaN ratio ends up at zero.
(self / divisor) as usize
}
}

impl LengthDiv for i16 {
fn div_count(self, divisor: Self) -> usize {
// Widen so that i16::MIN / -1 can't overflow.
(i32::from(self) / i32::from(divisor)).max(0) as usize
}
}

impl<T: LengthDiv + Clone, U> LengthDiv for euclid::Length<T, U> {
fn div_count(self, divisor: Self) -> usize {
self.get().div_count(divisor.get())
}
}

pub trait FontMetrics<Length: Copy + core::ops::Sub<Output = Length>> {
fn height(&self) -> Length {
self.ascent() - self.descent()
Expand Down Expand Up @@ -214,6 +244,20 @@ impl<Length> ShapeBuffer<Length> {
}
}

#[test]
fn test_div_count() {
assert_eq!(9.0_f32.div_count(3.0), 3);
assert_eq!(10.0_f32.div_count(3.0), 3);
assert_eq!((-10.0_f32).div_count(3.0), 0);

type IntLen = euclid::Length<i16, euclid::UnknownUnit>;
assert_eq!(IntLen::new(10).div_count(IntLen::new(3)), 3);
assert_eq!(IntLen::new(-10).div_count(IntLen::new(3)), 0);

type FloatLen = euclid::Length<f32, euclid::UnknownUnit>;
assert_eq!(FloatLen::new(10.).div_count(FloatLen::new(3.)), 3);
}

#[test]
fn test_shape_boundaries_simple() {
{
Expand Down
Loading