diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 2942fb962..bb5106208 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -1000,6 +1000,13 @@ pub struct FrameSpec { } impl FrameSpec { + /// The baseline grid when the text area can be counted in its steps: a step so small, or a + /// first line so far off, that the count is more than an f64 holds is no grid (as for the lines). + fn countable_grid(&self) -> Option<(f64, f64)> { + self.grid + .filter(|&(g_start, inc)| inc > 0.0 && inc.is_finite() && [self.area.y0, self.area.y1].iter().all(|y| ((y - g_start) / inc).is_finite())) + } + pub fn columns(&self) -> Vec { let a = self.area; // Primary frames inherit the document limit (216), beyond the interactive frame editor's 40. @@ -2808,7 +2815,7 @@ fn shape_paragraph(input: ParagraphShapeInput<'_, '_>) -> ShapedParagraph { let face = db.face(&base_chars.font_family, &base_chars.font_style); face.cap_height * base_chars.size / face.upem * base_chars.v_scale }; - let drop_grid = cur_frame.and_then(|f| f.grid).filter(|_| pp.grid_align == GridAlign::AllLines).map(|g| g.1); + let drop_grid = cur_frame.and_then(FrameSpec::countable_grid).filter(|_| pp.grid_align == GridAlign::AllLines).map(|g| g.1); // Align Left Edge (left-to-right paragraphs). let drop_align = pp.drop_cap_align_left && pp.direction == designcraft_doc::TextDirection::LeftToRight; // Tab stops for the drop cap, with the start-side indent (the right one, right to left). @@ -6253,7 +6260,7 @@ fn vertical_justify( } }; let center = f.opts.vertical_justification == VerticalJustification::Center; - let step = f.grid.map(|(_, inc)| inc).filter(|inc| *inc > 0.0 && inc.is_finite()).filter(|_| match f.opts.vertical_justification { + let step = f.countable_grid().map(|(_, inc)| inc).filter(|_| match f.opts.vertical_justification { VerticalJustification::Justify => idx.iter().zip(&rows).any(|(&i, &k)| k == n && on_grid(i)), _ => idx.iter().any(|&i| on_grid(i)), }); diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index f15ec4de4..fb8290b69 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -3000,6 +3000,19 @@ fn vertical_justification_with_an_extreme_grid_increment_stays_finite() { assert!((b - natural).abs() < 1e-6, "Top, grid away from the line: baseline {b}, natural {natural}"); } } + + // A grid whose first line is too far off to count the text area in its 0.7 pt steps: Bottom + // takes the room as it does without a grid. + let mut area = Rect::ZERO; + let (lines, _) = grid_doc("One", ParaAttrs { grid_align: Some(GridAlign::AllLines), ..Default::default() }, |d, sid| { + let spec = frame_specs(d, sid).remove(0); + area = spec.area; + let opts = &mut d.item_mut(spec.id).unwrap().text_frame_mut().unwrap().options; + opts.baseline_grid = Some(designcraft_doc::FrameBaselineGrid::new(-1.7e308, 0.7)); + opts.vertical_justification = Bottom; + }); + let b = lines[0].baseline; + assert!((b - area.y1).abs() < 1e-6, "Bottom with a far-off grid: baseline {b}, text area {area:?}"); } /// In a shaped frame that narrows and widens again, Align to Baseline Grid takes the grid step @@ -5195,6 +5208,22 @@ fn drop_cap_on_a_baseline_grid_reaches_down_by_grid_steps() { assert!((lines[2].baseline - cap_of(dc) - (lines[0].baseline - cap_of(body))).abs() < 0.01); } +/// A grid the lines can't count in steps (its first line too far off for a 0.7 pt step) is no grid +/// for the drop cap either: it reaches down to line 3 at the 12 pt leading the lines are set at. +#[test] +fn drop_cap_skips_a_grid_the_lines_skip() { + let (d, sid, _) = drop_doc(LOREM, ParaAttrs { grid_align: Some(designcraft_doc::GridAlign::AllLines), ..drop_cap(3, 1) }); + let mut specs = frame_specs(&d, sid); + specs[0].grid = Some((-1.7e308, 0.7)); + let cs = compose(&d, d.story(sid).unwrap(), &specs, &ComposeOptions::default()); + let lines = all_lines(&cs); + assert!((lines[2].baseline - lines[0].baseline - 24.0).abs() < 1e-6, "{} {}", lines[0].baseline, lines[2].baseline); + let dc = &lines[0].glyphs[0]; + assert!((lines[0].baseline + dc.y - lines[2].baseline).abs() < 1e-6, "drop cap baseline {} below line 1", dc.y); + let b = lines[0].drop_cap.unwrap(); + assert!((b.baseline - lines[2].baseline).abs() < 1e-6, "{}", b.baseline); +} + /// A line with a larger auto leading doesn't move the drop cap: it reaches down `lines − 1` /// leadings of the text after it, not to where line N is set. #[test]