Index: RTFHandler.java
===================================================================
--- RTFHandler.java	(revision 38)
+++ RTFHandler.java	(working copy)
@@ -106,6 +106,7 @@
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfHyperLink;
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfList;
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfListItem;
+import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfPage;
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfSection;
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTable;
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTableCell;
@@ -113,6 +114,7 @@
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfTextrun;
 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfListItem.RtfListItemLabel;
 import org.apache.fop.render.rtf.rtflib.tools.BuilderContext;
+import org.apache.fop.render.rtf.rtflib.tools.PercentContext;
 import org.apache.fop.render.rtf.rtflib.tools.TableContext;
 
 /**
@@ -145,6 +147,8 @@
     private BuilderContext builderContext = new BuilderContext(null);
 
     private SimplePageMaster pagemaster;
+    
+    private PercentContext percentManager=new PercentContext();
 
     /**
      * Creates a new RTF structure handler.
@@ -223,6 +227,14 @@
             }
 
             builderContext.pushContainer(sect);
+            
+            //Calculate usable page width for this flow
+            int UseAblePageWidth=pagemaster.getPageWidth().getValue()-
+            pagemaster.getCommonMarginBlock().marginLeft.getValue()-
+            pagemaster.getCommonMarginBlock().marginRight.getValue()-
+            Integer.parseInt(sect.getRtfAttributes().getValue(RtfPage.MARGIN_LEFT).toString())-
+            Integer.parseInt(sect.getRtfAttributes().getValue(RtfPage.MARGIN_RIGHT).toString());
+            percentManager.setDimension(pageSeq,UseAblePageWidth);
 
             bHeaderSpecified = false;
             bFooterSpecified = false;
@@ -1249,33 +1261,37 @@
             height = abstractGraphic.getHeight().getValue();
         }
 
-        //get content-width
+      //get content-width
         int contentwidth = 0;
-        if (abstractGraphic.getContentWidth().getEnum()
-                == Constants.EN_AUTO) {
-            contentwidth = info.getSize().getWidthMpt();
-        } else if (abstractGraphic.getContentWidth().getEnum()
+//        if (abstractGraphic.getContentWidth().getEnum()
+//                == Constants.EN_AUTO) {
+//            contentwidth = fopImage.getIntrinsicWidth();
+//        } else 
+          if (abstractGraphic.getContentWidth().getEnum()
                 == Constants.EN_SCALE_TO_FIT) {
             contentwidth = width;
-        } else {
-            //TODO: check, if the value is a percent value
-            contentwidth = abstractGraphic.getContentWidth().getValue();
+        } else if (abstractGraphic.getContentWidth().getEnum()==0){
+            //Register image to be able to calculate its width in percent
+            percentManager.setDimension(abstractGraphic,width);
+            contentwidth = abstractGraphic.getContentWidth().getValue(percentManager);
         }
 
         //get content-width
         int contentheight = 0;
-        if (abstractGraphic.getContentHeight().getEnum()
-                == Constants.EN_AUTO) {
-
-            contentheight = info.getSize().getHeightMpt();
-
-        } else if (abstractGraphic.getContentHeight().getEnum()
+//        if (abstractGraphic.getContentHeight().getEnum()
+//                == Constants.EN_AUTO) {
+//
+//            contentheight = fopImage.getIntrinsicHeight();
+//
+//        } else 
+          if (abstractGraphic.getContentHeight().getEnum()
                 == Constants.EN_SCALE_TO_FIT) {
 
             contentheight = height;
-        } else {
-            //TODO: check, if the value is a percent value
-            contentheight = abstractGraphic.getContentHeight().getValue();
+        } else if (abstractGraphic.getContentHeight().getEnum()==0){
+          //Register image to be able to calculate its width in percent
+          percentManager.setDimension(abstractGraphic,height);
+          contentheight = abstractGraphic.getContentHeight().getValue(percentManager);
         }
 
         //set width in rtf
Index: rtflib/rtfdoc/RtfExternalGraphic.java
===================================================================
--- rtflib/rtfdoc/RtfExternalGraphic.java	(revision 38)
+++ rtflib/rtfdoc/RtfExternalGraphic.java	(working copy)
@@ -232,17 +232,17 @@
     /**
      * The desired percent value of the height
      */
-    protected int heightPercent = -1;
+//    protected int heightPercent = -1;
 
     /**
      * The desired height (in twips)
      */
-    protected int heightDesired = -1;
+    protected int heightDesired = 0;
 
     /**
      * Flag whether the desired height is a percentage
      */
