Debian Codegraph @

src/com/github/weisj/jsvg/geometry/size/Length.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.geometry.size;
23
24 import java.util.Objects;
25
26 import org.jetbrains.annotations.NotNull;
27
28 import com.github.weisj.jsvg.attributes.UnitType;
29 import com.github.weisj.jsvg.attributes.value.LengthValue;
30 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
31 import com.github.weisj.jsvg.renderer.MeasureContext;
32
33 public final class Length implements LengthValue {
34 public static final float UNSPECIFIED_RAW = Float.NaN;
35 public static final @NotNull Length UNSPECIFIED = new Length(Unit.RAW, UNSPECIFIED_RAW);
36 public static final @NotNull Length ZERO = new Length(Unit.RAW, 0);
37 public static final @NotNull Length ONE = new Length(Unit.RAW, 1);
38 public static final @NotNull Length INHERITED = new Length(Unit.RAW, 0);
39
40 private final @NotNull Unit unit;
41 private final float value;
42
43 private static final float PIXELS_PER_INCH = 96f;
44 private static final float INCHES_PER_CM = .3936f;
45
46
47 public Length(@NotNull Unit unit, float value) {
48 this.unit = unit;
49 this.value = value;
50 }
51
52 public Length(@NotNull Length ry) {
53 this.unit = ry.unit;
54 this.value = ry.value;
55 }
56
57 public static boolean isUnspecified(float value) {
58 return Float.isNaN(value);
59 }
60
61 public static boolean isSpecified(float value) {
62 return !isUnspecified(value);
63 }
64
65 private static float resolveNonPercentage(@NotNull MeasureContext context, Unit unit, float value) {
66 if (unit == Unit.RAW) {
67 // If we are unspecified this will return UNSPECIFIED_RAW as intended.
68 return value;
69 }
70 assert !unit.isPercentage();
71 switch (unit) {
72 case PX:
73 return value;
74 case IN:
75 return PIXELS_PER_INCH * value;
76 case CM:
77 return INCHES_PER_CM * PIXELS_PER_INCH * value;
78 case MM:
79 return 0.1f * INCHES_PER_CM * PIXELS_PER_INCH * value;
80 case Q:
81 return 0.025f * INCHES_PER_CM * PIXELS_PER_INCH * value;
82 case PT:
83 return (1f / 72f) * PIXELS_PER_INCH * value;
84 case PC:
85 return (1f / 6f) * PIXELS_PER_INCH * value;
86 case EM:
87 return context.em() * value;
88 case REM:
89 return context.rem() * value;
90 case CH:
91 case EX:
92 return context.ex() * value;
93 default:
94 throw new UnsupportedOperationException("Not implemented: Can't convert " + unit + " to pixel");
95 }
96 }
97
98 @Override
99 public float resolve(@NotNull MeasureContext context) {
100 float raw = raw();
101 switch (unit) {
102 case PERCENTAGE_LENGTH:
103 return (raw * context.normedDiagonalLength()) / 100f;
104 case VW:
105 case VI:
106 case PERCENTAGE_WIDTH:
107 return (raw * context.viewWidth()) / 100f;
108 case VH:
109 case VB:
110 case PERCENTAGE_HEIGHT:
111 return (raw * context.viewHeight()) / 100f;
112 case PERCENTAGE:
113 return raw / 100f;
114 case V_MIN:
115 return (raw * Math.min(context.viewWidth(), context.viewHeight())) / 100f;
116 case V_MAX:
117 return (raw * Math.max(context.viewWidth(), context.viewHeight())) / 100f;
118 default:
119 return resolveNonPercentage(context, unit, raw);
120 }
121 }
122
123 /**
124 * Used for resolving font sizes. Relative values will be resolves with respect to the current font size.
125 * This isn't dependent on the current viewBox.
126 * @param context the measuring context.
127 * @return the resolved size.
128 */
129 public float resolveFontSize(@NotNull MeasureContext context) {
130 float raw = raw();
131 switch (unit) {
132 case PERCENTAGE_LENGTH:
133 case PERCENTAGE_WIDTH:
134 case PERCENTAGE_HEIGHT:
135 throw new IllegalStateException("Can't resolve font size with geometric percentage unit");
136 case PERCENTAGE:
137 return (raw / 100f) * context.em();
138 default:
139 return resolveNonPercentage(context, unit, raw);
140 }
141 }
142
143 @Override
144 public @NotNull String toString() {
145 if (isUnspecified()) return "<unspecified>";
146 return value + unit.suffix();
147 }
148
149 public boolean isZero() {
150 return value == 0;
151 }
152
153 @Override
154 public boolean isConstantlyZero() {
155 return isZero();
156 }
157
158 @Override
159 public boolean isConstantlyNonNegative() {
160 return raw() >= 0;
161 }
162
163 public boolean isAbsolute() {
164 switch (unit) {
165 case RAW:
166 case PX:
167 case IN:
168 case CM:
169 case MM:
170 case Q:
171 case PT:
172 case PC:
173 return true;
174 default:
175 return false;
176 }
177 }
178
179 public float raw() {
180 return value;
181 }
182
183 public @NotNull Unit unit() {
184 return unit;
185 }
186
187 public boolean isUnspecified() {
188 return isUnspecified(value);
189 }
190
191 public boolean isSpecified() {
192 return isSpecified(value);
193 }
194
195 public @NotNull Length coerceNonNegative() {
196 if (isSpecified() && value <= 0) return ZERO;
197 return this;
198 }
199
200 public @NotNull Length coercePercentageToCorrectUnit(@NotNull UnitType unitType,
201 @NotNull PercentageDimension dimension) {
202 if (unitType == UnitType.UserSpaceOnUse) return this;
203 if (unit.isPercentage()) return this;
204 Unit u;
205 switch (dimension) {
206 case WIDTH:
207 u = Unit.PERCENTAGE_WIDTH;
208 break;
209 case HEIGHT:
210 u = Unit.PERCENTAGE_HEIGHT;
211 break;
212 case LENGTH:
213 u = Unit.PERCENTAGE_LENGTH;
214 break;
215 case CUSTOM:
216 u = Unit.PERCENTAGE;
217 break;
218 default:
219 throw new IllegalStateException("Can't coerce to percentage with no dimension");
220 }
221 return new Length(u, raw() * 100);
222 }
223
224 public @NotNull Length orElseIfUnspecified(float value) {
225 if (isUnspecified()) return Unit.RAW.valueOf(value);
226 return this;
227 }
228
229 public @NotNull Length multiply(float scalingFactor) {
230 if (scalingFactor == 0) return ZERO;
231 if (scalingFactor == 1) return this;
232 return new Length(unit, scalingFactor * value);
233 }
234
235 @Override
236 public boolean equals(Object o) {
237 if (this == o) return true;
238 if (!(o instanceof Length)) return false;
239 Length length = (Length) o;
240 return unit == length.unit && Float.compare(length.value, value) == 0;
241 }
242
243 @Override
244 public int hashCode() {
245 return Objects.hash(unit, value);
246 }
247
248 }