Skip to content

Commit 8c362e0

Browse files
FOP-3203: Upgrade to PDFBox 3
1 parent 53ce1ed commit 8c362e0

File tree

15 files changed

+267
-61
lines changed

15 files changed

+267
-61
lines changed

fop-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
<dependency>
104104
<groupId>org.apache.pdfbox</groupId>
105105
<artifactId>fontbox</artifactId>
106-
<version>2.0.27</version>
106+
<version>3.0.3</version>
107107
</dependency>
108108
<dependency>
109109
<groupId>javax.media</groupId>
@@ -157,7 +157,7 @@
157157
<dependency>
158158
<groupId>org.apache.pdfbox</groupId>
159159
<artifactId>pdfbox</artifactId>
160-
<version>2.0.27</version>
160+
<version>3.0.3</version>
161161
<scope>test</scope>
162162
</dependency>
163163
</dependencies>

fop-core/src/main/java/org/apache/fop/fonts/CFFToType1Font.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.commons.io.IOUtils;
3131
import org.apache.fontbox.cff.CFFFont;
3232
import org.apache.fontbox.cff.CFFParser;
33+
import org.apache.pdfbox.io.RandomAccessReadBuffer;
3334

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

5758
private List<InputStream> convertOTFToType1(InputStream in) throws IOException {
58-
CFFFont f = new CFFParser().parse(IOUtils.toByteArray(in)).get(0);
59+
CFFFont f = new CFFParser().parse(new RandomAccessReadBuffer(IOUtils.toByteArray(in))).get(0);
5960
List<InputStream> fonts = new ArrayList<InputStream>();
6061
Map<Integer, Integer> glyphs = cidSet.getGlyphs();
6162
int i = 0;

fop-core/src/main/java/org/apache/fop/fonts/cff/CFFDataReader.java

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28-
import org.apache.fontbox.cff.CFFDataInput;
2928
import org.apache.fontbox.cff.CFFOperator;
29+
import org.apache.fontbox.cff.DataInputByteArray;
3030

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

4040
private byte[] header;
4141
private CFFIndexData nameIndex;
@@ -137,14 +137,10 @@ public LinkedHashMap<String, DICTEntry> parseDictData(byte[] dictData) throws IO
137137
operator[0] = dictData[i];
138138
}
139139
String operatorName = "";
140-
CFFOperator tempOp = null;
141140
if (operator.length > 1) {
142-
tempOp = CFFOperator.getOperator(new CFFOperator.Key(operator[0], operator[1]));
141+
operatorName = CFFOperator.getOperator(operator[0], operator[1]);
143142
} else {
144-
tempOp = CFFOperator.getOperator(new CFFOperator.Key(operator[0]));
145-
}
146-
if (tempOp != null) {
147-
operatorName = tempOp.getName();
143+
operatorName = CFFOperator.getOperator(operator[0]);
148144
}
149145
DICTEntry newEntry = new DICTEntry();
150146
newEntry.setOperator(operator);
@@ -330,7 +326,7 @@ private CFFIndexData readIndex() throws IOException {
330326
* @return Returns an object representing the index
331327
* @throws IOException Throws an IO Exception if an error occurs
332328
*/
333-
public CFFIndexData readIndex(CFFDataInput input) throws IOException {
329+
public CFFIndexData readIndex(DataInputByteArray input) throws IOException {
334330
CFFIndexData nameIndex = new CFFIndexData();
335331
if (input != null) {
336332
int origPos = input.getPosition();
@@ -352,11 +348,11 @@ public int getSIDFromGID(int charsetOffset, int gid) throws IOException {
352348
return 0;
353349
}
354350
cffData.setPosition(charsetOffset);
355-
int charsetFormat = cffData.readCard8();
351+
int charsetFormat = cffData.readUnsignedByte();
356352
switch (charsetFormat) {
357353
case 0: //Adjust for .notdef character
358354
cffData.setPosition(cffData.getPosition() + (--gid * 2));
359-
return cffData.readSID();
355+
return cffData.readUnsignedShort();
360356
case 1: return getSIDFromGIDFormat(gid, 1);
361357
case 2: return getSIDFromGIDFormat(gid, 2);
362358
default: return 0;
@@ -367,8 +363,8 @@ private int getSIDFromGIDFormat(int gid, int format) throws IOException {
367363
int glyphCount = 0;
368364
while (true) {
369365
int oldGlyphCount = glyphCount;
370-
int start = cffData.readSID();
371-
glyphCount += ((format == 1) ? cffData.readCard8() : cffData.readCard16()) + 1;
366+
int start = cffData.readUnsignedShort();
367+
glyphCount += ((format == 1) ? cffData.readUnsignedByte() : cffData.readUnsignedShort()) + 1;
372368
if (gid <= glyphCount) {
373369
return start + (gid - oldGlyphCount) - 1;
374370
}
@@ -407,7 +403,7 @@ public CFFIndexData getCharStringIndex() {
407403
return charStringIndex;
408404
}
409405

410-
public CFFDataInput getCFFData() {
406+
public DataInputByteArray getCFFData() {
411407
return cffData;
412408
}
413409

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

426-
public CFFDataInput getLocalSubrsForGlyph(int glyph) throws IOException {
422+
public DataInputByteArray getLocalSubrsForGlyph(int glyph) throws IOException {
427423
//Subsets are currently written using a Format0 FDSelect
428424
FDSelect fontDictionary = getFDSelect();
429425
if (fontDictionary instanceof Format0FDSelect) {
@@ -432,7 +428,7 @@ public CFFDataInput getLocalSubrsForGlyph(int glyph) throws IOException {
432428
FontDict font = getFDFonts().get(found);
433429
byte[] localSubrData = font.getLocalSubrData().getByteData();
434430
if (localSubrData != null) {
435-
return new CFFDataInput(localSubrData);
431+
return new DataInputByteArray(localSubrData);
436432
} else {
437433
return null;
438434
}
@@ -448,7 +444,7 @@ public CFFDataInput getLocalSubrsForGlyph(int glyph) throws IOException {
448444
FontDict font = getFDFonts().get(index);
449445
byte[] localSubrsData = font.getLocalSubrData().getByteData();
450446
if (localSubrsData != null) {
451-
return new CFFDataInput(localSubrsData);
447+
return new DataInputByteArray(localSubrsData);
452448
} else {
453449
return null;
454450
}
@@ -473,8 +469,8 @@ private CustomEncoding readEncoding() throws IOException {
473469
int offset = topDict.get("Encoding").getOperands().get(0).intValue();
474470
if (offset != 0 && offset != 1) {
475471
//No need to set the offset as we are reading the data sequentially.
476-
int format = cffData.readCard8();
477-
int numEntries = cffData.readCard8();
472+
int format = cffData.readUnsignedByte();
473+
int numEntries = cffData.readUnsignedByte();
478474
switch (format) {
479475
case 0:
480476
foundEncoding = readFormat0Encoding(format, numEntries);
@@ -496,7 +492,7 @@ private Format0Encoding readFormat0Encoding(int format, int numEntries)
496492
newEncoding.setNumEntries(numEntries);
497493
int[] codes = new int[numEntries];
498494
for (int i = 0; i < numEntries; i++) {
499-
codes[i] = cffData.readCard8();
495+
codes[i] = cffData.readUnsignedByte();
500496
}
501497
newEncoding.setCodes(codes);
502498
return newEncoding;
@@ -509,8 +505,8 @@ private Format1Encoding readFormat1Encoding(int format, int numEntries)
509505
newEncoding.setNumEntries(numEntries);
510506
Map<Integer, Integer> ranges = new LinkedHashMap<Integer, Integer>();
511507
for (int i = 0; i < numEntries; i++) {
512-
int first = cffData.readCard8();
513-
int left = cffData.readCard8();
508+
int first = cffData.readUnsignedByte();
509+
int left = cffData.readUnsignedByte();
514510
ranges.put(first, left);
515511
}
516512
newEncoding.setRanges(ranges);
@@ -523,7 +519,7 @@ private FDSelect readFDSelect() throws IOException {
523519
if (fdSelectEntry != null) {
524520
int fdOffset = fdSelectEntry.getOperands().get(0).intValue();
525521
cffData.setPosition(fdOffset);
526-
int format = cffData.readCard8();
522+
int format = cffData.readUnsignedByte();
527523
switch (format) {
528524
case 0:
529525
fdSelect = readFormat0FDSelect();
@@ -543,7 +539,7 @@ private Format0FDSelect readFormat0FDSelect() throws IOException {
543539
int glyphCount = charStringIndex.getNumObjects();
544540
int[] fds = new int[glyphCount];
545541
for (int i = 0; i < glyphCount; i++) {
546-
fds[i] = cffData.readCard8();
542+
fds[i] = cffData.readUnsignedByte();
547543
}
548544
newFDs.setFDIndexes(fds);
549545
return newFDs;
@@ -552,16 +548,16 @@ private Format0FDSelect readFormat0FDSelect() throws IOException {
552548
private Format3FDSelect readFormat3FDSelect() throws IOException {
553549
Format3FDSelect newFDs = new Format3FDSelect();
554550
newFDs.setFormat(3);
555-
int rangeCount = cffData.readCard16();
551+
int rangeCount = cffData.readUnsignedShort();
556552
newFDs.setRangeCount(rangeCount);
557553
Map<Integer, Integer> ranges = new LinkedHashMap<Integer, Integer>();
558554
for (int i = 0; i < rangeCount; i++) {
559-
int first = cffData.readCard16();
560-
int fd = cffData.readCard8();
555+
int first = cffData.readUnsignedShort();
556+
int fd = cffData.readUnsignedByte();
561557
ranges.put(first, fd);
562558
}
563559
newFDs.setRanges(ranges);
564-
newFDs.setSentinelGID(cffData.readCard16());
560+
newFDs.setSentinelGID(cffData.readUnsignedShort());
565561
return newFDs;
566562
}
567563

@@ -709,19 +705,19 @@ public byte[] getData() throws IOException {
709705
* @param cffData A byte array containing the CFF data
710706
* @throws IOException Throws an IO Exception if an error occurs
711707
*/
712-
public void parseIndexHeader(CFFDataInput cffData) throws IOException {
713-
setNumObjects(cffData.readCard16());
714-
setOffSize(cffData.readOffSize());
708+
public void parseIndexHeader(DataInputByteArray cffData) throws IOException {
709+
setNumObjects(cffData.readUnsignedShort());
710+
setOffSize(cffData.readUnsignedByte());
715711
int[] offsets = new int[getNumObjects() + 1];
716712
byte[] bytes;
717713
//Fills the offsets array
718714
for (int i = 0; i <= getNumObjects(); i++) {
719715
switch (getOffSize()) {
720716
case 1:
721-
offsets[i] = cffData.readCard8();
717+
offsets[i] = cffData.readUnsignedByte();
722718
break;
723719
case 2:
724-
offsets[i] = cffData.readCard16();
720+
offsets[i] = cffData.readUnsignedShort();
725721
break;
726722
case 3:
727723
bytes = cffData.readBytes(3);

fop-core/src/main/java/org/apache/fop/fonts/cff/FOPCFFDataInput.java

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,76 @@
2020

2121
import java.io.IOException;
2222

23-
import org.apache.fontbox.cff.CFFDataInput;
23+
import org.apache.fontbox.cff.DataInputByteArray;
24+
25+
public class FOPCFFDataInput extends DataInputByteArray {
26+
private final byte[] inputBuffer;
27+
private int bufferPosition;
2428

25-
public class FOPCFFDataInput extends CFFDataInput {
2629
public FOPCFFDataInput(byte[] buffer) {
2730
super(buffer);
31+
this.inputBuffer = buffer;
32+
}
33+
34+
public boolean hasRemaining() throws IOException {
35+
return this.bufferPosition < this.inputBuffer.length;
36+
}
37+
38+
public int getPosition() {
39+
return this.bufferPosition;
40+
}
41+
42+
public void setPosition(int position) throws IOException {
43+
if (position < 0) {
44+
throw new IOException("position is negative");
45+
// } else if (position >= this.inputBuffer.length) {
46+
// throw new IOException("New position is out of range " + position + " >= " + this.inputBuffer.length);
47+
} else {
48+
this.bufferPosition = position;
49+
}
50+
}
51+
52+
public byte readByte() throws IOException {
53+
if (!this.hasRemaining()) {
54+
throw new IOException("End off buffer reached");
55+
} else {
56+
return this.inputBuffer[this.bufferPosition++];
57+
}
58+
}
59+
60+
public int readUnsignedByte() throws IOException {
61+
if (!this.hasRemaining()) {
62+
throw new IOException("End off buffer reached");
63+
} else {
64+
return this.inputBuffer[this.bufferPosition++] & 255;
65+
}
66+
}
67+
68+
public int peekUnsignedByte(int offset) throws IOException {
69+
if (offset < 0) {
70+
throw new IOException("offset is negative");
71+
} else if (this.bufferPosition + offset >= this.inputBuffer.length) {
72+
throw new IOException("Offset position is out of range " + (this.bufferPosition + offset)
73+
+ " >= " + this.inputBuffer.length);
74+
} else {
75+
return this.inputBuffer[this.bufferPosition + offset] & 255;
76+
}
77+
}
78+
79+
public byte[] readBytes(int length) throws IOException {
80+
if (length < 0) {
81+
throw new IOException("length is negative");
82+
} else if (this.inputBuffer.length - this.bufferPosition < length) {
83+
throw new IOException("Premature end of buffer reached");
84+
} else {
85+
byte[] bytes = new byte[length];
86+
System.arraycopy(this.inputBuffer, this.bufferPosition, bytes, 0, length);
87+
this.bufferPosition += length;
88+
return bytes;
89+
}
2890
}
2991

30-
public int readOffSize() throws IOException {
31-
return readUnsignedByte();
92+
public int length() throws IOException {
93+
return this.inputBuffer.length;
3294
}
3395
}

fop-core/src/main/java/org/apache/fop/fonts/truetype/OTFFile.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import java.io.IOException;
2323
import java.util.List;
2424

25-
import org.apache.fontbox.cff.CFFDataInput;
2625
import org.apache.fontbox.cff.CFFFont;
2726
import org.apache.fontbox.cff.CFFParser;
2827
import org.apache.fontbox.cff.CFFType1Font;
28+
import org.apache.fontbox.cff.DataInputByteArray;
29+
import org.apache.pdfbox.io.RandomAccessReadBuffer;
2930

3031
public class OTFFile extends OpenFont {
3132

@@ -100,7 +101,7 @@ protected void initializeFont(FontFileReader in) throws IOException {
100101
fontFile = in;
101102
fontFile.seekSet(0);
102103
CFFParser parser = new CFFParser();
103-
fileFont = parser.parse(in.getAllBytes()).get(0);
104+
fileFont = parser.parse(new RandomAccessReadBuffer(in.getAllBytes())).get(0);
104105
embedFontName = fileFont.getName();
105106
}
106107

@@ -122,7 +123,7 @@ protected void readName() throws IOException {
122123
*/
123124
public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
124125
byte[] cff = fontFile.getAllBytes();
125-
CFFDataInput input = new CFFDataInput(fontFile.getAllBytes());
126+
DataInputByteArray input = new DataInputByteArray(fontFile.getAllBytes());
126127
input.readBytes(4); //OTTO
127128
short numTables = input.readShort();
128129
input.readShort(); //searchRange
@@ -143,8 +144,8 @@ public static byte[] getCFFData(FontFileReader fontFile) throws IOException {
143144
return cff;
144145
}
145146

146-
private static long readLong(CFFDataInput input) throws IOException {
147-
return (input.readCard16() << 16) | input.readCard16();
147+
private static long readLong(DataInputByteArray input) throws IOException {
148+
return (input.readUnsignedShort() << 16) | input.readUnsignedShort();
148149
}
149150

150151
public boolean isType1() {

0 commit comments

Comments
 (0)