Skip to content

Commit

Permalink
[refactor] convert constants PAGED_MODE_SCREEN/PAGED_MODE_PRINT to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 11, 2024
1 parent 27d8f78 commit bbc464f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@
* completed layout).
*/
public final class Layer {
public static final short PAGED_MODE_SCREEN = 1;
public static final short PAGED_MODE_PRINT = 2;
public enum PagedMode {
PAGED_MODE_SCREEN,
PAGED_MODE_PRINT
}

@Nullable
private final Layer _parent;
Expand Down Expand Up @@ -902,22 +904,19 @@ public void trimPageCount(int newPageCount) {
}
}

public void assignPagePaintingPositions(CssContext cssCtx, short mode) {
public void assignPagePaintingPositions(CssContext cssCtx, PagedMode mode) {
assignPagePaintingPositions(cssCtx, mode, 0);
}

public void assignPagePaintingPositions(
CssContext cssCtx, int mode, int additionalClearance) {
CssContext cssCtx, PagedMode mode, int additionalClearance) {
List<PageBox> pages = getPages();
int paintingTop = additionalClearance;
for (PageBox page : pages) {
page.setPaintingTop(paintingTop);
if (mode == PAGED_MODE_SCREEN) {
page.setPaintingBottom(paintingTop + page.getHeight(cssCtx));
} else if (mode == PAGED_MODE_PRINT) {
page.setPaintingBottom(paintingTop + page.getContentHeight(cssCtx));
} else {
throw new IllegalArgumentException("Illegal mode");
switch (mode) {
case PAGED_MODE_SCREEN -> page.setPaintingBottom(paintingTop + page.getHeight(cssCtx));
case PAGED_MODE_PRINT -> page.setPaintingBottom(paintingTop + page.getContentHeight(cssCtx));
}
paintingTop = page.getPaintingBottom() + additionalClearance;
}
Expand Down
160 changes: 59 additions & 101 deletions flying-saucer-core/src/main/java/org/xhtmlrenderer/render/PageBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.xhtmlrenderer.css.style.derived.LengthValue;
import org.xhtmlrenderer.css.style.derived.RectPropertySet;
import org.xhtmlrenderer.layout.BoxBuilder;
import org.xhtmlrenderer.layout.Layer;
import org.xhtmlrenderer.layout.Layer.PagedMode;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.newtable.TableBox;

Expand All @@ -52,7 +52,6 @@
import static org.xhtmlrenderer.css.style.CalculatedStyle.LEFT;
import static org.xhtmlrenderer.css.style.CalculatedStyle.RIGHT;
import static org.xhtmlrenderer.css.style.CalculatedStyle.TOP;
import static org.xhtmlrenderer.layout.Layer.PAGED_MODE_SCREEN;

public class PageBox {
private static final MarginArea[] MARGIN_AREA_DEFS = {
Expand Down Expand Up @@ -86,11 +85,13 @@ public class PageBox {
@Nullable
private PageDimensions _pageDimensions;

@Nullable
private PageInfo _pageInfo;

@Nullable
private final MarginAreaContainer[] _marginAreas = new MarginAreaContainer[MARGIN_AREA_DEFS.length];

@Nullable
private Element _metadata;

public int getWidth(CssContext cssCtx) {
Expand Down Expand Up @@ -282,23 +283,26 @@ private Rectangle getBorderEdge(int left, int top, CssContext cssCtx) {
getHeight(cssCtx) - (int) margin.top() - (int) margin.bottom());
}

public void paintBorder(RenderingContext c, int additionalClearance, short mode) {
int top = (mode == PAGED_MODE_SCREEN) ? getPaintingTop() : 0;
public void paintBorder(RenderingContext c, int additionalClearance, PagedMode mode) {
int top = switch (mode) {
case PAGED_MODE_SCREEN -> getPaintingTop();
case PAGED_MODE_PRINT -> 0;
};
c.getOutputDevice().paintBorder(c,
getStyle(),
getBorderEdge(additionalClearance, top, c),
BorderPainter.ALL);
}

public void paintBackground(RenderingContext c, int additionalClearance, short mode) {
Rectangle bounds = (mode == PAGED_MODE_SCREEN) ?
getScreenPaintingBounds(c, additionalClearance) :
getPrintPaintingBounds(c);

public void paintBackground(RenderingContext c, int additionalClearance, PagedMode mode) {
Rectangle bounds = switch (mode) {
case PAGED_MODE_SCREEN -> getScreenPaintingBounds(c, additionalClearance);
case PAGED_MODE_PRINT -> getPrintPaintingBounds(c);
};
c.getOutputDevice().paintBackground(c, getStyle(), bounds, bounds, getStyle().getBorder(c));
}

public void paintMarginAreas(RenderingContext c, int additionalClearance, short mode) {
public void paintMarginAreas(RenderingContext c, int additionalClearance, PagedMode mode) {
for (int i = 0; i < MARGIN_AREA_DEFS.length; i++) {
MarginAreaContainer container = _marginAreas[i];
if (container != null) {
Expand Down Expand Up @@ -341,6 +345,8 @@ public void setPageInfo(PageInfo pageInfo) {
_pageInfo = pageInfo;
}

@Nullable
@CheckReturnValue
public Element getMetadata() {
return _metadata;
}
Expand Down Expand Up @@ -445,8 +451,10 @@ private abstract static class MarginArea {
private final MarginBoxName[] _marginBoxNames;

public abstract Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet margin);

@CheckReturnValue
public abstract Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode);
RenderingContext c, PageBox page, int additionalClearance, PagedMode mode);

private MarginArea(MarginBoxName marginBoxName) {
_marginBoxNames = new MarginBoxName[] { marginBoxName };
Expand Down Expand Up @@ -476,17 +484,11 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
int top;
if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingTop();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = 0;
} else {
throw new IllegalArgumentException("Illegal mode");
}

public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingTop();
case PAGED_MODE_PRINT -> 0;
};
return new Point(additionalClearance, top);
}

Expand All @@ -503,18 +505,12 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int left = additionalClearance + page.getWidth(c) - (int)page.getMargin(c).right();
int top;
if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingTop();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = 0;
} else {
throw new IllegalArgumentException("Illegal mode");
}

int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingTop();
case PAGED_MODE_PRINT -> 0;
};
return new Point(left, top);
}
}
Expand All @@ -530,19 +526,12 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int left = additionalClearance + page.getWidth(c) - (int)page.getMargin(c).right();
int top;

if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingBottom() - (int)page.getMargin(c).bottom();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = page.getHeight(c) - (int)page.getMargin(c).bottom();
} else {
throw new IllegalArgumentException("Illegal mode");
}

int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingBottom() - (int)page.getMargin(c).bottom();
case PAGED_MODE_PRINT -> page.getHeight(c) - (int)page.getMargin(c).bottom();
};
return new Point(left, top);
}
}
Expand All @@ -558,18 +547,11 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {

int top;
if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingBottom() - (int)page.getMargin(c).bottom();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = page.getHeight(c) - (int)page.getMargin(c).bottom();
} else {
throw new IllegalArgumentException("Illegal mode");
}

