Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/CollinearSimplifier.java
38918 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.marlin;2627import sun.awt.geom.PathConsumer2D;2829final class CollinearSimplifier implements PathConsumer2D {3031enum SimplifierState {3233Empty, PreviousPoint, PreviousLine34};35// slope precision threshold36static final float EPS = 1e-4f; // aaime proposed 1e-3f3738PathConsumer2D delegate;39SimplifierState state;40float px1, py1, px2, py2;41float pslope;4243CollinearSimplifier() {44}4546public CollinearSimplifier init(PathConsumer2D delegate) {47this.delegate = delegate;48this.state = SimplifierState.Empty;4950return this; // fluent API51}5253@Override54public void pathDone() {55emitStashedLine();56state = SimplifierState.Empty;57delegate.pathDone();58}5960@Override61public void closePath() {62emitStashedLine();63state = SimplifierState.Empty;64delegate.closePath();65}6667@Override68public long getNativeConsumer() {69return 0;70}7172@Override73public void quadTo(float x1, float y1, float x2, float y2) {74emitStashedLine();75delegate.quadTo(x1, y1, x2, y2);76// final end point:77state = SimplifierState.PreviousPoint;78px1 = x2;79py1 = y2;80}8182@Override83public void curveTo(float x1, float y1, float x2, float y2,84float x3, float y3) {85emitStashedLine();86delegate.curveTo(x1, y1, x2, y2, x3, y3);87// final end point:88state = SimplifierState.PreviousPoint;89px1 = x3;90py1 = y3;91}9293@Override94public void moveTo(float x, float y) {95emitStashedLine();96delegate.moveTo(x, y);97state = SimplifierState.PreviousPoint;98px1 = x;99py1 = y;100}101102@Override103public void lineTo(final float x, final float y) {104switch (state) {105case Empty:106delegate.lineTo(x, y);107state = SimplifierState.PreviousPoint;108px1 = x;109py1 = y;110return;111112case PreviousPoint:113state = SimplifierState.PreviousLine;114px2 = x;115py2 = y;116pslope = getSlope(px1, py1, x, y);117return;118119case PreviousLine:120final float slope = getSlope(px2, py2, x, y);121// test for collinearity122if ((slope == pslope) || (Math.abs(pslope - slope) < EPS)) {123// merge segments124px2 = x;125py2 = y;126return;127}128// emit previous segment129delegate.lineTo(px2, py2);130px1 = px2;131py1 = py2;132px2 = x;133py2 = y;134pslope = slope;135return;136default:137}138}139140private void emitStashedLine() {141if (state == SimplifierState.PreviousLine) {142delegate.lineTo(px2, py2);143}144}145146private static float getSlope(float x1, float y1, float x2, float y2) {147float dy = y2 - y1;148if (dy == 0f) {149return (x2 > x1) ? Float.POSITIVE_INFINITY150: Float.NEGATIVE_INFINITY;151}152return (x2 - x1) / dy;153}154}155156157