You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2019/06/16 16:13:56 UTC

svn commit: r1861467 - in /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation: PDAnnotationMarkup.java handlers/PDCaretAppearanceHandler.java

Author: msahyoun
Date: Sun Jun 16 16:13:56 2019
New Revision: 1861467

URL: http://svn.apache.org/viewvc?rev=1861467&view=rev
Log:
PDFBOX-4574: add support for Caret  appearance handler; simplify code

Added:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDCaretAppearanceHandler.java
Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java?rev=1861467&r1=1861466&r2=1861467&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java Sun Jun 16 16:13:56 2019
@@ -22,12 +22,15 @@ import java.util.Calendar;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.cos.COSString;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
 import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDAppearanceHandler;
+import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDCaretAppearanceHandler;
 import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDPolygonAppearanceHandler;
+import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDPolylineAppearanceHandler;
 
 /**
  * This class represents the additional fields of a Markup type Annotation. See section 12.5.6 of ISO32000-1:2008
@@ -417,6 +420,54 @@ public class PDAnnotationMarkup extends
     }
 
     /**
+     * This will set the difference between the annotations "outer" rectangle defined by
+     * /Rect and boundaries of the underlying.
+     * 
+     * <p>This will set an equal difference for all sides</p>
+     * 
+     * @param difference from the annotations /Rect entry
+     */
+    public void setRectDifferences(float difference) {
+        setRectDifferences(difference, difference, difference, difference);
+    }
+    
+    /**
+     * This will set the difference between the annotations "outer" rectangle defined by
+     * /Rect and the border.
+     * 
+     * @param differenceLeft left difference from the annotations /Rect entry
+     * @param differenceTop top difference from the annotations /Rect entry
+     * @param differenceRight right difference from  the annotations /Rect entry
+     * @param differenceBottom bottom difference from the annotations /Rect entry
+     * 
+     */
+    public void setRectDifferences(float differenceLeft, float differenceTop, float differenceRight, float differenceBottom)
+    {
+        COSArray margins = new COSArray();
+        margins.add(new COSFloat(differenceLeft));
+        margins.add(new COSFloat(differenceTop));
+        margins.add(new COSFloat(differenceRight));
+        margins.add(new COSFloat(differenceBottom));
+        getCOSObject().setItem(COSName.RD, margins);    
+    }
+    
+    /**
+     * This will get the margin between the annotations "outer" rectangle defined by
+     * /Rect and the boundaries of the underlying caret.
+     * 
+     * @return the differences. If the entry hasn't been set am empty array is returned.
+     */
+    public float[] getRectDifferences()
+    {
+        COSBase margin = getCOSObject().getItem(COSName.RD);
+        if (margin instanceof COSArray)
+        {
+            return ((COSArray) margin).toFloatArray();
+        }
+        return new float[]{};
+    }
+
+    /**
      * This will set the line ending style for the start point, see the LE_ constants for the possible values.
      *
      * @param style The new style.
@@ -572,14 +623,22 @@ public class PDAnnotationMarkup extends
     {
         if (customAppearanceHandler == null)
         {
+            PDAppearanceHandler appearanceHandler = null;
+            if (SUB_TYPE_CARET.equals(getSubtype()))
+            {
+                appearanceHandler = new PDCaretAppearanceHandler(this);
+            }
             if (SUB_TYPE_POLYGON.equals(getSubtype()))
             {
-                PDPolygonAppearanceHandler appearanceHandler = new PDPolygonAppearanceHandler(this);
-                appearanceHandler.generateAppearanceStreams();
+                appearanceHandler = new PDPolygonAppearanceHandler(this);
             }
             else if (SUB_TYPE_POLYLINE.equals(getSubtype()))
             {
-                PDPolygonAppearanceHandler appearanceHandler = new PDPolygonAppearanceHandler(this);
+                appearanceHandler = new PDPolylineAppearanceHandler(this);
+            }
+
+            if (appearanceHandler != null)
+            {
                 appearanceHandler.generateAppearanceStreams();
             }
         }

Added: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDCaretAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDCaretAppearanceHandler.java?rev=1861467&view=auto
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDCaretAppearanceHandler.java (added)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDCaretAppearanceHandler.java Sun Jun 16 16:13:56 2019
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2018 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.interactive.annotation.handlers;
+
+import java.io.IOException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.io.IOUtils;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup;
+import org.apache.pdfbox.pdmodel.PDAppearanceContentStream;
+import org.apache.pdfbox.util.Matrix;
+
+/**
+ * Handler to generate the caret annotations appearance.
+ *
+ * @author Tilman Hausherr
+ */
+public class PDCaretAppearanceHandler extends PDAbstractAppearanceHandler
+{
+    private static final Log LOG = LogFactory.getLog(PDCaretAppearanceHandler.class);
+
+    public PDCaretAppearanceHandler(PDAnnotation annotation)
+    {
+        super(annotation);
+    }
+
+    @Override
+    public void generateAppearanceStreams()
+    {
+        generateNormalAppearance();
+        generateRolloverAppearance();
+        generateDownAppearance();
+    }
+
+    @Override
+    public void generateNormalAppearance()
+    {
+        PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
+        PDAppearanceContentStream contentStream = null;
+
+        try
+        {
+            contentStream = getNormalAppearanceAsContentStream();
+
+            contentStream.setStrokingColor(getColor());
+            contentStream.setNonStrokingColor(getColor());
+
+            setOpacity(contentStream, annotation.getConstantOpacity());
+
+            PDRectangle rect = getRectangle();
+            PDRectangle bbox = new PDRectangle(rect.getWidth(), rect.getHeight());
+            if (!annotation.getCOSObject().containsKey(COSName.RD))
+            {
+                // Adobe creates the /RD entry with a number that is decided
+                // by dividing the height by 10, with a maximum result of 5.
+                // That number is then used to enlarge the bbox and the rectangle and added to the
+                // translation values in the matrix and also used for the line width
+                // (not here because it has no effect, see comment near fill() ).
+                // The curves are based on the original rectangle.
+                float rd = Math.min(rect.getHeight() / 10, 5);
+                annotation.setRectDifferences(rd);
+                bbox = new PDRectangle(-rd, -rd, rect.getWidth() + 2 * rd, rect.getHeight() + 2 * rd);
+                Matrix matrix = annotation.getNormalAppearanceStream().getMatrix();
+                matrix.transformPoint(rd, rd);
+                annotation.getNormalAppearanceStream().setMatrix(matrix.createAffineTransform());
+                PDRectangle rect2 = new PDRectangle(rect.getLowerLeftX() - rd, rect.getLowerLeftY() - rd,
+                                                    rect.getWidth() + 2 * rd, rect.getHeight() + 2 * rd);
+                annotation.setRectangle(rect2);
+            }
+            annotation.getNormalAppearanceStream().setBBox(bbox);
+
+            float halfX = rect.getWidth() / 2;
+            float halfY = rect.getHeight() / 2;
+            contentStream.moveTo(0, 0);
+            contentStream.curveTo(halfX, 0,
+                                  halfX, halfY, 
+                                  halfX, rect.getHeight());
+            contentStream.curveTo(halfX, halfY, 
+                                  halfX, 0,
+                                  rect.getWidth(), 0);
+            contentStream.closePath();
+            contentStream.fill();
+            // Adobe has an additional stroke, but it has no effect
+            // because fill "consumes" the path.
+        }
+        catch (IOException e)
+        {
+            LOG.error(e);
+        }
+        finally
+        {
+            IOUtils.closeQuietly(contentStream);
+        }
+    }
+
+    @Override
+    public void generateRolloverAppearance()
+    {
+        // TODO to be implemented
+    }
+
+    @Override
+    public void generateDownAppearance()
+    {
+        // TODO to be implemented
+    }
+}
\ No newline at end of file