Skip to content

Commit

Permalink
Fixed bug in Stroker clipping for closed paths whose first point (mov…
Browse files Browse the repository at this point in the history
…eto) is outside
  • Loading branch information
bourgesl committed Sep 20, 2017
1 parent a43af2f commit 86235c1
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/marlin/pisces/DStroker.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ public void closePath() {
return;
}

if (sOutCode == 0) {
// basic acceptance criteria
if ((sOutCode & cOutCode) == 0) {
if (cx0 != sx0 || cy0 != sy0) {
lineTo(sx0, sy0, true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/marlin/pisces/Stroker.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ public void closePath() {
return;
}

if (sOutCode == 0) {
// basic acceptance criteria
if ((sOutCode & cOutCode) == 0) {
if (cx0 != sx0 || cy0 != sy0) {
lineTo(sx0, sy0, true);
}
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/test/LineTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Expand All @@ -28,24 +26,40 @@
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.marlin.pisces.MarlinProperties;
import sun.java2d.pipe.RenderingEngine;
import sun.java2d.marlin.MarlinProperties;

/**
* Simple Line rendering test using GeneralPath to enable Pisces / marlin / ductus renderers
*/
public class LineTests {

public static void main(String[] args) {
final float lineStroke = 2f;
final int size = 2600;

System.out.println("Testing renderer = " + RenderingEngine.getInstance().getClass().getName());
final float lineStroke = 4f;
final int size = 1000;

// First display which renderer is tested:
// JDK9 only:
System.setProperty("sun.java2d.renderer.verbose", "true");
System.out.println("Testing renderer: ");
// Other JDK:
String renderer = "undefined";
try {
renderer = sun.java2d.pipe.RenderingEngine.getInstance().getClass().getName();
System.out.println(renderer);
} catch (Throwable th) {
// may fail with JDK9 jigsaw (jake)
if (false) {
System.err.println("Unable to get RenderingEngine.getInstance()");
th.printStackTrace();
}
}

System.out.println("LineTests: size = " + size);

Expand Down Expand Up @@ -87,6 +101,10 @@ public static void main(String[] args) {

private static void paint(final Graphics2D g2d, final float size) {

final float half = size / 2f;
final float radius = 166f;
g2d.draw(new Ellipse2D.Float(half - radius, half - radius, 2f * radius, 2f * radius));

final Path2D.Float path = new Path2D.Float();

for (float angle = 1f / 3f; angle <= 90f; angle += 1f) {
Expand All @@ -103,4 +121,4 @@ private static void paint(final Graphics2D g2d, final float size) {
g2d.draw(path);
}
}
}
}
10 changes: 4 additions & 6 deletions src/main/java/test/QuickSort.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Expand All @@ -32,7 +30,7 @@ final class QuickSort {
/**
* Sorts the specified sub-array of integers into ascending order.
*/
static void sort1(final int x[], final int y[], final int off, final int len) {
static void sort1(final int[] x, final int[] y, final int off, final int len) {
int t;
// Insertion sort on smallest arrays
if (len < 20) { // 7 in jdk8 or 20 as benchmark ?
Expand Down Expand Up @@ -150,7 +148,7 @@ static void sort1(final int x[], final int y[], final int off, final int len) {
/**
* Returns the index of the median of the three indexed integers.
*/
private static int med3(final int x[], final int a, final int b, final int c) {
private static int med3(final int[] x, final int a, final int b, final int c) {
return (x[a] < x[b]
? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
: (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/test/StrokeClipBugTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
* Simple Stroke clipping BUG 0.8.1 test using GeneralPath
*
* run it with -Dsun.java2d.renderer.clip=true/false
*/
public class StrokeClipBugTest {

private final static int N = 1;

private final static boolean DO_FILL = true;
private final static boolean DO_DRAW = true;

public static void main(String[] args) {

final float lineStroke = 4f;
final int size = 400;

// First display which renderer is tested:
// JDK9 only:
System.setProperty("sun.java2d.renderer.verbose", "true");
System.out.println("Testing renderer: ");
// Other JDK:
String renderer = "undefined";
try {
renderer = sun.java2d.pipe.RenderingEngine.getInstance().getClass().getName();
System.out.println(renderer);
} catch (Throwable th) {
// may fail with JDK9 jigsaw (jake)
if (false) {
System.err.println("Unable to get RenderingEngine.getInstance()");
th.printStackTrace();
}
}

System.out.println("StrokeClipBugTest: size = " + size);

final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

final Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

g2d.setClip(0, 0, size, size);
g2d.setStroke(
new BasicStroke(lineStroke, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 20f)
);

g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, size, size);

for (int i = 0; i < N; i++) {
final long start = System.nanoTime();

paint(g2d, size);

final long time = System.nanoTime() - start;

System.out.println("paint: duration= " + (1e-6 * time) + " ms.");
}

final String clipFlag = System.getProperty("sun.java2d.renderer.clip");
try {
final File file = new File("StrokeClipBugTest-clip-" + clipFlag + ".png");

System.out.println("Writing file: " + file.getAbsolutePath());
ImageIO.write(image, "PNG", file);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
g2d.dispose();
}
}

private static void paint(final Graphics2D g2d, final float size) {
final Shape path = createPath();

if (DO_FILL) {
g2d.setColor(Color.BLUE);
g2d.fill(path);
}

if (DO_DRAW) {
g2d.setColor(Color.RED);
g2d.draw(path);
}
}

private static Shape createPath() {
final Path2D p = new Path2D.Float();
// outside top
p.moveTo(100, -100);

p.lineTo(100.0, 50);
p.lineTo(300.0, 200);

p.closePath();

return p;
}
}
11 changes: 3 additions & 8 deletions src/main/java/test/StrokeClipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
import javax.imageio.ImageIO;

/**
* Simple Line rendering test using GeneralPath to enable Pisces / marlin /
* ductus renderers
* Simple Stroke clipping test using GeneralPath
*/
public class StrokeClipTest {

Expand All @@ -47,13 +46,11 @@ public class StrokeClipTest {

private final static boolean DO_CIRCLE = false;

private final static boolean DO_FILL = false;
private final static boolean DO_DRAW = true;
private final static boolean DO_FILL = true;
private final static boolean DO_DRAW = false;

private final static float CIRCLE_RADIUS = 100f;

private final static float RECT_SIZE = 100f;

private final static double sqrt2 = Math.sqrt(2);

public static void main(String[] args) {
Expand Down Expand Up @@ -140,8 +137,6 @@ private static void paint(final Graphics2D g2d, final float size) {

private static Shape createPath(final float size) {
if (DO_CIRCLE) {
final float c = (float) (0.5f * size - CIRCLE_RADIUS / sqrt2);

return new Ellipse2D.Float(
-CIRCLE_RADIUS,
100,
Expand Down

0 comments on commit 86235c1

Please sign in to comment.