Skip to content

Commit 8c590a3

Browse files
author
Eugene Bochilo
committed
Introduce support of intrinsic aspect ratio in flex algorithm
DEVSIX-5004
1 parent 458abfd commit 8c590a3

File tree

258 files changed

+388
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

258 files changed

+388
-37
lines changed

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public final class LogMessageConstant {
110110
public static final String FAILED_TO_PROCESS_A_TRANSFORMATION_MATRIX = "Failed to process a transformation matrix which is noninvertible. Some content may be placed not as expected.";
111111
public static final String FIELD_VALUE_IS_NOT_CONTAINED_IN_OPT_ARRAY = "Value \"{0}\" is not contained in /Opt array of field \"{1}\".";
112112
public static final String FILE_CHANNEL_CLOSING_FAILED = "Closing of the file channel this source is based on failed.";
113+
public static final String FLEX_ITEM_LAYOUT_RESULT_IS_NOT_FULL =
114+
"Flex item layout result isn't full, but it must be. The cross size of the flex item will be 0.";
113115
public static final String FLUSHED_OBJECT_CONTAINS_FREE_REFERENCE = "Flushed object contains indirect reference which is free. Null object will be written instead.";
114116
public static final String FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT = "Flushed object contains indirect reference which doesn't refer to any other object. Null object will be written instead.";
115117
public static final String FONT_DICTIONARY_WITH_NO_FONT_DESCRIPTOR = "Font dictionary does not contain required /FontDescriptor entry.";

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,55 @@ protected static boolean isOverflowFit(OverflowPropertyValue rendererOverflowPro
13081308
return rendererOverflowProperty == null || OverflowPropertyValue.FIT.equals(rendererOverflowProperty);
13091309
}
13101310

1311+
/**
1312+
* Replaces given property own value with the given value.
1313+
*
1314+
* @param property the property to be replaced
1315+
* @param replacementValue the value with which property will be replaced
1316+
* @param <T> the type associated with the property
1317+
* @return previous property value
1318+
*/
1319+
<T> T replaceOwnProperty(int property, T replacementValue) {
1320+
T ownProperty = this.<T>getOwnProperty(property);
1321+
setProperty(property, replacementValue);
1322+
return ownProperty;
1323+
}
1324+
1325+
/**
1326+
* Returns back own value of the given property.
1327+
*
1328+
* @param property the property to be returned back
1329+
* @param prevValue the value which will be returned back
1330+
* @param <T> the type associated with the property
1331+
*/
1332+
<T> void returnBackOwnProperty(int property, T prevValue) {
1333+
if (prevValue == null) {
1334+
deleteOwnProperty(property);
1335+
} else {
1336+
setProperty(property, prevValue);
1337+
}
1338+
}
1339+
1340+
/**
1341+
* Checks if this renderer has intrinsic aspect ratio.
1342+
*
1343+
* @return true, if aspect ratio is defined for this renderer, false otherwise
1344+
*/
1345+
boolean hasAspectRatio() {
1346+
// TODO DEVSIX-5255 This method should be changed after we support aspect-ratio property
1347+
return false;
1348+
}
1349+
1350+
/**
1351+
* Gets intrinsic aspect ratio for this renderer.
1352+
*
1353+
* @return aspect ratio, if it is defined for this renderer, null otherwise
1354+
*/
1355+
Float getAspectRatio() {
1356+
// TODO DEVSIX-5255 This method should be changed after we support aspect-ratio property
1357+
return null;
1358+
}
1359+
13111360
static void processWaitingDrawing(IRenderer child, Transform transformProp, List<IRenderer> waitingDrawing) {
13121361
if (FloatingHelper.isRendererFloating(child) || transformProp != null) {
13131362
waitingDrawing.add(child);

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

Lines changed: 153 additions & 27 deletions
Large diffs are not rendered by default.

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public class ImageRenderer extends AbstractRenderer implements ILeafElementRende
101101
*/
102102
public ImageRenderer(Image image) {
103103
super(image);
104+
imageWidth = image.getImageWidth();
105+
imageHeight = image.getImageHeight();
104106
}
105107

106108
@Override
@@ -111,8 +113,6 @@ public LayoutResult layout(LayoutContext layoutContext) {
111113
AffineTransform t = new AffineTransform();
112114
Image modelElement = (Image) (getModelElement());
113115
PdfXObject xObject = modelElement.getXObject();
114-
imageWidth = modelElement.getImageWidth();
115-
imageHeight = modelElement.getImageHeight();
116116

117117
calculateImageDimensions(layoutBox, t, xObject);
118118

@@ -390,6 +390,40 @@ public Rectangle getBorderAreaBBox() {
390390
return initialOccupiedAreaBBox;
391391
}
392392

393+
/**
394+
* {@inheritDoc}
395+
*/
396+
@Override
397+
boolean hasAspectRatio() {
398+
return true;
399+
}
400+
401+
/**
402+
* {@inheritDoc}
403+
*/
404+
@Override
405+
Float getAspectRatio() {
406+
return imageWidth / imageHeight;
407+
}
408+
409+
/**
410+
* Gets original width of the image, not the width set by {@link Image#setWidth} method.
411+
*
412+
* @return original image width
413+
*/
414+
public float getImageWidth() {
415+
return imageWidth;
416+
}
417+
418+
/**
419+
* Gets original height of the image, not the height set by {@link Image#setHeight} method.
420+
*
421+
* @return original image height
422+
*/
423+
public float getImageHeight() {
424+
return imageHeight;
425+
}
426+
393427
@Override
394428
protected Rectangle applyPaddings(Rectangle rect, UnitValue[] paddings, boolean reverse) {
395429
return rect;

layout/src/test/java/com/itextpdf/layout/element/FlexContainerTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,9 @@ public FlexContainerTest(Object alignItemsValue, Object justifyContentValue, Obj
8080
public static Iterable<Object[]> alignItemsAndJustifyContentProperties() {
8181
return Arrays.asList(new Object[][]{
8282
{AlignmentPropertyValue.FLEX_START, JustifyContent.FLEX_START, 1},
83-
{AlignmentPropertyValue.START, JustifyContent.START, 2},
84-
{AlignmentPropertyValue.SELF_START, JustifyContent.SELF_START, 3},
85-
{AlignmentPropertyValue.BASELINE, JustifyContent.LEFT, 4},
86-
{AlignmentPropertyValue.FLEX_END, JustifyContent.FLEX_END, 5},
87-
{AlignmentPropertyValue.END, JustifyContent.END, 6},
88-
{AlignmentPropertyValue.SELF_END, JustifyContent.RIGHT, 7},
89-
{AlignmentPropertyValue.CENTER, JustifyContent.CENTER, 8},
90-
{AlignmentPropertyValue.STRETCH, JustifyContent.CENTER, 9}
83+
{AlignmentPropertyValue.FLEX_END, JustifyContent.FLEX_END, 2},
84+
{AlignmentPropertyValue.CENTER, JustifyContent.CENTER, 3},
85+
{AlignmentPropertyValue.STRETCH, JustifyContent.CENTER, 4}
9186
});
9287
}
9388

layout/src/test/java/com/itextpdf/layout/renderer/FlexUtilTest.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This file is part of the iText (R) project.
2929
import com.itextpdf.kernel.geom.Rectangle;
3030
import com.itextpdf.kernel.pdf.PdfDocument;
3131
import com.itextpdf.kernel.pdf.PdfWriter;
32+
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
3233
import com.itextpdf.layout.Document;
3334
import com.itextpdf.layout.Style;
3435
import com.itextpdf.layout.borders.SolidBorder;
@@ -2266,6 +2267,150 @@ public void differentBasisPercentSumGtWidthGrow1Shrink0Test01() {
22662267
Assert.assertEquals(200f, rectangleTable.get(0).get(2).getRectangle().getWidth(), EPS);
22672268
}
22682269

2270+
@Test
2271+
public void calculateMinContentWithMinWidthTest() {
2272+
DivRenderer divRenderer = new DivRenderer(new Div());
2273+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(100));
2274+
divRenderer.setProperty(Property.MIN_WIDTH, UnitValue.createPointValue(30));
2275+
2276+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo(divRenderer);
2277+
Assert.assertEquals(30f, info.minContent, EPS);
2278+
2279+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(30));
2280+
divRenderer.setProperty(Property.MIN_WIDTH, UnitValue.createPointValue(100));
2281+
2282+
info = createFlexItemCalculationInfo(divRenderer);
2283+
Assert.assertEquals(100f, info.minContent, EPS);
2284+
}
2285+
2286+
@Test
2287+
public void calculateMinContentForDivWithContentTest() {
2288+
Div div = new Div();
2289+
div.add(new Div().setWidth(50));
2290+
IRenderer divRenderer = div.createRendererSubTree();
2291+
2292+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) divRenderer);
2293+
Assert.assertEquals(50.0f, info.minContent, EPS);
2294+
}
2295+
2296+
@Test
2297+
public void calculateMinContentForDivWithWidthTest() {
2298+
DivRenderer divRenderer = new DivRenderer(new Div());
2299+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(100));
2300+
2301+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo(divRenderer);
2302+
Assert.assertEquals(0.0f, info.minContent, EPS);
2303+
}
2304+
2305+
@Test
2306+
public void calculateMinContentForDivWithWidthAndContentTest() {
2307+
Div div = new Div();
2308+
div.add(new Div().setWidth(50));
2309+
IRenderer divRenderer = div.createRendererSubTree();
2310+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(100));
2311+
2312+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) divRenderer);
2313+
Assert.assertEquals(50.0f, info.minContent, EPS);
2314+
2315+
div = new Div();
2316+
div.add(new Div().setWidth(150));
2317+
divRenderer = div.createRendererSubTree();
2318+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(100));
2319+
2320+
info = createFlexItemCalculationInfo((AbstractRenderer) divRenderer);
2321+
Assert.assertEquals(100.0f, info.minContent, EPS);
2322+
}
2323+
2324+
@Test
2325+
public void calculateMinContentForDivWithWidthMaxWidthAndContentTest() {
2326+
Div div = new Div();
2327+
div.add(new Div().setWidth(50));
2328+
div.setProperty(Property.MAX_WIDTH, UnitValue.createPointValue(45));
2329+
IRenderer divRenderer = div.createRendererSubTree();
2330+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(100));
2331+
2332+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) divRenderer);
2333+
Assert.assertEquals(45.0f, info.minContent, EPS);
2334+
2335+
div = new Div();
2336+
div.add(new Div().setWidth(150));
2337+
div.setProperty(Property.MAX_WIDTH, UnitValue.createPointValue(120));
2338+
divRenderer = div.createRendererSubTree();
2339+
divRenderer.setProperty(Property.WIDTH, UnitValue.createPointValue(100));
2340+
2341+
info = createFlexItemCalculationInfo((AbstractRenderer) divRenderer);
2342+
Assert.assertEquals(100.0f, info.minContent, EPS);
2343+
}
2344+
2345+
@Test
2346+
public void calculateMinContentForImageTest() {
2347+
Image image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2348+
IRenderer imageRenderer = image.createRendererSubTree();
2349+
2350+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2351+
Assert.assertEquals(60.0f, info.minContent, EPS);
2352+
}
2353+
2354+
@Test
2355+
public void calculateMinContentForImageWithHeightTest() {
2356+
Image image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2357+
image.setHeight(300);
2358+
IRenderer imageRenderer = image.createRendererSubTree();
2359+
2360+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2361+
Assert.assertEquals(60.0f, info.minContent, EPS);
2362+
2363+
image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2364+
image.setHeight(100);
2365+
imageRenderer = image.createRendererSubTree();
2366+
2367+
info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2368+
Assert.assertEquals(40.0f, info.minContent, EPS);
2369+
}
2370+
2371+
@Test
2372+
public void calculateMinContentForImageWithHeightAndMinMaxHeightsTest() {
2373+
Image image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2374+
image.setHeight(300);
2375+
image.setMinHeight(20);
2376+
image.setMaxHeight(100);
2377+
IRenderer imageRenderer = image.createRendererSubTree();
2378+
2379+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2380+
Assert.assertEquals(40.0f, info.minContent, EPS);
2381+
2382+
image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2383+
image.setHeight(100);
2384+
image.setMinHeight(20);
2385+
image.setMaxHeight(75);
2386+
imageRenderer = image.createRendererSubTree();
2387+
2388+
info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2389+
Assert.assertEquals(30.0f, info.minContent, EPS);
2390+
}
2391+
2392+
@Test
2393+
public void calculateMinContentForImageWithHeightAndWidthTest() {
2394+
Image image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2395+
image.setHeight(50);
2396+
image.setWidth(100);
2397+
IRenderer imageRenderer = image.createRendererSubTree();
2398+
2399+
FlexUtil.FlexItemCalculationInfo info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2400+
Assert.assertEquals(60.0f, info.minContent, EPS);
2401+
2402+
image = new Image(new PdfFormXObject(new Rectangle(60, 150)));
2403+
image.setHeight(50);
2404+
image.setWidth(50);
2405+
imageRenderer = image.createRendererSubTree();
2406+
2407+
info = createFlexItemCalculationInfo((AbstractRenderer) imageRenderer);
2408+
Assert.assertEquals(50.0f, info.minContent, EPS);
2409+
}
2410+
2411+
private static FlexUtil.FlexItemCalculationInfo createFlexItemCalculationInfo(AbstractRenderer renderer) {
2412+
return new FlexUtil.FlexItemCalculationInfo(renderer, 0, 0, 0, 0, false);
2413+
}
22692414

22702415
private static List<List<FlexItemInfo>> testFlex(List<UnitValue> flexBasisValues, List<Float> flexGrowValues,
22712416
List<Float> flexShrinkValues) {

0 commit comments

Comments
 (0)