Skip to content

Commit 52792d5

Browse files
AnhelinaMiText-CI
authored andcommitted
Make 'floatMultiplier' in ClipperBridge non-static configuration
DEVSIX-5770 DEVSIX-1279 Autoported commit. Original commit hash: [1bb85b3f2]
1 parent 2b2ca05 commit 52792d5

File tree

5 files changed

+192
-43
lines changed

5 files changed

+192
-43
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/canvas/parser/PdfContentExtractionTest.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,38 @@ You should have received a copy of the GNU Affero General Public License
2929
namespace iText.Kernel.Pdf.Canvas.Parser {
3030
[NUnit.Framework.Category("IntegrationTest")]
3131
public class PdfContentExtractionTest : ExtendedITextTest {
32-
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
32+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
3333
.CurrentContext.TestDirectory) + "/resources/itext/kernel/parser/PdfContentExtractionTest/";
3434

3535
[NUnit.Framework.Test]
3636
public virtual void ContentExtractionInDocWithBigCoordinatesTest() {
37-
//TODO: remove the expected exception construct once the issue is fixed (DEVSIX-1279)
38-
String inputFileName = sourceFolder + "docWithBigCoordinates.pdf";
39-
//In this document the CTM shrinks coordinates and this coordinates are large numbers.
37+
String inputFileName = SOURCE_FOLDER + "docWithBigCoordinates.pdf";
38+
// In this document the CTM shrinks coordinates and these coordinates are large numbers.
4039
// At the moment creation of this test clipper has a problem with handling large numbers
4140
// since internally it deals with integers and has to multiply large numbers even more
4241
// for internal purposes
43-
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFileName));
44-
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
45-
Exception e = NUnit.Framework.Assert.Catch(typeof(ClipperException), () => contentParser.ProcessContent(1,
46-
new LocationTextExtractionStrategy()));
47-
NUnit.Framework.Assert.AreEqual(ClipperExceptionConstant.COORDINATE_OUTSIDE_ALLOWED_RANGE, e.Message);
42+
using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFileName))) {
43+
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
44+
NUnit.Framework.Assert.DoesNotThrow(() => contentParser.ProcessContent(1, new LocationTextExtractionStrategy
45+
()));
46+
}
47+
}
48+
49+
[NUnit.Framework.Test]
50+
public virtual void ContentExtractionInDocWithStaticFloatMultiplierTest() {
51+
String inputFileName = SOURCE_FOLDER + "docWithBigCoordinates.pdf";
52+
// In this document the CTM shrinks coordinates and these coordinates are large numbers.
53+
// At the moment creation of this test clipper has a problem with handling large numbers
54+
// since internally it deals with integers and has to multiply large numbers even more
55+
// for internal purposes
56+
using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFileName))) {
57+
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
58+
ClipperBridge.floatMultiplier = Math.Pow(10, 14);
59+
Exception e = NUnit.Framework.Assert.Catch(typeof(ClipperException), () => contentParser.ProcessContent(1,
60+
new LocationTextExtractionStrategy()));
61+
NUnit.Framework.Assert.AreEqual(ClipperExceptionConstant.COORDINATE_OUTSIDE_ALLOWED_RANGE, e.Message);
62+
ClipperBridge.floatMultiplier = null;
63+
}
4864
}
4965
}
5066
}

