Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement -fs-page-break-min-height #31

Merged
merged 2 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public final class CSSName implements Comparable {
NOT_INHERITED,
new PrimitivePropertyBuilders.FSCheckboxStyle()
);

/**
* Unique CSSName instance for CSS2 property.
*/
Expand Down Expand Up @@ -405,6 +405,19 @@ public final class CSSName implements Comparable {
new PrimitivePropertyBuilders.FSNamedDestination()
);

/**
* Perform a page break before this element, if not at least the specified space is
* left on the current page.
*/
public final static CSSName FS_PAGE_BREAK_MIN_HEIGHT =
addProperty(
"-fs-page-break-min-height",
PRIMITIVE,
"0",
INHERITS,
new PrimitivePropertyBuilders.FSPageBreakMinHeight()
);

/**
* Unique CSSName instance for CSS2 property.
*/
Expand Down Expand Up @@ -1316,7 +1329,7 @@ public final class CSSName implements Comparable {
true,
new PrimitivePropertyBuilders.BorderBottomLeftRadius()
);

/**
* Unique CSSName instance for CSS2 property.
*/
Expand Down Expand Up @@ -1418,7 +1431,7 @@ public final class CSSName implements Comparable {
NOT_INHERITED,
new BackgroundPropertyBuilder()
);


/**
* Unique CSSName instance for CSS3 property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,9 @@ public static class MinHeight extends NonNegativeLengthLike {
public static class MinWidth extends NonNegativeLengthLike {
}

public static class FSPageBreakMinHeight extends NonNegativeLengthLike {
}

public static class Orphans extends PlainInteger {
protected boolean isNegativeValuesAllowed() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,10 @@ public int getColSpan() {
return result > 0 ? result : 1;
}

public float getFSPageBreakMinHeight(CssContext c){
return getFloatPropertyProportionalTo(CSSName.FS_PAGE_BREAK_MIN_HEIGHT, 0, c);
}

public Length asLength(CssContext c, CSSName cssName) {
Length result = new Length();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ private static void repositionBox(LayoutContext c, BlockBox child, int trimmedPa
}
if (c.isPrint()) {
boolean pageClear = child.isNeedPageClear() ||
child.getStyle().isForcePageBreakBefore();
child.getStyle().isForcePageBreakBefore() ||
child.isPageBreakNeededBecauseOfMinHeight(c);
boolean needNewPageContext = child.checkPageContext(c);

if (needNewPageContext && trimmedPageCount != NO_PAGE_TRIM) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static FloatLayoutResult layoutFloated(
private static void positionFloatOnPage(
final LayoutContext c, LineBox currentLine, BlockBox block,
boolean movedVertically) {
if (block.getStyle().isForcePageBreakBefore()) {
if (block.getStyle().isForcePageBreakBefore() || block.isPageBreakNeededBecauseOfMinHeight(c)) {
block.forcePageBreakBefore(c, block.getStyle().getIdent(CSSName.PAGE_BREAK_BEFORE), false);
block.calcCanvasLocation();
resetAndFloatBlock(c, currentLine, block);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,27 @@ public void positionAbsolute(CssContext cssCtx, int direction) {
calcChildLocations();
}

/**
* Using the css:
*
* -fs-page-break-min-height: 5cm;
*
* on a block element you can force a pagebreak before this block, if not
* enough space (e.g. 5cm in this case) is remaining on the current page for the block.
*
* @return true if a pagebreak is needed before this block because
* there is not enough space left on the current page.
*/
public boolean isPageBreakNeededBecauseOfMinHeight(LayoutContext context){
float minHeight = getStyle().getFSPageBreakMinHeight(context);
PageBox page = context.getRootLayer().getFirstPage(context, this);
return page != null && getAbsY() + minHeight > page.getBottom();
}


public void positionAbsoluteOnPage(LayoutContext c) {
if (c.isPrint() &&
(getStyle().isForcePageBreakBefore() || isNeedPageClear())) {
(getStyle().isForcePageBreakBefore() || isNeedPageClear() || isPageBreakNeededBecauseOfMinHeight(c))) {
forcePageBreakBefore(c, getStyle().getIdent(CSSName.PAGE_BREAK_BEFORE), false);
calcCanvasLocation();
calcChildLocations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static void main(String[] args) throws Exception {
*/
runTestCase("RepeatedTableSample");

/*
* This sample demonstrates the -fs-pagebreak-min-height css property
*/
runTestCase("FSPageBreakMinHeightSample");

/* Add additional test cases here. */
}

Expand All @@ -37,7 +42,8 @@ public static void runTestCase(String testCaseFile) throws Exception {
.getResourceAsStream("/testcases/" + testCaseFile + ".html"));
String html = new String(htmlBytes, Charsets.UTF_8);
String outDir = System.getProperty("OUT_DIRECTORY", ".");
FileOutputStream outputStream = new FileOutputStream(outDir + "/" + testCaseFile + ".pdf");
String testCaseOutputFile = outDir + "/" + testCaseFile + ".pdf";
FileOutputStream outputStream = new FileOutputStream(testCaseOutputFile);

try {
PdfRendererBuilder builder = new PdfRendererBuilder();
Expand All @@ -50,5 +56,6 @@ public static void runTestCase(String testCaseFile) throws Exception {
} finally {
outputStream.close();
}
System.out.println("Wrote " + testCaseOutputFile);
}
}
Loading