From 2deb342979641c8cf3fb917fad00e0b596a09e56 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Mon, 6 Nov 2017 11:56:26 +0000 Subject: [PATCH] FOP-2756: Merged IF has wrong Table of Contents page links git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1814395 13f79535-47bb-0310-9956-ffa450edef68 --- .../IFDocumentNavigationHandler.java | 1 + .../fop/render/intermediate/IFSerializer.java | 4 + .../extensions/DocumentNavigationHandler.java | 6 ++ .../pdf/PDFDocumentNavigationHandler.java | 4 + .../DocumentNavigationHandlerTestCase.java | 88 +++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java diff --git a/fop-core/src/main/java/org/apache/fop/render/intermediate/IFDocumentNavigationHandler.java b/fop-core/src/main/java/org/apache/fop/render/intermediate/IFDocumentNavigationHandler.java index 09a641ce2..fa4f6dc74 100644 --- a/fop-core/src/main/java/org/apache/fop/render/intermediate/IFDocumentNavigationHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/intermediate/IFDocumentNavigationHandler.java @@ -57,4 +57,5 @@ public interface IFDocumentNavigationHandler { */ void addResolvedAction(AbstractAction action) throws IFException; + int getPageIndex(); } diff --git a/fop-core/src/main/java/org/apache/fop/render/intermediate/IFSerializer.java b/fop-core/src/main/java/org/apache/fop/render/intermediate/IFSerializer.java index d91e11fbe..054f0f5cc 100644 --- a/fop-core/src/main/java/org/apache/fop/render/intermediate/IFSerializer.java +++ b/fop-core/src/main/java/org/apache/fop/render/intermediate/IFSerializer.java @@ -918,6 +918,10 @@ implements IFConstants, IFPainter, IFDocumentNavigationHandler { } } + public int getPageIndex() { + return -1; + } + private void commitNavigation() throws IFException { Iterator iter = this.completeActions.iterator(); while (iter.hasNext()) { 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 c174a74a9..fe7a744f0 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,6 +123,12 @@ public class DocumentNavigationHandler extends DefaultHandler if (pageIndex < 0) { location = null; } else { + if (hasNavigation()) { + int currentPageIndex = navHandler.getPageIndex(); + if (currentPageIndex >= 0) { + pageIndex = currentPageIndex; + } + } final int x = XMLUtil .getAttributeAsInt(attributes, "x"); final int y = XMLUtil diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java index 8ffd817aa..e9622309a 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java @@ -139,6 +139,10 @@ public class PDFDocumentNavigationHandler implements IFDocumentNavigationHandler } } + public int getPageIndex() { + return documentHandler.getCurrentPage().getPageIndex(); + } + private PDFAction getAction(AbstractAction action) throws IFException { if (action == null) { return null; 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 new file mode 100644 index 000000000..390bcbe50 --- /dev/null +++ b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ +package org.apache.fop.render.extensions; + +import java.awt.Dimension; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import javax.xml.transform.stream.StreamResult; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.xmlgraphics.util.QName; + +import org.apache.fop.accessibility.StructureTreeElement; +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.FopFactory; +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.DocumentNavigationExtensionConstants; +import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler; +import org.apache.fop.render.intermediate.extensions.GoToXYAction; +import org.apache.fop.render.pdf.PDFDocumentHandler; +import org.apache.fop.render.pdf.PDFDocumentNavigationHandler; + +public class DocumentNavigationHandlerTestCase { + @Test + public void testGotoXY() 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 goToXYActions = new ArrayList(); + PDFDocumentNavigationHandler pdfDocumentNavigationHandler = new PDFDocumentNavigationHandler(documentHandler) { + public void addResolvedAction(AbstractAction action) throws IFException { + super.addResolvedAction(action); + goToXYActions.add((GoToXYAction) action); + } + }; + DocumentNavigationHandler navigationHandler = new DocumentNavigationHandler(pdfDocumentNavigationHandler, + new HashMap()); + QName xy = DocumentNavigationExtensionConstants.GOTO_XY; + Attributes attributes = mock(Attributes.class); + when(attributes.getValue("page-index")).thenReturn("0"); + when(attributes.getValue("x")).thenReturn("0"); + when(attributes.getValue("y")).thenReturn("0"); + navigationHandler.startElement(xy.getNamespaceURI(), xy.getLocalName(), null, attributes); + navigationHandler.endElement(xy.getNamespaceURI(), xy.getLocalName(), null); + documentHandler.endPage(); + + //Since user may merge IF files we want to use current page + Assert.assertEquals(goToXYActions.get(0).getPageIndex(), currentPage); + } +} -- 2.39.5