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