Skip to content

Commit 5a8f86d

Browse files
committed
Support grid-gap, grid-column-gap and grid-row-gap and log unsupported properties
DEVSIX-8376
1 parent 1459417 commit 5a8f86d

File tree

7 files changed

+89
-19
lines changed

7 files changed

+89
-19
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/data/ImageRenderInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ImageRenderInfo(Stack<CanvasTag> canvasTagHierarchy, CanvasGraphicsState
9090
* <li>get image bytes with {@link PdfImageXObject#getImageBytes(boolean)}, these image bytes
9191
* represent native image, i.e you can write these bytes to disk and get just an usual image;
9292
* <li>obtain PdfStream object which contains image dictionary with {@link PdfImageXObject#getPdfObject()} method;
93-
* <li>convert image to {@link java.awt.image.BufferedImage} with {@link PdfImageXObject#getBufferedImage()};
93+
* <li>convert image to {@link java.awt.image.BufferedImage} with {@link PdfImageXObject#getBufferedImage()}; // Android-Conversion-Skip-Line (java.awt library isn't available on Android)
9494
* </ul>
9595
*
9696
* @return the {@link PdfImageXObject image}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,29 @@ public class CommonCssConstants {
529529
*/
530530
public static final String GRID_COLUMN_END = "grid-column-end";
531531

532+
/**
533+
* The Constant GRID_COLUMN_GAP.
534+
*/
535+
public static final String GRID_COLUMN_GAP = "grid-column-gap";
536+
532537
/**
533538
* The Constant GRID_COLUMN_START.
534539
*/
535540
public static final String GRID_COLUMN_START = "grid-column-start";
536541

542+
/**
543+
* The Constant GRID_GAP.
544+
*/
545+
public static final String GRID_GAP = "grid-gap";
546+
537547
/**
538548
* The Constant GRID_ROW_END.
539549
*/
540550
public static final String GRID_ROW_END = "grid-row-end";
551+
/**
552+
* The Constant GRID_ROW_GAP.
553+
*/
554+
public static final String GRID_ROW_GAP = "grid-row-gap";
541555

542556
/**
543557
* The Constant GRID_ROW_START.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public class ShorthandResolverFactory {
8383
shorthandResolvers.put(CommonCssConstants.TEXT_DECORATION, new TextDecorationShorthandResolver());
8484
shorthandResolvers.put(CommonCssConstants.FLEX, new FlexShorthandResolver());
8585
shorthandResolvers.put(CommonCssConstants.FLEX_FLOW, new FlexFlowShorthandResolver());
86-
shorthandResolvers.put(CommonCssConstants.GAP, new GapShorthandResolver());
86+
shorthandResolvers.put(CommonCssConstants.GAP, new GapShorthandResolver(CommonCssConstants.GAP));
87+
shorthandResolvers.put(CommonCssConstants.GRID_GAP, new GapShorthandResolver(CommonCssConstants.GRID_GAP));
8788
shorthandResolvers.put(CommonCssConstants.PLACE_ITEMS, new PlaceItemsShorthandResolver());
8889
shorthandResolvers.put(CommonCssConstants.COLUMNS, new ColumnsShorthandResolver());
8990
shorthandResolvers.put(CommonCssConstants.COLUMN_RULE, new ColumnRuleShortHandResolver());

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,41 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl;
2424

2525
import com.itextpdf.commons.utils.MessageFormatUtil;
26-
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
2726
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
2827
import com.itextpdf.styledxmlparser.css.CssDeclaration;
2928
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
3029
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
3130
import com.itextpdf.styledxmlparser.css.validate.CssDeclarationValidationMaster;
31+
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
3232

3333
import java.util.Arrays;
3434
import java.util.Collections;
3535
import java.util.List;
3636
import org.slf4j.Logger;
3737
import org.slf4j.LoggerFactory;
3838

39+
/**
40+
* Shorthand resolver for gap shorthand properties, can be used for
41+
* different gap properties like {@code gap} or {@code grid-gap}.
42+
*/
3943
public class GapShorthandResolver implements IShorthandResolver {
44+
private final String gapShorthandProperty;
45+
46+
/**
47+
* Instantiates default {@link GapShorthandResolver} for {@code gap} shorthand.
48+
*/
49+
public GapShorthandResolver() {
50+
this(CommonCssConstants.GAP);
51+
}
52+
53+
/**
54+
* Instantiates default {@link GapShorthandResolver} for passed gap shorthand.
55+
*
56+
* @param gapShorthandProperty the name of the gap shorthand property
57+
*/
58+
public GapShorthandResolver(String gapShorthandProperty) {
59+
this.gapShorthandProperty = gapShorthandProperty;
60+
}
4061

4162
private static final Logger LOGGER = LoggerFactory.getLogger(GapShorthandResolver.class);
4263

@@ -53,12 +74,12 @@ public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
5374
);
5475
}
5576
if (CssTypesValidationUtils.containsInitialOrInheritOrUnset(shorthandExpression)) {
56-
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, CommonCssConstants.GAP,
57-
shorthandExpression);
77+
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION,
78+
gapShorthandProperty, shorthandExpression);
5879
}
5980
if (shorthandExpression.isEmpty()) {
60-
return handleExpressionError(StyledXmlParserLogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY, CommonCssConstants.GAP,
61-
shorthandExpression);
81+
return handleExpressionError(StyledXmlParserLogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY,
82+
gapShorthandProperty, shorthandExpression);
6283
}
6384

6485
final String[] gapProps = shorthandExpression.split(" ");
@@ -68,8 +89,8 @@ public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
6889
} else if (gapProps.length == 2) {
6990
return resolveGapWithTwoProperties(gapProps[0], gapProps[1]);
7091
} else {
71-
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, CommonCssConstants.GAP,
72-
shorthandExpression);
92+
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION,
93+
gapShorthandProperty, shorthandExpression);
7394

7495
}
7596
}

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/validate/impl/CssDefaultValidator.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,18 @@ public CssDefaultValidator() {
113113
new MultiTypeDeclarationValidator(new CssNumberValueValidator(false),
114114
new CssLengthValueValidator(false), new CssPercentageValueValidator(false), normalValidator,
115115
inheritInitialUnsetValidator));
116-
defaultValidators.put(CommonCssConstants.COLUMN_GAP,
117-
new MultiTypeDeclarationValidator(new CssLengthValueValidator(false),
118-
new CssPercentageValueValidator(false), normalValidator));
116+
final MultiTypeDeclarationValidator gapValidator = new MultiTypeDeclarationValidator(
117+
new CssLengthValueValidator(false), new CssPercentageValueValidator(false), normalValidator,
118+
inheritInitialUnsetValidator);
119+
defaultValidators.put(CommonCssConstants.COLUMN_GAP, gapValidator);
120+
defaultValidators.put(CommonCssConstants.GRID_COLUMN_GAP, gapValidator);
119121
defaultValidators.put(CommonCssConstants.COLUMN_WIDTH,
120122
new MultiTypeDeclarationValidator(new CssLengthValueValidator(false),
121123
new CssPercentageValueValidator(false), new CssEnumValidator(CommonCssConstants.AUTO)));
122124
defaultValidators.put(CommonCssConstants.COLUMN_COUNT, new MultiTypeDeclarationValidator(
123125
new CssIntegerNumberValueValidator(false, false), new CssEnumValidator(CommonCssConstants.AUTO)));
124-
defaultValidators.put(CommonCssConstants.ROW_GAP, new MultiTypeDeclarationValidator(
125-
new CssLengthValueValidator(false), new CssPercentageValueValidator(false), normalValidator,
126-
inheritInitialUnsetValidator));
126+
defaultValidators.put(CommonCssConstants.ROW_GAP, gapValidator);
127+
defaultValidators.put(CommonCssConstants.GRID_ROW_GAP, gapValidator);
127128
defaultValidators.put(CommonCssConstants.FLEX_GROW, new MultiTypeDeclarationValidator(
128129
new CssNumberValueValidator(false), inheritInitialUnsetValidator));
129130
defaultValidators.put(CommonCssConstants.FLEX_SHRINK, new MultiTypeDeclarationValidator(

styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/GapShorthandResolverTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.styledxmlparser.css.resolve.shorthand;
2424

25-
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
2625
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
2726
import com.itextpdf.styledxmlparser.css.CssDeclaration;
2827
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.GapShorthandResolver;
28+
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
2929
import com.itextpdf.test.ExtendedITextTest;
3030
import com.itextpdf.test.annotations.LogMessage;
3131
import com.itextpdf.test.annotations.LogMessages;
3232
import com.itextpdf.test.annotations.type.UnitTest;
33-
import org.junit.Assert;
34-
import org.junit.Test;
35-
import org.junit.experimental.categories.Category;
3633

3734
import java.util.Collections;
3835
import java.util.List;
36+
import org.junit.Assert;
37+
import org.junit.Test;
38+
import org.junit.experimental.categories.Category;
3939

4040
@Category(UnitTest.class)
4141
public class GapShorthandResolverTest extends ExtendedITextTest {
@@ -146,6 +146,20 @@ public void gapWithTwoValidValuesTest() {
146146
Assert.assertEquals("15px", resolvedShorthand.get(1).getExpression());
147147
}
148148

149+
@Test
150+
public void gridGapWithTwoValidValuesTest() {
151+
IShorthandResolver resolver = new GapShorthandResolver(CommonCssConstants.GRID_GAP);
152+
153+
String shorthand = "10px 15px";
154+
List<CssDeclaration> resolvedShorthand = resolver.resolveShorthand(shorthand);
155+
156+
Assert.assertEquals(2, resolvedShorthand.size());
157+
Assert.assertEquals(CommonCssConstants.ROW_GAP, resolvedShorthand.get(0).getProperty());
158+
Assert.assertEquals("10px", resolvedShorthand.get(0).getExpression());
159+
Assert.assertEquals(CommonCssConstants.COLUMN_GAP, resolvedShorthand.get(1).getProperty());
160+
Assert.assertEquals("15px", resolvedShorthand.get(1).getExpression());
161+
}
162+
149163
@Test
150164
@LogMessages(messages = @LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION))
151165
public void gapWithValidAndInvalidValuesTest() {

styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/css/validate/CssDeclarationValidationMasterTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This file is part of the iText (R) project.
2828
import com.itextpdf.styledxmlparser.css.validate.impl.CssDeviceCmykAwareValidator;
2929
import com.itextpdf.test.ExtendedITextTest;
3030
import com.itextpdf.test.annotations.type.UnitTest;
31+
3132
import org.junit.Assert;
3233
import org.junit.Test;
3334
import org.junit.experimental.categories.Category;
@@ -411,6 +412,24 @@ public void multicolValidationTest() {
411412
new CssDeclaration(CommonCssConstants.COLUMN_GAP, "10")));
412413
}
413414

415+
@Test
416+
public void gridRowColumnGapTest() {
417+
Assert.assertTrue(CssDeclarationValidationMaster.checkDeclaration(
418+
new CssDeclaration(CommonCssConstants.GRID_ROW_GAP, "normal")));
419+
Assert.assertTrue(CssDeclarationValidationMaster.checkDeclaration(
420+
new CssDeclaration(CommonCssConstants.GRID_COLUMN_GAP, "30px")));
421+
Assert.assertTrue(CssDeclarationValidationMaster.checkDeclaration(
422+
new CssDeclaration(CommonCssConstants.GRID_ROW_GAP, "15%")));
423+
Assert.assertTrue(CssDeclarationValidationMaster.checkDeclaration(
424+
new CssDeclaration(CommonCssConstants.GRID_ROW_GAP, "2em")));
425+
Assert.assertTrue(CssDeclarationValidationMaster.checkDeclaration(
426+
new CssDeclaration(CommonCssConstants.GRID_COLUMN_GAP, "3rem")));
427+
Assert.assertFalse(CssDeclarationValidationMaster.checkDeclaration(
428+
new CssDeclaration(CommonCssConstants.GRID_COLUMN_GAP, "-5em")));
429+
Assert.assertFalse(CssDeclarationValidationMaster.checkDeclaration(
430+
new CssDeclaration(CommonCssConstants.GRID_ROW_GAP, "10")));
431+
}
432+
414433
@Test
415434
public void changeValidatorTest() {
416435
try {

0 commit comments

Comments
 (0)