diff --git a/src/java/org/apache/poi/sl/draw/DrawPaint.java b/src/java/org/apache/poi/sl/draw/DrawPaint.java index d6986f302..a83060538 100644 --- a/src/java/org/apache/poi/sl/draw/DrawPaint.java +++ b/src/java/org/apache/poi/sl/draw/DrawPaint.java @@ -29,7 +29,10 @@ import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; +import java.util.Map; import java.util.Objects; +import java.util.TreeMap; +import java.util.function.BiFunction; import org.apache.poi.sl.usermodel.AbstractColorStyle; import org.apache.poi.sl.usermodel.ColorStyle; @@ -197,28 +200,17 @@ public class DrawPaint { @Override public int getShade() { - int shade = orig.getShade(); - switch (modifier) { - case DARKEN: - return Math.min(100000, Math.max(0,shade)+40000); - case DARKEN_LESS: - return Math.min(100000, Math.max(0,shade)+20000); - default: - return shade; - } + return scale(orig.getShade(), PaintModifier.DARKEN_LESS, PaintModifier.DARKEN); } @Override public int getTint() { - int tint = orig.getTint(); - switch (modifier) { - case LIGHTEN: - return Math.min(100000, Math.max(0,tint)+40000); - case LIGHTEN_LESS: - return Math.min(100000, Math.max(0,tint)+20000); - default: - return tint; - } + return scale(orig.getTint(), PaintModifier.LIGHTEN_LESS, PaintModifier.LIGHTEN); + } + + private int scale(int value, PaintModifier lessModifier, PaintModifier moreModifier) { + int delta = (modifier == lessModifier ? 20000 : (modifier == moreModifier ? 40000 : 0)); + return Math.min(100000, Math.max(0,value)+delta); } }; @@ -300,7 +292,7 @@ public class DrawPaint { Color result = color.getColor(); double alpha = getAlpha(result, color); - double hsl[] = RGB2HSL(result); // values are in the range [0..100] (usually ...) + double[] hsl = RGB2HSL(result); // values are in the range [0..100] (usually ...) applyHslModOff(hsl, 0, color.getHueMod(), color.getHueOff()); applyHslModOff(hsl, 1, color.getSatMod(), color.getSatOff()); applyHslModOff(hsl, 2, color.getLumMod(), color.getLumOff()); @@ -344,7 +336,7 @@ public class DrawPaint { * @param mod the modulation adjustment * @param off the offset adjustment */ - private static void applyHslModOff(double hsl[], int hslPart, int mod, int off) { + private static void applyHslModOff(double[] hsl, int hslPart, int mod, int off) { if (mod == -1) { mod = 100000; } @@ -363,7 +355,7 @@ public class DrawPaint { * * For a shade, the equation is luminance * %tint. */ - private static void applyShade(double hsl[], ColorStyle fc) { + private static void applyShade(double[] hsl, ColorStyle fc) { int shade = fc.getShade(); if (shade == -1) { return; @@ -380,7 +372,7 @@ public class DrawPaint { * For a tint, the equation is luminance * %tint + (1-%tint). * (Note that 1-%tint is equal to the lumOff value in DrawingML.) */ - private static void applyTint(double hsl[], ColorStyle fc) { + private static void applyTint(double[] hsl, ColorStyle fc) { int tint = fc.getTint(); if (tint == -1) { return; @@ -403,70 +395,63 @@ public class DrawPaint { } Rectangle2D anchor = DrawShape.getAnchor(graphics, shape); - final double h = anchor.getHeight(), w = anchor.getWidth(), x = anchor.getX(), y = anchor.getY(); AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(angle), anchor.getCenterX(), anchor.getCenterY()); - double diagonal = Math.sqrt(h * h + w * w); - Point2D p1 = new Point2D.Double(x + w / 2 - diagonal / 2, y + h / 2); - p1 = at.transform(p1, null); - - Point2D p2 = new Point2D.Double(x + w, y + h / 2); - p2 = at.transform(p2, null); + double diagonal = Math.sqrt(Math.pow(anchor.getWidth(),2) + Math.pow(anchor.getHeight(),2)); + final Point2D p1 = at.transform(new Point2D.Double(anchor.getCenterX() - diagonal / 2, anchor.getCenterY()), null); + final Point2D p2 = at.transform(new Point2D.Double(anchor.getMaxX(), anchor.getCenterY()), null); // snapToAnchor(p1, anchor); // snapToAnchor(p2, anchor); - if (p1.equals(p2)) { - // gradient paint on the same point throws an exception ... and doesn't make sense - return null; - } - - float[] fractions = fill.getGradientFractions(); - Color[] colors = new Color[fractions.length]; - - int i = 0; - for (ColorStyle fc : fill.getGradientColors()) { - // if fc is null, use transparent color to get color of background - colors[i++] = (fc == null) ? TRANSPARENT : applyColorTransform(fc); - } - - return new LinearGradientPaint(p1, p2, fractions, colors); + // gradient paint on the same point throws an exception ... and doesn't make sense + return (p1.equals(p2)) ? null : safeFractions((f,c)->new LinearGradientPaint(p1,p2,f,c), fill); } + @SuppressWarnings("WeakerAccess") protected Paint createRadialGradientPaint(GradientPaint fill, Graphics2D graphics) { Rectangle2D anchor = DrawShape.getAnchor(graphics, shape); - Point2D pCenter = new Point2D.Double(anchor.getX() + anchor.getWidth()/2, - anchor.getY() + anchor.getHeight()/2); + final Point2D pCenter = new Point2D.Double(anchor.getCenterX(), anchor.getCenterY()); - float radius = (float)Math.max(anchor.getWidth(), anchor.getHeight()); + final float radius = (float)Math.max(anchor.getWidth(), anchor.getHeight()); - float[] fractions = fill.getGradientFractions(); - Color[] colors = new Color[fractions.length]; - - int i=0; - for (ColorStyle fc : fill.getGradientColors()) { - colors[i++] = applyColorTransform(fc); - } - - return new RadialGradientPaint(pCenter, radius, fractions, colors); + return safeFractions((f,c)->new RadialGradientPaint(pCenter,radius,f,c), fill); } @SuppressWarnings({"WeakerAccess", "unused"}) protected Paint createPathGradientPaint(GradientPaint fill, Graphics2D graphics) { // currently we ignore an eventually center setting - float[] fractions = fill.getGradientFractions(); - Color[] colors = new Color[fractions.length]; + return safeFractions(PathGradientPaint::new, fill); + } - int i=0; - for (ColorStyle fc : fill.getGradientColors()) { - colors[i++] = applyColorTransform(fc); + private Paint safeFractions(BiFunction init, GradientPaint fill) { + float[] fractions = fill.getGradientFractions(); + final ColorStyle[] styles = fill.getGradientColors(); + + // need to remap the fractions, because Java doesn't like repeating fraction values + Map m = new TreeMap<>(); + for (int i = 0; i me : m.entrySet()) { + fractions[i] = me.getKey(); + colors[i] = me.getValue(); + i++; + } + + return init.apply(fractions, colors); } /** diff --git a/src/java/org/apache/poi/sl/draw/PathGradientPaint.java b/src/java/org/apache/poi/sl/draw/PathGradientPaint.java index d4a2a5fa5..2281bbff8 100644 --- a/src/java/org/apache/poi/sl/draw/PathGradientPaint.java +++ b/src/java/org/apache/poi/sl/draw/PathGradientPaint.java @@ -23,21 +23,24 @@ import java.awt.MultipleGradientPaint.CycleMethod; import java.awt.geom.*; import java.awt.image.*; +import org.apache.poi.util.Internal; + +@Internal class PathGradientPaint implements Paint { // http://asserttrue.blogspot.de/2010/01/how-to-iimplement-custom-paint-in-50.html - protected final Color colors[]; - protected final float fractions[]; - protected final int capStyle; - protected final int joinStyle; - protected final int transparency; + private final Color[] colors; + private final float[] fractions; + private final int capStyle; + private final int joinStyle; + private final int transparency; - public PathGradientPaint(Color colors[], float fractions[]) { - this(colors,fractions,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); + PathGradientPaint(float[] fractions, Color[] colors) { + this(fractions,colors,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); } - public PathGradientPaint(Color colors[], float fractions[], int capStyle, int joinStyle) { + private PathGradientPaint(float[] fractions, Color[] colors, int capStyle, int joinStyle) { this.colors = colors.clone(); this.fractions = fractions.clone(); this.capStyle = capStyle; @@ -66,26 +69,26 @@ class PathGradientPaint implements Paint { } class PathGradientContext implements PaintContext { - protected final Rectangle deviceBounds; - protected final Rectangle2D userBounds; + final Rectangle deviceBounds; + final Rectangle2D userBounds; protected final AffineTransform xform; - protected final RenderingHints hints; + final RenderingHints hints; /** * for POI: the shape will be only known when the subclasses determines the concrete implementation * in the draw/-content method, so we need to postpone the setting/creation as long as possible **/ protected final Shape shape; - protected final PaintContext pCtx; - protected final int gradientSteps; + final PaintContext pCtx; + final int gradientSteps; WritableRaster raster; - public PathGradientContext( - ColorModel cm - , Rectangle deviceBounds - , Rectangle2D userBounds - , AffineTransform xform - , RenderingHints hints + PathGradientContext( + ColorModel cm + , Rectangle deviceBounds + , Rectangle2D userBounds + , AffineTransform xform + , RenderingHints hints ) { shape = (Shape)hints.get(Drawable.GRADIENT_SHAPE); if (shape == null) { @@ -139,7 +142,7 @@ class PathGradientPaint implements Paint { return childRaster; } - protected int getGradientSteps(Shape gradientShape) { + int getGradientSteps(Shape gradientShape) { Rectangle rect = gradientShape.getBounds(); int lower = 1; int upper = (int)(Math.max(rect.getWidth(),rect.getHeight())/2.0); @@ -158,7 +161,7 @@ class PathGradientPaint implements Paint { - protected void createRaster() { + void createRaster() { ColorModel cm = getColorModel(); raster = cm.createCompatibleWritableRaster((int)deviceBounds.getWidth(), (int)deviceBounds.getHeight()); BufferedImage img = new BufferedImage(cm, raster, false, null); @@ -168,7 +171,7 @@ class PathGradientPaint implements Paint { graphics.transform(xform); Raster img2 = pCtx.getRaster(0, 0, gradientSteps, 1); - int rgb[] = new int[cm.getNumComponents()]; + int[] rgb = new int[cm.getNumComponents()]; for (int i = gradientSteps-1; i>=0; i--) { img2.getPixel(i, 0, rgb); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java index 94205da14..6e2759972 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java @@ -47,7 +47,7 @@ public class TestPPTX2PNG { private static final POIDataSamples samples = POIDataSamples.getSlideShowInstance(); private static final File basedir = null; private static final String files = - "53446.ppt, alterman_security.ppt, alterman_security.pptx, KEY02.pptx, themes.pptx, backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt"; + "53446.ppt, alterman_security.ppt, alterman_security.pptx, KEY02.pptx, themes.pptx, backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt, keyframes.ppt"; diff --git a/test-data/slideshow/keyframes.ppt b/test-data/slideshow/keyframes.ppt new file mode 100644 index 000000000..e653d6465 Binary files /dev/null and b/test-data/slideshow/keyframes.ppt differ