pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1762519 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
Date Tue, 27 Sep 2016 17:08:38 GMT
Author: tilman
Date: Tue Sep 27 17:08:37 2016
New Revision: 1762519

URL: http://svn.apache.org/viewvc?rev=1762519&view=rev
Log:
PDFBOX-3000: consider page rotation, cropbox, initial translation; use Matrix class to get
correct scales even for rotated affine transforms

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=1762519&r1=1762518&r2=1762519&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 Sep
27 17:08:37 2016
@@ -1064,8 +1064,59 @@ public class PageDrawer extends PDFGraph
         AffineTransform prev = graphics.getTransform();
         float x = bbox.getLowerLeftX();
         float y = pageSize.getHeight() - bbox.getLowerLeftY() - bbox.getHeight();
-        graphics.setTransform(AffineTransform.getTranslateInstance(x * xform.getScaleX(),
-                                                                   y * xform.getScaleY()));
+
+        Matrix m = new Matrix(graphics.getTransform());
+        switch (getPage().getRotation())
+        {
+            case 0:
+            default:
+                // set to the initial translation plus cropbox, and
+                // adjust apply (x,y) at the initial scale
+                // however... what if the initial xform was e.g. rotated at 45°?
+                graphics.setTransform(AffineTransform.getTranslateInstance(
+                        xform.getTranslateX() + (x - pageSize.getLowerLeftX()) * xform.getScaleX(),
+                        xform.getTranslateY() + (y + pageSize.getLowerLeftY()) * xform.getScaleY()));
+                break;
+            case 90:
+                graphics.setTransform(new AffineTransform());
+
+                // adjust the initial translation (includes the translation used to "help"
the rotation)
+                graphics.translate(xform.getTranslateX(), xform.getTranslateY());
+
+                graphics.rotate(Math.toRadians(90));
+
+                graphics.translate(x * m.getScalingFactorX(), y * m.getScalingFactorY());
+
+                // adjust cropbox
+                graphics.translate(-(pageSize.getLowerLeftX()) * m.getScalingFactorX(), (pageSize.getLowerLeftY())
* m.getScalingFactorY());
+                break;
+            case 180:
+                graphics.setTransform(new AffineTransform());
+
+                // adjust the initial translation (includes the translation used to "help"
the rotation)
+                graphics.translate(xform.getTranslateX(), xform.getTranslateY());
+
+                graphics.rotate(Math.toRadians(180));
+
+                graphics.translate(x * m.getScalingFactorX(), y * m.getScalingFactorY());
+
+                // adjust cropbox
+                graphics.translate(-(pageSize.getLowerLeftX()) * m.getScalingFactorX(), (pageSize.getLowerLeftY())
* m.getScalingFactorY());
+                break;
+            case 270:
+                graphics.setTransform(new AffineTransform());
+
+                // adjust the initial translation (includes the translation used to "help"
the rotation)
+                graphics.translate(xform.getTranslateX(), xform.getTranslateY());
+
+                graphics.rotate(Math.toRadians(270));
+
+                graphics.translate(x * m.getScalingFactorX(), y * m.getScalingFactorY());
+
+                // adjust cropbox
+                graphics.translate(-(pageSize.getLowerLeftX()) * m.getScalingFactorX(), (pageSize.getLowerLeftY())
* m.getScalingFactorY());
+                break;
+        }
 
         PDSoftMask softMask = getGraphicsState().getSoftMask();
         if (softMask != null)
@@ -1102,8 +1153,6 @@ public class PageDrawer extends PDFGraph
         private final BufferedImage image;
         private final PDRectangle bbox;
 
-        private final int minX;
-        private final int minY;
         private final int width;
         private final int height;
 
@@ -1133,8 +1182,8 @@ public class PageDrawer extends PDFGraph
             Shape deviceClip = xform.createTransformedShape(clip);
             Rectangle2D bounds = deviceClip.getBounds2D();
 
-            minX = (int) Math.floor(bounds.getMinX());
-            minY = (int) Math.floor(bounds.getMinY());
+            int minX = (int) Math.floor(bounds.getMinX());
+            int minY = (int) Math.floor(bounds.getMinY());
             int maxX = (int) Math.floor(bounds.getMaxX()) + 1;
             int maxY = (int) Math.floor(bounds.getMaxY()) + 1;
 
@@ -1145,11 +1194,22 @@ public class PageDrawer extends PDFGraph
             Graphics2D g = image.createGraphics();
 
             // flip y-axis
-            g.translate(0, height);
-            g.scale(1, -1);
+            int rotation = getPage().getRotation();
+            if (rotation == 0 || rotation == 180)
+            {
+                g.translate(0, height);
+                g.scale(1, -1);
+            }
+            else
+            {
+                g.translate(0, width);
+                g.scale(1, -1);
+            }
 
             // apply device transform (DPI)
-            g.transform(xform);
+            // the initial translation is ignored, because we're not writing into the initial
graphics device
+            Matrix m = new Matrix(xform);
+            g.scale(m.getScalingFactorX(), m.getScalingFactorY());
 
             // adjust the origin
             g.translate(-clipRect.getX(), -clipRect.getY());



Mime
View raw message