Skip to content

Commit

Permalink
Upgrade PDFBox API to 2.0.24 (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomRoush authored Jun 27, 2022
1 parent d44e1e7 commit c093bc0
Show file tree
Hide file tree
Showing 111 changed files with 3,185 additions and 809 deletions.
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ dependencies {

// Test dependencies
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:4.6.1'
testImplementation 'com.googlecode.java-diff-utils:diffutils:1.3.0'

androidTestImplementation 'androidx.test:runner:1.4.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ private void checkStructTreeRootCount(File file) throws IOException
}
}
br.close();
assertEquals(1, count);
assertEquals(file.getPath(), 1, count);
}

/**
Expand Down Expand Up @@ -746,6 +746,65 @@ public void testFileDeletion() throws IOException
assertTrue(outFile.delete());
}


/**
* Check that there is a top level Document and Parts below in a merge of 2 documents.
*
* @param file
* @throws IOException
*/
public void testPDFBox5198_2() throws IOException
{
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + "PDFA3A.pdf"));
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + "PDFA3A.pdf"));
pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + "PDFA3A-merged2.pdf");
pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

checkParts(new File(TARGETTESTDIR + "PDFA3A-merged2.pdf"));
}

/**
* Check that there is a top level Document and Parts below in a merge of 3 documents.
*
* @param file
* @throws IOException
*/
public void testPDFBox5198_3() throws IOException
{
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + "PDFA3A.pdf"));
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + "PDFA3A.pdf"));
pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + "PDFA3A.pdf"));
pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + "PDFA3A-merged3.pdf");
pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

checkParts(new File(TARGETTESTDIR + "PDFA3A-merged3.pdf"));
}

/**
* Check that there is a top level Document and Parts below.
* @param file
* @throws IOException
*/
private void checkParts(File file) throws IOException
{
PDDocument doc = PDDocument.load(file);
PDStructureTreeRoot structureTreeRoot = doc.getDocumentCatalog().getStructureTreeRoot();
COSDictionary topDict = (COSDictionary) structureTreeRoot.getK();
assertEquals(COSName.DOCUMENT, topDict.getItem(COSName.S));
assertEquals(structureTreeRoot.getCOSObject(), topDict.getCOSDictionary(COSName.P));
COSArray kArray = topDict.getCOSArray(COSName.K);
assertEquals(doc.getNumberOfPages(), kArray.size());
for (int i = 0; i < kArray.size(); ++i)
{
COSDictionary dict = (COSDictionary) kArray.getObject(i);
assertEquals(COSName.PART, dict.getItem(COSName.S));
assertEquals(topDict, dict.getCOSDictionary(COSName.P));
}
doc.close();
}

