Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

Use own font metrics for inline fragments

ldm08d890e005d66
diff --git a/moli-layout/src/inline.rs b/moli-layout/src/inline.rs
index 891f9f493..9431f06d1 100644
--- a/moli-layout/src/inline.rs
+++ b/moli-layout/src/inline.rs
@@ -180,6 +180,7 @@ pub(crate) struct InlineLinePlacement {
content_offset: f32,
item_offsets: Vec<f32>,
glyph_offsets: Vec<InlineGlyphOffset>,
+ box_block_placements: Vec<InlineBoxBlockPlacement>,
}
impl InlineLinePlacement {
@@ -207,6 +208,9 @@ impl InlineLinePlacement {
for glyph_offset in &mut self.glyph_offsets {
glyph_offset.offset += offset;
}
+ for box_placement in &mut self.box_block_placements {
+ box_placement.top += offset;
+ }
}
}
@@ -217,6 +221,13 @@ struct InlineGlyphOffset {
offset: f32,
}
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct InlineBoxBlockPlacement {
+ box_id: LayoutBoxId,
+ top: f32,
+ height: f32,
+}
+
impl InlineFormattingContext {
pub(crate) fn object(&self, id: u64) -> Option<&InlineObject> {
usize::try_from(id)
@@ -316,6 +327,14 @@ pub(crate) fn build_inline_fragments(
rect: line_rect,
baseline: placement.map_or(metrics.baseline, |placement| placement.baseline),
});
+ if let Some(placement) = placement {
+ for box_placement in &placement.box_block_placements {
+ box_fragments
+ .entry((box_placement.box_id.index(), line_index))
+ .or_default()
+ .include_block_axis(box_placement.top, box_placement.height);
+ }
+ }
for run in line.runs() {
let run_metrics = run.metrics();
@@ -357,7 +376,7 @@ pub(crate) fn build_inline_fragments(
box_fragments
.entry((ancestor.index(), line_index))
.or_default()
- .include(rect);
+ .include_inline_axis(rect.x, rect.width);
}
}
for source in context
@@ -402,7 +421,7 @@ pub(crate) fn build_inline_fragments(
.entry((ancestor.index(), line_index))
.or_default();
if let Some(rect) = rect {
- accumulator.include(rect);
+ accumulator.include_inline_axis(rect.x, rect.width);
} else if matches!(
object.role,
InlineObjectRole::StartEdge | InlineObjectRole::EndEdge
@@ -833,6 +852,18 @@ pub(crate) fn build_inline_line_placements(
})
})
.collect();
+ let box_block_placements = states
+ .iter()
+ .filter_map(|state| {
+ let strut = state.strut?;
+ let baseline = root_baseline + state.global_offset;
+ Some(InlineBoxBlockPlacement {
+ box_id: state.box_id,
+ top: baseline - strut.text_ascent,
+ height: (strut.text_ascent + strut.text_descent).max(0.0),
+ })
+ })
+ .collect();
placements.push(InlineLinePlacement {
line_index,
rect: PaintRect::new(
@@ -846,6 +877,7 @@ pub(crate) fn build_inline_line_placements(
content_offset: root_baseline - metrics.baseline,
item_offsets,
glyph_offsets,
+ box_block_placements,
});
preceding_adjustment += line_height - (raw_bottom - raw_top);
unadjusted_line_top += metrics.line_height.max(0.0);
@@ -1176,6 +1208,11 @@ impl FragmentAccumulator {
self.max_x = Some(self.max_x.map_or(x + width, |value| value.max(x + width)));
}
+ fn include_block_axis(&mut self, y: f32, height: f32) {
+ self.min_y = Some(self.min_y.map_or(y, |value| value.min(y)));
+ self.max_y = Some(self.max_y.map_or(y + height, |value| value.max(y + height)));
+ }
+
fn rect(self, fallback_block_rect: PaintRect) -> Option<PaintRect> {
let min_x = self.min_x?;
let min_y = self.min_y.unwrap_or(fallback_block_rect.y);
diff --git a/moli-layout/tests/phase4_layout_contract.rs b/moli-layout/tests/phase4_layout_contract.rs
index 9e40e07a0..72fcc42d3 100644
--- a/moli-layout/tests/phase4_layout_contract.rs
+++ b/moli-layout/tests/phase4_layout_contract.rs
@@ -1488,6 +1488,87 @@ fn structural_inline_own_strut_contributes_without_direct_text() {
);
}
+#[test]
+fn inline_fragment_uses_its_own_font_box_instead_of_descendant_union() {
+ let source = Source(vec![
+ Node::element("root", "div", LayoutElementCategory::Generic, None, vec![1]),
+ Node::element(
+ "small-parent",
+ "span",
+ LayoutElementCategory::Generic,
+ None,
+ vec![2],
+ ),
+ Node::element(
+ "large-child",
+ "span",
+ LayoutElementCategory::Generic,
+ None,
+ vec![3],
+ ),
+ Node::text("text", "A"),
+ ]);
+ let mut styles = Styles::default();
+ styles.primary.insert(
+ 0,
+ sized(LayoutDisplay::Block, 200.0, 100.0, PaintColor::TRANSPARENT)
+ .with_text_metrics(10.0, 60.0),
+ );
+ styles.primary.insert(
+ 1,
+ style(LayoutDisplay::Inline, RED).with_text_metrics(10.0, 10.0),
+ );
+ styles.primary.insert(
+ 2,
+ style(LayoutDisplay::Inline, BLUE).with_text_metrics(40.0, 40.0),
+ );
+
+ let snapshot = render(&source, &mut styles, 200, 100);
+ let parent = rect(&snapshot, RED);
+ let child = rect(&snapshot, BLUE);
+ assert!(
+ parent.height + 10.0 < child.height,
+ "an inline fragment's block size comes from its own primary font, not the union of a larger descendant: parent={parent:?}, child={child:?}"
+ );
+}
+
+#[test]
+fn empty_decorated_inline_fragment_does_not_fill_the_line_height() {
+ let source = Source(vec![
+ Node::element("root", "div", LayoutElementCategory::Generic, None, vec![1]),
+ Node::element(
+ "empty-inline",
+ "span",
+ LayoutElementCategory::Generic,
+ None,
+ vec![],
+ ),
+ ]);
+ let mut styles = Styles::default();
+ styles.primary.insert(
+ 0,
+ sized(LayoutDisplay::Block, 200.0, 100.0, PaintColor::TRANSPARENT)
+ .with_text_metrics(10.0, 60.0),
+ );
+ styles.primary.insert(
+ 1,
+ style(LayoutDisplay::Inline, RED)
+ .tap_taffy(|taffy| {
+ taffy.padding.left = taffy::LengthPercentage::length(4.0);
+ taffy.padding.right = taffy::LengthPercentage::length(4.0);
+ })
+ .with_text_metrics(10.0, 10.0),
+ );
+
+ let snapshot = render(&source, &mut styles, 200, 100);
+ let empty = rect(&snapshot, RED);
+ assert!(empty.width >= 7.9, "missing inline-axis padding: {empty:?}");
+ assert!(
+ empty.height < 30.0,
+ "an empty decorated inline uses its own font box rather than the root's 60px line box: {empty:?}"
+ );
+}
+
#[test]
fn nested_inline_alignments_move_each_structural_subtree_in_order() {
let source = Source(vec![