From 584dd5fec69ad80feedf4018e02b427f2c812716 Mon Sep 17 00:00:00 2001 From: +merlan #flirora Date: Fri, 27 Jun 2025 05:26:15 -0400 Subject: Fix panic when sampling across two coincident gradient stops (#6166) --- crates/typst-library/src/visualize/gradient.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'crates/typst-library/src') diff --git a/crates/typst-library/src/visualize/gradient.rs b/crates/typst-library/src/visualize/gradient.rs index 5d7859a3..4917da68 100644 --- a/crates/typst-library/src/visualize/gradient.rs +++ b/crates/typst-library/src/visualize/gradient.rs @@ -1285,24 +1285,17 @@ fn process_stops(stops: &[Spanned]) -> SourceResult Color { let t = t.clamp(0.0, 1.0); - let mut low = 0; - let mut high = stops.len(); + let mut j = stops.partition_point(|(_, ratio)| ratio.get() < t); - while low < high { - let mid = (low + high) / 2; - if stops[mid].1.get() < t { - low = mid + 1; - } else { - high = mid; + if j == 0 { + while stops.get(j + 1).is_some_and(|(_, r)| r.is_zero()) { + j += 1; } + return stops[j].0; } - if low == 0 { - low = 1; - } - - let (col_0, pos_0) = stops[low - 1]; - let (col_1, pos_1) = stops[low]; + let (col_0, pos_0) = stops[j - 1]; + let (col_1, pos_1) = stops[j]; let t = (t - pos_0.get()) / (pos_1.get() - pos_0.get()); Color::mix_iter( -- cgit v1.2.3