]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Further cleanup/refinement of FixedLength:
authorAndreas L. Delmelle <adelmelle@apache.org>
Sat, 2 Feb 2008 15:13:40 +0000 (15:13 +0000)
committerAndreas L. Delmelle <adelmelle@apache.org>
Sat, 2 Feb 2008 15:13:40 +0000 (15:13 +0000)
* improve consistency in use of the PropertyCache
  remove public constructor ->  replaced by getInstance() in a handful of classes
* added a sourceResolution parameter to getInstance() and the private
  constructor to allow better handling of device-dependent units (px)

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@617812 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/fo/flow/ExternalGraphic.java
src/java/org/apache/fop/fo/properties/BorderWidthPropertyMaker.java
src/java/org/apache/fop/fo/properties/FixedLength.java
src/java/org/apache/fop/fo/properties/FontSizePropertyMaker.java
src/java/org/apache/fop/fo/properties/IndentPropertyMaker.java
src/java/org/apache/fop/fo/properties/LengthRangeProperty.java

index dd45f585c26c5c52276c1cd622e0a5951032c0f8..03229e4c2b320c3f0eda3ce5c5c5b4e3ba3e2bd2 100644 (file)
@@ -37,7 +37,8 @@ import org.apache.fop.fo.ValidationException;
 import org.apache.fop.fo.properties.FixedLength;
 
 /**
- * Class modelling the fo:external-graphic object.
+ * Class modelling the <a href="http://www.w3.org/TR/xsl/#fo_external-graphic">
+ * <code>fo:external-graphic</code></a> object.
  * This FO node handles the external graphic. It creates an image
  * inline area that can be added to the area tree.
  */
