From 734c9fb83e9f6bc9fab88347837ba100f076aecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20M=C3=BCller?= Date: Mon, 21 Mar 2016 18:26:10 +0100 Subject: [PATCH] Fixed #34 setClip now takes over color to new state. --- .../freehep/graphicsio/pdf/PDFGraphics2D.java | 9 ++++++++ .../org/freehep/graphicsio/test/TestClip.java | 21 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFGraphics2D.java b/freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFGraphics2D.java index e4eb0d05..890ee9e9 100644 --- a/freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFGraphics2D.java +++ b/freehep-graphicsio-pdf/src/main/java/org/freehep/graphicsio/pdf/PDFGraphics2D.java @@ -798,16 +798,25 @@ protected void writeTransform(AffineTransform t) throws IOException { * ================================================================================ */ protected void writeSetClip(Shape s) throws IOException { + // clear old clip + // FIXME this code only clears the clip if the PDF graphics state stack has been + // kept so shallow that a single "pop" removes the clip. However, when using + // Graphics.create states are being pushed on the stack and this method doesn't + // successfully clear the clip any more. A solution requires clearing the whole + // stack, setting a new clip and reconstructing the stack when the state with the + // new clip gets popped some time later. try { AffineTransform at = getTransform(); Stroke stroke = getStroke(); + Color color = getColor(); writeGraphicsRestore(); writeGraphicsSave(); writeStroke(stroke); writeTransform(at); + writePaint(color); } catch (IOException e) { handleException(e); } diff --git a/freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestClip.java b/freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestClip.java index 638c8701..8bd577d6 100644 --- a/freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestClip.java +++ b/freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestClip.java @@ -98,7 +98,7 @@ public void paintComponent(Graphics g) { vg.fillRect(0, 0, dim.width, dim.height); int nx = 6; - int ny = 6; + int ny = 7; int dw = dim.width / (nx + 1); int dh = dim.height / ny; @@ -216,8 +216,27 @@ public void paintComponent(Graphics g) { svg2.fill(clippedPath); svg2.dispose(); } + svg.dispose(); + // Java setClip and Draw + svg = (VectorGraphics) vg.create(); + svg.drawString("JavaSetClip", 10, 10 + 10 + 6 * dh); + svg.scale(factor, factor); + svg.translate(10 / factor, 10 / factor + 6 * dh / factor); + for (int i = 0; i < nx; i++) { + svg.translate(dw / factor, 0); + VectorGraphics svg2 = (VectorGraphics) svg.create(); + svg2.setColor(Color.red); + svg2.setLineWidth(1.0 / factor); + svg2.draw(clip[i]); + svg2.setColor(Color.blue); // color is set before setClip to check that it is taken over + svg2.setClip(clip[i]); + svg2.setLineWidth(3.0 / factor); + svg2.draw(path[i]); + svg2.dispose(); + } svg.dispose(); + } public static void main(String[] args) throws Exception {