1 /*
   2  * Copyright (c) 2007, 2010, 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 sun.java2d.pisces;
  27 
  28 import sun.awt.geom.PathConsumer2D;
  29 import java.awt.geom.AffineTransform;
  30 
  31 public class TransformingPathConsumer2D {
  32     public static PathConsumer2D
  33         transformConsumer(PathConsumer2D out,
  34                           AffineTransform at)
  35     {
  36         if (at == null) {
  37             return out;
  38         }
  39         float Mxx = (float) at.getScaleX();
  40         float Mxy = (float) at.getShearX();
  41         float Mxt = (float) at.getTranslateX();
  42         float Myx = (float) at.getShearY();
  43         float Myy = (float) at.getScaleY();
  44         float Myt = (float) at.getTranslateY();
  45         if (Mxy == 0f && Myx == 0f) {
  46             if (Mxx == 1f && Myy == 1f) {
  47                 if (Mxt == 0f && Myt == 0f) {
  48                     return out;
  49                 } else {
  50                     return new TranslateFilter(out, Mxt, Myt);
  51                 }
  52             } else {
  53                 return new ScaleFilter(out, Mxx, Myy, Mxt, Myt);
  54             }
  55         } else {
  56             return new TransformFilter(out, Mxx, Mxy, Mxt, Myx, Myy, Myt);
  57         }
  58     }
  59 
  60     static class TranslateFilter implements PathConsumer2D {
  61         PathConsumer2D out;
  62         float tx;
  63         float ty;
  64 
  65         TranslateFilter(PathConsumer2D out,
  66                         float tx, float ty)
  67         {
  68             this.out = out;
  69             this.tx = tx;
  70             this.ty = ty;
  71         }
  72 
  73         public void moveTo(float x0, float y0) {
  74             out.moveTo(x0 + tx, y0 + ty);
  75         }
  76 
  77         public void lineTo(float x1, float y1) {
  78             out.lineTo(x1 + tx, y1 + ty);
  79         }
  80 
  81         public void quadTo(float x1, float y1,
  82                            float x2, float y2)
  83         {
  84             out.quadTo(x1 + tx, y1 + ty,
  85                        x2 + tx, y2 + ty);
  86         }
  87 
  88         public void curveTo(float x1, float y1,
  89                             float x2, float y2,
  90                             float x3, float y3)
  91         {
  92             out.curveTo(x1 + tx, y1 + ty,
  93                         x2 + tx, y2 + ty,
  94                         x3 + tx, y3 + ty);
  95         }
  96 
  97         public void closePath() {
  98             out.closePath();
  99         }
 100 
 101         public void pathDone() {
 102             out.pathDone();
 103         }
 104 
 105         public long getNativeConsumer() {
 106             return 0;
 107         }
 108     }
 109 
 110     static class ScaleFilter implements PathConsumer2D {
 111         PathConsumer2D out;
 112         float sx;
 113         float sy;
 114         float tx;
 115         float ty;
 116 
 117         ScaleFilter(PathConsumer2D out,
 118                     float sx, float sy, float tx, float ty)
 119         {
 120             this.out = out;
 121             this.sx = sx;
 122             this.sy = sy;
 123             this.tx = tx;
 124             this.ty = ty;
 125         }
 126 
 127         public void moveTo(float x0, float y0) {
 128             out.moveTo(x0 * sx + tx, y0 * sy + ty);
 129         }
 130 
 131         public void lineTo(float x1, float y1) {
 132             out.lineTo(x1 * sx + tx, y1 * sy + ty);
 133         }
 134 
 135         public void quadTo(float x1, float y1,
 136                            float x2, float y2)
 137         {
 138             out.quadTo(x1 * sx + tx, y1 * sy + ty,
 139                        x2 * sx + tx, y2 * sy + ty);
 140         }
 141 
 142         public void curveTo(float x1, float y1,
 143                             float x2, float y2,
 144                             float x3, float y3)
 145         {
 146             out.curveTo(x1 * sx + tx, y1 * sy + ty,
 147                         x2 * sx + tx, y2 * sy + ty,
 148                         x3 * sx + tx, y3 * sy + ty);
 149         }
 150 
 151         public void closePath() {
 152             out.closePath();
 153         }
 154 
 155         public void pathDone() {
 156             out.pathDone();
 157         }
 158 
 159         public long getNativeConsumer() {
 160             return 0;
 161         }
 162     }
 163 
 164     static class TransformFilter implements PathConsumer2D {
 165         PathConsumer2D out;
 166         float Mxx;
 167         float Mxy;
 168         float Mxt;
 169         float Myx;
 170         float Myy;
 171         float Myt;
 172 
 173         TransformFilter(PathConsumer2D out,
 174                         float Mxx, float Mxy, float Mxt,
 175                         float Myx, float Myy, float Myt)
 176         {
 177             this.out = out;
 178             this.Mxx = Mxx;
 179             this.Mxy = Mxy;
 180             this.Mxt = Mxt;
 181             this.Myx = Myx;
 182             this.Myy = Myy;
 183             this.Myt = Myt;
 184         }
 185 
 186         public void moveTo(float x0, float y0) {
 187             out.moveTo(x0 * Mxx + y0 * Mxy + Mxt,
 188                        x0 * Myx + y0 * Myy + Myt);
 189         }
 190 
 191         public void lineTo(float x1, float y1) {
 192             out.lineTo(x1 * Mxx + y1 * Mxy + Mxt,
 193                        x1 * Myx + y1 * Myy + Myt);
 194         }
 195 
 196         public void quadTo(float x1, float y1,
 197                            float x2, float y2)
 198         {
 199             out.quadTo(x1 * Mxx + y1 * Mxy + Mxt,
 200                        x1 * Myx + y1 * Myy + Myt,
 201                        x2 * Mxx + y2 * Mxy + Mxt,
 202                        x2 * Myx + y2 * Myy + Myt);
 203         }
 204 
 205         public void curveTo(float x1, float y1,
 206                             float x2, float y2,
 207                             float x3, float y3)
 208         {
 209             out.curveTo(x1 * Mxx + y1 * Mxy + Mxt,
 210                         x1 * Myx + y1 * Myy + Myt,
 211                         x2 * Mxx + y2 * Mxy + Mxt,
 212                         x2 * Myx + y2 * Myy + Myt,
 213                         x3 * Mxx + y3 * Mxy + Mxt,
 214                         x3 * Myx + y3 * Myy + Myt);
 215         }
 216 
 217         public void closePath() {
 218             out.closePath();
 219         }
 220 
 221         public void pathDone() {
 222             out.pathDone();
 223         }
 224 
 225         public long getNativeConsumer() {
 226             return 0;
 227         }
 228     }
 229 }