diff --git a/src/rust_typeset/math_layout.rs b/src/rust_typeset/math_layout.rs index e441c90..112a400 100644 --- a/src/rust_typeset/math_layout.rs +++ b/src/rust_typeset/math_layout.rs @@ -893,7 +893,11 @@ impl InlineMathItem { Self::Text { width, .. } => *width, Self::Formula(layout) => layout.size.width, Self::MathSpacing(glue) => glue.width, - Self::Kern(width) | Self::Glue(width) => *width, + Self::Kern(width) => *width, + // Overfull inline-math lines can produce a negative shrink glue. + // Painting with a negative advance would put the following word on + // top of the current one, so clamp the space at zero. + Self::Glue(width) => (*width).max(Mm::from(0.6)), Self::ExplicitGlue(glue) => mm_from_scaled(glue.width), Self::BreakPenalty(_) => Mm::from(0.0), } diff --git a/src/rust_typeset/pdf.rs b/src/rust_typeset/pdf.rs index 39971dc..97dd7f6 100644 --- a/src/rust_typeset/pdf.rs +++ b/src/rust_typeset/pdf.rs @@ -1838,7 +1838,10 @@ fn paragraph_glue_width(glue: &Glue, order: common::GlueOrder, ratio: f64) -> Sc let adjustment = if ratio > 0.0 && glue.stretch_order == order { f64::from(glue.stretch.0) * ratio } else if ratio < 0.0 && glue.shrink_order == order { - f64::from(glue.shrink.0) * ratio + // Never shrink a space past its declared shrink amount: a TeX + // overfull line may report ratio < -1, which would make the space + // negative and paint adjacent words on top of each other. + f64::from(glue.shrink.0) * ratio.max(-1.0) } else { 0.0 }; diff --git a/src/rust_typeset/pdf/layout.rs b/src/rust_typeset/pdf/layout.rs index d650ac3..28b114a 100644 --- a/src/rust_typeset/pdf/layout.rs +++ b/src/rust_typeset/pdf/layout.rs @@ -1296,7 +1296,8 @@ fn measure_positioned_lines( .map(|(line_index, (height_mm, element))| { let height = i64::from(scaled_points(f64::from(height_mm) * PDF_POINTS_PER_INCH / 25.4).0) - .saturating_add(1); + .saturating_add(1) + .max(baseline_skip_scaled(font_size)); let ascent = millimeters_to_scaled(f64::from(element.baseline())).min(height); let pre_display_size = element .pre_display_size()