Skip to content

Commit 9ce4248

Browse files
committed
Add support of background-clip and background-origin
DEVSIX-2105
1 parent c62f83b commit 9ce4248

File tree

9 files changed

+498
-94
lines changed

9 files changed

+498
-94
lines changed

layout/src/main/java/com/itextpdf/layout/property/Background.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class Background {
5959
protected float extraRight;
6060
protected float extraTop;
6161
protected float extraBottom;
62+
private BackgroundBox backgroundClip = BackgroundBox.BORDER_BOX;
6263

6364
/**
6465
* Creates a background with a specified color.
@@ -110,6 +111,19 @@ public Background(Color color, float opacity, float extraLeft, float extraTop, f
110111
this.extraBottom = extraBottom;
111112
}
112113

114+
/**
115+
* Creates a background with a specified color, opacity and clip value.
116+
*
117+
* @param color the background color
118+
* @param opacity the opacity of the background color; a float between 0 and 1, where 1 stands for fully opaque
119+
* color and 0 - for fully transparent
120+
* @param clip the value to clip the background color
121+
*/
122+
public Background(Color color, float opacity, BackgroundBox clip) {
123+
this(color, opacity);
124+
this.backgroundClip = clip;
125+
}
126+
113127
/**
114128
* Gets the background's color.
115129
* @return a {@link Color} of any supported kind
@@ -157,4 +171,13 @@ public float getExtraTop() {
157171
public float getExtraBottom() {
158172
return extraBottom;
159173
}
174+
175+
/**
176+
* Gets background clip value.
177+
*
178+
* @return background clip value
179+
*/
180+
public BackgroundBox getBackgroundClip() {
181+
return backgroundClip;
182+
}
160183
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2020 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
45+
package com.itextpdf.layout.property;
46+
47+
/**
48+
* Enum with boxes that can be used as a value in background-clip and background-origin properties.
49+
*/
50+
public enum BackgroundBox {
51+
BORDER_BOX,
52+
PADDING_BOX,
53+
CONTENT_BOX
54+
}

layout/src/main/java/com/itextpdf/layout/property/BackgroundImage.java

Lines changed: 116 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,26 @@ public class BackgroundImage {
8080

8181
private final BackgroundSize backgroundSize;
8282

83+
private final BackgroundBox backgroundClip;
84+
85+
private final BackgroundBox backgroundOrigin;
86+
8387
/**
84-
* Creates a new {@link BackgroundImage} instance.
88+
* Creates a copy of passed {@link BackgroundImage} instance.
8589
*
86-
* @param image background-image property. {@link PdfXObject} instance.
87-
* @param repeat background-repeat property. {@link BackgroundRepeat} instance.
88-
* @param position background-position property. {@link BackgroundPosition} instance.
89-
* @param backgroundSize background-size property. {@link BackgroundSize} instance.
90-
* @param linearGradientBuilder background-image property. {@link AbstractLinearGradientBuilder} instance.
91-
* @param blendMode the image's blend mode. {@link BlendMode} instance.
90+
* @param backgroundImage {@link BackgroundImage} for cloning
9291
*/
93-
protected BackgroundImage(PdfXObject image, BackgroundRepeat repeat, BackgroundPosition position,
94-
BackgroundSize backgroundSize, AbstractLinearGradientBuilder linearGradientBuilder,
95-
BlendMode blendMode) {
96-
this.image = image;
97-
this.repeatX = !repeat.isNoRepeatOnXAxis();
98-
this.repeatY = !repeat.isNoRepeatOnYAxis();
99-
this.repeat = repeat;
100-
this.position = position;
101-
this.backgroundSize = backgroundSize;
102-
this.linearGradientBuilder = linearGradientBuilder;
103-
if (blendMode != null) {
104-
this.blendMode = blendMode;
105-
}
92+
public BackgroundImage(BackgroundImage backgroundImage) {
93+
this(backgroundImage.getImage() == null ? (PdfXObject) backgroundImage.getForm() : backgroundImage.getImage(),
94+
backgroundImage.getRepeat(),
95+
backgroundImage.getBackgroundPosition(),
96+
backgroundImage.getBackgroundSize(),
97+
backgroundImage.getLinearGradientBuilder(),
98+
backgroundImage.getBlendMode(),
99+
backgroundImage.getBackgroundClip(),
100+
backgroundImage.getBackgroundOrigin());
101+
repeatX = backgroundImage.isRepeatX();
102+
repeatY = backgroundImage.isRepeatY();
106103
}
107104

108105
/**
@@ -115,7 +112,8 @@ protected BackgroundImage(PdfXObject image, BackgroundRepeat repeat, BackgroundP
115112
*/
116113
@Deprecated
117114
public BackgroundImage(final PdfImageXObject image, final BackgroundRepeat repeat, final BlendMode blendMode) {
118-
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, blendMode);
115+
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, blendMode,
116+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
119117
}
120118

121119
/**
@@ -128,7 +126,8 @@ public BackgroundImage(final PdfImageXObject image, final BackgroundRepeat repea
128126
*/
129127
@Deprecated
130128
public BackgroundImage(final PdfFormXObject image, final BackgroundRepeat repeat, final BlendMode blendMode) {
131-
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, blendMode);
129+
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, blendMode,
130+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
132131
}
133132

134133
/**
@@ -140,7 +139,8 @@ public BackgroundImage(final PdfFormXObject image, final BackgroundRepeat repeat
140139
*/
141140
@Deprecated
142141
public BackgroundImage(final PdfImageXObject image, final BackgroundRepeat repeat) {
143-
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE);
142+
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE,
143+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
144144
}
145145

