Skip to content

Commit

Permalink
GH-79 misc. log combing fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Mar 9, 2023
1 parent e19723a commit 09fe6a2
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public AppearanceState(Library library, DictionaryEntries entries, Object stream
try {
Form form = (Form) streamOrDictionary;
form.init();
originalContentStream = new String(((Form) streamOrDictionary).getDecodedStreamBytes());
byte[] streamBytes = ((Form) streamOrDictionary).getDecodedStreamBytes();
originalContentStream = streamBytes != null ?
new String(((Form) streamOrDictionary).getDecodedStreamBytes()) : "";
resources = form.getResources();
shapes = form.getShapes();
matrix = form.getMatrix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ public boolean isEmbeddedFontDamaged() {
public Rectangle2D getFontBBox() {
Object value = library.getObject(entries, FONT_BBOX);
if (value instanceof List) {
List rectangle = (List) value;
return new PRectangle(rectangle).getOriginalPoints();
List<Float> coordinates = library.getFloatList((List<Object>)value);
return new PRectangle(coordinates).getOriginalPoints();
} else if (value instanceof int[]) {
int[] ints = (int[]) value;
List<Integer> intList = new ArrayList<>(ints.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.fontbox.util.BoundingBox;
import org.icepdf.core.pobjects.DictionaryEntries;
import org.icepdf.core.pobjects.Name;
import org.icepdf.core.pobjects.Reference;
import org.icepdf.core.pobjects.StringObject;
import org.icepdf.core.pobjects.fonts.FontManager;
import org.icepdf.core.pobjects.fonts.zfont.fontFiles.ZFontTrueType;
Expand Down Expand Up @@ -141,7 +142,16 @@ protected void parseWidths() {
i++;
} else if (currentNext instanceof Number) {
int currentEnd = ((Number) currentNext).intValue();
float width2 = (float) (((Number) individualWidths.get(i + 2)).intValue());
Object tmp = individualWidths.get(i + 2);
float width2;
if (tmp instanceof Number) {
width2 = (float) (((Number) tmp).intValue());
} else if (tmp instanceof Reference) {
tmp = library.getObject(tmp);
width2 = (float) (((Number) tmp).intValue());
} else {
width2 = 1.0f;
}
for (; current <= currentEnd; current++) {
widths[current] = width2 * 0.001f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public TypeCidType2Font(Library library, DictionaryEntries entries) {
@Override
public synchronized void init() {
super.init();
if (!(font instanceof ZFontType2)) {
if (!(font instanceof ZFontType2) && font instanceof ZFontTrueType) {
font = new ZFontType2((ZFontTrueType) font);
}
parseCidToGidMap();
Expand Down
42 changes: 30 additions & 12 deletions core/core-awt/src/main/java/org/icepdf/core/util/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,23 +683,41 @@ public List getArray(DictionaryEntries dictionaryEntries, Name key) {
* @return rectangle in Java2D coordinate system.
*/
public Rectangle2D.Float getRectangle(DictionaryEntries dictionaryEntries, Name key) {
List v = (List) getObject(dictionaryEntries, key);
List<Object> v = (List) getObject(dictionaryEntries, key);
if (v != null) {
// s by default contains data in the Cartesian plain.
if (v.get(0) instanceof Number) {
return new PRectangle(v).toJava2dCoordinates();
} // crazy corner case that contains each number as reference.
else if (v.get(0) instanceof Reference) {
v.set(0, getObject(v.get(0)));
v.set(1, getObject(v.get(1)));
v.set(2, getObject(v.get(2)));
v.set(3, getObject(v.get(3)));
return new PRectangle(v).toJava2dCoordinates();
}
return new PRectangle(getFloatList(v)).toJava2dCoordinates();
}
return null;
}

/**
* Checks the given values for floats and resolves and References.
* @param values list of floats
* @return list of floats
*/
public List<Float> getFloatList(List<Object> values) {
if (values != null) {
float x1 = getFloatNumber(values.get(0));
float y1 = getFloatNumber(values.get(1));

float x2 = getFloatNumber(values.get(2));
float y2 = getFloatNumber(values.get(3));

return List.of(x1, y1, x2, y2);
}
return null;
}

private Float getFloatNumber(Object object) {
if (object instanceof Number) {
return ((Number) object).floatValue();
} else if (object instanceof Reference) {
return ((Number)getObject((Reference) object)).floatValue();
} else {
return null;
}
}

/**
* The Reference is to the Stream from which the ICC color space data
* is to be parsed. So, without this method, we would be making and
Expand Down
6 changes: 3 additions & 3 deletions core/core-awt/src/main/java/org/icepdf/core/util/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ else if (o instanceof Dictionary) {
tmp.setPObjectReference(objectReference);
}

// the the object to the library
// the object to the library
library.addObject(o, objectReference);

return new PObject(o, objectReference);
Expand All @@ -677,7 +677,7 @@ public Object getStreamObject() throws IOException {
if (o.equals("<<")) {
HashMap<Object, Object> h = new HashMap<>();
Object o1 = getStreamObject();
while (!o1.equals(">>")) {
while (o1 != null && !o1.equals(">>")) {
h.put(o1, getStreamObject());
o1 = getStreamObject();
}
Expand All @@ -688,7 +688,7 @@ public Object getStreamObject() throws IOException {
else if (o.equals("[")) {
List<Object> v = new ArrayList<>();
Object o1 = getStreamObject();
while (!o1.equals("]")) {
while (o1 != null && !o1.equals("]")) {
v.add(o1);
o1 = getStreamObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1351,12 +1351,14 @@ protected static void consume_TJ(GraphicsState graphicState, Stack<Object> stack
stringObject = (StringObject) currentObject;
textState = graphicState.getTextState();
// draw string takes care of PageText extraction
drawString(stringObject.getLiteralStringBuffer(
textState.font.getSubTypeFormat(),
textState.font.getFont()),
textMetrics,
graphicState.getTextState(), shapes, glyphOutlineClip,
graphicState, oCGs);
if (stringObject.getLength() > 0) {
drawString(stringObject.getLiteralStringBuffer(
textState.font.getSubTypeFormat(),
textState.font.getFont()),
textMetrics,
graphicState.getTextState(), shapes, glyphOutlineClip,
graphicState, oCGs);
}
} else if (currentObject instanceof Number) {
f = (Number) currentObject;
textMetrics.getAdvance().x -= (f.floatValue() / 1000f) *
Expand Down Expand Up @@ -1384,14 +1386,16 @@ protected static void consume_Tj(GraphicsState graphicState, Stack<Object> stack
// apply transparency
setAlpha(shapes, graphicState, AlphaPaintType.ALPHA_FILL);
// draw string will take care of text pageText construction
drawString(stringObject.getLiteralStringBuffer(
textState.font.getSubTypeFormat(),
textState.font.getFont()),
textMetrics,
graphicState.getTextState(),
shapes,
glyphOutlineClip,
graphicState, oCGs);
if (stringObject.getLength() > 0) {
drawString(stringObject.getLiteralStringBuffer(
textState.font.getSubTypeFormat(),
textState.font.getFont()),
textMetrics,
graphicState.getTextState(),
shapes,
glyphOutlineClip,
graphicState, oCGs);
}
graphicState.set(tmp);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ private DictionaryEntries startDictionary() throws IOException {
Object key = null;
Object value;
int count = 1;
while (!(streamBytes[pos] == '>' && streamBytes[pos + 1] == '>')) {
while (pos < streamBytes.length &&
!(streamBytes[pos] == '>' && streamBytes[pos + 1] == '>')) {
if (count == 1) {
key = next();
// double check we don't have an empty dictionary << >>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public static PObject getInstance(Library library, int objectNumber, int generat
Object objectData, ByteBuffer streamData) {
// if we have as a byteBuffer then we have a stream.
if (streamData != null) {
DictionaryEntries entries = (DictionaryEntries) objectData;
DictionaryEntries entries;
if (objectData instanceof DictionaryEntries) {
entries = (DictionaryEntries)objectData;
} else {
entries = ((Dictionary)objectData).getEntries();
}
Name type = (Name) entries.get(Dictionary.TYPE_KEY);
Name subType = (Name) entries.get(Dictionary.SUBTYPE_KEY);
// bulk copy as all our filters expect byte[], this may be expensive in some instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public PObject getPObject(ByteBuffer byteBuffer, int objectOffsetStart)
lexer.skipWhiteSpace();
// stream offset
streamOffsetStart = byteBuffer.position();
int streamLength = library.getInt((DictionaryEntries) objectData, Dictionary.LENGTH_KEY);
int streamLength = getLength(objectData);
// create a new buffer to encapsulate the stream data using the length
streamByteBuffer = ByteBufferUtil.sliceObjectStream(
byteBuffer,
Expand Down Expand Up @@ -127,6 +127,17 @@ public PObject getPObject(ByteBuffer byteBuffer, int objectOffsetStart)
return ObjectFactory.getInstance(library, objectNumber, objectGeneration, objectData, streamByteBuffer);
}

private int getLength(Object objectData){
if (objectData instanceof DictionaryEntries) {
return library.getInt((DictionaryEntries) objectData, Dictionary.LENGTH_KEY);
} else if (objectData instanceof Dictionary) {
Dictionary dictionary = (Dictionary) objectData;
return library.getInt(dictionary.getEntries(), Dictionary.LENGTH_KEY);
} else {
return 0;
}
}

public PObject getCompressedObject(ByteBuffer streamObjectByteBuffer, int objectNumber,
int objectOffsetStart) throws IOException {
// grab the pieces of the object
Expand Down

0 comments on commit 09fe6a2

Please sign in to comment.