From 0dd5fd9c0515b47a6728e934a4e46ce54fbd2a77 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Thu, 11 Feb 2021 18:03:49 +0100 Subject: [PATCH] Skip page breaking on inner elements and inner sections if BREAK_BEFORE already processed on outer section --- .../w2phtml/xhtml/content/TextParser.java | 76 ++++++++++++------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/src/main/java/w2phtml/xhtml/content/TextParser.java b/src/main/java/w2phtml/xhtml/content/TextParser.java index 6a03408..d8a6d34 100644 --- a/src/main/java/w2phtml/xhtml/content/TextParser.java +++ b/src/main/java/w2phtml/xhtml/content/TextParser.java @@ -45,7 +45,6 @@ import w2phtml.office.XMLString; import w2phtml.pageSplitters.SplitFactory; import w2phtml.util.Misc; import w2phtml.xhtml.Converter; -import w2phtml.xhtml.ODFPageSplitter; import w2phtml.xhtml.Parser; import w2phtml.xhtml.StyleInfo; import w2phtml.xhtml.XhtmlConfig; @@ -122,6 +121,7 @@ public class TextParser extends Parser { private String footnotesContext = null; PageContainer pageContainer = null; private boolean applyAnnotationMetadata; + private boolean breakBeforeSectionHappened = false; public TextParser(OfficeReader ofr, XhtmlConfig config, Converter converter) { super(ofr,config,converter); @@ -168,9 +168,10 @@ public class TextParser extends Parser { if (pagination) { //ODFPageSplitter.splitOfficeText(onode, ofr); + //Debug.prettyPrintXml(onode); SplitFactory splitters = new SplitFactory(ofr); splitters.split(onode); - //Debug.printNode(onode); + //Debug.prettyPrintXml(onode); } hnode = (Element)traverseBlockText(onode,hnode); @@ -266,8 +267,6 @@ public class TextParser extends Parser { Node child = nList.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { String nodeName = child.getNodeName(); - // Block splitting - // System.out.println("CURNODE " + nodeName); if (OfficeReader.isDrawElement(child)) { getDrawParser().handleDrawElement((Element)child,(Element)hnode,null,nFloatMode); } @@ -1183,36 +1182,32 @@ public class TextParser extends Parser { if (inUnreakableElement() ) { return hnode; } - + Integer newPageNumber = null; newPageNumber = getPageNumber(style, newPageNumber); if (currentMasterPage == null && style != null) { hnode = startDocument(hnode, style, newPageNumber); hnode = docSep.processOutlineLevel(currentNode, hnode, pageNum); } else if (hasMasterPage(style) || hasBreakBefore(style) || breakBeforeNextNode) { - // Insert footnotes - insertFootnotes(hnode,false); + if (hasBreakBefore(style)) { + + if (currentNode.getNodeName() == TEXT_SECTION) { + if (breakBeforeSectionHappened) { + //skip one more section; + return hnode; + } else { + breakBeforeSectionHappened = true; + } + } else { + //assume in inner element, break happened, skip break. + if (breakBeforeSectionHappened) { + breakBeforeSectionHappened = false; + return hnode; + } + } + } + hnode = breakPage(currentNode, hnode, style, newPageNumber); - // Add previous MP footer - //hnode = exitPageContainer((Element) hnode); - addFooter(hnode); - // Update MP - updateMasterPageWith(style); - setPageContainerStyle(); - // Set new page number if defined or increment if not - if (newPageNumber != null) { - pageNum = newPageNumber; - } else { - pageNum++; - fitPageNumberToMasterPageStyle(); - } - hnode = docSep.breaksOrOutline(currentNode, hnode, pageNum); - - - // Print new header - addHeader(hnode); - //hnode = enterPageContainer(hnode); - breakBeforeNextNode = false; return hnode; } else { @@ -1228,6 +1223,33 @@ public class TextParser extends Parser { return hnode; } + private Node breakPage(Node currentNode, Node hnode, StyleWithProperties style, Integer newPageNumber) { + // Insert footnotes + insertFootnotes(hnode,false); + + // Add previous MP footer + //hnode = exitPageContainer((Element) hnode); + addFooter(hnode); + // Update MP + updateMasterPageWith(style); + setPageContainerStyle(); + // Set new page number if defined or increment if not + if (newPageNumber != null) { + pageNum = newPageNumber; + } else { + pageNum++; + fitPageNumberToMasterPageStyle(); + } + hnode = docSep.breaksOrOutline(currentNode, hnode, pageNum); + + + // Print new header + addHeader(hnode); + //hnode = enterPageContainer(hnode); + breakBeforeNextNode = false; + return hnode; + } + private boolean inUnreakableElement() { return inHeader || inFooter || inTable || inList || inFootnote || inEndnote; }