This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 7fd9af27b8 Draw shadows of flipped shapes in the direction PowerPoint 
uses (#1535)
7fd9af27b8 is described below

commit 7fd9af27b8fefd421aed3f0408052152ca2af5f9
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Nov 5 10:21:55 2026 +0100

    Draw shadows of flipped shapes in the direction PowerPoint uses (#1535)
    
    DrawSimpleShape.drawShadow compensated for the shape rotation and added
    180 degrees for a vertical flip, ignoring horizontal flips, flips and
    rotation of parent groups, and rotWithShape.
    
    PowerPoint keeps the shadow direction on the page with rotWithShape=0.
    With rotWithShape=1, or if it is unset, the shadow offset rotates and
    flips with the shape. Binary slide shows have no such property and keep
    the page direction, which is unverified against PowerPoint for flips.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/poi/xslf/usermodel/XSLFShadow.java  |  11 ++
 .../apache/poi/xslf/usermodel/TestXSLFShadow.java  | 141 +++++++++++++++++++++
 .../apache/poi/hslf/usermodel/TestHSLFShadow.java  | 121 ++++++++++++++++++
 .../org/apache/poi/sl/draw/DrawSimpleShape.java    |  50 ++++++--
 .../java/org/apache/poi/sl/usermodel/Shadow.java   |  12 +-
 5 files changed, 326 insertions(+), 9 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFShadow.java 
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFShadow.java
index 0de14f6645..6c79244afe 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFShadow.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFShadow.java
@@ -81,6 +81,17 @@ public class XSLFShadow extends XSLFShape implements 
Shadow<XSLFShape,XSLFTextPa
         return ct.isSetBlurRad() ? Units.toPoints(ct.getBlurRad()) : 0;
     }
 
+    /**
+     * @return {@code true} if the shadow offset rotates and flips with its 
shape, which is the default if unset
+     *
+     * @since 6.1.0
+     */
+    @Override
+    public boolean isRotateWithShape() {
+        CTOuterShadowEffect ct = (CTOuterShadowEffect)getXmlObject();
+        return !ct.isSetRotWithShape() || ct.getRotWithShape();
+    }
+
     /**
      * @return the color of this shadow.
      * Depending whether the parent shape is filled or stroked, this color is 
used to fill or stroke this shadow
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShadow.java 
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShadow.java
new file mode 100644
index 0000000000..3b5e66bafd
--- /dev/null
+++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFShadow.java
@@ -0,0 +1,141 @@
+/*
+ *  ====================================================================
+ *    Licensed to the Apache Software Foundation (ASF) under one or more
+ *    contributor license agreements.  See the NOTICE file distributed with
+ *    this work for additional information regarding copyright ownership.
+ *    The ASF licenses this file to You under the Apache License, Version 2.0
+ *    (the "License"); you may not use this file except in compliance with
+ *    the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ * ====================================================================
+ */
+package org.apache.poi.xslf.usermodel;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+import java.io.IOException;
+
+import org.apache.poi.sl.draw.DrawSimpleShape;
+import org.apache.poi.sl.usermodel.SimpleShape;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
+import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
+
+/**
+ * The expected angles were observed by rendering flipped and rotated shapes 
in PowerPoint.
+ */
+class TestXSLFShadow {
+    private static final long DIST_EMU = 254000;
+    private static final int DIR_45_DEGREES = 2700000;
+    private static final double DIST_POINTS = 20;
+
+    @ParameterizedTest
+    @CsvSource({
+        "false, 0,  false, false, 45",
+        "false, 0,  true,  false, 45",
+        "false, 0,  false, true,  45",
+        "false, 0,  true,  true,  45",
+        "false, 30, false, false, 45",
+        "false, 30, true,  false, 45",
+        "false, 30, false, true,  45",
+        "false, 30, true,  true,  45",
+        "true,  0,  false, false, 45",
+        "true,  0,  true,  false, 135",
+        "true,  0,  false, true,  -45",
+        "true,  0,  true,  true,  225",
+        "true,  30, false, false, 75",
+        "true,  30, true,  false, 165",
+        "true,  30, false, true,  -15",
+        "true,  30, true,  true,  255",
+    })
+    void shadowOfShape(boolean rotWithShape, double rotation, boolean flipH, 
boolean flipV, double pageAngle)
+    throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFAutoShape shape = ppt.createSlide().createAutoShape();
+            addShadow(shape).setRotWithShape(rotWithShape);
+            shape.setRotation(rotation);
+            shape.setFlipHorizontal(flipH);
+            shape.setFlipVertical(flipV);
+
+            AffineTransform tx = new AffineTransform();
+            tx.rotate(Math.toRadians(rotation));
+            tx.scale(flipH ? -1 : 1, flipV ? -1 : 1);
+
+            assertOffset(pageAngle, tx, shape);
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "false, false, false, 45",
+        "false, true,  false, 45",
+        "false, false, true,  45",
+        "false, true,  true,  45",
+        "true,  false, false, 45",
+        "true,  true,  false, 135",
+        "true,  false, true,  -45",
+        "true,  true,  true,  225",
+    })
+    void shadowOfShapeInFlippedGroup(boolean rotWithShape, boolean flipH, 
boolean flipV, double pageAngle)
+    throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFGroupShape group = ppt.createSlide().createGroup();
+            group.setFlipHorizontal(flipH);
+            group.setFlipVertical(flipV);
+            XSLFAutoShape shape = group.createAutoShape();
+            addShadow(shape).setRotWithShape(rotWithShape);
+
+            AffineTransform tx = AffineTransform.getScaleInstance(flipH ? -1 : 
1, flipV ? -1 : 1);
+
+            assertOffset(pageAngle, tx, shape);
+        }
+    }
+
+    @Test
+    void rotateWithShapeDefaultsToTrue() throws IOException {
+        try (XMLSlideShow ppt = new XMLSlideShow()) {
+            XSLFAutoShape shape = ppt.createSlide().createAutoShape();
+            CTOuterShadowEffect shadow = addShadow(shape);
+            assertTrue(shape.getShadow().isRotateWithShape(), "unset 
rotWithShape");
+
+            shadow.setRotWithShape(false);
+            assertFalse(shape.getShadow().isRotateWithShape(), 
"rotWithShape=0");
+        }
+    }
+
+    private static CTOuterShadowEffect addShadow(XSLFSimpleShape shape) {
+        CTOuterShadowEffect shadow = 
((CTShape)shape.getXmlObject()).getSpPr().addNewEffectLst().addNewOuterShdw();
+        shadow.setDist(DIST_EMU);
+        shadow.setDir(DIR_45_DEGREES);
+        return shadow;
+    }
+
+    private static void assertOffset(double pageAngle, AffineTransform 
shapeTransform, XSLFSimpleShape shape) {
+        Point2D onPage = shapeTransform.deltaTransform(new 
ShadowOffset(shape).get(), null);
+        assertEquals(DIST_POINTS * Math.cos(Math.toRadians(pageAngle)), 
onPage.getX(), 1e-6);
+        assertEquals(DIST_POINTS * Math.sin(Math.toRadians(pageAngle)), 
onPage.getY(), 1e-6);
+    }
+
+    private static final class ShadowOffset extends DrawSimpleShape {
+        ShadowOffset(SimpleShape<?,?> shape) {
+            super(shape);
+        }
+
+        Point2D get() {
+            return getShadowOffset(getShape().getShadow());
+        }
+    }
+}
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestHSLFShadow.java
 
