1 /*
   2  * Copyright (c) 2007, 2017, 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 com.sun.marlin;
  27 
  28 import com.sun.javafx.geom.Path2D;
  29 import com.sun.javafx.geom.PathConsumer2D;
  30 import com.sun.javafx.geom.transform.BaseTransform;
  31 
  32 public final class TransformingPathConsumer2D {
  33 
  34     TransformingPathConsumer2D() {
  35         // used by RendererContext
  36     }
  37 
  38     // recycled PathConsumer2D instance from wrapPath2d()
  39     private final Path2DWrapper        wp_Path2DWrapper        = new Path2DWrapper();
  40 
  41     // recycled PathConsumer2D instances from deltaTransformConsumer()
  42     private final DeltaScaleFilter     dt_DeltaScaleFilter     = new DeltaScaleFilter();
  43     private final DeltaTransformFilter dt_DeltaTransformFilter = new DeltaTransformFilter();
  44 
  45     public PathConsumer2D wrapPath2D(Path2D p2d) {
  46         return wp_Path2DWrapper.init(p2d);
  47     }
  48 
  49     public PathConsumer2D deltaTransformConsumer(PathConsumer2D out,
  50                                                  BaseTransform at)
  51     {
  52         if (at == null) {
  53             return out;
  54         }
  55         float mxx = (float) at.getMxx();
  56         float mxy = (float) at.getMxy();
  57         float myx = (float) at.getMyx();
  58         float myy = (float) at.getMyy();
  59 
  60         if (mxy == 0.0f && myx == 0.0f) {
  61             if (mxx == 1.0f && myy == 1.0f) {
  62                 return out;
  63             } else {
  64                 return dt_DeltaScaleFilter.init(out, mxx, myy);
  65             }
  66         } else {
  67             return dt_DeltaTransformFilter.init(out, mxx, mxy, myx, myy);
  68         }
  69     }
  70 
  71     // recycled PathConsumer2D instances from inverseDeltaTransformConsumer()
  72     private final DeltaScaleFilter     iv_DeltaScaleFilter     = new DeltaScaleFilter();
  73     private final DeltaTransformFilter iv_DeltaTransformFilter = new DeltaTransformFilter();
  74 
  75     public PathConsumer2D inverseDeltaTransformConsumer(PathConsumer2D out,
  76                                                         BaseTransform at)
  77     {
  78         if (at == null) {
  79             return out;
  80         }
  81         float mxx = (float) at.getMxx();
  82         float mxy = (float) at.getMxy();
  83         float myx = (float) at.getMyx();
  84         float myy = (float) at.getMyy();
  85 
  86         if (mxy == 0.0f && myx == 0.0f) {
  87             if (mxx == 1.0f && myy == 1.0f) {
  88                 return out;
  89             } else {
  90                 return iv_DeltaScaleFilter.init(out, 1.0f/mxx, 1.0f/myy);
  91             }
  92         } else {
  93             float det = mxx * myy - mxy * myx;
  94             return iv_DeltaTransformFilter.init(out,
  95                                                 myy / det,
  96                                                -mxy / det,
  97                                                -myx / det,
  98                                                 mxx / det);
  99         }
 100     }
 101 
 102     static final class DeltaScaleFilter implements PathConsumer2D {
 103         private PathConsumer2D out;
 104         private float sx, sy;
 105 
 106         DeltaScaleFilter() {}
 107 
 108         DeltaScaleFilter init(PathConsumer2D out,
 109                               float mxx, float myy)
 110         {
 111             this.out = out;
 112             sx = mxx;
 113             sy = myy;
 114             return this; // fluent API
 115         }
 116 
 117         @Override
 118         public void moveTo(float x0, float y0) {
 119             out.moveTo(x0 * sx, y0 * sy);
 120         }
 121 
 122         @Override
 123         public void lineTo(float x1, float y1) {
 124             out.lineTo(x1 * sx, y1 * sy);
 125         }
 126 
 127         @Override
 128         public void quadTo(float x1, float y1,
 129                            float x2, float y2)
 130         {
 131             out.quadTo(x1 * sx, y1 * sy,
 132                        x2 * sx, y2 * sy);
 133         }
 134 
 135         @Override
 136         public void curveTo(float x1, float y1,
 137                             float x2, float y2,
 138                             float x3, float y3)
 139         {
 140             out.curveTo(x1 * sx, y1 * sy,
 141                         x2 * sx, y2 * sy,
 142                         x3 * sx, y3 * sy);
 143         }
 144 
 145         @Override
 146         public void closePath() {
 147             out.closePath();
 148         }
 149 
 150         @Override
 151         public void pathDone() {
 152             out.pathDone();
 153         }
 154     }
 155 
 156     static final class DeltaTransformFilter implements PathConsumer2D {
 157         private PathConsumer2D out;
 158         private float mxx, mxy, myx, myy;
 159 
 160         DeltaTransformFilter() {}
 161 
 162         DeltaTransformFilter init(PathConsumer2D out,
 163                                   float mxx, float mxy,
 164                                   float myx, float myy)
 165         {
 166             this.out = out;
 167             this.mxx = mxx;
 168             this.mxy = mxy;
 169             this.myx = myx;
 170             this.myy = myy;
 171             return this; // fluent API
 172         }
 173 
 174         @Override
 175         public void moveTo(float x0, float y0) {
 176             out.moveTo(x0 * mxx + y0 * mxy,
 177                        x0 * myx + y0 * myy);
 178         }
 179 
 180         @Override
 181         public void lineTo(float x1, float y1) {
 182             out.lineTo(x1 * mxx + y1 * mxy,
 183                        x1 * myx + y1 * myy);
 184         }
 185 
 186         @Override
 187         public void quadTo(float x1, float y1,
 188                            float x2, float y2)
 189         {
 190             out.quadTo(x1 * mxx + y1 * mxy,
 191                        x1 * myx + y1 * myy,
 192                        x2 * mxx + y2 * mxy,
 193                        x2 * myx + y2 * myy);
 194         }
 195 
 196         @Override
 197         public void curveTo(float x1, float y1,
 198                             float x2, float y2,
 199                             float x3, float y3)
 200         {
 201             out.curveTo(x1 * mxx + y1 * mxy,
 202                         x1 * myx + y1 * myy,
 203                         x2 * mxx + y2 * mxy,
 204                         x2 * myx + y2 * myy,
 205                         x3 * mxx + y3 * mxy,
 206                         x3 * myx + y3 * myy);
 207         }
 208 
 209         @Override
 210         public void closePath() {
 211             out.closePath();
 212         }
 213 
 214         @Override
 215         public void pathDone() {
 216             out.pathDone();
 217         }
 218     }
 219 
 220     static final class Path2DWrapper implements PathConsumer2D {
 221         private Path2D p2d;
 222 
 223         Path2DWrapper() {}
 224 
 225         Path2DWrapper init(Path2D p2d) {
 226             this.p2d = p2d;
 227             return this;
 228         }
 229 
 230         @Override
 231         public void moveTo(float x0, float y0) {
 232             p2d.moveTo(x0, y0);
 233         }
 234 
 235         @Override
 236         public void lineTo(float x1, float y1) {
 237             p2d.lineTo(x1, y1);
 238         }
 239 
 240         @Override
 241         public void closePath() {
 242             p2d.closePath();
 243         }
 244 
 245         @Override
 246         public void pathDone() {}
 247 
 248         @Override
 249         public void curveTo(float x1, float y1,
 250                             float x2, float y2,
 251                             float x3, float y3)
 252         {
 253             p2d.curveTo(x1, y1, x2, y2, x3, y3);
 254         }
 255 
 256         @Override
 257         public void quadTo(float x1, float y1, float x2, float y2) {
 258             p2d.quadTo(x1, y1, x2, y2);
 259         }
 260     }
 261 }