| 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.assertFalse; |
| 22 |
import static org.junit.Assert.assertNotNull; |
| 23 |
import static org.junit.Assert.assertNull; |
| 24 |
import static org.junit.Assert.assertTrue; |
| 25 |
|
| 26 |
import java.io.IOException; |
| 27 |
|
| 28 |
import org.junit.Before; |
| 29 |
import org.junit.Test; |
| 30 |
import org.sejda.impl.sambox.component.ReadOnlyFilteredCOSStream; |
| 31 |
import org.sejda.sambox.cos.COSDictionary; |
| 32 |
import org.sejda.sambox.cos.COSName; |
| 33 |
import org.sejda.sambox.cos.COSStream; |
| 34 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 35 |
import org.sejda.sambox.pdmodel.PDPage; |
| 36 |
|
| 37 |
/** |
| 38 |
* @author Andrea Vacondio |
| 39 |
* |
| 40 |
*/ |
| 41 |
public class ResourceDictionaryCleanerTest { |
| 42 |
private ReadOnlyFilteredCOSStream imageStream; |
| 43 |
private ReadOnlyFilteredCOSStream formStream; |
| 44 |
private InUseDictionary inUseFont; |
| 45 |
private InUseDictionary inUseState; |
| 46 |
private COSStream image; |
| 47 |
private COSStream form; |
| 48 |
|
| 49 |
@Before |
| 50 |
public void setUp() throws IOException { |
| 51 |
image = new COSStream(); |
| 52 |
image.setItem(COSName.TYPE, COSName.XOBJECT); |
| 53 |
image.setItem(COSName.SUBTYPE, COSName.IMAGE); |
| 54 |
form = new COSStream(); |
| 55 |
form.setItem(COSName.TYPE, COSName.XOBJECT); |
| 56 |
form.setItem(COSName.SUBTYPE, COSName.FORM); |
| 57 |
imageStream = ReadOnlyFilteredCOSStream.readOnly(image); |
| 58 |
formStream = ReadOnlyFilteredCOSStream.readOnly(form); |
| 59 |
inUseFont = new InUseDictionary(new COSDictionary()); |
| 60 |
inUseState = new InUseDictionary(new COSDictionary()); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void clean() { |
| 65 |
PDDocument doc = new PDDocument(); |
| 66 |
COSDictionary rootRes = new COSDictionary(); |
| 67 |
COSDictionary rootXobjects = new COSDictionary(); |
| 68 |
rootXobjects.setItem(COSName.getPDFName("keepMe"), imageStream); |
| 69 |
rootXobjects.setItem(COSName.getPDFName("keepMeForm"), formStream); |
| 70 |
rootXobjects.setItem(COSName.getPDFName("discardMe"), image); |
| 71 |
rootXobjects.setItem(COSName.getPDFName("discardMeForm"), form); |
| 72 |
rootRes.setItem(COSName.XOBJECT, rootXobjects); |
| 73 |
COSDictionary rootFonts = new COSDictionary(); |
| 74 |
rootFonts.setItem(COSName.getPDFName("keepMe"), inUseFont); |
| 75 |
rootFonts.setItem(COSName.getPDFName("discardMe"), new COSDictionary()); |
| 76 |
rootRes.setItem(COSName.FONT, rootFonts); |
| 77 |
COSDictionary rootGState = new COSDictionary(); |
| 78 |
rootGState.setItem(COSName.getPDFName("keepMe"), inUseState); |
| 79 |
rootGState.setItem(COSName.getPDFName("discardMe"), new COSDictionary()); |
| 80 |
rootRes.setItem(COSName.EXT_G_STATE, rootGState); |
| 81 |
COSDictionary pageRes = new COSDictionary(); |
| 82 |
COSDictionary pageXobjects = new COSDictionary(); |
| 83 |
pageXobjects.setItem(COSName.getPDFName("keepMe"), imageStream); |
| 84 |
pageXobjects.setItem(COSName.getPDFName("keepMeForm"), formStream); |
| 85 |
pageXobjects.setItem(COSName.getPDFName("discardMe"), image); |
| 86 |
pageXobjects.setItem(COSName.getPDFName("discardMeToo"), image); |
| 87 |
pageXobjects.setItem(COSName.getPDFName("discardMeForm"), form); |
| 88 |
pageRes.setItem(COSName.XOBJECT, pageXobjects); |
| 89 |
COSDictionary pageFonts = new COSDictionary(); |
| 90 |
pageFonts.setItem(COSName.getPDFName("keepMe"), inUseFont); |
| 91 |
pageFonts.setItem(COSName.getPDFName("discardMe"), new COSDictionary()); |
| 92 |
pageFonts.setItem(COSName.getPDFName("discardMeToo"), new COSDictionary()); |
| 93 |
pageRes.setItem(COSName.FONT, pageFonts); |
| 94 |
COSDictionary pageGStates = new COSDictionary(); |
| 95 |
pageGStates.setItem(COSName.getPDFName("keepMe"), inUseFont); |
| 96 |
pageGStates.setItem(COSName.getPDFName("discardMe"), new COSDictionary()); |
| 97 |
pageGStates.setItem(COSName.getPDFName("discardMeToo"), new COSDictionary()); |
| 98 |
pageRes.setItem(COSName.EXT_G_STATE, pageGStates); |
| 99 |
doc.getDocumentCatalog().getPages().getCOSObject().setItem(COSName.RESOURCES, rootRes); |
| 100 |
PDPage page0 = new PDPage(); |
| 101 |
PDPage page1 = new PDPage(); |
| 102 |
page1.getCOSObject().setItem(COSName.RESOURCES, pageRes); |
| 103 |
doc.getDocumentCatalog().getPages().add(page0); |
| 104 |
doc.getDocumentCatalog().getPages().add(page1); |
| 105 |
assertCurrentStatus(page0, page1); |
| 106 |
new ResourceDictionaryCleaner().accept(doc); |
| 107 |
assertCleanedStatus(page0, page1); |
| 108 |
} |
| 109 |
|
| 110 |
private void assertCurrentStatus(PDPage page0, PDPage page1) { |
| 111 |
assertTrue(page0.getResources().isImageXObject(COSName.getPDFName("keepMe"))); |
| 112 |
assertTrue(page0.getResources().isImageXObject(COSName.getPDFName("discardMe"))); |
| 113 |
assertTrue(page0.getResources().isFormXObject(COSName.getPDFName("keepMeForm"))); |
| 114 |
assertTrue(page0.getResources().isFormXObject(COSName.getPDFName("discardMeForm"))); |
| 115 |
assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("keepMe"))); |
| 116 |
assertTrue(page1.getResources().isFormXObject(COSName.getPDFName("keepMeForm"))); |
| 117 |
assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("discardMe"))); |
| 118 |
assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("discardMeToo"))); |
| 119 |
assertTrue(page1.getResources().isFormXObject(COSName.getPDFName("discardMeForm"))); |
| 120 |
COSDictionary page0fonts = page0.getResources().getCOSObject().getDictionaryObject(COSName.FONT, |
| 121 |
COSDictionary.class); |
| 122 |
assertNotNull(page0fonts.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 123 |
assertNotNull(page0fonts.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 124 |
COSDictionary page1fonts = page1.getResources().getCOSObject().getDictionaryObject(COSName.FONT, |
| 125 |
COSDictionary.class); |
| 126 |
assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 127 |
assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 128 |
assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMeToo"))); |
| 129 |
COSDictionary page0states = page0.getResources().getCOSObject().getDictionaryObject(COSName.EXT_G_STATE, |
| 130 |
COSDictionary.class); |
| 131 |
assertNotNull(page0states.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 132 |
assertNotNull(page0states.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 133 |
COSDictionary page1states = page1.getResources().getCOSObject().getDictionaryObject(COSName.EXT_G_STATE, |
| 134 |
COSDictionary.class); |
| 135 |
assertNotNull(page1states.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 136 |
assertNotNull(page1states.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 137 |
assertNotNull(page1states.getDictionaryObject(COSName.getPDFName("discardMeToo"))); |
| 138 |
} |
| 139 |
|
| 140 |
private void assertCleanedStatus(PDPage page0, PDPage page1) { |
| 141 |
assertTrue(page0.getResources().isImageXObject(COSName.getPDFName("keepMe"))); |
| 142 |
assertTrue(page0.getResources().isFormXObject(COSName.getPDFName("keepMeForm"))); |
| 143 |
assertFalse(page0.getResources().isImageXObject(COSName.getPDFName("discardMe"))); |
| 144 |
assertFalse(page0.getResources().isFormXObject(COSName.getPDFName("discardMeForm"))); |
| 145 |
assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("keepMe"))); |
| 146 |
assertTrue(page1.getResources().isFormXObject(COSName.getPDFName("keepMeForm"))); |
| 147 |
assertFalse(page1.getResources().isImageXObject(COSName.getPDFName("discardMe"))); |
| 148 |
assertFalse(page1.getResources().isImageXObject(COSName.getPDFName("discardMeToo"))); |
| 149 |
assertFalse(page1.getResources().isFormXObject(COSName.getPDFName("discardMeForm"))); |
| 150 |
COSDictionary page0fonts = page0.getResources().getCOSObject().getDictionaryObject(COSName.FONT, |
| 151 |
COSDictionary.class); |
| 152 |
assertNotNull(page0fonts.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 153 |
assertNull(page0fonts.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 154 |
COSDictionary page1fonts = page1.getResources().getCOSObject().getDictionaryObject(COSName.FONT, |
| 155 |
COSDictionary.class); |
| 156 |
assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 157 |
assertNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 158 |
assertNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMeToo"))); |
| 159 |
COSDictionary page0states = page0.getResources().getCOSObject().getDictionaryObject(COSName.EXT_G_STATE, |
| 160 |
COSDictionary.class); |
| 161 |
assertNotNull(page0states.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 162 |
assertNull(page0states.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 163 |
COSDictionary page1states = page1.getResources().getCOSObject().getDictionaryObject(COSName.EXT_G_STATE, |
| 164 |
COSDictionary.class); |
| 165 |
assertNotNull(page1states.getDictionaryObject(COSName.getPDFName("keepMe"))); |
| 166 |
assertNull(page1states.getDictionaryObject(COSName.getPDFName("discardMe"))); |
| 167 |
assertNull(page1states.getDictionaryObject(COSName.getPDFName("discardMeToo"))); |
| 168 |
} |
| 169 |
|
| 170 |
@Test |
| 171 |
public void noExceptionMissingResources() { |
| 172 |
PDDocument doc = new PDDocument(); |
| 173 |
PDPage page0 = new PDPage(); |
| 174 |
page0.getCOSObject().setItem(COSName.RESOURCES, null); |
| 175 |
doc.getDocumentCatalog().getPages().add(page0); |
| 176 |
new ResourceDictionaryCleaner().accept(doc); |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
public void noExceptionMissingXObjects() { |
| 181 |
PDDocument doc = new PDDocument(); |
| 182 |
PDPage page0 = new PDPage(); |
| 183 |
COSDictionary rootRes = new COSDictionary(); |
| 184 |
page0.getCOSObject().setItem(COSName.RESOURCES, rootRes); |
| 185 |
rootRes.setItem(COSName.XOBJECT, null); |
| 186 |
doc.getDocumentCatalog().getPages().add(page0); |
| 187 |
new ResourceDictionaryCleaner().accept(doc); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void noExceptionMissingFonts() { |
| 192 |
PDDocument doc = new PDDocument(); |
| 193 |
PDPage page0 = new PDPage(); |
| 194 |
COSDictionary rootRes = new COSDictionary(); |
| 195 |
page0.getCOSObject().setItem(COSName.RESOURCES, rootRes); |
| 196 |
rootRes.setItem(COSName.FONT, null); |
| 197 |
doc.getDocumentCatalog().getPages().add(page0); |
| 198 |
new ResourceDictionaryCleaner().accept(doc); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void noExceptionMissingExtgstate() { |
| 203 |
PDDocument doc = new PDDocument(); |
| 204 |
PDPage page0 = new PDPage(); |
| 205 |
COSDictionary rootRes = new COSDictionary(); |
| 206 |
page0.getCOSObject().setItem(COSName.RESOURCES, rootRes); |
| 207 |
rootRes.setItem(COSName.EXT_G_STATE, null); |
| 208 |
doc.getDocumentCatalog().getPages().add(page0); |
| 209 |
new ResourceDictionaryCleaner().accept(doc); |
| 210 |
} |
| 211 |
} |