private void checkForPageOrphans(PDDocument doc) throws IOException
{
// check for orphan pages in the StructTreeRoot/K, StructTreeRoot/ParentTree and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@

import androidx.test.platform.app.InstrumentationRegistry;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
import com.tom_roush.pdfbox.android.TestResourceGenerator;
import com.tom_roush.pdfbox.cos.COSName;
import com.tom_roush.pdfbox.io.IOUtils;
import com.tom_roush.pdfbox.pdmodel.PDDocument;
import com.tom_roush.pdfbox.pdmodel.graphics.color.PDDeviceGray;
import com.tom_roush.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -45,6 +48,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

/**
* Unit tests for JPEGFactory
Expand Down Expand Up @@ -295,6 +299,34 @@ public void testCreateFromImageUSHORT_555_RGB() throws IOException
doWritePDF(document, ximage, testResultsDir, "jpeg-ushort555rgb.pdf");
}

/**
* PDFBOX-5137 and PDFBOX-5196: check that numFrameComponents and not numScanComponents is used
* to determine the color space.
*
* @throws IOException
*/
@Test
public void testPDFBox5137() throws IOException
{
File cacheDir = new File(testContext.getCacheDir(), "imgs");
cacheDir.mkdirs();
File imgFile = TestResourceGenerator.downloadTestResource(cacheDir, "PDFBOX-5196-lotus.jpg", "https://issues.apache.org/jira/secure/attachment/13025718/lotus.jpg");
assumeTrue(imgFile.exists());

InputStream is = new FileInputStream(imgFile);
byte[] ba = IOUtils.toByteArray(is);
is.close();

PDDocument document = new PDDocument();

PDImageXObject ximage = JPEGFactory.createFromByteArray(document, ba);

validate(ximage, 8, 500, 500, "jpg", PDDeviceRGB.INSTANCE.getName());

doWritePDF(document, ximage, testResultsDir, "PDFBOX-5196-lotus.pdf");
checkJpegStream(testResultsDir, "PDFBOX-5196-lotus.pdf", new ByteArrayInputStream(ba));
}

// check whether it is possible to extract the jpeg stream exactly
// as it was passed to createFromStream
private void checkJpegStream(File testResultsDir, String filename, InputStream resourceStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public void testFlattenPDFBOX2469Empty() throws IOException
/*
* PDFBOX-2469 Filled template.
*/
@Test
// Disabled as there is a minimal difference which can not be seen visually, see PDFBOX-5133
// @Test
public void testFlattenPDFBOX2469Filled() throws IOException
{
String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12678455/testPDF_acroForm.pdf";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ private void verifySemicolon( StringTokenizer tokenizer ) throws IOException
private boolean readBoolean() throws IOException
{
String theBoolean = readString();
return Boolean.valueOf( theBoolean );
return Boolean.parseBoolean(theBoolean );
}

/**
Expand Down
27 changes: 1 addition & 26 deletions library/src/main/java/com/tom_roush/fontbox/cff/CFFEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
*/
package com.tom_roush.fontbox.cff;

import java.util.HashMap;
import java.util.Map;

import com.tom_roush.fontbox.encoding.Encoding;

/**
Expand All @@ -29,40 +26,20 @@
*/
public abstract class CFFEncoding extends Encoding
{
private final Map<Integer, String> codeToName = new HashMap<Integer, String>(250);

/**
* Package-private constructor for subclasses.
*/
CFFEncoding()
{
}

/**
* Returns the name of the glyph for the given character code.
*
* @param code character code
* @return PostScript glyph name
*/
@Override
public String getName(int code)
{
String name = codeToName.get(code);
if (name == null)
{
return ".notdef";
}
return name;
}

/**
* Adds a new code/SID combination to the encoding.
* @param code the given code
* @param sid the given SID
*/
public void add(int code, int sid, String name)
{
codeToName.put(code, name);
addCharacterEncoding(code, name);
}

Expand All @@ -71,8 +48,6 @@ public void add(int code, int sid, String name)
*/
protected void add(int code, int sid)
{
String name = CFFStandardString.getName(sid);
codeToName.put(code, name);
addCharacterEncoding(code, name);
addCharacterEncoding(code, CFFStandardString.getName(sid));
}
}
8 changes: 4 additions & 4 deletions library/src/main/java/com/tom_roush/fontbox/cff/CFFFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void setCharset(CFFCharset charset)
/**
* Returns the character strings dictionary. For expert users only.
*
* @return the dictionary
* @return the character strings dictionary as a list of byte arrays.
*/
public final List<byte[]> getCharStringBytes()
{
Expand Down Expand Up @@ -158,17 +158,17 @@ public int getNumCharStrings()
/**
* Sets the global subroutine index data.
*
* @param globalSubrIndexValue an list containing the global subroutines
* @param globalSubrIndexValue a list of the global subroutines.
*/
void setGlobalSubrIndex(byte[][] globalSubrIndexValue)
{
globalSubrIndex = globalSubrIndexValue;
}

/**
* Returns the list containing the global subroutine .
* Returns the list containing the global subroutines.
*
* @return the dictionary
* @return a list of the global subroutines.
*/
public List<byte[]> getGlobalSubrIndex()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,9 @@ private Format1Encoding readFormat1Encoding(CFFDataInput dataInput, CFFCharset c
int gid = 1;
for (int i = 0; i < encoding.nRanges; i++)
{
int rangeFirst = dataInput.readCard8();
int rangeLeft = dataInput.readCard8();
for (int j = 0; j < 1 + rangeLeft; j++)
int rangeFirst = dataInput.readCard8(); // First code in range
int rangeLeft = dataInput.readCard8(); // Codes left in range (excluding first)
for (int j = 0; j <= rangeLeft; j++)
{
int sid = charset.getSIDForGID(gid);
int code = rangeFirst + j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,7 @@ private Object getProperty(String name)
{
return topDictValue;
}
Object privateDictValue = privateDict.get(name);
if (privateDictValue != null)
{
return privateDictValue;
}
return null;
return privateDict.get(name);
}

private int getDefaultWidthX()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ else if ("rrcurveto".equals(name))
}
else if ("closepath".equals(name))
{
closepath();
closeCharString1Path();
}
else if ("sbw".equals(name))
{
Expand Down Expand Up @@ -450,7 +450,7 @@ private void rrcurveTo(Number dx1, Number dy1, Number dx2, Number dy2,
/**
* Close path.
*/
private void closepath()
private void closeCharString1Path()
{
if (path.isEmpty())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ private static byte[] encrypt(byte[] plaintextBytes, int r, int n)
{
byte[] buffer = new byte[plaintextBytes.length + n];

for (int i = 0; i < n; i++)
{
buffer[i] = 0;
}

System.arraycopy(plaintextBytes, 0, buffer, n, buffer.length - n);

int c1 = 52845;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ else if ("rrcurveto".equals(name))
else if ("endchar".equals(name))
{
numbers = clearStack(numbers, numbers.size() == 5 || numbers.size() == 1);
closePath();
closeCharString2Path();
if (numbers.size() == 4)
{
// deprecated "seac" operator
Expand Down Expand Up @@ -288,12 +288,12 @@ private void markPath()
{
if (pathCount > 0)
{
closePath();
closeCharString2Path();
}
pathCount++;
}

private void closePath()
private void closeCharString2Path()
{
CharStringCommand command = pathCount > 0 ? (CharStringCommand) type1Sequence
.get(type1Sequence.size() - 1)
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/tom_roush/fontbox/cmap/CMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class CMap
private final Map<Integer,String> charToUnicode = new HashMap<Integer,String>();

// inverted map
Map <String, byte[]> unicodeToByteCodes = new HashMap<String, byte[]>();
private final Map <String, byte[]> unicodeToByteCodes = new HashMap<String, byte[]>();

// CID mappings
private final Map<Integer,Integer> codeToCid = new HashMap<Integer,Integer>();
Expand Down
Loading

0 comments on commit c093bc0

Please sign in to comment.