| •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.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 |
|
| 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 |
} |