pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1833043 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
Date Wed, 06 Jun 2018 15:27:26 GMT
Author: tilman
Date: Wed Jun  6 15:27:26 2018
New Revision: 1833043

URL: http://svn.apache.org/viewvc?rev=1833043&view=rev
Log:
PDFBOX-3353: support undocumented /Rotate setting; make width smaller

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java?rev=1833043&r1=1833042&r2=1833043&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDFreeTextAppearanceHandler.java
Wed Jun  6 15:27:26 2018
@@ -23,6 +23,7 @@ import org.apache.fontbox.util.Charsets;
 import org.apache.pdfbox.contentstream.operator.Operator;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSNumber;
 import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.pdfparser.PDFStreamParser;
@@ -205,7 +206,13 @@ public class PDFreeTextAppearanceHandler
             }
             cs.drawShape(ab.width, hasStroke, hasBackground);
 
-
+            // rotation is an undocumented feature, but Adobe uses it. Examples can be found
+            // in pdf_commenting_new.pdf file.
+            int rotation = annotation.getCOSObject().getInt(COSName.ROTATE, 0);
+            cs.transform(Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0));
+            float xOffset;
+            float yOffset;
+            float width = rotation == 90 || rotation == 270 ? getRectangle().getHeight():
getRectangle().getWidth();
             // somewhat inspired by AppearanceGeneratorHelper.insertGeneratedAppearance()
             cs.beginText();
             PDFont font = PDType1Font.HELVETICA;
@@ -215,6 +222,26 @@ public class PDFreeTextAppearanceHandler
                 factor = 2;
             }
             float fontSize = extractFontSize(annotation);
+            switch (rotation)
+            {
+                case 180:
+                    xOffset = - getRectangle().getUpperRightX() + fontSize / 2 * factor;

+                    yOffset = - getRectangle().getLowerLeftY() - font.getBoundingBox().getHeight()
* fontSize / 1000 * factor;
+                    break;
+                case 90:
+                    xOffset = getRectangle().getLowerLeftY() + fontSize / 2 * factor;
+                    yOffset = - getRectangle().getLowerLeftX() - font.getBoundingBox().getHeight()
* fontSize / 1000 * factor;
+                    break;
+                case 270:
+                    xOffset = - getRectangle().getUpperRightY() + fontSize / 2 * factor;
+                    yOffset = getRectangle().getUpperRightX() - font.getBoundingBox().getHeight()
* fontSize / 1000 * factor;
+                    break;
+                case 0:
+                default:
+                    xOffset = getRectangle().getLowerLeftX() + fontSize / 2 * factor;
+                    yOffset = getRectangle().getUpperRightY() - font.getBoundingBox().getHeight()
* fontSize / 1000 * factor;
+                    break;
+            }
             cs.setFont(font, fontSize);
             cs.setNonStrokingColor(strokingColor);
             AppearanceStyle appearanceStyle = new AppearanceStyle();
@@ -223,12 +250,10 @@ public class PDFreeTextAppearanceHandler
             PlainTextFormatter formatter = new PlainTextFormatter.Builder(cs)
                     .style(appearanceStyle)
                     .text(new PlainText(annotation.getContents()))
-                    .width(getRectangle().getWidth())
+                    .width(width - fontSize / factor)
                     .wrapLines(true)
                     //TODO some reverse engineering needed to find out padding
-                    //TODO fat cloudy rectangle in CTAN file has "the" incomplete
-                    .initialOffset(getRectangle().getLowerLeftX() + fontSize / 2 * factor,

-                                   getRectangle().getUpperRightY() - font.getBoundingBox().getHeight()
* fontSize / 1000 * factor)
+                    .initialOffset(xOffset, yOffset)
                     // Adobe ignores the /Q
                     //.textAlign(annotation.getQ())
                     .build();



Mime
View raw message