]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3208: Only restart line manager when there is a linebreak for blocklayout
authorSimon Steiner <ssteiner@apache.org>
Tue, 24 Sep 2024 08:15:08 +0000 (09:15 +0100)
committerSimon Steiner <ssteiner@apache.org>
Tue, 24 Sep 2024 08:15:08 +0000 (09:15 +0100)
fop-core/src/main/java/org/apache/fop/apps/FOUserAgent.java
fop-core/src/main/java/org/apache/fop/apps/FopConfParser.java
fop-core/src/main/java/org/apache/fop/apps/FopFactory.java
fop-core/src/main/java/org/apache/fop/apps/FopFactoryBuilder.java
fop-core/src/main/java/org/apache/fop/apps/FopFactoryConfig.java
fop-core/src/main/java/org/apache/fop/layoutmgr/RestartAtLM.java
fop-core/src/main/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java
fop-core/src/test/java/org/apache/fop/apps/MutableConfig.java
fop-core/src/test/java/org/apache/fop/intermediate/TestAssistant.java
fop/test/layoutengine/standard-testcases/flow_changing-ipd_last-page_19_legacy.xml [new file with mode: 0755]
fop/test/layoutengine/standard-testcases/flow_changing-ipd_last-page_20.xml [new file with mode: 0755]

index 12707b30bfd3b78bf48c1642865c150d9f064d6b..149af5c908974c2b23e3d228e815f6eaf41def32 100644 (file)
@@ -851,4 +851,8 @@ public class FOUserAgent {
     public boolean isLegacySkipPagePositionOnly() {
         return factory.isLegacySkipPagePositionOnly();
     }
+
+    public boolean isLegacyLastPageChangeIPD() {
+        return factory.isLegacyLastPageChangeIPD();
+    }
 }
index f36593ed575a7df5343a51d5d289b42ac4a54eea..d3344ff77225d11d834b7ea339715b52d1c1314e 100644 (file)
@@ -60,6 +60,7 @@ public class FopConfParser {
     private static final String SIMPLE_LINE_BREAKING = "simple-line-breaking";
     private static final String SKIP_PAGE_POSITION_ONLY_ALLOWED = "skip-page-position-only-allowed";
     private static final String LEGACY_SKIP_PAGE_POSITION_ONLY = "legacy-skip-page-position-only";
+    private static final String LEGACY_LAST_PAGE_CHANGE_IPD = "legacy-last-page-change-ipd";
 
     private final Log log = LogFactory.getLog(FopConfParser.class);
 
@@ -308,6 +309,14 @@ public class FopConfParser {
                 LogUtil.handleException(log, e, false);
             }
         }
+        if (cfg.getChild(LEGACY_LAST_PAGE_CHANGE_IPD, false) != null) {
+            try {
+                fopFactoryBuilder.setLegacyLastPageChangeIPD(
+                        cfg.getChild(LEGACY_LAST_PAGE_CHANGE_IPD).getValueAsBoolean());
+            } catch (ConfigurationException e) {
+                LogUtil.handleException(log, e, false);
+            }
+        }
 
         // configure font manager
         new FontManagerConfigurator(cfg, baseURI, fopFactoryBuilder.getBaseURI(), resourceResolver)
index 7696c399746132e18c9a7416c16070ffc39f6c13..bdf789bf143ed86c486a27920ff180849acf7785 100644 (file)
@@ -248,6 +248,10 @@ public final class FopFactory implements ImageContext {
         return config.isLegacySkipPagePositionOnly();
     }
 
