Debian Codegraph @

sejda-sambox/src/test/java/org/sejda/impl/sambox/component/OutlineDistillerTest.java

raw
1 /*
2 * Created on 16 dic 2015
3 * Copyright 2015 by Andrea Vacondio ([email protected]).
4 * This file is part of Sejda.
5 *
6 * Sejda is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Sejda is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Sejda. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package org.sejda.impl.sambox.component;
20
21 import static org.hamcrest.Matchers.instanceOf;
22 import static org.hamcrest.Matchers.is;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.sejda.commons.LookupTable;
34 import org.sejda.commons.util.IOUtils;
35 import org.sejda.io.SeekableSources;
36 import org.sejda.sambox.input.PDFParser;
37 import org.sejda.sambox.pdmodel.PDDocument;
38 import org.sejda.sambox.pdmodel.PDPage;
39 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDDestination;
40 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
41 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
42 import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
43 import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
44
45 /**
46 * @author Andrea Vacondio
47 *
48 */
49 public class OutlineDistillerTest {
50
51 private PDDocument document;
52 private LookupTable<PDPage> mapping = new LookupTable<>();
53
54 @Before
55 public void setUp() throws IOException {
56 document = PDFParser.parse(SeekableSources
57 .inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/test_outline.pdf")));
58 }
59
60 @After
61 public void tearDown() {
62 IOUtils.closeQuietly(document);
63 mapping.clear();
64 }
65
66 @Test
67 public void keepAll() {
68 for (PDPage current : document.getPages()) {
69 mapping.addLookupEntry(current, new PDPage());
70 }
71 PDDocumentOutline outline = new PDDocumentOutline();
72 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
73 assertTrue(outline.hasChildren());
74 assertEquals(5, outline.getOpenCount());
75 }
76
77 @Test
78 public void keepOneBranch() {
79 mapping.addLookupEntry(document.getPage(2), new PDPage());
80 PDDocumentOutline outline = new PDDocumentOutline();
81 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
82 assertTrue(outline.hasChildren());
83 assertEquals(2, outline.getOpenCount());
84 }
85
86 @Test
87 public void emptyMapping() {
88 PDDocumentOutline outline = new PDDocumentOutline();
89 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
90 assertFalse(outline.hasChildren());
91 }
92
93 @Test
94 public void rootIsKeptChildIsStripped() {
95 PDPage page1 = new PDPage();
96 PDPage page2 = new PDPage();
97 PDDocument document = new PDDocument();
98 document.addPage(page1);
99 document.addPage(page2);
100 PDDocumentOutline outlines = new PDDocumentOutline();
101 PDOutlineItem root = new PDOutlineItem();
102 root.setTitle("title");
103 root.setDestination(page1);
104 PDOutlineItem child = new PDOutlineItem();
105 child.setTitle("child");
106 child.setDestination(page2);
107 root.addFirst(child);
108 outlines.addFirst(root);
109 document.getDocumentCatalog().setDocumentOutline(outlines);
110 mapping.addLookupEntry(document.getPage(0), new PDPage());
111 PDDocumentOutline outline = new PDDocumentOutline();
112 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
113 assertTrue(outline.hasChildren());
114 assertEquals(1, outline.getOpenCount());
115 }
116
117 @Test
118 public void destinationTypeIsPreservedInLeaves() throws IOException {
119 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
120 getClass().getClassLoader().getResourceAsStream("pdf/large_outline.pdf")))) {
121 mapping.addLookupEntry(document.getPage(2), new PDPage());
122 PDDocumentOutline outline = new PDDocumentOutline();
123 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
124 PDDestination clonedDest = outline.getFirstChild().getDestination();
125 assertThat(clonedDest, is(instanceOf(PDPageFitWidthDestination.class)));
126 assertEquals(806, ((PDPageFitWidthDestination) clonedDest).getTop());
127 }
128 }
129
130 @Test
131 public void destinationTypeIsPreservedInNodes() throws IOException {
132 mapping.addLookupEntry(document.getPage(2), new PDPage());
133 PDDocumentOutline outline = new PDDocumentOutline();
134 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
135 PDDestination clonedDest = outline.getFirstChild().getDestination();
136 assertThat(clonedDest, is(instanceOf(PDPageXYZDestination.class)));
137 assertEquals(759, ((PDPageXYZDestination) clonedDest).getTop());
138 assertEquals(56, ((PDPageXYZDestination) clonedDest).getLeft());
139 }
140
141 @Test
142 public void fallbackHandlesBrokenDestinations() throws IOException {
143 // https://github.com/torakiki/pdfsam/issues/361
144 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(
145 getClass().getClassLoader().getResourceAsStream("pdf/page_dests_with_number_insteadof_refs.pdf")))) {
146 for (PDPage current : document.getPages()) {
147 mapping.addLookupEntry(current, new PDPage());
148 }
149 PDDocumentOutline outline = new PDDocumentOutline();
150 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
151 assertTrue(outline.hasChildren());
152 }
153 }
154
155 @Test
156 public void fallbackHandlesBrokenDestinationsWithNonExistingPageNumber() throws IOException {
157 // https://github.com/torakiki/pdfsam/issues/361
158 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(getClass()
159 .getClassLoader().getResourceAsStream("pdf/page_dests_with_number_insteadof_refs_wrong_num.pdf")))) {
160 for (PDPage current : document.getPages()) {
161 mapping.addLookupEntry(current, new PDPage());
162 }
163 PDDocumentOutline outline = new PDDocumentOutline();
164 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
165 assertTrue(outline.hasChildren());
166 }
167 }
168
169 @Test
170 public void infiniteLoop() throws IOException {
171 try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom(getClass()
172 .getClassLoader().getResourceAsStream("pdf/infinite_outline.pdf")))) {
173
174 for (PDPage current : document.getPages()) {
175 mapping.addLookupEntry(current, new PDPage());
176 }
177 PDDocumentOutline outline = new PDDocumentOutline();
178 new OutlineDistiller(document).appendRelevantOutlineTo(outline, mapping);
179 assertTrue(outline.hasChildren());
180
181 int outlineChildCount = 0;
182 for(PDOutlineItem child: outline.children()) {
183 outlineChildCount++;
184 }
185
186 assertEquals(3, outlineChildCount);
187 }
188 }
189 }