public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingBottom() - (int)page.getMargin(c).bottom();
case PAGED_MODE_PRINT -> page.getHeight(c) - (int)page.getMargin(c).bottom();
};
return new Point(additionalClearance, top);
}
}
Expand All @@ -588,17 +570,11 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
int top;
if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingTop() + (int)page.getMargin(c).top();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = (int)page.getMargin(c).top();
} else {
throw new IllegalArgumentException("Illegal mode");
}

public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingTop() + (int)page.getMargin(c).top();
case PAGED_MODE_PRINT -> (int) page.getMargin(c).top();
};
return new Point(additionalClearance, top);
}

Expand All @@ -621,18 +597,12 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int left = additionalClearance + page.getWidth(c) - (int)page.getMargin(c).right();
int top;
if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingTop() + (int)page.getMargin(c).top();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = (int)page.getMargin(c).top();
} else {
throw new IllegalArgumentException("Illegal mode");
}

int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingTop() + (int)page.getMargin(c).top();
case PAGED_MODE_PRINT -> (int) page.getMargin(c).top();
};
return new Point(left, top);
}

Expand All @@ -655,18 +625,12 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet
}

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
public Point getPaintingPosition(RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int left = additionalClearance + (int)page.getMargin(c).left();
int top;
if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingTop();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = 0;
} else {
throw new IllegalArgumentException("Illegal mode");
}

