Debian Codegraph @

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

raw
1 /*
2 * Created on 16/ago/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.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.instanceOf;
23 import static org.hamcrest.Matchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38 import org.junit.Test;
39 import org.sejda.io.SeekableSources;
40 import org.sejda.sambox.cos.COSArray;
41 import org.sejda.sambox.cos.COSInteger;
42 import org.sejda.sambox.cos.COSName;
43 import org.sejda.sambox.input.PDFParser;
44 import org.sejda.sambox.pdmodel.PDDocument;
45 import org.sejda.sambox.pdmodel.PDDocumentCatalog;
46 import org.sejda.sambox.pdmodel.PDPage;
47 import org.sejda.sambox.pdmodel.common.PDRectangle;
48 import org.sejda.sambox.pdmodel.interactive.action.PDActionGoTo;
49 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination;
50 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
51 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageFitDestination;
52 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageFitHeightDestination;
53 import org.sejda.sambox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
54 import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
55 import org.sejda.sambox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
56
57 /**
58 * @author Andrea Vacondio
59 *
60 */
61 public class OutlineUtilsTest {
62
63 @Test
64 public void toPageDestinationEmpty() {
65 PDOutlineItem victim = new PDOutlineItem();
66 assertFalse(OutlineUtils.toPageDestination(victim, null).isPresent());
67 }
68
69 @Test
70 public void toPageDestinationAction() {
71 PDPageFitDestination destination = new PDPageFitDestination();
72 PDPage page = new PDPage();
73 destination.setPage(page);
74 PDActionGoTo action = new PDActionGoTo();
75 action.setDestination(destination);
76 PDOutlineItem victim = new PDOutlineItem();
77 victim.setAction(action);
78 assertEquals(destination.getPage(), OutlineUtils.toPageDestination(victim, null).get().getPage());
79 }
80
81 @Test
82 public void toPageDestinationDestination() {
83 PDPageFitDestination destination = new PDPageFitDestination();
84 destination.setPageNumber(5);
85 PDOutlineItem victim = new PDOutlineItem();
86 victim.setDestination(destination);
87 PDDocumentCatalog catalog = mock(PDDocumentCatalog.class);
88 assertEquals(5, OutlineUtils.toPageDestination(victim, catalog).get().getPageNumber());
89 }
90
91 @Test
92 public void toPageDestinationDestinationNullCatalog() {
93 PDPageFitDestination dest = new PDPageFitDestination();
94 dest.setPageNumber(5);
95 PDNamedDestination destination = new PDNamedDestination();
96 destination.setNamedDestination("ChuckNorris");
97 PDOutlineItem victim = new PDOutlineItem();
98 victim.setDestination(destination);
99 PDDocumentCatalog catalog = null;
100 assertFalse(OutlineUtils.toPageDestination(victim, catalog).isPresent());
101 }
102
103 @Test
104 public void toPageDestinationNamedDestinationNullNames() {
105 PDNamedDestination destination = new PDNamedDestination();
106 PDOutlineItem victim = new PDOutlineItem();
107 victim.setDestination(destination);
108 assertFalse(OutlineUtils.toPageDestination(victim, null).isPresent());
109 }
110
111 @Test
112 public void toPageDestinationNamedDestination() throws IOException {
113 PDPageFitDestination dest = new PDPageFitDestination();
114 dest.setPageNumber(5);
115 PDNamedDestination destination = new PDNamedDestination();
116 destination.setNamedDestination("ChuckNorris");
117 PDOutlineItem victim = new PDOutlineItem();
118 victim.setDestination(destination);
119 PDDocumentCatalog catalog = mock(PDDocumentCatalog.class);
120 when(catalog.findNamedDestinationPage(any())).thenReturn(dest);
121 assertEquals(dest, OutlineUtils.toPageDestination(victim, catalog).get());
122 }
123
124 @Test(expected = IllegalArgumentException.class)
125 public void pageDestinationForNull() {
126 OutlineUtils.pageDestinationFor(null);
127 }
128
129 @Test
130 public void pageDestinationFor() {
131 PDPage page = new PDPage(PDRectangle.A4);
132 PDPageXYZDestination noRotation = OutlineUtils.pageDestinationFor(page);
133 assertEquals(0, noRotation.getLeft());
134 assertEquals((int) PDRectangle.A4.getHeight(), noRotation.getTop());
135
136 page.setRotation(90);
137 PDPageXYZDestination rotation90 = OutlineUtils.pageDestinationFor(page);
138 assertEquals(0, rotation90.getLeft());
139 assertEquals(0, rotation90.getTop());
140
141 page.setRotation(180);
142 PDPageXYZDestination rotation180 = OutlineUtils.pageDestinationFor(page);
143 assertEquals((int) PDRectangle.A4.getWidth(), rotation180.getLeft());
144 assertEquals(0, rotation180.getTop());
145
146 page.setRotation(270);
147 PDPageXYZDestination rotation270 = OutlineUtils.pageDestinationFor(page);
148 assertEquals((int) PDRectangle.A4.getWidth(), rotation270.getLeft());
149 assertEquals((int) PDRectangle.A4.getHeight(), rotation270.getTop());
150 }
151
152 @Test(expected = IllegalArgumentException.class)
153 public void clonePageDestinationNullDest() {
154 OutlineUtils.clonePageDestination(null, new PDPage());
155 }
156
157 @Test
158 public void clonePageDestinationSameType() {
159 PDPage origin = new PDPage();
160 PDPage newPage = new PDPage();
161 PDPageFitHeightDestination destination = new PDPageFitHeightDestination();
162 destination.setPage(origin);
163 destination.setLeft(20);
164 PDPageDestination cloned = OutlineUtils.clonePageDestination(destination, newPage);
165 assertThat(cloned, is(instanceOf(PDPageFitHeightDestination.class)));
166 assertEquals(newPage, cloned.getPage());
167 assertEquals(20, ((PDPageFitHeightDestination) cloned).getLeft());
168 }
169
170 @Test
171 public void copyDictionary() {
172 PDOutlineItem from = new PDOutlineItem();
173 from.setBold(true);
174 from.setItalic(true);
175 from.setTitle("Chuck");
176 PDOutlineItem to = new PDOutlineItem();
177 to.setBold(false);
178 to.setItalic(false);
179 OutlineUtils.copyOutlineDictionary(from, to);
180 assertTrue(to.isBold());
181 assertTrue(to.isItalic());
182 assertEquals("Chuck", to.getTitle());
183 assertNull(to.getCOSObject().getDictionaryObject(COSName.C));
184 }
185
186 @Test
187 // title is required by the spec
188 public void copyDictionaryFixesNullTitles() {
189 PDOutlineItem from = new PDOutlineItem();
190 from.setTitle(null);
191 PDOutlineItem to = new PDOutlineItem();
192 assertNull(from.getTitle());
193 OutlineUtils.copyOutlineDictionary(from, to);
194 assertNotNull(to.getTitle());
195 }
196
197 @Test
198 public void noOutlineFlat() throws IOException {
199 try (PDDocument doc = PDFParser.parse(SeekableSources
200 .inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/test_no_outline.pdf")))) {
201 assertTrue(OutlineUtils.getFlatOutline(doc).isEmpty());
202 }
203 }
204
205 @Test
206 public void outlineFlat() throws IOException {
207 try (PDDocument doc = PDFParser.parse(
208 SeekableSources.inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/test_outline.pdf")))) {
209 assertEquals(5, OutlineUtils.getFlatOutline(doc).size());
210 }
211 }
212
213 @Test
214 public void outlineFlatIntPageDestinations() throws IOException {
215 try (PDDocument doc = PDFParser.parse(SeekableSources
216 .inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/destination_pages_as_int.pdf")))) {
217 List<OutlineItem> flatOutline = OutlineUtils.getFlatOutline(doc);
218 assertEquals(5, flatOutline.size());
219 assertEquals(1, flatOutline.get(0).page);
220 assertEquals(1, flatOutline.get(1).page);
221 assertEquals(3, flatOutline.get(2).page);
222 assertEquals(3, flatOutline.get(3).page);
223 assertEquals(5, flatOutline.get(4).page);
224 }
225 }
226
227 @Test
228 public void outlineLevels() throws IOException {
229 try (PDDocument doc = PDFParser.parse(
230 SeekableSources.inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/test_outline.pdf")))) {
231 Set<Integer> levels = OutlineUtils.getOutlineLevelsWithPageDestination(doc);
232 assertEquals(3, levels.size());
233 assertTrue(levels.contains(1));
234 assertTrue(levels.contains(2));
235 assertTrue(levels.contains(3));
236 }
237 }
238
239 @Test
240 public void outlineLevelsParentHasNoPageDest() {
241 PDPage page1 = new PDPage();
242 PDDocument document = new PDDocument();
243 document.addPage(page1);
244 PDDocumentOutline outlines = new PDDocumentOutline();
245 PDOutlineItem root = new PDOutlineItem();
246 root.setTitle("title");
247 PDOutlineItem child = new PDOutlineItem();
248 child.setTitle("child");
249 PDOutlineItem child2 = new PDOutlineItem();
250 child2.setTitle("child2");
251 child2.setDestination(page1);
252 child.addFirst(child2);
253 root.addLast(child);
254 outlines.addFirst(root);
255 document.getDocumentCatalog().setDocumentOutline(outlines);
256 Set<Integer> levels = OutlineUtils.getOutlineLevelsWithPageDestination(document);
257 assertEquals(1, levels.size());
258 assertTrue(levels.contains(3));
259 }
260
261 @Test
262 public void rootNoDestinationAndSorted() {
263 PDPage page1 = new PDPage();
264 PDPage page2 = new PDPage();
265 PDDocument document = new PDDocument();
266 document.addPage(page1);
267 document.addPage(page2);
268 PDDocumentOutline outlines = new PDDocumentOutline();
269 PDOutlineItem root = new PDOutlineItem();
270 root.setTitle("title");
271 PDOutlineItem child = new PDOutlineItem();
272 child.setTitle("child");
273 child.setDestination(page2);
274 PDOutlineItem child2 = new PDOutlineItem();
275 child2.setTitle("child2");
276 child2.setDestination(page1);
277 root.addFirst(child);
278 root.addLast(child2);
279 outlines.addFirst(root);
280 document.getDocumentCatalog().setDocumentOutline(outlines);
281 List<OutlineItem> flat = OutlineUtils.getFlatOutline(document);
282 assertEquals(2, flat.size());
283 assertEquals("child2", flat.get(0).title);
284 }
285
286 @Test
287 public void resolvePageDestination() throws IOException {
288 PDPage page1 = new PDPage();
289 PDPage page2 = new PDPage();
290 PDDocument document = new PDDocument();
291 document.addPage(page1);
292 document.addPage(page2);
293 PDDocumentOutline outlines = new PDDocumentOutline();
294 PDOutlineItem root = new PDOutlineItem();
295 root.setTitle("title");
296 PDOutlineItem child = new PDOutlineItem();
297 child.setTitle("child");
298 child.setDestination(page2);
299 PDOutlineItem child2 = new PDOutlineItem();
300 child2.setTitle("child2");
301 child2.setDestination(page1);
302 child2.getCOSObject().getDictionaryObject(COSName.DEST, COSArray.class).set(0, COSInteger.ONE);
303 root.addFirst(child);
304 root.addLast(child2);
305 outlines.addFirst(root);
306 document.getDocumentCatalog().setDocumentOutline(outlines);
307 assertNotNull(OutlineUtils.resolvePageDestination((PDPageDestination) child2.getDestination(), document));
308 assertNotNull(OutlineUtils.resolvePageDestination((PDPageDestination) child.getDestination(), document));
309 PDPageDestination negative = new PDPageXYZDestination();
310 negative.setPageNumber(-1);
311 assertNull(OutlineUtils.resolvePageDestination(negative, document));
312 PDPageDestination notFound = new PDPageXYZDestination();
313 notFound.setPageNumber(10);
314 assertNull(OutlineUtils.resolvePageDestination(notFound, document));
315 }
316
317 @Test
318 public void pageGroupedPageDestinations() {
319 PDPage page1 = new PDPage();
320 PDPage page2 = new PDPage();
321 PDPage page3 = new PDPage();
322 PDDocument document = new PDDocument();
323 document.addPage(page1);
324 document.addPage(page2);
325 document.addPage(page3);
326 PDDocumentOutline outlines = new PDDocumentOutline();
327 PDOutlineItem root = new PDOutlineItem();
328 root.setTitle("title");
329 PDOutlineItem child = new PDOutlineItem();
330 child.setTitle("child");
331 child.setDestination(page2);
332 PDOutlineItem child2 = new PDOutlineItem();
333 child2.setTitle("child2");
334 child2.setDestination(page1);
335 PDOutlineItem child3 = new PDOutlineItem();
336 child3.setTitle("child3");
337 PDPageFitHeightDestination gotoDest = new PDPageFitHeightDestination();
338 gotoDest.setLeft(50);
339 gotoDest.setPage(page1);
340 PDActionGoTo action = new PDActionGoTo();
341 action.setDestination(gotoDest);
342 child3.setAction(action);
343 child2.getCOSObject().getDictionaryObject(COSName.DEST, COSArray.class).set(0, COSInteger.ONE);
344 PDOutlineItem child4 = new PDOutlineItem();
345 child4.setTitle("child4");
346 child4.setDestination(page3);
347 child3.addLast(child4);
348 root.addFirst(child);
349 root.addLast(child2);
350 root.addLast(child3);
351 outlines.addFirst(root);
352 document.getDocumentCatalog().setDocumentOutline(outlines);
353 Map<PDPage, Set<PDPageDestination>> victim = OutlineUtils.pageGroupedOutlinePageDestinations(document);
354 assertEquals(3, victim.size());
355 assertEquals(1, victim.get(page1).size());
356 assertEquals(2, victim.get(page2).size());
357 assertEquals(1, victim.get(page3).size());
358 }
359 }