+    boolean isLegacyLastPageChangeIPD() {
+        return config.isLegacyLastPageChangeIPD();
+    }
+
     /**
      * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
      * instance. Use this factory method if your output type requires an output stream.
index aa90bd544ce8652153311f41ccf0d97d817fee68..ebb176c03a4375ee4586a6b4ce16091fe1c5c0d6 100644 (file)
@@ -360,6 +360,11 @@ public final class FopFactoryBuilder {
         return this;
     }
 
+    public FopFactoryBuilder setLegacyLastPageChangeIPD(boolean b) {
+        fopFactoryConfigBuilder.setLegacyLastPageChangeIPD(b);
+        return this;
+    }
+
     public static class FopFactoryConfigImpl implements FopFactoryConfig {
 
         private final EnvironmentProfile enviro;
@@ -408,6 +413,8 @@ public final class FopFactoryBuilder {
 
         private boolean legacySkipPagePositionOnly;
 
+        private boolean legacyLastPageChangeIPD;
+
         private static final class ImageContextImpl implements ImageContext {
 
             private final FopFactoryConfig config;
@@ -540,6 +547,10 @@ public final class FopFactoryBuilder {
             return legacySkipPagePositionOnly;
         }
 
+        public boolean isLegacyLastPageChangeIPD() {
+            return legacyLastPageChangeIPD;
+        }
+
         public Map<String, String> getHyphenationPatternNames() {
             return hyphPatNames;
         }
@@ -593,6 +604,8 @@ public final class FopFactoryBuilder {
         void setSkipPagePositionOnlyAllowed(boolean b);
 
         void setLegacySkipPagePositionOnly(boolean b);
+
+        void setLegacyLastPageChangeIPD(boolean b);
     }
 
     private static final class CompletedFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
@@ -692,6 +705,10 @@ public final class FopFactoryBuilder {
         public void setLegacySkipPagePositionOnly(boolean b) {
             throwIllegalStateException();
         }
+
+        public void setLegacyLastPageChangeIPD(boolean b) {
+            throwIllegalStateException();
+        }
     }
 
     private static final class ActiveFopFactoryConfigBuilder implements FopFactoryConfigBuilder {
@@ -792,6 +809,10 @@ public final class FopFactoryBuilder {
         public void setLegacySkipPagePositionOnly(boolean b) {
             config.legacySkipPagePositionOnly = b;
         }
+
+        public void setLegacyLastPageChangeIPD(boolean b) {
+            config.legacyLastPageChangeIPD = b;
+        }
     }
 
 }
index a74b0437ceda06c79e8b94608cdbed6bb8f48f81..55dbb8671ee6af5519bfe8c16301d8dc50fbd4c3 100644 (file)
@@ -171,6 +171,8 @@ public interface FopFactoryConfig {
 
     boolean isLegacySkipPagePositionOnly();
 
+    boolean isLegacyLastPageChangeIPD();
+
     /** @return the hyphenation pattern names */
     Map<String, String> getHyphenationPatternNames();
 
index 80b1487a44b09fa624dbf5a12355254f19187507..4a0fcd885581103f5cb87fa9c3d4d72ec7ea5cb0 100644 (file)
@@ -23,25 +23,29 @@ import java.util.Iterator;
 import java.util.LinkedList;
 
 import org.apache.fop.events.EventBroadcaster;
