Debian Codegraph @

src/com/github/weisj/jsvg/attributes/MarkerOrientation.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 }