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 a1f491bd84 Implement ImageUtils.getDimensionFromAnchor variation. 
(#1090)
a1f491bd84 is described below

commit a1f491bd84aa0602867b2777534aceb3cd004002
Author: Jacobo Aragunde PĂ©rez <[email protected]>
AuthorDate: Wed May 27 14:40:14 2026 +0200

    Implement ImageUtils.getDimensionFromAnchor variation. (#1090)
    
    Add a variation of ImageUtils.getDimensionFromAnchor() that accepts
    ClientAnchor objects. A Sheet needs to be passed to obtain the cell
    width and height needed for the calculation. It uses the same code
    underneath as getDimensionFromAnchor(Picture).
---
 .../apache/poi/xssf/usermodel/TestXSSFPicture.java |  23 ++++++++++++++++++---
 .../java/org/apache/poi/ss/util/ImageUtils.java    |  20 ++++++++++++++++++
 .../apache/poi/ss/usermodel/BaseTestPicture.java   |   2 +-
 .../spreadsheet/picture-and-shape-same-size.xlsx   | Bin 0 -> 13204 bytes
 4 files changed, 41 insertions(+), 4 deletions(-)

diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java 
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java
index dc5d2bf09d..dae54b4773 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFPicture.java
@@ -22,14 +22,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
 
+import java.awt.Dimension;
 import java.io.IOException;
 import java.util.List;
 
 import org.apache.poi.openxml4j.opc.ZipPackage;
-import org.apache.poi.ss.usermodel.BaseTestPicture;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
-import org.apache.poi.ss.usermodel.Drawing;
-import org.apache.poi.ss.usermodel.Picture;
+import org.apache.poi.ss.usermodel.Shape;
+import org.apache.poi.ss.util.ImageUtils;
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
@@ -175,4 +176,20 @@ public final class TestXSSFPicture extends BaseTestPicture 
{
             ZipPackage.setEncryptTempFilePackageParts(originalEncryptSetting);
         }
     }
+
+    @Test
+    void testGetDimensionFromAnchor() throws IOException {
+        try (Workbook wb = 
_testDataProvider.openSampleWorkbook("picture-and-shape-same-size.xlsx")) {
+            Sheet sh = wb.getSheetAt(0);
+            Drawing<?> pat = sh.createDrawingPatriarch();
+
+            Shape shape = ((XSSFDrawing)pat).getShapes().get(0);
+            Dimension shapeDimension = 
ImageUtils.getDimensionFromAnchor((ClientAnchor) shape.getAnchor(), sh);
+            Picture picture = getPictureShape(pat, 1);
+            Dimension pictureDimension = 
ImageUtils.getDimensionFromAnchor(picture);
+
+            assertEquals(shapeDimension.getHeight(), 
pictureDimension.getHeight(), "the image height differs");
+            assertEquals(shapeDimension.getWidth(), 
pictureDimension.getWidth(), "the image width differs");
+        }
+    }
 }
diff --git a/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java 
b/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
index 7d4c0be5b8..418e4f990c 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
@@ -216,6 +216,26 @@ public final class ImageUtils {
         return new Dimension(w, h);
     }
 
+    /**
+     * Calculates the dimensions in EMUs for the given anchor in the context 
of the given sheet.
+     *
+     * @param anchor the anchor of which we want to calculate the dimensions.
+     * @param sheet the sheet where the anchor is inserted; it's required to 
obtain the cell width/height necessary to
+     *              calculate the dimensions of the anchor.
+     * @return the dimensions in EMUs
+     * @since 6.0.0
+     */
+    public static Dimension getDimensionFromAnchor(ClientAnchor anchor, Sheet 
sheet) {
+        boolean isHSSF = (anchor instanceof HSSFClientAnchor);
+
+        int w = getDimFromCell(0, anchor.getCol1(), anchor.getDx1(), 
anchor.getCol2(), anchor.getDx2(),
+                isHSSF ? WIDTH_UNITS : 0, sheet::getColumnWidthInPixels);
+
+        int h = getDimFromCell(0, anchor.getRow1(), anchor.getDy1(), 
anchor.getRow2(), anchor.getDy2(),
+                isHSSF ? HEIGHT_UNITS : 0, (row) -> 
getRowHeightInPixels(sheet, row));
+
+        return new Dimension(w, h);
+    }
 
     public static double getRowHeightInPixels(Sheet sheet, int rowNum) {
         Row r = sheet.getRow(rowNum);
diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestPicture.java 
b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestPicture.java
index 8502bbfcd1..778358c84e 100644
--- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestPicture.java
+++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestPicture.java
@@ -47,7 +47,7 @@ import org.junit.jupiter.api.Test;
 
 public abstract class BaseTestPicture {
 
-    private final ITestDataProvider _testDataProvider;
+    protected final ITestDataProvider _testDataProvider;
 
     protected BaseTestPicture(ITestDataProvider testDataProvider) {
         _testDataProvider = testDataProvider;
diff --git a/test-data/spreadsheet/picture-and-shape-same-size.xlsx 
b/test-data/spreadsheet/picture-and-shape-same-size.xlsx
new file mode 100644
index 0000000000..3a8270858d
Binary files /dev/null and 
b/test-data/spreadsheet/picture-and-shape-same-size.xlsx differ


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

Reply via email to