Skip to content

Commit 9117b3b

Browse files
committed
Merge branch 'feature/min_max_width' into develop
Autoported commit. Original commit hash: [3bb0c17e7]
2 parents ec7f596 + e82a285 commit 9117b3b

File tree

13 files changed

+427
-80
lines changed

13 files changed

+427
-80
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2017 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using System;
44+
using iText.IO.Image;
45+
using iText.Kernel.Pdf;
46+
using iText.Kernel.Pdf.Xobject;
47+
using iText.Kernel.Utils;
48+
using iText.Layout;
49+
using iText.Layout.Element;
50+
using iText.Layout.Minmaxwidth;
51+
using iText.Layout.Properties;
52+
using iText.Test;
53+
54+
namespace iText.Layout.Renderer {
55+
public class ImageWidthTest : ExtendedITextTest {
56+
private const double EPSILON = 0.01;
57+
58+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
59+
+ "/test/itext/layout/ImageWidthTest/";
60+
61+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
62+
.CurrentContext.TestDirectory) + "/resources/itext/layout/ImageWidthTest/";
63+
64+
public static readonly String imageFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
65+
.CurrentContext.TestDirectory) + "/resources/itext/layout/ImageTest/";
66+
67+
[NUnit.Framework.OneTimeSetUp]
68+
public static void BeforeClass() {
69+
CreateDestinationFolder(destinationFolder);
70+
}
71+
72+
/// <exception cref="System.IO.IOException"/>
73+
/// <exception cref="System.Exception"/>
74+
[NUnit.Framework.Test]
75+
public virtual void ImageWidthTest01() {
76+
String outFileName = destinationFolder + "imageWidthTest01.pdf";
77+
String cmpFileName = sourceFolder + "cmp_imageWidthTest01.pdf";
78+
PdfWriter writer = new PdfWriter(outFileName);
79+
PdfDocument pdfDoc = new PdfDocument(writer);
80+
Document doc = new Document(pdfDoc);
81+
doc.Add(new Paragraph(new Text("First Line")));
82+
Paragraph p = new Paragraph();
83+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "Desert.jpg"));
84+
iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject);
85+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(100));
86+
p.Add(image);
87+
doc.Add(p);
88+
doc.Add(new Paragraph(new Text("Second Line")));
89+
p = new Paragraph();
90+
xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "itis.jpg"));
91+
image = new iText.Layout.Element.Image(xObject);
92+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(100));
93+
p.Add(image);
94+
doc.Add(p);
95+
doc.Add(new Paragraph(new Text("Third Line")));
96+
p = new Paragraph();
97+
xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "Desert.jpg"));
98+
image = new iText.Layout.Element.Image(xObject);
99+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(100));
100+
image.SetProperty(Property.MAX_HEIGHT, 200f);
101+
p.Add(image);
102+
doc.Add(p);
103+
doc.Add(new Paragraph(new Text("Fourth Line")));
104+
p = new Paragraph();
105+
xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "itis.jpg"));
106+
image = new iText.Layout.Element.Image(xObject);
107+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePointValue(100));
108+
p.Add(image);
109+
doc.Add(p);
110+
doc.Add(new Paragraph(new Text("Fifth Line")));
111+
doc.Close();
112+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
113+
, "diff"));
114+
}
115+
116+
/// <exception cref="System.IO.IOException"/>
117+
/// <exception cref="System.Exception"/>
118+
[NUnit.Framework.Test]
119+
public virtual void ImageWidthTest02() {
120+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "Desert.jpg"));
121+
iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject);
122+
ImageRenderer renderer = new ImageRenderer(image);
123+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePointValue(50));
124+
MinMaxWidth minMaxWidth = renderer.GetMinMaxWidth(5000);
125+
NUnit.Framework.Assert.AreEqual(50.0, minMaxWidth.GetMaxWidth(), EPSILON);
126+
NUnit.Framework.Assert.AreEqual(0.0, minMaxWidth.GetMaxWidth() - minMaxWidth.GetMinWidth(), EPSILON);
127+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(50));
128+
minMaxWidth = renderer.GetMinMaxWidth(5000);
129+
NUnit.Framework.Assert.AreEqual(1024.0, minMaxWidth.GetMaxWidth(), EPSILON);
130+
minMaxWidth = renderer.GetMinMaxWidth(500);
131+
NUnit.Framework.Assert.AreEqual(250.0, minMaxWidth.GetMaxWidth(), EPSILON);
132+
image.SetProperty(Property.MAX_HEIGHT, 100f);
133+
minMaxWidth = renderer.GetMinMaxWidth(500);
134+
NUnit.Framework.Assert.AreEqual(100.0 * 1024.0 / 768.0, minMaxWidth.GetMaxWidth(), EPSILON);
135+
image = new iText.Layout.Element.Image(xObject);
136+
renderer = new ImageRenderer(image);
137+
image.SetProperty(Property.MIN_WIDTH, UnitValue.CreatePointValue(2000));
138+
image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePointValue(3000));
139+
minMaxWidth = renderer.GetMinMaxWidth(5000);
140+
NUnit.Framework.Assert.AreEqual(2000.0, minMaxWidth.GetMaxWidth(), EPSILON);
141+
NUnit.Framework.Assert.AreEqual(0.0, minMaxWidth.GetMaxWidth() - minMaxWidth.GetMinWidth(), EPSILON);
142+
image.SetProperty(Property.MIN_HEIGHT, 100f);
143+
image.SetProperty(Property.HEIGHT, 100f);
144+
minMaxWidth = renderer.GetMinMaxWidth(5000);
145+
NUnit.Framework.Assert.AreEqual(100.0 * 1024.0 / 768.0, minMaxWidth.GetMaxWidth(), EPSILON);
146+
}
147+
}
148+
}
Binary file not shown.

