Skip to content

Commit 0abc56a

Browse files
committed
Fix precision issue occured while image processing. Add a test.
DEVSIX-1817
1 parent 3413140 commit 0abc56a

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/ImageRenderer.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,20 @@ private float adjustPositionAfterRotation(float angle, float maxWidth, float max
492492
// So let's find scaling coefficient
493493
float scaleCoeff = 1;
494494
if (Boolean.TRUE.equals(getPropertyAsBoolean(Property.AUTO_SCALE))) {
495-
scaleCoeff = Math.min(maxWidth / (float) width, maxHeight / (float) height);
496-
height *= scaleCoeff;
497-
width *= scaleCoeff;
498-
} else if (null != getPropertyAsBoolean(Property.AUTO_SCALE_WIDTH) && (boolean) getPropertyAsBoolean(Property.AUTO_SCALE_WIDTH)) {
495+
if (maxWidth / (float) width < maxHeight / (float) height) {
496+
scaleCoeff = maxWidth / (float) width;
497+
height *= maxWidth / (float) width;
498+
width = maxWidth;
499+
} else {
500+
scaleCoeff = maxHeight / (float) height;
501+
width *= maxHeight / (float) height;
502+
height = maxHeight;
503+
}
504+
} else if (Boolean.TRUE.equals(getPropertyAsBoolean(Property.AUTO_SCALE_WIDTH))) {
499505
scaleCoeff = maxWidth / (float) width;
500506
height *= scaleCoeff;
501507
width = maxWidth;
502-
} else if (null != getPropertyAsBoolean(Property.AUTO_SCALE_HEIGHT) && (boolean) getPropertyAsBoolean(Property.AUTO_SCALE_HEIGHT)) {
508+
} else if (Boolean.TRUE.equals(getPropertyAsBoolean(Property.AUTO_SCALE_HEIGHT))) {
503509
scaleCoeff = maxHeight / (float) height;
504510
height = maxHeight;
505511
width *= scaleCoeff;

layout/src/test/java/com/itextpdf/layout/ImageTest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,33 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.io.util.UrlUtil;
4848
import com.itextpdf.kernel.color.ColorConstants;
4949
import com.itextpdf.kernel.geom.PageSize;
50+
import com.itextpdf.kernel.geom.Rectangle;
5051
import com.itextpdf.kernel.pdf.PdfDocument;
52+
import com.itextpdf.kernel.pdf.PdfPage;
5153
import com.itextpdf.kernel.pdf.PdfWriter;
54+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
5255
import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
5356
import com.itextpdf.kernel.utils.CompareTool;
5457
import com.itextpdf.layout.border.SolidBorder;
55-
import com.itextpdf.layout.element.*;
58+
import com.itextpdf.layout.element.AreaBreak;
59+
import com.itextpdf.layout.element.Cell;
60+
import com.itextpdf.layout.element.Div;
61+
import com.itextpdf.layout.element.Image;
62+
import com.itextpdf.layout.element.List;
63+
import com.itextpdf.layout.element.Paragraph;
64+
import com.itextpdf.layout.element.Table;
65+
import com.itextpdf.layout.element.Text;
5666
import com.itextpdf.layout.property.HorizontalAlignment;
5767
import com.itextpdf.layout.property.Property;
68+
import com.itextpdf.layout.property.TextAlignment;
5869
import com.itextpdf.layout.property.UnitValue;
70+
import com.itextpdf.layout.property.VerticalAlignment;
5971
import com.itextpdf.test.ExtendedITextTest;
6072
import com.itextpdf.test.annotations.LogMessage;
6173
import com.itextpdf.test.annotations.LogMessages;
6274
import com.itextpdf.test.annotations.type.IntegrationTest;
6375
import org.junit.Assert;
6476
import org.junit.BeforeClass;
65-
import org.junit.Ignore;
6677
import org.junit.Test;
6778
import org.junit.experimental.categories.Category;
6879

@@ -556,13 +567,13 @@ public void imageTest21() throws IOException, InterruptedException {
556567

557568
Image image = new Image(ImageDataFactory.create(sourceFolder + "Desert.jpg"));
558569
image.setAutoScaleHeight(true);
559-
float[] colWidths = {1f,1f};
570+
float[] colWidths = {1f, 1f};
560571

561572
Table container = new Table(UnitValue.createPercentArray(colWidths));
562573
container.addCell("Text");
563574
container.addCell("autoscaling image, height only");
564575

565-
int textIterations =50;
576+
int textIterations = 50;
566577
Paragraph p = new Paragraph();
567578
for (int i = 0; i < textIterations; i++) {
568579
p.add("Text will wrap");
@@ -841,4 +852,35 @@ public void imageWithMinMaxHeightTest01() throws IOException, InterruptedExcepti
841852

842853
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
843854
}
855+
856+
@Test
857+
public void precisionTest01() throws IOException, InterruptedException {
858+
String outFileName = destinationFolder + "precisionTest01.pdf";
859+
String cmpFileName = sourceFolder + "cmp_precisionTest01.pdf";
860+
String imageFileName = sourceFolder + "LOGO_PDF_77.jpg";
861+
862+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
863+
PdfPage page = pdfDoc.addNewPage();
864+
PdfCanvas currentPdfCanvas = new PdfCanvas(page);
865+
866+
Rectangle rc = new Rectangle(56.6929131f, 649.13385f, 481.889771f, 136.062988f);
867+
Canvas canvas = new Canvas(currentPdfCanvas, pdfDoc, rc);
868+
869+
Table table = new Table(UnitValue.createPointArray(new float[]{158f}));
870+
table.setTextAlignment(TextAlignment.LEFT);
871+
872+
Image logoImage = new Image(ImageDataFactory.create(imageFileName));
873+
Paragraph p = new Paragraph().add(logoImage.setAutoScale(true));
874+
875+
Cell cell = new Cell();
876+
cell.setKeepTogether(true);
877+
cell.add(p);
878+
879+
table.addCell(cell.setHeight(85.03937f).setVerticalAlignment(VerticalAlignment.TOP).setPadding(0));
880+
canvas.add(table);
881+
882+
pdfDoc.close();
883+
884+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
885+
}
844886
}
5.05 KB
Loading
Binary file not shown.

0 commit comments

Comments
 (0)