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 2016/05/26 20:39:49 UTC

svn commit: r1745641 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation: PDAnnotationLink.java handlers/PDAbstractAppearanceHandler.java handlers/PDLinkAppearanceHandler.java

Author: msahyoun
Date: Thu May 26 20:39:49 2016
New Revision: 1745641

URL: http://svn.apache.org/viewvc?rev=1745641&view=rev
Log:
PDFBOX-3364: add handler for PDAnnotationLink appearance

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationLink.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAbstractAppearanceHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationLink.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationLink.java?rev=1745641&r1=1745640&r2=1745641&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationLink.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationLink.java Thu May 26 20:39:49 2016
@@ -25,6 +25,8 @@ import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
 import org.apache.pdfbox.pdmodel.interactive.action.PDAction;
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
+import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDAppearanceHandler;
+import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDLinkAppearanceHandler;
 import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
 
 /**
@@ -57,6 +59,11 @@ public class PDAnnotationLink extends PD
      * The type of annotation.
      */
     public static final String SUB_TYPE = "Link";
+    
+    /**
+     * Custom apperance handler to generate an appearance stream.
+     */
+    private PDAppearanceHandler customAppearanceHandler;
 
     /**
      * Constructor.
@@ -223,4 +230,27 @@ public class PDAnnotationLink extends PD
         // Should never happen as this is a required item
         return null; 
     }
+    
+    /**
+     * Set a custom appearance handler for generating the annotations appearance streams.
+     * 
+     * @param circleAppearanceHandler
+     */
+    public void setCustomAppearanceHandler(PDAppearanceHandler customAppearanceHandler)
+    {
+        this.customAppearanceHandler = customAppearanceHandler;
+    }
+    
+    public void constructAppearances()
+    {
+        if (customAppearanceHandler == null)
+        {
+            PDLinkAppearanceHandler appearanceHandler = new PDLinkAppearanceHandler(this);
+            appearanceHandler.generateAppearanceStreams();
+        }
+        else
+        {
+            customAppearanceHandler.generateAppearanceStreams();
+        }
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAbstractAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAbstractAppearanceHandler.java?rev=1745641&r1=1745640&r2=1745641&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAbstractAppearanceHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAbstractAppearanceHandler.java Thu May 26 20:39:49 2016
@@ -61,23 +61,6 @@ public abstract class PDAbstractAppearan
         return annotation;
     }
 
-    /**
-     * Get the line width of the border.
-     * 
-     * Get the width of the line used to draw a border around the annotation.
-     * This may either be specified by the annotation dictionaries Border
-     * setting or by the W entry in the BS border style dictionary. If both are
-     * missing the default width is 1.
-     * 
-     * @return the line width
-     */
-    // TODO: according to the PDF spec the use of the BS entry is annotation
-    // specific
-    // so we will leave that to be implemented by individual handlers.
-    // If at the end all annotations support the BS entry this can be handled
-    // here and removed from the individual handlers.
-    abstract float getLineWidth();
-
     PDColor getColor()
     {
         return annotation.getColor();

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java?rev=1745641&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java Thu May 26 20:39:49 2016
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.pdfbox.cos.COSArray;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
+
+/**
+ * Handler to generate the link annotations appearance.
+ *
+ */
+public class PDLinkAppearanceHandler extends PDAbstractAppearanceHandler
+{
+    
+    public PDLinkAppearanceHandler(PDAnnotation annotation)
+    {
+        super(annotation);
+    }
+    
+    @Override
+    public void generateAppearanceStreams()
+    {
+        generateNormalAppearance();
+        generateRolloverAppearance();
+        generateDownAppearance();
+    }
+
+    @Override
+    public void generateNormalAppearance()
+    {
+        // Adobe doesn't generate an appearance for a link annotation
+        float lineWidth = getLineWidth();
+        try
+        {
+            PDAppearanceContentStream contentStream = getNormalAppearanceAsContentStream();;
+            contentStream.setStrokingColorOnDemand(getColor());
+            boolean hasBackground = contentStream
+                    .setNonStrokingColorOnDemand(((PDAnnotationLink) getAnnotation()).getColor());
+
+            // TODO: handle opacity settings
+            
+            contentStream.setBorderLine(lineWidth, ((PDAnnotationLink) getAnnotation()).getBorderStyle());
+            
+            // the differences rectangle
+            // TODO: this only works for border effect solid. Cloudy needs a different approach.
+            setRectDifference(lineWidth);
+            
+            // Acrobat applies a padding to each side of the bbox so the line is completely within
+            // the bbox.
+            PDRectangle borderEdge = getPaddedRectangle(getRectangle(),lineWidth/2);
+            contentStream.addRect(borderEdge.getLowerLeftX() , borderEdge.getLowerLeftY(),
+                    borderEdge.getWidth(), borderEdge.getHeight());
+            
+            contentStream.drawShape(lineWidth, hasBackground);
+            
+            contentStream.close();
+        } catch (IOException e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void generateRolloverAppearance()
+    {
+        // No rollover appearance generated for a link annotation
+    }
+
+    @Override
+    public void generateDownAppearance()
+    {
+     // No down appearance generated for a link annotation
+    }
+    
+    /**
+     * Get the line with of the border.
+     * 
+     * Get the width of the line used to draw a border around the annotation.
+     * This may either be specified by the annotation dictionaries Border
+     * setting or by the W entry in the BS border style dictionary. If both are
+     * missing the default width is 1.
+     * 
+     * @return the line width
+     */
+    // TODO: according to the PDF spec the use of the BS entry is annotation
+    // specific
+    // so we will leave that to be implemented by individual handlers.
+    // If at the end all annotations support the BS entry this can be handled
+    // here and removed from the individual handlers.
+    float getLineWidth()
+    {
+        PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();
+
+        PDBorderStyleDictionary bs = annotation.getBorderStyle();
+
+        if (bs != null)
+        {
+            return bs.getWidth();
+        } else
+        {
+            COSArray borderCharacteristics = annotation.getBorder();
+            if (borderCharacteristics != null && borderCharacteristics.size() >= 3)
+            {
+                return borderCharacteristics.getInt(3);
+            }
+        }
+
+        return 1;
+    }
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDLinkAppearanceHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain