1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.com.sun.prism;
  27 
  28 import com.sun.javafx.geom.Path2D;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 import com.sun.prism.BasicStroke;
  31 import com.sun.prism.BasicStrokeShim;
  32 import org.junit.Before;
  33 import org.junit.Test;
  34 import static org.junit.Assert.*;
  35 
  36 public class StrokedShapeTest {
  37 
  38     BasicStroke stroke;
  39     Path2D path;
  40 
  41     @Before
  42     public void setUp() {
  43         stroke = new BasicStroke();
  44         BasicStrokeShim.set_width(stroke, 10);
  45         path = new Path2D();
  46         path.moveTo(0, 0);
  47     }
  48 
  49     @Test
  50     public void lineTo() {
  51         path.lineTo(10, 0);
  52         assertBounds(-5, -5, 15, 5);
  53     }
  54 
  55     @Test
  56     public void lineTo_cap() {
  57         path.lineTo(3, 4);
  58         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_SQUARE);
  59         assertBounds(-7, -7, 10, 11);
  60         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_BUTT);
  61         assertBounds(-4, -3, 7, 7);
  62         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_ROUND);
  63         assertBounds(-5, -5, 8, 9);
  64     }
  65 
  66     @Test
  67     public void quadTo() {
  68         path.quadTo(3, 4, 6, 0);
  69         assertBounds(-7, -7, 13, 7);
  70     }
  71 
  72     @Test
  73     public void quadTo_cap() {
  74         path.quadTo(3, 4, 6, 0);
  75         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_SQUARE);
  76         assertBounds(-7, -7, 13, 7);
  77         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_BUTT);
  78         assertBounds(-4, -3, 10, 7);
  79         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_ROUND);
  80         assertBounds(-5, -5, 11, 7);
  81     }
  82     
  83     @Test
  84     public void curveTo() {
  85         path.curveTo(10, 0, 0, 10, 20, 10);
  86         assertBounds(-5, -5, 25, 15);
  87         
  88     }
  89     
  90     @Test
  91     public void curveTo_cap() {
  92         path.curveTo(3, 4, 6, 0, 9, 4);
  93         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_SQUARE);
  94         assertBounds(-7, -7, 16, 11);
  95         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_BUTT);
  96         assertBounds(-4, -3, 13, 7);
  97         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_ROUND);
  98         assertBounds(-5, -5, 14, 9);
  99     }
 100     
 101     @Test
 102     public void lineLine_join() {
 103         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_ROUND); // for easy computation
 104         path.lineTo(3, 4);
 105         path.lineTo(6, 0);
 106         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_MITER);
 107         assertBounds(-5, -5, 11, 12.33f);
 108         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_BEVEL);
 109         assertBounds(-5, -5, 11, 7);
 110         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_ROUND);
 111         assertBounds(-5, -5, 11, 9);
 112     }
 113     
 114     @Test
 115     public void lineQuad_join() {
 116         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_ROUND); // for easy computation
 117         path.lineTo(3, 4);
 118         path.quadTo(6, 0, 10, 4);
 119         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_MITER);
 120         assertBounds(-5, -5, 15, 12.33f);
 121         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_BEVEL);
 122         assertBounds(-5, -5, 15, 9); //a better test here? (width accumulates more than join)
 123         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_ROUND);
 124         assertBounds(-5, -5, 15, 9);
 125     }
 126     
 127     
 128     @Test
 129     public void lineCurve_join() {
 130         BasicStrokeShim.set_cap(stroke, BasicStroke.CAP_ROUND); // for easy computation
 131         path.lineTo(3, 4);
 132         path.curveTo(6, 0, 0, 0, 10, 4);
 133         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_MITER);
 134         assertBounds(-5, -5, 15, 12.33f);
 135         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_BEVEL);
 136         assertBounds(-5, -5, 15, 9); //a better test here? (width accumulates more than join)
 137         BasicStrokeShim.set_join(stroke, BasicStroke.JOIN_ROUND);
 138         assertBounds(-5, -5, 15, 9);
 139     }
 140     
 141     private void assertBounds(float x0, float y0, float x1, float y1) {
 142         float[] bbox = new float[]{0, 0, 0, 0};
 143         stroke.accumulateShapeBounds(bbox, path, BaseTransform.IDENTITY_TRANSFORM);
 144         assertArrayEquals(new float[]{x0, y0, x1, y1}, bbox, 0.01f);
 145     }
 146 }