-import org.apache.fop.layoutmgr.table.TableContentPosition;
+import org.apache.fop.layoutmgr.inline.LineLayoutManager;
 
 /**
  * Class to find the restart layoutmanager for changing IPD
  */
 class RestartAtLM {
     protected boolean invalidPosition;
+    private Position lineBreakPosition;
+    private int positionIndex;
 
     protected LayoutManager getRestartAtLM(AbstractBreaker breaker, PageBreakingAlgorithm alg,
                                            boolean ipdChangesOnNextPage, boolean onLastPageAndIPDChanges,
                                            boolean visitedBefore, AbstractBreaker.BlockSequence blockList, int start) {
+        lineBreakPosition = null;
         BreakingAlgorithm.KnuthNode optimalBreak = ipdChangesOnNextPage ? alg.getBestNodeBeforeIPDChange() : alg
                 .getBestNodeForLastPage();
         if (onLastPageAndIPDChanges && visitedBefore && breaker.originalRestartAtLM == null) {
             optimalBreak = null;
         }
-        int positionIndex = findPositionIndex(breaker, optimalBreak, alg, start);
-        if (breaker.positionAtBreak.getLM() instanceof BlockLayoutManager) {
-            positionIndex = findPositionIndex(breaker, optimalBreak, alg, 0);
+        findPositionIndex(breaker, optimalBreak, alg, start);
+        if (!breaker.getPageProvider().foUserAgent.isLegacyLastPageChangeIPD()
+                && breaker.positionAtBreak.getLM() instanceof BlockLayoutManager) {
+            findPositionIndexForBlockLayout(breaker, optimalBreak, alg);
         }
         if (ipdChangesOnNextPage || (breaker.positionAtBreak != null && breaker.positionAtBreak.getIndex() > -1)) {
             breaker.firstElementsForRestart = Collections.EMPTY_LIST;
@@ -116,23 +120,15 @@ class RestartAtLM {
         if (onLastPageAndIPDChanges && !visitedBefore && breaker.positionAtBreak.getPosition() != null) {
             restartAtLM = breaker.positionAtBreak.getPosition().getLM();
         }
-        findLeafPosition(breaker);
-        return restartAtLM;
-    }
-
-    private void findLeafPosition(AbstractBreaker breaker) {
-        while (breaker.positionAtBreak instanceof NonLeafPosition) {
-            Position pos = breaker.positionAtBreak.getPosition();
-            if (pos instanceof TableContentPosition) {
-                break;
-            }
-            breaker.positionAtBreak = pos;
+        if (lineBreakPosition != null && restartAtLM instanceof BlockLayoutManager) {
+            breaker.positionAtBreak = lineBreakPosition;
         }
+        return restartAtLM;
     }
 
-    private int findPositionIndex(AbstractBreaker breaker, BreakingAlgorithm.KnuthNode optimalBreak,
+    private void findPositionIndex(AbstractBreaker breaker, BreakingAlgorithm.KnuthNode optimalBreak,
                                   PageBreakingAlgorithm alg, int start) {
-        int positionIndex = (optimalBreak != null) ? optimalBreak.position : start;
+        positionIndex = (optimalBreak != null) ? optimalBreak.position : start;
         for (int i = positionIndex; i < alg.par.size(); i++) {
             KnuthElement elementAtBreak = alg.getElement(i);
             if (elementAtBreak.getPosition() == null) {
@@ -142,9 +138,40 @@ class RestartAtLM {
             /* Retrieve the original position wrapped into this space position */
             breaker.positionAtBreak = breaker.positionAtBreak.getPosition();
             if (breaker.positionAtBreak != null) {
-                return i;
+                this.positionIndex = i;
+                return;
+            }
+        }
+    }
+
+    private void findPositionIndexForBlockLayout(AbstractBreaker breaker, BreakingAlgorithm.KnuthNode optimalBreak,
+                                  PageBreakingAlgorithm alg) {
+        int positionIndex = (optimalBreak != null) ? optimalBreak.position : 0;
+        for (int i = positionIndex; i < alg.par.size(); i++) {
+            KnuthElement elementAtBreak = alg.getElement(i);
+            if (elementAtBreak.getPosition() == null) {
+                elementAtBreak = alg.getElement(0);
+            }
+            Position positionAtBreak = elementAtBreak.getPosition();
+            /* Retrieve the original position wrapped into this space position */
+            positionAtBreak = positionAtBreak.getPosition();
+            if (positionAtBreak != null) {
+                findLineBreakPosition(positionAtBreak);
+                if (lineBreakPosition != null) {
+                    breaker.positionAtBreak = positionAtBreak;
+                    this.positionIndex = i;
+                }
+                break;
+            }
+        }
+    }
+
+    private void findLineBreakPosition(Position positionAtBreak) {
+        while (positionAtBreak instanceof NonLeafPosition) {
+            positionAtBreak = positionAtBreak.getPosition();
+            if (positionAtBreak instanceof LineLayoutManager.LineBreakPosition) {
+                lineBreakPosition = positionAtBreak;
             }
         }
-        return positionIndex;
     }
 }
index 2fff6c494203f9901e38680d3466d5377176e7c0..25b68842c8f7942296c33c64123154d380d08e78 100644 (file)
@@ -108,7 +108,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager
      * Each value holds the start and end indexes into a List of
      * inline break positions.
      */
-    static class LineBreakPosition extends LeafPosition {
+    public static class LineBreakPosition extends LeafPosition {
         private final int parIndex; // index of the Paragraph this Position refers to
         private final int startIndex; //index of the first element this Position refers to
         private final int availableShrink;
index 17348a2e658e645e6612e5c37b2c41081642f33e..3fcaa4fbcccd69ed687151d7bcf7f8d6390436b1 100644 (file)
@@ -149,6 +149,10 @@ public final class MutableConfig implements FopFactoryConfig {
         return delegate.isLegacySkipPagePositionOnly();
     }
 
+    public boolean isLegacyLastPageChangeIPD() {
+        return delegate.isLegacyLastPageChangeIPD();
+    }
+
     public Map<String, String> getHyphenationPatternNames() {
         return delegate.getHyphenationPatternNames();
     }
index 8b5aad6c226f35261c84f3d1fcba27d723699ab5..f87905772b757932d29016b557264ccd9b68dceb 100644 (file)
@@ -127,6 +127,7 @@ public class TestAssistant {
         builder.setSimpleLineBreaking(isSimpleLineBreaking(testDoc));
         builder.setSkipPagePositionOnlyAllowed(isSkipPagePositionOnlyAllowed(testDoc));
         builder.setLegacySkipPagePositionOnly(isLegacySkipPagePositionOnly(testDoc));
+        builder.setLegacyLastPageChangeIPD(isLegacyLastPageChangeIPD(testDoc));
         return builder.build();
     }
 
@@ -189,6 +190,15 @@ public class TestAssistant {
         }
     }
 
+    private boolean isLegacyLastPageChangeIPD(Document testDoc) {
+        try {
+            String s = eval(testDoc, "/testcase/cfg/legacy-last-page-change-ipd");
+            return "true".equalsIgnoreCase(s);
+        } catch (XPathExpressionException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * Loads a test case into a DOM document.
      * @param testFile the test file
diff --git a/fop/test/layoutengine/standard-testcases/flow_changing-ipd_last-page_19_legacy.xml b/fop/test/layoutengine/standard-testcases/flow_changing-ipd_last-page_19_legacy.xml
new file mode 100755 (executable)
index 0000000..35d8a7e
--- /dev/null
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!-- $Id$ -->
+<testcase>
+  <info>
+    <p>
+      This test checks that the definition of a special page-master for the last page with a
+      different width that the previous "rest" page causes FOP to redo the line breaking layout.
+      Legacy option is enabled so wrong line break is used
+    </p>
+  </info>
+  <cfg>
+    <legacy-last-page-change-ipd>true</legacy-last-page-change-ipd>
+  </cfg>
+  <fo>
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
+  <fo:layout-master-set>
+    <fo:simple-page-master master-name="Portrait" page-width="8.5in" page-height="11in" margin-bottom="0pt" margin-right="0pt" margin-top="0pt" margin-left="0pt">
+      <fo:region-body margin-bottom="0pt" margin-right="0pt" margin-top="0pt" margin-left="0pt" region-name="Body"/>
+    </fo:simple-page-master>
+    <fo:simple-page-master master-name="Landscape" page-width="11in" page-height="8.5in" margin-bottom="0pt" margin-right="0pt" margin-top="0pt" margin-left="0pt">
+      <fo:region-body region-name="Body" margin-bottom="0pt" margin-right="0pt" margin-top="0pt" margin-left="0pt"/>
+    </fo:simple-page-master>
+    <fo:page-sequence-master master-name="LetterPages">
+      <fo:repeatable-page-master-alternatives>
+        <fo:conditional-page-master-reference page-position="first" master-reference="Portrait"/>
+        <fo:conditional-page-master-reference page-position="rest" master-reference="Portrait"/>
+        <fo:conditional-page-master-reference page-position="last" master-reference="Landscape"/>
+      </fo:repeatable-page-master-alternatives>
+    </fo:page-sequence-master>
+  </fo:layout-master-set>
+  <fo:page-sequence master-reference="LetterPages">
+    <fo:flow flow-name="Body">
+      <fo:block>
+          <fo:block>test</fo:block>
+          <fo:block break-before="page">test2</fo:block>
+          <fo:block>test3</fo:block>
+      </fo:block>
+    </fo:flow>
+  </fo:page-sequence>
+</fo:root>
+  </fo>
+  <checks>
+    <eval expected="2" xpath="count(//pageViewport)"/>
+    <eval expected="test" xpath="//pageViewport[1]//word"/>
+    <eval expected="test3" xpath="//pageViewport[2]//block/block[1]//word"/>
+  </checks>
+</testcase>
diff --git a/fop/test/layoutengine/standard-testcases/flow_changing-ipd_last-page_20.xml b/fop/test/layoutengine/standard-testcases/flow_changing-ipd_last-page_20.xml
new file mode 100755 (executable)
index 0000000..d28c56d
--- /dev/null
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!-- $Id$ -->
+<testcase>
+  <info>
+    <p>
+      This test checks that the definition of a special page-master for the last page with a
+      different width that the previous "rest" page causes FOP to redo the line breaking layout.
+    </p>
+  </info>
+  <fo>
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">
+  <fo:layout-master-set>
+    <fo:simple-page-master master-name="Page" page-width="8.5in" page-height="11in">
+      <fo:region-body region-name="Body"/>
+    </fo:simple-page-master>
+    <fo:simple-page-master master-name="PageRest" page-width="8.5in" page-height="11in">
+      <fo:region-body region-name="Body"/>
+    </fo:simple-page-master>
+    <fo:simple-page-master margin-right="0.48in" master-name="PageLast" page-width="8.5in" page-height="11in">
+      <fo:region-body region-name="Body"/>
+    </fo:simple-page-master>
+    <fo:page-sequence-master master-name="PageSequence">
+      <fo:repeatable-page-master-alternatives>
+        <fo:conditional-page-master-reference page-position="first" master-reference="Page"/>
+        <fo:conditional-page-master-reference page-position="rest" master-reference="PageRest"/>
+        <fo:conditional-page-master-reference page-position="last" master-reference="PageLast"/>
+      </fo:repeatable-page-master-alternatives>
+    </fo:page-sequence-master>
+  </fo:layout-master-set>
+  <fo:page-sequence master-reference="PageSequence">
+    <fo:flow flow-name="Body">
+        <fo:block>
+          <fo:leader/>
+          <fo:block break-before="page"/>NOTE</fo:block>
+    </fo:flow>
+  </fo:page-sequence>
+</fo:root>
+  </fo>
+  <checks>
+    <eval expected="2" xpath="count(//pageViewport)"/>
+    <eval expected="NOTE" xpath="//pageViewport[2]//word"/>
+  </checks>
+</testcase>