]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Bugzilla 48380: Avoid ClassCastException when using fox:widow-content-limit
authorAndreas L. Delmelle <adelmelle@apache.org>
Fri, 7 Jan 2011 21:14:33 +0000 (21:14 +0000)
committerAndreas L. Delmelle <adelmelle@apache.org>
Fri, 7 Jan 2011 21:14:33 +0000 (21:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1056513 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/layoutmgr/ElementListUtils.java
status.xml
test/layoutengine/standard-testcases/fox_widow-content-limit_bug48380.xml [new file with mode: 0644]

index 6a843ac63994f3de3b859483aac897cdeb406a1a..2bd6f429a1ef7996436b3d497987c4e8267dc3c6 100644 (file)
@@ -55,43 +55,7 @@ public final class ElementListUtils {
      * @return true if the constraint is bigger than the list contents
      */
     public static boolean removeLegalBreaks(List elements, int constraint) {
-        int len = 0;
-        ListIterator iter = elements.listIterator();
-        while (iter.hasNext()) {
-            ListElement el = (ListElement)iter.next();
-            if (el.isPenalty()) {
-                KnuthPenalty penalty = (KnuthPenalty)el;
-                //Convert all penalties to break inhibitors
-                if (penalty.getPenalty() < KnuthPenalty.INFINITE) {
-                    iter.set(new KnuthPenalty(penalty.getWidth(), KnuthPenalty.INFINITE,
-                            penalty.isPenaltyFlagged(), penalty.getPosition(),
-                            penalty.isAuxiliary()));
-                }
-            } else if (el.isGlue()) {
-                KnuthGlue glue = (KnuthGlue)el;
-                len += glue.getWidth();
-                iter.previous();
-                el = (ListElement)iter.previous();
-                iter.next();
-                if (el.isBox()) {
-                    iter.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
-                            null, false));
-                }
-                iter.next();
-            } else if (el instanceof BreakElement) {
-                BreakElement breakEl = (BreakElement)el;
-                if (breakEl.getPenaltyValue() < KnuthPenalty.INFINITE) {
-                    breakEl.setPenaltyValue(KnuthPenalty.INFINITE);
-                }
-            } else {
-                KnuthElement kel = (KnuthElement)el;
-                len += kel.getWidth();
-            }
-            if (len >= constraint) {
-                return false;
-            }
-        }
-        return true;
+        return removeLegalBreaks(elements, constraint, false);
     }
 
     /**
@@ -103,27 +67,48 @@ public final class ElementListUtils {
      * @return true if the constraint is bigger than the list contents
      */
     public static boolean removeLegalBreaksFromEnd(List elements, int constraint) {
+        return removeLegalBreaks(elements, constraint, true);
+    }
+
+    private static boolean removeLegalBreaks(List elements, int constraint, boolean fromEnd) {
+
         int len = 0;
-        ListIterator i = elements.listIterator(elements.size());
-        while (i.hasPrevious()) {
-            ListElement el = (ListElement)i.previous();
+        ListElement el;
+
+        for (ListIterator iter = elements.listIterator(fromEnd ? elements.size() : 0);
+                (fromEnd ? iter.hasPrevious() : iter.hasNext());) {
+
+            if (fromEnd) {
+                el = (ListElement) iter.previous();
+            } else {
+                el = (ListElement) iter.next();
+            }
+
             if (el.isPenalty()) {
                 KnuthPenalty penalty = (KnuthPenalty)el;
-                //Convert all penalties to break inhibitors
+                //Convert penalty to break inhibitor
                 if (penalty.getPenalty() < KnuthPenalty.INFINITE) {
-                    i.set(new KnuthPenalty(penalty.getWidth(), KnuthPenalty.INFINITE,
+                    iter.set(new KnuthPenalty(penalty.getWidth(), KnuthPenalty.INFINITE,
                             penalty.isPenaltyFlagged(), penalty.getPosition(),
                             penalty.isAuxiliary()));
                 }
             } else if (el.isGlue()) {
                 KnuthGlue glue = (KnuthGlue)el;
                 len += glue.getWidth();
-                el = (ListElement)i.previous();
-                i.next();
+                //check if previous is a box
+                if (!fromEnd) {
+                    iter.previous();
+                }
+                el = (ListElement)iter.previous();
+                iter.next();
                 if (el.isBox()) {
-                    i.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
+                    //add break inhibitor
+                    iter.add(new KnuthPenalty(0, KnuthPenalty.INFINITE, false,
                             null, false));
                 }
+                if (!fromEnd) {
+                    iter.next();
+                }
             } else if (el.isUnresolvedElement()) {
                 if (el instanceof BreakElement) {
                     BreakElement breakEl = (BreakElement)el;
@@ -138,10 +123,12 @@ public final class ElementListUtils {
                 KnuthElement kel = (KnuthElement)el;
                 len += kel.getWidth();
             }
+
             if (len >= constraint) {
                 return false;
             }
         }
+
         return true;
     }
 
@@ -191,8 +178,7 @@ public final class ElementListUtils {
      * @return true if the list ends with a forced break
      */
     public static boolean endsWithForcedBreak(List elems) {
-        ListElement last = (ListElement) ListUtil.getLast(elems);
-        return last.isForcedBreak();
+        return ((ListElement) ListUtil.getLast(elems)).isForcedBreak();
     }
 
     /**
index 2f2608cfd1bbebfefccce15ec360b6762d6b8cdc..16ef4dfe6f36b306367b3541c99d0fce6e99626c 100644 (file)
@@ -58,6 +58,9 @@
       documents. Example: the fix of marks layering will be such a case when it's done.
     -->
     <release version="FOP Trunk" date="TBD">
+      <action context="Layout" dev="AD" type="fix" fixes-bug="48380">
+        Bugfix: avoid ClassCastException when using fox:widow-content-limit
+      </action>
       <action context="Layout" dev="VH" type="fix" fixes-bug="50089">
         Bugfix: content after forced break in block-container is not rendered.
       </action>
diff --git a/test/layoutengine/standard-testcases/fox_widow-content-limit_bug48380.xml b/test/layoutengine/standard-testcases/fox_widow-content-limit_bug48380.xml
new file mode 100644 (file)
index 0000000..7dab0b3
--- /dev/null
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>\r
+<!--\r
+  Licensed to the Apache Software Foundation (ASF) under one or more\r
+  contributor license agreements.  See the NOTICE file distributed with\r
+  this work for additional information regarding copyright ownership.\r
+  The ASF licenses this file to You under the Apache License, Version 2.0\r
+  (the "License"); you may not use this file except in compliance with\r
+  the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+  Unless required by applicable law or agreed to in writing, software\r
+  distributed under the License is distributed on an "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+  See the License for the specific language governing permissions and\r
+  limitations under the License.\r
+-->\r
+<!-- $Id$ -->\r
+<testcase>\r
+  <info>\r
+    <p>\r
+      Check for bug 48380: ClassCastException when using fox:widow-content-limit.\r
+    </p>\r
+  </info>\r
+  <fo>\r
+    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">\r
+      <fo:layout-master-set>\r
+        <fo:simple-page-master master-name="normal" page-width="210mm" page-height="297mm">\r
+          <fo:region-body/>\r
+        </fo:simple-page-master>\r
+      </fo:layout-master-set>\r
+      <fo:page-sequence master-reference="normal" white-space-collapse="true">\r
+        <fo:flow flow-name="xsl-region-body">\r
+          <fo:table fox:orphan-content-limit="4em" fox:widow-content-limit="4em" table-layout="fixed" width="100%">\r
+            <fo:table-body>\r
+              <fo:table-row>\r
+                <fo:table-cell>\r
+                  <fo:block>\r
+                    <fo:list-block space-after="6pt">\r
+                      <fo:list-item>\r
+                        <fo:list-item-label>\r
+                          <fo:block>.</fo:block>\r
+                        </fo:list-item-label>\r
+                        <fo:list-item-body>\r
+                          <fo:block start-indent="1cm">test</fo:block>\r
+                        </fo:list-item-body>\r
+                      </fo:list-item>\r
+                    </fo:list-block>\r
+                  </fo:block>\r
+                </fo:table-cell>\r
+              </fo:table-row>\r
+            </fo:table-body>\r
+          </fo:table>\r
+        </fo:flow>\r
+      </fo:page-sequence>\r
+    </fo:root>\r
+  </fo>\r
+  <checks>\r
+    <!-- Run only basic checks; should not throw a ClassCastException -->\r
+  </checks>\r
+</testcase>
\ No newline at end of file