itext/itext.layout/itext/layout/element/BlockElement.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ public virtual T SetMinHeight(float minHeight) {
353353
return (T)(Object)this;
354354
}
355355

356+
public virtual T SetMaxWidth(float maxWidth) {
357+
SetProperty(Property.MAX_WIDTH, maxWidth);
358+
return (T)(Object)this;
359+
}
360+
361+
public virtual T SetMinWidth(float minWidth) {
362+
SetProperty(Property.MIN_WIDTH, minWidth);
363+
return (T)(Object)this;
364+
}
365+
356366
public abstract AccessibilityProperties GetAccessibilityProperties();
357367

358368
public abstract PdfName GetRole();

itext/itext.layout/itext/layout/element/Image.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ public virtual iText.Layout.Element.Image SetMinHeight(float minHeight) {
452452
return (iText.Layout.Element.Image)(Object)this;
453453
}
454454

455+
public virtual iText.Layout.Element.Image SetMaxWidth(float maxWidth) {
456+
SetProperty(Property.MAX_WIDTH, maxWidth);
457+
return (iText.Layout.Element.Image)(Object)this;
458+
}
459+
460+
public virtual iText.Layout.Element.Image SetMinWidth(float minWidth) {
461+
SetProperty(Property.MIN_WIDTH, minWidth);
462+
return (iText.Layout.Element.Image)(Object)this;
463+
}
464+
455465
/// <summary>Gets scaled width of the image.</summary>
456466
/// <returns>the current scaled width</returns>
457467
public virtual float GetImageScaledWidth() {

itext/itext.layout/itext/layout/margincollapse/MarginsCollapseHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ private static bool RendererIsFloated(IRenderer renderer) {
531531

532532
private static float GetModelTopMargin(IRenderer renderer) {
533533
float? margin = renderer.GetModelElement().GetProperty<float?>(Property.MARGIN_TOP);
534-
return margin != null ? (float)margin : 0;
534+
// TODO Concerning "renderer instanceof CellRenderer" check: may be try to apply more general solution in future
535+
return margin != null && !(renderer is CellRenderer) ? (float)margin : 0;
535536
}
536537

537538
private static void IgnoreModelTopMargin(IRenderer renderer) {
@@ -544,7 +545,8 @@ private static void OverrideModelTopMargin(IRenderer renderer, float collapsedMa
544545

545546
private static float GetModelBottomMargin(IRenderer renderer) {
546547
float? margin = renderer.GetModelElement().GetProperty<float?>(Property.MARGIN_BOTTOM);
547-
return margin != null ? (float)margin : 0;
548+
// TODO Concerning "renderer instanceof CellRenderer" check: may be try to apply more general solution in future
549+
return margin != null && !(renderer is CellRenderer) ? (float)margin : 0;
548550
}
549551

550552
private static void IgnoreModelBottomMargin(IRenderer renderer) {

itext/itext.layout/itext/layout/properties/Property.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,12 @@ private Property() {
216216

217217
public const int MAX_HEIGHT = 84;
218218

219+
public const int MAX_WIDTH = 107;
220+
219221
public const int MIN_HEIGHT = 85;
220222

223+
public const int MIN_WIDTH = 108;
224+
221225
public const int OPACITY = 92;
222226

223227
public const int OVERFLOW = 102;

0 commit comments

Comments
 (0)