|
| 1 | +package com.itextpdf.kernel.pdf.canvas.parser.clipper; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.geom.IShape; |
| 4 | +import com.itextpdf.kernel.geom.Line; |
| 5 | +import com.itextpdf.kernel.geom.Path; |
| 6 | +import com.itextpdf.kernel.geom.Subpath; |
| 7 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants.LineCapStyle; |
| 8 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants.LineJoinStyle; |
| 9 | +import com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.ClipType; |
| 10 | +import com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.EndType; |
| 11 | +import com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.JoinType; |
| 12 | +import com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.PolyType; |
| 13 | +import com.itextpdf.test.ExtendedITextTest; |
| 14 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.experimental.categories.Category; |
| 21 | + |
| 22 | +@Category(UnitTest.class) |
| 23 | +public class ClipperBridgeTest extends ExtendedITextTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void squareClippingTest() { |
| 27 | + Subpath squareSubpath = new Subpath(new com.itextpdf.kernel.geom.Point(10, 10)); |
| 28 | + squareSubpath.addSegment(new Line(10, 10, 10, 30)); |
| 29 | + squareSubpath.addSegment(new Line(10, 30, 30, 30)); |
| 30 | + squareSubpath.addSegment(new Line(30, 30, 30, 10)); |
| 31 | + squareSubpath.addSegment(new Line(30, 10, 10, 10)); |
| 32 | + squareSubpath.setClosed(true); |
| 33 | + Path squarePath = new Path(); |
| 34 | + squarePath.addSubpath(squareSubpath); |
| 35 | + |
| 36 | + Subpath rectangleSubpath = new Subpath(new com.itextpdf.kernel.geom.Point(20, 20)); |
| 37 | + rectangleSubpath.addSegment(new Line(20, 20, 20, 40)); |
| 38 | + rectangleSubpath.addSegment(new Line(20, 40, 30, 40)); |
| 39 | + rectangleSubpath.addSegment(new Line(30, 40, 30, 20)); |
| 40 | + rectangleSubpath.addSegment(new Line(30, 20, 20, 20)); |
| 41 | + rectangleSubpath.setClosed(true); |
| 42 | + Path rectanglePath = new Path(); |
| 43 | + rectanglePath.addSubpath(rectangleSubpath); |
| 44 | + |
| 45 | + DefaultClipper clipper = new DefaultClipper(); |
| 46 | + ClipperBridge.addPath(clipper, squarePath, PolyType.SUBJECT); |
| 47 | + ClipperBridge.addPath(clipper, rectanglePath, PolyType.CLIP); |
| 48 | + |
| 49 | + PolyTree polyTree = new PolyTree(); |
| 50 | + clipper.execute(ClipType.UNION, polyTree); |
| 51 | + Path result = ClipperBridge.convertToPath(polyTree); |
| 52 | + |
| 53 | + Assert.assertEquals(new com.itextpdf.kernel.geom.Point(20, 40), result.getCurrentPoint()); |
| 54 | + Assert.assertEquals(2, result.getSubpaths().size()); |
| 55 | + |
| 56 | + Subpath closedPath = result.getSubpaths().get(0); |
| 57 | + Assert.assertEquals(new com.itextpdf.kernel.geom.Point(20, 40), closedPath.getStartPoint()); |
| 58 | + List<IShape> closedPartSegments = closedPath.getSegments(); |
| 59 | + Assert.assertEquals(5, closedPartSegments.size()); |
| 60 | + Assert.assertTrue(areShapesEqual(new Line(20, 40, 20, 30), closedPartSegments.get(0))); |
| 61 | + Assert.assertTrue(areShapesEqual(new Line(20, 30, 10, 30), closedPartSegments.get(1))); |
| 62 | + Assert.assertTrue(areShapesEqual(new Line(10, 30, 10, 10), closedPartSegments.get(2))); |
| 63 | + Assert.assertTrue(areShapesEqual(new Line(10, 10, 30, 10), closedPartSegments.get(3))); |
| 64 | + Assert.assertTrue(areShapesEqual(new Line(30, 10, 30, 40), closedPartSegments.get(4))); |
| 65 | + Assert.assertTrue(closedPath.isClosed()); |
| 66 | + |
| 67 | + Subpath openPart = result.getSubpaths().get(1); |
| 68 | + Assert.assertEquals(new com.itextpdf.kernel.geom.Point(20, 40), openPart.getStartPoint()); |
| 69 | + Assert.assertEquals(0, openPart.getSegments().size()); |
| 70 | + Assert.assertFalse(openPart.isClosed()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void getJoinTypeTest() { |
| 75 | + Assert.assertEquals(JoinType.BEVEL, ClipperBridge.getJoinType(LineJoinStyle.BEVEL)); |
| 76 | + Assert.assertEquals(JoinType.MITER, ClipperBridge.getJoinType(LineJoinStyle.MITER)); |
| 77 | + Assert.assertEquals(JoinType.ROUND, ClipperBridge.getJoinType(LineJoinStyle.ROUND)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void getEndTypeTest() { |
| 82 | + Assert.assertEquals(EndType.OPEN_BUTT, ClipperBridge.getEndType(LineCapStyle.BUTT)); |
| 83 | + Assert.assertEquals(EndType.OPEN_SQUARE, ClipperBridge.getEndType(LineCapStyle.PROJECTING_SQUARE)); |
| 84 | + Assert.assertEquals(EndType.OPEN_ROUND, ClipperBridge.getEndType(LineCapStyle.ROUND)); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean areShapesEqual(IShape expected, IShape actual) { |
| 88 | + if (expected == actual) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + if (actual == null || expected.getClass() != actual.getClass()) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + return expected.getBasePoints().equals(actual.getBasePoints()); |
| 95 | + } |
| 96 | +} |
0 commit comments