Debian Codegraph @

src/com/github/weisj/jsvg/animation/value/AnimatedPaint.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.value;
23
24 import java.awt.*;
25 import java.awt.geom.Rectangle2D;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.animation.Track;
31 import com.github.weisj.jsvg.paint.SVGPaint;
32 import com.github.weisj.jsvg.paint.impl.PredefinedPaints;
33 import com.github.weisj.jsvg.renderer.MeasureContext;
34 import com.github.weisj.jsvg.renderer.RenderContext;
35 import com.github.weisj.jsvg.renderer.output.Output;
36
37 public class AnimatedPaint implements SVGPaint {
38
39 private final @NotNull Track track;
40 private final @NotNull SVGPaint initial;
41 private final @NotNull SVGPaint @NotNull [] values;
42
43 private SVGPaint current;
44 private long currentTimestamp = -1;
45
46 public AnimatedPaint(@NotNull Track track, @NotNull SVGPaint initial, @NotNull SVGPaint @NotNull [] values) {
47 this.track = track;
48 this.initial = initial;
49 this.values = values;
50 }
51
52 public @NotNull AnimatedPaint derive(@NotNull SVGPaint initialValue) {
53 if (this.initial != PredefinedPaints.INHERITED) return this;
54 return new AnimatedPaint(track, initialValue, values);
55 }
56
57 private @NotNull SVGPaint current(@NotNull MeasureContext context) {
58 long timestamp = context.timestamp();
59 if (currentTimestamp == timestamp) return current;
60 currentTimestamp = timestamp;
61 current = computeCurrent(timestamp);
62 return current;
63 }
64
65 private @NotNull SVGPaint computeCurrent(long timestamp) {
66 Track.InterpolationProgress progress = track.interpolationProgress(timestamp, values.length);
67 if (progress.isInitial()) return initial;
68 int i = progress.iterationIndex();
69
70 assert i >= 0;
71 assert values.length > 0;
72 if (i >= values.length - 1) {
73 return values[values.length - 1];
74 }
75
76 SVGPaint start = values[i];
77 SVGPaint end = values[i + 1];
78
79 return track.paintInterpolator().interpolate(initial, start, end, progress.indexProgress());
80 }
81
82 @Override
83 public void fillShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape,
84 @Nullable Rectangle2D bounds) {
85 current(context.measureContext()).fillShape(output, context, shape, bounds);
86 }
87
88 @Override
89 public void drawShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape,
90 @Nullable Rectangle2D bounds) {
91 current(context.measureContext()).drawShape(output, context, shape, bounds);
92 }
93
94 @Override
95 public boolean isVisible(@NotNull RenderContext context) {
96 return current(context.measureContext()).isVisible(context);
97 }
98 }