Skip to content

Commit 3c6c526

Browse files
authored
Upgrade PDFBox API to v2.0.21 (#404)
1 parent ad6b957 commit 3c6c526

File tree

64 files changed

+2072
-775
lines changed

Some content is hidden

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

64 files changed

+2072
-775
lines changed

library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/font/PDFontTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.tom_roush.pdfbox.pdmodel.font;
1919

2020
import android.content.Context;
21+
import android.util.Log;
2122

2223
import androidx.test.platform.app.InstrumentationRegistry;
2324

@@ -28,10 +29,15 @@
2829
import java.io.IOException;
2930
import java.io.InputStream;
3031
import java.io.OutputStream;
32+
import java.net.URI;
3133
import java.net.URISyntaxException;
34+
import java.util.ArrayList;
35+
import java.util.List;
3236

3337
import com.tom_roush.fontbox.ttf.TTFParser;
38+
import com.tom_roush.fontbox.ttf.TrueTypeCollection;
3439
import com.tom_roush.fontbox.ttf.TrueTypeFont;
40+
import com.tom_roush.fontbox.util.autodetect.FontFileFinder;
3541
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
3642
import com.tom_roush.pdfbox.android.TestResourceGenerator;
3743
import com.tom_roush.pdfbox.cos.COSName;
@@ -205,6 +211,53 @@ public void testPDFox4318() throws IOException
205211
}
206212
}
207213

