Debian Codegraph @

src/com/github/weisj/jsvg/animation/Track.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-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.animation;
23
24 import java.util.*;
25 import java.util.stream.Collectors;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.animation.interpolation.*;
31 import com.github.weisj.jsvg.animation.time.Duration;
32 import com.github.weisj.jsvg.animation.time.Interval;
33 import com.github.weisj.jsvg.parser.impl.AttributeNode;
34 import com.github.weisj.jsvg.parser.impl.SeparatorMode;
35
36 public final class Track {
37 private final @NotNull List<@NotNull Interval> intervals;
38 private final float repeatCount;
39 private final Fill fill;
40 private final DefaultInterpolator interpolator;
41
42 private Track(@NotNull List<@NotNull Interval> intervals, float repeatCount, Fill fill,
43 AnimationValuesType valuesType, Additive additive) {
44 this.intervals = intervals;
45 this.repeatCount = repeatCount;
46 this.fill = fill;
47 this.interpolator = new DefaultInterpolator(valuesType, additive);
48 }
49
50 public static @Nullable Track parse(@NotNull AttributeNode attributeNode,
51 @NotNull AnimationValuesType valuesType, @NotNull Additive additive) {
52 List<Duration> begins = parseBegin(attributeNode);
53 Duration duration = attributeNode.getDuration("dur", Duration.INDEFINITE);
54
55 List<Interval> intervals = begins.stream()
56 .map(b -> new Interval(b, b.plus(duration)))
57 .filter(Interval::isValid)
58 .sorted(Comparator.comparing(Interval::begin))
59 .collect(Collectors.toList());
60
61 String repeatCountStr = attributeNode.getValue("repeatCount");
62 float repeatCount;
63 if ("indefinite".equals(repeatCountStr)) {
64 repeatCount = Float.POSITIVE_INFINITY;
65 } else {
66 repeatCount = attributeNode.parser().parseFloat(repeatCountStr, 1);
67 }
68 if (intervals.isEmpty() || repeatCount <= 0) {
69 return null;
70 }
71
72 return new Track(intervals, repeatCount, attributeNode.getEnum("fill", Fill.REMOVE),
73 valuesType, additive);
74 }
75
76 @NotNull
77 private static List<Duration> parseBegin(@NotNull AttributeNode attributeNode) {
78 String[] beginsRaw = attributeNode.getStringList("begin", SeparatorMode.SEMICOLON_ONLY);
79 List<Duration> begins;
80
81 if (beginsRaw.length > 0) {
82 begins = new ArrayList<>(beginsRaw.length);
83 for (String s : beginsRaw) {
84 Duration b = attributeNode.parser().parseDuration(s, null);
85 if (b != null) begins.add(b);
86 }
87 } else {
88 begins = Collections.singletonList(Duration.ZERO);
89 }
90 return begins;
91 }
92
93 public @NotNull List<@NotNull Interval> intervals() {
94 return intervals;
95 }
96
97 public float repeatCount() {
98 return repeatCount;
99 }
100
101 public @NotNull Fill fill() {
102 return fill;
103 }
104
105 private int iterationCount(@NotNull Duration duration, long timestampMillis) {
106 long durationMillis = duration.milliseconds();
107 return (int) (timestampMillis / durationMillis);
108 }
109
110 private float iterationProgress(@NotNull Duration duration, long timestampMillis) {
111 long durationMillis = duration.milliseconds();
112 return (float) (timestampMillis % durationMillis) / durationMillis;
113 }
114
115 private @Nullable Interval currentInterval(long timestamp) {
116 ListIterator<@NotNull Interval> iterator = intervals.listIterator(intervals.size());
117 while (iterator.hasPrevious()) {
118 Interval interval = iterator.previous();
119 if (interval.end().milliseconds() <= timestamp) {
120 return interval;
121 }
122 }
123 return null;
124 }
125
126 public @NotNull InterpolationProgress interpolationProgress(long timestamp, int valueCount) {
127 if (valueCount == 0) return InterpolationProgress.INITIAL;
128 Interval currentInterval = currentInterval(timestamp);
129 if (currentInterval == null) return InterpolationProgress.INITIAL;
130
131 long time = timestamp - currentInterval.begin().milliseconds();
132 Duration duration = currentInterval.duration();
133 int iterationCount = iterationCount(duration, time);
134 float iterationProgress = iterationProgress(duration, time);
135 float totalIteration = iterationCount + iterationProgress;
136
137 if (totalIteration > repeatCount) {
138 if (fill == Fill.FREEZE) {
139 return new InterpolationProgress(valueCount - 1, 0);
140 } else {
141 return InterpolationProgress.INITIAL;
142 }
143 }
144
145 int i = (int) Math.floor(iterationProgress * (valueCount - 1));
146 float t = (valueCount - 1) * iterationProgress - i;
147 return new InterpolationProgress(i, t);
148 }
149
150 public @NotNull FloatInterpolator floatInterpolator() {
151 return interpolator;
152 }
153
154 public @NotNull FloatListInterpolator floatListInterpolator() {
155 return interpolator;
156 }
157
158 public @NotNull PaintInterpolator paintInterpolator() {
159 return interpolator;
160 }
161
162 public @NotNull TransformInterpolator transformInterpolator() {
163 return interpolator;
164 }
165
166 public static final class InterpolationProgress {
167 public static final InterpolationProgress INITIAL = new InterpolationProgress(-1, 0);
168
169 private final int iterationIndex;
170 private final float indexProgress;
171
172 public InterpolationProgress(int iterationIndex, float indexProgress) {
173 this.iterationIndex = iterationIndex;
174 this.indexProgress = indexProgress;
175 }
176
177 public boolean isInitial() {
178 return iterationIndex == -1;
179 }
180
181 public int iterationIndex() {
182 return iterationIndex;
183 }
184
185 public float indexProgress() {
186 return indexProgress;
187 }
188
189 @Override
190 public boolean equals(Object o) {
191 if (this == o) return true;
192 if (o == null || getClass() != o.getClass()) return false;
193 InterpolationProgress that = (InterpolationProgress) o;
194 return iterationIndex == that.iterationIndex && Float.compare(indexProgress, that.indexProgress) == 0;
195 }
196
197 @Override
198 public int hashCode() {
199 return Objects.hash(iterationIndex, indexProgress);
200 }
201 }
202 }