Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/TransformingPathConsumer2D.java
38918 views
/*1* Copyright (c) 2007, 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;28import java.awt.geom.AffineTransform;29import java.awt.geom.Path2D;3031final class TransformingPathConsumer2D {3233TransformingPathConsumer2D() {34// used by RendererContext35}3637// recycled PathConsumer2D instance from transformConsumer()38private final Path2DWrapper wp_Path2DWrapper = new Path2DWrapper();3940PathConsumer2D wrapPath2d(Path2D.Float p2d)41{42return wp_Path2DWrapper.init(p2d);43}4445// recycled PathConsumer2D instances from transformConsumer()46private final TranslateFilter tx_TranslateFilter = new TranslateFilter();47private final DeltaScaleFilter tx_DeltaScaleFilter = new DeltaScaleFilter();48private final ScaleFilter tx_ScaleFilter = new ScaleFilter();49private final DeltaTransformFilter tx_DeltaTransformFilter = new DeltaTransformFilter();50private final TransformFilter tx_TransformFilter = new TransformFilter();5152PathConsumer2D transformConsumer(PathConsumer2D out,53AffineTransform at)54{55if (at == null) {56return out;57}58float mxx = (float) at.getScaleX();59float mxy = (float) at.getShearX();60float mxt = (float) at.getTranslateX();61float myx = (float) at.getShearY();62float myy = (float) at.getScaleY();63float myt = (float) at.getTranslateY();64if (mxy == 0f && myx == 0f) {65if (mxx == 1f && myy == 1f) {66if (mxt == 0f && myt == 0f) {67return out;68} else {69return tx_TranslateFilter.init(out, mxt, myt);70}71} else {72if (mxt == 0f && myt == 0f) {73return tx_DeltaScaleFilter.init(out, mxx, myy);74} else {75return tx_ScaleFilter.init(out, mxx, myy, mxt, myt);76}77}78} else if (mxt == 0f && myt == 0f) {79return tx_DeltaTransformFilter.init(out, mxx, mxy, myx, myy);80} else {81return tx_TransformFilter.init(out, mxx, mxy, mxt, myx, myy, myt);82}83}8485// recycled PathConsumer2D instances from deltaTransformConsumer()86private final DeltaScaleFilter dt_DeltaScaleFilter = new DeltaScaleFilter();87private final DeltaTransformFilter dt_DeltaTransformFilter = new DeltaTransformFilter();8889PathConsumer2D deltaTransformConsumer(PathConsumer2D out,90AffineTransform at)91{92if (at == null) {93return out;94}95float mxx = (float) at.getScaleX();96float mxy = (float) at.getShearX();97float myx = (float) at.getShearY();98float myy = (float) at.getScaleY();99if (mxy == 0f && myx == 0f) {100if (mxx == 1f && myy == 1f) {101return out;102} else {103return dt_DeltaScaleFilter.init(out, mxx, myy);104}105} else {106return dt_DeltaTransformFilter.init(out, mxx, mxy, myx, myy);107}108}109110// recycled PathConsumer2D instances from inverseDeltaTransformConsumer()111private final DeltaScaleFilter iv_DeltaScaleFilter = new DeltaScaleFilter();112private final DeltaTransformFilter iv_DeltaTransformFilter = new DeltaTransformFilter();113114PathConsumer2D inverseDeltaTransformConsumer(PathConsumer2D out,115AffineTransform at)116{117if (at == null) {118return out;119}120float mxx = (float) at.getScaleX();121float mxy = (float) at.getShearX();122float myx = (float) at.getShearY();123float myy = (float) at.getScaleY();124if (mxy == 0f && myx == 0f) {125if (mxx == 1f && myy == 1f) {126return out;127} else {128return iv_DeltaScaleFilter.init(out, 1.0f/mxx, 1.0f/myy);129}130} else {131float det = mxx * myy - mxy * myx;132return iv_DeltaTransformFilter.init(out,133myy / det,134-mxy / det,135-myx / det,136mxx / det);137}138}139140static final class TranslateFilter implements PathConsumer2D {141private PathConsumer2D out;142private float tx, ty;143144TranslateFilter() {}145146TranslateFilter init(PathConsumer2D out,147float tx, float ty)148{149this.out = out;150this.tx = tx;151this.ty = ty;152return this; // fluent API153}154155@Override156public void moveTo(float x0, float y0) {157out.moveTo(x0 + tx, y0 + ty);158}159160@Override161public void lineTo(float x1, float y1) {162out.lineTo(x1 + tx, y1 + ty);163}164165@Override166public void quadTo(float x1, float y1,167float x2, float y2)168{169out.quadTo(x1 + tx, y1 + ty,170x2 + tx, y2 + ty);171}172173@Override174public void curveTo(float x1, float y1,175float x2, float y2,176float x3, float y3)177{178out.curveTo(x1 + tx, y1 + ty,179x2 + tx, y2 + ty,180x3 + tx, y3 + ty);181}182183@Override184public void closePath() {185out.closePath();186}187188@Override189public void pathDone() {190out.pathDone();191}192193@Override194public long getNativeConsumer() {195return 0;196}197}198199static final class ScaleFilter implements PathConsumer2D {200private PathConsumer2D out;201private float sx, sy, tx, ty;202203ScaleFilter() {}204205ScaleFilter init(PathConsumer2D out,206float sx, float sy,207float tx, float ty)208{209this.out = out;210this.sx = sx;211this.sy = sy;212this.tx = tx;213this.ty = ty;214return this; // fluent API215}216217@Override218public void moveTo(float x0, float y0) {219out.moveTo(x0 * sx + tx, y0 * sy + ty);220}221222@Override223public void lineTo(float x1, float y1) {224out.lineTo(x1 * sx + tx, y1 * sy + ty);225}226227@Override228public void quadTo(float x1, float y1,229float x2, float y2)230{231out.quadTo(x1 * sx + tx, y1 * sy + ty,232x2 * sx + tx, y2 * sy + ty);233}234235@Override236public void curveTo(float x1, float y1,237float x2, float y2,238float x3, float y3)239{240out.curveTo(x1 * sx + tx, y1 * sy + ty,241x2 * sx + tx, y2 * sy + ty,242x3 * sx + tx, y3 * sy + ty);243}244245@Override246public void closePath() {247out.closePath();248}249250@Override251public void pathDone() {252out.pathDone();253}254255@Override256public long getNativeConsumer() {257return 0;258}259}260261static final class TransformFilter implements PathConsumer2D {262private PathConsumer2D out;263private float mxx, mxy, mxt, myx, myy, myt;264265TransformFilter() {}266267TransformFilter init(PathConsumer2D out,268float mxx, float mxy, float mxt,269float myx, float myy, float myt)270{271this.out = out;272this.mxx = mxx;273this.mxy = mxy;274this.mxt = mxt;275this.myx = myx;276this.myy = myy;277this.myt = myt;278return this; // fluent API279}280281@Override282public void moveTo(float x0, float y0) {283out.moveTo(x0 * mxx + y0 * mxy + mxt,284x0 * myx + y0 * myy + myt);285}286287@Override288public void lineTo(float x1, float y1) {289out.lineTo(x1 * mxx + y1 * mxy + mxt,290x1 * myx + y1 * myy + myt);291}292293@Override294public void quadTo(float x1, float y1,295float x2, float y2)296{297out.quadTo(x1 * mxx + y1 * mxy + mxt,298x1 * myx + y1 * myy + myt,299x2 * mxx + y2 * mxy + mxt,300x2 * myx + y2 * myy + myt);301}302303@Override304public void curveTo(float x1, float y1,305float x2, float y2,306float x3, float y3)307{308out.curveTo(x1 * mxx + y1 * mxy + mxt,309x1 * myx + y1 * myy + myt,310x2 * mxx + y2 * mxy + mxt,311x2 * myx + y2 * myy + myt,312x3 * mxx + y3 * mxy + mxt,313x3 * myx + y3 * myy + myt);314}315316@Override317public void closePath() {318out.closePath();319}320321@Override322public void pathDone() {323out.pathDone();324}325326@Override327public long getNativeConsumer() {328return 0;329}330}331332static final class DeltaScaleFilter implements PathConsumer2D {333private PathConsumer2D out;334private float sx, sy;335336DeltaScaleFilter() {}337338DeltaScaleFilter init(PathConsumer2D out,339float mxx, float myy)340{341this.out = out;342sx = mxx;343sy = myy;344return this; // fluent API345}346347@Override348public void moveTo(float x0, float y0) {349out.moveTo(x0 * sx, y0 * sy);350}351352@Override353public void lineTo(float x1, float y1) {354out.lineTo(x1 * sx, y1 * sy);355}356357@Override358public void quadTo(float x1, float y1,359float x2, float y2)360{361out.quadTo(x1 * sx, y1 * sy,362x2 * sx, y2 * sy);363}364365@Override366public void curveTo(float x1, float y1,367float x2, float y2,368float x3, float y3)369{370out.curveTo(x1 * sx, y1 * sy,371x2 * sx, y2 * sy,372x3 * sx, y3 * sy);373}374375@Override376public void closePath() {377out.closePath();378}379380@Override381public void pathDone() {382out.pathDone();383}384385@Override386public long getNativeConsumer() {387return 0;388}389}390391static final class DeltaTransformFilter implements PathConsumer2D {392private PathConsumer2D out;393private float mxx, mxy, myx, myy;394395DeltaTransformFilter() {}396397DeltaTransformFilter init(PathConsumer2D out,398float mxx, float mxy,399float myx, float myy)400{401this.out = out;402this.mxx = mxx;403this.mxy = mxy;404this.myx = myx;405this.myy = myy;406return this; // fluent API407}408409@Override410public void moveTo(float x0, float y0) {411out.moveTo(x0 * mxx + y0 * mxy,412x0 * myx + y0 * myy);413}414415@Override416public void lineTo(float x1, float y1) {417out.lineTo(x1 * mxx + y1 * mxy,418x1 * myx + y1 * myy);419}420421@Override422public void quadTo(float x1, float y1,423float x2, float y2)424{425out.quadTo(x1 * mxx + y1 * mxy,426x1 * myx + y1 * myy,427x2 * mxx + y2 * mxy,428x2 * myx + y2 * myy);429}430431@Override432public void curveTo(float x1, float y1,433float x2, float y2,434float x3, float y3)435{436out.curveTo(x1 * mxx + y1 * mxy,437x1 * myx + y1 * myy,438x2 * mxx + y2 * mxy,439x2 * myx + y2 * myy,440x3 * mxx + y3 * mxy,441x3 * myx + y3 * myy);442}443444@Override445public void closePath() {446out.closePath();447}448449@Override450public void pathDone() {451out.pathDone();452}453454@Override455public long getNativeConsumer() {456return 0;457}458}459460static final class Path2DWrapper implements PathConsumer2D {461private Path2D.Float p2d;462463Path2DWrapper() {}464465Path2DWrapper init(Path2D.Float p2d) {466this.p2d = p2d;467return this;468}469470@Override471public void moveTo(float x0, float y0) {472p2d.moveTo(x0, y0);473}474475@Override476public void lineTo(float x1, float y1) {477p2d.lineTo(x1, y1);478}479480@Override481public void closePath() {482p2d.closePath();483}484485@Override486public void pathDone() {}487488@Override489public void curveTo(float x1, float y1,490float x2, float y2,491float x3, float y3)492{493p2d.curveTo(x1, y1, x2, y2, x3, y3);494}495496@Override497public void quadTo(float x1, float y1, float x2, float y2) {498p2d.quadTo(x1, y1, x2, y2);499}500501@Override502public long getNativeConsumer() {503throw new InternalError("Not using a native peer");504}505}506}507508509