]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-2782: Bookmarks point to last page when run via IF
authorSimon Steiner <ssteiner@apache.org>
Tue, 3 Apr 2018 10:46:05 +0000 (10:46 +0000)
committerSimon Steiner <ssteiner@apache.org>
Tue, 3 Apr 2018 10:46:05 +0000 (10:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1828216 13f79535-47bb-0310-9956-ffa450edef68

fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/DocumentNavigationHandler.java
fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java

index fe7a744f0558faa57ff235a6c3a124dcd1abe666..5221ad04b6f2c0a8e2823dec43bd4d753f04e368 100644 (file)
@@ -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)) {
index ee020aec7c9fa4ebf0ad24233faedb24443f2177..523da388fece120f4a49f61e17b7157b30de3da7 100644 (file)
@@ -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<BookmarkTree> trees = new ArrayList<BookmarkTree>();
+        PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) {
+            public void renderBookmarkTree(BookmarkTree tree) throws IFException {
+                trees.add(tree);
+            }
+        };
+        DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler,
+                new HashMap<String, StructureTreeElement>());
+        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);
+    }
 }