Skip to content

Commit

Permalink
GH-118 Internal Error workaround (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtache authored Sep 12, 2020
1 parent 84c3a06 commit 17295b1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import java.awt.geom.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.*;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Logger;

public final class BlendComposite implements Composite {
Expand Down Expand Up @@ -98,8 +101,38 @@ public enum BlendingMode {
private static boolean disableBlendComposite;

static {
// sets the shadow colour of the decorator.
disableBlendComposite = Defs.booleanProperty(
"org.icepdf.core.paint.disableBlendComposite", false);

/*
Check for XRSurfaceData.XRInternalSurfaceData.getRaster implementation
On Linux, the implementation simply returns InternalError
We have to preventatively disable it, otherwise it will cause an InternalError when annotations using
BlendComposite are rendered
*/
try {
final Class xrSurfaceDataClass = Class.forName("sun.java2d.xr.XRSurfaceData$XRInternalSurfaceData");
final Constructor constructor = xrSurfaceDataClass.getDeclaredConstructor(Class.forName("sun.java2d.xr.XRBackend"), int.class);
final Object xrSurfaceData = constructor.newInstance(null, 0);
final Method getRaster = xrSurfaceDataClass.getMethod("getRaster", int.class, int.class, int.class, int.class);
getRaster.invoke(xrSurfaceData, 0, 0, 0, 0);
} catch (final InvocationTargetException e) {
if (e.getCause() instanceof InternalError) {
disableBlendComposite = true;
}
/*
If there is another cause, this means that getRaster implementation is not simply "throw
new InternalError()", in which case this implementation may perfectly work with real values.
*/
} catch (final InternalError e) {
disableBlendComposite = true;
} catch (final Exception ignored) {
/*
If there are other exceptions (NoSuchClass, NoSuchMethod, etc), this probably means that the api changed
or that java2d.xr is not available, in which case the blend composite rendering could work perfectly.
*/
}
}

public static final Name NORMAL_VALUE = new Name("Normal");
Expand Down

0 comments on commit 17295b1

Please sign in to comment.