< prev index next >

src/java.desktop/share/classes/sun/java2d/pisces/Dasher.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.pisces;
  27 
  28 import sun.awt.geom.PathConsumer2D;
  29 
  30 /**
  31  * The <code>Dasher</code> class takes a series of linear commands
  32  * (<code>moveTo</code>, <code>lineTo</code>, <code>close</code> and
  33  * <code>end</code>) and breaks them into smaller segments according to a
  34  * dash pattern array and a starting dash phase.
  35  *
  36  * <p> Issues: in J2Se, a zero length dash segment as drawn as a very
  37  * short dash, whereas Pisces does not draw anything.  The PostScript
  38  * semantics are unclear.
  39  *
  40  */
  41 final class Dasher implements sun.awt.geom.PathConsumer2D {
  42 
  43     private final PathConsumer2D out;
  44     private final float[] dash;
  45     private final float startPhase;
  46     private final boolean startDashOn;
  47     private final int startIdx;
  48 
  49     private boolean starting;
  50     private boolean needsMoveTo;
  51 
  52     private int idx;
  53     private boolean dashOn;
  54     private float phase;
  55 
  56     private float sx, sy;
  57     private float x0, y0;
  58 
  59     // temporary storage for the current curve
  60     private float[] curCurvepts;
  61 
  62     /**
  63      * Constructs a <code>Dasher</code>.
  64      *
  65      * @param out an output <code>PathConsumer2D</code>.
  66      * @param dash an array of <code>float</code>s containing the dash pattern
  67      * @param phase a <code>float</code> containing the dash phase
  68      */
  69     public Dasher(PathConsumer2D out, float[] dash, float phase) {
  70         if (phase < 0) {
  71             throw new IllegalArgumentException("phase < 0 !");
  72         }
  73 
  74         this.out = out;
  75 
  76         // Normalize so 0 <= phase < dash[0]
  77         int idx = 0;
  78         dashOn = true;
  79         float d;
  80         while (phase >= (d = dash[idx])) {
  81             phase -= d;
  82             idx = (idx + 1) % dash.length;
  83             dashOn = !dashOn;
  84         }
  85 
  86         this.dash = dash;
  87         this.startPhase = this.phase = phase;




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.pisces;
  27 
  28 import sun.awt.geom.PathConsumer2D;
  29 
  30 /**
  31  * The {@code Dasher} class takes a series of linear commands
  32  * ({@code moveTo}, {@code lineTo}, {@code close} and
  33  * {@code end}) and breaks them into smaller segments according to a
  34  * dash pattern array and a starting dash phase.
  35  *
  36  * <p> Issues: in J2Se, a zero length dash segment as drawn as a very
  37  * short dash, whereas Pisces does not draw anything.  The PostScript
  38  * semantics are unclear.
  39  *
  40  */
  41 final class Dasher implements sun.awt.geom.PathConsumer2D {
  42 
  43     private final PathConsumer2D out;
  44     private final float[] dash;
  45     private final float startPhase;
  46     private final boolean startDashOn;
  47     private final int startIdx;
  48 
  49     private boolean starting;
  50     private boolean needsMoveTo;
  51 
  52     private int idx;
  53     private boolean dashOn;
  54     private float phase;
  55 
  56     private float sx, sy;
  57     private float x0, y0;
  58 
  59     // temporary storage for the current curve
  60     private float[] curCurvepts;
  61 
  62     /**
  63      * Constructs a {@code Dasher}.
  64      *
  65      * @param out an output {@code PathConsumer2D}.
  66      * @param dash an array of {@code float}s containing the dash pattern
  67      * @param phase a {@code float} containing the dash phase
  68      */
  69     public Dasher(PathConsumer2D out, float[] dash, float phase) {
  70         if (phase < 0) {
  71             throw new IllegalArgumentException("phase < 0 !");
  72         }
  73 
  74         this.out = out;
  75 
  76         // Normalize so 0 <= phase < dash[0]
  77         int idx = 0;
  78         dashOn = true;
  79         float d;
  80         while (phase >= (d = dash[idx])) {
  81             phase -= d;
  82             idx = (idx + 1) % dash.length;
  83             dashOn = !dashOn;
  84         }
  85 
  86         this.dash = dash;
  87         this.startPhase = this.phase = phase;


< prev index next >