fix FOP-1648

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1877589 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFFactory.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFFactory.java
index aad24ca..1238d11 100644
--- a/fop-core/src/main/java/org/apache/fop/pdf/PDFFactory.java
+++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFFactory.java
@@ -519,6 +519,26 @@
     }
 
     /**
+     * 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
      *
      * @param rect   the clickable rectangle
diff --git a/fop-core/src/main/java/org/apache/fop/pdf/PDFGoTo.java b/fop-core/src/main/java/org/apache/fop/pdf/PDFGoTo.java
index 695a5b7..1cc69a8 100644
--- a/fop-core/src/main/java/org/apache/fop/pdf/PDFGoTo.java
+++ b/fop-core/src/main/java/org/apache/fop/pdf/PDFGoTo.java
@@ -35,6 +35,19 @@
     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 @@
                           + " " + 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 @@
             }
         }
 
-        return true;
+        return (isNamedDestination == gt.isNamedDestination);
     }
 }
 
diff --git a/fop-core/src/main/java/org/apache/fop/svg/PDFANode.java b/fop-core/src/main/java/org/apache/fop/svg/PDFANode.java
index 005a34a..a555617 100644
--- a/fop-core/src/main/java/org/apache/fop/svg/PDFANode.java
+++ b/fop-core/src/main/java/org/apache/fop/svg/PDFANode.java
@@ -26,6 +26,7 @@
 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 @@
 
                     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);
             }
         }
     }
diff --git a/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java b/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
index 3fa6d31..dbb0a2e 100644
--- a/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
+++ b/fop-core/src/main/java/org/apache/fop/svg/PDFGraphics2D.java
@@ -421,9 +421,15 @@
             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));
diff --git a/fop-core/src/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java b/fop-core/src/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java
index da7a8a0..f487a5b 100644
--- a/fop-core/src/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java
+++ b/fop-core/src/test/java/org/apache/fop/pdf/PDFFactoryTestCase.java
@@ -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 @@
 
         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
index 0000000..ca15926
--- /dev/null
+++ b/fop-core/src/test/java/org/apache/fop/pdf/PDFGoToTestCase.java
@@ -0,0 +1,17 @@
+package org.apache.fop.pdf;

+

+import org.junit.Test;

+

+import static junit.framework.TestCase.assertEquals;

+

+public class PDFGoToTestCase {

+

+    @Test

+    public void test() {

+        PDFGoTo pdfGoTo = new PDFGoTo("destination", true);

+        String expected = "<< /Type /Action\n"

+                + "/S /GoTo\n/D (destination)\n"

+                + ">>";

+        assertEquals(expected, pdfGoTo.toPDFString());

+    }

+}