You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/01/21 08:01:59 UTC

svn commit: r1821771 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDStrikeoutAppearanceHandler.java

Author: tilman
Date: Sun Jan 21 08:01:59 2018
New Revision: 1821771

URL: http://svn.apache.org/viewvc?rev=1821771&view=rev
Log:
PDFBOX-3353: add handler for strikeout annotations

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDStrikeoutAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDStrikeoutAppearanceHandler.java?rev=1821771&r1=1821770&r2=1821771&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDStrikeoutAppearanceHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDStrikeoutAppearanceHandler.java Sun Jan 21 08:01:59 2018
@@ -16,9 +16,13 @@
 
 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.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationStrikeout;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream;
 
 /**
  *
@@ -43,7 +47,88 @@ public class PDStrikeoutAppearanceHandle
     @Override
     public void generateNormalAppearance()
     {
-        //TODO
+        PDAnnotationStrikeout annotation = (PDAnnotationStrikeout) getAnnotation();
+        PDRectangle rect = annotation.getRectangle();
+        float[] pathsArray = annotation.getQuadPoints();
+        if (pathsArray == null)
+        {
+            return;
+        }
+        AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
+        if (ab.color.getComponents().length == 0)
+        {
+            return;
+        }
+        if (ab.width == 0)
+        {
+            // value found in adobe reader
+            ab.width = 1.5f;
+        }
+
+        // Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
+        //TODO in a class structure this should be overridable
+        // this is similar to polyline but different data type
+        float minX = Float.MAX_VALUE;
+        float minY = Float.MAX_VALUE;
+        float maxX = Float.MIN_VALUE;
+        float maxY = Float.MIN_VALUE;
+        for (int i = 0; i < pathsArray.length / 2; ++i)
+        {
+            float x = pathsArray[i * 2];
+            float y = pathsArray[i * 2 + 1];
+            minX = Math.min(minX, x);
+            minY = Math.min(minY, y);
+            maxX = Math.max(maxX, x);
+            maxY = Math.max(maxY, y);
+        }
+        rect.setLowerLeftX(Math.min(minX - ab.width / 2, rect.getLowerLeftX()));
+        rect.setLowerLeftY(Math.min(minY - ab.width / 2, rect.getLowerLeftY()));
+        rect.setUpperRightX(Math.max(maxX + ab.width, rect.getUpperRightX()));
+        rect.setUpperRightY(Math.max(maxY + ab.width, rect.getUpperRightY()));
+        annotation.setRectangle(rect);
+
+        try
+        {
+            try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream())
+            {
+                cs.setStrokingColor(ab.color);
+                if (ab.dashArray != null)
+                {
+                    cs.setLineDashPattern(ab.dashArray, 0);
+                }
+                cs.setLineWidth(ab.width);
+
+                // spec is incorrect
+                // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
+                for (int i = 0; i < pathsArray.length / 8; ++i)
+                {
+                    // get mid point between bounds, substract the line width to approximate what Adobe is doing
+                    // See e.g. CTAN-example-Annotations.pdf and PLPDF.com-MarkupAnnotations.pdf
+                    // and https://bugs.ghostscript.com/show_bug.cgi?id=693664
+                    // do the math for diagonal annotations with this weird old trick:
+                    // https://stackoverflow.com/questions/7740507/extend-a-line-segment-a-specific-distance
+                    float len0 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8] - pathsArray[i * 8 + 4], 2) + 
+                                          Math.pow(pathsArray[i * 8 + 1] - pathsArray[i * 8 + 5], 2)));
+                    float x0 = pathsArray[i * 8 + 4] + (pathsArray[i * 8] - 
+                               pathsArray[i * 8 + 4]) / len0 * (len0 / 2 - ab.width);
+                    float y0 = pathsArray[i * 8 + 5] + (pathsArray[i * 8 + 1] - 
+                               pathsArray[i * 8 + 5]) / len0 * (len0 / 2 - ab.width);
+                    float len1 = (float) (Math.sqrt(Math.pow(pathsArray[i * 8 + 2] - pathsArray[i * 8 + 6], 2) + 
+                                          Math.pow(pathsArray[i * 8 + 3] - pathsArray[i * 8 + 7], 2)));
+                    float x1 = pathsArray[i * 8 + 6] + (pathsArray[i * 8 + 2] - 
+                               pathsArray[i * 8 + 6]) / len1 * (len1 / 2 - ab.width);
+                    float y1 = pathsArray[i * 8 + 7] + (pathsArray[i * 8 + 3] - 
+                               pathsArray[i * 8 + 7]) / len1 * (len1 / 2 - ab.width);
+                    cs.moveTo(x0, y0);
+                    cs.lineTo(x1, y1);
+                }
+                cs.stroke();
+            }
+        }
+        catch (IOException ex)
+        {
+            LOG.error(ex);
+        }
     }
 
     @Override