From 0c19e852f5db578f96f496b5e25e0e13e6a7aa55 Mon Sep 17 00:00:00 2001 From: Chris Bowditch Date: Mon, 11 May 2020 15:56:59 +0000 Subject: [PATCH] fix FOP-1648 git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1877589 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/fop/pdf/PDFFactory.java | 20 +++++++++++++++++++ .../main/java/org/apache/fop/pdf/PDFGoTo.java | 20 ++++++++++++++++++- .../java/org/apache/fop/svg/PDFANode.java | 6 +++++- .../org/apache/fop/svg/PDFGraphics2D.java | 12 ++++++++--- .../apache/fop/pdf/PDFFactoryTestCase.java | 16 +++++++++++++++ .../org/apache/fop/pdf/PDFGoToTestCase.java | 17 ++++++++++++++++ 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 fop-core/src/test/java/org/apache/fop/pdf/PDFGoToTestCase.java 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 aad24cae9..1238d11a6 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 @@ -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 * 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 695a5b73c..1cc69a800 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 @@ 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); } } 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 005a34a71..a55561706 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.awt.geom.Rectangle2D; import java.util.StringTokenizer; import org.apache.batik.gvt.CompositeGraphicsNode; +import org.apache.fop.pdf.PDFLink; /** *

A graphics node that represents an image described as a graphics node.

@@ -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); } } } 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 3fa6d3149..dbb0a2e65 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 @@ 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)); 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 da7a8a0de..f487a5b0c 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 @@ 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 index 000000000..ca15926fd --- /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()); + } +} -- 2.39.5