pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1821396 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation: ./ handlers/
Date Wed, 17 Jan 2018 16:58:38 GMT
Author: tilman
Date: Wed Jan 17 16:58:38 2018
New Revision: 1821396

URL: http://svn.apache.org/viewvc?rev=1821396&view=rev
Log:
PDFBOX-4070: create polyline and polygon annotation classes, use getVertices for handler,
create polygon annotation handler

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolygon.java
  (with props)
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolyline.java
  (with props)
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolylineAppearanceHandler.java
  (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolygonAppearanceHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java?rev=1821396&r1=1821395&r2=1821396&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
Wed Jan 17 16:58:38 2018
@@ -131,6 +131,14 @@ public abstract class PDAnnotation imple
             {
                 annot = new PDAnnotationCircle(annotDic);
             }
+            else if (PDAnnotationPolygon.SUB_TYPE.equals(subtype))
+            {
+                annot = new PDAnnotationPolygon(annotDic);
+            }
+            else if (PDAnnotationPolyline.SUB_TYPE.equals(subtype))
+            {
+                annot = new PDAnnotationPolyline(annotDic);
+            }
             else if (PDAnnotationText.SUB_TYPE.equals(subtype))
             {
                 annot = new PDAnnotationText(annotDic);
@@ -148,8 +156,6 @@ public abstract class PDAnnotation imple
                 annot = new PDAnnotationWidget(annotDic);
             }
             else if (PDAnnotationMarkup.SUB_TYPE_FREETEXT.equals(subtype)
-                    || PDAnnotationMarkup.SUB_TYPE_POLYGON.equals(subtype)
-                    || PDAnnotationMarkup.SUB_TYPE_POLYLINE.equals(subtype)
                     || PDAnnotationMarkup.SUB_TYPE_CARET.equals(subtype)
                     || PDAnnotationMarkup.SUB_TYPE_INK.equals(subtype)
                     || PDAnnotationMarkup.SUB_TYPE_SOUND.equals(subtype))

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java?rev=1821396&r1=1821395&r2=1821396&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationMarkup.java
Wed Jan 17 16:58:38 2018
@@ -23,7 +23,6 @@ import org.apache.pdfbox.cos.COSDictiona
 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;
 
 /**
  * This class represents the additional fields of a Markup type Annotation. See section 12.5.6
of ISO32000-1:2008
@@ -38,14 +37,6 @@ public class PDAnnotationMarkup extends
      */
     public static final String SUB_TYPE_FREETEXT = "FreeText";
     /**
-     * Constant for an Polygon type of annotation.
-     */
-    public static final String SUB_TYPE_POLYGON = "Polygon";
-    /**
-     * Constant for an PolyLine type of annotation.
-     */
-    public static final String SUB_TYPE_POLYLINE = "PolyLine";
-    /**
      * Constant for an Caret type of annotation.
      */
     public static final String SUB_TYPE_CARET = "Caret";
@@ -354,18 +345,4 @@ public class PDAnnotationMarkup extends
         }
         return null;
     }
