Debian Codegraph @

src/com/github/weisj/jsvg/renderer/MeasureContext.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.renderer;
23
24 import java.util.Objects;
25
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import com.github.weisj.jsvg.geometry.size.Length;
30 import com.github.weisj.jsvg.renderer.animation.AnimationState;
31 import com.github.weisj.jsvg.view.FloatSize;
32 import com.github.weisj.jsvg.view.ViewBox;
33 import com.google.errorprone.annotations.Immutable;
34
35 @Immutable
36 public final class MeasureContext {
37 private final float vw;
38 private final float vh;
39 private final float em;
40 private final float rem;
41 private final float ex;
42 private final @NotNull AnimationState animationState;
43
44 public MeasureContext(float vw, float vh, float em, float ex, float rem, @NotNull AnimationState animationState) {
45 this.vw = vw;
46 this.vh = vh;
47 this.em = em;
48 this.rem = rem;
49 this.ex = ex;
50 this.animationState = animationState;
51 }
52
53 public static @NotNull MeasureContext createInitial(@NotNull FloatSize viewBoxSize, float em, float ex,
54 @NotNull AnimationState animationState) {
55 return new MeasureContext(viewBoxSize.width, viewBoxSize.height, em, ex, em, animationState);
56 }
57
58 public @NotNull MeasureContext deriveRoot(float rem) {
59 return new MeasureContext(vw, vh, em, ex, rem, animationState);
60 }
61
62 public @NotNull MeasureContext derive(float viewWidth, float viewHeight) {
63 return new MeasureContext(viewWidth, viewHeight, em, ex, rem, animationState);
64 }
65
66 public @NotNull MeasureContext derive(@Nullable ViewBox viewBox, float em, float ex) {
67 if (viewBox == null && Length.isUnspecified(em) && Length.isUnspecified(ex)) return this;
68 float newVw = vw;
69 float newVh = vh;
70 if (viewBox != null) {
71 // If any width or height are unspecified keep the width/height of the parent viewPort.
72 if (viewBox.hasSpecifiedWidth()) newVw = viewBox.width;
73 if (viewBox.hasSpecifiedHeight()) newVh = viewBox.height;
74 }
75 float effectiveEm = Length.isUnspecified(em) ? this.em : em;
76 float effectiveEx = Length.isUnspecified(ex) ? this.ex : ex;
77 return new MeasureContext(newVw, newVh, effectiveEm, effectiveEx, rem, animationState);
78 }
79
80 public float viewWidth() {
81 return vw;
82 }
83
84 public float viewHeight() {
85 return vh;
86 }
87
88 public float normedDiagonalLength() {
89 return (float) Math.sqrt((vw * vw + vh * vh) / 2);
90 }
91
92 public float em() {
93 return em;
94 }
95
96 public float rem() {
97 return rem;
98 }
99
100 public float ex() {
101 return ex;
102 }
103
104 public long timestamp() {
105 return animationState.timestamp();
106 }
107
108 @Override
109 public String toString() {
110 return "MeasureContext{" +
111 "vw=" + vw +
112 ", vh=" + vh +
113 ", em=" + em +
114 ", rem=" + rem +
115 ", ex=" + ex +
116 ", animationState=" + animationState +
117 '}';
118 }
119
120 @Override
121 public boolean equals(Object o) {
122 if (this == o) return true;
123 if (!(o instanceof MeasureContext)) return false;
124 MeasureContext that = (MeasureContext) o;
125 return Float.compare(that.vw, vw) == 0
126 && Float.compare(that.vh, vh) == 0
127 && Float.compare(that.em, em) == 0
128 && Float.compare(that.rem, rem) == 0
129 && Float.compare(that.ex, ex) == 0
130 && animationState.equals(that.animationState);
131 }
132
133 @Override
134 public int hashCode() {
135 return Objects.hash(vw, vh, em, ex, rem, animationState);
136 }
137
138 }