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