]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3185: Exclude shape not in clipping path for AFP
authorSimon Steiner <ssteiner@apache.org>
Thu, 13 Jun 2024 14:19:18 +0000 (15:19 +0100)
committerSimon Steiner <ssteiner@apache.org>
Thu, 13 Jun 2024 14:19:18 +0000 (15:19 +0100)
fop-core/src/main/java/org/apache/fop/afp/AFPGraphics2D.java
fop-core/src/test/java/org/apache/fop/afp/AFPGraphics2DTestCase.java

index 88a15ad12db46735b39458553abd1654011c5a78..cf07286830958932dcb49b60948f38fab29ca1b1 100644 (file)
@@ -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();
         }
index 7045e80ee04004c97d9c0d53c871867a090e03c3..d6b14af0f75efceab149df43fec3e8d001477b15 100644 (file)
@@ -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);
+    }
 }