@@ -23,7 +23,6 @@ This file is part of the iText (R) project.
23
23
package com .itextpdf .layout .renderer ;
24
24
25
25
import com .itextpdf .kernel .geom .Rectangle ;
26
- import com .itextpdf .layout .borders .Border ;
27
26
import com .itextpdf .layout .element .GridContainer ;
28
27
import com .itextpdf .layout .layout .LayoutArea ;
29
28
import com .itextpdf .layout .layout .LayoutContext ;
@@ -45,6 +44,7 @@ This file is part of the iText (R) project.
45
44
public class GridContainerRenderer extends BlockRenderer {
46
45
private boolean isFirstLayout = true ;
47
46
private float containerHeight = 0.0f ;
47
+ private float containerWidth = 0.0f ;
48
48
/**
49
49
* Creates a Grid renderer from its corresponding layout object.
50
50
* @param modelElement the {@link GridContainer} which this object should manage
@@ -72,14 +72,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
72
72
73
73
Rectangle actualBBox = layoutContext .getArea ().getBBox ().clone ();
74
74
Float blockWidth = retrieveWidth (actualBBox .getWidth ());
75
- if (blockWidth != null ) {
76
- actualBBox .setWidth ((float ) blockWidth );
77
- }
78
75
79
76
ContinuousContainer .setupContinuousContainerIfNeeded (this );
80
77
applyPaddings (actualBBox , false );
81
78
applyBorderBox (actualBBox , false );
82
79
applyMargins (actualBBox , false );
80
+ applyWidth (actualBBox , blockWidth , OverflowPropertyValue .VISIBLE );
81
+ containerWidth = actualBBox .getWidth ();
83
82
84
83
Float blockHeight = retrieveHeight ();
85
84
if (blockHeight != null && (float ) blockHeight < actualBBox .getHeight ()) {
@@ -100,7 +99,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
100
99
} else {
101
100
this .occupiedArea = calculateContainerOccupiedArea (layoutContext , false );
102
101
return new LayoutResult (LayoutResult .PARTIAL , this .occupiedArea ,
103
- createSplitRenderer (layoutResult .getSplitRenderers ()),
102
+ GridMulticolUtil . createSplitRenderer (layoutResult .getSplitRenderers (), this ),
104
103
createOverflowRenderer (layoutResult .getOverflowRenderers ()));
105
104
}
106
105
}
@@ -141,18 +140,6 @@ private static Boolean determineCollapsingMargins(IRenderer renderer) {
141
140
return Boolean .FALSE ;
142
141
}
143
142
144
- private AbstractRenderer createSplitRenderer (List <IRenderer > children ) {
145
- AbstractRenderer splitRenderer = (AbstractRenderer ) getNextRenderer ();
146
- splitRenderer .parent = parent ;
147
- splitRenderer .modelElement = modelElement ;
148
- splitRenderer .occupiedArea = occupiedArea ;
149
- splitRenderer .isLastRendererForModelElement = false ;
150
- splitRenderer .setChildRenderers (children );
151
- splitRenderer .addAllProperties (getOwnProperties ());
152
- ContinuousContainer .setupContinuousContainerIfNeeded (splitRenderer );
153
- return splitRenderer ;
154
- }
155
-
156
143
private AbstractRenderer createOverflowRenderer (List <IRenderer > children ) {
157
144
GridContainerRenderer overflowRenderer = (GridContainerRenderer ) getNextRenderer ();
158
145
overflowRenderer .isFirstLayout = false ;
@@ -273,67 +260,25 @@ private static LayoutContext getCellLayoutContext(LayoutContext layoutContext, R
273
260
// Calculate grid container occupied area based on its width/height properties and cell layout areas
274
261
private LayoutArea calculateContainerOccupiedArea (LayoutContext layoutContext , boolean isFull ) {
275
262
LayoutArea area = layoutContext .getArea ().clone ();
276
- final float totalHeight = updateOccupiedHeight (containerHeight , isFull );
277
- if (totalHeight < area .getBBox ().getHeight () || isFull ) {
278
- area .getBBox ().setHeight (totalHeight );
279
- final Rectangle initialBBox = layoutContext .getArea ().getBBox ();
280
- area .getBBox ().setY (initialBBox .getY () + initialBBox .getHeight () - area .getBBox ().getHeight ());
281
- recalculateHeightAndWidthAfterLayout (area .getBBox (), isFull );
282
- }
283
- return area ;
284
- }
285
-
286
- // Recalculate height/width after grid sizing and re-apply height/width properties
287
- private void recalculateHeightAndWidthAfterLayout (Rectangle bBox , boolean isFull ) {
288
- Float height = retrieveHeight ();
289
- if (height != null ) {
290
- height = updateOccupiedHeight ((float ) height , isFull );
291
- float heightDelta = bBox .getHeight () - (float ) height ;
292
- bBox .moveUp (heightDelta );
293
- bBox .setHeight ((float ) height );
294
- }
295
- Float blockWidth = retrieveWidth (bBox .getWidth ());
296
- if (blockWidth != null ) {
297
- bBox .setWidth ((float ) blockWidth );
298
- }
299
- }
263
+ final Rectangle areaBBox = area .getBBox ();
300
264
301
- private float updateOccupiedHeight (float initialHeight , boolean isFull ) {
302
- if (isFull ) {
303
- initialHeight += safelyRetrieveFloatProperty (Property .PADDING_BOTTOM );
304
- initialHeight += safelyRetrieveFloatProperty (Property .MARGIN_BOTTOM );
305
- if (!this .hasOwnProperty (Property .BORDER ) || this .<Border >getProperty (Property .BORDER ) == null ) {
306
- initialHeight += safelyRetrieveFloatProperty (Property .BORDER_BOTTOM );
265
+ final float totalContainerHeight = GridMulticolUtil .updateOccupiedHeight (containerHeight , isFull , isFirstLayout , this );
266
+ if (totalContainerHeight < areaBBox .getHeight () || isFull ) {
267
+ Float height = retrieveHeight ();
268
+ if (height == null ) {
269
+ areaBBox .setHeight (totalContainerHeight );
270
+ } else {
271
+ height = GridMulticolUtil .updateOccupiedHeight ((float ) height , isFull , isFirstLayout , this );
272
+ areaBBox .setHeight ((float ) height );
307
273
}
308
274
}
309
- initialHeight += safelyRetrieveFloatProperty (Property .PADDING_TOP );
310
-
311
- initialHeight += safelyRetrieveFloatProperty (Property .MARGIN_TOP );
312
275
313
- if (!this .hasOwnProperty (Property .BORDER ) || this .<Border >getProperty (Property .BORDER ) == null ) {
314
- initialHeight += safelyRetrieveFloatProperty (Property .BORDER_TOP );
315
- }
276
+ final Rectangle initialBBox = layoutContext .getArea ().getBBox ();
277
+ areaBBox .setY (initialBBox .getY () + initialBBox .getHeight () - areaBBox .getHeight ());
316
278
317
- // isFirstLayout is necessary to handle the case when grid container laid out on more
318
- // than 2 pages, and on the last page layout result is full, but there is no bottom border
319
- float TOP_AND_BOTTOM = isFull && isFirstLayout ? 2 : 1 ;
320
- //If container laid out on more than 3 pages, then it is a page where there are no bottom and top borders
321
- if (!isFull && !isFirstLayout ) {
322
- TOP_AND_BOTTOM = 0 ;
323
- }
324
- initialHeight += safelyRetrieveFloatProperty (Property .BORDER ) * TOP_AND_BOTTOM ;
325
- return initialHeight ;
326
- }
327
-
328
- private float safelyRetrieveFloatProperty (int property ) {
329
- final Object value = this .<Object >getProperty (property );
330
- if (value instanceof UnitValue ) {
331
- return ((UnitValue ) value ).getValue ();
332
- }
333
- if (value instanceof Border ) {
334
- return ((Border ) value ).getWidth ();
335
- }
336
- return 0F ;
279
+ final float totalContainerWidth = GridMulticolUtil .updateOccupiedWidth (containerWidth , this );
280
+ areaBBox .setWidth (totalContainerWidth );
281
+ return area ;
337
282
}
338
283
339
284
// Grid layout algorithm is based on a https://drafts.csswg.org/css-grid/#layout-algorithm
0 commit comments