b/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestHSLFShadow.java
new file mode 100644
index 0000000000..e320aaec4a
--- /dev/null
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestHSLFShadow.java
@@ -0,0 +1,121 @@
+/*
+ *  ====================================================================
+ *    Licensed to the Apache Software Foundation (ASF) under one or more
+ *    contributor license agreements.  See the NOTICE file distributed with
+ *    this work for additional information regarding copyright ownership.
+ *    The ASF licenses this file to You under the Apache License, Version 2.0
+ *    (the "License"); you may not use this file except in compliance with
+ *    the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ * ====================================================================
+ */
+package org.apache.poi.hslf.usermodel;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+import java.io.IOException;
+
+import org.apache.poi.ddf.AbstractEscherOptRecord;
+import org.apache.poi.ddf.EscherPropertyTypes;
+import org.apache.poi.ddf.EscherSimpleProperty;
+import org.apache.poi.sl.draw.DrawSimpleShape;
+import org.apache.poi.sl.usermodel.Shadow;
+import org.apache.poi.sl.usermodel.SimpleShape;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Binary slide shows have no rotWithShape property, so the shadow keeps its 
direction on the page.
+ */
+class TestHSLFShadow {
+    private static final int OFFSET_EMU = 254000;
+
+    @ParameterizedTest
+    @CsvSource({
+        "0,  false, false",
+        "0,  true,  false",
+        "0,  false, true",
+        "0,  true,  true",
+        "30, false, false",
+        "30, true,  false",
+        "30, false, true",
+        "30, true,  true",
+    })
+    void shadowOfShape(double rotation, boolean flipH, boolean flipV) throws 
IOException {
+        try (HSLFSlideShow ppt = new HSLFSlideShow()) {
+            HSLFAutoShape shape = ppt.createSlide().createAutoShape();
+            addShadow(shape);
+            shape.setRotation(rotation);
+            flip(shape, flipH, flipV);
+
+            // HSLF flips before it rotates, see DrawShape.applyTransform
+            AffineTransform tx = AffineTransform.getScaleInstance(flipH ? -1 : 
1, flipV ? -1 : 1);
+            tx.rotate(Math.toRadians(rotation));
+
+            assertPageDirection(tx, shape);
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "false, false",
+        "true,  false",
+        "false, true",
+        "true,  true",
+    })
+    void shadowOfShapeInFlippedGroup(boolean flipH, boolean flipV) throws 
IOException {
+        try (HSLFSlideShow ppt = new HSLFSlideShow()) {
+            HSLFGroupShape group = ppt.createSlide().createGroup();
+            flip(group, flipH, flipV);
+            HSLFAutoShape shape = group.createAutoShape();
+            addShadow(shape);
+
+            AffineTransform tx = AffineTransform.getScaleInstance(flipH ? -1 : 
1, flipV ? -1 : 1);
+
+            assertPageDirection(tx, shape);
+        }
+    }
+
+    private static void flip(HSLFShape shape, boolean flipH, boolean flipV) {
+        if (flipH) {
+            shape.setFlipHorizontal(true);
+        }
+        if (flipV) {
+            shape.setFlipVertical(true);
+        }
+    }
+
+    private static void addShadow(HSLFSimpleShape shape) {
+        AbstractEscherOptRecord opt = shape.getEscherOptRecord();
+        opt.setEscherProperty(new 
EscherSimpleProperty(EscherPropertyTypes.SHADOWSTYLE__TYPE, 0));
+        opt.setEscherProperty(new 
EscherSimpleProperty(EscherPropertyTypes.SHADOWSTYLE__OFFSETX, OFFSET_EMU));
+        opt.setEscherProperty(new 
EscherSimpleProperty(EscherPropertyTypes.SHADOWSTYLE__OFFSETY, OFFSET_EMU));
+    }
+
+    private static void assertPageDirection(AffineTransform shapeTransform, 
HSLFSimpleShape shape) {
+        Shadow<?,?> shadow = shape.getShadow();
+        double expected = shadow.getDistance() * Math.cos(Math.toRadians(45));
+        Point2D onPage = shapeTransform.deltaTransform(new 
ShadowOffset(shape).get(), null);
+        assertEquals(expected, onPage.getX(), 1e-6);
+        assertEquals(expected, onPage.getY(), 1e-6);
+    }
+
+    private static final class ShadowOffset extends DrawSimpleShape {
+        ShadowOffset(SimpleShape<?,?> shape) {
+            super(shape);
+        }
+
+        Point2D get() {
+            return getShadowOffset(getShape().getShadow());
+        }
+    }
+}
diff --git a/poi/src/main/java/org/apache/poi/sl/draw/DrawSimpleShape.java 
b/poi/src/main/java/org/apache/poi/sl/draw/DrawSimpleShape.java
index 4ed23e03d9..2ed4bf905b 100644
--- a/poi/src/main/java/org/apache/poi/sl/draw/DrawSimpleShape.java
+++ b/poi/src/main/java/org/apache/poi/sl/draw/DrawSimpleShape.java
@@ -27,6 +27,7 @@ import java.awt.Stroke;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Ellipse2D;
 import java.awt.geom.Path2D;
