jdk/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
author dlila
Tue, 10 Aug 2010 13:19:44 -0400
changeset 6284 695b3c6241c8
parent 5506 202f599c92aa
child 6285 96a57de47def
permissions -rw-r--r--
6967436: lines longer than 2^15 can fill window. 6967433: dashed lines broken when using scaling transforms. Summary: converted pisces to floating point. Also, using better AA algorithm Reviewed-by: flar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3927
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3927
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3927
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3927
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3927
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3927
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.java2d.pisces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.BasicStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.Path2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.geom.PathIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.awt.geom.PathConsumer2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.java2d.pipe.Region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.java2d.pipe.RenderingEngine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.java2d.pipe.AATileGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class PiscesRenderingEngine extends RenderingEngine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static double defaultFlat = 0.1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Create a widened path as specified by the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * The specified {@code src} {@link Shape} is widened according
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * to the specified attribute parameters as per the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * {@link BasicStroke} specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param src the source path to be widened
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param width the width of the widened path as per {@code BasicStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param caps the end cap decorations as per {@code BasicStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param join the segment join decorations as per {@code BasicStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param miterlimit the miter limit as per {@code BasicStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param dashes the dash length array as per {@code BasicStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param dashphase the initial dash phase as per {@code BasicStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @return the widened path stored in a new {@code Shape} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public Shape createStrokedShape(Shape src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                    float width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                    int caps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                    int join,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                    float miterlimit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                    float dashes[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                    float dashphase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        final Path2D p2d = new Path2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        strokeTo(src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                 null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                 width,
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
    72
                 false,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                 caps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                 join,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                 miterlimit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                 dashes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                 dashphase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                 new LineSink() {
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
    79
                     public void moveTo(float x0, float y0) {
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
    80
                         p2d.moveTo(x0, y0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                     public void lineJoin() {}
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
    83
                     public void lineTo(float x1, float y1) {
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
    84
                         p2d.lineTo(x1, y1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                     public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                         p2d.closePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                     public void end() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return p2d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Sends the geometry for a widened path as specified by the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * to the specified consumer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The specified {@code src} {@link Shape} is widened according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * to the parameters specified by the {@link BasicStroke} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Adjustments are made to the path as appropriate for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * {@link VALUE_STROKE_NORMALIZE} hint if the {@code normalize}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * boolean parameter is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Adjustments are made to the path as appropriate for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * {@link VALUE_ANTIALIAS_ON} hint if the {@code antialias}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * boolean parameter is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The geometry of the widened path is forwarded to the indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * {@link PathConsumer2D} object as it is calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param src the source path to be widened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param bs the {@code BasicSroke} object specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *           decorations to be applied to the widened path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param normalize indicates whether stroke normalization should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                  be applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param antialias indicates whether or not adjustments appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *                  to antialiased rendering should be applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param consumer the {@code PathConsumer2D} instance to forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *                 the widened geometry to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void strokeTo(Shape src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                         AffineTransform at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                         BasicStroke bs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                         boolean thin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                         boolean normalize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                         boolean antialias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                         final PathConsumer2D consumer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        strokeTo(src, at, bs, thin, normalize, antialias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                 new LineSink() {
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   132
                     public void moveTo(float x0, float y0) {
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   133
                         consumer.moveTo(x0, y0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                     public void lineJoin() {}
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   136
                     public void lineTo(float x1, float y1) {
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   137
                         consumer.lineTo(x1, y1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                     public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                         consumer.closePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                     public void end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                         consumer.pathDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    void strokeTo(Shape src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                  AffineTransform at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                  BasicStroke bs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                  boolean thin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                  boolean normalize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                  boolean antialias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                  LineSink lsink)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        float lw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (thin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (antialias) {
3927
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   159
                lw = userSpaceLineWidth(at, 0.5f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else {
3927
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   161
                lw = userSpaceLineWidth(at, 1.0f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            lw = bs.getLineWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        strokeTo(src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 lw,
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   169
                 normalize,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                 bs.getEndCap(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                 bs.getLineJoin(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                 bs.getMiterLimit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                 bs.getDashArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                 bs.getDashPhase(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                 lsink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
3927
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   178
    private float userSpaceLineWidth(AffineTransform at, float lw) {
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   179
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   180
        double widthScale;
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   181
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   182
        if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM |
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   183
                            AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   184
            widthScale = Math.sqrt(at.getDeterminant());
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   185
        } else {
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   186
            /* First calculate the "maximum scale" of this transform. */
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   187
            double A = at.getScaleX();       // m00
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   188
            double C = at.getShearX();       // m01
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   189
            double B = at.getShearY();       // m10
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   190
            double D = at.getScaleY();       // m11
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   191
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   192
            /*
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   193
             * Given a 2 x 2 affine matrix [ A B ] such that
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   194
             *                             [ C D ]
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   195
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   196
             * find the maximum magnitude (norm) of the vector v'
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   197
             * with the constraint (x^2 + y^2 = 1).
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   198
             * The equation to maximize is
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   199
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   200
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   201
             * Since sqrt is monotonic we can maximize |v'|^2
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   202
             * instead and plug in the substitution y = sqrt(1 - x^2).
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   203
             * Trigonometric equalities can then be used to get
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   204
             * rid of most of the sqrt terms.
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   205
             */
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   206
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   207
            double EA = A*A + B*B;          // x^2 coefficient
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   208
            double EB = 2*(A*C + B*D);      // xy coefficient
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   209
            double EC = C*C + D*D;          // y^2 coefficient
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   210
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   211
            /*
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   212
             * There is a lot of calculus omitted here.
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   213
             *
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   214
             * Conceptually, in the interests of understanding the
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   215
             * terms that the calculus produced we can consider
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   216
             * that EA and EC end up providing the lengths along
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   217
             * the major axes and the hypot term ends up being an
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   218
             * adjustment for the additional length along the off-axis
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   219
             * angle of rotated or sheared ellipses as well as an
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   220
             * adjustment for the fact that the equation below
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   221
             * averages the two major axis lengths.  (Notice that
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   222
             * the hypot term contains a part which resolves to the
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   223
             * difference of these two axis lengths in the absence
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   224
             * of rotation.)
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   225
             *
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   226
             * In the calculus, the ratio of the EB and (EA-EC) terms
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   227
             * ends up being the tangent of 2*theta where theta is
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   228
             * the angle that the long axis of the ellipse makes
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   229
             * with the horizontal axis.  Thus, this equation is
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   230
             * calculating the length of the hypotenuse of a triangle
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   231
             * along that axis.
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   232
             */
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   233
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   234
            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   235
            /* sqrt omitted, compare to squared limits below. */
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   236
            double widthsquared = ((EA + EC + hypot)/2.0);
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   237
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   238
            widthScale = Math.sqrt(widthsquared);
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   239
        }
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   240
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   241
        return (float) (lw / widthScale);
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   242
    }
d717df90e151 6829673: ThinLineTest: A line < 1 pixel disappears.
jgodinez
parents: 2391
diff changeset
   243
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    void strokeTo(Shape src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                  AffineTransform at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                  float width,
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   247
                  boolean normalize,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                  int caps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                  int join,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                  float miterlimit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                  float dashes[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                  float dashphase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                  LineSink lsink)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    {
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   255
        float a00 = 1f, a01 = 0f, a10 = 0f, a11 = 1f;
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   256
        if (at != null && !at.isIdentity()) {
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   257
            a00 = (float)at.getScaleX();
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   258
            a01 = (float)at.getShearX();
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   259
            a10 = (float)at.getShearY();
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   260
            a11 = (float)at.getScaleY();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   262
        lsink = new Stroker(lsink, width, caps, join, miterlimit, a00, a01, a10, a11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (dashes != null) {
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   264
            lsink = new Dasher(lsink, dashes, dashphase, a00, a01, a10, a11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        PathIterator pi = src.getPathIterator(at, defaultFlat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        pathTo(pi, lsink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    void pathTo(PathIterator pi, LineSink lsink) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        float coords[] = new float[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        while (!pi.isDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            switch (pi.currentSegment(coords)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            case PathIterator.SEG_MOVETO:
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   276
                lsink.moveTo(coords[0], coords[1]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            case PathIterator.SEG_LINETO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                lsink.lineJoin();
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   280
                lsink.lineTo(coords[0], coords[1]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            case PathIterator.SEG_CLOSE:
2391
3397fe90e591 6812600: The miter line join decoration isn't rendered properly
jgodinez
parents: 2
diff changeset
   283
                lsink.lineJoin();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                lsink.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                throw new InternalError("unknown flattened segment type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            pi.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        lsink.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Construct an antialiased tile generator for the given shape with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * the given rendering attributes and store the bounds of the tile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * iteration in the bbox parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * The {@code at} parameter specifies a transform that should affect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * both the shape and the {@code BasicStroke} attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * The {@code clip} parameter specifies the current clip in effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * in device coordinates and can be used to prune the data for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * operation, but the renderer is not required to perform any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * clipping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * If the {@code BasicStroke} parameter is null then the shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * should be filled as is, otherwise the attributes of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * {@code BasicStroke} should be used to specify a draw operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The {@code thin} parameter indicates whether or not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * transformed {@code BasicStroke} represents coordinates smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * than the minimum resolution of the antialiasing rasterizer as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * specified by the {@code getMinimumAAPenWidth()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Upon returning, this method will fill the {@code bbox} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * with 4 values indicating the bounds of the iteration of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * tile generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * The iteration order of the tiles will be as specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * pseudo-code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *     for (y = bbox[1]; y < bbox[3]; y += tileheight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *         for (x = bbox[0]; x < bbox[2]; x += tilewidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * If there is no output to be rendered, this method may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param s the shape to be rendered (fill or draw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param at the transform to be applied to the shape and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *           stroke attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param clip the current clip in effect in device coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param bs if non-null, a {@code BasicStroke} whose attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *           should be applied to this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param thin true if the transformed stroke attributes are smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *             than the minimum dropout pen width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param normalize true if the {@code VALUE_STROKE_NORMALIZE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *                  {@code RenderingHint} is in effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param bbox returns the bounds of the iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return the {@code AATileGenerator} instance to be consulted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *         for tile coverages, or null if there is no output to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public AATileGenerator getAATileGenerator(Shape s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                              AffineTransform at,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                              Region clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                              BasicStroke bs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                              boolean thin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                              boolean normalize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                              int bbox[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        PiscesCache pc = PiscesCache.createInstance();
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   350
        Renderer r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (bs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            PathIterator pi = s.getPathIterator(at, defaultFlat);
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   353
            r = new Renderer(3, 3,
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   354
                             clip.getLoX(), clip.getLoY(),
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   355
                             clip.getWidth(), clip.getHeight(),
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   356
                             pi.getWindingRule(), pc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            pathTo(pi, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } else {
6284
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   359
            r = new Renderer(3, 3,
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   360
                             clip.getLoX(), clip.getLoY(),
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   361
                             clip.getWidth(), clip.getHeight(),
695b3c6241c8 6967436: lines longer than 2^15 can fill window.
dlila
parents: 5506
diff changeset
   362
                             PathIterator.WIND_NON_ZERO, pc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            strokeTo(s, at, bs, thin, normalize, true, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        r.endRendering();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        PiscesTileGenerator ptg = new PiscesTileGenerator(pc, r.MAX_AA_ALPHA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        ptg.getBbox(bbox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return ptg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns the minimum pen width that the antialiasing rasterizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * can represent without dropouts occuring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public float getMinimumAAPenSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (PathIterator.WIND_NON_ZERO != Renderer.WIND_NON_ZERO ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            PathIterator.WIND_EVEN_ODD != Renderer.WIND_EVEN_ODD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            BasicStroke.JOIN_MITER != Stroker.JOIN_MITER ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            BasicStroke.JOIN_ROUND != Stroker.JOIN_ROUND ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            BasicStroke.JOIN_BEVEL != Stroker.JOIN_BEVEL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            BasicStroke.CAP_BUTT != Stroker.CAP_BUTT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            BasicStroke.CAP_ROUND != Stroker.CAP_ROUND ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            BasicStroke.CAP_SQUARE != Stroker.CAP_SQUARE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw new InternalError("mismatched renderer constants");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}