Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -175,4 +176,20 @@ private void multiRelationships(boolean tempFileParts, boolean encrypt) throws I
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");
}
}
}
20 changes: 20 additions & 0 deletions poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@ public static Dimension getDimensionFromAnchor(Picture picture) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more

public abstract class BaseTestPicture {

private final ITestDataProvider _testDataProvider;
protected final ITestDataProvider _testDataProvider;

protected BaseTestPicture(ITestDataProvider testDataProvider) {
_testDataProvider = testDataProvider;
Expand Down
Binary file not shown.