Skip to content

Commit 6d0297a

Browse files
author
Dmitry Radchuk
committed
Add grid shorthands
DEVSIX-8358
1 parent bba9f55 commit 6d0297a

File tree

14 files changed

+732
-5
lines changed

14 files changed

+732
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ private int[] initAxisPlacement(Integer start, Integer end, Integer span) {
146146
if (intStart < intEnd) {
147147
result[0] = intStart;
148148
result[1] = intEnd - intStart;
149+
} else if (intStart == intEnd) {
150+
result[0] = intStart;
149151
} else {
150152
result[0] = intEnd;
151153
result[1] = intStart - intEnd;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public LayoutResult layout(LayoutContext layoutContext) {
9292
GridLayoutResult layoutResult = layoutGrid(layoutContext, actualBBox, grid);
9393

9494
if (layoutResult.getOverflowRenderers().isEmpty()) {
95-
this.occupiedArea = calculateContainerOccupiedArea(layoutContext, grid, true);
95+
this.occupiedArea = calculateContainerOccupiedArea(layoutContext, true);
9696
return new LayoutResult(LayoutResult.FULL, this.occupiedArea, null, null);
9797
} else if (layoutResult.getSplitRenderers().isEmpty()) {
9898
IRenderer cause = layoutResult.getCauseOfNothing() == null ? this : layoutResult.getCauseOfNothing();
9999
return new LayoutResult(LayoutResult.NOTHING, null, null, this, cause);
100100
} else {
101-
this.occupiedArea = calculateContainerOccupiedArea(layoutContext, grid, false);
101+
this.occupiedArea = calculateContainerOccupiedArea(layoutContext, false);
102102
return new LayoutResult(LayoutResult.PARTIAL, this.occupiedArea,
103103
createSplitRenderer(layoutResult.getSplitRenderers()),
104104
createOverflowRenderer(layoutResult.getOverflowRenderers()));
@@ -271,7 +271,7 @@ private static LayoutContext getCellLayoutContext(LayoutContext layoutContext, R
271271
}
272272

273273
// Calculate grid container occupied area based on its width/height properties and cell layout areas
274-
private LayoutArea calculateContainerOccupiedArea(LayoutContext layoutContext, Grid grid, boolean isFull) {
274+
private LayoutArea calculateContainerOccupiedArea(LayoutContext layoutContext, boolean isFull) {
275275
LayoutArea area = layoutContext.getArea().clone();
276276
final float totalHeight = updateOccupiedHeight(containerHeight, isFull);
277277
if (totalHeight < area.getBBox().getHeight() || isFull) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void resolveGridRows() {
146146
private float calculateGridOccupiedHeight(List<Float> originalSizes) {
147147
// Calculate explicit height to ensure that even empty rows which covered by template would be considered
148148
float minHeight = 0.0f;
149-
for (int i = 0; i < (templateRows == null ? 0 : templateRows.size()); ++i) {
149+
for (int i = 0; i < (templateRows == null ? 0 : Math.min(templateRows.size(), originalSizes.size())); ++i) {
150150
minHeight += (float) originalSizes.get(i);
151151
}
152152
float maxHeight = sum(originalSizes);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GridTemplateResolver {
5454
* @return true if auto-fit repeat was encountered, false otherwise
5555
*/
5656
boolean isCollapseNullLines() {
57-
return autoRepeatResolver != null;
57+
return autoRepeatResolver != null && autoRepeatResolver.repeat.isAutoFit();
5858
}
5959

6060
/**

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/CommonCssConstants.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ public class CommonCssConstants {
349349
*/
350350
public static final String DISPLAY = "display";
351351

352+
/**
353+
* The Constant DENSE.
354+
*/
355+
public static final String DENSE = "dense";
356+
352357
/**
353358
* The Constant EMPTY_CELLS.
354359
*/
@@ -499,6 +504,81 @@ public class CommonCssConstants {
499504
*/
500505
public static final String GAP = "gap";
501506

507+
/**
508+
* The Constant GRID.
509+
*/
510+
public static final String GRID = "grid";
511+
512+
/**
513+
* The Constant GRID_COLUMN.
514+
*/
515+
public static final String GRID_COLUMN = "grid-column";
516+
517+
/**
518+
* The Constant GRID_ROW.
519+
*/
520+
public static final String GRID_ROW = "grid-row";
521+
522+
/**
523+
* The Constant GRID_TEMPLATE.
524+
*/
525+
public static final String GRID_TEMPLATE = "grid-template";
526+
527+
/**
528+
* The Constant GRID_COLUMN_END.
529+
*/
530+
public static final String GRID_COLUMN_END = "grid-column-end";
531+
532+
/**
533+
* The Constant GRID_COLUMN_START.
534+
*/
535+
public static final String GRID_COLUMN_START = "grid-column-start";
536+
537+
/**
538+
* The Constant GRID_ROW_END.
539+
*/
540+
public static final String GRID_ROW_END = "grid-row-end";
541+
542+
/**
543+
* The Constant GRID_ROW_START.
544+
*/
545+
public static final String GRID_ROW_START = "grid-row-start";
546+
547+
/**
548+
* The Constant GRID_TEMPLATE_AREAS.
549+
*/
550+
public static final String GRID_TEMPLATE_AREAS = "grid-template-areas";
551+
552+
/**
553+
* The Constant GRID_TEMPLATE_COLUMNS.
554+
*/
555+
public static final String GRID_TEMPLATE_COLUMNS = "grid-template-columns";
556+
557+
/**
558+
* The Constant GRID_TEMPLATE_ROWS.
559+
*/
560+
public static final String GRID_TEMPLATE_ROWS = "grid-template-rows";
561+
562+
/**
563+
* The Constant GRID_AUTO_ROWS.
564+
*/
565+
public static final String GRID_AUTO_ROWS = "grid-auto-rows";
566+
567+
/**
568+
* The Constant GRID_AUTO_COLUMNS.
569+
*/
570+
public static final String GRID_AUTO_COLUMNS = "grid-auto-columns";
571+
572+
/**
573+
* The Constant GRID_AUTO_FLOW.
574+
*/
575+
public static final String GRID_AUTO_FLOW = "grid-auto-flow";
576+
577+
/**
578+
* The Constant AUTO_FLOW.
579+
*/
580+
public static final String AUTO_FLOW = "auto-flow";
581+
502582
/**
503583
* The Constant HANGING_PUNCTUATION.
504584
*/

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/ShorthandResolverFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ This file is part of the iText (R) project.
4141
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.FlexShorthandResolver;
4242
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.FontShorthandResolver;
4343
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.GapShorthandResolver;
44+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.GridColumnShorthandResolver;
45+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.GridRowShorthandResolver;
46+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.GridShorthandResolver;
47+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.GridTemplateShorthandResolver;
4448
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.ListStyleShorthandResolver;
4549
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.MarginShorthandResolver;
4650
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.OutlineShorthandResolver;
@@ -83,6 +87,10 @@ public class ShorthandResolverFactory {
8387
shorthandResolvers.put(CommonCssConstants.PLACE_ITEMS, new PlaceItemsShorthandResolver());
8488
shorthandResolvers.put(CommonCssConstants.COLUMNS, new ColumnsShorthandResolver());
8589
shorthandResolvers.put(CommonCssConstants.COLUMN_RULE, new ColumnRuleShortHandResolver());
90+
shorthandResolvers.put(CommonCssConstants.GRID_ROW, new GridRowShorthandResolver());
91+
shorthandResolvers.put(CommonCssConstants.GRID_COLUMN, new GridColumnShorthandResolver());
92+
shorthandResolvers.put(CommonCssConstants.GRID_TEMPLATE, new GridTemplateShorthandResolver());
93+
shorthandResolvers.put(CommonCssConstants.GRID, new GridShorthandResolver());
8694
}
8795

8896
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl;
2+
3+
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
4+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
5+
6+
/**
7+
* {@link IShorthandResolver} implementation for grid-column shorthand.
8+
*/
9+
public class GridColumnShorthandResolver extends GridItemShorthandResolver {
10+
/**
11+
* Creates a shorthand resolver for grid-column property
12+
*/
13+
public GridColumnShorthandResolver() {
14+
super(CommonCssConstants.GRID_COLUMN);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl;
2+
3+
import com.itextpdf.commons.utils.MessageFormatUtil;
4+
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
5+
import com.itextpdf.styledxmlparser.css.CssDeclaration;
6+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
7+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
8+
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
import java.util.Collections;
15+
import java.util.List;
16+
17+
/**
18+
* {@link IShorthandResolver} implementation for grid items column/row start and end positions.
19+
*/
20+
public abstract class GridItemShorthandResolver implements IShorthandResolver {
21+
private static final Logger LOGGER = LoggerFactory.getLogger(GridItemShorthandResolver.class);
22+
private final String propertyTemplate;
23+
24+
/**
25+
* Creates a new shorthand resolver for provided shorthand template
26+
*
27+
* @param shorthand shorthand from which template will be created.
28+
*/
29+
protected GridItemShorthandResolver(String shorthand) {
30+
this.propertyTemplate = shorthand + "-{0}";
31+
}
32+
33+
@Override
34+
public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
35+
shorthandExpression = shorthandExpression.trim();
36+
if (shorthandExpression.isEmpty()) {
37+
LOGGER.warn(MessageFormatUtil.format(
38+
StyledXmlParserLogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY,
39+
propertyTemplate.substring(0, propertyTemplate.length() - 4)
40+
));
41+
return new ArrayList<>();
42+
}
43+
if (CssTypesValidationUtils.isInitialOrInheritOrUnset(shorthandExpression)
44+
|| CommonCssConstants.AUTO.equals(shorthandExpression)) {
45+
return new ArrayList<>();
46+
}
47+
final String[] values = shorthandExpression.split("/");
48+
if (values.length == 1) {
49+
if (shorthandExpression.startsWith("span")) {
50+
return Collections.singletonList(
51+
new CssDeclaration(MessageFormatUtil.format(propertyTemplate, "start"), values[0]));
52+
}
53+
return Arrays.asList(
54+
new CssDeclaration(MessageFormatUtil.format(propertyTemplate, "start"), values[0]),
55+
new CssDeclaration(MessageFormatUtil.format(propertyTemplate, "end"), values[0])
56+
);
57+
}
58+
return Arrays.asList(
59+
new CssDeclaration(MessageFormatUtil.format(propertyTemplate, "start"), values[0]),
60+
new CssDeclaration(MessageFormatUtil.format(propertyTemplate, "end"), values[1])
61+
);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl;
2+
3+
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
4+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
5+
6+
/**
7+
* {@link IShorthandResolver} implementation for grid-row shorthand.
8+
*/
9+
public class GridRowShorthandResolver extends GridItemShorthandResolver {
10+
/**
11+
* Creates a shorthand resolver for grid-row property
12+
*/
13+
public GridRowShorthandResolver() {
14+
super(CommonCssConstants.GRID_ROW);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl;
2+
3+
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
4+
import com.itextpdf.styledxmlparser.css.CssDeclaration;
5+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* {@link IShorthandResolver} implementation for grid shorthand.
12+
*/
13+
public class GridShorthandResolver implements IShorthandResolver {
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
@Override
19+
public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
20+
if (!shorthandExpression.contains(CommonCssConstants.AUTO_FLOW)) {
21+
return new GridTemplateShorthandResolver().resolveShorthand(shorthandExpression);
22+
}
23+
String[] values = shorthandExpression.trim().split("/");
24+
List<CssDeclaration> result = new ArrayList<>();
25+
if (values[0].contains(CommonCssConstants.AUTO_FLOW)) {
26+
if (values[0].contains(CommonCssConstants.DENSE)) {
27+
result.add(new CssDeclaration(CommonCssConstants.GRID_AUTO_FLOW, CommonCssConstants.DENSE));
28+
}
29+
String rowsTemplate =
30+
values[0].substring(
31+
Math.max(values[0].indexOf(CommonCssConstants.AUTO_FLOW) + CommonCssConstants.AUTO_FLOW.length(),
32+
values[0].indexOf(CommonCssConstants.DENSE) + CommonCssConstants.DENSE.length())
33+
);
34+
if (!rowsTemplate.trim().isEmpty()) {
35+
result.add(new CssDeclaration(CommonCssConstants.GRID_AUTO_ROWS, rowsTemplate));
36+
}
37+
if (values.length == 2) {
38+
result.add(new CssDeclaration(CommonCssConstants.GRID_TEMPLATE_COLUMNS, values[1]));
39+
}
40+
} else if (values.length == 2) {
41+
result.add(new CssDeclaration(CommonCssConstants.GRID_TEMPLATE_ROWS, values[0]));
42+
if (values[1].contains(CommonCssConstants.DENSE)) {
43+
result.add(new CssDeclaration(CommonCssConstants.GRID_AUTO_FLOW, CommonCssConstants.COLUMN + " " + CommonCssConstants.DENSE));
44+
}
45+
String columnsTemplate =
46+
values[1].substring(
47+
Math.max(values[1].indexOf(CommonCssConstants.AUTO_FLOW) + CommonCssConstants.AUTO_FLOW.length(),
48+
values[1].indexOf(CommonCssConstants.DENSE) + CommonCssConstants.DENSE.length())
49+
);
50+
if (!columnsTemplate.trim().isEmpty()) {
51+
result.add(new CssDeclaration(CommonCssConstants.GRID_AUTO_COLUMNS, columnsTemplate));
52+
}
53+
}
54+
return result;
55+
}
56+
}

0 commit comments

Comments
 (0)