146146
/**
@@ -152,7 +152,8 @@ public BackgroundImage(final PdfImageXObject image, final BackgroundRepeat repea
152152
*/
153153
@Deprecated
154154
public BackgroundImage(final PdfFormXObject image, final BackgroundRepeat repeat) {
155-
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE);
155+
this(image, repeat, new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE,
156+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
156157
}
157158

158159
/**
@@ -163,7 +164,8 @@ public BackgroundImage(final PdfFormXObject image, final BackgroundRepeat repeat
163164
*/
164165
@Deprecated
165166
public BackgroundImage(final PdfImageXObject image) {
166-
this(image, new BackgroundRepeat(), new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE);
167+
this(image, new BackgroundRepeat(), new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE,
168+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
167169
}
168170

169171
/**
@@ -174,7 +176,8 @@ public BackgroundImage(final PdfImageXObject image) {
174176
*/
175177
@Deprecated
176178
public BackgroundImage(final PdfFormXObject image) {
177-
this(image, new BackgroundRepeat(), new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE);
179+
this(image, new BackgroundRepeat(), new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE,
180+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
178181
}
179182

180183
/**
@@ -188,8 +191,9 @@ public BackgroundImage(final PdfFormXObject image) {
188191
@Deprecated
189192
public BackgroundImage(final PdfImageXObject image, final boolean repeatX, final boolean repeatY) {
190193
this(image, new BackgroundRepeat(repeatX ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT,
191-
repeatY ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT), new BackgroundPosition(),
192-
new BackgroundSize(), null, DEFAULT_BLEND_MODE);
194+
repeatY ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT),
195+
new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE,
196+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
193197
}
194198

195199
/**
@@ -203,8 +207,9 @@ public BackgroundImage(final PdfImageXObject image, final boolean repeatX, final
203207
@Deprecated
204208
public BackgroundImage(final PdfFormXObject image, final boolean repeatX, final boolean repeatY) {
205209
this(image, new BackgroundRepeat(repeatX ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT,
206-
repeatY ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT), new BackgroundPosition(),
207-
new BackgroundSize(), null, DEFAULT_BLEND_MODE);
210+
repeatY ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT),
211+
new BackgroundPosition(), new BackgroundSize(), null, DEFAULT_BLEND_MODE,
212+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
208213
}
209214

210215
/**
@@ -230,7 +235,8 @@ public BackgroundImage(final AbstractLinearGradientBuilder linearGradientBuilder
230235
@Deprecated
231236
public BackgroundImage(final AbstractLinearGradientBuilder linearGradientBuilder, final BlendMode blendMode) {
232237
this(null, new BackgroundRepeat(BackgroundRepeatValue.NO_REPEAT), new BackgroundPosition(),
233-
new BackgroundSize(), linearGradientBuilder, blendMode);
238+
new BackgroundSize(), linearGradientBuilder, blendMode,
239+
BackgroundBox.BORDER_BOX, BackgroundBox.PADDING_BOX);
234240
}
235241

236242
public PdfImageXObject getImage() {
@@ -241,6 +247,37 @@ public PdfFormXObject getForm() {
241247
return image instanceof PdfFormXObject ? (PdfFormXObject) image : null;
242248
}
243249

250+
/**
251+
* Creates a new {@link BackgroundImage} instance.
252+
*
253+
* @param image background-image property. {@link PdfXObject} instance.
254+
* @param repeat background-repeat property. {@link BackgroundRepeat} instance.
255+
* @param position background-position property. {@link BackgroundPosition} instance.
256+
* @param backgroundSize background-size property. {@link BackgroundSize} instance.
257+
* @param linearGradientBuilder background-image property. {@link AbstractLinearGradientBuilder} instance.
258+
* @param blendMode the image's blend mode. {@link BlendMode} instance.
259+
* @param clip background-clip property. {@link BackgroundBox} instance.
260+
* @param origin background-origin property. {@link BackgroundBox} instance.
261+
*/
262+
private BackgroundImage(PdfXObject image, BackgroundRepeat repeat, BackgroundPosition position,
263+
BackgroundSize backgroundSize, AbstractLinearGradientBuilder linearGradientBuilder,
264+
BlendMode blendMode, BackgroundBox clip, BackgroundBox origin) {
265+
this.image = image;
266+
if (repeat != null) {
267+
this.repeatX = !repeat.isNoRepeatOnXAxis();
268+
this.repeatY = !repeat.isNoRepeatOnYAxis();
269+
}
270+
this.repeat = repeat;
271+
this.position = position;
272+
this.backgroundSize = backgroundSize;
273+
this.linearGradientBuilder = linearGradientBuilder;
274+
if (blendMode != null) {
275+
this.blendMode = blendMode;
276+
}
277+
this.backgroundClip = clip;
278+
this.backgroundOrigin = origin;
279+
}
280+
244281
/**
245282
* Gets background-position.
246283
*
@@ -319,10 +356,12 @@ public float getHeight() {
319356
public BackgroundRepeat getRepeat() {
320357
// Remove this if-blocks after removing repeatX and repeatY
321358
if (repeatX == repeat.isNoRepeatOnXAxis()) {
322-
repeat = new BackgroundRepeat(repeatX ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT, repeat.getYAxisRepeat());
359+
repeat = new BackgroundRepeat(repeatX ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT,
360+
repeat.getYAxisRepeat());
323361
}
324362
if (repeatY == repeat.isNoRepeatOnYAxis()) {
325-
repeat = new BackgroundRepeat(repeat.getXAxisRepeat(), repeatY ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT);
363+
repeat = new BackgroundRepeat(repeat.getXAxisRepeat(),
364+
repeatY ? BackgroundRepeatValue.REPEAT : BackgroundRepeatValue.NO_REPEAT);
326365
}
327366

328367
return repeat;
@@ -337,6 +376,24 @@ public BlendMode getBlendMode() {
337376
return blendMode;
338377
}
339378

379+
/**
380+
* Gets background-clip.
381+
*
382+
* @return {@link BackgroundBox}
383+
*/
384+
public BackgroundBox getBackgroundClip() {
385+
return backgroundClip;
386+
}
387+
388+
/**
389+
* Gets background-origin.
390+
*
391+
* @return {@link BackgroundBox}
392+
*/
393+
public BackgroundBox getBackgroundOrigin() {
394+
return backgroundOrigin;
395+
}
396+
340397
/**
341398
* {@link BackgroundImage} builder class.
342399
*/
@@ -348,6 +405,8 @@ public static class Builder {
348405
private BackgroundRepeat repeat = new BackgroundRepeat();
349406
private BlendMode blendMode = DEFAULT_BLEND_MODE;
350407
private BackgroundSize backgroundSize = new BackgroundSize();
408+
private BackgroundBox clip = BackgroundBox.BORDER_BOX;
409+
private BackgroundBox origin = BackgroundBox.PADDING_BOX;
351410

352411
public Builder() {
353412
}
@@ -429,13 +488,36 @@ public Builder setBackgroundSize(BackgroundSize backgroundSize) {
429488
return this;
430489
}
431490

491+
/**
492+
* Sets background-clip.
493+
*
494+
* @param clip {@link BackgroundBox} to be set.
495+
* @return this {@link Builder}.
496+
*/
497+
public Builder setBackgroundClip(BackgroundBox clip) {
498+
this.clip = clip;
499+
return this;
500+
}
501+
502+
/**
503+
* Sets background-origin.
504+
*
505+
* @param origin {@link BackgroundBox} to be set.
506+
* @return this {@link Builder}.
507+
*/
508+
public Builder setBackgroundOrigin(BackgroundBox origin) {
509+
this.origin = origin;
510+
return this;
511+
}
512+
432513
/**
433514
* Builds new {@link BackgroundImage} using set fields.
434515
*
435516
* @return new {@link BackgroundImage}.
436517
*/
437518
public BackgroundImage build() {
438-
return new BackgroundImage(image, repeat, position, backgroundSize, linearGradientBuilder, blendMode);
519+
return new BackgroundImage(image, repeat, position, backgroundSize, linearGradientBuilder, blendMode, clip,
520+
origin);
439521
}
440522
}
441523
}

0 commit comments

Comments
 (0)