From fbdd70cf1980956555dec8c3b3502fc1d2dab217 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Tue, 3 Apr 2018 10:46:05 +0000 Subject: [PATCH] FOP-2782: Bookmarks point to last page when run via IF git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1828216 13f79535-47bb-0310-9956-ffa450edef68 --- .../extensions/DocumentNavigationHandler.java | 6 ++- .../DocumentNavigationHandlerTestCase.java | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/DocumentNavigationHandler.java b/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/DocumentNavigationHandler.java index fe7a744f0..5221ad04b 100644 --- a/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/DocumentNavigationHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/DocumentNavigationHandler.java @@ -123,7 +123,7 @@ public class DocumentNavigationHandler extends DefaultHandler if (pageIndex < 0) { location = null; } else { - if (hasNavigation()) { + if (hasNavigation() && !inBookmark()) { int currentPageIndex = navHandler.getPageIndex(); if (currentPageIndex >= 0) { pageIndex = currentPageIndex; @@ -170,6 +170,10 @@ public class DocumentNavigationHandler extends DefaultHandler } } + private boolean inBookmark() { + return !objectStack.empty() && objectStack.peek() instanceof Bookmark; + } + /** {@inheritDoc} */ public void endElement(String uri, String localName, String qName) throws SAXException { if (NAMESPACE.equals(uri)) { diff --git a/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java index ee020aec7..523da388f 100644 --- a/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java @@ -46,6 +46,8 @@ import org.apache.fop.fonts.FontInfo; import org.apache.fop.render.intermediate.IFContext; import org.apache.fop.render.intermediate.IFException; import org.apache.fop.render.intermediate.extensions.AbstractAction; +import org.apache.fop.render.intermediate.extensions.Bookmark; +import org.apache.fop.render.intermediate.extensions.BookmarkTree; import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants; import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler; import org.apache.fop.render.intermediate.extensions.GoToXYAction; @@ -126,4 +128,49 @@ public class DocumentNavigationHandlerTestCase { Assert.assertEquals(pageLink.size(), 2); Assert.assertFalse(pageLink.get(0).equals(pageLink.get(1))); } + + @Test + public void testBookmarkGotoXY() throws SAXException, IFException { + FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent(); + PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(ua)); + documentHandler.setResult(new StreamResult(new ByteArrayOutputStream())); + documentHandler.setFontInfo(new FontInfo()); + documentHandler.startDocument(); + + documentHandler.startPage(0, "", "", new Dimension()); + documentHandler.endPage(); + + int currentPage = 1; + documentHandler.startPage(currentPage, "", "", new Dimension()); + + final List trees = new ArrayList(); + PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) { + public void renderBookmarkTree(BookmarkTree tree) throws IFException { + trees.add(tree); + } + }; + DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler, + new HashMap()); + Attributes attributes = mock(Attributes.class); + when(attributes.getValue("page-index")).thenReturn("0"); + when(attributes.getValue("x")).thenReturn("0"); + when(attributes.getValue("y")).thenReturn("0"); + + for (QName q : Arrays.asList(DocumentNavigationExtensionConstants.BOOKMARK_TREE, + DocumentNavigationExtensionConstants.BOOKMARK, + DocumentNavigationExtensionConstants.GOTO_XY)) { + navigationHandler.startElement(q.getNamespaceURI(), q.getLocalName(), null, attributes); + } + for (QName q : Arrays.asList(DocumentNavigationExtensionConstants.GOTO_XY, + DocumentNavigationExtensionConstants.BOOKMARK, + DocumentNavigationExtensionConstants.BOOKMARK_TREE)) { + navigationHandler.endElement(q.getNamespaceURI(), q.getLocalName(), null); + } + + documentHandler.endPage(); + + Bookmark b = (Bookmark) trees.get(0).getBookmarks().get(0); + GoToXYAction a = (GoToXYAction) b.getAction(); + Assert.assertEquals(a.getPageIndex(), 0); + } } -- 2.39.5