Skip to content

Commit e50f8ce

Browse files
authored
Upgrade PDFBox API to v2.0.6 (TomRoush#327)
1 parent eed9be2 commit e50f8ce

File tree

86 files changed

+1791
-1111
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1791
-1111
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2015 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.tom_roush.fontbox.ttf;
17+
18+
import android.content.Context;
19+
20+
import androidx.test.platform.app.InstrumentationRegistry;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
27+
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
28+
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertFalse;
34+
import static org.junit.Assert.assertTrue;
35+
36+
/**
37+
*
38+
* @author Tilman Hausherr
39+
*/
40+
public class TTFSubsetterInstrumentationTest
41+
{
42+
43+
private Context testContext;
44+
45+
@Before
46+
public void setUp() throws Exception
47+
{
48+
testContext = InstrumentationRegistry.getInstrumentation().getContext();
49+
PDFBoxResourceLoader.init(testContext);
50+
}
51+
52+
/**
53+
* Test of PDFBOX-3757: check that postcript names that are not part of WGL4Names don't get
54+
* shuffled in buildPostTable().
55+
*
56+
* @throws java.io.IOException
57+
*/
58+
@Test
59+
public void testPDFBox3757() throws IOException
60+
{
61+
InputStream testFile = testContext.getAssets().open("fontbox/ttf/LiberationSans-Regular.ttf");
62+
63+
TrueTypeFont ttf = new TTFParser().parse(testFile);
64+
TTFSubsetter ttfSubsetter = new TTFSubsetter(ttf);
65+
ttfSubsetter.add('Ö');
66+
ttfSubsetter.add('\u200A');
67+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
68+
ttfSubsetter.writeToStream(baos);
69+
TrueTypeFont subset = new TTFParser(true).parse(new ByteArrayInputStream(baos.toByteArray()));
70+
71+
assertEquals(5, subset.getNumberOfGlyphs());
72+
73+
assertEquals(0, subset.nameToGID(".notdef"));
74+
assertEquals(1, subset.nameToGID("O"));
75+
assertEquals(2, subset.nameToGID("Odieresis"));
76+
assertEquals(3, subset.nameToGID("uni200A"));
77+
assertEquals(4, subset.nameToGID("dieresis.uc"));
78+
79+
PostScriptTable pst = subset.getPostScript();
80+
assertEquals(pst.getName(0), ".notdef");
81+
assertEquals(pst.getName(1), "O");
82+
assertEquals(pst.getName(2), "Odieresis");
83+
assertEquals(pst.getName(3), "uni200A");
84+
assertEquals(pst.getName(4), "dieresis.uc");
85+
86+
assertTrue("Hair space path should be empty", subset.getPath("uni200A").isEmpty());
87+
assertFalse("UC dieresis path should not be empty", subset.getPath("dieresis.uc").isEmpty());
88+
89+
subset.close();
90+
}
91+
}

library/src/androidTest/java/com/tom_roush/pdfbox/encryption/TestPublicKeyEncryption.java

+25-34
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import androidx.test.platform.app.InstrumentationRegistry;
2222

23-
import java.io.ByteArrayInputStream;
2423
import java.io.ByteArrayOutputStream;
2524
import java.io.IOException;
2625
import java.io.InputStream;
@@ -30,26 +29,23 @@
3029

3130
import javax.crypto.Cipher;
3231

32+
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
3333
import com.tom_roush.pdfbox.io.MemoryUsageSetting;
3434
import com.tom_roush.pdfbox.pdmodel.PDDocument;
3535
import com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission;
3636
import com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
3737
import com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyRecipient;
38-
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
3938

40-
import org.junit.After;
4139
import org.junit.Assert;
42-
import org.junit.Before;
43-
import org.junit.Test;
4440

45-
import static org.junit.Assert.fail;
41+
import junit.framework.TestCase;
4642

4743
/**
4844
* Tests for public key encryption.
4945
*
5046
* @author Ben Litchfield
5147
*/
52-
public class TestPublicKeyEncryption
48+
public class TestPublicKeyEncryption extends TestCase
5349
{
5450

5551
private AccessPermission permission1;
@@ -64,7 +60,7 @@ public class TestPublicKeyEncryption
6460
private String password1;
6561
private String password2;
6662

67-
Context testContext;
63+
private Context testContext;
6864
private final String path = "pdfbox/com/tom_roush/pdfbox/pdmodel/encryption/";
6965

7066
/**
@@ -73,8 +69,11 @@ public class TestPublicKeyEncryption
7369
private PDDocument document;
7470

7571

76-
@Before
77-
public void setUp() throws Exception
72+
/**
73+
* {@inheritDoc}
74+
*/
75+
@Override
76+
protected void setUp() throws Exception
7877
{
7978
if (Cipher.getMaxAllowedKeyLength("AES") != Integer.MAX_VALUE)
8079
{
@@ -114,19 +113,14 @@ public void setUp() throws Exception
114113
keyStore1 = "test1.pfx";
115114
keyStore2 = "test2.pfx";
116115

117-
InputStream input = testContext.getAssets().open(path + "test.pdf");
118-
try
119-
{
120-
document = PDDocument.load(input);
121-
}
122-
finally
123-
{
124-
input.close();
125-
}
116+
document = PDDocument.load(testContext.getAssets().open(path + "test.pdf"));
126117
}
127118

128-
@After
129-
public void tearDown() throws Exception
119+
/**
120+
* {@inheritDoc}
121+
*/
122+
@Override
123+
protected void tearDown() throws Exception
130124
{
131125
document.close();
132126
}
@@ -137,7 +131,6 @@ public void tearDown() throws Exception
137131
*
138132
* @throws Exception If there is an unexpected error during the test.
139133
*/
140-
@Test
141134
public void testProtectionError() throws Exception
142135
{
143136
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
@@ -155,7 +148,7 @@ public void testProtectionError() throws Exception
155148
{
156149
String msg = ex.getMessage();
157150
Assert.assertTrue("not the expected exception: " + msg,
158-
msg.contains("serial-#: rid 2 vs. cert 3"));
151+
msg.contains("serial-#: rid 2 vs. cert 3"));
159152
}
160153
finally
161154
{
@@ -173,7 +166,6 @@ public void testProtectionError() throws Exception
173166
*
174167
* @throws Exception If there is an unexpected error during the test.
175168
*/
176-
@Test
177169
public void testProtection() throws Exception
178170
{
179171
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
@@ -186,7 +178,7 @@ public void testProtection() throws Exception
186178
Assert.assertTrue(encryptedDoc.isEncrypted());
187179

188180
AccessPermission permission =
189-
encryptedDoc.getCurrentAccessPermission();
181+
encryptedDoc.getCurrentAccessPermission();
190182
Assert.assertFalse(permission.canAssembleDocument());
191183
Assert.assertFalse(permission.canExtractContent());
192184
Assert.assertTrue(permission.canExtractForAccessibility());
@@ -208,7 +200,6 @@ public void testProtection() throws Exception
208200
*
209201
* @throws Exception If there is an error during the test.
210202
*/
211-
@Test
212203
public void testMultipleRecipients() throws Exception
213204
{
214205
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
@@ -221,7 +212,7 @@ public void testMultipleRecipients() throws Exception
221212
try
222213
{
223214
AccessPermission permission =
224-
encryptedDoc1.getCurrentAccessPermission();
215+
encryptedDoc1.getCurrentAccessPermission();
225216
Assert.assertFalse(permission.canAssembleDocument());
226217
Assert.assertFalse(permission.canExtractContent());
227218
Assert.assertTrue(permission.canExtractForAccessibility());
@@ -241,7 +232,7 @@ public void testMultipleRecipients() throws Exception
241232
try
242233
{
243234
AccessPermission permission =
244-
encryptedDoc2.getCurrentAccessPermission();
235+
encryptedDoc2.getCurrentAccessPermission();
245236
Assert.assertFalse(permission.canAssembleDocument());
246237
Assert.assertFalse(permission.canExtractContent());
247238
Assert.assertTrue(permission.canExtractForAccessibility());
@@ -265,14 +256,14 @@ public void testMultipleRecipients() throws Exception
265256
* @param decryptionPassword password to be used to decrypt the doc
266257
* @param keyStore password to be used to decrypt the doc
267258
* @return reloaded document
268-
* @throws Exception if
259+
* @throws Exception if
269260
*/
270261
private PDDocument reload(PDDocument doc, String decryptionPassword, InputStream keyStore)
271-
throws IOException, NoSuchAlgorithmException
262+
throws IOException, NoSuchAlgorithmException
272263
{
273-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
274-
doc.save(buffer);
275-
return PDDocument.load(new ByteArrayInputStream(buffer.toByteArray()), decryptionPassword,
264+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
265+
doc.save(baos);
266+
return PDDocument.load(baos.toByteArray(), decryptionPassword,
276267
keyStore, null, MemoryUsageSetting.setupMainMemoryOnly());
277268
}
278269

@@ -294,7 +285,7 @@ private PublicKeyRecipient getRecipient(String certificate, AccessPermission per
294285
PublicKeyRecipient recipient = new PublicKeyRecipient();
295286
recipient.setPermission(permission);
296287
recipient.setX509(
297-
(X509Certificate) factory.generateCertificate(input));
288+
(X509Certificate) factory.generateCertificate(input));
298289
return recipient;
299290
}
300291
finally

0 commit comments

Comments
 (0)