Bug 66377

Summary: The x coordinate value of the anchor point in the lower right corner of the image exceeds the width value of the cell
Product: POI Reporter: zhangjingyang <zhangjy>
Component: SS CommonAssignee: POI Developers List <dev>
Status: NEW ---    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: debug data

Description zhangjingyang 2022-12-05 08:27:12 UTC
Created attachment 38443 [details]
debug data

I'm trying to figure out how far an image is from the padding of the cell it's in. Currently I want to calculate the difference by the top left and bottom right anchor coordinates of the image and the width and height of the cell the image is in.

But I found a problem when calculating the x-coordinate of the bottom right corner of the image and the width of the cell.

When the lower right corner of the picture is close to the right side of the cell, the value of the x coordinate will be greater than the width of the cell, but in fact the whole picture is inside the cell.

For details, please refer to the contents of the attachment.

The following piece of code is the part that I think has a problem with the calculation.

Cariable dim(the cell width) is smaller than variable endD(the x-coordinate of the bottom right corner)

org.apache.poi.ss.util.ImageUtils

    private static int getDimFromCell(double imgSize, int startCell, int startD, int endCell, int endD, int hssfUnits,
         Function<Integer,Number> nextSize) {
        double targetSize;
        if (endCell < startCell) {
            targetSize = imgSize * EMU_PER_PIXEL;
        } else {
            targetSize = 0;
            for (int cellIdx = startCell; cellIdx<=endCell; cellIdx++) {
                final double dim = nextSize.apply(cellIdx).doubleValue() * EMU_PER_PIXEL;
                double leadSpace = 0;
                if (cellIdx == startCell) {
                    //space in the leftmost cell
                    leadSpace = (hssfUnits > 0)
                        ? dim * startD/(double)hssfUnits
                        : startD;
                }

                double trailSpace = 0;
                if (cellIdx == endCell) {
                    // space after the rightmost cell
                    trailSpace = (hssfUnits > 0)
                        ? dim * (hssfUnits-endD)/(double)hssfUnits
                        : dim - endD;
                }
                targetSize += dim - leadSpace - trailSpace;
            }
        }

        return (int)Math.rint(targetSize);
    }
}