Fix panic in pad node

This commit is contained in:
Laurenz 2021-10-31 13:51:28 +01:00
parent 4645d1a517
commit 564cb7e989
4 changed files with 14 additions and 3 deletions

View File

@ -126,6 +126,13 @@ impl Length {
pub fn approx_eq(self, other: Self) -> bool {
self == other || (self - other).to_raw().abs() < 1e-6
}
/// Perform a checked division by a number, returning `None` if the result
/// is not finite.
pub fn div_finite(self, number: f64) -> Option<Self> {
let result = self.to_raw() / number;
result.is_finite().then(|| Self::raw(result))
}
}
impl Debug for Length {

View File

@ -25,7 +25,9 @@ impl BlockLevel for PadNode {
frames.iter_mut().zip(regions.iter())
{
fn solve_axis(length: Length, padding: Linear) -> Length {
(length + padding.abs) / (1.0 - padding.rel.get())
(length + padding.abs)
.div_finite(1.0 - padding.rel.get())
.unwrap_or_default()
}
// Solve for the size `padded` that satisfies (approximately):

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -27,9 +27,11 @@ Hi #box(pad(left: 10pt)[]) there
---
// Test that the pad node doesn't consume the whole region.
#page(height: 6cm)
#align(left)[Before]
#pad(10pt, image("../../res/tiger.jpg"))
#align(right)[After]
---
// Test that padding adding up to 100% does not panic.
#pad(50%)[]