int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingTop();
case PAGED_MODE_PRINT -> 0;
};
return new Point(left, top);
}
}
Expand All @@ -686,18 +650,12 @@ public Dimension getLayoutDimension(CssContext c, PageBox page, RectPropertySet

@Override
public Point getPaintingPosition(
RenderingContext c, PageBox page, int additionalClearance, short mode) {
RenderingContext c, PageBox page, int additionalClearance, PagedMode mode) {
int left = additionalClearance + (int)page.getMargin(c).left();
int top;

if (mode == PAGED_MODE_SCREEN) {
top = page.getPaintingBottom() - (int)page.getMargin(c).bottom();
} else if (mode == Layer.PAGED_MODE_PRINT) {
top = page.getHeight(c) - (int)page.getMargin(c).bottom();
} else {
throw new IllegalArgumentException("Illegal mode");
}

int top = switch (mode) {
case PAGED_MODE_SCREEN -> page.getPaintingBottom() - (int)page.getMargin(c).bottom();
case PAGED_MODE_PRINT -> page.getHeight(c) - (int)page.getMargin(c).bottom();
};
return new Point(left, top);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.List;
import java.util.logging.Level;

import static org.xhtmlrenderer.layout.Layer.PagedMode.PAGED_MODE_PRINT;
import static org.xhtmlrenderer.layout.Layer.PagedMode.PAGED_MODE_SCREEN;
import static org.xhtmlrenderer.util.ImageUtil.withGraphics;

/**
Expand Down Expand Up @@ -176,7 +178,7 @@ private void paintPagedView(RenderingContext c, Layer root) {
calcCenteredPageLeftOffset(root.getMaxPageWidth(c, 0)) :
PAGE_PAINTING_CLEARANCE_WIDTH;
root.assignPagePaintingPositions(
c, Layer.PAGED_MODE_SCREEN, PAGE_PAINTING_CLEARANCE_HEIGHT);
c, PAGED_MODE_SCREEN, PAGE_PAINTING_CLEARANCE_HEIGHT);

setPreferredSize(new Dimension(
root.getMaxPageWidth(c, pagePaintingClearanceWidth),
Expand Down Expand Up @@ -204,9 +206,9 @@ private void paintPagedView(RenderingContext c, Layer root) {
bounds.width += 1;
bounds.height += 1;
if (working.intersects(bounds)) {
page.paintBackground(c, pagePaintingClearanceWidth, Layer.PAGED_MODE_SCREEN);
page.paintMarginAreas(c, pagePaintingClearanceWidth, Layer.PAGED_MODE_SCREEN);
page.paintBorder(c, pagePaintingClearanceWidth, Layer.PAGED_MODE_SCREEN);
page.paintBackground(c, pagePaintingClearanceWidth, PAGED_MODE_SCREEN);
page.paintMarginAreas(c, pagePaintingClearanceWidth, PAGED_MODE_SCREEN);
page.paintBorder(c, pagePaintingClearanceWidth, PAGED_MODE_SCREEN);

Color old = g.getColor();

Expand Down Expand Up @@ -256,9 +258,9 @@ public void paintPage(Graphics2D g, int pageNo) {
c.setPageCount(root.getPages().size());
c.setPage(pageNo, page);

page.paintBackground(c, 0, Layer.PAGED_MODE_PRINT);
page.paintMarginAreas(c, 0, Layer.PAGED_MODE_PRINT);
page.paintBorder(c, 0, Layer.PAGED_MODE_PRINT);
page.paintBackground(c, 0, PAGED_MODE_PRINT);
page.paintMarginAreas(c, 0, PAGED_MODE_PRINT);
page.paintBorder(c, 0, PAGED_MODE_PRINT);

Shape working = g.getClip();

Expand All @@ -279,7 +281,7 @@ public void paintPage(Graphics2D g, int pageNo) {

public void assignPagePrintPositions(Graphics2D g) {
RenderingContext c = newRenderingContext(g);
getRootLayer().assignPagePaintingPositions(c, Layer.PAGED_MODE_PRINT);
getRootLayer().assignPagePaintingPositions(c, PAGED_MODE_PRINT);
}

public void printTree() {
Expand Down
Loading

0 comments on commit bbc464f

Please sign in to comment.