| •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.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 |
|
| 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 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 97 |
|
| 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 |
|
| 104 |
|
| 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 |
|
| 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 |
} |