From 99761e03651693ce29bf1e8605d0fae035c268b7 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Wed, 30 Oct 2024 21:18:46 +0200 Subject: [PATCH] #429 [refactor] replace few setters in `InlineBoxMeasurements` by constructor parameters --- .../xhtmlrenderer/layout/InlineBoxing.java | 3 +- .../layout/VerticalAlignContext.java | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/InlineBoxing.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/InlineBoxing.java index 88a915fb8..7242705bb 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/InlineBoxing.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/InlineBoxing.java @@ -483,9 +483,8 @@ private static void positionVertically( current.setHeight(0); } else { FSFontMetrics strutM = container.getStyle().getFSFontMetrics(c); - VerticalAlignContext vaContext = new VerticalAlignContext(); InlineBoxMeasurements measurements = getInitialMeasurements(c, container, strutM); - vaContext.setInitialMeasurements(measurements); + VerticalAlignContext vaContext = new VerticalAlignContext(measurements); List lBDecorations = calculateTextDecorations(container, measurements.getBaseline(), strutM); current.setTextDecorations(lBDecorations); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/VerticalAlignContext.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/VerticalAlignContext.java index 7a3ce5c77..50a11cb10 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/VerticalAlignContext.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/VerticalAlignContext.java @@ -19,6 +19,7 @@ */ package org.xhtmlrenderer.layout; +import org.jspecify.annotations.Nullable; import org.xhtmlrenderer.css.constants.CSSName; import org.xhtmlrenderer.css.constants.IdentValue; import org.xhtmlrenderer.render.Box; @@ -27,6 +28,8 @@ import java.util.ArrayList; import java.util.List; +import static java.util.Objects.requireNonNullElse; + /** * This class performs the real work of vertically positioning inline boxes * within a line (i.e. implementing the vertical-align property). Because @@ -51,7 +54,17 @@ public class VerticalAlignContext { private final List _children = new ArrayList<>(); - private VerticalAlignContext _parent = null; + @Nullable + private final VerticalAlignContext _parent; + + public VerticalAlignContext(VerticalAlignContext parent) { + _parent = parent; + } + + public VerticalAlignContext(InlineBoxMeasurements initialMeasurements) { + _parent = null; + _measurements.add(initialMeasurements); + } private void moveTrackedValues(int ty) { if (_inlineTopSet) { @@ -138,13 +151,10 @@ public int getPaintingTop() { } public VerticalAlignContext createChild(Box root) { - VerticalAlignContext result = new VerticalAlignContext(); - VerticalAlignContext vaRoot = getRoot(); - - result.setParent(vaRoot); - InlineBoxMeasurements initial = vaRoot._measurements.get(0); + + VerticalAlignContext result = new VerticalAlignContext(vaRoot); result.pushMeasurements(initial); vaRoot._children.add(new ChildContextData(root, result)); @@ -155,17 +165,13 @@ private List getChildren() { return _children; } + @Nullable public VerticalAlignContext getParent() { return _parent; } - public void setParent(VerticalAlignContext parent) { - _parent = parent; - } - private VerticalAlignContext getRoot() { - VerticalAlignContext result = this; - return result.getParent() != null ? result.getParent() : this; + return requireNonNullElse(_parent, this); } private void merge(VerticalAlignContext context) { @@ -184,10 +190,6 @@ public void alignChildren() { } } - public void setInitialMeasurements(InlineBoxMeasurements measurements) { - _measurements.add(measurements); - } - private static final class ChildContextData { private final Box _root; private final VerticalAlignContext _verticalAlignContext;