Skip to content

Commit 2b9abed

Browse files
Artyom YanchevskyiText-CI
authored andcommitted
Add background size support
DEVSIX-1708 Autoported commit. Original commit hash: [ee4240999]
1 parent 6ef3dbf commit 2b9abed

16 files changed

+727
-19
lines changed

itext.tests/itext.layout.tests/itext/layout/BackgroundImageTest.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,170 @@ public virtual void BackgroundImageForText() {
167167
, "diff"));
168168
}
169169

170+
[NUnit.Framework.Test]
171+
public virtual void BackgroundImageWithPercentWidth() {
172+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
173+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
174+
));
175+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
176+
Build();
177+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithPercentWidth.pdf";
178+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithPercentWidth.pdf";
179+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
180+
)))) {
181+
Document doc = new Document(pdfDocument);
182+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
183+
);
184+
textElement.SetFontSize(50);
185+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(UnitValue.CreatePercentValue(30), null);
186+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
187+
doc.Add(new Paragraph(textElement));
188+
}
189+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
190+
, "diff"));
191+
}
192+
193+
[NUnit.Framework.Test]
194+
public virtual void BackgroundImageWithPercentHeight() {
195+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
196+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
197+
));
198+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
199+
Build();
200+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithPercentHeight.pdf";
201+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithPercentHeight.pdf";
202+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
203+
)))) {
204+
Document doc = new Document(pdfDocument);
205+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
206+
);
207+
textElement.SetFontSize(50);
208+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(null, UnitValue.CreatePercentValue(30));
209+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
210+
doc.Add(new Paragraph(textElement));
211+
}
212+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
213+
, "diff"));
214+
}
215+
216+
[NUnit.Framework.Test]
217+
public virtual void BackgroundImageWithPercentHeightAndWidth() {
218+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
219+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
220+
));
221+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
222+
Build();
223+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithPercentHeightAndWidth.pdf";
224+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithPercentHeightAndWidth.pdf";
225+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
226+
)))) {
227+
Document doc = new Document(pdfDocument);
228+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
229+
);
230+
textElement.SetFontSize(50);
231+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(UnitValue.CreatePercentValue(20), UnitValue.
232+
CreatePercentValue(20));
233+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
234+
doc.Add(new Paragraph(textElement));
235+
}
236+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
237+
, "diff"));
238+
}
239+
240+
[NUnit.Framework.Test]
241+
public virtual void BackgroundImageWithPointWidth() {
242+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
243+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
244+
));
245+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
246+
Build();
247+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithPointWidth.pdf";
248+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithPointWidth.pdf";
249+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
250+
)))) {
251+
Document doc = new Document(pdfDocument);
252+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
253+
);
254+
textElement.SetFontSize(50);
255+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(UnitValue.CreatePointValue(15), null);
256+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
257+
doc.Add(new Paragraph(textElement));
258+
}
259+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
260+
, "diff"));
261+
}
262+
263+
[NUnit.Framework.Test]
264+
public virtual void BackgroundImageWithPointHeight() {
265+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
266+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
267+
));
268+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
269+
Build();
270+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithPointHeight.pdf";
271+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithPointHeight.pdf";
272+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
273+
)))) {
274+
Document doc = new Document(pdfDocument);
275+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
276+
);
277+
textElement.SetFontSize(50);
278+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(null, UnitValue.CreatePointValue(20));
279+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
280+
doc.Add(new Paragraph(textElement));
281+
}
282+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
283+
, "diff"));
284+
}
285+
286+
[NUnit.Framework.Test]
287+
public virtual void BackgroundImageWithPointHeightAndWidth() {
288+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
289+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
290+
));
291+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
292+
Build();
293+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithPointHeightAndWidth.pdf";
294+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithPointHeightAndWidth.pdf";
295+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
296+
)))) {
297+
Document doc = new Document(pdfDocument);
298+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
299+
);
300+
textElement.SetFontSize(50);
301+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(UnitValue.CreatePointValue(50), UnitValue.CreatePointValue
302+
(100));
303+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
304+
doc.Add(new Paragraph(textElement));
305+
}
306+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
307+
, "diff"));
308+
}
309+
310+
[NUnit.Framework.Test]
311+
public virtual void BackgroundImageWithLowWidthAndHeight() {
312+
//TODO DEVSIX-4370 first pixel near the border is redrawn by another image
313+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
314+
));
315+
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
316+
Build();
317+
String outFileName = DESTINATION_FOLDER + "backgroundImageWithLowWidthAndHeight.pdf";
318+
String cmpFileName = SOURCE_FOLDER + "cmp_backgroundImageWithLowWidthAndHeight.pdf";
319+
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create
320+
)))) {
321+
Document doc = new Document(pdfDocument);
322+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
323+
);
324+
textElement.SetFontSize(50);
325+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToValues(UnitValue.CreatePointValue(-1), UnitValue.CreatePointValue
326+
(-1));
327+
textElement.SetProperty(Property.BACKGROUND_IMAGE, backgroundImage);
328+
doc.Add(new Paragraph(textElement));
329+
}
330+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
331+
, "diff"));
332+
}
333+
170334
[NUnit.Framework.Test]
171335
public virtual void BackgroundImageWithoutRepeatXTest() {
172336
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using iText.Test;
24+
25+
namespace iText.Layout.Properties {
26+
public class BackgroundSizeTest : ExtendedITextTest {
27+
[NUnit.Framework.Test]
28+
public virtual void ConstructorTest() {
29+
BackgroundSize size = new BackgroundSize();
30+
NUnit.Framework.Assert.IsFalse(size.IsContain());
31+
NUnit.Framework.Assert.IsFalse(size.IsCover());
32+
NUnit.Framework.Assert.IsNull(size.GetBackgroundWidthSize());
33+
NUnit.Framework.Assert.IsNull(size.GetBackgroundHeightSize());
34+
}
35+
36+
[NUnit.Framework.Test]
37+
public virtual void ClearAndSetToCoverTest() {
38+
BackgroundSize size = new BackgroundSize();
39+
size.SetBackgroundSizeToValues(UnitValue.CreatePointValue(10), UnitValue.CreatePointValue(10));
40+
size.SetBackgroundSizeToCover();
41+
NUnit.Framework.Assert.IsFalse(size.IsContain());
42+
NUnit.Framework.Assert.IsTrue(size.IsCover());
43+
NUnit.Framework.Assert.IsNull(size.GetBackgroundWidthSize());
44+
NUnit.Framework.Assert.IsNull(size.GetBackgroundHeightSize());
45+
}
46+
47+
[NUnit.Framework.Test]
48+
public virtual void ClearAndSetToContainTest() {
49+
BackgroundSize size = new BackgroundSize();
50+
size.SetBackgroundSizeToValues(UnitValue.CreatePointValue(10), UnitValue.CreatePointValue(10));
51+
size.SetBackgroundSizeToContain();
52+
NUnit.Framework.Assert.IsTrue(size.IsContain());
53+
NUnit.Framework.Assert.IsFalse(size.IsCover());
54+
NUnit.Framework.Assert.IsNull(size.GetBackgroundWidthSize());
55+
NUnit.Framework.Assert.IsNull(size.GetBackgroundHeightSize());
56+
}
57+
}
58+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using iText.IO.Image;
25+
using iText.Kernel.Pdf.Xobject;
26+
using iText.Layout.Properties;
27+
using iText.Test;
28+
29+
namespace iText.Layout.Renderer {
30+
public class BackgroundSizeCalculationUtilUnitTest : ExtendedITextTest {
31+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
32+
.CurrentContext.TestDirectory) + "/resources/itext/layout/BackgroundImageTest/";
33+
34+
private const float delta = 0.0001f;
35+
36+
[NUnit.Framework.Test]
37+
public virtual void CalculateImageSizeTest() {
38+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
39+
));
40+
BackgroundImage backgroundImage = new BackgroundImage(xObject);
41+
float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
42+
300f);
43+
NUnit.Framework.Assert.AreEqual(new float[] { 45f, 45f }, widthAndHeight);
44+
}
45+
46+
[NUnit.Framework.Test]
47+
public virtual void CalculateImageSizeWithCoverPropertyTest() {
48+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
49+
));
50+
BackgroundImage backgroundImage = new BackgroundImage(xObject);
51+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToCover();
52+
float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
53+
300f);
54+
NUnit.Framework.Assert.AreEqual(new float[] { 300f, 300f }, widthAndHeight);
55+
}
56+
57+
[NUnit.Framework.Test]
58+
public virtual void CalculateSizeWithContainPropertyTest() {
59+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
60+
));
61+
BackgroundImage backgroundImage = new BackgroundImage(xObject);
62+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToContain();
63+
float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
64+
300f);
65+
NUnit.Framework.Assert.AreEqual(new float[] { 200f, 200.000015f }, widthAndHeight);
66+
}
67+
68+
[NUnit.Framework.Test]
69+
public virtual void CalculateSizeWithContainAndImageWeightMoreThatHeightTest() {
70+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
71+
BackgroundImage backgroundImage = new BackgroundImage(xObject);
72+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToContain();
73+
float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
74+
300f);
75+
NUnit.Framework.Assert.AreEqual(new float[] { 200f, 112.5f }, widthAndHeight);
76+
}
77+
78+
[NUnit.Framework.Test]
79+
public virtual void CalculateSizeWithCoverAndImageWeightMoreThatHeightTest() {
80+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
81+
BackgroundImage backgroundImage = new BackgroundImage(xObject);
82+
backgroundImage.GetBackgroundSize().SetBackgroundSizeToCover();
83+
float[] widthAndHeight = BackgroundSizeCalculationUtil.CalculateBackgroundImageSize(backgroundImage, 200f,
84+
300f);
85+
NUnit.Framework.Assert.AreEqual(new float[] { 533.3333f, 300f }, widthAndHeight);
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)