You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2021/10/12 06:11:45 UTC

svn commit: r1894144 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java

Author: tilman
Date: Tue Oct 12 06:11:44 2021
New Revision: 1894144

URL: http://svn.apache.org/viewvc?rev=1894144&view=rev
Log:
PDFBOX-5293: store x and y scaling factor

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1894144&r1=1894143&r2=1894144&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Tue Oct 12 06:11:44 2021
@@ -127,6 +127,8 @@ public class PageDrawer extends PDFGraph
     // the graphics device to draw to, xform is the initial transform of the device (i.e. DPI)
     private Graphics2D graphics;
     private AffineTransform xform;
+    private float xformScalingFactorX;
+    private float xformScalingFactorY;
     
     // the page box to draw (usually the crop box but may be another)
     private PDRectangle pageSize;
@@ -251,6 +253,9 @@ public class PageDrawer extends PDFGraph
     {
         graphics = (Graphics2D) g;
         xform = graphics.getTransform();
+        Matrix m = new Matrix(xform);
+        xformScalingFactorX = Math.abs(m.getScalingFactorX());
+        xformScalingFactorY = Math.abs(m.getScalingFactorY());
         initialClip = graphics.getClip();
         this.pageSize = pageSize;
 
@@ -635,12 +640,8 @@ public class PageDrawer extends PDFGraph
     //    (needs rendering identity testing before committing!)
     private void adjustRectangle(Rectangle2D r)
     {
-        Matrix m = new Matrix(xform);
-        float scaleX = Math.abs(m.getScalingFactorX());
-        float scaleY = Math.abs(m.getScalingFactorY());
-
         AffineTransform adjustedTransform = new AffineTransform(xform);
-        adjustedTransform.scale(1.0 / scaleX, 1.0 / scaleY);
+        adjustedTransform.scale(1.0 / xformScalingFactorX, 1.0 / xformScalingFactorY);
         r.setRect(adjustedTransform.createTransformedShape(r).getBounds2D());
     }
 
@@ -648,8 +649,7 @@ public class PageDrawer extends PDFGraph
     private BufferedImage adjustImage(BufferedImage gray)
     {
         AffineTransform at = new AffineTransform(xform);
-        Matrix m = new Matrix(at);
-        at.scale(1.0 / Math.abs(m.getScalingFactorX()), 1.0 / Math.abs(m.getScalingFactorY()));
+        at.scale(1.0 / xformScalingFactorX, 1.0 / xformScalingFactorY);
 
         Rectangle originalBounds = new Rectangle(gray.getWidth(), gray.getHeight());
         Rectangle2D transformedBounds = at.createTransformedShape(originalBounds).getBounds2D();
@@ -761,7 +761,6 @@ public class PageDrawer extends PDFGraph
         }
         if (JAVA_VERSION < 10)
         {
-            float scalingFactorX = new Matrix(xform).getScalingFactorX();
             for (int i = 0; i < dashArray.length; ++i)
             {
                 // apply the CTM
@@ -769,7 +768,7 @@ public class PageDrawer extends PDFGraph
                 // minimum line dash width avoids JVM crash,
                 // see PDFBOX-2373, PDFBOX-2929, PDFBOX-3204, PDFBOX-3813
                 // also avoid 0 in array like "[ 0 1000 ] 0 d", see PDFBOX-3724
-                if (scalingFactorX < 0.5f)
+                if (xformScalingFactorX < 0.5f)
                 {
                     // PDFBOX-4492
                     dashArray[i] = Math.max(w, 0.2f);
@@ -1041,10 +1040,9 @@ public class PageDrawer extends PDFGraph
             {
                 bim = pdImage.getImage();
             }
-            Matrix xformMatrix = new Matrix(xform);
             boolean isScaledUp =
-                    bim.getWidth() <= Math.round(ctm.getScalingFactorX() * xformMatrix.getScalingFactorX()) ||
-                    bim.getHeight() <= Math.round(ctm.getScalingFactorY() * xformMatrix.getScalingFactorY());
+                    bim.getWidth() <= Math.round(ctm.getScalingFactorX() * xformScalingFactorX) ||
+                    bim.getHeight() <= Math.round(ctm.getScalingFactorY() * xformScalingFactorY);
             if (isScaledUp)
             {
                 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
@@ -1509,18 +1507,13 @@ public class PageDrawer extends PDFGraph
 
         // both the DPI xform and the CTM were already applied to the group, so all we do
         // here is draw it directly onto the Graphics2D device at the appropriate position
-        PDRectangle bbox = group.getBBox();
         AffineTransform savedTransform = graphics.getTransform();
-
-        Matrix m = new Matrix(xform);
-        float xScale = Math.abs(m.getScalingFactorX());
-        float yScale = Math.abs(m.getScalingFactorY());
-        
         AffineTransform transform = new AffineTransform(xform);
-        transform.scale(1.0 / xScale, 1.0 / yScale);
+        transform.scale(1.0 / xformScalingFactorX, 1.0 / xformScalingFactorY);
         graphics.setTransform(transform);
 
         // adjust bbox (x,y) position at the initial scale + cropbox
+        PDRectangle bbox = group.getBBox();
         float x = bbox.getLowerLeftX() - pageSize.getLowerLeftX();
         float y = pageSize.getUpperRightY() - bbox.getUpperRightY();
 
@@ -1531,7 +1524,7 @@ public class PageDrawer extends PDFGraph
         }
         else
         {
-            graphics.translate(x * xScale, y * yScale);
+            graphics.translate(x * xformScalingFactorX, y * xformScalingFactorY);
         }
 
         PDSoftMask softMask = getGraphicsState().getSoftMask();
@@ -1542,7 +1535,7 @@ public class PageDrawer extends PDFGraph
             awtPaint = applySoftMaskToPaint(awtPaint, softMask);
             graphics.setPaint(awtPaint);
             graphics.fill(
-                    new Rectangle2D.Float(0, 0, bbox.getWidth() * xScale, bbox.getHeight() * yScale));
+                    new Rectangle2D.Float(0, 0, bbox.getWidth() * xformScalingFactorX, bbox.getHeight() * xformScalingFactorY));
         }
         else
         {
@@ -1574,8 +1567,6 @@ public class PageDrawer extends PDFGraph
         private final int maxY;
         private final int width;
         private final int height;
-        private final float scaleX;
-        private final float scaleY;
 
         /**
          * Creates a buffered image for a transparency group result.
@@ -1606,9 +1597,6 @@ public class PageDrawer extends PDFGraph
             Area transformed = new Area(transformedBox);
             transformed.intersect(getGraphicsState().getCurrentClippingPath());
             Rectangle2D clipRect = transformed.getBounds2D();
-            Matrix m = new Matrix(xform);
-            scaleX = Math.abs(m.getScalingFactorX());
-            scaleY = Math.abs(m.getScalingFactorY());
             if (clipRect.isEmpty())
             {
                 image = null;
@@ -1625,7 +1613,7 @@ public class PageDrawer extends PDFGraph
                                         (float)clipRect.getWidth(), (float)clipRect.getHeight());
 
             // apply the underlying Graphics2D device's DPI transform
-            AffineTransform dpiTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
+            AffineTransform dpiTransform = AffineTransform.getScaleInstance(xformScalingFactorX, xformScalingFactorY);
             Rectangle2D bounds = dpiTransform.createTransformedShape(clipRect).getBounds2D();
 
             minX = (int) Math.floor(bounds.getMinX());
@@ -1706,12 +1694,12 @@ public class PageDrawer extends PDFGraph
             g.transform(dpiTransform);
 
             AffineTransform xformOriginal = xform;
-            xform = AffineTransform.getScaleInstance(scaleX, scaleY);
+            xform = AffineTransform.getScaleInstance(xformScalingFactorX, xformScalingFactorY);
             PDRectangle pageSizeOriginal = pageSize;
-            pageSize = new PDRectangle(minX / scaleX, 
-                                       minY / scaleY,
-                        (float) bounds.getWidth() / scaleX,
-                        (float) bounds.getHeight() / scaleY);
+            pageSize = new PDRectangle(minX / xformScalingFactorX,
+                                       minY / xformScalingFactorY,
+                                       (float) (bounds.getWidth() / xformScalingFactorX),
+                                        (float) (bounds.getHeight() / xformScalingFactorY));
             int clipWindingRuleOriginal = clipWindingRule;
             clipWindingRule = -1;
             GeneralPath linePathOriginal = linePath;
@@ -1815,13 +1803,9 @@ public class PageDrawer extends PDFGraph
 
         public Rectangle2D getBounds()
         {
-            Point2D size = new Point2D.Double(pageSize.getWidth(), pageSize.getHeight());
             // apply the underlying Graphics2D device's DPI transform and y-axis flip
-            AffineTransform dpiTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
-            size = dpiTransform.transform(size, size);
-            // Flip y
-            return new Rectangle2D.Double(minX - pageSize.getLowerLeftX() * scaleX,
-                    size.getY() - minY - height + pageSize.getLowerLeftY() * scaleY,
+            return new Rectangle2D.Double(minX - pageSize.getLowerLeftX() * xformScalingFactorX,
+                    (pageSize.getLowerLeftY() + pageSize.getHeight()) * xformScalingFactorY - minY - height,
                     width, height);
         }
     }