2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte Denizet
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
12 package org.scilab.forge.scirenderer.implementation.g2d.motor;
14 import java.awt.BasicStroke;
15 import java.awt.Stroke;
16 import java.util.HashMap;
19 import org.scilab.forge.scirenderer.shapes.appearance.Appearance;
22 * @author Calixte DENIZET
24 public class G2DStroke extends BasicStroke {
26 private static final int[] array = new int[16];
27 private static final Stroke BASIC = new G2DStroke(1, null, 0);
29 public G2DStroke(float lineWidth, float[] dash, float phase) {
30 super(lineWidth, CAP_BUTT, JOIN_MITER, 10.0f, dash, phase);
33 public static Stroke getStroke(Appearance appearance, double dashPhase) {
34 Appearance usedAppearance;
35 if (appearance == null) {
36 usedAppearance = new Appearance();
38 usedAppearance = appearance;
41 float factor = usedAppearance.getLineWidth();
43 return new G2DStroke(0, null, 0);
46 short pattern = usedAppearance.getLinePattern();
52 return new G2DStroke(factor, null, 0);
55 return new G2DStroke(factor, decodePattern(factor, pattern), (float) dashPhase);
58 private static final float[] decodePattern(final float factor, short pattern) {
59 // If the pattern is 1111101011111010, from right to left it becomes
60 // 0101111101011111, the first 0 is put on the right 1011111010111110 and it is converted into
61 // 1, 1, 5, 1, 1, 1, 5, 1
62 int n = 0xFFFF & pattern;
64 int t = Integer.numberOfTrailingZeros(n);
66 int k = Integer.numberOfLeadingZeros(n) - 16;
68 t = Integer.numberOfTrailingZeros(n);
70 t = Integer.numberOfTrailingZeros(0xFFFF - n);
78 float[] ret = new float[i + 1];
80 for (int j = 0; j <= i; j++) {
81 ret[j] = ((float) array[j]) * factor;