| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.attributes; |
| 23 |
|
| 24 |
import org.jetbrains.annotations.NotNull; |
| 25 |
import org.jetbrains.annotations.Nullable; |
| 26 |
|
| 27 |
import com.github.weisj.jsvg.geometry.size.Angle; |
| 28 |
import com.github.weisj.jsvg.parser.impl.AttributeParser; |
| 29 |
|
| 30 |
public abstract class MarkerOrientation { |
| 31 |
|
| 32 |
private MarkerOrientation() {} |
| 33 |
|
| 34 |
public enum MarkerType { |
| 35 |
START, |
| 36 |
MID, |
| 37 |
END |
| 38 |
} |
| 39 |
|
| 40 |
public static @NotNull MarkerOrientation parse(@Nullable String value, @NotNull AttributeParser parser) { |
| 41 |
if (value == null) return AngleOrientation.DEFAULT; |
| 42 |
if ("auto".equals(value)) return AutoOrientation.INSTANCE; |
| 43 |
if ("auto-start-reverse".equals(value)) return AutoStartReverseOrientation.INSTANCE; |
| 44 |
Angle angle = parser.parseAngle(value, Angle.UNSPECIFIED); |
| 45 |
if (angle.isSpecified()) return new AngleOrientation(angle); |
| 46 |
return AngleOrientation.DEFAULT; |
| 47 |
} |
| 48 |
|
| 49 |
public abstract float orientationFor(@NotNull MarkerType type, float dxIn, float dyIn, float dxOut, |
| 50 |
float dyOut); |
| 51 |
|
| 52 |
private static final class AutoOrientation extends MarkerOrientation { |
| 53 |
private static final @NotNull AutoOrientation INSTANCE = new AutoOrientation(); |
| 54 |
|
| 55 |
@Override |
| 56 |
public float orientationFor(@NotNull MarkerType type, float dxIn, float dyIn, float dxOut, |
| 57 |
float dyOut) { |
| 58 |
switch (type) { |
| 59 |
case START: |
| 60 |
return (float) Math.atan2(dyOut, dxOut); |
| 61 |
case END: |
| 62 |
return (float) Math.atan2(dyIn, dxIn); |
| 63 |
case MID: |
| 64 |
return (float) Math.atan2((dyIn + dyOut) / 2f, (dxIn + dxOut) / 2f); |
| 65 |
default: |
| 66 |
throw new IllegalStateException(); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private static final class AutoStartReverseOrientation extends MarkerOrientation { |
| 72 |
private static final @NotNull AutoStartReverseOrientation INSTANCE = new AutoStartReverseOrientation(); |
| 73 |
|
| 74 |
@Override |
| 75 |
public float orientationFor(@NotNull MarkerType type, float dxIn, float dyIn, float dxOut, |
| 76 |
float dyOut) { |
| 77 |
switch (type) { |
| 78 |
case START: |
| 79 |
return (float) Math.atan2(-dyOut, -dxOut); |
| 80 |
case END: |
| 81 |
return (float) Math.atan2(dyIn, dxIn); |
| 82 |
case MID: |
| 83 |
return (float) Math.atan2((dyIn + dyOut) / 2f, (dxIn + dxOut) / 2f); |
| 84 |
default: |
| 85 |
throw new IllegalStateException(); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private static final class AngleOrientation extends MarkerOrientation { |
| 91 |
private static final @NotNull AngleOrientation DEFAULT = new AngleOrientation(Angle.ZERO); |
| 92 |
private final @NotNull Angle angle; |
| 93 |
|
| 94 |
private AngleOrientation(@NotNull Angle angle) { |
| 95 |
this.angle = angle; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public float orientationFor(@NotNull MarkerType type, float dxIn, float dyIn, float dxOut, |
| 100 |
float dyOut) { |
| 101 |
return angle.radians(); |
| 102 |
} |
| 103 |
} |
| 104 |
} |