Path: blob/master/src/java.desktop/share/classes/sun/java2d/marlin/PathSimplifier.java
41159 views
/*1* Copyright (c) 2017, 2021, 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*/24package sun.java2d.marlin;2526final class PathSimplifier implements DPathConsumer2D {2728// distance threshold in pixels (device)29private static final double PIX_THRESHOLD = MarlinProperties.getPathSimplifierPixelTolerance();3031private static final double SQUARE_TOLERANCE = PIX_THRESHOLD * PIX_THRESHOLD;3233// members:34private DPathConsumer2D delegate;35private double cx, cy;3637PathSimplifier() {38}3940PathSimplifier init(final DPathConsumer2D delegate) {41this.delegate = delegate;42return this; // fluent API43}4445@Override46public void pathDone() {47delegate.pathDone();48}4950@Override51public void closePath() {52delegate.closePath();53}5455@Override56public long getNativeConsumer() {57return 0;58}5960@Override61public void quadTo(final double x1, final double y1,62final double xe, final double ye)63{64// Test if curve is too small:65double dx = (xe - cx);66double dy = (ye - cy);6768if ((dx * dx + dy * dy) <= SQUARE_TOLERANCE) {69// check control points P1:70dx = (x1 - cx);71dy = (y1 - cy);7273if ((dx * dx + dy * dy) <= SQUARE_TOLERANCE) {74return;75}76}77delegate.quadTo(x1, y1, xe, ye);78// final end point:79cx = xe;80cy = ye;81}8283@Override84public void curveTo(final double x1, final double y1,85final double x2, final double y2,86final double xe, final double ye)87{88// Test if curve is too small:89double dx = (xe - cx);90double dy = (ye - cy);9192if ((dx * dx + dy * dy) <= SQUARE_TOLERANCE) {93// check control points P1:94dx = (x1 - cx);95dy = (y1 - cy);9697if ((dx * dx + dy * dy) <= SQUARE_TOLERANCE) {98// check control points P2:99dx = (x2 - cx);100dy = (y2 - cy);101102if ((dx * dx + dy * dy) <= SQUARE_TOLERANCE) {103return;104}105}106}107delegate.curveTo(x1, y1, x2, y2, xe, ye);108// final end point:109cx = xe;110cy = ye;111}112113@Override114public void moveTo(final double xe, final double ye) {115delegate.moveTo(xe, ye);116// starting point:117cx = xe;118cy = ye;119}120121@Override122public void lineTo(final double xe, final double ye) {123// Test if segment is too small:124double dx = (xe - cx);125double dy = (ye - cy);126127if ((dx * dx + dy * dy) <= SQUARE_TOLERANCE) {128return;129}130delegate.lineTo(xe, ye);131// final end point:132cx = xe;133cy = ye;134}135}136137138