| 1 |
/* |
| 2 |
* Created on 01 feb 2016 |
| 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.optimization; |
| 20 |
|
| 21 |
import static org.junit.Assert.assertEquals; |
| 22 |
import static org.junit.Assert.assertNotNull; |
| 23 |
import static org.junit.Assert.assertNull; |
| 24 |
import static org.junit.Assert.assertTrue; |
| 25 |
import static org.mockito.Mockito.doThrow; |
| 26 |
import static org.mockito.Mockito.mock; |
| 27 |
import static org.mockito.Mockito.when; |
| 28 |
|
| 29 |
import java.io.IOException; |
| 30 |
import java.util.List; |
| 31 |
import java.util.stream.Collectors; |
| 32 |
|
| 33 |
import org.junit.Test; |
| 34 |
import org.sejda.impl.sambox.component.ReadOnlyFilteredCOSStream; |
| 35 |
import org.sejda.io.SeekableSources; |
| 36 |
import org.sejda.sambox.cos.COSDictionary; |
| 37 |
import org.sejda.sambox.cos.COSName; |
| 38 |
import org.sejda.sambox.input.PDFParser; |
| 39 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 40 |
import org.sejda.sambox.pdmodel.PDPage; |
| 41 |
import org.sejda.sambox.pdmodel.common.PDRectangle; |
| 42 |
import org.sejda.sambox.pdmodel.graphics.form.PDFormXObject; |
| 43 |
import org.sejda.sambox.pdmodel.interactive.annotation.PDAppearanceDictionary; |
| 44 |
|
| 45 |
public class ResourcesHitterTest { |
| 46 |
|
| 47 |
private ResourcesHitter victim = new ResourcesHitter(); |
| 48 |
|
| 49 |
@Test |
| 50 |
public void testAccept() throws Exception { |
| 51 |
try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 52 |
getClass().getClassLoader().getResourceAsStream("pdf/draw_w_transparency.pdf")))) { |
| 53 |
document.getPages().forEach(victim::accept); |
| 54 |
PDPage page = document.getPage(0); |
| 55 |
COSDictionary pageRes = page.getResources().getCOSObject(); |
| 56 |
assertTrue(((COSDictionary) pageRes.getDictionaryObject(COSName.XOBJECT)) |
| 57 |
.getDictionaryObject(COSName.getPDFName("x5")).getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 58 |
assertTrue(((COSDictionary) pageRes.getDictionaryObject(COSName.XOBJECT)) |
| 59 |
.getDictionaryObject(COSName.getPDFName("x7")).getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 60 |
PDFormXObject form = (PDFormXObject) page.getResources().getXObject(COSName.getPDFName("x7")); |
| 61 |
COSDictionary formRes = form.getResources().getCOSObject(); |
| 62 |
assertTrue("Hitter should discover forms nested in form xobjects", |
| 63 |
((COSDictionary) formRes.getDictionaryObject(COSName.XOBJECT)) |
| 64 |
.getDictionaryObject(COSName.getPDFName("x10")) |
| 65 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 66 |
PDFormXObject nestedForm = (PDFormXObject) form.getResources().getXObject(COSName.getPDFName("x10")); |
| 67 |
COSDictionary nestedFormRes = nestedForm.getResources().getCOSObject(); |
| 68 |
assertTrue("Hitter should discover images nested in form xobjects", |
| 69 |
((COSDictionary) nestedFormRes.getDictionaryObject(COSName.XOBJECT)) |
| 70 |
.getDictionaryObject(COSName.getPDFName("x17")) |
| 71 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void anotationsAppearanceIsProcessed() throws Exception { |
| 77 |
try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 78 |
getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) { |
| 79 |
document.getPages().forEach(victim::accept); |
| 80 |
PDPage page = document.getPage(0); |
| 81 |
List<PDAppearanceDictionary> appearence = page.getAnnotations().stream() |
| 82 |
.filter(a -> a.getSubtype().equals("Text")).map(a -> a.getAppearance()) |
| 83 |
.collect(Collectors.toList()); |
| 84 |
assertEquals(1, appearence.size()); |
| 85 |
COSDictionary normalAppRes = appearence.get(0).getNormalAppearance().getAppearanceStream().getResources() |
| 86 |
.getCOSObject(); |
| 87 |
assertTrue("Hitter should discover images in appearance streams resource dictionarlies", |
| 88 |
((COSDictionary) normalAppRes.getDictionaryObject(COSName.XOBJECT)) |
| 89 |
.getDictionaryObject(COSName.getPDFName("X0")) |
| 90 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 91 |
assertTrue("Hitter should discover images in appearance streams resource dictionarlies", |
| 92 |
((COSDictionary) normalAppRes.getDictionaryObject(COSName.XOBJECT)) |
| 93 |
.getDictionaryObject(COSName.getPDFName("X1")) |
| 94 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 95 |
|
| 96 |
COSDictionary downAppRes = appearence.get(0).getDownAppearance().getAppearanceStream().getResources() |
| 97 |
.getCOSObject(); |
| 98 |
assertTrue("Hitter should discover images in appearance streams resource dictionarlies", |
| 99 |
downAppRes.getDictionaryObject(COSName.XOBJECT, COSDictionary.class) |
| 100 |
.getDictionaryObject(COSName.getPDFName("X0")) |
| 101 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 102 |
assertTrue("Hitter should discover images in appearance streams resource dictionarlies", |
| 103 |
downAppRes.getDictionaryObject(COSName.XOBJECT, COSDictionary.class) |
| 104 |
.getDictionaryObject(COSName.getPDFName("X1")) |
| 105 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void testType3() throws Exception { |
| 111 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 112 |
.inMemorySeekableSourceFrom(getClass().getClassLoader().getResourceAsStream("pdf/type3.pdf")))) { |
| 113 |
document.getPages().forEach(victim::accept); |
| 114 |
PDPage page = document.getPage(0); |
| 115 |
COSDictionary pageRes = page.getResources().getCOSObject(); |
| 116 |
assertTrue(((COSDictionary) pageRes.getDictionaryObject(COSName.FONT)) |
| 117 |
.getDictionaryObject(COSName.getPDFName("A")).getCOSObject() instanceof InUseDictionary); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void testTilingPattern() throws Exception { |
| 123 |
try (PDDocument document = PDFParser.parse(SeekableSources |
| 124 |
.inMemorySeekableSourceFrom( |
| 125 |
getClass().getClassLoader().getResourceAsStream("pdf/pattern_shared_res.pdf")))) { |
| 126 |
document.getPages().forEach(victim::accept); |
| 127 |
PDPage page = document.getPage(0); |
| 128 |
COSDictionary pageRes = page.getResources().getCOSObject(); |
| 129 |
assertTrue(pageRes.getDictionaryObject(COSName.XOBJECT, COSDictionary.class) |
| 130 |
.getDictionaryObject(COSName.getPDFName("Im1")) |
| 131 |
.getCOSObject() instanceof ReadOnlyFilteredCOSStream); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void exceptionDoesntBubble() { |
| 137 |
PDPage page = mock(PDPage.class); |
| 138 |
when(page.getCropBox()).thenReturn(new PDRectangle(2f, 2f)); |
| 139 |
doThrow(RuntimeException.class).when(page).getResources(); |
| 140 |
victim.accept(page); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void sameFontSameInUseFontInstance() throws IOException { |
| 145 |
try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 146 |
getClass().getClassLoader().getResourceAsStream("pdf/multiple_res_dic_sharing_same_font.pdf")))) { |
| 147 |
document.getPages().forEach(victim::accept); |
| 148 |
PDPage page0 = document.getPage(0); |
| 149 |
InUseDictionary page0Font = page0.getResources().getCOSObject() |
| 150 |
.getDictionaryObject(COSName.FONT, COSDictionary.class) |
| 151 |
.getDictionaryObject(COSName.getPDFName("F1"), InUseDictionary.class); |
| 152 |
PDPage page1 = document.getPage(1); |
| 153 |
InUseDictionary page1Font = page1.getResources().getCOSObject() |
| 154 |
.getDictionaryObject(COSName.FONT, COSDictionary.class) |
| 155 |
.getDictionaryObject(COSName.getPDFName("F1"), InUseDictionary.class); |
| 156 |
assertEquals(page0Font, page1Font); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void unusedExtgstateAreNotHit() throws IOException { |
| 162 |
try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( |
| 163 |
getClass().getClassLoader().getResourceAsStream("pdf/shared_pages_res_unused_extgstate.pdf")))) { |
| 164 |
document.getPages().forEach(victim::accept); |
| 165 |
PDPage page0 = document.getPage(0); |
| 166 |
|
| 167 |
assertNotNull("Hitter should hit used extgstate", |
| 168 |
page0.getResources().getCOSObject().getDictionaryObject(COSName.EXT_G_STATE, COSDictionary.class) |
| 169 |
.getDictionaryObject(COSName.getPDFName("gs2"), InUseDictionary.class)); |
| 170 |
|
| 171 |
assertNull("Hitter should not hit unused extgstate", |
| 172 |
page0.getResources().getCOSObject().getDictionaryObject(COSName.EXT_G_STATE, COSDictionary.class) |
| 173 |
.getDictionaryObject(COSName.getPDFName("gs1"), InUseDictionary.class)); |
| 174 |
|
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} |