Skip to content

Commit 32c7a20

Browse files
Veronika Lisovskayaitext-teamcity
authored andcommitted
Add FloatExample test
Thanks to Richard Cohn, who provided the code and image samples Autoported commit. Original commit hash: [58207a6e0]
1 parent f02f184 commit 32c7a20

File tree

8 files changed

+272
-1
lines changed

8 files changed

+272
-1
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2017 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+
using System;
45+
using iText.IO.Image;
46+
using iText.Kernel.Colors;
47+
using iText.Kernel.Geom;
48+
using iText.Kernel.Pdf;
49+
using iText.Kernel.Utils;
50+
using iText.Layout;
51+
using iText.Layout.Borders;
52+
using iText.Layout.Element;
53+
using iText.Layout.Properties;
54+
using iText.Test;
55+
56+
namespace iText.Layout.Renderer {
57+
public class FloatExampleTest : ExtendedITextTest {
58+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
59+
.CurrentContext.TestDirectory) + "/resources/itext/layout/FloatExampleTest/";
60+
61+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
62+
+ "/test/itext/layout/FloatExampleTest/";
63+
64+
private static readonly Color imageBorderColor = ColorConstants.LIGHT_GRAY;
65+
66+
private const float BORDER_MARGIN = 5f;
67+
68+
private const float IMAGE_BORDER_WIDTH = 15f;
69+
70+
private const float DIV_BORDER_WIDTH = 1f;
71+
72+
[NUnit.Framework.OneTimeSetUp]
73+
public static void BeforeClass() {
74+
CreateDestinationFolder(destinationFolder);
75+
}
76+
77+
/// <exception cref="System.IO.IOException"/>
78+
/// <exception cref="System.Exception"/>
79+
[NUnit.Framework.Test]
80+
public virtual void FloatMaxWidthTest01() {
81+
/* This test illustrate behaviour of images with different width and mas_width properties, that have
82+
there is text paragraph below image,
83+
shown examples, wrapped and not wrapped in divs
84+
Divs have property CLEAR = BOTH, and different values of FLOAT
85+
*/
86+
String cmpFileName = sourceFolder + "cmp_floatMaxWidthTest01.pdf";
87+
String outFile = destinationFolder + "floatMaxWidthTest01.pdf";
88+
// defined range is 0..3
89+
int firstImage = 0;
90+
int lastImage = 1;
91+
//Initialize PDF writer
92+
PdfWriter writer = new PdfWriter(outFile);
93+
//Initialize PDF document
94+
PdfDocument pdf = new PdfDocument(writer);
95+
// Initialize document
96+
Document document = new Document(pdf);
97+
pdf.SetTagged();
98+
// divWidthProperty, divWidth are n/a when not wrapping image in a div
99+
document.Add(new Paragraph("IMAGE IS NOT WRAPPED IN A DIV.\n"));
100+
document.Add(new Paragraph("Actual width of image -- no explicit width, no max.\n"));
101+
AddContent(document, false, 0, null, 0, null, ClearPropertyValue.BOTH, firstImage, lastImage);
102+
document.Add(new AreaBreak());
103+
document.Add(new Paragraph("Width < actual width.\n"));
104+
AddContent(document, false, Property.WIDTH, new UnitValue(UnitValue.PERCENT, 30f), 0, null, ClearPropertyValue
105+
.BOTH, firstImage, lastImage);
106+
document.Add(new AreaBreak());
107+
document.Add(new Paragraph("Max width < actual width.\n"));
108+
AddContent(document, false, Property.MAX_WIDTH, new UnitValue(UnitValue.PERCENT, 30f), 0, null, ClearPropertyValue
109+
.BOTH, firstImage, lastImage);
110+
document.Add(new AreaBreak());
111+
document.Add(new Paragraph("Max width > actual width.\n"));
112+
AddContent(document, false, Property.MAX_WIDTH, new UnitValue(UnitValue.PERCENT, 60f), 0, null, ClearPropertyValue
113+
.BOTH, firstImage, lastImage);
114+
document.Add(new AreaBreak());
115+
// Image wrapped in div
116+
document.Add(new Paragraph("IMAGE IS WRAPPED IN A DIV.\n"));
117+
// Width of Paragraph inside Div if width of parent
118+
document.Add(new Paragraph("No explicit width or max: Non-floating text width is parent width.\n"));
119+
AddContent(document, true, 0, null, 0, null, ClearPropertyValue.BOTH, firstImage, lastImage);
120+
document.Add(new AreaBreak());
121+
document.Add(new Paragraph("Bug: Non-floating text width is parent width (limited by max).\n"));
122+
AddContent(document, true, Property.MAX_WIDTH, new UnitValue(UnitValue.PERCENT, 100f), Property.WIDTH, new
123+
UnitValue(UnitValue.PERCENT, 30f), ClearPropertyValue.BOTH, firstImage, lastImage);
124+
document.Add(new AreaBreak());
125+
document.Add(new Paragraph("Max width < actual width.\n"));
126+
AddContent(document, true, Property.MAX_WIDTH, new UnitValue(UnitValue.PERCENT, 100f), Property.MAX_WIDTH,
127+
new UnitValue(UnitValue.PERCENT, 30f), ClearPropertyValue.BOTH, firstImage, lastImage);
128+
document.Add(new AreaBreak());
129+
document.Add(new Paragraph("Bug: Non-floating text width is parent width (limited by max).\nMax width > actual width.\n"
130+
));
131+
AddContent(document, true, Property.MAX_WIDTH, new UnitValue(UnitValue.PERCENT, 100f), Property.MAX_WIDTH,
132+
new UnitValue(UnitValue.PERCENT, 60f), ClearPropertyValue.BOTH, firstImage, lastImage);
133+
document.Close();
134+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFileName, destinationFolder,
135+
"diff01_"));
136+
}
137+
138+
/// <exception cref="System.IO.IOException"/>
139+
/// <exception cref="System.Exception"/>
140+
[NUnit.Framework.Test]
141+
public virtual void FloatMaxWidthTest02() {
142+
/* This test illustrate behaviour of images, wrapped in Div containers, that have paragraph below image
143+
Divs have property CLEAR = BOTH, and different values of FLOAT
144+
Text in divs has HorizontalAlignment.CENTER
145+
Now this example shows several problems with FLOAT:
146+
* 1. image and text under image are splitted to different pages
147+
* 2. order of images is displayed ignoring CLEAR = BOTH property (last div before previous, not after)
148+
* 3. incorrect line wrapping (paragraph and div overlaps)
149+
* */
150+
//TODO: update after DEVSIX-1437 fix (Fix edge cases for floats splitting)
151+
//TODO: update after DEVSIX-1269 fix (Float property: support correct lines wrapping around floats that are under the lines)
152+
String cmpFileName = sourceFolder + "cmp_floatMaxWidthTest02.pdf";
153+
String outFile = destinationFolder + "floatMaxWidthTest02.pdf";
154+
// defined range is 0..3
155+
int firstImage = 0;
156+
int lastImage = 2;
157+
//Initialize PDF writer
158+
PdfWriter writer = new PdfWriter(outFile);
159+
//Initialize PDF document with non-default size
160+
PdfDocument pdf = new PdfDocument(writer);
161+
pdf.SetDefaultPageSize(new PageSize(new Rectangle(537, 800)));
162+
// Initialize document
163+
Document document = new Document(pdf);
164+
pdf.SetTagged();
165+
document.Add(new Paragraph("IMAGE IS WRAPPED IN A DIV.\n"));
166+
document.Add(new Paragraph("No explicit width or max: Non-floating text width is parent width.\n"));
167+
AddContent(document, true, 0, null, 0, null, ClearPropertyValue.BOTH, firstImage, lastImage);
168+
document.Close();
169+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFileName, destinationFolder,
170+
"diff01_"));
171+
}
172+
173+
/// <exception cref="System.UriFormatException"/>
174+
private void AddContent(Document document, bool wrapImages, int imageWidthProperty, UnitValue imageWidth,
175+
int divWidthProperty, UnitValue divWidth, ClearPropertyValue? clearValue, int firstImage, int lastImage
176+
) {
177+
FloatExampleTest.ImageProperties[] images = new FloatExampleTest.ImageProperties[4];
178+
images[0] = new FloatExampleTest.ImageProperties(this, FloatPropertyValue.NONE, clearValue, HorizontalAlignment
179+
.CENTER);
180+
images[1] = new FloatExampleTest.ImageProperties(this, FloatPropertyValue.RIGHT, clearValue, HorizontalAlignment
181+
.CENTER);
182+
images[2] = new FloatExampleTest.ImageProperties(this, FloatPropertyValue.LEFT, clearValue, HorizontalAlignment
183+
.CENTER);
184+
images[3] = new FloatExampleTest.ImageProperties(this, FloatPropertyValue.NONE, clearValue, HorizontalAlignment
185+
.CENTER);
186+
Paragraph paragraph = new Paragraph().Add("Four images followed by two paragraphs.\n");
187+
if (wrapImages) {
188+
String s = "Each image is wrapped in a div.\n";
189+
s += "All divs specify CLEAR = " + clearValue;
190+
if (divWidthProperty > 0) {
191+
s += ", " + ((divWidthProperty == Property.WIDTH) ? "WIDTH" : "MAX_WIDTH") + "= " + divWidth;
192+
}
193+
if (imageWidthProperty > 0) {
194+
s += ".\nAll images specify " + ((imageWidthProperty == Property.WIDTH) ? "WIDTH" : "MAX_WIDTH") + " = " +
195+
imageWidth;
196+
}
197+
paragraph.Add(s + ".\n");
198+
}
199+
else {
200+
String s = "All images specify CLEAR = " + clearValue;
201+
if (imageWidthProperty > 0) {
202+
s += ", " + ((imageWidthProperty == Property.WIDTH) ? "WIDTH" : "MAX_WIDTH") + "= " + imageWidth;
203+
}
204+
paragraph.Add(s + ".\n");
205+
}
206+
for (int i = firstImage; i <= lastImage; i++) {
207+
paragraph.Add((wrapImages ? "Div" : "Image") + " " + (i) + ": " + images[i] + "\n");
208+
}
209+
document.Add(paragraph);
210+
for (int i = firstImage; i <= lastImage; i++) {
211+
int pictNumber = i + 1;
212+
iText.Layout.Element.Image image = new Image(ImageDataFactory.Create(sourceFolder + pictNumber + ".png")).
213+
SetBorder(new SolidBorder(imageBorderColor, IMAGE_BORDER_WIDTH)).SetHorizontalAlignment(images[i].horizontalAlignment
214+
);
215+
if (wrapImages) {
216+
Div div = new Div().SetBorder(new SolidBorder(DIV_BORDER_WIDTH)).SetMargins(BORDER_MARGIN, 0, BORDER_MARGIN
217+
, BORDER_MARGIN);
218+
div.SetHorizontalAlignment(images[i].horizontalAlignment);
219+
div.SetProperty(Property.CLEAR, images[i].clearPropertyValue);
220+
div.SetProperty(Property.FLOAT, images[i].floatPropertyValue);
221+
if (divWidthProperty > 0) {
222+
div.SetProperty(divWidthProperty, divWidth);
223+
}
224+
if (imageWidthProperty > 0) {
225+
image.SetProperty(imageWidthProperty, imageWidth);
226+
}
227+
div.Add(image);
228+
div.Add(new Paragraph("Figure for Div" + i + ": This is longer text that wraps This is longer text that wraps"
229+
).SetTextAlignment(TextAlignment.CENTER)).SetBold();
230+
document.Add(div);
231+
}
232+
else {
233+
image.SetMargins(BORDER_MARGIN, 0, BORDER_MARGIN, BORDER_MARGIN);
234+
image.SetProperty(Property.CLEAR, images[i].clearPropertyValue);
235+
image.SetProperty(Property.FLOAT, images[i].floatPropertyValue);
236+
if (imageWidthProperty > 0) {
237+
image.SetProperty(imageWidthProperty, imageWidth);
238+
}
239+
document.Add(image);
240+
}
241+
}
242+
document.Add(new Paragraph("The following outline is provided as an over-view of and topical guide to Zambia:"
243+
));
244+
document.Add(new Paragraph("Zambia – landlocked sovereign country located in Southern Africa.[1] Zambia has been inhabited for thousands of years by hunter-gatherers and migrating tribes. After sporadic visits by European explorers starting in the 18th century, Zambia was gradually claimed and occupied by the British as protectorate of Northern Rhodesia towards the end of the nineteenth century. On 24 October 1964, the protectorate gained independence with the new name of Zambia, derived from the Zam-bezi river which flows through the country. After independence the country moved towards a system of one party rule with Kenneth Kaunda as president. Kaunda dominated Zambian politics until multiparty elections were held in 1991."
245+
));
246+
}
247+
248+
private class ImageProperties {
249+
internal FloatPropertyValue? floatPropertyValue;
250+
251+
internal ClearPropertyValue? clearPropertyValue;
252+
253+
internal HorizontalAlignment? horizontalAlignment;
254+
255+
internal ImageProperties(FloatExampleTest _enclosing, FloatPropertyValue? floatPropertyValue, ClearPropertyValue?
256+
clearPropertyValue, HorizontalAlignment? horizontalAlignment) {
257+
this._enclosing = _enclosing;
258+
this.floatPropertyValue = floatPropertyValue;
259+
this.clearPropertyValue = clearPropertyValue;
260+
this.horizontalAlignment = horizontalAlignment;
261+
}
262+
263+
public override String ToString() {
264+
return "float=" + this.floatPropertyValue + ", clear=" + this.clearPropertyValue + ", horiz_align=" + this
265+
.horizontalAlignment;
266+
}
267+
268+
private readonly FloatExampleTest _enclosing;
269+
}
270+
}
271+
}
2.28 KB
Loading
5.97 KB
Loading
3.18 KB
Loading
2.14 KB
Loading

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f30a01fa2e6ba1c1893b0cbe16d40f319b14e42c
1+
58207a6e0bf46d2d6333a2e8e44db4df58f7052b

0 commit comments

Comments
 (0)