diff options
author | Chris Bowditch <cbowditch@apache.org> | 2020-05-11 15:56:59 +0000 |
---|---|---|
committer | Chris Bowditch <cbowditch@apache.org> | 2020-05-11 15:56:59 +0000 |
commit | 0c19e852f5db578f96f496b5e25e0e13e6a7aa55 (patch) | |
tree | 405dc7eff92e49c9fe106bb790eb79a750eb25d5 | |
parent | 804da057d55a8eb000c7a4f93aca11976b5b3ff6 (diff) | |
download | xmlgraphics-fop-0c19e852f5db578f96f496b5e25e0e13e6a7aa55.tar.gz xmlgraphics-fop-0c19e852f5db578f96f496b5e25e0e13e6a7aa55.zip |
fix FOP-1648
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1877589 13f79535-47bb-0310-9956-ffa450edef68
6 files changed, 86 insertions, 5 deletions
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 @@ -519,6 +519,26 @@ public class PDFFactory { } /** + * 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 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; /** * <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); } } } 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());
+ }
+}
|