Index: layoutmgr/inline/LineLayoutManager.java
===================================================================
--- layoutmgr/inline/LineLayoutManager.java	(revision 432103)
+++ layoutmgr/inline/LineLayoutManager.java	(working copy)
@@ -60,6 +60,8 @@
 
 import org.apache.fop.traits.MinOptMax;
 
+import org.apache.fop.layoutmgr.table.TableContentLayoutManager;
+
 /**
  * LayoutManager for lines. It builds one or more lines containing
  * inline areas generated by its sub layout managers.
@@ -682,6 +684,9 @@
                 }
             }
             
+            int sum = 0;
+            int max = 0;
+
             // loop over the KnuthSequences (and single KnuthElements) in returnedList
             ListIterator iter = returnedList.listIterator();
             while (iter.hasNext()) {
@@ -697,6 +702,25 @@
                     }
                     bPrevWasKnuthBox = lastElement.isBox() && ((KnuthElement) lastElement).getW() != 0;
                     
+
+                    //compute the full width of the element list
+                    ListElement elem;
+                    for (int i=0; i < sequence.size(); i++) {
+                      elem = sequence.getElement(i);
+                      //System.out.println("width of element #" + i + ": " + ((KnuthElement) elem).getW());
+                      sum += ((KnuthElement) elem).getW();
+                      //System.out.println(elem);
+                      if (max < (((KnuthElement) elem).getW())) max = ((KnuthElement) elem).getW();
+
+                    }
+                    
+                    //System.out.println("full width of element list: " + sum);
+                    //System.out.println("minimum width: " + max);
+                    //System.out.println("=======================================");
+
+                    TableContentLayoutManager tclm = new TableContentLayoutManager(sum, max);
+                    
+
                     // if last paragraph is open, add the new elements to the paragraph
                     // else this is the last paragraph
                     if (lastPar == null) { 
Index: layoutmgr/table/TableContentLayoutManager.java
===================================================================
--- layoutmgr/table/TableContentLayoutManager.java	(revision 432103)
+++ layoutmgr/table/TableContentLayoutManager.java	(working copy)
@@ -56,6 +56,8 @@
 import org.apache.fop.layoutmgr.SpaceResolver.SpaceHandlingBreakPosition;
 import org.apache.fop.traits.MinOptMax;
 
+import org.apache.fop.fo.properties.FixedLength;
+
 /**
  * Layout manager for table contents, particularly managing the creation of combined element lists.
  */
@@ -76,6 +78,9 @@
 
     private int startXOffset;
     private int usedBPD;
+
+    public static int spanWidthOpt;
+    public static int spanWidthMin;
     
     private TableStepper stepper = new TableStepper(this);
         
@@ -97,6 +102,11 @@
         }
     }
     
+    public TableContentLayoutManager(int spanWidthOpt, int spanWidthMin) {
+        if (this.spanWidthOpt < spanWidthOpt) this.spanWidthOpt = spanWidthOpt;
+        if (this.spanWidthMin > spanWidthMin) this.spanWidthMin = spanWidthMin;
+    }
+
     /**
      * @return the table layout manager
      */
@@ -470,9 +480,42 @@
                         childLC.setStackLimit(context.getStackLimit()); //necessary?
                         childLC.setRefIPD(spanWidth);
                         
+                        //System.out.println("========================================");
+                        //System.out.println("cell-width: " + spanWidth);
+
+                        //spanWidth2 = primary.getCellLM().getNextKnuthElements2(
+                        //                           childLC, alignment);
+
+                        //System.out.println("getNextKnuthElements2 " + spanWidth2);
+                        
+
+                        this.spanWidthOpt = 0;
+                        this.spanWidthMin = 0;
+
                         //Get the element list for the cell contents
                         LinkedList elems = primary.getCellLM().getNextKnuthElements(
                                                 childLC, alignment);