-
-    //TODO this isn't available for all markup annotations, only
-    // polygon, polyline, line, square, circle. 
-    // Thus we need to created more classes (polygon, polyline).
-
-    /**
-     * This will retrieve the interior color.
-     *
-     * @return object representing the color.
-     */
-    public PDColor getInteriorColor()
-    {
-        return getColor(COSName.IC);
-    }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolygon.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolygon.java?rev=1821396&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolygon.java
(added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolygon.java
Wed Jan 17 16:58:38 2018
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+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.PDPolygonAppearanceHandler;
+
+/**
+ *
+ * @author Paul King
+ */
+public class PDAnnotationPolygon extends PDAnnotationMarkup
+{
+    /**
+     * The type of annotation.
+     */
+    public static final String SUB_TYPE = "Polygon";
+
+    private PDAppearanceHandler polygonAppearanceHandler;
+    
+    /**
+     * Constructor.
+     */
+    public PDAnnotationPolygon()
+    {
+        getCOSObject().setName(COSName.SUBTYPE, SUB_TYPE);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param dict The annotations dictionary.
+     */
+    public PDAnnotationPolygon(COSDictionary dict)
+    {
+        super(dict);
+    }
+
+    // PDF 32000 specification has "the interior color with which to fill the annotation’s
line endings"
+    // but it is the inside of the polygon.
+    
+    /**
+     * This will set interior color.
+     *
+     * @param ic color.
+     */
+    public void setInteriorColor(PDColor ic)
+    {
+        getCOSObject().setItem(COSName.IC, ic.toCOSArray());
+    }
+
+    /**
+     * This will retrieve the interior color.
+     *
+     * @return object representing the color.
+     */
+    public PDColor getInteriorColor()
+    {
+        return getColor(COSName.IC);
+    }
+
+    /**
+     * This will retrieve the numbers that shall represent the alternating horizontal and
vertical
+     * coordinates.
+     *
+     * @return An array of floats representing the alternating horizontal and vertical coordinates.
+     */
+    public float[] getVertices()
+    {
+        COSBase base = getCOSObject().getDictionaryObject(COSName.VERTICES);
+        if (base instanceof COSArray)
+        {
+            return ((COSArray) base).toFloatArray();
+        }
+        return null;
+    }
+
+    /**
+     * This will set the numbers that shall represent the alternating horizontal and vertical
+     * coordinates.
+     *
+     * @param points an array with the numbers that shall represent the alternating horizontal
and
+     * vertical coordinates.
+     */
+    public void setVertices(float[] points)
+    {
+        COSArray ar = new COSArray();
+        ar.setFloatArray(points);
+        getCOSObject().setItem(COSName.VERTICES, ar);
+    }
+
+    /**
+     * Set a custom appearance handler for generating the annotations appearance streams.
+     * 
+     * @param squareAppearanceHandler
+     */
+    public void setCustomSquareAppearanceHandler(PDAppearanceHandler squareAppearanceHandler)
+    {
+        this.polygonAppearanceHandler = squareAppearanceHandler;
+    }
+
+    @Override
+    public void constructAppearances()
+    {
+        if (polygonAppearanceHandler == null)
+        {
+            PDPolygonAppearanceHandler appearanceHandler = new PDPolygonAppearanceHandler(this);
+            appearanceHandler.generateAppearanceStreams();
+        }
+        else
+        {
+            polygonAppearanceHandler.generateAppearanceStreams();
+        }
+    }
+}

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

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolyline.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolyline.java?rev=1821396&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolyline.java
(added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationPolyline.java
Wed Jan 17 16:58:38 2018
@@ -0,0 +1,130 @@
+/*
+ * 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;
+
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+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.PDPolylineAppearanceHandler;
+
+/**
+ *
+ * @author Paul King
+ */
+public class PDAnnotationPolyline extends PDAnnotationMarkup
+{
+    /**
+     * The type of annotation.
+     */
+    public static final String SUB_TYPE = "PolyLine";
+
+    private PDAppearanceHandler polylineAppearanceHandler;
+    
+    /**
+     * Constructor.
+     */
+    public PDAnnotationPolyline()
+    {
+        getCOSObject().setName(COSName.SUBTYPE, SUB_TYPE);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param dict The annotations dictionary.
+     */
+    public PDAnnotationPolyline(COSDictionary dict)
+    {
+        super(dict);
+    }
+
+    /**
+     * This will set interior color of the line endings defined in the LE entry.
+     *
+     * @param ic color.
+     */
+    public void setInteriorColor(PDColor ic)
+    {
+        getCOSObject().setItem(COSName.IC, ic.toCOSArray());
+    }
+
+    /**
+     * This will retrieve the interior color with which to fill the annotation’s line
endings.
+     *
+     * @return object representing the color.
+     */
+    public PDColor getInteriorColor()
+    {
+        return getColor(COSName.IC);
+    }
+
+    /**
+     * This will retrieve the numbers that shall represent the alternating horizontal and
vertical
+     * coordinates.
+     *
+     * @return An array of floats representing the alternating horizontal and vertical coordinates.
+     */
+    public float[] getVertices()
+    {
+        COSBase base = getCOSObject().getDictionaryObject(COSName.VERTICES);
+        if (base instanceof COSArray)
+        {
+            return ((COSArray) base).toFloatArray();
+        }
+        return null;
+    }
+
+    /**
+     * This will set the numbers that shall represent the alternating horizontal and vertical
+     * coordinates.
+     *
+     * @param points an array with the numbers that shall represent the alternating horizontal
and
+     * vertical coordinates.
+     */
+    public void setVertices(float[] points)
+    {
+        COSArray ar = new COSArray();
+        ar.setFloatArray(points);
+        getCOSObject().setItem(COSName.VERTICES, ar);
+    }
+
+    /**
+     * Set a custom appearance handler for generating the annotations appearance streams.
+     * 
+     * @param squareAppearanceHandler
+     */
+    public void setCustomSquareAppearanceHandler(PDAppearanceHandler squareAppearanceHandler)
+    {
+        this.polylineAppearanceHandler = squareAppearanceHandler;
+    }
+
+    @Override
+    public void constructAppearances()
+    {
+        if (polylineAppearanceHandler == null)
+        {
+            PDPolylineAppearanceHandler appearanceHandler = new PDPolylineAppearanceHandler(this);
+            appearanceHandler.generateAppearanceStreams();
+        }
+        else
+        {
+            polylineAppearanceHandler.generateAppearanceStreams();
+        }
+    }
+}

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolygonAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolygonAppearanceHandler.java?rev=1821396&r1=1821395&r2=1821396&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolygonAppearanceHandler.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolygonAppearanceHandler.java
Wed Jan 17 16:58:38 2018
@@ -22,10 +22,8 @@ import java.io.IOException;
 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.pdmodel.interactive.annotation.PDAnnotation;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolygon;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
 
@@ -52,17 +50,16 @@ public class PDPolygonAppearanceHandler
     @Override
     public void generateNormalAppearance()
     {
-        // Adobe doesn't generate an appearance for a link annotation
         float lineWidth = getLineWidth();
         try
         {
-            PDAnnotation annotation = getAnnotation();
+            PDAnnotationPolygon annotation = (PDAnnotationPolygon) getAnnotation();
             PDAppearanceContentStream contentStream = getNormalAppearanceAsContentStream();
             contentStream.setStrokingColorOnDemand(getColor());
 
             // TODO: handle opacity settings
 
-            contentStream.setBorderLine(lineWidth, ((PDAnnotationMarkup) annotation).getBorderStyle());
+            contentStream.setBorderLine(lineWidth, annotation.getBorderStyle());
 
             // the differences rectangle
             // TODO: this only works for border effect solid. Cloudy needs a
@@ -108,38 +105,35 @@ public class PDPolygonAppearanceHandler
             }
             else
             {
-                COSBase vertices = annotation.getCOSObject().getDictionaryObject(COSName.VERTICES);
-                if (!(vertices instanceof COSArray))
+                //TODO use getVertices
+                float[] verticesArray = annotation.getVertices();
+                if (verticesArray == null)
                 {
                     return;
                 }
 
-                COSArray verticesArray = (COSArray) vertices;
-                int nPoints = verticesArray.size() / 2;
+                int nPoints = verticesArray.length / 2;
                 for (int i = 0; i < nPoints; i++)
                 {
-                    COSBase bx = verticesArray.getObject(i * 2);
-                    COSBase by = verticesArray.getObject(i * 2 + 1);
-                    if (bx instanceof COSNumber && by instanceof COSNumber)
+                    float x = verticesArray[i * 2];
+                    float y = verticesArray[i * 2 + 1];
+                    if (i == 0)
                     {
-                        float x = ((COSNumber) bx).floatValue();
-                        float y = ((COSNumber) by).floatValue();
-                        if (i == 0)
-                        {
-                            contentStream.moveTo(x, y);
-                        }
-                        else
-                        {
-                            contentStream.lineTo(x, y);
-                        }
+                        contentStream.moveTo(x, y);
+                    }
+                    else
+                    {
+                        contentStream.lineTo(x, y);
                     }
                 }
                 contentStream.stroke();
             }
 
             contentStream.close();
-        } catch (IOException e)
+        }
+        catch (IOException e)
         {
+            //TODO
             e.printStackTrace();
         }
     }
@@ -173,7 +167,7 @@ public class PDPolygonAppearanceHandler
     // here and removed from the individual handlers.
     float getLineWidth()
     {
-        PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();
+        PDAnnotationPolygon annotation = (PDAnnotationPolygon) getAnnotation();
 
         PDBorderStyleDictionary bs = annotation.getBorderStyle();
 

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolylineAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolylineAppearanceHandler.java?rev=1821396&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolylineAppearanceHandler.java
(added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDPolylineAppearanceHandler.java
Wed Jan 17 16:58:38 2018
@@ -0,0 +1,100 @@
+/*
+ * 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 org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPolyline;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
+
+/**
+ * Handler to generate the polygon annotations appearance.
+ *
+ */
+public class PDPolylineAppearanceHandler extends PDAbstractAppearanceHandler
+{
+    public PDPolylineAppearanceHandler(PDAnnotation annotation)
+    {
+        super(annotation);
+    }
+
+    @Override
+    public void generateAppearanceStreams()
+    {
+        generateNormalAppearance();
+        generateRolloverAppearance();
+        generateDownAppearance();
+    }
+
+    @Override
+    public void generateNormalAppearance()
+    {
+        //TODO
+    }
+
+    @Override
+    public void generateRolloverAppearance()
+    {
+        // No rollover appearance generated for a polygon annotation
+    }
+
+    @Override
+    public void generateDownAppearance()
+    {
+        // No down appearance generated for a polygon annotation
+    }
+
+    //TODO DRY, this code is from polygonAppearanceHandler so it's double
+    
+    /**
+     * 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()
+    {
+        PDAnnotationPolyline annotation = (PDAnnotationPolyline) getAnnotation();
+
+        PDBorderStyleDictionary bs = annotation.getBorderStyle();
+
+        if (bs != null)
+        {
+            return bs.getWidth();
+        }
+        else
+        {
+            COSArray borderCharacteristics = annotation.getBorder();
+            if (borderCharacteristics.size() >= 3)
+            {
+                return borderCharacteristics.getInt(3);
+            }
+        }
+
+        return 1;
+    }
+}

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



Mime
View raw message