xmlgraphics-fop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerem...@apache.org
Subject svn commit: r675592 - in /xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp: AFPRenderer.java AFPState.java
Date Thu, 10 Jul 2008 14:34:25 GMT
Author: jeremias
Date: Thu Jul 10 07:34:25 2008
New Revision: 675592

URL: http://svn.apache.org/viewvc?rev=675592&view=rev
Log:
Fixed image positioning for RenderedImages.
Created a common method for converting millipoints to units to improve code readability and
to fix rounding errors because some of that conversion were using integer division which lead
to different results than floating point division.

Modified:
    xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPRenderer.java
    xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPState.java

Modified: xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPRenderer.java?rev=675592&r1=675591&r2=675592&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPRenderer.java
(original)
+++ xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPRenderer.java
Thu Jul 10 07:34:25 2008
@@ -133,18 +133,18 @@
  * handle all types of inline area, text, image etc and draws various lines and
  * rectangles.
  * </p>
- * 
+ *
  * Note: There are specific extensions that have been added to the FO. They are
  * specific to their location within the FO and have to be processed accordingly
  * (ie. at the start or end of the page).
- * 
+ *
  */
 public class AFPRenderer extends AbstractPathOrientedRenderer {
 
     private static final int X = 0;
-    
+
     private static final int Y = 1;
-    
+
     private static final int X1 = 0;
 
     private static final int Y1 = 1;
@@ -297,7 +297,7 @@
     public Graphics2DAdapter getGraphics2DAdapter() {
         return new AFPGraphics2DAdapter();
     }
-    
+
     /** {@inheritDoc} */
     public void startVParea(CTM ctm, Rectangle2D clippingRect) {
         saveGraphicsState();
@@ -306,9 +306,9 @@
             concatenateTransformationMatrix(at);
         }
         if (clippingRect != null) {
-            clipRect((float)clippingRect.getX() / 1000f, 
-                    (float)clippingRect.getY() / 1000f, 
-                    (float)clippingRect.getWidth() / 1000f, 
+            clipRect((float)clippingRect.getX() / 1000f,
+                    (float)clippingRect.getY() / 1000f,
+                    (float)clippingRect.getWidth() / 1000f,
                     (float)clippingRect.getHeight() / 1000f);
         }
     }
@@ -330,10 +330,10 @@
         currentState.clear();
 
         Rectangle2D bounds = pageViewport.getViewArea();
-        
+
         AffineTransform basicPageTransform = new AffineTransform();
         int resolution = currentState.getResolution();
-        double scale = (double)1 / (AFPConstants.DPI_72_MPTS / resolution);
+        double scale = mpt2units(1);
         basicPageTransform.scale(scale, scale);
 
         currentState.concatenate(basicPageTransform);
@@ -343,10 +343,10 @@
                     (PageObject)getPages().remove(pageViewport));
         } else {
             int pageWidth
-                = (int)Math.round(bounds.getWidth() / (AFPConstants.DPI_72_MPTS / resolution));
+                = (int)Math.round(mpt2units((float)bounds.getWidth()));
             currentState.setPageWidth(pageWidth);
             int pageHeight
-                = (int)Math.round(bounds.getHeight() / (AFPConstants.DPI_72_MPTS / resolution));
+                = (int)Math.round(mpt2units((float)bounds.getHeight()));
             currentState.setPageHeight(pageHeight);
 
             final int pageRotation = 0;
@@ -355,7 +355,7 @@
 
             renderPageObjectExtensions(pageViewport);
         }
-        
+
         super.renderPage(pageViewport);
 
         AFPPageFonts pageFonts = currentState.getPageFonts();
@@ -412,14 +412,17 @@
         return transformPoints(srcPts, null, false);
     }
 
