]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
RTF: support for nested tables
authorPeter Herweg <pherweg@apache.org>
Sun, 15 Jan 2006 17:37:58 +0000 (17:37 +0000)
committerPeter Herweg <pherweg@apache.org>
Sun, 15 Jan 2006 17:37:58 +0000 (17:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@369237 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTable.java
src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableCell.java
src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTableRow.java

index e376ff5a34526e93a8771df30b7f28654eb66c76..65ac2eb49375ecaa77d25815d388653584ff6b80 100644 (file)
@@ -36,6 +36,7 @@ import java.io.IOException;
 public class RtfTable extends RtfContainer {
     private RtfTableRow row;
     private int highestRow = 0;
+    private Boolean isNestedTable = null;
 
     /** Added by Boris Poudérous on 07/22/2002 in order to process
      *  number-columns-spanned attribute */
@@ -104,15 +105,23 @@ public class RtfTable extends RtfContainer {
      * @throws IOException for I/O problems
      */
     protected void writeRtfPrefix() throws IOException {
-        writeGroupMark(true);
+       if (isNestedTable()) {
+               writeControlWordNS("pard");
+       }
+       
+        writeGroupMark(true);   
     }
-
+    
     /**
      * Overridden to write RTF suffix code, what comes after our children
      * @throws IOException for I/O problems
      */
     protected void writeRtfSuffix() throws IOException {
         writeGroupMark(false);
+        
+        if(isNestedTable()) {
+               getRow().writeRowAndCellsDefintions();
+        }
     }
 
     /**
@@ -159,5 +168,37 @@ public class RtfTable extends RtfContainer {
 
         return super.getRtfAttributes();
     }
-    /** - end - */
+    
+    public boolean isNestedTable() {
+       if (isNestedTable == null) {
+               RtfElement e=this;
+               while(e.parent != null) {
+                       if (e.parent instanceof RtfTableCell) {
+                               isNestedTable=new Boolean(true);
+                               return true;
+                       }
+                       
+                       e = e.parent;
+               }
+               
+               isNestedTable=new Boolean(false);
+       } else {
+               return isNestedTable.booleanValue();
+       }
+       
+       return false;
+    }
+    
+    public RtfTableRow getRow() {
+       RtfElement e=this;
+       while(e.parent != null) {
+               if (e.parent instanceof RtfTableRow) {
+                       return (RtfTableRow) e.parent;
+               }
+               
+               e = e.parent;
+       }
+       
+       return null;  
+    }
 }
index bf763133fab0daa1a234ffb5475b4542268a1faa..49fe9fd092c68e43fb3ccd38c7235a553267b4c3 100644 (file)
@@ -141,6 +141,7 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
         if (attrs == null) {
             attrs = new RtfAttributes();
         }
+        
         attrs.set("intbl");
 
         paragraph = new RtfParagraph(this, writer, attrs);
@@ -280,30 +281,39 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
      * @throws IOException for I/O problems
      */
     protected void writeRtfSuffix() throws IOException {
-        // word97 hangs if cell does not contain at least one "par" control word
-        // TODO this is what causes the extra spaces in nested table of test
-        //      004-spacing-in-tables.fo,
-        // but if is not here we generate invalid RTF for word97
-
-        if (setCenter) {
-            writeControlWord("qc");
-        } else if (setRight) {
-            writeControlWord("qr");
-        } else {
-            writeControlWord("ql");
-        }
-
-
-
-        if (!containsText()) {
-            writeControlWord("intbl");
-
-            //R.Marra this create useless paragraph
-            //Seem working into Word97 with the "intbl" only
-//            writeControlWord("par");
-        }
-
-        writeControlWord("cell");
+       if (getRow().getTable().isNestedTable()) {
+               //nested table
+               writeControlWordNS("nestcell");
+               writeGroupMark(true);
+               writeControlWord("nonesttables");
+               writeControlWord("par");
+               writeGroupMark(false);
+       } else {
+               // word97 hangs if cell does not contain at least one "par" control word
+               // TODO this is what causes the extra spaces in nested table of test
+               //      004-spacing-in-tables.fo,
+               // but if is not here we generate invalid RTF for word97
+       
+               if (setCenter) {
+                   writeControlWord("qc");
+               } else if (setRight) {
+                   writeControlWord("qr");
+               } else {
+                   writeControlWord("ql");
+               }
+       
+       
+               
+               if (!containsText()) {
+                   writeControlWord("intbl");
+       
+                   //R.Marra this create useless paragraph
+                   //Seem working into Word97 with the "intbl" only
+       //            writeControlWord("par");
+               }
+       
+               writeControlWord("cell");
+       }
     }
 
 
@@ -374,6 +384,7 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
      * disabled for V0.3 - nested table support is not done yet
      * @throws IOException for I/O problems
      */
+    /*
     protected void writeRtfContent()
     throws IOException {
         int extraRowIndex = 0;
@@ -405,7 +416,7 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
                 e.writeRtf();
             }
         }
-    }
+    }*/
 
     /**
      * A table cell always contains "useful" content, as it is here to take some
@@ -449,7 +460,10 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
     public RtfTextrun getTextrun()
     throws IOException {
         RtfAttributes attrs = new RtfAttributes();
-        attrs.set("intbl");
+        
+        if(!getRow().getTable().isNestedTable()) {
+               attrs.set("intbl");
+        }
         
         RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, attrs);
 
@@ -459,4 +473,17 @@ implements IRtfParagraphContainer, IRtfListContainer, IRtfTableContainer,
         
         return textrun;
     }
+    
+    public RtfTableRow getRow() {
+       RtfElement e=this;
+       while(e.parent != null) {
+               if (e.parent instanceof RtfTableRow) {
+                       return (RtfTableRow) e.parent;
+               }
+               
+               e = e.parent;
+       }
+       
+       return null;  
+    }
 }
index 846a782d5d7456778f9fd1a4372a0b53da659341..d43cecc4aa21c9fecfdbd3c8d602bce8b9196da2 100644 (file)
@@ -38,7 +38,7 @@ import java.util.Iterator;
 
 public class RtfTableRow extends RtfContainer implements ITableAttributes {
     private RtfTableCell cell;
-    private RtfExtraRowSet extraRowSet;
+//    private RtfExtraRowSet extraRowSet;
     private int id;
     private int highestCell = 0;
 
@@ -132,13 +132,26 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes {
      */
     protected void writeRtfContent() throws IOException {
 
-        // create new extra row set to allow our cells to put nested tables
-        // in rows that will be rendered after this one
-        extraRowSet = new RtfExtraRowSet(writer);
-
-        // render the row and cells definitions
+       if (getTable().isNestedTable()) {
+               //nested table
+               writeControlWord("intbl");
+               writeControlWord("itap2");
+       } else {
+               //normal (not nested) table
+               writeRowAndCellsDefintions();
+       }
+        // now children can write themselves, we have the correct RTF prefix code
+        super.writeRtfContent();
+    }
+    
+    public void writeRowAndCellsDefintions() throws IOException {
+//      render the row and cells definitions
         writeControlWord("trowd");
-
+        
+        if (!getTable().isNestedTable()) {
+               writeControlWord("itap0");
+        }
+               
         //check for keep-together
         if (attrib != null && attrib.isSet(ITableAttributes.ROW_KEEP_TOGETHER)) {
             writeControlWord(ROW_KEEP_TOGETHER);
@@ -160,47 +173,6 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes {
                     attrib.getValue(ITableAttributes.ROW_HEIGHT));
         }
 
-        /**
-         * Added by Boris POUDEROUS on 07/02/2002
-         * in order to get the indexes of the cells preceding a cell that
-         * contains a nested table.
-         * Thus, the cells of the extra row will be merged with the cells above.
-         */
-        boolean nestedTableFound = false;
-        int index = 0; // Used to store the index of the cell that contains a nested table
-        int numberOfCellsBeforeNestedTable = 0;
-
-        java.util.Vector indexesFound = new java.util.Vector();
-        for (Iterator it = getChildren().iterator(); it.hasNext();) {
-            final RtfElement e = (RtfElement)it.next();
-
-            if (e instanceof RtfTableCell) {
-                if (!nestedTableFound) {
-                    ++numberOfCellsBeforeNestedTable;
-                }
-                for (Iterator it2 = ((RtfTableCell)e).getChildren().iterator(); it2.hasNext();) {
-                      final RtfElement subElement = (RtfElement)it2.next();
-                      if (subElement instanceof RtfTable) {
-                          nestedTableFound = true;
-                          indexesFound.addElement(new Integer(index));
-                        } else if (subElement instanceof RtfParagraph) {
-                           for (Iterator it3
-                                = ((RtfParagraph)subElement).getChildren().iterator(); 
-                                it3.hasNext();) {
-                                final RtfElement subSubElement = (RtfElement)it3.next();
-                                if (subSubElement instanceof RtfTable) {
-                                    nestedTableFound = true;
-                                    indexesFound.addElement(new Integer(index));
-                                  }
-                             }
-                        }
-                   }
-              }
-
-            index++;
-          }
-         /** - end - */
-
         // write X positions of our cells
         int xPos = 0;
         
@@ -209,37 +181,10 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes {
             xPos = ((Integer)leftIndent).intValue();
         }
         
-        index = 0;            // Line added by Boris POUDEROUS on 07/02/2002
+        int index = 0;
         for (Iterator it = getChildren().iterator(); it.hasNext();) {
             final RtfElement e = (RtfElement)it.next();
             if (e instanceof RtfTableCell) {
-                /**
-                 * Added by Boris POUDEROUS on 2002/07/02
-                 */
-                // If one of the row's child cells contains a nested table :
-                if (!indexesFound.isEmpty()) {
-                    for (int i = 0; i < indexesFound.size(); i++) {
-                        // If the current cell index is equals to the index of the cell that
-                        // contains a nested table => NO MERGE
-                        if (index == ((Integer)indexesFound.get(i)).intValue()) {
-                            break;
-
-                        // If the current cell index is lower than the index of the cell that
-                        // contains a nested table => START VERTICAL MERGE
-                        } else if (index < ((Integer)indexesFound.get(i)).intValue()) {
-                            ((RtfTableCell)e).setVMerge(RtfTableCell.MERGE_START);
-                            break;
-
-                        // If the current cell index is greater than the index of the cell that
-                        // contains a nested table => START VERTICAL MERGE
-                        } else if (index > ((Integer)indexesFound.get(i)).intValue()) {
-                            ((RtfTableCell)e).setVMerge(RtfTableCell.MERGE_START);
-                            break;
-                          }
-                      }
-                  }
-                /** - end - */
-
                 // Added by Normand Masse
                 // Adjust the cell's display attributes so the table's/row's borders
                 // are drawn properly.
@@ -276,11 +221,8 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes {
             }
           index++; // Added by Boris POUDEROUS on 2002/07/02
         }
-
-        newLine();
         
-        // now children can write themselves, we have the correct RTF prefix code
-        super.writeRtfContent();
+        newLine();
     }
 
     private void adjustBorderProperties(RtfTable parentTable) {
@@ -309,16 +251,29 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes {
      * @throws IOException for I/O problems
      */
     protected void writeRtfSuffix() throws IOException {
-        writeControlWord("row");
+       if (getTable().isNestedTable()) {
+               //nested table
+               writeGroupMark(true);
+               writeStarControlWord("nesttableprops");
+               writeRowAndCellsDefintions();
+               writeControlWordNS("nestrow");
+               writeGroupMark(false);
+               
+               
+               writeGroupMark(true);
+               writeControlWord("nonesttables");
+               writeControlWord("par");
+               writeGroupMark(false);
+       } else {
+               writeControlWord("row");
+       }
 
-        // write extra rows if any
-        extraRowSet.writeRtf();
         writeGroupMark(false);
     }
 
-    RtfExtraRowSet getExtraRowSet() {
-        return extraRowSet;
-    }
+//    RtfExtraRowSet getExtraRowSet() {
+//        return extraRowSet;
+//    }
 
     private void writePaddingAttributes()
     throws IOException {
@@ -365,4 +320,17 @@ public class RtfTableRow extends RtfContainer implements ITableAttributes {
     public boolean isHighestCell(int id) {
         return (highestCell == id) ? true : false;
     }
+    
+    public RtfTable getTable() {
+       RtfElement e=this;
+       while(e.parent != null) {
+               if (e.parent instanceof RtfTable) {
+                       return (RtfTable) e.parent;
+               }
+               
+               e = e.parent;
+       }
+       
+       return null;  
+    }
 }