-    protected boolean perCentH = false;
+//    protected boolean perCentH = false;
 
     /**
      * The width of the image (in pixels)
@@ -252,17 +252,17 @@
     /**
      * The desired percent value of the width
      */
-    protected int widthPercent = -1;
+//    protected int widthPercent = -1;
 
     /**
      * The desired width (in twips)
      */
-    protected int widthDesired = -1;
+    protected int widthDesired = 0;
 
     /**
      * Flag whether the desired width is a percentage
      */
-    protected boolean perCentW = false;
+//    protected boolean perCentW = false;
 
     /**
      * Flag whether the image size shall be adjusted
@@ -279,6 +279,18 @@
 
      /** The image format */
      private FormatBase imageformat;
+     
+     /** Control word for the width */
+     private static final String PIC_WIDTH="picw";
+     
+     /** Control word for the height */
+     private static final String PIC_HEIGHT="pich";
+     
+     /** Control word for the desired width */
+     private static final String PIC_WIDTH_SCALE="picwgoal";
+     
+     /** Control word for the desired height */
+     private static final String PIC_HEIGHT_SCALE="pichgoal";
 
     //////////////////////////////////////////////////
     // @@ Construction
@@ -480,45 +492,73 @@
     }
 
     private void writeSizeInfo () throws IOException {
-        // Set image size
-        if (width != -1) {
-            writeControlWord("picw" + width);
+      //TODO Recalculate the values if a compression rate is set
+      
+      //Modified by Maximilian Aster
+      //Set image size
+      //widthDesired and heightDesired are always set by RtfHandler
+      if(width != 0 && height != 0)
+      {
+        writeControlWord(PIC_WIDTH + width);
+        writeControlWord(PIC_HEIGHT + height);
+      }
+      if(heightDesired != 0 && !(widthDesired != 0 && scaleUniform))
+      {
+        writeControlWord(PIC_HEIGHT_SCALE + heightDesired);
+        if(scaleUniform)
+        {
+          int scaledheight=heightDesired/height*width;
+          writeControlWord(PIC_WIDTH_SCALE + scaledheight);
         }
-        if (height != -1) {
-            writeControlWord("pich" + height);
+      }
+      if(widthDesired != 0)
+      {
+        writeControlWord(PIC_WIDTH_SCALE + widthDesired);
+        if(scaleUniform)
+        {
+          int scaledheight=widthDesired/width*height;
+          writeControlWord(PIC_HEIGHT_SCALE + scaledheight);
         }
+      }
+//      if (width != -1) {
+//        writeControlWord("picw" + width);
+//    }
+//    if (height != -1) {
+//        writeControlWord("pich" + height);
+//    }
+//
+//    if (widthDesired != -1) {
+//        if (perCentW) {
+//            writeControlWord("picscalex" + widthDesired);
+//        } else {
+//            //writeControlWord("picscalex" + widthDesired * 100 / width);
+//            writeControlWord("picwgoal" + widthDesired);
+//        }
+//
+//    } else if (scaleUniform && heightDesired != -1) {
+//        if (perCentH) {
+//            writeControlWord("picscalex" + heightDesired);
+//        } else {
+//            writeControlWord("picscalex" + heightDesired * 100 / height);
+//        }
+//    }
+//
+//    if (heightDesired != -1) {
+//        if (perCentH) {
+//            writeControlWord("picscaley" + heightDesired);
+//        } else {
+//            //writeControlWord("picscaley" + heightDesired * 100 / height);
+//            writeControlWord("pichgoal" + heightDesired);
+//        }
+//
+//    } else if (scaleUniform && widthDesired != -1) {
+//        if (perCentW) {
+//            writeControlWord("picscaley" + widthDesired);
+//        } else {
+//            writeControlWord("picscaley" + widthDesired * 100 / width);
+//        }
+//    }
 
-        if (widthDesired != -1) {
-            if (perCentW) {
-                writeControlWord("picscalex" + widthDesired);
-            } else {
-                //writeControlWord("picscalex" + widthDesired * 100 / width);
-                writeControlWord("picwgoal" + widthDesired);
-            }
-
-        } else if (scaleUniform && heightDesired != -1) {
-            if (perCentH) {
-                writeControlWord("picscalex" + heightDesired);
-            } else {
-                writeControlWord("picscalex" + heightDesired * 100 / height);
-            }
-        }
-
-        if (heightDesired != -1) {
-            if (perCentH) {
-                writeControlWord("picscaley" + heightDesired);
-            } else {
-                //writeControlWord("picscaley" + heightDesired * 100 / height);
-                writeControlWord("pichgoal" + heightDesired);
-            }
-
-        } else if (scaleUniform && widthDesired != -1) {
-            if (perCentW) {
-                writeControlWord("picscaley" + widthDesired);
-            } else {
-                writeControlWord("picscaley" + widthDesired * 100 / width);
-            }
-        }
     }
 
     //////////////////////////////////////////////////
@@ -532,7 +572,7 @@
      */
     public void setHeight(String theHeight) {
         this.heightDesired = ImageUtil.getInt(theHeight);
-        this.perCentH = ImageUtil.isPercent(theHeight);
+//        this.perCentH = ImageUtil.isPercent(theHeight);
     }
 
     /**
@@ -542,7 +582,7 @@
      */
     public void setWidth(String theWidth) {
         this.widthDesired = ImageUtil.getInt(theWidth);
-        this.perCentW = ImageUtil.isPercent(theWidth);
+//        this.perCentW = ImageUtil.isPercent(theWidth);
     }
 
     /**
Index: rtflib/tools/PercentContext.java
===================================================================
--- rtflib/tools/PercentContext.java	(revision 0)
+++ rtflib/tools/PercentContext.java	(revision 0)
@@ -0,0 +1,108 @@
+package org.apache.fop.render.rtf.rtflib.tools;
+
+import java.util.HashMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.fop.datatypes.PercentBaseContext;
+import org.apache.fop.fo.FONode;
+import org.apache.fop.fo.FObj;
+import org.apache.fop.fo.pagination.PageSequence;
+
+public class PercentContext implements PercentBaseContext
+{
+  private static Log log = LogFactory.getLog(PercentContext.class);
+  
+  private static final String BLOCKCNTAINER="fo:block-container";
+  private static final String INLINECONTAINER="fo:inline-container";
+  private static final String TABLE="fo:table";
+  private static final String TABLECOLUMN="fo:table-column";
+  private static final String PAGESEQUENCE="fo:page-sequence";
+  private static  String EXTERNALGRAPHIC="fo:external-graphic";
+  
+  /** String array of Elements having a width property */
+  public static final String[] WIDTH_OBJECTS = new String[]{
+    BLOCKCNTAINER, INLINECONTAINER, TABLE, TABLECOLUMN, 
+    PAGESEQUENCE, EXTERNALGRAPHIC
+  };
+
+  /** Map containing the FObj and its width */
+  private HashMap LengthMap = new HashMap();
+  
+  /** Variable to check if a base width is set */
+  private boolean BaseWidthSet=false;
+  
+  /**
+   * Returns the available width for a specific FObj
+   * @param lengthBase lengthBase not used
+   * @param fobj the FObj
+   * @return Available Width
+   */
+  public int getBaseLength(int lengthBase, FObj fobj)
+  {
+    Object width=LengthMap.get(fobj);
+    if(width!=null)
+    {
+      return Integer.parseInt(width.toString());
+    }
+    else
+    {
+      return -1;
+    }
+  }
+  
+  /**
+   * Elements having a width property can call this function if their
+   * width is calculated in RTFHandler
+   * @param fobj the FObj
+   * @param width width of the FObj
+   */
+  public void setDimension(FObj fobj, int width)
+  {
+    //TODO ACCEPT only objects above for setting a width
+    if(fobj instanceof PageSequence)
+    {
+      BaseWidthSet=true;
+    }
+    //width in mpt
+    LengthMap.put(fobj, new Integer(width));
+  }
+  
+  //Searches for the parent object of fobj
+  private Integer findParent(FONode fobj)
+  {
+    if(fobj.getRoot()!=fobj)
+    {
+      if(LengthMap.containsKey(fobj))
+      {
+        return new Integer(LengthMap.get(fobj).toString());
+      }
+      else
+      {
+        return findParent(fobj.getParent());
+      }
+    }
+    else
+    {
+      log.error("Base Value for element "+ fobj.getName() +" not found");
+      return new Integer(-1);
+    }
+  }
+  
+  /**
+   * Elements willing to use this context have to register themselves 
+   * by calling this function 
+   * @param fobj the FObj
+   */
+  public void setDimension(FObj fobj)
+  {
+    if(BaseWidthSet)
+    {
+      Integer width=findParent(fobj.getParent());
+      if(width.intValue()!=-1)
+      {
+        LengthMap.put(fobj,width);
+      }
+    }
+  }
+}
