Skip to content

Commit

Permalink
FOP-3203: Upgrade to PDFBox 3
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsteiner1984 committed Oct 14, 2024
1 parent 53ce1ed commit 8c362e0
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 61 deletions.
4 changes: 2 additions & 2 deletions fop-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.27</version>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>javax.media</groupId>
Expand Down Expand Up @@ -157,7 +157,7 @@
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.fontbox.cff.CFFFont;
import org.apache.fontbox.cff.CFFParser;
import org.apache.pdfbox.io.RandomAccessReadBuffer;

import org.apache.fop.apps.io.InternalResourceResolver;
import org.apache.fop.fonts.type1.PFBData;
Expand All @@ -55,7 +56,7 @@ public List<InputStream> getInputStreams() throws IOException {
}

private List<InputStream> convertOTFToType1(InputStream in) throws IOException {
CFFFont f = new CFFParser().parse(IOUtils.toByteArray(in)).get(0);
CFFFont f = new CFFParser().parse(new RandomAccessReadBuffer(IOUtils.toByteArray(in))).get(0);
List<InputStream> fonts = new ArrayList<InputStream>();
Map<Integer, Integer> glyphs = cidSet.getGlyphs();
int i = 0;
Expand Down
62 changes: 29 additions & 33 deletions fop-core/src/main/java/org/apache/fop/fonts/cff/CFFDataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import java.util.List;
import java.util.Map;

import org.apache.fontbox.cff.CFFDataInput;
import org.apache.fontbox.cff.CFFOperator;
import org.apache.fontbox.cff.DataInputByteArray;

import org.apache.fop.fonts.truetype.FontFileReader;
import org.apache.fop.fonts.truetype.OTFFile;
Expand All @@ -35,7 +35,7 @@
* A class to read the CFF data from an OTF CFF font file.
*/
public class CFFDataReader {
private CFFDataInput cffData;
private DataInputByteArray cffData;

private byte[] header;
private CFFIndexData nameIndex;
Expand Down Expand Up @@ -137,14 +137,10 @@ public LinkedHashMap<String, DICTEntry> parseDictData(byte[] dictData) throws IO
operator[0] = dictData[i];
}
String operatorName = "";
CFFOperator tempOp = null;
if (operator.length > 1) {
tempOp = CFFOperator.getOperator(new CFFOperator.Key(operator[0], operator[1]));
operatorName = CFFOperator.getOperator(operator[0], operator[1]);
} else {
tempOp = CFFOperator.getOperator(new CFFOperator.Key(operator[0]));
}
if (tempOp != null) {
operatorName = tempOp.getName();
operatorName = CFFOperator.getOperator(operator[0]);
}
DICTEntry newEntry = new DICTEntry();
newEntry.setOperator(operator);
Expand Down Expand Up @@ -330,7 +326,7 @@ private CFFIndexData readIndex() throws IOException {
* @return Returns an object representing the index
* @throws IOException Throws an IO Exception if an error occurs
*/
public CFFIndexData readIndex(CFFDataInput input) throws IOException {
public CFFIndexData readIndex(DataInputByteArray input) throws IOException {
CFFIndexData nameIndex = new CFFIndexData();
if (input != null) {
int origPos = input.getPosition();
Expand All @@ -352,11 +348,11 @@ public int getSIDFromGID(int charsetOffset, int gid) throws IOException {
return 0;
}
cffData.setPosition(charsetOffset);
int charsetFormat = cffData.readCard8();
int charsetFormat = cffData.readUnsignedByte();
switch (charsetFormat) {
case 0: //Adjust for .notdef character
cffData.setPosition(cffData.getPosition() + (--gid * 2));
return cffData.readSID();
return cffData.readUnsignedShort();
case 1: return getSIDFromGIDFormat(gid, 1);
case 2: return getSIDFromGIDFormat(gid, 2);
default: return 0;
Expand All @@ -367,8 +363,8 @@ private int getSIDFromGIDFormat(int gid, int format) throws IOException {
int glyphCount = 0;
while (true) {
int oldGlyphCount = glyphCount;
int start = cffData.readSID();
glyphCount += ((format == 1) ? cffData.readCard8() : cffData.readCard16()) + 1;
int start = cffData.readUnsignedShort();
glyphCount += ((format == 1) ? cffData.readUnsignedByte() : cffData.readUnsignedShort()) + 1;
if (gid <= glyphCount) {
return start + (gid - oldGlyphCount) - 1;
}
Expand Down Expand Up @@ -407,7 +403,7 @@ public CFFIndexData getCharStringIndex() {
return charStringIndex;
}

public CFFDataInput getCFFData() {
public DataInputByteArray getCFFData() {
return cffData;
}

Expand All @@ -423,7 +419,7 @@ public List<FontDict> getFDFonts() {
return fdFonts;
}

public CFFDataInput getLocalSubrsForGlyph(int glyph) throws IOException {
public DataInputByteArray getLocalSubrsForGlyph(int glyph) throws IOException {
//Subsets are currently written using a Format0 FDSelect
FDSelect fontDictionary = getFDSelect();
if (fontDictionary instanceof Format0FDSelect) {
Expand All @@ -432,7 +428,7 @@ public CFFDataInput getLocalSubrsForGlyph(int glyph) throws IOException {
FontDict font = getFDFonts().get(found);
byte[] localSubrData = font.getLocalSubrData().getByteData();
if (localSubrData != null) {
return new CFFDataInput(localSubrData);
return new DataInputByteArray(localSubrData);
} else {
return null;
}
Expand All @@ -448,7 +444,7 @@ public CFFDataInput getLocalSubrsForGlyph(int glyph) throws IOException {
FontDict font = getFDFonts().get(index);
byte[] localSubrsData = font.getLocalSubrData().getByteData();
if (localSubrsData != null) {
return new CFFDataInput(localSubrsData);
return new DataInputByteArray(localSubrsData);
} else {
return null;
}
Expand All @@ -473,8 +469,8 @@ private CustomEncoding readEncoding() throws IOException {
int offset = topDict.get("Encoding").getOperands().get(0).intValue();
if (offset != 0 && offset != 1) {
//No need to set the offset as we are reading the data sequentially.
int format = cffData.readCard8();
int numEntries = cffData.readCard8();
int format = cffData.readUnsignedByte();
int numEntries = cffData.readUnsignedByte();
switch (format) {
case 0:
foundEncoding = readFormat0Encoding(format, numEntries);
Expand All @@ -496,7 +492,7 @@ private Format0Encoding readFormat0Encoding(int format, int numEntries)
newEncoding.setNumEntries(numEntries);
int[] codes = new int[numEntries];
for (int i = 0; i < numEntries; i++) {
codes[i] = cffData.readCard8();
codes[i] = cffData.readUnsignedByte();
}
newEncoding.setCodes(codes);
return newEncoding;
Expand All @@ -509,8 +505,8 @@ private Format1Encoding readFormat1Encoding(int format, int numEntries)
newEncoding.setNumEntries(numEntries);
Map<Integer, Integer> ranges = new LinkedHashMap<Integer, Integer>();
for (int i = 0; i < numEntries; i++) {
int first = cffData.readCard8();
int left = cffData.readCard8();
int first = cffData.readUnsignedByte();
int left = cffData.readUnsignedByte();
ranges.put(first, left);
}
newEncoding.setRanges(ranges);
Expand All @@ -523,7 +519,7 @@ private FDSelect readFDSelect() throws IOException {
if (fdSelectEntry != null) {
int fdOffset = fdSelectEntry.getOperands().get(0).intValue();
cffData.setPosition(fdOffset);
int format = cffData.readCard8();
int format = cffData.readUnsignedByte();
switch (format) {
case 0:
fdSelect = readFormat0FDSelect();
Expand All @@ -543,7 +539,7 @@ private Format0FDSelect readFormat0FDSelect() throws IOException {
int glyphCount = charStringIndex.getNumObjects();
int[] fds = new int[glyphCount];
for (int i = 0; i < glyphCount; i++) {
fds[i] = cffData.readCard8();
fds[i] = cffData.readUnsignedByte();
}
newFDs.setFDIndexes(fds);
return newFDs;
Expand All @@ -552,16 +548,16 @@ private Format0FDSelect readFormat0FDSelect() throws IOException {
private Format3FDSelect readFormat3FDSelect() throws IOException {
Format3FDSelect newFDs = new Format3FDSelect();
newFDs.setFormat(3);
int rangeCount = cffData.readCard16();
int rangeCount = cffData.readUnsignedShort();
newFDs.setRangeCount(rangeCount);
Map<Integer, Integer> ranges = new LinkedHashMap<Integer, Integer>();
for (int i = 0; i < rangeCount; i++) {
int first = cffData.readCard16();
int fd = cffData.readCard8();
int first = cffData.readUnsignedShort();
int fd = cffData.readUnsignedByte();
ranges.put(first, fd);
}
newFDs.setRanges(ranges);
newFDs.setSentinelGID(cffData.readCard16());
newFDs.setSentinelGID(cffData.readUnsignedShort());
return newFDs;
}

Expand Down Expand Up @@ -709,19 +705,19 @@ public byte[] getData() throws IOException {
* @param cffData A byte array containing the CFF data
* @throws IOException Throws an IO Exception if an error occurs
*/
public void parseIndexHeader(CFFDataInput cffData) throws IOException {
setNumObjects(cffData.readCard16());
setOffSize(cffData.readOffSize());
public void parseIndexHeader(DataInputByteArray cffData) throws IOException {
setNumObjects(cffData.readUnsignedShort());
setOffSize(cffData.readUnsignedByte());
int[] offsets = new int[getNumObjects() + 1];
byte[] bytes;
//Fills the offsets array
for (int i = 0; i <= getNumObjects(); i++) {
switch (getOffSize()) {
case 1:
offsets[i] = cffData.readCard8();
offsets[i] = cffData.readUnsignedByte();
break;
case 2:
offsets[i] = cffData.readCard16();
offsets[i] = cffData.readUnsignedShort();
break;
case 3:
bytes = cffData.readBytes(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,76 @@

import java.io.IOException;

import org.apache.fontbox.cff.CFFDataInput;
import org.apache.fontbox.cff.DataInputByteArray;

public class FOPCFFDataInput extends DataInputByteArray {
private final byte[] inputBuffer;
private int bufferPosition;

public class FOPCFFDataInput extends CFFDataInput {
public FOPCFFDataInput(byte[] buffer) {
super(buffer);
this.inputBuffer = buffer;
}

public boolean hasRemaining() throws IOException {
return this.bufferPosition < this.inputBuffer.length;
}

public int getPosition() {
return this.bufferPosition;
}

public void setPosition(int position) throws IOException {
if (position < 0) {
throw new IOException("position is negative");
// } else if (position >= this.inputBuffer.length) {
// throw new IOException("New position is out of range " + position + " >= " + this.inputBuffer.length);
} else {
this.bufferPosition = position;
}
}

public byte readByte() throws IOException {
if (!this.hasRemaining()) {
throw new IOException("End off buffer reached");
} else {
return this.inputBuffer[this.bufferPosition++];
}
}

public int readUnsignedByte() throws IOException {
if (!this.hasRemaining()) {
throw new IOException("End off buffer reached");
} else {
return this.inputBuffer[this.bufferPosition++] & 255;
}
}

public int peekUnsignedByte(int offset) throws IOException {
if (offset < 0) {
throw new IOException("offset is negative");
} else if (this.bufferPosition + offset >= this.inputBuffer.length) {
throw new IOException("Offset position is out of range " + (this.bufferPosition + offset)
+ " >= " + this.inputBuffer.length);
} else {
return this.inputBuffer[this.bufferPosition + offset] & 255;
}
}

public byte[] readBytes(int length) throws IOException {
if (length < 0) {
throw new IOException("length is negative");
} else if (this.inputBuffer.length - this.bufferPosition < length) {
throw new IOException("Premature end of buffer reached");
} else {
byte[] bytes = new byte[length];
System.arraycopy(this.inputBuffer, this.bufferPosition, bytes, 0, length);
this.bufferPosition += length;
return bytes;
}
}

public int readOffSize() throws IOException {
return readUnsignedByte();
public int length() throws IOException {
return this.inputBuffer.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import java.io.IOException;
import java.util.List;

import org.apache.fontbox.cff.CFFDataInput;
import org.apache.fontbox.cff.CFFFont;
import org.apache.fontbox.cff.CFFParser;
import org.apache.fontbox.cff.CFFType1Font;
import org.apache.fontbox.cff.DataInputByteArray;
import org.apache.pdfbox.io.RandomAccessReadBuffer;

public class OTFFile extends OpenFont {

Expand Down Expand Up @@ -100,7 +101,7 @@ protected void initializeFont(FontFileReader in) throws IOException {
fontFile = in;
fontFile.seekSet(0);
CFFParser parser = new CFFParser();
fileFont = parser.parse(in.getAllBytes()).get(0);
fileFont = parser.parse(new RandomAccessReadBuffer(in.getAllBytes())).get(0);
embedFontName = fileFont.getName();
}

Expand All @@ -122,7 +123,7 @@ protected void readName() throws IOException {
*/
public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
byte[] cff = fontFile.getAllBytes();
CFFDataInput input = new CFFDataInput(fontFile.getAllBytes());
DataInputByteArray input = new DataInputByteArray(fontFile.getAllBytes());
input.readBytes(4); //OTTO
short numTables = input.readShort();
input.readShort(); //searchRange
Expand All @@ -143,8 +144,8 @@ public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
return cff;
}

private static long readLong(CFFDataInput input) throws IOException {
return (input.readCard16() << 16) | input.readCard16();
private static long readLong(DataInputByteArray input) throws IOException {
return (input.readUnsignedShort() << 16) | input.readUnsignedShort();
}

public boolean isType1() {
Expand Down
Loading

0 comments on commit 8c362e0

Please sign in to comment.