Skip to content

Commit

Permalink
PDFBOX-758: fixed rotation algo if the rotation angle isn't a multipl…
Browse files Browse the repository at this point in the history
…e of 90 degrees.

git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@957289 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lehmi committed Jun 23, 2010
1 parent f0d4395 commit 7dd6ba2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,21 @@ public void process(PDFOperator operator, List<COSBase> arguments) throws IOExc
ctmAT.scale(1f/imageWidth, 1f/imageHeight);
Matrix rotationMatrix = new Matrix();
rotationMatrix.setFromAffineTransform( ctmAT );
// calculate the inverse rotation angle
// scaleX = m00 = cos
// shearX = m01 = -sin
// tan = sin/cos
double angle = Math.atan(ctmAT.getShearX()/ctmAT.getScaleX());
Matrix translationMatrix = null;
if (pageRotation == 0 || pageRotation == 180)
{
rotationMatrix.setValue(2,1,(float)pageHeight-ctm.getYPosition()-ctm.getYScale());
translationMatrix = Matrix.getTranslatingInstance((float)(Math.sin(angle)*ctm.getXScale()), (float)(pageHeight-2*ctm.getYPosition()-Math.cos(angle)*ctm.getYScale()));
}
else if (pageRotation == 90 || pageRotation == 270)
{
rotationMatrix.setValue(2,0,(float)ctm.getXPosition()-ctm.getYScale());
rotationMatrix.setValue(2,1,(float)pageHeight-ctm.getYPosition());
translationMatrix = Matrix.getTranslatingInstance((float)(Math.sin(angle)*ctm.getYScale()), (float)(pageHeight-2*ctm.getYPosition()));
}
rotationMatrix = rotationMatrix.multiply(translationMatrix);
rotationMatrix.setValue(0, 1, (-1)*rotationMatrix.getValue(0, 1));
rotationMatrix.setValue(1, 0, (-1)*rotationMatrix.getValue(1, 0));
AffineTransform at = new AffineTransform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void process(PDFOperator operator, List<COSBase> arguments) throws IOExce
int imageHeight = awtImage.getHeight();
double pageHeight = drawer.getPageSize().getHeight();

log.info("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight);
log.debug("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight);

Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
int pageRotation = page.findRotation();
Expand All @@ -90,21 +90,27 @@ public void process(PDFOperator operator, List<COSBase> arguments) throws IOExce
ctmAT.scale(1f/imageWidth, 1f/imageHeight);
Matrix rotationMatrix = new Matrix();
rotationMatrix.setFromAffineTransform( ctmAT );
// calculate the inverse rotation angle
// scaleX = m00 = cos
// shearX = m01 = -sin
// tan = sin/cos
double angle = Math.atan(ctmAT.getShearX()/ctmAT.getScaleX());
Matrix translationMatrix = null;
if (pageRotation == 0 || pageRotation == 180)
{
rotationMatrix.setValue(2,1,(float)pageHeight-ctm.getYPosition()-ctm.getYScale());
translationMatrix = Matrix.getTranslatingInstance((float)(Math.sin(angle)*ctm.getXScale()), (float)(pageHeight-2*ctm.getYPosition()-Math.cos(angle)*ctm.getYScale()));
}
else if (pageRotation == 90 || pageRotation == 270)
{
rotationMatrix.setValue(2,0,(float)ctm.getXPosition()-ctm.getYScale());
rotationMatrix.setValue(2,1,(float)pageHeight-ctm.getYPosition());
translationMatrix = Matrix.getTranslatingInstance((float)(Math.sin(angle)*ctm.getYScale()), (float)(pageHeight-2*ctm.getYPosition()));
}
rotationMatrix = rotationMatrix.multiply(translationMatrix);
rotationMatrix.setValue(0, 1, (-1)*rotationMatrix.getValue(0, 1));
rotationMatrix.setValue(1, 0, (-1)*rotationMatrix.getValue(1, 0));

AffineTransform at = new AffineTransform(
rotationMatrix.getValue(0,0),rotationMatrix.getValue(0,1),
rotationMatrix.getValue(1,0), rotationMatrix.getValue( 1, 1),
rotationMatrix.getValue(1,0),rotationMatrix.getValue(1,1),
rotationMatrix.getValue(2,0),rotationMatrix.getValue(2,1)
);

Expand Down

0 comments on commit 7dd6ba2

Please sign in to comment.