Bug 66377 - The x coordinate value of the anchor point in the lower right corner of the image exceeds the width value of the cell
Summary: The x coordinate value of the anchor point in the lower right corner of the i...
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: SS Common (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-12-05 08:27 UTC by zhangjingyang
Modified: 2022-12-26 09:36 UTC (History)
0 users



Attachments
debug data (791.92 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2022-12-05 08:27 UTC, zhangjingyang
Details

Note You need to log in before you can comment on or make changes to this bug.
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);
    }
}