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 
  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 instances from deltaTransformConsumer()
  38     private final DeltaScaleFilter     dt_DeltaScaleFilter     = new DeltaScaleFilter();
  39     private final DeltaTransformFilter dt_DeltaTransformFilter = new DeltaTransformFilter();
  40 
  41     public DPathConsumer2D deltaTransformConsumer(DPathConsumer2D out,
  42                                                  BaseTransform at)
  43     {
  44         if (at == null) {
  45             return out;
  46         }
  47         double mxx =  at.getMxx();
  48         double mxy =  at.getMxy();
  49         double myx =  at.getMyx();
  50         double myy =  at.getMyy();
  51 
  52         if (mxy == 0d && myx == 0d) {
  53             if (mxx == 1d && myy == 1d) {
  54                 return out;
  55             } else {
  56                 return dt_DeltaScaleFilter.init(out, mxx, myy);
  57             }
  58         } else {
  59             return dt_DeltaTransformFilter.init(out, mxx, mxy, myx, myy);
  60         }
  61     }
  62 
  63     // recycled DPathConsumer2D instances from inverseDeltaTransformConsumer()
  64     private final DeltaScaleFilter     iv_DeltaScaleFilter     = new DeltaScaleFilter();
  65     private final DeltaTransformFilter iv_DeltaTransformFilter = new DeltaTransformFilter();
  66 
  67     public DPathConsumer2D inverseDeltaTransformConsumer(DPathConsumer2D out,
  68                                                         BaseTransform at)
  69     {
  70         if (at == null) {
  71             return out;
  72         }
  73         double mxx =  at.getMxx();
  74         double mxy =  at.getMxy();
  75         double myx =  at.getMyx();
  76         double myy =  at.getMyy();
  77 
  78         if (mxy == 0d && myx == 0d) {
  79             if (mxx == 1d && myy == 1d) {
  80                 return out;
  81             } else {
  82                 return iv_DeltaScaleFilter.init(out, 1.0d/mxx, 1.0d/myy);
  83             }
  84         } else {
  85             double det = mxx * myy - mxy * myx;
  86             return iv_DeltaTransformFilter.init(out,
  87                                                 myy / det,
  88                                                -mxy / det,
  89                                                -myx / det,
  90                                                 mxx / det);
  91         }
  92     }
  93 
  94 
  95     static final class DeltaScaleFilter implements DPathConsumer2D {
  96         private DPathConsumer2D out;
  97         private double sx, sy;
  98 
  99         DeltaScaleFilter() {}
 100 
 101         DeltaScaleFilter init(DPathConsumer2D out,
 102                               double mxx, double myy)
 103         {
 104             this.out = out;
 105             sx = mxx;
 106             sy = myy;
 107             return this; // fluent API
 108         }
 109 
 110         @Override
 111         public void moveTo(double x0, double y0) {
 112             out.moveTo(x0 * sx, y0 * sy);
 113         }
 114 
 115         @Override
 116         public void lineTo(double x1, double y1) {
 117             out.lineTo(x1 * sx, y1 * sy);
 118         }
 119 
 120         @Override
 121         public void quadTo(double x1, double y1,
 122                            double x2, double y2)
 123         {
 124             out.quadTo(x1 * sx, y1 * sy,
 125                        x2 * sx, y2 * sy);
 126         }
 127 
 128         @Override
 129         public void curveTo(double x1, double y1,
 130                             double x2, double y2,
 131                             double x3, double y3)
 132         {
 133             out.curveTo(x1 * sx, y1 * sy,
 134                         x2 * sx, y2 * sy,
 135                         x3 * sx, y3 * sy);
 136         }
 137 
 138         @Override
 139         public void closePath() {
 140             out.closePath();
 141         }
 142 
 143         @Override
 144         public void pathDone() {
 145             out.pathDone();
 146         }
 147     }
 148 
 149     static final class DeltaTransformFilter implements DPathConsumer2D {
 150         private DPathConsumer2D out;
 151         private double mxx, mxy, myx, myy;
 152 
 153         DeltaTransformFilter() {}
 154 
 155         DeltaTransformFilter init(DPathConsumer2D out,
 156                                   double mxx, double mxy,
 157                                   double myx, double myy)
 158         {
 159             this.out = out;
 160             this.mxx = mxx;
 161             this.mxy = mxy;
 162             this.myx = myx;
 163             this.myy = myy;
 164             return this; // fluent API
 165         }
 166 
 167         @Override
 168         public void moveTo(double x0, double y0) {
 169             out.moveTo(x0 * mxx + y0 * mxy,
 170                        x0 * myx + y0 * myy);
 171         }
 172 
 173         @Override
 174         public void lineTo(double x1, double y1) {
 175             out.lineTo(x1 * mxx + y1 * mxy,
 176                        x1 * myx + y1 * myy);
 177         }
 178 
 179         @Override
 180         public void quadTo(double x1, double y1,
 181                            double x2, double y2)
 182         {
 183             out.quadTo(x1 * mxx + y1 * mxy,
 184                        x1 * myx + y1 * myy,
 185                        x2 * mxx + y2 * mxy,
 186                        x2 * myx + y2 * myy);
 187         }
 188 
 189         @Override
 190         public void curveTo(double x1, double y1,
 191                             double x2, double y2,
 192                             double x3, double y3)
 193         {
 194             out.curveTo(x1 * mxx + y1 * mxy,
 195                         x1 * myx + y1 * myy,
 196                         x2 * mxx + y2 * mxy,
 197                         x2 * myx + y2 * myy,
 198                         x3 * mxx + y3 * mxy,
 199                         x3 * myx + y3 * myy);
 200         }
 201 
 202         @Override
 203         public void closePath() {
 204             out.closePath();
 205         }
 206 
 207         @Override
 208         public void pathDone() {
 209             out.pathDone();
 210         }
 211     }
 212 }