Skip to content

Commit fe2db6b

Browse files
authored
Upgrade PDFBox API to v2.0.19 (#396)
1 parent d592d0b commit fe2db6b

49 files changed

Lines changed: 1819 additions & 547 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.tom_roush.pdfbox.pdfwriter;
18+
19+
import android.content.Context;
20+
import android.graphics.Bitmap;
21+
22+
import androidx.test.platform.app.InstrumentationRegistry;
23+
24+
import java.io.File;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.io.OutputStream;
28+
29+
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
30+
import com.tom_roush.pdfbox.android.TestResourceGenerator;
31+
import com.tom_roush.pdfbox.cos.COSName;
32+
import com.tom_roush.pdfbox.pdfparser.PDFStreamParser;
33+
import com.tom_roush.pdfbox.pdmodel.PDDocument;
34+
import com.tom_roush.pdfbox.pdmodel.PDPage;
35+
import com.tom_roush.pdfbox.pdmodel.common.PDStream;
36+
import com.tom_roush.pdfbox.rendering.PDFRenderer;
37+
import com.tom_roush.pdfbox.rendering.TestRendering;
38+
39+
import org.junit.After;
40+
import org.junit.AfterClass;
41+
import org.junit.Before;
42+
import org.junit.BeforeClass;
43+
import org.junit.Test;
44+
45+
import static org.junit.Assume.assumeTrue;
46+
47+
/**
48+
*
49+
* @author Tilman Hausherr
50+
*/
51+
public class ContentStreamWriterTest
52+
{
53+
54+
private File testDirIn;
55+
private File testDirOut;
56+
57+
private Context testContext;
58+
59+
public ContentStreamWriterTest()
60+
{
61+
}
62+
63+
@BeforeClass
64+
public static void setUpClass()
65+
{
66+
}
67+
68+
@AfterClass
69+
public static void tearDownClass()
70+
{
71+
}
72+
73+
@Before
74+
public void setUp()
75+
{
76+
testContext = InstrumentationRegistry.getInstrumentation().getContext();
77+
PDFBoxResourceLoader.init(testContext);
78+
79+
testDirIn = new File(testContext.getCacheDir(), "pdfs");
80+
testDirIn.mkdirs();
81+
testDirOut = new File(testContext.getCacheDir(), "pdfbox-test-output/contentstream");
82+
testDirOut.mkdirs();
83+
}
84+
85+
@After
86+
public void tearDown()
87+
{
88+
}
89+
90+
/**
91+
* Test parse content stream, write back tokens and compare rendering.
92+
*
93+
* @throws java.io.IOException
94+
*/
95+
@Test
96+
public void testPDFBox4750() throws IOException
97+
{
98+
String filename = "PDFBOX-4750.pdf";
99+
File file = TestResourceGenerator.downloadTestResource(testDirIn, filename, "https://issues.apache.org/jira/secure/attachment/12991833/PDFBOX-4750-test.pdf");
100+
assumeTrue(file.exists());
101+
PDDocument doc = PDDocument.load(file);
102+
103+
PDFRenderer r = new PDFRenderer(doc);
104+
for (int i = 0; i < doc.getNumberOfPages(); ++i)
105+
{
106+
Bitmap bim1 = r.renderImageWithDPI(i, 96);
107+
FileOutputStream fileOut = new FileOutputStream(new File(testDirOut, filename + "-" + (i + 1) + ".png"));
108+
bim1.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
109+
fileOut.close();
110+
111+
PDPage page = doc.getPage(i);
112+
PDStream newContent = new PDStream(doc);
113+
114+
PDFStreamParser parser = new PDFStreamParser(page);
115+
parser.parse();
116+
OutputStream os = newContent.createOutputStream(COSName.FLATE_DECODE);
117+
ContentStreamWriter tokenWriter = new ContentStreamWriter(os);
118+
tokenWriter.writeTokens(parser.getTokens());
119+
os.close();
120+
121+
page.setContents(newContent);
122+
}
123+
doc.save(new File(testDirOut, filename));
124+
doc.close();
125+
126+
File renderFile = new File(testDirOut, filename);
127+
TestRendering testRendering = new TestRendering();
128+
testRendering.setUp();
129+
testRendering.render(renderFile);
130+
}
131+
}

library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/interactive/form/AlignmentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class AlignmentTest
3838
private static final String NAME_OF_PDF = "AlignmentTests.pdf";
3939
private static final String TEST_VALUE = "sdfASDF1234äöü";
4040

41-
Context testContext;
41+
private Context testContext;
4242

4343
private PDDocument document;
4444
private PDAcroForm acroForm;

library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/interactive/form/PDAcroFormFlattenTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
/**
4343
* Test flatten different forms and compare with rendering.
4444
*
45-
* The tests are currently disabled to not run within the CI environment
45+
* Some of the tests are currently disabled to not run within the CI environment
4646
* as the test results need manual inspection. Enable as needed.
4747
*
4848
*/
@@ -70,7 +70,7 @@ public void setUp()
7070
/*
7171
* PDFBOX-142 Filled template.
7272
*/
73-
@Test
73+
// @Test
7474
public void testFlattenPDFBOX142() throws IOException
7575
{
7676
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12742551/Testformular1.pdf";
@@ -130,7 +130,7 @@ public void testFlattenPDFBOX2586() throws IOException
130130
/*
131131
* PDFBOX-3083 Filled template rotated.
132132
*/
133-
@Test
133+
// @Test
134134
public void testFlattenPDFBOX3083() throws IOException
135135
{
136136
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12770263/mypdf.pdf";
@@ -202,7 +202,7 @@ public void testFlattenPDFBOX3396_4() throws IOException
202202
/*
203203
* PDFBOX-3587 Empty template.
204204
*/
205-
@Test
205+
// @Test
206206
public void testFlattenOpenOfficeForm() throws IOException
207207
{
208208
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12839977/OpenOfficeForm.pdf";
@@ -214,7 +214,7 @@ public void testFlattenOpenOfficeForm() throws IOException
214214
/*
215215
* PDFBOX-3587 Filled template.
216216
*/
217-
@Test
217+
// @Test
218218
public void testFlattenOpenOfficeFormFilled() throws IOException
219219
{
220220
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12840280/OpenOfficeForm_filled.pdf";
@@ -226,7 +226,7 @@ public void testFlattenOpenOfficeFormFilled() throws IOException
226226
/**
227227
* PDFBOX-4157 Filled template.
228228
*/
229-
@Test
229+
// @Test
230230
public void testFlattenPDFBox4157() throws IOException
231231
{
232232
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12976553/PDFBOX-4157-filled.pdf";
@@ -238,7 +238,7 @@ public void testFlattenPDFBox4157() throws IOException
238238
/**
239239
* PDFBOX-4172 Filled template.
240240
*/
241-
@Test
241+
// @Test
242242
public void testFlattenPDFBox4172() throws IOException
243243
{
244244
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12976552/PDFBOX-4172-filled.pdf";
@@ -250,7 +250,7 @@ public void testFlattenPDFBox4172() throws IOException
250250
/**
251251
* PDFBOX-4615 Filled template.
252252
*/
253-
@Test
253+
// @Test
254254
public void testFlattenPDFBox4615() throws IOException
255255
{
256256
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12976452/resetboundingbox-filled.pdf";
@@ -368,7 +368,7 @@ public boolean accept(File dir, String name)
368368

369369
for (File testFile : testFiles)
370370
{
371-
if (! new File(OUT_DIR, testFile.getName()).exists())
371+
if (!new File(OUT_DIR, testFile.getName()).exists())
372372
{
373373
testFile.delete();
374374
}

library/src/main/java/com/tom_roush/fontbox/cff/CFFParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,14 @@ private static Double readRealNumber(CFFDataInput input, int b0) throws IOExcept
407407
{
408408
return 0d;
409409
}
410-
return Double.valueOf(sb.toString());
410+
try
411+
{
412+
return Double.valueOf(sb.toString());
413+
}
414+
catch (NumberFormatException ex)
415+
{
416+
throw new IOException(ex);
417+
}
411418
}
412419

413420
private CFFFont parseFont(CFFDataInput input, String name, byte[] topDictIndex) throws IOException

library/src/main/java/com/tom_roush/fontbox/cff/Type1CharString.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
public class Type1CharString
3939
{
4040
private Type1CharStringReader font;
41-
private final String fontName, glyphName;
41+
private final String fontName;
42+
private final String glyphName;
4243
private Path path = null;
4344
private int width = 0;
4445
private PointF leftSideBearing = null;
@@ -173,7 +174,7 @@ private List<Number> handleCommand(List<Number> numbers, CharStringCommand comma
173174
}
174175
else if ("vmoveto".equals(name))
175176
{
176-
if (numbers.size() >= 1)
177+
if (!numbers.isEmpty())
177178
{
178179
if (isFlex)
179180
{
@@ -188,7 +189,7 @@ else if ("vmoveto".equals(name))
188189
}
189190
else if ("hmoveto".equals(name))
190191
{
191-
if (numbers.size() >= 1)
192+
if (!numbers.isEmpty())
192193
{
193194
if (isFlex)
194195
{
@@ -210,14 +211,14 @@ else if ("rlineto".equals(name))
210211
}
211212
else if ("hlineto".equals(name))
212213
{
213-
if (numbers.size() >= 1)
214+
if (!numbers.isEmpty())
214215
{
215216
rlineTo(numbers.get(0), 0);
216217
}
217218
}
218219
else if ("vlineto".equals(name))
219220
{
220-
if (numbers.size() >= 1)
221+
if (!numbers.isEmpty())
221222
{
222223
rlineTo(0, numbers.get(0));
223224
}
@@ -284,7 +285,7 @@ else if ("setcurrentpoint".equals(name))
284285
}
285286
else if ("callothersubr".equals(name))
286287
{
287-
if (numbers.size() >= 1)
288+
if (!numbers.isEmpty())
288289
{
289290
callothersubr(numbers.get(0).intValue());
290291
}
@@ -441,7 +442,7 @@ private void rrcurveTo(Number dx1, Number dy1, Number dx2, Number dy2,
441442
}
442443
else
443444
{
444-
path.cubicTo(x1, y1, x2, y2, x3, y3); // TODO: PdfBox-Android Should this be relative?
445+
path.cubicTo(x1, y1, x2, y2, x3, y3);
445446
}
446447
current.set(x3, y3);
447448
}

library/src/main/java/com/tom_roush/fontbox/cff/Type1CharStringParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class Type1CharStringParser
4242
static final int CALLOTHERSUBR = 16;
4343
static final int POP = 17;
4444

45-
private final String fontName, glyphName;
45+
private final String fontName;
46+
private final String glyphName;
4647

4748
/**
4849
* Constructs a new Type1CharStringParser object.

library/src/main/java/com/tom_roush/fontbox/cff/Type2CharString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ else if ("hstemhm".equals(name))
211211
else if ("hintmask".equals(name) || "cntrmask".equals(name))
212212
{
213213
numbers = clearStack(numbers, numbers.size() % 2 != 0);
214-
if (numbers.size() > 0)
214+
if (!numbers.isEmpty())
215215
{
216216
expandStemHints(numbers, false);
217217
}
@@ -308,7 +308,7 @@ private void closePath()
308308

309309
private void drawAlternatingLine(List<Number> numbers, boolean horizontal)
310310
{
311-
while (numbers.size() > 0)
311+
while (!numbers.isEmpty())
312312
{
313313
addCommand(numbers.subList(0, 1), new CharStringCommand(
314314
horizontal ? 6 : 7));

library/src/main/java/com/tom_roush/fontbox/cff/Type2CharStringParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public class Type2CharStringParser
3131
private int vstemCount = 0;
3232
private List<Object> sequence = null;
3333
@SuppressWarnings("unused")
34-
private final String fontName, glyphName;
34+
private final String fontName;
35+
@SuppressWarnings("unused")
36+
private final String glyphName;
3537

3638
/**
3739
* Constructs a new Type1CharStringParser object for a Type 1-equivalent font.

library/src/main/java/com/tom_roush/fontbox/cmap/CMapParser.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,28 @@ else if (nextToken instanceof byte[])
392392
// PDFBOX-3450: ignore <>
393393
if (tokenBytes.length > 0)
394394
{
395-
// PDFBOX-4661: avoid overflow of the last byte, all following values are undefined
396-
int values = Math.min(end - start,
397-
255 - (tokenBytes[tokenBytes.length - 1] & 0xFF)) + 1;
398-
addMappingFrombfrange(result, startCode, values, tokenBytes);
395+
// PDFBOX-4720:
396+
// some pdfs use the malformed bfrange <0000> <FFFF> <0000>. Add support by adding a identity
397+
// mapping for the whole range instead of cutting it after 255 entries
398+
// TODO find a more efficient method to represent all values for a identity mapping
399+
if (tokenBytes.length == 2 && start == 0 && end == 0xffff
400+
&& tokenBytes[0] == 0 && tokenBytes[1] == 0)
401+
{
402+
for (int i = 0; i < 256; i++)
403+
{
404+
startCode[1] = (byte) i;
405+
tokenBytes[1] = (byte) i;
406+
addMappingFrombfrange(result, startCode, 0xff, tokenBytes);
407+
408+
}
409+
}
410+
else
411+
{
412+
// PDFBOX-4661: avoid overflow of the last byte, all following values are undefined
413+
int values = Math.min(end - start,
414+
255 - (tokenBytes[tokenBytes.length - 1] & 0xFF)) + 1;
415+
addMappingFrombfrange(result, startCode, values, tokenBytes);
416+
}
399417
}
400418
}
401419
}

library/src/main/java/com/tom_roush/fontbox/ttf/BufferedRandomAccessFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class BufferedRandomAccessFile extends RandomAccessFile
3636
/**
3737
* Uses a byte instead of a char buffer for efficiency reasons.
3838
*/
39-
private final byte buffer[];
39+
private final byte[] buffer;
4040
private int bufend = 0;
4141
private int bufpos = 0;
4242

@@ -146,7 +146,7 @@ private void invalidate() throws IOException
146146
* {@inheritDoc}
147147
*/
148148
@Override
149-
public int read(byte b[], int off, int len) throws IOException
149+
public int read(byte[] b, int off, int len) throws IOException
150150
{
151151
int leftover = bufend - bufpos;
152152
if (len <= leftover)

0 commit comments

Comments
 (0)