diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StyleReference.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StyleReference.java index 722af8493..61b32987e 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StyleReference.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StyleReference.java @@ -19,6 +19,8 @@ */ package org.xhtmlrenderer.context; +import com.google.errorprone.annotations.CheckReturnValue; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -165,7 +167,9 @@ public CascadedStyle getCascadedStyle(@Nullable Element e, boolean restyle) { return _matcher.getCascadedStyle(e, restyle); } - public PageInfo getPageStyle(String pageName, String pseudoPage) { + @NonNull + @CheckReturnValue + public PageInfo getPageStyle(@Nullable String pageName, String pseudoPage) { return _matcher.getPageCascadedStyle(pageName, pseudoPage); } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/newmatch/Matcher.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/newmatch/Matcher.java index dd4218cc8..53201de59 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/newmatch/Matcher.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/newmatch/Matcher.java @@ -20,6 +20,8 @@ */ package org.xhtmlrenderer.css.newmatch; +import com.google.errorprone.annotations.CheckReturnValue; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -105,7 +107,9 @@ public CascadedStyle getPECascadedStyle(Element e, String pseudoElement) { } } - public PageInfo getPageCascadedStyle(String pageName, String pseudoPage) { + @NonNull + @CheckReturnValue + public PageInfo getPageCascadedStyle(@Nullable String pageName, String pseudoPage) { List props = new ArrayList<>(); Map> marginBoxes = new HashMap<>(); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/sheet/PageRule.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/sheet/PageRule.java index 4e16b7ffb..1b37cf99c 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/sheet/PageRule.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/sheet/PageRule.java @@ -19,6 +19,7 @@ */ package org.xhtmlrenderer.css.sheet; +import org.jspecify.annotations.Nullable; import org.xhtmlrenderer.css.constants.MarginBoxName; import org.xhtmlrenderer.css.sheet.StylesheetInfo.Origin; @@ -110,7 +111,7 @@ public long getOrder() { return result; } - public boolean applies(String pageName, String pseudoPage) { + public boolean applies(@Nullable String pageName, String pseudoPage) { if (_name == null && _pseudoPage == null) { return true; } else if (_name == null && diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/style/CalculatedStyle.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/style/CalculatedStyle.java index ee0e5ea56..b6ff0634f 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/style/CalculatedStyle.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/style/CalculatedStyle.java @@ -20,6 +20,8 @@ */ package org.xhtmlrenderer.css.style; +import com.google.errorprone.annotations.CheckReturnValue; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.xhtmlrenderer.css.constants.CSSName; import org.xhtmlrenderer.css.constants.IdentValue; @@ -182,6 +184,8 @@ private static boolean checkBordersAllowed(IdentValue display) { * @param matched the CascadedStyle to apply * @return The derived child style */ + @NonNull + @CheckReturnValue public CalculatedStyle deriveStyle(CascadedStyle matched) { String fingerprint = matched.getFingerprint(); return _childCache.computeIfAbsent(fingerprint, (key) -> new CalculatedStyle(this, matched)); diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/Layer.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/Layer.java index a69131aa5..7bd82cb0a 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/Layer.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/Layer.java @@ -777,8 +777,6 @@ public void removeLastPage() { @CheckReturnValue public static PageBox createPageBox(CssContext c, String pseudoPage) { - PageBox result = new PageBox(); - String pageName = null; // HACK We only create pages during layout, but the OutputDevice // queries page positions and since pages are created lazily, changing @@ -788,13 +786,8 @@ public static PageBox createPageBox(CssContext c, String pseudoPage) { } PageInfo pageInfo = c.getCss().getPageStyle(pageName, pseudoPage); - result.setPageInfo(pageInfo); - CalculatedStyle cs = new EmptyStyle().deriveStyle(pageInfo.getPageStyle()); - result.setStyle(cs); - result.setOuterPageWidth(result.getWidth(c)); - - return result; + return new PageBox(pageInfo, c, cs); } @Nullable diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/PageBox.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/PageBox.java index c326fff3f..78537316e 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/PageBox.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/PageBox.java @@ -20,6 +20,7 @@ package org.xhtmlrenderer.render; import com.google.errorprone.annotations.CheckReturnValue; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.w3c.dom.Element; import org.w3c.dom.css.CSSPrimitiveValue; @@ -74,8 +75,7 @@ public class PageBox { private static final int LEADING_TRAILING_SPLIT = 5; - @Nullable - private CalculatedStyle _style; + private final CalculatedStyle _style; private int _top; private int _bottom; @@ -85,13 +85,13 @@ public class PageBox { private int _pageNo; - private int _outerPageWidth; + private final int _outerPageWidth; @Nullable private PageDimensions _pageDimensions; - @Nullable - private PageInfo _pageInfo; + @NonNull + private final PageInfo _pageInfo; @Nullable private final MarginAreaContainer[] _marginAreas = new MarginAreaContainer[MARGIN_AREA_DEFS.length]; @@ -99,6 +99,12 @@ public class PageBox { @Nullable private Element _metadata; + public PageBox(PageInfo pageInfo, CssContext cssContext, CalculatedStyle style) { + _pageInfo = pageInfo; + _style = style; + _outerPageWidth = getWidth(cssContext); + } + public int getWidth(CssContext cssCtx) { return getPageDimensions(cssCtx).width(); } @@ -211,10 +217,6 @@ public CalculatedStyle getStyle() { return _style; } - public void setStyle(CalculatedStyle style) { - _style = style; - } - public int getBottom() { return _bottom; } @@ -333,10 +335,6 @@ public int getOuterPageWidth() { return _outerPageWidth; } - public void setOuterPageWidth(int containingBlockWidth) { - _outerPageWidth = containingBlockWidth; - } - public int getMarginBorderPadding(CssContext cssCtx, Edge edge) { return getStyle().getMarginBorderPadding(cssCtx, getOuterPageWidth(), edge); } @@ -345,10 +343,6 @@ public PageInfo getPageInfo() { return _pageInfo; } - public void setPageInfo(PageInfo pageInfo) { - _pageInfo = pageInfo; - } - @Nullable @CheckReturnValue public Element getMetadata() { @@ -451,7 +445,7 @@ private record PageDimensions(int width, int height) { private record MarginAreaContainer(MarginArea area, TableBox table) { } - private abstract static class MarginArea { + private abstract static sealed class MarginArea { private final MarginBoxName[] _marginBoxNames; public abstract Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet margin); @@ -477,7 +471,7 @@ public MarginDirection getDirection() { } } - private static class TopLeftCorner extends MarginArea { + private static final class TopLeftCorner extends MarginArea { private TopLeftCorner() { super(MarginBoxName.TOP_LEFT_CORNER); } @@ -498,7 +492,7 @@ public Point getPaintingPosition(RenderingContext c, PageBox page, int additiona } - private static class TopRightCorner extends MarginArea { + private static final class TopRightCorner extends MarginArea { private TopRightCorner() { super(MarginBoxName.TOP_RIGHT_CORNER); } @@ -519,7 +513,7 @@ public Point getPaintingPosition(RenderingContext c, PageBox page, int additiona } } - private static class BottomRightCorner extends MarginArea { + private static final class BottomRightCorner extends MarginArea { private BottomRightCorner() { super(MarginBoxName.BOTTOM_RIGHT_CORNER); } @@ -540,7 +534,7 @@ public Point getPaintingPosition(RenderingContext c, PageBox page, int additiona } } - private static class BottomLeftCorner extends MarginArea { + private static final class BottomLeftCorner extends MarginArea { private BottomLeftCorner() { super(MarginBoxName.BOTTOM_LEFT_CORNER); } @@ -560,7 +554,7 @@ public Point getPaintingPosition(RenderingContext c, PageBox page, int additiona } } - private static class LeftMarginArea extends MarginArea { + private static final class LeftMarginArea extends MarginArea { private LeftMarginArea() { super(new MarginBoxName[] { MarginBoxName.LEFT_TOP, @@ -587,7 +581,7 @@ public MarginDirection getDirection() { } } - private static class RightMarginArea extends MarginArea { + private static final class RightMarginArea extends MarginArea { private RightMarginArea() { super(new MarginBoxName[] { MarginBoxName.RIGHT_TOP, @@ -615,7 +609,7 @@ public MarginDirection getDirection() { } } - private static class TopMarginArea extends MarginArea { + private static final class TopMarginArea extends MarginArea { private TopMarginArea() { super(new MarginBoxName[] { MarginBoxName.TOP_LEFT, @@ -639,7 +633,7 @@ public Point getPaintingPosition(RenderingContext c, PageBox page, int additiona } } - private static class BottomMarginArea extends MarginArea { + private static final class BottomMarginArea extends MarginArea { private BottomMarginArea() { super(new MarginBoxName[] { MarginBoxName.BOTTOM_LEFT,