mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Fix panic in pad node
This commit is contained in:
parent
4645d1a517
commit
564cb7e989
@ -126,6 +126,13 @@ impl Length {
|
|||||||
pub fn approx_eq(self, other: Self) -> bool {
|
pub fn approx_eq(self, other: Self) -> bool {
|
||||||
self == other || (self - other).to_raw().abs() < 1e-6
|
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 {
|
impl Debug for Length {
|
||||||
|
@ -25,7 +25,9 @@ impl BlockLevel for PadNode {
|
|||||||
frames.iter_mut().zip(regions.iter())
|
frames.iter_mut().zip(regions.iter())
|
||||||
{
|
{
|
||||||
fn solve_axis(length: Length, padding: Linear) -> Length {
|
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):
|
// Solve for the size `padded` that satisfies (approximately):
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |
@ -27,9 +27,11 @@ Hi #box(pad(left: 10pt)[]) there
|
|||||||
|
|
||||||
---
|
---
|
||||||
// Test that the pad node doesn't consume the whole region.
|
// Test that the pad node doesn't consume the whole region.
|
||||||
|
|
||||||
#page(height: 6cm)
|
#page(height: 6cm)
|
||||||
|
|
||||||
#align(left)[Before]
|
#align(left)[Before]
|
||||||
#pad(10pt, image("../../res/tiger.jpg"))
|
#pad(10pt, image("../../res/tiger.jpg"))
|
||||||
#align(right)[After]
|
#align(right)[After]
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test that padding adding up to 100% does not panic.
|
||||||
|
#pad(50%)[]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user