Skip to content

Commit

Permalink
Merge pull request #25 from moxionio/moxion
Browse files Browse the repository at this point in the history
Added support for Apache PDFBox TilingPaint.
  • Loading branch information
rototor committed May 29, 2020
2 parents adc72b3 + 2e97fe5 commit 60a90c9
Showing 1 changed file with 56 additions and 0 deletions.
Expand Up @@ -27,6 +27,7 @@
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.*;
Expand Down Expand Up @@ -149,6 +150,9 @@ else if (simpleName.equals("PatternPaint"))
{
applyPatternPaint(paint, state);
}
else if(simpleName.equals("TilingPaint")) {
applyTilingPaint(paint, state);
}
else if (paint instanceof GradientPaint)
{
return shadingCache.makeUnqiue(buildGradientShading((GradientPaint) paint, state));
Expand Down Expand Up @@ -248,6 +252,23 @@ private void applyPatternPaint(Paint paint, PaintApplierState state) throws IOEx
state.contentStream.setStrokingColor(patternColor);
}

/*
* Apache PDFBox Tiling Paint
*/
private void applyTilingPaint(Paint paint, PaintApplierState state)
{
try
{
Paint tilingPaint = getPrivateFieldValue(paint, "paint");
applyPaint(tilingPaint, state);
}
catch (Exception e)
{
System.err.println("PdfBoxGraphics2DPaintApplier error while drawing Tiling Paint");
e.printStackTrace();
}
}

private void applyComposite(PaintApplierState state)
{
/*
Expand Down Expand Up @@ -828,6 +849,41 @@ protected static <T> T getPropertyValue(Object obj, String propertyGetter)
}
}

/**
* Get the value of a private field from the specified object.
*
* @param obj The object containing the field.
* @param fieldName The name of the field.
* @param <T> The field type.
* @return The private field value.
*/
@SuppressWarnings({ "unchecked", "WeakerAccess" })
protected static <T> T getPrivateFieldValue(Object obj, String fieldName)
{
try
{
Class<?> c = obj.getClass();
while (c != null)
{
try
{
Field f = c.getDeclaredField(fieldName);
f.setAccessible(true);
return (T) f.get(obj);
}
catch (NoSuchFieldException ignored)
{
}
c = c.getSuperclass();
}
throw new NullPointerException("Field " + fieldName + " not found!");
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

private static abstract class COSResourceCacheBase<TObject extends COSObjectable>
{
private Map<Integer, List<TObject>> states = new HashMap<Integer, List<TObject>>();
Expand Down

0 comments on commit 60a90c9

Please sign in to comment.