@@ -63,9 +64,7 @@ public class ExternalGraphic extends AbstractGraphics {
         super(parent);
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     public void bind(PropertyList pList) throws FOPException {
         super.bind(pList);
         src = pList.get(PR_SRC).getString();
@@ -87,15 +86,14 @@ public class ExternalGraphic extends AbstractGraphics {
             this.intrinsicHeight = info.getSize().getHeightMpt();
             int baseline = info.getSize().getBaselinePositionFromBottom();
             if (baseline != 0) {
-                this.intrinsicAlignmentAdjust = new FixedLength(-baseline);
+                this.intrinsicAlignmentAdjust
+                    = FixedLength.getInstance(-baseline);
             }
         }
         //TODO Report to caller so he can decide to throw an exception
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     protected void startOfNode() throws FOPException {
         super.startOfNode();
         getFOEventHandler().image(this);
@@ -103,23 +101,19 @@ public class ExternalGraphic extends AbstractGraphics {
 
     /**
      * {@inheritDoc}
-     * XSL Content Model: empty
+     * <br>XSL Content Model: empty
      */
     protected void validateChildNode(Locator loc, String nsURI, String localName) 
         throws ValidationException {
             invalidChildError(loc, nsURI, localName);
     }
 
-    /**
-     * @return the "src" property.
-     */
+    /** @return the "src" property */
     public String getSrc() {
         return src;
     }
 
-    /**
-     * @return Get the resulting URL based on the src property.
-     */
+    /** @return Get the resulting URL based on the src property */
     public String getURL() {
         return url;
     }
@@ -129,30 +123,22 @@ public class ExternalGraphic extends AbstractGraphics {
         return "external-graphic";
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     public int getNameId() {
         return FO_EXTERNAL_GRAPHIC;
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     public int getIntrinsicWidth() {
         return this.intrinsicWidth;
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     public int getIntrinsicHeight() {
         return this.intrinsicHeight;
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     public Length getIntrinsicAlignmentAdjust() {
         return this.intrinsicAlignmentAdjust;
     }
index b828de2851895a064867c445e5275014d51bb7c3..86986bca5ac49f219e9dec38328fc6af47b5e39c 100644 (file)
@@ -63,7 +63,7 @@ public class BorderWidthPropertyMaker extends LengthProperty.Maker {
         // Calculate the values as described in 7.7.20.
         Property style = propertyList.get(borderStyleId);
         if (style.getEnum() == Constants.EN_NONE) {
-            return new FixedLength(0);
+            return FixedLength.getInstance(0);
         }
         return p;
     }
index 28206a12ab46c5eb6270cfaa2a492154d8ebf85a..fbd235b3013929df32c40b8fac4ac2fab441e968 100644 (file)
@@ -32,18 +32,40 @@ public final class FixedLength extends LengthProperty {
     private int millipoints;
 
     /**
-     * Set the length given a number of units and a unit name.
+     * Set the length given a number of units, a unit name and
+     * an assumed resolution (used in case the units are pixels)
      * 
-     * @param numUnits quantity of input units
-     * @param units input unit specifier (in, cm, etc.)
+     * @param numUnits  quantity of input units
+     * @param units     input unit specifier
+     * @param res       input/source resolution
      */
-    private FixedLength(double numUnits, String units) {
-        convert(numUnits, units);
+    private FixedLength(double numUnits, String units, float res) {
+        this.millipoints = convert(numUnits, units, res);
     }
     
     /**
-     * Return the cached FixedLength instance corresponding
+     * Return the cached {@link FixedLength} instance corresponding
+     * to the computed value in base-units (millipoints).
+     * 
+     * @param numUnits  quantity of input units
+     * @param units     input unit specifier
+     * @param sourceResolution input/source resolution (= ratio of pixels per pt)
+     * @return  the canonical FixedLength instance corresponding
+     *          to the given number of units and unit specifier
+     *          in the given resolution
+     */
+    public static FixedLength getInstance(double numUnits, 
+                                          String units,
+                                          float sourceResolution) {
+        return (FixedLength)cache.fetch(
+                new FixedLength(numUnits, units, sourceResolution));
+        
+    }
+    
+    /**
+     * Return the cached {@link FixedLength} instance corresponding
      * to the computed value
+     * This method assumes a source-resolution of 1 (1px = 1pt)
      * 
      * @param numUnits  input units
      * @param units     unit specifier
@@ -52,59 +74,56 @@ public final class FixedLength extends LengthProperty {
      */
     public static FixedLength getInstance(double numUnits, 
                                           String units) {
-        return (FixedLength) cache.fetch(new FixedLength(numUnits, units));
+        return getInstance(numUnits, units, 1.0f);
         
     }
     
     /**
-     * @param baseUnits the length as a number of base units (millipoints)
+     * Return the cached {@link FixedLength} instance corresponding
+     * to the computed value.
+     * This method assumes 'millipoints' (non-standard) as units, 
+     * and an implied source-resolution of 1 (1px = 1pt).
+     * 
+     * @param numUnits  input units
+     * @return  the canonical FixedLength instance corresponding
+     *          to the given number of units and unit specifier
      */
-    public FixedLength(int baseUnits) {
-        millipoints = baseUnits;
+    public static FixedLength getInstance(double numUnits) {
+        return getInstance(numUnits, "mpt", 1.0f);
+        
     }
-
+    
     /**
      * Convert the given length to a dimensionless integer representing
      * a whole number of base units (milli-points).
+     * 
      * @param dvalue quantity of input units
      * @param unit input unit specifier (in, cm, etc.)
+     * @param res   the input/source resolution (in case the unit spec is "px")
      */
-    private void convert(double dvalue, String unit) {
-        // TODO: the whole routine smells fishy.
+    private static int convert(double dvalue, String unit, float res) {
+        // TODO: Maybe this method has a better place in org.apache.fop.util.UnitConv?.
 
-        int assumedResolution = 1;    // points/pixel = 72dpi
-
-        if (unit.equals("in")) {
-            dvalue = dvalue * 72;
-        } else if (unit.equals("cm")) {
-            dvalue = dvalue * 28.3464567;
-        } else if (unit.equals("mm")) {
-            dvalue = dvalue * 2.83464567;
-        } else if (unit.equals("pt")) {
-            // Do nothing.
-            // dvalue = dvalue;
-        } else if (unit.equals("mpt")) { //mpt is non-standard!!! mpt=millipoints
-            // TODO: this seems to be wrong.
-            // Do nothing.
-            // dvalue = dvalue;
-        } else if (unit.equals("pc")) {
-            dvalue = dvalue * 12;
-            /*
-             * } else if (unit.equals("em")) {
-             * dvalue = dvalue * fontsize;
-             */
-        } else if (unit.equals("px")) {
-            // TODO: get resolution from user agent?
-            dvalue = dvalue * assumedResolution;
-        } else {
-            dvalue = 0;
-            log.error("Unknown length unit '" + unit + "'");
-        }
-        if (unit.equals("mpt")) {
-            millipoints = (int)dvalue;
+        if ("px".equals(unit)) {
+            //device-dependent units, take the resolution into account
+            dvalue *= (res * 1000);
         } else {
-            millipoints = (int)(dvalue * 1000);
+            if ("in".equals(unit)) {
+                dvalue *= 72000;
+            } else if ("cm".equals(unit)) {
+                dvalue *= 28346.4567;
+            } else if ("mm".equals(unit)) {
+                dvalue *= 2834.64567;
+            } else if ("pt".equals(unit)) {
+                dvalue *= 1000;
+            } else if ("pc".equals(unit)) {
+                dvalue *= 12000;
+            } else if (!"mpt".equals(unit)) {
+                dvalue = 0;
+                log.error("Unknown length unit '" + unit + "'");
+            }
         }
+        return (int)dvalue;
     }
 
     /** {@inheritDoc} */
@@ -128,7 +147,7 @@ public final class FixedLength extends LengthProperty {
     }
 
     /**
-     * Return true since FixedLength are always absolute.
+     * Return true since a FixedLength is always absolute.
      * {@inheritDoc}
      */
     public boolean isAbsolute() {
@@ -142,11 +161,13 @@ public final class FixedLength extends LengthProperty {
 
     /** {@inheritDoc} */
     public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
         if (obj instanceof FixedLength) {
             return (((FixedLength)obj).millipoints == this.millipoints);
-        } else {
-            return false;
         }
+        return false;
     }
 
     /** {@inheritDoc} */
index acc02dc136951634782ec9651d68e68dcd7d3346..b641b2abf57dcc18a17be4da09e5e16b5b62f705 100644 (file)
@@ -55,7 +55,7 @@ public class FontSizePropertyMaker
         if (p instanceof PercentLength) {
             Property pp = propertyList.getFromParent(this.propId);
             p = FixedLength.getInstance(
-                    pp.getLength().getValue() * ((PercentLength)p).getPercentage() / 100, "mpt");
+                    pp.getLength().getValue() * ((PercentLength)p).getPercentage() / 100);
         }
         return p;
     }
@@ -74,10 +74,10 @@ public class FontSizePropertyMaker
             int baseFontSize = computeClosestAbsoluteFontSize(pp.getLength().getValue());
             if (p.getEnum() == EN_LARGER) {
                 return FixedLength.getInstance(
-                        Math.round(baseFontSize * FONT_SIZE_GROWTH_FACTOR), "mpt");
+                        Math.round(baseFontSize * FONT_SIZE_GROWTH_FACTOR));
             } else {
                 return FixedLength.getInstance(
-                        Math.round(baseFontSize / FONT_SIZE_GROWTH_FACTOR), "mpt");
+                        Math.round(baseFontSize / FONT_SIZE_GROWTH_FACTOR));
             }
         }
         return super.convertProperty(p, propertyList, fo);
index ebc1b80f0b3646f93e3a0cd3f3c6152b8cea83d0..b29caba0472766402ee2248533feb341cbb65bfa 100644 (file)
@@ -108,7 +108,7 @@ public class IndentPropertyMaker extends CorrespondingPropertyMaker {
             //Margin is used
             Numeric margin = propertyList.get(marginProp).getNumeric();
             
-            Numeric v = new FixedLength(0);
+            Numeric v = FixedLength.getInstance(0, "mpt");
             if (!propertyList.getFObj().generatesReferenceAreas()) {
                 // The inherited_value_of([start|end]-indent)
                 v = NumericOp.addition(v, propertyList.getInherited(baseMaker.propId).getNumeric());
@@ -173,7 +173,7 @@ public class IndentPropertyMaker extends CorrespondingPropertyMaker {
                 if (isInherited(propertyList) || !marginNearest) {
                     return null;
                 } else {
-                    return new FixedLength(0);
+                    return FixedLength.getInstance(0);
                 }
             } else {
                 return indent;
@@ -182,7 +182,7 @@ public class IndentPropertyMaker extends CorrespondingPropertyMaker {
             //Margin is used
             Numeric margin = propertyList.get(marginProp).getNumeric();
             
-            Numeric v = new FixedLength(0);
+            Numeric v = FixedLength.getInstance(0);
             if (isInherited(propertyList)) {
                 // The inherited_value_of([start|end]-indent)
                 v = NumericOp.addition(v, propertyList.getInherited(baseMaker.propId).getNumeric());
index acce6f564d0178283b69d029bbe7c64466053ceb..966f51a5130fdc8729748c30b6ea810052485789 100644 (file)
@@ -65,9 +65,7 @@ public class LengthRangeProperty extends Property implements CompoundDatatype {
                     || (len.isAbsolute() && len.getValue() < 0));
         }
         
-        /**
-         * {@inheritDoc}
-         */        
+        /** {@inheritDoc} */        
         public Property convertProperty(Property p, 
                                 PropertyList propertyList, FObj fo)
                         throws PropertyException {
@@ -84,7 +82,7 @@ public class LengthRangeProperty extends Property implements CompoundDatatype {
                         log.warn(FObj.decorateWithContextInfo(
                                 "Replaced negative value (" + len + ") for " + getName()
                                 + " with 0mpt", fo));
-                        p = new FixedLength(0);
+                        p = FixedLength.getInstance(0);
                     }
                 }
             }
@@ -107,7 +105,7 @@ public class LengthRangeProperty extends Property implements CompoundDatatype {
                         log.warn("Replaced negative value (" + len + ") for " + getName()
                                 + " with 0mpt");
                         val.setComponent(subpropertyId,
-                                new FixedLength(0), false);
+                                FixedLength.getInstance(0), false);
                         return baseProperty;
                     }
                 }