Skip to content

Commit 7b3e713

Browse files
GrimySoalAnhelinaM
authored andcommitted
Don't layout absolutely positioned wrappers in other absolutely positioned wrappers
1 parent 7919d66 commit 7b3e713

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,15 @@ public final class Property {
243243
public static final int TOP_CALCULATED = 162;
244244
public static final int LEFT_CALCULATED = 163;
245245
public static final int POSITIONED_ELEMENT_WRAPPED = 164;
246+
public static final int POSITIONED_ELEMENT_WRAPPER_LAYOUT = 165;
246247

247248
/**
248249
* Some properties must be passed to {@link IPropertyContainer} objects that
249250
* are lower in the document's hierarchy. Most inherited properties are
250251
* related to textual operations. Indicates whether this type of property is inheritable.
251252
*/
252253
private static final boolean[] INHERITED_PROPERTIES;
253-
private static final int MAX_INHERITED_PROPERTY_ID = 161;
254+
private static final int MAX_INHERITED_PROPERTY_ID = 165;
254255

255256
static {
256257
INHERITED_PROPERTIES = new boolean[MAX_INHERITED_PROPERTY_ID + 1];
@@ -301,6 +302,7 @@ public final class Property {
301302
INHERITED_PROPERTIES[Property.ADD_MARKED_CONTENT_TEXT] = true;
302303
INHERITED_PROPERTIES[Property.TREAT_AS_CONTINUOUS_CONTAINER] = true;
303304
INHERITED_PROPERTIES[Property.IGNORE_AREA_AND_SECTION_BREAKS] = true;
305+
INHERITED_PROPERTIES[Property.POSITIONED_ELEMENT_WRAPPER_LAYOUT] = true;
304306
}
305307

306308
private Property() {

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ public AbsolutelyPositionedRenderer(IRenderer wrappedRenderer, boolean verticalC
5757
@Override
5858
public LayoutResult layout(LayoutContext layoutContext) {
5959
LayoutContext copiedContext = copyContext(layoutContext);
60+
if (wrappedRenderer.hasProperty(Property.POSITIONED_ELEMENT_WRAPPER_LAYOUT)) {
61+
return dummyRenderer.layout(copiedContext);
62+
}
6063
Object positioning = wrappedRenderer.<Integer>getOwnProperty(Property.POSITION);
6164
wrappedRenderer.setProperty(Property.POSITION, LayoutPosition.STATIC);
6265
wrappedRenderer.setProperty(Property.FLOAT, FloatPropertyValue.NONE);
66+
wrappedRenderer.setProperty(Property.POSITIONED_ELEMENT_WRAPPER_LAYOUT, new Object());
6367
LayoutResult result = wrappedRenderer.layout(copiedContext);
6468
if (result.getStatus() == LayoutResult.NOTHING) {
6569
wrappedRenderer.setProperty(Property.FORCED_PLACEMENT, true);
@@ -71,6 +75,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
7175
} else {
7276
wrappedRenderer.setProperty(Property.POSITION, positioning);
7377
}
78+
wrappedRenderer.deleteOwnProperty(Property.POSITIONED_ELEMENT_WRAPPER_LAYOUT);
7479
if (verticalCoordinateMissing) {
7580
wrappedRenderer.setProperty(Property.TOP_CALCULATED, result.getOccupiedArea().getBBox().getTop());
7681
}
@@ -87,6 +92,26 @@ public IRenderer getWrappedRenderer() {
8792
return wrappedRenderer;
8893
}
8994

95+
@Override
96+
public <T1> T1 getProperty(int property, T1 defaultValue) {
97+
if (Property.POSITION == property) {
98+
// This absolutely positioned renderer wrapper is never supposed to be treated as absolutely positioned.
99+
// The whole idea of this wrapper is to calculate it's potential static coordinates.
100+
return (T1) (Object) LayoutPosition.STATIC;
101+
}
102+
return wrappedRenderer.<T1>getProperty(property, defaultValue);
103+
}
104+
105+
@Override
106+
public <T1> T1 getProperty(int property) {
107+
if (Property.POSITION == property) {
108+
// This absolutely positioned renderer wrapper is never supposed to be treated as absolutely positioned.
109+
// The whole idea of this wrapper is to calculate it's potential static coordinates.
110+
return (T1) (Object) LayoutPosition.STATIC;
111+
}
112+
return wrappedRenderer.<T1>getProperty(property);
113+
}
114+
90115
@Override
91116
public IRenderer getNextRenderer() {
92117
return new AbsolutelyPositionedRenderer(wrappedRenderer.getNextRenderer(),
@@ -118,26 +143,6 @@ public boolean hasOwnProperty(int property) {
118143
return wrappedRenderer.hasOwnProperty(property);
119144
}
120145

121-
@Override
122-
public <T1> T1 getProperty(int property) {
123-
if (Property.POSITION == property) {
124-
// This absolutely positioned renderer wrapper is never supposed to be treated as absolutely positioned.
125-
// The whole idea of this wrapper is to calculate it's potential static coordinates.
126-
return (T1) (Object) LayoutPosition.STATIC;
127-
}
128-
return wrappedRenderer.<T1>getProperty(property);
129-
}
130-
131-
@Override
132-
public <T1> T1 getProperty(int property, T1 defaultValue) {
133-
if (Property.POSITION == property) {
134-
// This absolutely positioned renderer wrapper is never supposed to be treated as absolutely positioned.
135-
// The whole idea of this wrapper is to calculate it's potential static coordinates.
136-
return (T1) (Object) LayoutPosition.STATIC;
137-
}
138-
return wrappedRenderer.<T1>getProperty(property, defaultValue);
139-
}
140-
141146
@Override
142147
public <T1> T1 getOwnProperty(int property) {
143148
return wrappedRenderer.<T1>getOwnProperty(property);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public void getPropertyTest() {
6767
Assertions.assertEquals(50, absolutelyPositionedRenderer.<Integer>getProperty(Property.LEFT));
6868
}
6969

70+
@Test
71+
public void getPropertyDefaultValueTest() {
72+
DivRenderer wrappedRenderer = new DivRenderer(new Div());
73+
AbsolutelyPositionedRenderer absolutelyPositionedRenderer = new AbsolutelyPositionedRenderer(wrappedRenderer, false, false);
74+
75+
Assertions.assertEquals(LayoutPosition.STATIC, absolutelyPositionedRenderer.<Integer>getProperty(Property.POSITION, LayoutPosition.FIXED));
76+
Assertions.assertEquals(50, absolutelyPositionedRenderer.<Integer>getProperty(Property.LEFT, 50));
77+
}
78+
7079
static class CustomRenderer extends DivRenderer {
7180
public int counter = 0;
7281

0 commit comments

Comments
 (0)