214+
@Test
215+
public void testFullEmbeddingTTC() throws IOException
216+
{
217+
FontFileFinder fff = new FontFileFinder();
218+
TrueTypeCollection ttc = null;
219+
for (URI uri : fff.find())
220+
{
221+
if (uri.getPath().endsWith(".ttc"))
222+
{
223+
File file = new File(uri);
224+
Log.i("PdfBox-Android", "TrueType collection file: " + file);
225+
ttc = new TrueTypeCollection(file);
226+
break;
227+
}
228+
}
229+
if (ttc == null)
230+
{
231+
Log.i("PdfBox-Android", "testFullEmbeddingTTC skipped, no .ttc files available");
232+
return;
233+
}
234+
235+
final List<String> names = new ArrayList<String>();
236+
ttc.processAllFonts(new TrueTypeCollection.TrueTypeFontProcessor()
237+
{
238+
@Override
239+
public void process(TrueTypeFont ttf) throws IOException
240+
{
241+
Log.i("PdfBox-Android", "TrueType font in collection: " + ttf.getName());
242+
names.add(ttf.getName());
243+
}
244+
});
245+
246+
TrueTypeFont ttf = ttc.getFontByName(names.get(0)); // take the first one
247+
Log.i("PdfBox-Android", "TrueType font used for test: " + ttf.getName());
248+
249+
try
250+
{
251+
PDType0Font.load(new PDDocument(), ttf, false);
252+
}
253+
catch (IOException ex)
254+
{
255+
Assert.assertEquals("Full embedding of TrueType font collections not supported", ex.getMessage());
256+
return;
257+
}
258+
Assert.fail("should have thrown IOException");
259+
}
260+
208261
private void testPDFBox3826checkFonts(byte[] byteArray, File fontFile) throws IOException
209262
{
210263
PDDocument doc = PDDocument.load(byteArray);

library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/interactive/form/MultilineFieldsTest.java renamed to library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/interactive/form/MultilineFieldsInstrumentationTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,23 @@
2424
import java.io.File;
2525
import java.io.IOException;
2626

27+
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
2728
import com.tom_roush.pdfbox.pdmodel.PDDocument;
2829
import com.tom_roush.pdfbox.rendering.TestRendering;
29-
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
3030

3131
import org.junit.After;
3232
import org.junit.Before;
3333
import org.junit.Test;
3434

35-
public class MultilineFieldsTest
35+
public class MultilineFieldsInstrumentationTest
3636
{
3737
private static File OUT_DIR;
3838
private static final String IN_DIR = "pdfbox/com/tom_roush/pdfbox/pdmodel/interactive/form";
3939
private static final String NAME_OF_PDF = "MultilineFields.pdf";
40-
private static final String TEST_VALUE =
41-
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " +
42-
"sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam";
40+
private static final String TEST_VALUE = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " +
41+
"sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam";
4342

44-
Context testContext;
43+
private Context testContext;
4544

4645
private PDDocument document;
4746
private PDAcroForm acroForm;
@@ -51,7 +50,6 @@ public void setUp() throws IOException
5150
{
5251
testContext = InstrumentationRegistry.getInstrumentation().getContext();
5352
PDFBoxResourceLoader.init(testContext);
54-
System.out.println("Working Directory = " + System.getProperty("user.dir"));
5553
document = PDDocument.load(testContext.getAssets().open(IN_DIR + "/" + NAME_OF_PDF));
5654
acroForm = document.getDocumentCatalog().getAcroForm();
5755
OUT_DIR = new File(testContext.getCacheDir(), "pdfbox-test-output");
@@ -110,4 +108,5 @@ public void tearDown() throws IOException
110108
{
111109
document.close();
112110
}
111+
113112
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ public void testFlattenPDFBox4788() throws IOException
284284
flattenAndCompare(sourceUrl, targetFileName);
285285
}
286286

287+
/**
288+
* PDFBOX-4889: appearance streams with empty /BBox.
289+
*
290+
* @throws IOException
291+
*/
292+
@Test
293+
public void testFlattenPDFBox4889() throws IOException
294+
{
295+
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/13005793/f1040sb%20test.pdf";
296+
String targetFileName = "PDFBOX-4889.pdf";
297+
298+
flattenAndCompare(sourceUrl, targetFileName);
299+
}
300+
287301
/*
288302
* Flatten and compare with generated image samples.
289303
*/

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.tom_roush.fontbox.cmap;
1818

19+
import java.io.BufferedInputStream;
1920
import java.io.File;
2021
import java.io.FileInputStream;
2122
import java.io.IOException;
@@ -122,27 +123,27 @@ public CMap parse(InputStream input) throws IOException
122123

123124
if (previousToken != null)
124125
{
125-
if (op.op.equals("usecmap"))
126+
if (op.op.equals("usecmap") && previousToken instanceof LiteralName)
126127
{
127128
parseUsecmap((LiteralName) previousToken, result);
128129
}
129-
else if (op.op.equals("begincodespacerange"))
130+
else if (op.op.equals("begincodespacerange") && previousToken instanceof Number)
130131
{
131132
parseBegincodespacerange((Number) previousToken, cmapStream, result);
132133
}
133-
else if (op.op.equals("beginbfchar"))
134+
else if (op.op.equals("beginbfchar") && previousToken instanceof Number)
134135
{
135136
parseBeginbfchar((Number) previousToken, cmapStream, result);
136137
}
137-
else if (op.op.equals("beginbfrange"))
138+
else if (op.op.equals("beginbfrange") && previousToken instanceof Number)
138139
{
139140
parseBeginbfrange((Number) previousToken, cmapStream, result);
140141
}
141-
else if (op.op.equals("begincidchar"))
142+
else if (op.op.equals("begincidchar") && previousToken instanceof Number)
142143
{
143144
parseBegincidchar((Number) previousToken, cmapStream, result);
144145
}
145-
else if (op.op.equals("begincidrange"))
146+
else if (op.op.equals("begincidrange") && previousToken instanceof Integer)
146147
{
147148
parseBegincidrange((Integer) previousToken, cmapStream, result);
148149
}
@@ -452,12 +453,7 @@ protected InputStream getExternalCMap(String name) throws IOException
452453
return PDFBoxResourceLoader.getStream("com/tom_roush/fontbox/resources/cmap/" + name);
453454
}
454455

455-
InputStream is = getClass().getResourceAsStream("/com/tom_roush/fontbox/resources/cmap/" + name);
456-
if (is == null)
457-
{
458-
throw new IOException("Error: Could not find referenced cmap stream " + name);
459-
}
460-
return is;
456+
return new BufferedInputStream(getClass().getResourceAsStream("/com/tom_roush/fontbox/resources/cmap/" + name));
461457
}
462458

463459
private Object parseNextToken(PushbackInputStream is) throws IOException

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,26 @@ public class CodespaceRange
3737
* &lt;8140&gt; to &lt;9FFC&gt; defines a rectangular range. The high byte has to be within 0x81 and 0x9F and the
3838
* low byte has to be within 0x40 and 0xFC
3939
*
40+
* @param startBytes
41+
* @param endBytes
4042
*/
4143
public CodespaceRange(byte[] startBytes, byte[] endBytes)
4244
{
43-
if (startBytes.length != endBytes.length)
45+
byte[] correctedStartBytes = startBytes;
46+
if (startBytes.length != endBytes.length && startBytes.length == 1 && startBytes[0] == 0)
47+
{
48+
correctedStartBytes = new byte[endBytes.length];
49+
}
50+
else if (startBytes.length != endBytes.length)
4451
{
4552
throw new IllegalArgumentException(
4653
"The start and the end values must not have different lengths.");
4754
}
48-
start = new int[startBytes.length];
55+
start = new int[correctedStartBytes.length];
4956
end = new int[endBytes.length];
50-
for (int i = 0; i < startBytes.length; i++)
57+
for (int i = 0; i < correctedStartBytes.length; i++)
5158
{
52-
start[i] = startBytes[i] & 0xFF;
59+
start[i] = correctedStartBytes[i] & 0xFF;
5360
end[i] = endBytes[i] & 0xFF;
5461
}
5562
codeLength = endBytes.length;

library/src/main/java/com/tom_roush/fontbox/pfb/PfbParser.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,21 @@ private void parsePfb(final byte[] pfb) throws IOException
140140
size += in.read() << 8;
141141
size += in.read() << 16;
142142
size += in.read() << 24;
143+
if (size < 0)
144+
{
145+
throw new IOException("PFB record size is negative: " + size);
146+
}
143147
lengths[records] = size;
144148
if (pointer >= pfbdata.length)
145149
{
146150
throw new EOFException("attempted to read past EOF");
147151
}
152+
if (size > pfbdata.length - pointer)
153+
{
154+
throw new IOException("PFB record size (" + size +
155+
") doesn't fit in buffer, position: " + pointer +
156+
", total length: " + pfbdata.length);
157+
}
148158
int got = in.read(pfbdata, pointer, size);
149159
if (got < 0)
150160
{

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,37 @@ private void invalidate() throws IOException
148148
@Override
149149
public int read(byte[] b, int off, int len) throws IOException
150150
{
151-
int leftover = bufend - bufpos;
152-
if (len <= leftover)
153-
{
154-
System.arraycopy(buffer, bufpos, b, off, len);
155-
bufpos += len;
156-
return len;
157-
}
158-
System.arraycopy(buffer, bufpos, b, off, leftover);
159-
bufpos += leftover;
160-
if (fillBuffer() > 0)
151+
int curLen = len; // length of what is left to read (shrinks)
152+
int curOff = off; // offset where to put read data (grows)
153+
int totalRead = 0;
154+
155+
while (true)
161156
{
162-
int bytesRead = read(b, off + leftover, len - leftover);
163-
if (bytesRead > 0)
157+
int leftover = bufend - bufpos;
158+
if (curLen <= leftover)
159+
{
160+
System.arraycopy(buffer, bufpos, b, curOff, curLen);
161+
bufpos += curLen;
162+
return totalRead + curLen;
163+
}
164+
// curLen > leftover, we need to read more than what remains in buffer
165+
System.arraycopy(buffer, bufpos, b, curOff, leftover);
166+
totalRead += leftover;
167+
bufpos += leftover;
168+
if (fillBuffer() > 0)
169+
{
170+
curOff += leftover;
171+
curLen -= leftover;
172+
}
173+
else
164174
{
165-
leftover += bytesRead;
175+
if (totalRead == 0)
176+
{
177+
return -1;
178+
}
179+
return totalRead;
166180
}
167181
}
168-
return leftover > 0 ? leftover : -1;
169182
}
170183

171184
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private Collection<LangSysTable> getLangSysTables(String scriptTag)
402402
*
403403
* @param langSysTables The {@code LangSysTable}s indicating {@code FeatureRecord}s to search
404404
* for
405-
* @param enabledFeatures An optional whitelist of feature tags ({@code null} to allow all)
405+
* @param enabledFeatures An optional list of feature tags ({@code null} to allow all)
406406
* @return The indicated {@code FeatureRecord}s
407407
*/
408408
private List<FeatureRecord> getFeatureRecords(Collection<LangSysTable> langSysTables,
@@ -510,16 +510,16 @@ private int doLookup(LookupTable lookupTable, int gid)
510510

511511
/**
512512
* Apply glyph substitutions to the supplied gid. The applicable substitutions are determined by
513-
* the {@code scriptTags} which indicate the language of the gid, and by the
514-
* {@code enabledFeatures} which acts as a whitelist.
513+
* the {@code scriptTags} which indicate the language of the gid, and by the list of
514+
* {@code enabledFeatures}.
515515
*
516516
* To ensure that a single gid isn't mapped to multiple substitutions, subsequent invocations
517517
* with the same gid will return the same result as the first, regardless of script or enabled
518518
* features.
519519
*
520520
* @param gid GID
521521
* @param scriptTags Script tags applicable to the gid (see {@link OpenTypeScript})
522-
* @param enabledFeatures Whitelist of features to apply
522+
* @param enabledFeatures list of features to apply
523523
*/
524524
public int getSubstitution(int gid, String[] scriptTags, List<String> enabledFeatures)
525525
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public class OS2WindowsMetricsTable extends TTFTable
152152
* <p>For Restricted License embedding to take effect, it must be the only level of embedding
153153
* selected.
154154
*/
155-
public static final short FSTYPE_RESTRICTED = 0x0001;
155+
public static final short FSTYPE_RESTRICTED = 0x0002;
156156

157157
/**
158158
* Preview and Print embedding: the font may be embedded, and temporarily loaded on the

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.util.Log;
2020

21+
import java.io.BufferedInputStream;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.io.InputStreamReader;
@@ -222,21 +223,13 @@ public final class OpenTypeScript
222223
{
223224
if (PDFBoxResourceLoader.isReady())
224225
{
225-
input = PDFBoxResourceLoader.getStream(path);
226+
input = new BufferedInputStream(PDFBoxResourceLoader.getStream(path));
226227
}
227228
else
228229
{
229-
// Fallback
230-
input = OpenTypeScript.class.getResourceAsStream(path);
231-
}
232-
if (input != null)
233-
{
234-
parseScriptsFile(input);
235-
}
236-
else
237-
{
238-
Log.w("PdfBox-Android", "Could not find '" + path + "', mirroring char map will be empty: ");
230+
input = new BufferedInputStream(OpenTypeScript.class.getResourceAsStream(path));
239231
}
232+
parseScriptsFile(input);
240233
}
241234
catch (IOException e)
242235
{

0 commit comments

Comments
 (0)