Skip to content

Commit 451ef20

Browse files
committed
Add flex container element and flex related properties
DEVSIX-4996
1 parent 26287f1 commit 451ef20

16 files changed

+308
-360
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: Bruno Lowagie, Paulo Soares, et al.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.layout.element;
44+
45+
import com.itextpdf.layout.renderer.FlexContainerRenderer;
46+
import com.itextpdf.layout.renderer.IRenderer;
47+
48+
public class FlexContainer extends Div {
49+
50+
public FlexContainer() {
51+
super();
52+
}
53+
54+
@Override
55+
protected IRenderer makeNewRenderer() {
56+
return new FlexContainerRenderer(this);
57+
}
58+
}

layout/src/main/java/com/itextpdf/layout/property/Property.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class Property {
5454

5555
public static final int ACTION = 1;
5656
public static final int ALIGN_CONTENT = 130;
57+
public static final int ALIGN_ITEMS = 134;
5758
public static final int ALIGN_SELF = 129;
5859
public static final int APPEARANCE_STREAM_LAYOUT = 82;
5960
public static final int AREA_BREAK_TYPE = 2;
@@ -135,6 +136,7 @@ public final class Property {
135136
public static final int IGNORE_FOOTER = 96;
136137
public static final int IGNORE_HEADER = 97;
137138
public static final int ITALIC_SIMULATION = 31;
139+
public static final int JUSTIFY_CONTENT = 135;
138140
public static final int KEEP_TOGETHER = 32;
139141
public static final int KEEP_WITH_NEXT = 81;
140142
public static final int LEADING = 33;

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ This file is part of the iText (R) project.
5959
import java.util.Set;
6060

6161
public class FlexContainerRenderer extends DivRenderer {
62-
private static float FLEX_GROW_DEFAULT_VALUE = 0;
63-
private static float FLEX_SHRINK_DEFAULT_VALUE = 1;
6462

6563
private List<List<FlexItemInfo>> lines;
6664

@@ -86,21 +84,10 @@ public IRenderer getNextRenderer() {
8684
@Override
8785
public LayoutResult layout(LayoutContext layoutContext) {
8886
Rectangle layoutContextRectangle = layoutContext.getArea().getBBox();
89-
final List<FlexUtil.FlexItemCalculationInfo> flexItemInfos = new ArrayList<>();
90-
// TODO DEVSIX-4996 Change properties FLEX_GROW/FLEX_SHRINK/FLEX_BASIS on the combined one added in this ticket
9187
for (final IRenderer childRenderer : this.getChildRenderers()) {
9288
if (childRenderer instanceof AbstractRenderer) {
9389
final AbstractRenderer abstractChildRenderer = (AbstractRenderer) childRenderer;
9490
abstractChildRenderer.setParent(this);
95-
final UnitValue flexBasis = abstractChildRenderer.<UnitValue>getProperty(Property.WIDTH) == null ?
96-
UnitValue.createPointValue(abstractChildRenderer.getMinMaxWidth().getMinWidth()) :
97-
abstractChildRenderer.<UnitValue>getProperty(Property.WIDTH);
98-
flexItemInfos.add(new FlexUtil.FlexItemCalculationInfo(
99-
(AbstractRenderer) childRenderer,
100-
childRenderer.<UnitValue>getProperty(Property.FLEX_BASIS, flexBasis),
101-
(float) childRenderer.<Float>getProperty(Property.FLEX_GROW, FLEX_GROW_DEFAULT_VALUE),
102-
(float) childRenderer.<Float>getProperty(Property.FLEX_SHRINK, FLEX_SHRINK_DEFAULT_VALUE),
103-
layoutContextRectangle.getWidth()));
10491
}
10592
}
10693
Float containerWidth = retrieveWidth(layoutContextRectangle.getWidth());
@@ -112,7 +99,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
11299
containerHeight = layoutContextRectangle.getHeight();
113100
}
114101
lines = FlexUtil.calculateChildrenRectangles(
115-
new Rectangle((float) containerWidth, (float) containerHeight), this, flexItemInfos);
102+
new Rectangle((float) containerWidth, (float) containerHeight), this);
116103
final List<UnitValue> previousWidths = new ArrayList<>();
117104
for (final List<FlexItemInfo> line : lines) {
118105
for (final FlexItemInfo itemInfo : line) {
@@ -281,6 +268,7 @@ private void findMinMaxWidthIfCorrespondingPropertiesAreNotSet(MinMaxWidth minMa
281268
// TODO DEVSIX-5086 When flex-wrap will be fully supported we'll find min/max width with respect to the lines
282269
for (final IRenderer childRenderer : childRenderers) {
283270
MinMaxWidth childMinMaxWidth;
271+
childRenderer.setParent(this);
284272
if (childRenderer instanceof AbstractRenderer) {
285273
childMinMaxWidth = ((AbstractRenderer) childRenderer).getMinMaxWidth();
286274
} else {

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ This file is part of the iText (R) project.
5858
import java.util.List;
5959

6060
final class FlexUtil {
61+
6162
private static final float EPSILON = 0.00001f;
6263

64+
private static final Float FLEX_GROW_INITIAL_VALUE = 0F;
65+
66+
private static final Float FLEX_SHRINK_INITIAL_VALUE = 1F;
67+
6368
private FlexUtil() {
6469
// Do nothing
6570
}
@@ -73,11 +78,12 @@ private FlexUtil() {
7378
*
7479
* @param flexContainerBBox bounding box in which flex container should be rendered
7580
* @param flexContainerRenderer flex container's renderer
76-
* @param flexItemCalculationInfos list of flex item descriptions
7781
* @return list of lines
7882
*/
7983
public static List<List<FlexItemInfo>> calculateChildrenRectangles(Rectangle flexContainerBBox,
80-
FlexContainerRenderer flexContainerRenderer, List<FlexItemCalculationInfo> flexItemCalculationInfos) {
84+
FlexContainerRenderer flexContainerRenderer) {
85+
final List<FlexItemCalculationInfo> flexItemCalculationInfos = createFlexItemCalculationInfos(
86+
flexContainerRenderer, flexContainerBBox);
8187
Rectangle layoutBox = flexContainerBBox.clone();
8288
flexContainerRenderer.applyMarginsBordersPaddings(layoutBox, false);
8389

@@ -503,6 +509,38 @@ static boolean isZero(final float value) {
503509
return Math.abs(value) < EPSILON;
504510
}
505511

512+
private static List<FlexItemCalculationInfo> createFlexItemCalculationInfos(
513+
FlexContainerRenderer flexContainerRenderer, Rectangle flexContainerBBox) {
514+
final List<IRenderer> childRenderers = flexContainerRenderer.getChildRenderers();
515+
final List<FlexItemCalculationInfo> flexItems = new ArrayList<>();
516+
for (final IRenderer renderer : childRenderers) {
517+
if (renderer instanceof AbstractRenderer) {
518+
Float flexGrow = renderer.<Float>getProperty(Property.FLEX_GROW);
519+
if (flexGrow == null) {
520+
flexGrow = FLEX_GROW_INITIAL_VALUE;
521+
}
522+
Float flexShrink = renderer.<Float>getProperty(Property.FLEX_SHRINK);
523+
if (flexShrink == null) {
524+
flexShrink = FLEX_SHRINK_INITIAL_VALUE;
525+
}
526+
527+
UnitValue flexBasis = renderer.<UnitValue>getProperty(Property.FLEX_BASIS);
528+
if (flexBasis == null) {
529+
// TODO DEVSIX-5091 improve determining of the flex base size when flex-basis: content
530+
flexBasis = UnitValue.createPointValue(
531+
((AbstractRenderer) renderer).getMinMaxWidth().getMaxWidth());
532+
}
533+
534+
final FlexItemCalculationInfo flexItemInfo = new FlexItemCalculationInfo(
535+
(AbstractRenderer) renderer, flexBasis, (float) flexGrow, (float) flexShrink,
536+
flexContainerBBox.getWidth());
537+
538+
flexItems.add(flexItemInfo);
539+
}
540+
}
541+
return flexItems;
542+
}
543+
506544
static class FlexItemCalculationInfo {
507545
AbstractRenderer renderer;
508546
UnitValue flexBasis;
@@ -524,8 +562,8 @@ static class FlexItemCalculationInfo {
524562
float hypotheticalMainSize;
525563
float hypotheticalCrossSize;
526564

527-
public FlexItemCalculationInfo(AbstractRenderer renderer, UnitValue flexBasis, float flexGrow, float flexShrink,
528-
float areaWidth) {
565+
public FlexItemCalculationInfo(AbstractRenderer renderer, UnitValue flexBasis,
566+
float flexGrow, float flexShrink, float areaWidth) {
529567
this.renderer = renderer;
530568
if (null == flexBasis) {
531569
throw new IllegalArgumentException(LayoutExceptionMessageConstant.FLEX_BASIS_CANNOT_BE_NULL);

0 commit comments

Comments
 (0)