pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1821540 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers: AnnotationBorder.java PDInkAppearanceHandler.java
Date Thu, 18 Jan 2018 18:39:34 GMT
Author: tilman
Date: Thu Jan 18 18:39:33 2018
New Revision: 1821540

URL: http://svn.apache.org/viewvc?rev=1821540&view=rev
Log:
PDFBOX-3353: complete appearance handler for ink annotation; utility class for border and
color

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

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/AnnotationBorder.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/AnnotationBorder.java?rev=1821540&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/AnnotationBorder.java
(added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/AnnotationBorder.java
Thu Jan 18 18:39:33 2018
@@ -0,0 +1,96 @@
+/*
+ * 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 org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSNumber;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
+
+/**
+ *
+ * @author Tilman Hausherr
+ */
+class AnnotationBorder
+{
+    float[] dashArray = null;
+    boolean underline = false;
+    float width = 0;
+    PDColor color;
+
+    // return border info. BorderStyle must be provided as parameter because
+    // method is not available in the base class
+    static AnnotationBorder getAnnotationBorder(PDAnnotation annotation,
+            PDBorderStyleDictionary borderStyle)
+    {
+        AnnotationBorder ab = new AnnotationBorder();
+        COSArray border = annotation.getBorder();
+        if (borderStyle == null)
+        {
+            if (border.getObject(2) instanceof COSNumber)
+            {
+                ab.width = ((COSNumber) border.getObject(2)).floatValue();
+            }
+            if (border.size() > 3)
+            {
+                COSBase base3 = border.getObject(3);
+                if (base3 instanceof COSArray)
+                {
+                    ab.dashArray = ((COSArray) base3).toFloatArray();
+                }
+            }
+        }
+        else
+        {
+            ab.width = borderStyle.getWidth();
+            if (borderStyle.getStyle().equals(PDBorderStyleDictionary.STYLE_DASHED))
+            {
+                ab.dashArray = borderStyle.getDashStyle().getDashArray();
+            }
+            if (borderStyle.getStyle().equals(PDBorderStyleDictionary.STYLE_UNDERLINE))
+            {
+                ab.underline = true;
+            }
+        }
+        ab.color = annotation.getColor();
+        if (ab.color == null)
+        {
+            // spec is unclear, but black seems to be the right thing to do
+            ab.color = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
+        }
+        if (ab.dashArray != null)
+        {
+            boolean allZero = true;
+            for (float f : ab.dashArray)
+            {
+                if (f != 0)
+                {
+                    allZero = false;
+                    break;
+                }
+            }
+            if (allZero)
+            {
+                ab.dashArray = null;
+            }
+        }
+        return ab;
+    }
+}

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDInkAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDInkAppearanceHandler.java?rev=1821540&r1=1821539&r2=1821540&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDInkAppearanceHandler.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDInkAppearanceHandler.java
Thu Jan 18 18:39:33 2018
@@ -16,7 +16,12 @@
 
 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.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationInk;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream;
 
 /**
  * Handler to generate the ink annotations appearance.
@@ -24,6 +29,8 @@ import org.apache.pdfbox.pdmodel.interac
  */
 public class PDInkAppearanceHandler extends PDAbstractAppearanceHandler
 {
+    private static final Log LOG = LogFactory.getLog(PDInkAppearanceHandler.class);
+
     public PDInkAppearanceHandler(PDAnnotation annotation)
     {
         super(annotation);
@@ -40,18 +47,67 @@ public class PDInkAppearanceHandler exte
     @Override
     public void generateNormalAppearance()
     {
-        //TODO
+        PDAnnotationInk ink = (PDAnnotationInk) getAnnotation();
+        try
+        {
+            try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream())
+            {
+                AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
+                if (ab.width == 0 || ab.color.getComponents().length == 0)
+                {
+                    ;
+                }
+                else
+                {
+                    cs.setStrokingColor(ab.color);
+                    if (ab.dashArray != null)
+                    {
+                        cs.setLineDashPattern(ab.dashArray, 0);
+                    }
+                    cs.setLineWidth(ab.width);
+
+                    float[][] inkList = ink.getInkList();
+                    // PDF spec does not mention /Border for ink annotations, but it is used
if /BS is not available
+                    for (float[] pathArray : inkList)
+                    {
+                        int nPoints = pathArray.length / 2;
+
+                        // "When drawn, the points shall be connected by straight lines or
curves 
+                        // in an implementation-dependent way" - we do lines.
+                        for (int i = 0; i < nPoints; ++i)
+                        {
+                            float x = pathArray[i * 2];
+                            float y = pathArray[i * 2 + 1];
+
+                            if (i == 0)
+                            {
+                                cs.moveTo(x, y);
+                            }
+                            else
+                            {
+                                cs.lineTo(x, y);
+                            }
+                        }
+                        cs.stroke();
+                    }
+                }
+            }
+        }
+        catch (IOException ex)
+        {
+            LOG.error(ex);
+        }
     }
 
     @Override
     public void generateRolloverAppearance()
     {
-        // No rollover appearance generated for a polygon annotation
+        // No rollover appearance generated
     }
 
     @Override
     public void generateDownAppearance()
     {
-        // No down appearance generated for a polygon annotation
+        // No down appearance generated
     }
-}
+}
\ No newline at end of file



Mime
View raw message