From: Simon Steiner Date: Thu, 13 Jun 2024 14:19:18 +0000 (+0100) Subject: FOP-3185: Exclude shape not in clipping path for AFP X-Git-Tag: 2_10~25 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b8a6ee005de66e26ba4418c611985f68f323e230;p=xmlgraphics-fop.git FOP-3185: Exclude shape not in clipping path for AFP --- diff --git a/fop-core/src/main/java/org/apache/fop/afp/AFPGraphics2D.java b/fop-core/src/main/java/org/apache/fop/afp/AFPGraphics2D.java index 88a15ad12..cf0728683 100644 --- a/fop-core/src/main/java/org/apache/fop/afp/AFPGraphics2D.java +++ b/fop-core/src/main/java/org/apache/fop/afp/AFPGraphics2D.java @@ -33,6 +33,7 @@ import java.awt.Rectangle; import java.awt.Shape; import java.awt.Stroke; import java.awt.geom.AffineTransform; +import java.awt.geom.Area; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.PathIterator; @@ -356,6 +357,28 @@ public class AFPGraphics2D extends AbstractGraphics2D implements NativeImageHand * true if the shape is to be drawn filled */ private void doDrawing(Shape shape, boolean fill) { + Shape clip = getClip(); + if (clip != null) { + if (shape instanceof Line2D + && (((Line2D) shape).getX1() == ((Line2D) shape).getX2() + || ((Line2D) shape).getY1() == ((Line2D) shape).getY2())) { + Rectangle2D bounds = shape.getBounds2D(); + Rectangle2D newShape = bounds.createIntersection(clip.getBounds2D()); + ((Line2D) shape).setLine(newShape.getX(), + newShape.getY(), + newShape.getX() + newShape.getWidth(), + newShape.getY() + newShape.getHeight()); + } else if (shape instanceof Rectangle2D) { + Area area = new Area(shape); + area.intersect(new Area(clip)); + shape = area.getBounds2D(); + } else { + Area area = new Area(shape); + if (!area.isEmpty() && !area.intersects(clip.getBounds2D())) { + return; + } + } + } if (!fill) { graphicsObj.newSegment(); } diff --git a/fop-core/src/test/java/org/apache/fop/afp/AFPGraphics2DTestCase.java b/fop-core/src/test/java/org/apache/fop/afp/AFPGraphics2DTestCase.java index 7045e80ee..d6b14af0f 100644 --- a/fop-core/src/test/java/org/apache/fop/afp/AFPGraphics2DTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/afp/AFPGraphics2DTestCase.java @@ -66,7 +66,7 @@ public class AFPGraphics2DTestCase { @Test public void testDrawGraphicsFillet() throws IOException { GraphicContext gc = new GraphicContext(); - gc.setClip(new Rectangle(0, 0, 2, 2)); + gc.setClip(new Rectangle(0, 0, 100, 100)); graphics2D.setGraphicContext(gc); GraphicsObject go = new GraphicsObject(new Factory(), "test"); graphics2D.setGraphicsObject(go); @@ -80,4 +80,17 @@ public class AFPGraphics2DTestCase { int sizeOfGraphicsFillet = 128; Assert.assertEquals(is.read(), sizeOfGraphicsFillet); } + + @Test + public void testDrawGraphicsFilletClipped() throws IOException { + GraphicContext gc = new GraphicContext(); + gc.setClip(new Rectangle(50, 50, 100, 100)); + graphics2D.setGraphicContext(gc); + GraphicsObject go = new GraphicsObject(new Factory(), "test"); + graphics2D.setGraphicsObject(go); + graphics2D.draw(new Ellipse2D.Double(0, 0, 50, 50)); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + go.writeToStream(bos); + Assert.assertEquals(bos.size(), 17 + 17); + } }