@@ -23,7 +23,6 @@ This file is part of the iText (R) project.
2323package com .itextpdf .layout .renderer ;
2424
2525import com .itextpdf .kernel .geom .Rectangle ;
26- import com .itextpdf .layout .borders .Border ;
2726import com .itextpdf .layout .element .GridContainer ;
2827import com .itextpdf .layout .layout .LayoutArea ;
2928import com .itextpdf .layout .layout .LayoutContext ;
@@ -45,6 +44,7 @@ This file is part of the iText (R) project.
4544public class GridContainerRenderer extends BlockRenderer {
4645 private boolean isFirstLayout = true ;
4746 private float containerHeight = 0.0f ;
47+ private float containerWidth = 0.0f ;
4848 /**
4949 * Creates a Grid renderer from its corresponding layout object.
5050 * @param modelElement the {@link GridContainer} which this object should manage
@@ -72,14 +72,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
7272
7373 Rectangle actualBBox = layoutContext .getArea ().getBBox ().clone ();
7474 Float blockWidth = retrieveWidth (actualBBox .getWidth ());
75- if (blockWidth != null ) {
76- actualBBox .setWidth ((float ) blockWidth );
77- }
7875
7976 ContinuousContainer .setupContinuousContainerIfNeeded (this );
8077 applyPaddings (actualBBox , false );
8178 applyBorderBox (actualBBox , false );
8279 applyMargins (actualBBox , false );
80+ applyWidth (actualBBox , blockWidth , OverflowPropertyValue .VISIBLE );
81+ containerWidth = actualBBox .getWidth ();
8382
8483 Float blockHeight = retrieveHeight ();
8584 if (blockHeight != null && (float ) blockHeight < actualBBox .getHeight ()) {
@@ -100,7 +99,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
10099 } else {
101100 this .occupiedArea = calculateContainerOccupiedArea (layoutContext , false );
102101 return new LayoutResult (LayoutResult .PARTIAL , this .occupiedArea ,
103- createSplitRenderer (layoutResult .getSplitRenderers ()),
102+ GridMulticolUtil . createSplitRenderer (layoutResult .getSplitRenderers (), this ),
104103 createOverflowRenderer (layoutResult .getOverflowRenderers ()));
105104 }
106105 }
@@ -141,18 +140,6 @@ private static Boolean determineCollapsingMargins(IRenderer renderer) {
141140 return Boolean .FALSE ;
142141 }
143142
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-
156143 private AbstractRenderer createOverflowRenderer (List <IRenderer > children ) {
157144 GridContainerRenderer overflowRenderer = (GridContainerRenderer ) getNextRenderer ();
158145 overflowRenderer .isFirstLayout = false ;
@@ -273,67 +260,25 @@ private static LayoutContext getCellLayoutContext(LayoutContext layoutContext, R
273260 // Calculate grid container occupied area based on its width/height properties and cell layout areas
274261 private LayoutArea calculateContainerOccupiedArea (LayoutContext layoutContext , boolean isFull ) {
275262 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 ();
300264
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 );
307273 }
308274 }
309- initialHeight += safelyRetrieveFloatProperty (Property .PADDING_TOP );
310-
311- initialHeight += safelyRetrieveFloatProperty (Property .MARGIN_TOP );
312275
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 ());
316278
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 ;
337282 }
338283
339284 // Grid layout algorithm is based on a https://drafts.csswg.org/css-grid/#layout-algorithm
0 commit comments