Skip to content

Commit 34da5c6

Browse files
committed
Implement supporting background-repeat CSS property
DEVSIX-4370
1 parent 61935ad commit 34da5c6

File tree

222 files changed

+273
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

222 files changed

+273
-115
lines changed

src/main/java/com/itextpdf/html2pdf/css/apply/util/BackgroundApplierUtil.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ This file is part of the iText (R) project.
5959
import com.itextpdf.layout.property.BackgroundRepeat;
6060
import com.itextpdf.layout.property.BackgroundSize;
6161
import com.itextpdf.layout.property.BlendMode;
62+
import com.itextpdf.layout.property.BackgroundRepeat.BackgroundRepeatValue;
6263
import com.itextpdf.layout.property.Property;
6364
import com.itextpdf.layout.property.UnitValue;
6465
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
6566
import com.itextpdf.styledxmlparser.css.util.CssGradientUtil;
67+
import com.itextpdf.styledxmlparser.css.util.CssMappingUtils;
6668
import com.itextpdf.styledxmlparser.css.util.CssUtils;
6769
import com.itextpdf.styledxmlparser.exceptions.StyledXMLParserException;
6870

@@ -127,10 +129,11 @@ public static void applyBackground(Map<String, String> cssProps, ProcessorContex
127129
applyBackgroundPosition(backgroundPositionXArray, backgroundPositionYArray, i, em, rem);
128130
final BlendMode blendMode = applyBackgroundBlendMode(backgroundBlendModeArray, i);
129131
boolean imageApplied = false;
132+
final BackgroundRepeat repeat = applyBackgroundRepeat(backgroundRepeatArray, i);
133+
130134
if (CssGradientUtil.isCssLinearGradientValue(backgroundImage)) {
131-
imageApplied = applyLinearGradient(backgroundImage, backgroundImagesList, blendMode, position, em, rem);
135+
imageApplied = applyLinearGradient(backgroundImage, backgroundImagesList, blendMode, position, em, rem, repeat);
132136
} else {
133-
final BackgroundRepeat repeat = applyBackgroundRepeat(backgroundRepeatArray, i);
134137
final PdfXObject image = context.getResourceResolver().retrieveImageExtended(
135138
CssUtils.extractUrl(backgroundImage));
136139
imageApplied = applyBackgroundImage(image, backgroundImagesList, repeat, blendMode, position);
@@ -251,12 +254,23 @@ private static void applyBackgroundPositionY(BackgroundPosition position, String
251254
private static BackgroundRepeat applyBackgroundRepeat(List<String> backgroundRepeatArray, int iteration) {
252255
final int index = getBackgroundSidePropertyIndex(backgroundRepeatArray.size(), iteration);
253256
if (index != -1) {
254-
final boolean repeatX = CssConstants.REPEAT.equals(backgroundRepeatArray.get(index)) ||
255-
CssConstants.REPEAT_X.equals(backgroundRepeatArray.get(index));
256-
final boolean repeatY = CssConstants.REPEAT.equals(backgroundRepeatArray.get(index)) ||
257-
CssConstants.REPEAT_Y.equals(backgroundRepeatArray.get(index));
258-
return new BackgroundRepeat(repeatX, repeatY);
257+
String[] repeatProps = backgroundRepeatArray.get(index).split(" ");
258+
if (repeatProps.length == 1) {
259+
if (CommonCssConstants.REPEAT_X.equals(repeatProps[0])) {
260+
return new BackgroundRepeat(BackgroundRepeatValue.REPEAT, BackgroundRepeatValue.NO_REPEAT);
261+
} else if (CommonCssConstants.REPEAT_Y.equals(repeatProps[0])) {
262+
return new BackgroundRepeat(BackgroundRepeatValue.NO_REPEAT, BackgroundRepeatValue.REPEAT);
263+
} else {
264+
BackgroundRepeatValue value = CssMappingUtils.parseBackgroundRepeat(repeatProps[0]);
265+
return new BackgroundRepeat(value);
266+
}
267+
} else if (repeatProps.length == 2) {
268+
return new BackgroundRepeat(CssMappingUtils.parseBackgroundRepeat(repeatProps[0]),
269+
CssMappingUtils.parseBackgroundRepeat(repeatProps[1]));
270+
}
259271
}
272+
// Valid cases are processed by the block above, and in invalid
273+
// situations, the REPEAT property on both axes is used by default
260274
return new BackgroundRepeat();
261275
}
262276

@@ -296,13 +310,13 @@ private static boolean applyBackgroundImage(PdfXObject image, List<BackgroundIma
296310
}
297311

298312
private static boolean applyLinearGradient(String image, List<BackgroundImage> backgroundImagesList,
299-
BlendMode blendMode, BackgroundPosition position, float em, float rem) {
313+
BlendMode blendMode, BackgroundPosition position, float em, float rem, final BackgroundRepeat repeat) {
300314
try {
301315
StrategyBasedLinearGradientBuilder gradientBuilder =
302316
CssGradientUtil.parseCssLinearGradient(image, em, rem);
303317
if (gradientBuilder != null) {
304318
backgroundImagesList.add(new BackgroundImage.Builder().setLinearGradientBuilder(gradientBuilder)
305-
.setBackgroundBlendMode(blendMode).setBackgroundPosition(position).build());
319+
.setBackgroundBlendMode(blendMode).setBackgroundPosition(position).setBackgroundRepeat(repeat).build());
306320
return true;
307321
}
308322
} catch (StyledXMLParserException e) {

src/test/java/com/itextpdf/html2pdf/css/BackgroundRepeatTest.java

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This file is part of the iText (R) project.
2626
import com.itextpdf.test.annotations.type.IntegrationTest;
2727

2828
import java.io.IOException;
29+
2930
import org.junit.BeforeClass;
3031
import org.junit.Test;
3132
import org.junit.experimental.categories.Category;
@@ -42,181 +43,167 @@ public static void beforeClass() {
4243
}
4344

4445
@Test
45-
//TODO: DEVSIX-4370 update cmp file
4646
public void imageBckgRepeatTest() throws IOException, InterruptedException {
4747
convertToPdfAndCompare("imageBckgRepeat", SOURCE_FOLDER, DESTINATION_FOLDER);
4848
}
4949

5050
@Test
51-
//TODO: DEVSIX-4370 update cmp file
5251
public void imageBckgNoRepeatTest() throws InterruptedException, IOException {
5352
convertToPdfAndCompare("imageBckgNoRepeat", SOURCE_FOLDER, DESTINATION_FOLDER);
5453
}
5554

5655
@Test
57-
//TODO: DEVSIX-4370 update cmp file
5856
public void imageBckgRoundTest() throws InterruptedException, IOException {
5957
convertToPdfAndCompare("imageBckgRound", SOURCE_FOLDER, DESTINATION_FOLDER);
6058
}
6159

6260
@Test
63-
//TODO: DEVSIX-4370 update cmp file
6461
public void imageBckgSpaceTest() throws InterruptedException, IOException {
6562
convertToPdfAndCompare("imageBckgSpace", SOURCE_FOLDER, DESTINATION_FOLDER);
6663
}
6764

6865
@Test
69-
//TODO: DEVSIX-4370 update cmp file
66+
public void imageBckgSpaceWithBigBorderTest() throws InterruptedException, IOException {
67+
// TODO DEVSIX-2105
68+
convertToPdfAndCompare("imageBckgSpaceWithBigBorder", SOURCE_FOLDER, DESTINATION_FOLDER);
69+
}
70+
71+
@Test
7072
public void imageBckgRepeatXTest() throws InterruptedException, IOException {
7173
convertToPdfAndCompare("imageBckgRepeatX", SOURCE_FOLDER, DESTINATION_FOLDER);
7274
}
7375

7476
@Test
75-
//TODO: DEVSIX-4370 update cmp file
7677
public void imageBckgRepeatYTest() throws InterruptedException, IOException {
7778
convertToPdfAndCompare("imageBckgRepeatY", SOURCE_FOLDER, DESTINATION_FOLDER);
7879
}
7980

8081
@Test
81-
//TODO: DEVSIX-4370 update cmp file
8282
public void linearGradientBckgRepeatTest() throws InterruptedException, IOException {
8383
convertToPdfAndCompare("linearGradientBckgRepeat", SOURCE_FOLDER, DESTINATION_FOLDER);
8484
}
8585

8686
@Test
87-
//TODO: DEVSIX-4370 update cmp file
8887
public void linearGradientBckgNoRepeatTest() throws InterruptedException, IOException {
8988
convertToPdfAndCompare("linearGradientBckgNoRepeat", SOURCE_FOLDER, DESTINATION_FOLDER);
9089
}
9190

9291
@Test
93-
//TODO: DEVSIX-4370 update cmp file
9492
public void linearGradientBckgRoundTest() throws IOException, InterruptedException {
9593
convertToPdfAndCompare("linearGradientBckgRound", SOURCE_FOLDER, DESTINATION_FOLDER);
9694
}
9795

9896
@Test
99-
//TODO: DEVSIX-1708 update cmp file
10097
public void linearGradientBckgSpaceTest() throws IOException, InterruptedException {
10198
convertToPdfAndCompare("linearGradientBckgSpace", SOURCE_FOLDER, DESTINATION_FOLDER);
10299
}
103100

104101
@Test
105-
//TODO: DEVSIX-4370 update cmp file
106102
public void linearGradientBckgRepeatXTest() throws IOException, InterruptedException {
107103
convertToPdfAndCompare("linearGradientBckgRepeatX", SOURCE_FOLDER, DESTINATION_FOLDER);
108104
}
109105

110106
@Test
111-
//TODO: DEVSIX-4370 update cmp file
112107
public void linearGradientBckgRepeatYTest() throws IOException, InterruptedException {
113108
convertToPdfAndCompare("linearGradientBckgRepeatY", SOURCE_FOLDER, DESTINATION_FOLDER);
114109
}
115110

116111
@Test
117-
//TODO: DEVSIX-4370 update cmp file
118112
public void imageBckgRepeatAndSpaceTest() throws IOException, InterruptedException {
119113
convertToPdfAndCompare("imageBckgRepeatAndSpace", SOURCE_FOLDER, DESTINATION_FOLDER);
120114
}
121115

122116
@Test
123-
//TODO: DEVSIX-4370 update cmp file
124117
public void imageBckgRoundAndSpaceTest() throws IOException, InterruptedException {
125118
convertToPdfAndCompare("imageBckgRoundAndSpace", SOURCE_FOLDER, DESTINATION_FOLDER);
126119
}
127120

128121
@Test
129-
//TODO: DEVSIX-4370 update cmp file
130122
public void bckgRepeatAndBckgPositionXTest() throws IOException, InterruptedException {
131123
convertToPdfAndCompare("bckgRepeatAndBckgPositionX", SOURCE_FOLDER, DESTINATION_FOLDER);
132124
}
133125

134126
@Test
135-
//TODO: DEVSIX-4370 update cmp file
136127
public void bckgRepeatAndBckgPositionYTest() throws IOException, InterruptedException {
137128
convertToPdfAndCompare("bckgRepeatAndBckgPositionY", SOURCE_FOLDER, DESTINATION_FOLDER);
138129
}
139130

140131
@Test
141-
//TODO: DEVSIX-4370 update cmp file
142132
public void bckgRoundSpaceAndBckgPositionXTest() throws IOException, InterruptedException {
143133
convertToPdfAndCompare("bckgRoundSpaceAndBckgPositionX", SOURCE_FOLDER, DESTINATION_FOLDER);
144134
}
145135

146136
@Test
147-
//TODO: DEVSIX-4370 update cmp file
148137
public void bckgSpaceRoundAndBckgPositionYTest() throws IOException, InterruptedException {
149138
convertToPdfAndCompare("bckgSpaceRoundAndBckgPositionY", SOURCE_FOLDER, DESTINATION_FOLDER);
150139
}
151140

152141
@Test
153-
//TODO: DEVSIX-4370 update cmp file
154142
public void bckgRepeatXAndBckgPositionYTest() throws IOException, InterruptedException {
155143
convertToPdfAndCompare("bckgRepeatXAndBckgPositionY", SOURCE_FOLDER, DESTINATION_FOLDER);
156144
}
157145

158146
@Test
159-
//TODO: DEVSIX-4370 update cmp file
160147
public void bckgRepeatYAndBckgPositionXTest() throws IOException, InterruptedException {
161148
convertToPdfAndCompare("bckgRepeatYAndBckgPositionX", SOURCE_FOLDER, DESTINATION_FOLDER);
162149
}
163150

164151
@Test
165-
//TODO: DEVSIX-4370 update cmp file
166152
public void bckgRoundAndBckgPositionTest() throws IOException, InterruptedException {
167153
convertToPdfAndCompare("bckgRoundAndBckgPosition", SOURCE_FOLDER, DESTINATION_FOLDER);
168154
}
169155

170156
@Test
171-
//TODO: DEVSIX-4370 update cmp file
172157
public void bckgRoundAndNegativeBckgPositionTest() throws IOException, InterruptedException {
173158
convertToPdfAndCompare("bckgRoundAndNegativeBckgPosition", SOURCE_FOLDER, DESTINATION_FOLDER);
174159
}
175160

176161
@Test
177-
//TODO: DEVSIX-4370 update cmp file
178162
public void bckgSpaceAndBckgPositionTest() throws IOException, InterruptedException {
179163
convertToPdfAndCompare("bckgSpaceAndBckgPosition", SOURCE_FOLDER, DESTINATION_FOLDER);
180164
}
181165

182166
@Test
183-
//TODO: DEVSIX-4370 update cmp file
184167
public void bckgSpaceAndBckgPositionPageSeparationTest() throws IOException, InterruptedException {
185168
convertToPdfAndCompare("bckgSpaceAndBckgPositionPageSeparation", SOURCE_FOLDER, DESTINATION_FOLDER);
186169
}
187170

188171
@Test
189-
//TODO: DEVSIX-4370 update cmp file
190172
public void bckgSpaceAndNegativeBckgPositionTest() throws IOException, InterruptedException {
191173
convertToPdfAndCompare("bckgSpaceAndNegativeBckgPosition", SOURCE_FOLDER, DESTINATION_FOLDER);
192174
}
193175

194176
@Test
195-
//TODO: DEVSIX-4370 update cmp file
177+
public void bckgSpaceAndBckgPositionAdvancedTest() throws IOException, InterruptedException {
178+
convertToPdfAndCompare("bckgSpaceAndBckgPositionAdvanced", SOURCE_FOLDER, DESTINATION_FOLDER);
179+
}
180+
181+
@Test
182+
public void bcgRoundAndBckgSizeAutoAndContainAdvancedTest() throws IOException, InterruptedException {
183+
convertToPdfAndCompare("bcgRoundAndBckgSizeAutoAndContainAdvanced", SOURCE_FOLDER, DESTINATION_FOLDER);
184+
}
185+
186+
@Test
196187
public void bckgRoundRemainsLessHalfOfImageTest() throws IOException, InterruptedException {
197188
convertToPdfAndCompare("bckgRoundRemainsLessHalfOfImage", SOURCE_FOLDER, DESTINATION_FOLDER);
198189
}
199190

200191
@Test
201-
//TODO: DEVSIX-4370 update cmp file
202192
public void bckgRoundRemainsMoreHalfOfImageTest() throws IOException, InterruptedException {
203193
convertToPdfAndCompare("bckgRoundRemainsMoreHalfOfImage", SOURCE_FOLDER, DESTINATION_FOLDER);
204194
}
205195

206196
@Test
207-
//TODO: DEVSIX-4370 update cmp file
208197
public void bckgRoundCompressAndStretchImageTest() throws IOException, InterruptedException {
209198
convertToPdfAndCompare("bckgRoundCompressAndStretchImage", SOURCE_FOLDER, DESTINATION_FOLDER);
210199
}
211200

212201
@Test
213-
//TODO: DEVSIX-4370 update cmp file
214202
public void imageBckgRoundBckgSizeLessThanImageTest() throws IOException, InterruptedException {
215203
convertToPdfAndCompare("imageBckgRoundBckgSizeLessThanImage", SOURCE_FOLDER, DESTINATION_FOLDER);
216204
}
217205

218206
@Test
219-
//TODO: DEVSIX-4370 update cmp file
220207
public void imageBckgSpaceBckgSizeLessThanImageTest() throws IOException, InterruptedException {
221208
convertToPdfAndCompare("imageBckgSpaceBckgSizeLessThanImage", SOURCE_FOLDER, DESTINATION_FOLDER);
222209
}

src/test/java/com/itextpdf/html2pdf/css/BackgroundTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,11 @@ public void backgroundSizeTest01() throws IOException, InterruptedException {
7979
}
8080

8181
@Test
82-
// TODO DEVSIX-4370 support background repeat for linear-gradient
8382
public void backgroundAttachmentMarginRoot1Test() throws IOException, InterruptedException {
8483
convertToPdfAndCompare("backgroundAttachmentMarginRoot1", sourceFolder, destinationFolder);
8584
}
8685

8786
@Test
88-
// TODO DEVSIX-4370 support background repeat for linear-gradient
8987
public void backgroundAttachmentMarginRoot2Test() throws IOException, InterruptedException {
9088
convertToPdfAndCompare("backgroundAttachmentMarginRoot2", sourceFolder, destinationFolder);
9189
}

src/test/java/com/itextpdf/html2pdf/css/LinearGradientTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public void backgroundImageLinearGradientInHeaderTest() throws IOException, Inte
243243

244244
@Test
245245
public void backgroundImageLinearGradientInAbbrTest() throws IOException, InterruptedException {
246+
// TODO DEVSIX-4617 process abbr tag with "text-decoration: underline dotted" CSS styles
246247
convertToPdfAndCompare("backgroundImageLinearGradientInAbbr", sourceFolder, destinationFolder);
247248
}
248249

0 commit comments

Comments
 (0)