Debian Codegraph @

src/com/github/weisj/jsvg/util/AttributeUtil.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2026 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.util;
23
24 import java.util.Objects;
25 import java.util.function.Function;
26
27 import org.jetbrains.annotations.Contract;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30
31 import com.github.weisj.jsvg.attributes.Animatable;
32 import com.github.weisj.jsvg.attributes.Inherited;
33 import com.github.weisj.jsvg.attributes.value.LengthValue;
34 import com.github.weisj.jsvg.attributes.value.PercentageDimension;
35 import com.github.weisj.jsvg.geometry.size.Length;
36 import com.github.weisj.jsvg.parser.impl.AttributeNode;
37
38 public final class AttributeUtil {
39 private AttributeUtil() {}
40
41 public static boolean isBlank(@NotNull String s) {
42 for (int i = 0; i < s.length(); i++) {
43 if (!Character.isWhitespace(s.charAt(i))) return false;
44 }
45 return true;
46 }
47
48 @SafeVarargs
49 public static <T> @NotNull T @Nullable [] toNonnullArray(@Nullable T... values) {
50 if (values == null) return null;
51 for (T value : values) {
52 if (value == null) return null;
53 }
54 // noinspection NullableProblems
55 return values;
56 }
57
58 public static <T> @NotNull T notNullOrElse(@Nullable T value, @NotNull T defaultValue) {
59 return value != null ? value : defaultValue;
60 }
61
62 public static @NotNull AxisPair parseAxisPair(
63 @NotNull AttributeNode node,
64 @NotNull String xAttr, @NotNull String yAttr,
65 @NotNull Length fallback, Inherited inherited,
66 @NotNull Function<@NotNull LengthValue, @Nullable LengthValue> validator) {
67 LengthValue initialRx =
68 node.getLength(xAttr, PercentageDimension.WIDTH, Length.UNSPECIFIED,
69 inherited, Animatable.NO);
70 initialRx = validator.apply(initialRx);
71 if (initialRx == null) initialRx = Length.UNSPECIFIED;
72
73 LengthValue initialRy =
74 node.getLength(yAttr, PercentageDimension.HEIGHT, initialRx,
75 inherited, Animatable.NO);
76 initialRy = validator.apply(initialRy);
77 if (initialRy == null) initialRy = Length.UNSPECIFIED;
78
79 LengthValue rx;
80 LengthValue ry;
81
82 if (initialRx == Length.UNSPECIFIED && initialRy == Length.UNSPECIFIED) {
83 // Neither rx nor ry is specified.
84 // First try if either is specified through animation.
85 // Then fallback to the other if not.
86 rx = notNullOrElse(
87 node.getAnimatedLength(xAttr, fallback, PercentageDimension.WIDTH), initialRx);
88 ry = notNullOrElse(
89 node.getAnimatedLength(yAttr, fallback, PercentageDimension.HEIGHT), initialRy);
90 if (rx == Length.UNSPECIFIED) {
91 rx = ry;
92 } else if (ry == Length.UNSPECIFIED) {
93 ry = rx;
94 }
95 } else if (initialRx == Length.UNSPECIFIED) {
96 // rx is unspecified, but ry is specified.
97 // Use ry as value for rx.
98 ry = notNullOrElse(
99 node.getAnimatedLength(yAttr, initialRy, PercentageDimension.HEIGHT), initialRy);
100 rx = notNullOrElse(
101 node.getAnimatedLength(xAttr, ry, PercentageDimension.WIDTH), ry);
102 } else if (initialRy == Length.UNSPECIFIED) {
103 // ry is unspecified, but rx is specified.
104 // Use rx as value for ry.
105 rx = notNullOrElse(
106 node.getAnimatedLength(xAttr, initialRx, PercentageDimension.WIDTH), initialRx);
107 ry = notNullOrElse(
108 node.getAnimatedLength(yAttr, rx, PercentageDimension.HEIGHT), rx);
109 } else {
110 // Both rx and ry are specified.
111 rx = notNullOrElse(
112 node.getAnimatedLength(xAttr, initialRx, PercentageDimension.WIDTH), initialRx);
113 ry = notNullOrElse(
114 node.getAnimatedLength(yAttr, initialRy, PercentageDimension.HEIGHT), initialRy);
115 }
116
117 if (rx == Length.UNSPECIFIED) rx = fallback;
118 if (ry == Length.UNSPECIFIED) ry = fallback;
119
120 return new AxisPair(rx, ry);
121 }
122
123 @Contract(pure = true)
124 public static <T> boolean arrayContains(T @NotNull [] arr, T element) {
125 for (T t : arr) {
126 if (Objects.equals(t, element)) return true;
127 }
128 return false;
129 }
130
131 public static final class AxisPair {
132 private final @NotNull LengthValue xAxis;
133 private final @NotNull LengthValue yAxis;
134
135 public AxisPair(@NotNull LengthValue xAxis, @NotNull LengthValue yAxis) {
136 this.xAxis = xAxis;
137 this.yAxis = yAxis;
138 }
139
140 public @NotNull LengthValue xAxis() {
141 return xAxis;
142 }
143
144 public @NotNull LengthValue yAxis() {
145 return yAxis;
146 }
147 }
148 }