itext.tests/itext.kernel.tests/itext/kernel/pdf/canvas/parser/clipperlib/ClipperBridgeTest.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ You should have received a copy of the GNU Affero General Public License
2222
*/
2323
using System.Collections.Generic;
2424
using System.Linq;
25+
using iText.Commons.Utils;
2526
using iText.Kernel.Geom;
2627
using iText.Kernel.Pdf.Canvas;
2728
using iText.Test;
@@ -48,11 +49,12 @@ public virtual void SquareClippingTest() {
4849
Path rectanglePath = new Path();
4950
rectanglePath.AddSubpath(rectangleSubpath);
5051
Clipper clipper = new Clipper();
51-
ClipperBridge.AddPath(clipper, squarePath, PolyType.SUBJECT);
52-
ClipperBridge.AddPath(clipper, rectanglePath, PolyType.CLIP);
52+
ClipperBridge clipperBridge = new ClipperBridge(squarePath, rectanglePath);
53+
clipperBridge.AddPath(clipper, squarePath, PolyType.SUBJECT);
54+
clipperBridge.AddPath(clipper, rectanglePath, PolyType.CLIP);
5355
PolyTree polyTree = new PolyTree();
5456
clipper.Execute(ClipType.UNION, polyTree);
55-
Path result = ClipperBridge.ConvertToPath(polyTree);
57+
Path result = clipperBridge.ConvertToPath(polyTree);
5658
NUnit.Framework.Assert.AreEqual(new Point(20, 40), result.GetCurrentPoint());
5759
NUnit.Framework.Assert.AreEqual(2, result.GetSubpaths().Count);
5860
Subpath closedPath = result.GetSubpaths()[0];
@@ -95,14 +97,34 @@ public virtual void GetEndTypeTest() {
9597
public virtual void LongRectWidthTest() {
9698
IntRect longRect = new IntRect(14900000000000000L, 21275000000000000L, 71065802001953128L, 71075000000000000L
9799
);
98-
NUnit.Framework.Assert.AreEqual(561.658, ClipperBridge.LongRectCalculateWidth(longRect), 0.001f);
100+
NUnit.Framework.Assert.AreEqual(561.658, new ClipperBridge().LongRectCalculateWidth(longRect), 0.001f);
99101
}
100102

101103
[NUnit.Framework.Test]
102104
public virtual void LongRectHeightTest() {
103105
IntRect longRect = new IntRect(14900000000000000L, 21275000000000000L, 71065802001953128L, 71075000000000000L
104106
);
105-
NUnit.Framework.Assert.AreEqual(498, ClipperBridge.LongRectCalculateHeight(longRect), 0.001f);
107+
NUnit.Framework.Assert.AreEqual(498, new ClipperBridge().LongRectCalculateHeight(longRect), 0.001f);
108+
}
109+
110+
[NUnit.Framework.Test]
111+
public virtual void DynamicFloatMultiplierCalculationsSmallValuesTest() {
112+
Point[] points = new Point[] { new Point(1e-10, 0), new Point(0, 1e-13) };
113+
NUnit.Framework.Assert.AreEqual(1.8014398509481984e26, new ClipperBridge(points).GetFloatMultiplier(), 0e+10
114+
);
115+
}
116+
117+
[NUnit.Framework.Test]
118+
public virtual void DynamicFloatMultiplierCalculationsBigValuesTest() {
119+
Point[] points = new Point[] { new Point(1e+11, 10), new Point(10, 1e+10) };
120+
NUnit.Framework.Assert.AreEqual(180143, new ClipperBridge(points).GetFloatMultiplier(), 0.001f);
121+
}
122+
123+
[NUnit.Framework.Test]
124+
public virtual void SmallFloatMultiplierCoefficientTest() {
125+
Point[] points = new Point[] { new Point(1e-10, 1e+10) };
126+
NUnit.Framework.Assert.AreEqual(new IntPoint(0, 18014390000000000L), new ClipperBridge(points).ConvertToLongPoints
127+
(JavaUtil.ArraysAsList(points))[0]);
106128
}
107129

108130
private bool AreShapesEqual(IShape expected, IShape actual) {

itext/itext.kernel/itext/kernel/pdf/canvas/parser/ParserGraphicsState.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public override void UpdateCtm(Matrix newCtm) {
6262
/// <summary>Intersects the current clipping path with the given path.</summary>
6363
/// <remarks>
6464
/// Intersects the current clipping path with the given path.
65+
/// <para />
6566
/// <strong>Note:</strong> Coordinates of the given path should be in
6667
/// the transformed user space.
6768
/// </remarks>
@@ -80,17 +81,19 @@ public virtual void Clip(Path path, int fillingRule) {
8081
Path pathCopy = new Path(path);
8182
pathCopy.CloseAllSubpaths();
8283
Clipper clipper = new Clipper();
83-
ClipperBridge.AddPath(clipper, clippingPath, PolyType.SUBJECT);
84-
ClipperBridge.AddPath(clipper, pathCopy, PolyType.CLIP);
84+
ClipperBridge clipperBridge = new ClipperBridge(clippingPath, pathCopy);
85+
clipperBridge.AddPath(clipper, clippingPath, PolyType.SUBJECT);
86+
clipperBridge.AddPath(clipper, pathCopy, PolyType.CLIP);
8587
PolyTree resultTree = new PolyTree();
8688
clipper.Execute(ClipType.INTERSECTION, resultTree, PolyFillType.NON_ZERO, ClipperBridge.GetFillType(fillingRule
8789
));
88-
clippingPath = ClipperBridge.ConvertToPath(resultTree);
90+
clippingPath = clipperBridge.ConvertToPath(resultTree);
8991
}
9092

9193
/// <summary>Getter for the current clipping path.</summary>
9294
/// <remarks>
9395
/// Getter for the current clipping path.
96+
/// <para />
9497
/// <strong>Note:</strong> The returned clipping path is in the transformed user space, so
9598
/// if you want to get it in default user space, apply transformation matrix (
9699
/// <see cref="iText.Kernel.Pdf.Canvas.CanvasGraphicsState.GetCtm()"/>
@@ -104,6 +107,7 @@ public virtual Path GetClippingPath() {
104107
/// <summary>Sets the current clipping path to the specified path.</summary>
105108
/// <remarks>
106109
/// Sets the current clipping path to the specified path.
110+
/// <para />
107111
/// <strong>Note:</strong>This method doesn't modify existing clipping path,
108112
/// it simply replaces it with the new one instead.
109113
/// </remarks>

0 commit comments

Comments
 (0)