Skip to content

Commit

Permalink
Make 'floatMultiplier' in ClipperBridge non-static configuration
Browse files Browse the repository at this point in the history
DEVSIX-5770
DEVSIX-1279

Autoported commit.
Original commit hash: [1bb85b3f2]
  • Loading branch information
AnhelinaM authored and iText-CI committed Oct 11, 2024
1 parent 2b2ca05 commit 52792d5
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,38 @@ You should have received a copy of the GNU Affero General Public License
namespace iText.Kernel.Pdf.Canvas.Parser {
[NUnit.Framework.Category("IntegrationTest")]
public class PdfContentExtractionTest : ExtendedITextTest {
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/parser/PdfContentExtractionTest/";

[NUnit.Framework.Test]
public virtual void ContentExtractionInDocWithBigCoordinatesTest() {
//TODO: remove the expected exception construct once the issue is fixed (DEVSIX-1279)
String inputFileName = sourceFolder + "docWithBigCoordinates.pdf";
//In this document the CTM shrinks coordinates and this coordinates are large numbers.
String inputFileName = SOURCE_FOLDER + "docWithBigCoordinates.pdf";
// In this document the CTM shrinks coordinates and these coordinates are large numbers.
// At the moment creation of this test clipper has a problem with handling large numbers
// since internally it deals with integers and has to multiply large numbers even more
// for internal purposes
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFileName));
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
Exception e = NUnit.Framework.Assert.Catch(typeof(ClipperException), () => contentParser.ProcessContent(1,
new LocationTextExtractionStrategy()));
NUnit.Framework.Assert.AreEqual(ClipperExceptionConstant.COORDINATE_OUTSIDE_ALLOWED_RANGE, e.Message);
using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFileName))) {
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
NUnit.Framework.Assert.DoesNotThrow(() => contentParser.ProcessContent(1, new LocationTextExtractionStrategy
()));
}
}

