]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
fix FOP-1648
authorChris Bowditch <cbowditch@apache.org>
Mon, 11 May 2020 15:56:59 +0000 (15:56 +0000)
committerChris Bowditch <cbowditch@apache.org>
Mon, 11 May 2020 15:56:59 +0000 (15:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1877589 13f79535-47bb-0310-9956-ffa450edef68

fop-core/src/main/java/org/apache/fop/pdf/PDFFactory.java
fop-core/src/main/java/org/apache/fop/pdf/PDFGoTo.java
fop-core/src/main/java/org/apache/fop/svg/PDFANode.java
fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
fop-core/src/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java
fop-core/src/test/java/org/apache/fop/pdf/PDFGoToTestCase.java [new file with mode: 0644]

index aad24cae91dfbb4907efcf405bf4bf6a05681a4e..1238d11a631b0e4f67e1aa8bc9925a97a7423cff 100644 (file)
@@ -518,6 +518,26 @@ public class PDFFactory {
         return link;
     }
 
+    /**
+     * Make an internal link.
+     *
+     * @param rect the hotspot position in absolute coordinates
+     * @param dest the position destination
+     * @param isNamedDestination set to true if dest param is a named destination
+     * @return the new PDF link object
+     */
+    public PDFLink makeLink(Rectangle2D rect, String dest, boolean isNamedDestination) {
+        PDFLink link = new PDFLink(rect);
+        getDocument().registerObject(link);
+
+        PDFAction pdfAction = new PDFGoTo(dest, isNamedDestination);
+        getDocument().registerObject(pdfAction);
+
+        link.setAction(pdfAction);
+
+        return link;
+    }
+
     /**
      * Make a {@link PDFLink} object
      *
index 695a5b73cd82bb505add79d299184e992366e180..1cc69a80017a7ffde4bedc67aca67313b4f65614 100644 (file)
@@ -35,6 +35,19 @@ public class PDFGoTo extends PDFAction {
     private String destination;
     private float xPosition;
     private float yPosition;
+    private boolean isNamedDestination;
+
+    /**
+     * create a /GoTo object.
+     *
+     * @param destination name of the destination
+     * @param isNamedDestination set to true if the destination is a named destination
+     */
+    public PDFGoTo(String destination, boolean isNamedDestination) {
+        super();
+        this.destination = destination;
+        this.isNamedDestination = isNamedDestination;
+    }
 
     /**
      * create a /GoTo object.
@@ -125,6 +138,11 @@ public class PDFGoTo extends PDFAction {
                           + " " + yPosition + " null]\n";
         } else {
             dest = "/D [" + this.pageReference + " " + destination + "]\n";
+            if (this.isNamedDestination) {
+                dest = "/D (" + this.destination + ")\n";
+             } else {
+                dest = "/D [" + this.pageReference + " " + destination + "]\n";
+             }
         }
         return "<< /Type /Action\n/S /GoTo\n" + dest + ">>";
     }
@@ -172,7 +190,7 @@ public class PDFGoTo extends PDFAction {
             }
         }
 
-        return true;
+        return (isNamedDestination == gt.isNamedDestination);
     }
 }
 
index 005a34a71f49255b9391c0233fab37a44f4f3bdf..a5556170663265ba162f6cabe16eb3e18cbd03cb 100644 (file)
@@ -26,6 +26,7 @@ import java.awt.geom.Rectangle2D;
 import java.util.StringTokenizer;
 
 import org.apache.batik.gvt.CompositeGraphicsNode;
+import org.apache.fop.pdf.PDFLink;
 
 /**
  * <p>A graphics node that represents an image described as a graphics node.</p>
@@ -115,8 +116,11 @@ public class PDFANode extends CompositeGraphicsNode {
 
                     destination = "" + x + " " + y + " "
                                   + (x + width) + " " + (y + height);
+                } else if (destination.startsWith("#")) {
+                    pdfg.addLink(getBounds(), transform, destination, PDFLink.INTERNAL);
+                } else {
+                    pdfg.addLink(getBounds(), transform, destination, type);
                 }
-                pdfg.addLink(getBounds(), transform, destination, type);
             }
         }
     }
index 3fa6d3149f66c23caca661cf57917ac1a382f72d..dbb0a2e65f93aba70c950bc0605a97b27c10eb31 100644 (file)
@@ -421,9 +421,15 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand
             Rectangle rect = b.getBounds();
 
             if (linkType != PDFLink.EXTERNAL) {
-                String pdfdest = "/FitR " + dest;
-                resourceContext.addAnnotation(
-                    pdfDoc.getFactory().makeLink(rect, getPageReference().toString(), pdfdest));
+                if (dest.startsWith("#")) {
+                    String idDest = dest.substring(1);
+                    resourceContext.addAnnotation(
+                            pdfDoc.getFactory().makeLink(rect, idDest, true));
+                } else {
+                    String pdfdest = "/FitR " + dest;
+                    resourceContext.addAnnotation(
+                            pdfDoc.getFactory().makeLink(rect, getPageReference().toString(), pdfdest));
+                }
             } else {
                 resourceContext.addAnnotation(
                     pdfDoc.getFactory().makeLink(rect, dest, linkType, 0));
index da7a8a0de9d780c3c82641c7017a3658cec1dbbd..f487a5b0c52cfe39ff7308c2de16dd11cc023b48 100644 (file)
@@ -20,6 +20,7 @@
 package org.apache.fop.pdf;
 
 import java.awt.Rectangle;
+import java.awt.geom.Rectangle2D;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -211,4 +212,19 @@ public class PDFFactoryTestCase {
 
         assertEquals(expectedString, pdfAction.toPDFString());
     }
+
+    @Test
+    public void testMakeLink() {
+        PDFDocument doc = new PDFDocument("");
+        PDFFactory pdfFactory = new PDFFactory(doc);
+        Rectangle2D rect = new Rectangle(10,20);
+        PDFLink link = pdfFactory.makeLink(rect, "dest", true);
+
+        String expectedString = "<< /Type /Annot\n" + "/Subtype /Link\n" + "/Rect [ "
+                + "0.0 0.0 10.0 20.0 ]\n/C [ 0 0 0 ]\n"
+                + "/Border [ 0 0 0 ]\n" + "/A 1 0 R"
+                + "\n/H /I\n\n>>";
+
+        assertEquals(expectedString, link.toPDFString());
+    }
 }
diff --git a/fop-core/src/test/java/org/apache/fop/pdf/PDFGoToTestCase.java b/fop-core/src/test/java/org/apache/fop/pdf/PDFGoToTestCase.java
new file mode 100644 (file)
index 0000000..ca15926
--- /dev/null
@@ -0,0 +1,17 @@
+package org.apache.fop.pdf;\r
+\r
+import org.junit.Test;\r
+\r
+import static junit.framework.TestCase.assertEquals;\r
+\r
+public class PDFGoToTestCase {\r
+\r
+    @Test\r
+    public void test() {\r
+        PDFGoTo pdfGoTo = new PDFGoTo("destination", true);\r
+        String expected = "<< /Type /Action\n"\r
+                + "/S /GoTo\n/D (destination)\n"\r
+                + ">>";\r
+        assertEquals(expected, pdfGoTo.toPDFString());\r
+    }\r
+}\r