+
+                        // if we found new optimal width for the column
+                        if (getTableLM().getColumns2().getColumn(primary.getStartCol()+1)
+                                    .getColumnWidth().getValue(getTableLM()) < this.spanWidthOpt) {
+                          getTableLM().getColumns2().getColumn(primary.getStartCol()+1)
+                                    .setColumnWidth(new FixedLength(this.spanWidthOpt));
+                        }
+
+                        // if we found a new minimal width for the column
+                        if (getTableLM().getColumns2().getColumn(primary.getStartCol()+1)
+                                    .getColumnMinWidth().getValue(getTableLM()) > this.spanWidthMin) {
+                          getTableLM().getColumns2().getColumn(primary.getStartCol()+1)
+                                    .setColumnMinWidth(new FixedLength(this.spanWidthMin));
+                        }
+
+                        //System.out.println("full width2: " + this.spanWidthOpt);
+                        //System.out.println("min width2: " + this.spanWidthMin);
+                        
+
+                        //System.out.println("----------------------------------------");
+
                         //Temporary? Multiple calls in case of break conditions.
                         //TODO Revisit when table layout is restartable
                         while (!primary.getCellLM().isFinished()) {
Index: layoutmgr/table/TableLayoutManager.java
===================================================================
--- layoutmgr/table/TableLayoutManager.java	(revision 432103)
+++ layoutmgr/table/TableLayoutManager.java	(working copy)
@@ -46,6 +46,8 @@
 import org.apache.fop.fo.FONode;
 import org.apache.fop.fo.FObj;
 
+import org.apache.fop.fo.properties.FixedLength;
+
 /**
  * LayoutManager for a table FO.
  * A table consists of oldColumns, table header, table footer and multiple
@@ -58,7 +60,9 @@
                 implements ConditionalElementListener {
     
     private TableContentLayoutManager contentLM; 
+    private TableContentLayoutManager contentLM2; 
     private ColumnSetup columns = null;
+    private ColumnSetup columns2 = null;
 
     private Block curBlockArea;
 
@@ -82,6 +86,7 @@
     public TableLayoutManager(Table node) {
         super(node);
         this.columns = new ColumnSetup(node);
+        this.columns2 = new ColumnSetup(node);
     }
 
     /** @return the table FO */
@@ -95,6 +100,10 @@
     public ColumnSetup getColumns() {
         return this.columns;
     }
+
+    public ColumnSetup getColumns2() {
+        return this.columns2;
+    }
     
     /** @see org.apache.fop.layoutmgr.LayoutManager#initialize() */
     public void initialize() {
@@ -214,6 +223,106 @@
         //Spaces, border and padding to be repeated at each break
         addPendingMarks(context);
 
+
+        /* Start auto table layout */
+        // set the column width when table-layout="auto"
+        if (getTable().isAutoLayout()) {
+            //System.out.println("table-layout=\"auto\" has been detected");
+
+            int w = getTable().getInlineProgressionDimension().getOptimum(this).getLength().getValue(this);
+            //System.out.println("optimum table width: " + w);
+
+            LinkedList returnedList2 = null;
+            LinkedList contentList2 = new LinkedList();
+
+    
+            LayoutContext childLC2 = new LayoutContext(0);
+            
+            childLC2.setRefIPD(context.getRefIPD());
+            childLC2.copyPendingMarksFrom(context);
+    
+            if (contentLM2 == null) {
+                contentLM2 = new TableContentLayoutManager(this);
+                contentLM2.spanWidthOpt = 0;
+            }
+
+            for (Iterator i2 = columns2.iterator(); i2.hasNext();) {
+                TableColumn column2 = (TableColumn) i2.next();
+                if (column2 != null) {
+                    column2.setColumnWidth(new FixedLength(0));
+                }
+            }
+
+            returnedList2 = contentLM2.getNextKnuthElements(childLC2, alignment);
+
+            //int sumCols = 0;
+            //float factors = 0;
+              
+            int sum = 0;
+            int sumOpt = 0;
+
+            for (Iterator i2 = columns2.iterator(); i2.hasNext();) {
+                TableColumn column2 = (TableColumn) i2.next();
+                if (column2 != null) {
+                    sum += column2.getColumnWidth().getValue(this);
+                    sumOpt += column2.getColumnMinWidth().getValue(this);
+                }
+            }
+
+            int add = w - sum;
+            int sub = w - sumOpt;
+            
+
+            // if the sum of the column withs is greater than the table width.
+            if (sub < 0) {
+              int t = sub;
+              while (t < 0 ) {
+                Iterator i2 = columns2.iterator();
+                for (Iterator i = columns.iterator(); i.hasNext();) {
+                    TableColumn column = (TableColumn) i.next();
+                    TableColumn column2 = (TableColumn) i2.next();
+                    if (column != null) {
+                        //System.out.println("substracted space to column: " + 1);
+                        //column.setColumnWidth(new FixedLength(column2.getColumnWidth().getValue(this) - 1));
+                        //System.out.println("Fixed column-width to " + (column.getColumnWidth().getValue(this)));
+                        //}
+                        if (column2.getColumnMinWidth().getValue(this) < column.getColumnWidth().getValue(this)) {
+                          column.setColumnWidth(new FixedLength(column2.getColumnWidth().getValue(this) - 1));
+                          t++;
+                        }
+                      }
+                }
+              }
+            } else {
+              // otherwise add extra space
+              add /= columns.getColumnCount();
+              Iterator i2 = columns2.iterator();
+              for (Iterator i = columns.iterator(); i.hasNext();) {
+                  TableColumn column = (TableColumn) i.next();
+                  TableColumn column2 = (TableColumn) i2.next();
+                  if (column != null) {
+                      //System.out.println("added space to columns: " + add);
+                      column.setColumnWidth(new FixedLength(column2.getColumnWidth().getValue(this) + add));
+                      //System.out.println("Fixed column-width to " + (column.getColumnWidth().getValue(this)));
+                      //}
+                  }
+              }
+            }
+
+            // print out the column widths
+            Iterator i2 = columns2.iterator();
+            for (Iterator i = columns.iterator(); i.hasNext();) {
+                TableColumn column = (TableColumn) i.next();
+                TableColumn column2 = (TableColumn) i2.next();
+                if (column != null) {
+                    //System.out.println("Fixed column-width to " + (column.getColumnWidth().getValue(this)));
+                }
+            }
+
+        }
+
+        /* End auto table layout */
+
         LinkedList returnedList = null;
         LinkedList contentList = new LinkedList();
         //Position returnPosition = new NonLeafPosition(this, null);
Index: fo/Constants.java
===================================================================
--- fo/Constants.java	(revision 432103)
+++ fo/Constants.java	(working copy)
@@ -325,6 +325,8 @@
     /** Property constant */
     int PR_COLUMN_WIDTH = 71;
     /** Property constant */
+    int PR_COLUMN_MIN_WIDTH = 252;
+    /** Property constant */
     int PR_CONTENT_HEIGHT = 72;
     /** Property constant */
     int PR_CONTENT_TYPE = 73;
@@ -685,7 +687,7 @@
     /** Property constant - Custom extension */
     int PR_X_BLOCK_PROGRESSION_UNIT = 251;
     /** Number of property constants defined */
-    int PROPERTY_COUNT = 251;
+    int PROPERTY_COUNT = 252;
 
     // compound property constants
 
Index: fo/FOPropertyMapping.java
===================================================================
--- fo/FOPropertyMapping.java	(revision 432103)
+++ fo/FOPropertyMapping.java	(working copy)
@@ -2302,6 +2302,13 @@
         m.setDefault("proportional-column-width(1)", true);
         m.setPercentBase(LengthBase.CONTAINING_BLOCK_WIDTH);
         addPropertyMaker("column-width", m);
+        
+        // column min width
+        m  = new LengthProperty.Maker(PR_COLUMN_MIN_WIDTH);
+        m.setInherited(false);
+        m.setDefault("proportional-column-width(1)", true);
+        m.setPercentBase(LengthBase.CONTAINING_BLOCK_WIDTH);
+        addPropertyMaker("column-width", m);
 
         // empty-cells
         m  = new EnumProperty.Maker(PR_EMPTY_CELLS);
Index: fo/flow/TableColumn.java
===================================================================
--- fo/flow/TableColumn.java	(revision 432103)
+++ fo/flow/TableColumn.java	(working copy)
@@ -41,6 +41,7 @@
     private CommonBorderPaddingBackground commonBorderPaddingBackground;
     private Numeric columnNumber;
     private Length columnWidth;
+    private Length columnMinWidth;
     private Numeric numberColumnsRepeated;
     private Numeric numberColumnsSpanned;
     private int visibility;
@@ -73,6 +74,7 @@
         commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
         columnNumber = pList.get(PR_COLUMN_NUMBER).getNumeric();
         columnWidth = pList.get(PR_COLUMN_WIDTH).getLength();
+        columnMinWidth = pList.get(PR_COLUMN_MIN_WIDTH).getLength();
         numberColumnsRepeated = pList.get(PR_NUMBER_COLUMNS_REPEATED).getNumeric();
         numberColumnsSpanned = pList.get(PR_NUMBER_COLUMNS_SPANNED).getNumeric();
         visibility = pList.get(PR_VISIBILITY).getEnum();
@@ -126,6 +128,10 @@
         return columnWidth;
     }
 
+    public Length getColumnMinWidth() {
+        return columnMinWidth;
+    }
+
     /**
      * Sets the column width.
      * @param columnWidth the column width
@@ -134,6 +140,10 @@
         this.columnWidth = columnWidth;
     }
 
+    public void setColumnMinWidth(Length columnMinWidth) {
+        this.columnMinWidth = columnMinWidth;
+    }
+
     /**
      * @return the "column-number" property.
      */
