You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/08/20 15:39:38 UTC

(poi) branch trunk updated: Fix getDimFromCell trailing space for XSSF in-cell picture anchors (#1211)

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 003ef4b8ad Fix getDimFromCell trailing space for XSSF in-cell picture anchors (#1211)
003ef4b8ad is described below

commit 003ef4b8add74e9c8bce57a68d86356cee2cd466
Author: dev_Hakaze <af...@gmail.com>
AuthorDate: Thu Aug 20 22:39:32 2026 +0700

    Fix getDimFromCell trailing space for XSSF in-cell picture anchors (#1211)
    
    * Fix degenerate XSSF in-cell anchor dimensions for resize
    
    Revert the global XSSF trailSpace change (endD was wrong for multi-cell
    anchors). Handle only the degenerate same-cell case where endD - startD
    is zero or negative: use full cell size when both offsets are 0, otherwise
    remaining cell space (dim - leadSpace).
    
    Fixes #1201
    
    * Use XSSFITestDataProvider in TestXSSFPicture degenerate anchor test
    
    Signed-off-by: arimu1 <19...@users.noreply.github.com>
    
    ---------
    
    Signed-off-by: arimu1 <19...@users.noreply.github.com>
    Co-authored-by: arimu1 <19...@users.noreply.github.com>
---
 .../apache/poi/xssf/usermodel/TestXSSFPicture.java | 69 ++++++++++++++++++++++
 .../java/org/apache/poi/ss/util/ImageUtils.java    |  8 +++
 2 files changed, 77 insertions(+)

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 dae54b4773..f35a589cfd 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
@@ -17,10 +17,13 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import static org.apache.poi.util.Units.EMU_PER_PIXEL;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.awt.Dimension;
 import java.io.IOException;
@@ -177,6 +180,72 @@ public final class TestXSSFPicture extends BaseTestPicture {
         }
     }
 
+    /**
+     * Degenerate in-cell XSSF anchors (col1 == col2, dx1 == dx2 == 0) must report the
+     * full cell size so picture.resize() has a positive base dimension. See #1201.
+     */
+    @Test
+    void testGetDimensionFromAnchorDegenerateInCell() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet sheet = wb.createSheet();
+            byte[] png = XSSFITestDataProvider.instance.getTestDataFileContent("45829.png");
+            int idx = wb.addPicture(png, Workbook.PICTURE_TYPE_PNG);
+
+            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 1, 1);
+            XSSFDrawing drawing = sheet.createDrawingPatriarch();
+            XSSFPicture picture = drawing.createPicture(anchor, idx);
+
+            int expectedWidth = Math.round(sheet.getColumnWidthInPixels(1) * EMU_PER_PIXEL);
+            int expectedHeight = (int) Math.round(ImageUtils.getRowHeightInPixels(sheet, 1) * EMU_PER_PIXEL);
+
+            Dimension dim = ImageUtils.getDimensionFromAnchor(picture);
+            assertEquals(expectedWidth, dim.getWidth(), "degenerate in-cell width should equal cell width");
+            assertEquals(expectedHeight, dim.getHeight(), "degenerate in-cell height should equal cell height");
+
+            picture.resize(1.0, 1.0);
+            Dimension afterResize = ImageUtils.getDimensionFromAnchor(picture);
+            assertTrue(afterResize.getWidth() > 0, "width should stay positive after resize");
+            assertTrue(afterResize.getHeight() > 0, "height should stay positive after resize");
+        }
+    }
+
+    /**
+     * Normal in-cell XSSF anchors with startD &lt; endD must report endD - startD.
+     */
+    @Test
+    void testGetDimensionFromAnchorInCellWithOffsets() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet sheet = wb.createSheet();
+            int startD = 100_000;
+            int endD = 350_000;
+            XSSFClientAnchor anchor = new XSSFClientAnchor(startD, startD, endD, endD, 1, 1, 1, 1);
+
+            Dimension dim = ImageUtils.getDimensionFromAnchor(anchor, sheet);
+            assertEquals(endD - startD, dim.getWidth(), "in-cell width should be endD - startD");
+            assertEquals(endD - startD, dim.getHeight(), "in-cell height should be endD - startD");
+        }
+    }
+
+    /**
+     * Multi-cell XSSF anchors with dx2/dy2 == 0 must not include a full extra cell.
+     */
+    @Test
+    void testGetDimensionFromAnchorMultiCellZeroEndOffset() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet sheet = wb.createSheet();
+            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, 1, 2, 1);
+
+            Dimension dim = ImageUtils.getDimensionFromAnchor(anchor, sheet);
+            int col1Emu = Math.round(sheet.getColumnWidthInPixels(1) * EMU_PER_PIXEL);
+            int col2Emu = Math.round(sheet.getColumnWidthInPixels(2) * EMU_PER_PIXEL);
+
+            assertEquals(col1Emu, dim.getWidth(),
+                "width must equal first column only when dx2 == 0 on second column");
+            assertNotEquals(col1Emu + col2Emu, dim.getWidth(),
+                "width must not include full second column when dx2 == 0");
+        }
+    }
+
     @Test
     void testGetDimensionFromAnchor() throws IOException {
         try (Workbook wb = _testDataProvider.openSampleWorkbook("picture-and-shape-same-size.xlsx")) {
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 91fffdf6d4..bd86cb49ca 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
@@ -316,6 +316,14 @@ public final class ImageUtils {
                 }
                 targetSize += dim - leadSpace - trailSpace;
             }
+
+            // Degenerate same-cell XSSF anchor (typically dx1=dy1=dx2=dy2=0): endD - startD
+            // is zero or negative. Use remaining cell space so resize has a positive base size.
+            if (hssfUnits == 0 && startCell == endCell && targetSize <= 0) {
+                final double dim = nextSize.apply(startCell).doubleValue() * EMU_PER_PIXEL;
+                final double leadSpace = startD;
+                targetSize = (startD == 0 && endD == 0) ? dim : dim - leadSpace;
+            }
         }
 
         return MathUtil.safeDoubleToInt(Math.rint(targetSize));


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org