Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/marlin/TextClipErrorTest.java
38838 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.BasicStroke;24import java.awt.Color;25import java.awt.Font;26import java.awt.Graphics2D;27import java.awt.RenderingHints;28import java.awt.Shape;29import java.awt.font.FontRenderContext;30import java.awt.font.GlyphVector;31import java.awt.geom.AffineTransform;32import java.awt.geom.Line2D;33import java.awt.geom.Path2D;34import java.awt.geom.PathIterator;35import static java.awt.geom.PathIterator.SEG_CLOSE;36import static java.awt.geom.PathIterator.SEG_CUBICTO;37import static java.awt.geom.PathIterator.SEG_LINETO;38import static java.awt.geom.PathIterator.SEG_MOVETO;39import static java.awt.geom.PathIterator.SEG_QUADTO;40import java.awt.image.BufferedImage;41import java.io.File;42import java.io.IOException;43import java.util.ArrayList;44import java.util.Arrays;45import java.util.Locale;46import java.util.logging.Handler;47import java.util.logging.LogRecord;48import java.util.logging.Logger;49import javax.imageio.ImageIO;5051/**52* @test @bug 814471853* @summary Check the Stroker.drawBezApproxForArc() bug (stoke with round54* joins): if cosext2 > 0.5, it generates curves with NaN coordinates55* @run main TextClipErrorTest56*/57public class TextClipErrorTest {5859static final boolean SAVE_IMAGE = false;60static final boolean SERIALIZE = false;6162public static void main(String[] args) {63Locale.setDefault(Locale.US);6465// initialize j.u.l Looger:66final Logger log = Logger.getLogger("sun.java2d.marlin");67log.addHandler(new Handler() {68@Override69public void publish(LogRecord record) {70Throwable th = record.getThrown();71// detect any Throwable:72if (th != null) {73System.out.println("Test failed:\n" + record.getMessage());74th.printStackTrace(System.out);7576throw new RuntimeException("Test failed: ", th);77}78}7980@Override81public void flush() {82}8384@Override85public void close() throws SecurityException {86}87});8889log.info("TextClipErrorTest: start");9091// enable Marlin logging & internal checks:92System.setProperty("sun.java2d.renderer.log", "true");93System.setProperty("sun.java2d.renderer.useLogger", "true");94System.setProperty("sun.java2d.renderer.doChecks", "true");9596BufferedImage image = new BufferedImage(256, 256,97BufferedImage.TYPE_INT_ARGB);9899Graphics2D g2d = image.createGraphics();100g2d.setColor(Color.red);101try {102g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,103RenderingHints.VALUE_ANTIALIAS_ON);104105Font font = g2d.getFont();106FontRenderContext frc = new FontRenderContext(107new AffineTransform(), true, true);108109g2d.setStroke(new BasicStroke(4.0f,110BasicStroke.CAP_ROUND,111BasicStroke.JOIN_ROUND));112113final Shape badShape;114if (SERIALIZE) {115final GlyphVector gv1 = font.createGlyphVector(frc, "\u00d6");116final Shape textShape = gv1.getOutline();117118final AffineTransform at1 = AffineTransform.getTranslateInstance(119-2091202.554154681, 5548.601436981691);120badShape = at1.createTransformedShape(textShape);121serializeShape(badShape);122} else {123badShape = deserializeShape();124}125126g2d.draw(badShape);127128// Draw anything within bounds and it fails:129g2d.draw(new Line2D.Double(10, 20, 30, 40));130131if (SAVE_IMAGE) {132final File file = new File("TextClipErrorTest.png");133System.out.println("Writing file: " + file.getAbsolutePath());134ImageIO.write(image, "PNG", file);135}136} catch (IOException ex) {137ex.printStackTrace();138} finally {139g2d.dispose();140log.info("TextClipErrorTest: end");141}142}143144private static void serializeShape(Shape shape) {145final double[] coords = new double[6];146147final int len = 32;148final ArrayList<Integer> typeList = new ArrayList<Integer>(len);149final ArrayList<double[]> coordsList = new ArrayList<double[]>(len);150151for (PathIterator pi = shape.getPathIterator(null);152!pi.isDone(); pi.next())153{154switch (pi.currentSegment(coords)) {155case SEG_MOVETO:156typeList.add(SEG_MOVETO);157coordsList.add(Arrays.copyOf(coords, 2));158break;159case SEG_LINETO:160typeList.add(SEG_LINETO);161coordsList.add(Arrays.copyOf(coords, 2));162break;163case SEG_QUADTO:164typeList.add(SEG_QUADTO);165coordsList.add(Arrays.copyOf(coords, 4));166break;167case SEG_CUBICTO:168typeList.add(SEG_CUBICTO);169coordsList.add(Arrays.copyOf(coords, 6));170break;171case SEG_CLOSE:172typeList.add(SEG_CLOSE);173coordsList.add(null);174break;175default:176}177}178179final StringBuilder sb = new StringBuilder(1024);180// types:181sb.append("private static final int[] SHAPE_TYPES = new int[]{\n");182for (Integer i : typeList) {183sb.append(i).append(",\n");184}185sb.append("};\n");186187// coords:188sb.append("private static final double[][] SHAPE_COORDS = new double[][]{\n");189for (double[] c : coordsList) {190if (c == null) {191sb.append("null,\n");192} else {193sb.append("new double[]{");194for (int i = 0; i < c.length; i++) {195sb.append(c[i]).append(",");196}197sb.append("},\n");198}199}200sb.append("};\n");201202System.out.println("Shape size: " + typeList.size());203System.out.println("Serialized shape:\n" + sb.toString());204}205206private static Shape deserializeShape() {207final Path2D.Double path = new Path2D.Double();208209for (int i = 0; i < SHAPE_TYPES.length; i++) {210double[] coords = SHAPE_COORDS[i];211212switch (SHAPE_TYPES[i]) {213case SEG_MOVETO:214path.moveTo(coords[0], coords[1]);215break;216case SEG_LINETO:217path.lineTo(coords[0], coords[1]);218break;219case SEG_QUADTO:220path.quadTo(coords[0], coords[1],221coords[2], coords[3]);222break;223case SEG_CUBICTO:224path.curveTo(coords[0], coords[1],225coords[2], coords[3],226coords[4], coords[5]);227break;228case SEG_CLOSE:229path.closePath();230break;231default:232}233}234235return path;236}237238// generated code:239private static final int[] SHAPE_TYPES = new int[]{2400,2412,2422,2432,2442,2452,2462,2472,2482,2494,2500,2512,2522,2532,2542,2552,2562,2572,2582,2594,2600,2611,2621,2631,2641,2654,2660,2671,2681,2691,2701,2714,272};273274private static final double[][] SHAPE_COORDS = new double[][]{275new double[]{-2091197.819779681, 5540.648311981691,},276new double[]{-2091199.116654681, 5540.648311981691, -2091199.874467181, 5541.609249481691,},277new double[]{-2091200.632279681, 5542.570186981691, -2091200.632279681, 5544.242061981691,},278new double[]{-2091200.632279681, 5545.882686981691, -2091199.874467181, 5546.843624481691,},279new double[]{-2091199.116654681, 5547.804561981691, -2091197.819779681, 5547.804561981691,},280new double[]{-2091196.538529681, 5547.804561981691, -2091195.780717181, 5546.843624481691,},281new double[]{-2091195.022904681, 5545.882686981691, -2091195.022904681, 5544.242061981691,},282new double[]{-2091195.022904681, 5542.570186981691, -2091195.780717181, 5541.609249481691,},283new double[]{-2091196.538529681, 5540.648311981691, -2091197.819779681, 5540.648311981691,},284null,285new double[]{-2091197.819779681, 5539.695186981691,},286new double[]{-2091195.991654681, 5539.695186981691, -2091194.890092181, 5540.929561981691,},287new double[]{-2091193.788529681, 5542.163936981691, -2091193.788529681, 5544.242061981691,},288new double[]{-2091193.788529681, 5546.304561981691, -2091194.890092181, 5547.538936981691,},289new double[]{-2091195.991654681, 5548.773311981691, -2091197.819779681, 5548.773311981691,},290new double[]{-2091199.663529681, 5548.773311981691, -2091200.772904681, 5547.538936981691,},291new double[]{-2091201.882279681, 5546.304561981691, -2091201.882279681, 5544.242061981691,},292new double[]{-2091201.882279681, 5542.163936981691, -2091200.772904681, 5540.929561981691,},293new double[]{-2091199.663529681, 5539.695186981691, -2091197.819779681, 5539.695186981691,},294null,295new double[]{-2091197.210404681, 5537.835811981691,},296new double[]{-2091196.022904681, 5537.835811981691,},297new double[]{-2091196.022904681, 5539.023311981691,},298new double[]{-2091197.210404681, 5539.023311981691,},299new double[]{-2091197.210404681, 5537.835811981691,},300null,301new double[]{-2091199.632279681, 5537.835811981691,},302new double[]{-2091198.444779681, 5537.835811981691,},303new double[]{-2091198.444779681, 5539.023311981691,},304new double[]{-2091199.632279681, 5539.023311981691,},305new double[]{-2091199.632279681, 5537.835811981691,},306null,307};308309}310311312