[NUnit.Framework.Test]
public virtual void ContentExtractionInDocWithStaticFloatMultiplierTest() {
String inputFileName = SOURCE_FOLDER + "docWithBigCoordinates.pdf";
// In this document the CTM shrinks coordinates and these coordinates are large numbers.
// At the moment creation of this test clipper has a problem with handling large numbers
// since internally it deals with integers and has to multiply large numbers even more
// for internal purposes
using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFileName))) {
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(pdfDocument);
ClipperBridge.floatMultiplier = Math.Pow(10, 14);
Exception e = NUnit.Framework.Assert.Catch(typeof(ClipperException), () => contentParser.ProcessContent(1,
new LocationTextExtractionStrategy()));
NUnit.Framework.Assert.AreEqual(ClipperExceptionConstant.COORDINATE_OUTSIDE_ALLOWED_RANGE, e.Message);
ClipperBridge.floatMultiplier = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You should have received a copy of the GNU Affero General Public License
*/
using System.Collections.Generic;
using System.Linq;
using iText.Commons.Utils;
using iText.Kernel.Geom;
using iText.Kernel.Pdf.Canvas;
using iText.Test;
Expand All @@ -48,11 +49,12 @@ public virtual void SquareClippingTest() {
Path rectanglePath = new Path();
rectanglePath.AddSubpath(rectangleSubpath);
Clipper clipper = new Clipper();
ClipperBridge.AddPath(clipper, squarePath, PolyType.SUBJECT);
ClipperBridge.AddPath(clipper, rectanglePath, PolyType.CLIP);
ClipperBridge clipperBridge = new ClipperBridge(squarePath, rectanglePath);
clipperBridge.AddPath(clipper, squarePath, PolyType.SUBJECT);
clipperBridge.AddPath(clipper, rectanglePath, PolyType.CLIP);
PolyTree polyTree = new PolyTree();
clipper.Execute(ClipType.UNION, polyTree);
Path result = ClipperBridge.ConvertToPath(polyTree);
Path result = clipperBridge.ConvertToPath(polyTree);
NUnit.Framework.Assert.AreEqual(new Point(20, 40), result.GetCurrentPoint());
NUnit.Framework.Assert.AreEqual(2, result.GetSubpaths().Count);
Subpath closedPath = result.GetSubpaths()[0];
Expand Down Expand Up @@ -95,14 +97,34 @@ public virtual void GetEndTypeTest() {
public virtual void LongRectWidthTest() {
IntRect longRect = new IntRect(14900000000000000L, 21275000000000000L, 71065802001953128L, 71075000000000000L
);
NUnit.Framework.Assert.AreEqual(561.658, ClipperBridge.LongRectCalculateWidth(longRect), 0.001f);
NUnit.Framework.Assert.AreEqual(561.658, new ClipperBridge().LongRectCalculateWidth(longRect), 0.001f);
}

[NUnit.Framework.Test]
public virtual void LongRectHeightTest() {
IntRect longRect = new IntRect(14900000000000000L, 21275000000000000L, 71065802001953128L, 71075000000000000L
);
NUnit.Framework.Assert.AreEqual(498, ClipperBridge.LongRectCalculateHeight(longRect), 0.001f);
NUnit.Framework.Assert.AreEqual(498, new ClipperBridge().LongRectCalculateHeight(longRect), 0.001f);
}

[NUnit.Framework.Test]
public virtual void DynamicFloatMultiplierCalculationsSmallValuesTest() {
Point[] points = new Point[] { new Point(1e-10, 0), new Point(0, 1e-13) };
NUnit.Framework.Assert.AreEqual(1.8014398509481984e26, new ClipperBridge(points).GetFloatMultiplier(), 0e+10
);
}

[NUnit.Framework.Test]
public virtual void DynamicFloatMultiplierCalculationsBigValuesTest() {
Point[] points = new Point[] { new Point(1e+11, 10), new Point(10, 1e+10) };
NUnit.Framework.Assert.AreEqual(180143, new ClipperBridge(points).GetFloatMultiplier(), 0.001f);
}

[NUnit.Framework.Test]
public virtual void SmallFloatMultiplierCoefficientTest() {
Point[] points = new Point[] { new Point(1e-10, 1e+10) };
NUnit.Framework.Assert.AreEqual(new IntPoint(0, 18014390000000000L), new ClipperBridge(points).ConvertToLongPoints
(JavaUtil.ArraysAsList(points))[0]);
}

private bool AreShapesEqual(IShape expected, IShape actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public override void UpdateCtm(Matrix newCtm) {
/// <summary>Intersects the current clipping path with the given path.</summary>
/// <remarks>
/// Intersects the current clipping path with the given path.
/// <para />
/// <strong>Note:</strong> Coordinates of the given path should be in
/// the transformed user space.
/// </remarks>
Expand All @@ -80,17 +81,19 @@ public virtual void Clip(Path path, int fillingRule) {
Path pathCopy = new Path(path);
pathCopy.CloseAllSubpaths();
Clipper clipper = new Clipper();
ClipperBridge.AddPath(clipper, clippingPath, PolyType.SUBJECT);
ClipperBridge.AddPath(clipper, pathCopy, PolyType.CLIP);
ClipperBridge clipperBridge = new ClipperBridge(clippingPath, pathCopy);
clipperBridge.AddPath(clipper, clippingPath, PolyType.SUBJECT);
clipperBridge.AddPath(clipper, pathCopy, PolyType.CLIP);
PolyTree resultTree = new PolyTree();
clipper.Execute(ClipType.INTERSECTION, resultTree, PolyFillType.NON_ZERO, ClipperBridge.GetFillType(fillingRule
));
clippingPath = ClipperBridge.ConvertToPath(resultTree);
clippingPath = clipperBridge.ConvertToPath(resultTree);
}

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

0 comments on commit 52792d5

Please sign in to comment.