src/java.desktop/share/classes/sun/java2d/marlin/DDasher.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 47126 jdk/src/java.desktop/share/classes/sun/java2d/marlin/DDasher.java@188ef162f019
child 48284 fd7fbc929001
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47126
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     1
/*
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     2
 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     4
 *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    10
 *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    15
 * accompanied this code).
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    16
 *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    20
 *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    23
 * questions.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    24
 */
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    25
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    26
package sun.java2d.marlin;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    27
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    28
import java.util.Arrays;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    29
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    30
/**
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    31
 * The <code>DDasher</code> class takes a series of linear commands
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    32
 * (<code>moveTo</code>, <code>lineTo</code>, <code>close</code> and
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    33
 * <code>end</code>) and breaks them into smaller segments according to a
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    34
 * dash pattern array and a starting dash phase.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    35
 *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    36
 * <p> Issues: in J2Se, a zero length dash segment as drawn as a very
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    37
 * short dash, whereas Pisces does not draw anything.  The PostScript
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    38
 * semantics are unclear.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    39
 *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    40
 */
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    41
final class DDasher implements DPathConsumer2D, MarlinConst {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    42
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    43
    static final int REC_LIMIT = 4;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    44
    static final double ERR = 0.01d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    45
    static final double MIN_T_INC = 1.0d / (1 << REC_LIMIT);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    46
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    47
    // More than 24 bits of mantissa means we can no longer accurately
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    48
    // measure the number of times cycled through the dash array so we
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    49
    // punt and override the phase to just be 0 past that point.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    50
    static final double MAX_CYCLES = 16000000.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    51
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    52
    private DPathConsumer2D out;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    53
    private double[] dash;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    54
    private int dashLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    55
    private double startPhase;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    56
    private boolean startDashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    57
    private int startIdx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    58
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    59
    private boolean starting;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    60
    private boolean needsMoveTo;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    61
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    62
    private int idx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    63
    private boolean dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    64
    private double phase;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    65
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    66
    private double sx, sy;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    67
    private double x0, y0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    68
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    69
    // temporary storage for the current curve
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    70
    private final double[] curCurvepts;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    71
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    72
    // per-thread renderer context
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    73
    final DRendererContext rdrCtx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    74
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    75
    // flag to recycle dash array copy
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    76
    boolean recycleDashes;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    77
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    78
    // dashes ref (dirty)
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    79
    final DoubleArrayCache.Reference dashes_ref;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    80
    // firstSegmentsBuffer ref (dirty)
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    81
    final DoubleArrayCache.Reference firstSegmentsBuffer_ref;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    82
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    83
    /**
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    84
     * Constructs a <code>DDasher</code>.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    85
     * @param rdrCtx per-thread renderer context
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    86
     */
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    87
    DDasher(final DRendererContext rdrCtx) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    88
        this.rdrCtx = rdrCtx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    89
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    90
        dashes_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_ARRAY); // 1K
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    91
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    92
        firstSegmentsBuffer_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_ARRAY); // 1K
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    93
        firstSegmentsBuffer     = firstSegmentsBuffer_ref.initial;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    94
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    95
        // we need curCurvepts to be able to contain 2 curves because when
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    96
        // dashing curves, we need to subdivide it
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    97
        curCurvepts = new double[8 * 2];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    98
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
    99
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   100
    /**
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   101
     * Initialize the <code>DDasher</code>.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   102
     *
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   103
     * @param out an output <code>DPathConsumer2D</code>.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   104
     * @param dash an array of <code>double</code>s containing the dash pattern
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   105
     * @param dashLen length of the given dash array
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   106
     * @param phase a <code>double</code> containing the dash phase
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   107
     * @param recycleDashes true to indicate to recycle the given dash array
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   108
     * @return this instance
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   109
     */
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   110
    DDasher init(final DPathConsumer2D out, double[] dash, int dashLen,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   111
                double phase, boolean recycleDashes)
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   112
    {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   113
        this.out = out;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   114
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   115
        // Normalize so 0 <= phase < dash[0]
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   116
        int sidx = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   117
        dashOn = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   118
        double sum = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   119
        for (double d : dash) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   120
            sum += d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   121
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   122
        double cycles = phase / sum;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   123
        if (phase < 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   124
            if (-cycles >= MAX_CYCLES) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   125
                phase = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   126
            } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   127
                int fullcycles = FloatMath.floor_int(-cycles);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   128
                if ((fullcycles & dash.length & 1) != 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   129
                    dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   130
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   131
                phase += fullcycles * sum;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   132
                while (phase < 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   133
                    if (--sidx < 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   134
                        sidx = dash.length - 1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   135
                    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   136
                    phase += dash[sidx];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   137
                    dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   138
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   139
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   140
        } else if (phase > 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   141
            if (cycles >= MAX_CYCLES) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   142
                phase = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   143
            } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   144
                int fullcycles = FloatMath.floor_int(cycles);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   145
                if ((fullcycles & dash.length & 1) != 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   146
                    dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   147
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   148
                phase -= fullcycles * sum;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   149
                double d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   150
                while (phase >= (d = dash[sidx])) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   151
                    phase -= d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   152
                    sidx = (sidx + 1) % dash.length;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   153
                    dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   154
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   155
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   156
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   157
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   158
        this.dash = dash;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   159
        this.dashLen = dashLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   160
        this.startPhase = this.phase = phase;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   161
        this.startDashOn = dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   162
        this.startIdx = sidx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   163
        this.starting = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   164
        needsMoveTo = false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   165
        firstSegidx = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   166
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   167
        this.recycleDashes = recycleDashes;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   168
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   169
        return this; // fluent API
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   170
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   171
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   172
    /**
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   173
     * Disposes this dasher:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   174
     * clean up before reusing this instance
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   175
     */
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   176
    void dispose() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   177
        if (DO_CLEAN_DIRTY) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   178
            // Force zero-fill dirty arrays:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   179
            Arrays.fill(curCurvepts, 0.0d);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   180
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   181
        // Return arrays:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   182
        if (recycleDashes) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   183
            dash = dashes_ref.putArray(dash);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   184
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   185
        firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   186
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   187
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   188
    double[] copyDashArray(final float[] dashes) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   189
        final int len = dashes.length;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   190
        final double[] newDashes;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   191
        if (len <= MarlinConst.INITIAL_ARRAY) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   192
            newDashes = dashes_ref.initial;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   193
        } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   194
            if (DO_STATS) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   195
                rdrCtx.stats.stat_array_dasher_dasher.add(len);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   196
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   197
            newDashes = dashes_ref.getArray(len);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   198
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   199
        for (int i = 0; i < len; i++) { newDashes[i] = dashes[i]; }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   200
        return newDashes;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   201
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   202
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   203
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   204
    public void moveTo(double x0, double y0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   205
        if (firstSegidx > 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   206
            out.moveTo(sx, sy);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   207
            emitFirstSegments();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   208
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   209
        needsMoveTo = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   210
        this.idx = startIdx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   211
        this.dashOn = this.startDashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   212
        this.phase = this.startPhase;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   213
        this.sx = this.x0 = x0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   214
        this.sy = this.y0 = y0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   215
        this.starting = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   216
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   217
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   218
    private void emitSeg(double[] buf, int off, int type) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   219
        switch (type) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   220
        case 8:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   221
            out.curveTo(buf[off+0], buf[off+1],
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   222
                        buf[off+2], buf[off+3],
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   223
                        buf[off+4], buf[off+5]);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   224
            return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   225
        case 6:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   226
            out.quadTo(buf[off+0], buf[off+1],
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   227
                       buf[off+2], buf[off+3]);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   228
            return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   229
        case 4:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   230
            out.lineTo(buf[off], buf[off+1]);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   231
            return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   232
        default:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   233
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   234
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   235
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   236
    private void emitFirstSegments() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   237
        final double[] fSegBuf = firstSegmentsBuffer;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   238
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   239
        for (int i = 0; i < firstSegidx; ) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   240
            int type = (int)fSegBuf[i];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   241
            emitSeg(fSegBuf, i + 1, type);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   242
            i += (type - 1);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   243
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   244
        firstSegidx = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   245
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   246
    // We don't emit the first dash right away. If we did, caps would be
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   247
    // drawn on it, but we need joins to be drawn if there's a closePath()
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   248
    // So, we store the path elements that make up the first dash in the
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   249
    // buffer below.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   250
    private double[] firstSegmentsBuffer; // dynamic array
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   251
    private int firstSegidx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   252
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   253
    // precondition: pts must be in relative coordinates (relative to x0,y0)
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   254
    private void goTo(double[] pts, int off, final int type) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   255
        double x = pts[off + type - 4];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   256
        double y = pts[off + type - 3];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   257
        if (dashOn) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   258
            if (starting) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   259
                int len = type - 1; // - 2 + 1
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   260
                int segIdx = firstSegidx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   261
                double[] buf = firstSegmentsBuffer;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   262
                if (segIdx + len  > buf.length) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   263
                    if (DO_STATS) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   264
                        rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   265
                            .add(segIdx + len);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   266
                    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   267
                    firstSegmentsBuffer = buf
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   268
                        = firstSegmentsBuffer_ref.widenArray(buf, segIdx,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   269
                                                             segIdx + len);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   270
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   271
                buf[segIdx++] = type;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   272
                len--;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   273
                // small arraycopy (2, 4 or 6) but with offset:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   274
                System.arraycopy(pts, off, buf, segIdx, len);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   275
                segIdx += len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   276
                firstSegidx = segIdx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   277
            } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   278
                if (needsMoveTo) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   279
                    out.moveTo(x0, y0);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   280
                    needsMoveTo = false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   281
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   282
                emitSeg(pts, off, type);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   283
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   284
        } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   285
            starting = false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   286
            needsMoveTo = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   287
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   288
        this.x0 = x;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   289
        this.y0 = y;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   290
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   291
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   292
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   293
    public void lineTo(double x1, double y1) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   294
        double dx = x1 - x0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   295
        double dy = y1 - y0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   296
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   297
        double len = dx*dx + dy*dy;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   298
        if (len == 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   299
            return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   300
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   301
        len = Math.sqrt(len);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   302
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   303
        // The scaling factors needed to get the dx and dy of the
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   304
        // transformed dash segments.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   305
        final double cx = dx / len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   306
        final double cy = dy / len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   307
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   308
        final double[] _curCurvepts = curCurvepts;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   309
        final double[] _dash = dash;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   310
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   311
        double leftInThisDashSegment;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   312
        double dashdx, dashdy, p;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   313
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   314
        while (true) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   315
            leftInThisDashSegment = _dash[idx] - phase;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   316
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   317
            if (len <= leftInThisDashSegment) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   318
                _curCurvepts[0] = x1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   319
                _curCurvepts[1] = y1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   320
                goTo(_curCurvepts, 0, 4);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   321
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   322
                // Advance phase within current dash segment
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   323
                phase += len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   324
                // TODO: compare double values using epsilon:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   325
                if (len == leftInThisDashSegment) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   326
                    phase = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   327
                    idx = (idx + 1) % dashLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   328
                    dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   329
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   330
                return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   331
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   332
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   333
            dashdx = _dash[idx] * cx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   334
            dashdy = _dash[idx] * cy;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   335
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   336
            if (phase == 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   337
                _curCurvepts[0] = x0 + dashdx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   338
                _curCurvepts[1] = y0 + dashdy;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   339
            } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   340
                p = leftInThisDashSegment / _dash[idx];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   341
                _curCurvepts[0] = x0 + p * dashdx;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   342
                _curCurvepts[1] = y0 + p * dashdy;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   343
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   344
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   345
            goTo(_curCurvepts, 0, 4);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   346
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   347
            len -= leftInThisDashSegment;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   348
            // Advance to next dash segment
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   349
            idx = (idx + 1) % dashLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   350
            dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   351
            phase = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   352
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   353
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   354
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   355
    // shared instance in DDasher
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   356
    private final LengthIterator li = new LengthIterator();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   357
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   358
    // preconditions: curCurvepts must be an array of length at least 2 * type,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   359
    // that contains the curve we want to dash in the first type elements
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   360
    private void somethingTo(int type) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   361
        if (pointCurve(curCurvepts, type)) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   362
            return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   363
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   364
        li.initializeIterationOnCurve(curCurvepts, type);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   365
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   366
        // initially the current curve is at curCurvepts[0...type]
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   367
        int curCurveoff = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   368
        double lastSplitT = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   369
        double t;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   370
        double leftInThisDashSegment = dash[idx] - phase;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   371
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   372
        while ((t = li.next(leftInThisDashSegment)) < 1.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   373
            if (t != 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   374
                DHelpers.subdivideAt((t - lastSplitT) / (1.0d - lastSplitT),
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   375
                                    curCurvepts, curCurveoff,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   376
                                    curCurvepts, 0,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   377
                                    curCurvepts, type, type);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   378
                lastSplitT = t;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   379
                goTo(curCurvepts, 2, type);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   380
                curCurveoff = type;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   381
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   382
            // Advance to next dash segment
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   383
            idx = (idx + 1) % dashLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   384
            dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   385
            phase = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   386
            leftInThisDashSegment = dash[idx];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   387
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   388
        goTo(curCurvepts, curCurveoff+2, type);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   389
        phase += li.lastSegLen();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   390
        if (phase >= dash[idx]) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   391
            phase = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   392
            idx = (idx + 1) % dashLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   393
            dashOn = !dashOn;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   394
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   395
        // reset LengthIterator:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   396
        li.reset();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   397
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   398
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   399
    private static boolean pointCurve(double[] curve, int type) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   400
        for (int i = 2; i < type; i++) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   401
            if (curve[i] != curve[i-2]) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   402
                return false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   403
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   404
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   405
        return true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   406
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   407
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   408
    // Objects of this class are used to iterate through curves. They return
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   409
    // t values where the left side of the curve has a specified length.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   410
    // It does this by subdividing the input curve until a certain error
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   411
    // condition has been met. A recursive subdivision procedure would
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   412
    // return as many as 1<<limit curves, but this is an iterator and we
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   413
    // don't need all the curves all at once, so what we carry out a
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   414
    // lazy inorder traversal of the recursion tree (meaning we only move
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   415
    // through the tree when we need the next subdivided curve). This saves
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   416
    // us a lot of memory because at any one time we only need to store
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   417
    // limit+1 curves - one for each level of the tree + 1.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   418
    // NOTE: the way we do things here is not enough to traverse a general
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   419
    // tree; however, the trees we are interested in have the property that
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   420
    // every non leaf node has exactly 2 children
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   421
    static final class LengthIterator {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   422
        private enum Side {LEFT, RIGHT};
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   423
        // Holds the curves at various levels of the recursion. The root
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   424
        // (i.e. the original curve) is at recCurveStack[0] (but then it
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   425
        // gets subdivided, the left half is put at 1, so most of the time
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   426
        // only the right half of the original curve is at 0)
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   427
        private final double[][] recCurveStack; // dirty
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   428
        // sides[i] indicates whether the node at level i+1 in the path from
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   429
        // the root to the current leaf is a left or right child of its parent.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   430
        private final Side[] sides; // dirty
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   431
        private int curveType;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   432
        // lastT and nextT delimit the current leaf.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   433
        private double nextT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   434
        private double lenAtNextT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   435
        private double lastT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   436
        private double lenAtLastT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   437
        private double lenAtLastSplit;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   438
        private double lastSegLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   439
        // the current level in the recursion tree. 0 is the root. limit
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   440
        // is the deepest possible leaf.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   441
        private int recLevel;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   442
        private boolean done;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   443
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   444
        // the lengths of the lines of the control polygon. Only its first
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   445
        // curveType/2 - 1 elements are valid. This is an optimization. See
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   446
        // next() for more detail.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   447
        private final double[] curLeafCtrlPolyLengths = new double[3];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   448
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   449
        LengthIterator() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   450
            this.recCurveStack = new double[REC_LIMIT + 1][8];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   451
            this.sides = new Side[REC_LIMIT];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   452
            // if any methods are called without first initializing this object
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   453
            // on a curve, we want it to fail ASAP.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   454
            this.nextT = Double.MAX_VALUE;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   455
            this.lenAtNextT = Double.MAX_VALUE;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   456
            this.lenAtLastSplit = Double.MIN_VALUE;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   457
            this.recLevel = Integer.MIN_VALUE;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   458
            this.lastSegLen = Double.MAX_VALUE;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   459
            this.done = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   460
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   461
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   462
        /**
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   463
         * Reset this LengthIterator.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   464
         */
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   465
        void reset() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   466
            // keep data dirty
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   467
            // as it appears not useful to reset data:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   468
            if (DO_CLEAN_DIRTY) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   469
                final int recLimit = recCurveStack.length - 1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   470
                for (int i = recLimit; i >= 0; i--) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   471
                    Arrays.fill(recCurveStack[i], 0.0d);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   472
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   473
                Arrays.fill(sides, Side.LEFT);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   474
                Arrays.fill(curLeafCtrlPolyLengths, 0.0d);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   475
                Arrays.fill(nextRoots, 0.0d);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   476
                Arrays.fill(flatLeafCoefCache, 0.0d);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   477
                flatLeafCoefCache[2] = -1.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   478
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   479
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   480
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   481
        void initializeIterationOnCurve(double[] pts, int type) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   482
            // optimize arraycopy (8 values faster than 6 = type):
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   483
            System.arraycopy(pts, 0, recCurveStack[0], 0, 8);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   484
            this.curveType = type;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   485
            this.recLevel = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   486
            this.lastT = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   487
            this.lenAtLastT = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   488
            this.nextT = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   489
            this.lenAtNextT = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   490
            goLeft(); // initializes nextT and lenAtNextT properly
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   491
            this.lenAtLastSplit = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   492
            if (recLevel > 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   493
                this.sides[0] = Side.LEFT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   494
                this.done = false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   495
            } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   496
                // the root of the tree is a leaf so we're done.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   497
                this.sides[0] = Side.RIGHT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   498
                this.done = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   499
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   500
            this.lastSegLen = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   501
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   502
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   503
        // 0 == false, 1 == true, -1 == invalid cached value.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   504
        private int cachedHaveLowAcceleration = -1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   505
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   506
        private boolean haveLowAcceleration(double err) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   507
            if (cachedHaveLowAcceleration == -1) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   508
                final double len1 = curLeafCtrlPolyLengths[0];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   509
                final double len2 = curLeafCtrlPolyLengths[1];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   510
                // the test below is equivalent to !within(len1/len2, 1, err).
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   511
                // It is using a multiplication instead of a division, so it
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   512
                // should be a bit faster.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   513
                if (!DHelpers.within(len1, len2, err * len2)) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   514
                    cachedHaveLowAcceleration = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   515
                    return false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   516
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   517
                if (curveType == 8) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   518
                    final double len3 = curLeafCtrlPolyLengths[2];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   519
                    // if len1 is close to 2 and 2 is close to 3, that probably
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   520
                    // means 1 is close to 3 so the second part of this test might
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   521
                    // not be needed, but it doesn't hurt to include it.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   522
                    final double errLen3 = err * len3;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   523
                    if (!(DHelpers.within(len2, len3, errLen3) &&
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   524
                          DHelpers.within(len1, len3, errLen3))) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   525
                        cachedHaveLowAcceleration = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   526
                        return false;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   527
                    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   528
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   529
                cachedHaveLowAcceleration = 1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   530
                return true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   531
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   532
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   533
            return (cachedHaveLowAcceleration == 1);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   534
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   535
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   536
        // we want to avoid allocations/gc so we keep this array so we
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   537
        // can put roots in it,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   538
        private final double[] nextRoots = new double[4];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   539
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   540
        // caches the coefficients of the current leaf in its flattened
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   541
        // form (see inside next() for what that means). The cache is
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   542
        // invalid when it's third element is negative, since in any
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   543
        // valid flattened curve, this would be >= 0.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   544
        private final double[] flatLeafCoefCache = new double[]{0.0d, 0.0d, -1.0d, 0.0d};
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   545
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   546
        // returns the t value where the remaining curve should be split in
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   547
        // order for the left subdivided curve to have length len. If len
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   548
        // is >= than the length of the uniterated curve, it returns 1.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   549
        double next(final double len) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   550
            final double targetLength = lenAtLastSplit + len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   551
            while (lenAtNextT < targetLength) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   552
                if (done) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   553
                    lastSegLen = lenAtNextT - lenAtLastSplit;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   554
                    return 1.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   555
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   556
                goToNextLeaf();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   557
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   558
            lenAtLastSplit = targetLength;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   559
            final double leaflen = lenAtNextT - lenAtLastT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   560
            double t = (targetLength - lenAtLastT) / leaflen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   561
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   562
            // cubicRootsInAB is a fairly expensive call, so we just don't do it
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   563
            // if the acceleration in this section of the curve is small enough.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   564
            if (!haveLowAcceleration(0.05d)) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   565
                // We flatten the current leaf along the x axis, so that we're
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   566
                // left with a, b, c which define a 1D Bezier curve. We then
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   567
                // solve this to get the parameter of the original leaf that
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   568
                // gives us the desired length.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   569
                final double[] _flatLeafCoefCache = flatLeafCoefCache;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   570
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   571
                if (_flatLeafCoefCache[2] < 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   572
                    double x =     curLeafCtrlPolyLengths[0],
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   573
                          y = x + curLeafCtrlPolyLengths[1];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   574
                    if (curveType == 8) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   575
                        double z = y + curLeafCtrlPolyLengths[2];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   576
                        _flatLeafCoefCache[0] = 3.0d * (x - y) + z;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   577
                        _flatLeafCoefCache[1] = 3.0d * (y - 2.0d * x);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   578
                        _flatLeafCoefCache[2] = 3.0d * x;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   579
                        _flatLeafCoefCache[3] = -z;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   580
                    } else if (curveType == 6) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   581
                        _flatLeafCoefCache[0] = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   582
                        _flatLeafCoefCache[1] = y - 2.0d * x;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   583
                        _flatLeafCoefCache[2] = 2.0d * x;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   584
                        _flatLeafCoefCache[3] = -y;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   585
                    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   586
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   587
                double a = _flatLeafCoefCache[0];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   588
                double b = _flatLeafCoefCache[1];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   589
                double c = _flatLeafCoefCache[2];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   590
                double d = t * _flatLeafCoefCache[3];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   591
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   592
                // we use cubicRootsInAB here, because we want only roots in 0, 1,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   593
                // and our quadratic root finder doesn't filter, so it's just a
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   594
                // matter of convenience.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   595
                int n = DHelpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0d, 1.0d);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   596
                if (n == 1 && !Double.isNaN(nextRoots[0])) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   597
                    t = nextRoots[0];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   598
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   599
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   600
            // t is relative to the current leaf, so we must make it a valid parameter
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   601
            // of the original curve.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   602
            t = t * (nextT - lastT) + lastT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   603
            if (t >= 1.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   604
                t = 1.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   605
                done = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   606
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   607
            // even if done = true, if we're here, that means targetLength
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   608
            // is equal to, or very, very close to the total length of the
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   609
            // curve, so lastSegLen won't be too high. In cases where len
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   610
            // overshoots the curve, this method will exit in the while
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   611
            // loop, and lastSegLen will still be set to the right value.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   612
            lastSegLen = len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   613
            return t;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   614
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   615
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   616
        double lastSegLen() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   617
            return lastSegLen;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   618
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   619
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   620
        // go to the next leaf (in an inorder traversal) in the recursion tree
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   621
        // preconditions: must be on a leaf, and that leaf must not be the root.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   622
        private void goToNextLeaf() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   623
            // We must go to the first ancestor node that has an unvisited
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   624
            // right child.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   625
            int _recLevel = recLevel;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   626
            final Side[] _sides = sides;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   627
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   628
            _recLevel--;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   629
            while(_sides[_recLevel] == Side.RIGHT) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   630
                if (_recLevel == 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   631
                    recLevel = 0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   632
                    done = true;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   633
                    return;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   634
                }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   635
                _recLevel--;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   636
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   637
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   638
            _sides[_recLevel] = Side.RIGHT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   639
            // optimize arraycopy (8 values faster than 6 = type):
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   640
            System.arraycopy(recCurveStack[_recLevel], 0,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   641
                             recCurveStack[_recLevel+1], 0, 8);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   642
            _recLevel++;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   643
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   644
            recLevel = _recLevel;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   645
            goLeft();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   646
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   647
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   648
        // go to the leftmost node from the current node. Return its length.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   649
        private void goLeft() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   650
            double len = onLeaf();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   651
            if (len >= 0.0d) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   652
                lastT = nextT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   653
                lenAtLastT = lenAtNextT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   654
                nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   655
                lenAtNextT += len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   656
                // invalidate caches
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   657
                flatLeafCoefCache[2] = -1.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   658
                cachedHaveLowAcceleration = -1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   659
            } else {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   660
                DHelpers.subdivide(recCurveStack[recLevel], 0,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   661
                                  recCurveStack[recLevel+1], 0,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   662
                                  recCurveStack[recLevel], 0, curveType);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   663
                sides[recLevel] = Side.LEFT;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   664
                recLevel++;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   665
                goLeft();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   666
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   667
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   668
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   669
        // this is a bit of a hack. It returns -1 if we're not on a leaf, and
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   670
        // the length of the leaf if we are on a leaf.
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   671
        private double onLeaf() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   672
            double[] curve = recCurveStack[recLevel];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   673
            double polyLen = 0.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   674
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   675
            double x0 = curve[0], y0 = curve[1];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   676
            for (int i = 2; i < curveType; i += 2) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   677
                final double x1 = curve[i], y1 = curve[i+1];
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   678
                final double len = DHelpers.linelen(x0, y0, x1, y1);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   679
                polyLen += len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   680
                curLeafCtrlPolyLengths[i/2 - 1] = len;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   681
                x0 = x1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   682
                y0 = y1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   683
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   684
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   685
            final double lineLen = DHelpers.linelen(curve[0], curve[1],
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   686
                                                  curve[curveType-2],
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   687
                                                  curve[curveType-1]);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   688
            if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   689
                return (polyLen + lineLen) / 2.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   690
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   691
            return -1.0d;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   692
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   693
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   694
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   695
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   696
    public void curveTo(double x1, double y1,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   697
                        double x2, double y2,
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   698
                        double x3, double y3)
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   699
    {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   700
        final double[] _curCurvepts = curCurvepts;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   701
        _curCurvepts[0] = x0;        _curCurvepts[1] = y0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   702
        _curCurvepts[2] = x1;        _curCurvepts[3] = y1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   703
        _curCurvepts[4] = x2;        _curCurvepts[5] = y2;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   704
        _curCurvepts[6] = x3;        _curCurvepts[7] = y3;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   705
        somethingTo(8);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   706
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   707
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   708
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   709
    public void quadTo(double x1, double y1, double x2, double y2) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   710
        final double[] _curCurvepts = curCurvepts;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   711
        _curCurvepts[0] = x0;        _curCurvepts[1] = y0;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   712
        _curCurvepts[2] = x1;        _curCurvepts[3] = y1;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   713
        _curCurvepts[4] = x2;        _curCurvepts[5] = y2;
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   714
        somethingTo(6);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   715
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   716
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   717
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   718
    public void closePath() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   719
        lineTo(sx, sy);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   720
        if (firstSegidx > 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   721
            if (!dashOn || needsMoveTo) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   722
                out.moveTo(sx, sy);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   723
            }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   724
            emitFirstSegments();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   725
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   726
        moveTo(sx, sy);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   727
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   728
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   729
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   730
    public void pathDone() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   731
        if (firstSegidx > 0) {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   732
            out.moveTo(sx, sy);
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   733
            emitFirstSegments();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   734
        }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   735
        out.pathDone();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   736
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   737
        // Dispose this instance:
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   738
        dispose();
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   739
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   740
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   741
    @Override
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   742
    public long getNativeConsumer() {
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   743
        throw new InternalError("DDasher does not use a native consumer");
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   744
    }
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   745
}
188ef162f019 8180055: Upgrade the Marlin renderer in Java2D
lbourges
parents:
diff changeset
   746