+import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -44,6 +45,7 @@ import 
org.apache.poi.sl.usermodel.LineDecoration.DecorationSize;
 import org.apache.poi.sl.usermodel.PaintStyle;
 import org.apache.poi.sl.usermodel.PaintStyle.PaintModifier;
 import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
+import org.apache.poi.sl.usermodel.PlaceableShape;
 import org.apache.poi.sl.usermodel.Shadow;
 import org.apache.poi.sl.usermodel.SimpleShape;
 import org.apache.poi.util.Units;
@@ -355,14 +357,9 @@ public class DrawSimpleShape extends DrawShape {
         SolidPaint shadowPaint = shadow.getFillStyle();
         Color shadowColor = 
DrawPaint.applyColorTransform(shadowPaint.getSolidColor());
 
-        double shapeRotation = getShape().getRotation();
-        if (getShape().getFlipVertical()) {
-            shapeRotation += 180;
-        }
-        double angle = shadow.getAngle() - shapeRotation;
-        double dist = shadow.getDistance();
-        double dx = dist * Math.cos(Math.toRadians(angle));
-        double dy = dist * Math.sin(Math.toRadians(angle));
+        Point2D offset = getShadowOffset(shadow);
+        double dx = offset.getX();
+        double dy = offset.getY();
 
         graphics.translate(dx, dy);
 
@@ -382,6 +379,43 @@ public class DrawSimpleShape extends DrawShape {
         graphics.translate(-dx, -dy);
     }
 
+    /**
+     * Calculates the shadow offset to apply to a graphics, which already has 
the transforms
+     * of this shape and its parent groups applied (see {@link 
DrawShape#applyTransform(Graphics2D)}).
+     *
+     * @param shadow the shadow of this shape
+     * @return the offset in the coordinate system of the transformed graphics
+     *
+     * @since 6.1.0
+     */
+    protected Point2D getShadowOffset(Shadow<?,?> shadow) {
+        double angle = Math.toRadians(shadow.getAngle());
+        double dist = shadow.getDistance();
+        Point2D offset = new Point2D.Double(dist * Math.cos(angle), dist * 
Math.sin(angle));
+        if (shadow.isRotateWithShape()) {
+            // the graphics transform rotates and flips the offset together 
with the shape
+            return offset;
+        }
+
+        // the offset is relative to the page, so revert the rotation and 
flips of the graphics transform
+        AffineTransform inverse = new AffineTransform();
+        Object current = getShape();
+        while (current instanceof PlaceableShape<?,?> ps) {
+            double rotation = Math.toRadians(ps.getRotation());
+            double scaleX = ps.getFlipHorizontal() ? -1 : 1;
+            double scaleY = ps.getFlipVertical() ? -1 : 1;
+            if (isHSLF(ps)) {
+                inverse.rotate(-rotation);
+                inverse.scale(scaleX, scaleY);
+            } else {
+                inverse.scale(scaleX, scaleY);
+                inverse.rotate(-rotation);
+            }
+            current = ps.getParent();
+        }
+        return inverse.deltaTransform(offset, null);
+    }
+
     protected Collection<Outline> computeOutlines(Graphics2D graphics) {
         final SimpleShape<?,?> sh = getShape();
 
diff --git a/poi/src/main/java/org/apache/poi/sl/usermodel/Shadow.java 
b/poi/src/main/java/org/apache/poi/sl/usermodel/Shadow.java
index de462f9132..95e31a47a5 100644
--- a/poi/src/main/java/org/apache/poi/sl/usermodel/Shadow.java
+++ b/poi/src/main/java/org/apache/poi/sl/usermodel/Shadow.java
@@ -49,5 +49,15 @@ P extends TextParagraph<S,P,? extends TextRun>
      * @return the color of this shadow. 
      * Depending whether the parent shape is filled or stroked, this color is 
used to fill or stroke this shadow
      */
-    SolidPaint getFillStyle();    
+    SolidPaint getFillStyle();
+
+    /**
+     * @return {@code true} if the shadow offset rotates and flips with its 
shape,
+     *  {@code false} if the shadow keeps its direction on the page
+     *
+     * @since 6.1.0
+     */
+    default boolean isRotateWithShape() {
+        return false;
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to