+    private float mpt2units(float mpt) {
+        return mpt / ((float)AFPConstants.DPI_72_MPTS / currentState.getResolution());
+    }
+
     /** {@inheritDoc} */
     public void fillRect(float x, float y, float width, float height) {
         float[] srcPts = new float[] {x * 1000, y * 1000};
         float[] dstPts = new float[srcPts.length];
-        int[] coords = mpts2units(srcPts, dstPts);        
-        int resolution = currentState.getResolution();
-        int x2 = Math.round(dstPts[X] + ((width * 1000) / (AFPConstants.DPI_72_MPTS / resolution)));
-        int thickness = Math.round((height * 1000) / (AFPConstants.DPI_72_MPTS / resolution));
+        int[] coords = mpts2units(srcPts, dstPts);
+        int x2 = coords[X] + Math.round(mpt2units(width * 1000));
+        int thickness = Math.round(mpt2units(height * 1000));
         getAFPDataStream().createLine(
                 coords[X],
                 coords[Y],
@@ -575,7 +578,7 @@
                         Math.round(w3),
                         leftcol);
                 afpDataStream.createLine(
-                        Math.round(xm1 + w3), 
+                        Math.round(xm1 + w3),
                         coords[Y1],
                         Math.round(xm1 + w3),
                         coords[Y2],
@@ -634,7 +637,7 @@
         Point origin = new Point(currentIPPosition, currentBPPosition);
         int x = origin.x + posInt.x;
         int y = origin.y + posInt.y;
-        
+
         String name = (String)getPageSegments().get(uri);
         if (name != null) {
             float[] srcPts = {x, y};
@@ -687,23 +690,19 @@
                     imageObjectInfo.setBuffered(false);
                     imageObjectInfo.setUri(uri);
                     imageObjectInfo.setMimeType(mimeType);
-                    
+
                     ObjectAreaInfo objectAreaInfo = new ObjectAreaInfo();
                     objectAreaInfo.setX(coords[X]);
                     objectAreaInfo.setY(coords[Y]);
                     int resolution = currentState.getResolution();
-                    int w = Math.round(
-                            ((float)posInt.getWidth() * 1000)
-                            / (AFPConstants.DPI_72_MPTS / resolution));
-                    int h = Math.round(
-                            ((float)posInt.getHeight() * 1000)
-                            / (AFPConstants.DPI_72_MPTS / resolution));
+                    int w = Math.round(mpt2units((float)posInt.getWidth() * 1000));
+                    int h = Math.round(mpt2units((float)posInt.getHeight() * 1000));
                     objectAreaInfo.setWidth(w);
                     objectAreaInfo.setHeight(h);
                     objectAreaInfo.setWidthRes(resolution);
                     objectAreaInfo.setHeightRes(resolution);
                     imageObjectInfo.setObjectAreaInfo(objectAreaInfo);
-                    
+
                     imageObjectInfo.setData(buf);
                     imageObjectInfo.setDataHeight(ccitt.getSize().getHeightPx());
                     imageObjectInfo.setDataWidth(ccitt.getSize().getWidthPx());
@@ -746,7 +745,7 @@
 
     /**
      * Writes a RenderedImage to an OutputStream as raw sRGB bitmaps.
-     * 
+     *
      * @param image
      *            the RenderedImage
      * @param out
@@ -763,7 +762,7 @@
 
     /**
      * Draws a BufferedImage to AFP.
-     * 
+     *
      * @param imageInfo
      *            the image info
      * @param image
@@ -811,16 +810,11 @@
         int[] coords = mpts2units(srcPts);
         objectAreaInfo.setX(coords[X]);
         objectAreaInfo.setY(coords[Y]);
-        int resolution = currentState.getResolution();
-        int w = Math.round(
-                (width * 1000)
-                / (AFPConstants.DPI_72_MPTS / resolution));
-        int h = Math.round(
-                (height * 1000)
-                / (AFPConstants.DPI_72_MPTS / resolution));
+        int w = Math.round(mpt2units(width));
+        int h = Math.round(mpt2units(height));
         objectAreaInfo.setWidth(w);
         objectAreaInfo.setHeight(h);
-        
+
         objectAreaInfo.setWidthRes(imageRes);
         objectAreaInfo.setHeightRes(imageRes);
         imageObjectInfo.setObjectAreaInfo(objectAreaInfo);
@@ -896,7 +890,7 @@
     }
 
     /** {@inheritDoc} */
-    public void renderText(TextArea text) { 
+    public void renderText(TextArea text) {
         log.debug(text.getText());
         renderInlineAreaBackAndBorders(text);
 
@@ -956,11 +950,9 @@
         int variableSpaceCharacterIncrement = font.getWidth(' ', fontSize) / 1000
           + text.getTextWordSpaceAdjust()
           + text.getTextLetterSpaceAdjust();
-        int resolution = currentState.getResolution();
-        variableSpaceCharacterIncrement /= (AFPConstants.DPI_72_MPTS / resolution);
-      
-        int interCharacterAdjustment = text.getTextLetterSpaceAdjust();
-        interCharacterAdjustment /= (AFPConstants.DPI_72_MPTS / resolution);
+        variableSpaceCharacterIncrement = Math.round(mpt2units(variableSpaceCharacterIncrement));
+
+        int interCharacterAdjustment = Math.round(mpt2units(text.getTextLetterSpaceAdjust()));
 
         AFPTextDataInfo textDataInfo = new AFPTextDataInfo();
         textDataInfo.setFontReference(fontReference);
@@ -983,7 +975,7 @@
     /**
      * Render leader area. This renders a leader area which is an area with a
      * rule.
-     * 
+     *
      * @param area
      *            the leader area to render
      */
@@ -1018,7 +1010,7 @@
     /**
      * Sets the rotation to be used for portrait pages, valid values are 0
      * (default), 90, 180, 270.
-     * 
+     *
      * @param rotation
      *            The rotation in degrees.
      */
@@ -1029,7 +1021,7 @@
     /**
      * Sets the rotation to be used for landsacpe pages, valid values are 0, 90,
      * 180, 270 (default).
-     * 
+     *
      * @param rotation
      *            The rotation in degrees.
      */
@@ -1039,7 +1031,7 @@
 
     /**
      * Get the MIME type of the renderer.
-     * 
+     *
      * @return The MIME type of the renderer
      */
     public String getMimeType() {
@@ -1056,7 +1048,7 @@
     /**
      * Method to render the page extension.
      * <p>
-     * 
+     *
      * @param pageViewport
      *            the page object
      */
@@ -1103,7 +1095,7 @@
 
     /**
      * Sets the number of bits used per pixel
-     * 
+     *
      * @param bitsPerPixel
      *            number of bits per pixel
      */
@@ -1113,7 +1105,7 @@
 
     /**
      * Sets whether images are color or not
-     * 
+     *
      * @param colorImages
      *            color image output
      */
@@ -1123,7 +1115,7 @@
 
     /**
      * Returns the AFPDataStream
-     * 
+     *
      * @return the AFPDataStream
      */
     public AFPDataStream getAFPDataStream() {
@@ -1135,7 +1127,7 @@
 
     /**
      * Sets the output/device resolution
-     * 
+     *
      * @param resolution
      *            the output resolution (dpi)
      */
@@ -1145,7 +1137,7 @@
 
     /**
      * Returns the output/device resolution.
-     * 
+     *
      * @return the resolution in dpi
      */
     public int getResolution() {
@@ -1176,8 +1168,8 @@
     protected boolean isGOCAEnabled() {
         return this.gocaEnabled;
     }
-    
-    // TODO: remove this and use the superclass implementation 
+
+    // TODO: remove this and use the superclass implementation
     /** {@inheritDoc} */
     protected void renderReferenceArea(Block block) {
         // save position and offset
@@ -1189,7 +1181,7 @@
         at.translate(currentIPPosition, currentBPPosition);
         at.translate(block.getXOffset(), block.getYOffset());
         at.translate(0, block.getSpaceBefore());
-        
+
         if (!at.isIdentity()) {
             saveGraphicsState();
             concatenateTransformationMatrix(at);
@@ -1207,7 +1199,7 @@
         if (!at.isIdentity()) {
             restoreGraphicsState();
         }
-        
+
         // stacked and relative blocks effect stacking
         currentIPPosition = saveIP;
         currentBPPosition = saveBP;
@@ -1231,6 +1223,7 @@
             }
             coords[i] = Math.round(dstPts[i]);
         }
-        return coords;        
+        return coords;
     }
+
 }

Modified: xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPState.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPState.java?rev=675592&r1=675591&r2=675592&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPState.java
(original)
+++ xmlgraphics/fop/branches/Temp_AFPGOCAResources/src/java/org/apache/fop/render/afp/AFPState.java
Thu Jul 10 07:34:25 2008
@@ -5,9 +5,9 @@
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -55,14 +55,14 @@
     private int resolution = 240; // 240 dpi
 
     /**
-     * The current page 
+     * The current page
      */
     private AFPPageState pageState = new AFPPageState();
-    
+
     /**
      * Sets the rotation to be used for portrait pages, valid values are 0
      * (default), 90, 180, 270.
-     * 
+     *
      * @param rotation
      *            The rotation in degrees.
      */
@@ -88,7 +88,7 @@
     /**
      * Sets the rotation to be used for landscape pages, valid values are 0, 90,
      * 180, 270 (default).
-     * 
+     *
      * @param rotation
      *            The rotation in degrees.
      */
@@ -112,20 +112,20 @@
 
     /**
      * Sets the number of bits used per pixel
-     * 
+     *
      * @param bitsPerPixel
      *            number of bits per pixel
      */
     public void setBitsPerPixel(int bitsPerPixel) {
-        this.bitsPerPixel = bitsPerPixel;
         switch (bitsPerPixel) {
         case 1:
         case 4:
         case 8:
+            this.bitsPerPixel = bitsPerPixel;
             break;
         default:
             log.warn("Invalid bits_per_pixel value, must be 1, 4 or 8.");
-            bitsPerPixel = 8;
+            this.bitsPerPixel = 8;
             break;
         }
     }
@@ -139,7 +139,7 @@
 
     /**
      * Sets whether images are color or not
-     * 
+     *
      * @param colorImages
      *            color image output
      */
@@ -156,7 +156,7 @@
 
     /**
      * Sets the output/device resolution
-     * 
+     *
      * @param resolution
      *            the output resolution (dpi)
      */
@@ -169,7 +169,7 @@
 
     /**
      * Returns the output/device resolution.
-     * 
+     *
      * @return the resolution in dpi
      */
     protected int getResolution() {
@@ -187,7 +187,7 @@
     protected AFPPageState getPageState() {
         return this.pageState;
     }
-    
+
     /**
      * Sets if the current painted shape is to be filled
      * @param fill true if the current painted shape is to be filled
@@ -261,7 +261,7 @@
      */
     public String getImageUri() {
         return ((AFPData)getData()).imageUri;
-    }    
+    }
 
     /** {@inheritDoc} */
     public String toString() {
@@ -275,7 +275,7 @@
     }
 
     /**
-     * Page level state data 
+     * Page level state data
      */
     private class AFPPageState {
         /** The current page width */
@@ -341,7 +341,7 @@
         protected int incrementFontCount() {
             return ++fontCount;
         }
-        
+
         /** {@inheritDoc} */
         public String toString() {
             return "AFPPageState{width=" + width
@@ -360,9 +360,9 @@
 
         /** The current fill status */
         private boolean filled = false;
-        
+
         private String imageUri = null;
-        
+
         /** {@inheritDoc} */
         public Object clone() throws CloneNotSupportedException {
             AFPData obj = (AFPData)super.clone();
@@ -370,7 +370,7 @@
             obj.imageUri = this.imageUri;
             return obj;
         }
-        
+
         /** {@inheritDoc} */
         public String toString() {
             return "AFPData{" + super.toString()



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Mime
View raw message