Search in sources :

Example 1 with GradientPaint

use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint in project poi by apache.

the class DrawPaint method createRadialGradientPaint.

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);
    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);
}
Also used : Point2D(java.awt.geom.Point2D) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) RadialGradientPaint(java.awt.RadialGradientPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint)

Example 2 with GradientPaint

use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint in project poi by apache.

the class HSLFFill method getGradientPaint.

private GradientPaint getGradientPaint(final GradientType gradientType) {
    AbstractEscherOptRecord opt = shape.getEscherOptRecord();
    final EscherArrayProperty ep = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__SHADECOLORS);
    final int colorCnt = (ep == null) ? 0 : ep.getNumberOfElementsInArray();
    // NOFILLHITTEST can be in the normal escher opt record but also in the tertiary record
    // the extended bit fields seem to be in the second
    opt = (AbstractEscherOptRecord) shape.getEscherChild(RecordTypes.EscherUserDefined);
    EscherSimpleProperty p = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST);
    int propVal = (p == null) ? 0 : p.getPropertyValue();
    final boolean rotateWithShape = FILL_USE_USE_SHAPE_ANCHOR.isSet(propVal) && FILL_USE_SHAPE_ANCHOR.isSet(propVal);
    return new GradientPaint() {

        @Override
        public double getGradientAngle() {
            // A value of type FixedPoint, as specified in [MS-OSHARED] section 2.2.1.6,
            // that specifies the angle of the gradient fill. Zero degrees represents a vertical vector from
            // bottom to top. The default value for this property is 0x00000000.
            int rot = shape.getEscherProperty(EscherProperties.FILL__ANGLE);
            return 90 - Units.fixedPointToDouble(rot);
        }

        @Override
        public ColorStyle[] getGradientColors() {
            ColorStyle[] cs;
            if (colorCnt == 0) {
                cs = new ColorStyle[2];
                cs[0] = wrapColor(getBackgroundColor());
                cs[1] = wrapColor(getForegroundColor());
            } else {
                cs = new ColorStyle[colorCnt];
                int idx = 0;
                // TODO: handle palette colors and alpha(?) value 
                for (byte[] data : ep) {
                    EscherColorRef ecr = new EscherColorRef(data, 0, 4);
                    cs[idx++] = wrapColor(shape.getColor(ecr));
                }
            }
            return cs;
        }

        private ColorStyle wrapColor(Color col) {
            return (col == null) ? null : DrawPaint.createSolidPaint(col).getSolidColor();
        }

        @Override
        public float[] getGradientFractions() {
            float[] frc;
            if (colorCnt == 0) {
                frc = new float[] { 0, 1 };
            } else {
                frc = new float[colorCnt];
                int idx = 0;
                for (byte[] data : ep) {
                    double pos = Units.fixedPointToDouble(LittleEndian.getInt(data, 4));
                    frc[idx++] = (float) pos;
                }
            }
            return frc;
        }

        @Override
        public boolean isRotatedWithShape() {
            return rotateWithShape;
        }

        @Override
        public GradientType getGradientType() {
            return gradientType;
        }
    };
}
Also used : EscherColorRef(org.apache.poi.ddf.EscherColorRef) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) EscherArrayProperty(org.apache.poi.ddf.EscherArrayProperty) Color(java.awt.Color) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) AbstractEscherOptRecord(org.apache.poi.ddf.AbstractEscherOptRecord) DrawPaint(org.apache.poi.sl.draw.DrawPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty)

Example 3 with GradientPaint

use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint in project poi by apache.

the class TestXSLFTheme method slide4.

void slide4(XSLFSlide slide) {
    PaintStyle fs = slide.getBackground().getFillStyle().getPaint();
    assertTrue(fs instanceof GradientPaint);
    XSLFTextShape sh1 = (XSLFTextShape) getShape(slide, "Rectangle 4");
    XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
    assertTrue(sameColor(Color.white, run1.getFontColor()));
    assertEquals(new Color(148, 198, 0), sh1.getFillColor());
    // solid fill
    assertTrue(sh1.getFillStyle().getPaint() instanceof SolidPaint);
    XSLFTextShape sh2 = (XSLFTextShape) getShape(slide, "Title 3");
    XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
    assertTrue(sameColor(new Color(148, 198, 0), run2.getFontColor()));
    // no fill
    assertNull(sh2.getFillColor());
    assertTrue(slide.getSlideLayout().getFollowMasterGraphics());
}
Also used : SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) Color(java.awt.Color) TestCommonSL.sameColor(org.apache.poi.sl.TestCommonSL.sameColor) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)

Example 4 with GradientPaint

use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint in project poi by apache.

the class DrawPaint method createPathGradientPaint.

protected Paint createPathGradientPaint(GradientPaint fill, Graphics2D graphics) {
    // currently we ignore an eventually center setting
    float[] fractions = fill.getGradientFractions();
    Color[] colors = new Color[fractions.length];
    int i = 0;
    for (ColorStyle fc : fill.getGradientColors()) {
        colors[i++] = applyColorTransform(fc);
    }
    return new PathGradientPaint(colors, fractions);
}
Also used : ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Color(java.awt.Color) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint)

Example 5 with GradientPaint

use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint in project poi by apache.

the class DrawPaint method createLinearGradientPaint.

protected Paint createLinearGradientPaint(GradientPaint fill, Graphics2D graphics) {
    // TODO: we need to find the two points for gradient - the problem is, which point at the outline
    // do you take? My solution would be to apply the gradient rotation to the shape in reverse
    // and then scan the shape for the largest possible horizontal distance
    double angle = fill.getGradientAngle();
    if (!fill.isRotatedWithShape()) {
        angle -= shape.getRotation();
    }
    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);
    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);
}
Also used : Point2D(java.awt.geom.Point2D) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) LinearGradientPaint(java.awt.LinearGradientPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint)

Aggregations

GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)8 Color (java.awt.Color)5 ColorStyle (org.apache.poi.sl.usermodel.ColorStyle)5 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)5 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)4 LinearGradientPaint (java.awt.LinearGradientPaint)3 Paint (java.awt.Paint)3 RadialGradientPaint (java.awt.RadialGradientPaint)3 PaintStyle (org.apache.poi.sl.usermodel.PaintStyle)3 Point2D (java.awt.geom.Point2D)2 Rectangle2D (java.awt.geom.Rectangle2D)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)2 AffineTransform (java.awt.geom.AffineTransform)1 AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)1 EscherArrayProperty (org.apache.poi.ddf.EscherArrayProperty)1 EscherColorRef (org.apache.poi.ddf.EscherColorRef)1 EscherSimpleProperty (org.apache.poi.ddf.EscherSimpleProperty)1 TestCommonSL.sameColor (org.apache.poi.sl.TestCommonSL.sameColor)1 CTGradientStop (org.openxmlformats.schemas.drawingml.x2006.main.CTGradientStop)1 CTSchemeColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor)1