| •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.parser.impl; |
| 23 |
|
| 24 |
import static com.github.weisj.jsvg.util.AttributeUtil.toNonnullArray; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Locale; |
| 29 |
import java.util.function.UnaryOperator; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.Contract; |
| 32 |
import org.jetbrains.annotations.NotNull; |
| 33 |
import org.jetbrains.annotations.Nullable; |
| 34 |
|
| 35 |
import com.github.weisj.jsvg.animation.time.Duration; |
| 36 |
import com.github.weisj.jsvg.animation.time.TimeUnit; |
| 37 |
import com.github.weisj.jsvg.attributes.HasMatchName; |
| 38 |
import com.github.weisj.jsvg.attributes.SuffixUnit; |
| 39 |
import com.github.weisj.jsvg.attributes.transform.TransformPart; |
| 40 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 41 |
import com.github.weisj.jsvg.geometry.size.Angle; |
| 42 |
import com.github.weisj.jsvg.geometry.size.AngleUnit; |
| 43 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 44 |
import com.github.weisj.jsvg.geometry.size.Percentage; |
| 45 |
import com.github.weisj.jsvg.geometry.size.Unit; |
| 46 |
import com.github.weisj.jsvg.logging.Logger; |
| 47 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 48 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 49 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 50 |
import com.github.weisj.jsvg.parser.NumberListSplitter; |
| 51 |
import com.github.weisj.jsvg.parser.PaintParser; |
| 52 |
|
| 53 |
|
| 54 |
public final class AttributeParser { |
| 55 |
|
| 56 |
private static final Logger LOGGER = LogFactory.createLogger(AttributeParser.class); |
| 57 |
private final @NotNull PaintParser paintParser; |
| 58 |
|
| 59 |
public AttributeParser(@NotNull PaintParser paintParser) { |
| 60 |
this.paintParser = paintParser; |
| 61 |
} |
| 62 |
|
| 63 |
@Contract("_,!null,_ -> !null") |
| 64 |
public @Nullable Length parseLength(@Nullable String value, @Nullable Length fallback, |
| 65 |
@NotNull PercentageDimension dimension) { |
| 66 |
return parseSuffixUnit(value, Unit.RAW, fallback, u -> { |
| 67 |
if (u == Unit.PERCENTAGE) { |
| 68 |
switch (dimension) { |
| 69 |
case WIDTH: |
| 70 |
return Unit.PERCENTAGE_WIDTH; |
| 71 |
case HEIGHT: |
| 72 |
return Unit.PERCENTAGE_HEIGHT; |
| 73 |
case LENGTH: |
| 74 |
return Unit.PERCENTAGE_LENGTH; |
| 75 |
case CUSTOM: |
| 76 |
return Unit.PERCENTAGE; |
| 77 |
case NONE: |
| 78 |
return null; |
| 79 |
} |
| 80 |
} |
| 81 |
return u; |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
@Contract("_,!null -> !null") |
| 86 |
public @Nullable Duration parseTimeOffsetValue(@Nullable String value, @Nullable Duration fallback) { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
return parseSuffixUnit(value, TimeUnit.Raw, fallback, u -> u); |
| 92 |
} |
| 93 |
|
| 94 |
@Contract("_,!null -> !null") |
| 95 |
public @Nullable Duration parseDuration(@Nullable String value, @Nullable Duration fallback) { |
| 96 |
if (value == null) return fallback; |
| 97 |
if ("indefinite".equals(value)) return Duration.INDEFINITE; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Duration timeCount = parseSuffixUnit(value, TimeUnit.Raw, null, u -> u); |
| 104 |
if (timeCount != null) return timeCount; |
| 105 |
|
| 106 |
return fallback; |
| 107 |
} |
| 108 |
|
| 109 |
@Contract("_,!null -> !null") |
| 110 |
public @Nullable Percentage parsePercentage(@Nullable String value, @Nullable Percentage fallback) { |
| 111 |
return parsePercentage(value, fallback, 0, 1); |
| 112 |
} |
| 113 |
|
| 114 |
@Contract("_,!null,_,_ -> !null") |
| 115 |
public @Nullable Percentage parsePercentage(@Nullable String value, @Nullable Percentage fallback, float min, |
| 116 |
float max) { |
| 117 |
if (value == null) return fallback; |
| 118 |
try { |
| 119 |
float parsed; |
| 120 |
if (value.endsWith("%")) { |
| 121 |
parsed = Float.parseFloat(value.substring(0, value.length() - 1)) / 100f; |
| 122 |
} else { |
| 123 |
parsed = Float.parseFloat(value); |
| 124 |
} |
| 125 |
return new Percentage(Math.max(min, Math.min(max, parsed))); |
| 126 |
} catch (NumberFormatException e) { |
| 127 |
return fallback; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Contract("_,_,!null,_ -> !null") |
| 132 |
private <U, V> @Nullable V parseSuffixUnit(@Nullable String value, @NotNull SuffixUnit<U, V> defaultUnit, |
| 133 |
@Nullable V fallback, |
| 134 |
@NotNull UnaryOperator<@Nullable SuffixUnit<U, V>> unitMapper) { |
| 135 |
if (value == null) return fallback; |
| 136 |
SuffixUnit<U, V> unit = defaultUnit; |
| 137 |
String lower = value.toLowerCase(Locale.ENGLISH); |
| 138 |
int i = lower.length() - 1; |
| 139 |
for (; i >= 0; i--) { |
| 140 |
if (Character.isDigit(lower.charAt(i))) { |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
String suffix = lower.substring(i + 1); |
| 145 |
for (SuffixUnit<U, V> u : defaultUnit.units()) { |
| 146 |
if (suffix.equals(u.suffix())) { |
| 147 |
unit = u; |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
unit = unitMapper.apply(unit); |
| 152 |
if (unit == null) return fallback; |
| 153 |
String str = lower.substring(0, lower.length() - unit.suffix().length()); |
| 154 |
try { |
| 155 |
return unit.valueOf(Float.parseFloat(str)); |
| 156 |
} catch (NumberFormatException e) { |
| 157 |
return fallback; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public int parseInt(@Nullable String value, int fallback) { |
| 162 |
return ParserUtil.parseInt(value, fallback); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
@Contract("_,!null -> !null") |
| 167 |
public @Nullable Length parseNumber(@Nullable String value, @Nullable Length fallback) { |
| 168 |
return ParserUtil.parseNumber(value, fallback); |
| 169 |
} |
| 170 |
|
| 171 |
public float parseFloat(@Nullable String value, float fallback) { |
| 172 |
return ParserUtil.parseFloat(value, fallback); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
public @NotNull Angle parseAngle(@Nullable String value, @NotNull Angle fallback) { |
| 177 |
if (value == null) return fallback; |
| 178 |
AngleUnit unit = AngleUnit.Raw; |
| 179 |
String lower = value.toLowerCase(Locale.ENGLISH); |
| 180 |
for (AngleUnit u : AngleUnit.units()) { |
| 181 |
if (lower.endsWith(u.suffix())) { |
| 182 |
unit = u; |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
String str = lower.substring(0, lower.length() - unit.suffix().length()); |
| 187 |
try { |
| 188 |
return new Angle(unit, Float.parseFloat(str)); |
| 189 |
} catch (NumberFormatException e) { |
| 190 |
return fallback; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Contract("_,!null,_ -> !null") |
| 195 |
public @NotNull Length @Nullable [] parseLengthList(@Nullable String value, @NotNull Length @Nullable [] fallback, |
| 196 |
@NotNull PercentageDimension dimension) { |
| 197 |
if (value != null && value.equalsIgnoreCase("none")) return new Length[0]; |
| 198 |
String[] values = parseStringList(value, NumberListSplitter.INSTANCE, null); |
| 199 |
if (values == null) return fallback; |
| 200 |
Length[] ret = new Length[values.length]; |
| 201 |
for (int i = 0; i < ret.length; i++) { |
| 202 |
Length length = parseLength(values[i], null, dimension); |
| 203 |
if (length == null) return fallback; |
| 204 |
ret[i] = length; |
| 205 |
} |
| 206 |
return ret; |
| 207 |
} |
| 208 |
|
| 209 |
public float @NotNull [] parseFloatList(@Nullable String value) { |
| 210 |
return ParserUtil.parseFloatList(value); |
| 211 |
} |
| 212 |
|
| 213 |
public double @NotNull [] parseDoubleList(@Nullable String value) { |
| 214 |
return ParserUtil.parseDoubleList(value); |
| 215 |
} |
| 216 |
|
| 217 |
public @NotNull String @NotNull [] parseStringList(@Nullable String value, @NotNull ListSplitter listSplitter) { |
| 218 |
return ParserUtil.parseStringList(value, listSplitter); |
| 219 |
} |
| 220 |
|
| 221 |
@Contract("_,_,!null -> !null") |
| 222 |
public @NotNull String @Nullable [] parseStringList(@Nullable String value, @NotNull ListSplitter listSplitter, |
| 223 |
@NotNull String @Nullable [] fallback) { |
| 224 |
return ParserUtil.parseStringList(value, listSplitter, fallback); |
| 225 |
} |
| 226 |
|
| 227 |
public @Nullable SVGPaint parsePaint(@Nullable String value, @NotNull AttributeNode attributeNode) { |
| 228 |
return paintParser.parsePaint(value); |
| 229 |
} |
| 230 |
|
| 231 |
public <E extends Enum<E>> @NotNull E parseEnum(@Nullable String value, @NotNull E fallback) { |
| 232 |
E e = parseEnum(value, fallback.getDeclaringClass()); |
| 233 |
if (e == null) return fallback; |
| 234 |
return e; |
| 235 |
} |
| 236 |
|
| 237 |
public <E extends Enum<E>> @Nullable E parseEnum(@Nullable String value, @NotNull Class<E> enumType) { |
| 238 |
if (value == null) return null; |
| 239 |
for (E enumConstant : enumType.getEnumConstants()) { |
| 240 |
String name = enumConstant instanceof HasMatchName |
| 241 |
? ((HasMatchName) enumConstant).matchName() |
| 242 |
: enumConstant.name(); |
| 243 |
if (name.equalsIgnoreCase(value)) return enumConstant; |
| 244 |
} |
| 245 |
return null; |
| 246 |
} |
| 247 |
|
| 248 |
private static void warnIllegalTransform(@NotNull String value, @NotNull String input) { |
| 249 |
LOGGER.log(Level.WARNING, |
| 250 |
() -> String.format("Illegal transform definition '%s' encountered error while parsing '%s'", |
| 251 |
value, input)); |
| 252 |
} |
| 253 |
|
| 254 |
private static final class RawTransformFunction { |
| 255 |
final int endIndex; |
| 256 |
final @NotNull String name; |
| 257 |
final @NotNull String args; |
| 258 |
|
| 259 |
RawTransformFunction(int endIndex, @NotNull String name, @NotNull String args) { |
| 260 |
this.endIndex = endIndex; |
| 261 |
this.name = name; |
| 262 |
this.args = args; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private static @Nullable AttributeParser.RawTransformFunction parseNextTransformFunction(@NotNull String value, |
| 274 |
int start) { |
| 275 |
int i = start; |
| 276 |
int len = value.length(); |
| 277 |
|
| 278 |
while (i < len && (Character.isWhitespace(value.charAt(i)) || value.charAt(i) == ',')) { |
| 279 |
i++; |
| 280 |
} |
| 281 |
if (i >= len) return null; |
| 282 |
|
| 283 |
int nameStart = i; |
| 284 |
while (i < len && (Character.isLetterOrDigit(value.charAt(i)) |
| 285 |
|| value.charAt(i) == '-' || value.charAt(i) == '_')) { |
| 286 |
i++; |
| 287 |
} |
| 288 |
if (i >= len || value.charAt(i) != '(') return null; |
| 289 |
String name = value.substring(nameStart, i); |
| 290 |
i++; |
| 291 |
int argStart = i; |
| 292 |
while (i < len && value.charAt(i) != ')') |
| 293 |
i++; |
| 294 |
if (i >= len) return null; |
| 295 |
String args = value.substring(argStart, i); |
| 296 |
i++; |
| 297 |
return new RawTransformFunction(i, name, args); |
| 298 |
} |
| 299 |
|
| 300 |
public @Nullable List<@NotNull TransformPart> parseTransform(@Nullable String value) { |
| 301 |
if (value == null) return null; |
| 302 |
if ("none".equals(value)) return null; |
| 303 |
List<TransformPart> parts = new ArrayList<>(); |
| 304 |
int i = 0; |
| 305 |
while (i < value.length()) { |
| 306 |
|
| 307 |
int skipped = i; |
| 308 |
while (skipped < value.length() |
| 309 |
&& (Character.isWhitespace(value.charAt(skipped)) || value.charAt(skipped) == ',')) |
| 310 |
skipped++; |
| 311 |
if (skipped >= value.length()) break; |
| 312 |
|
| 313 |
RawTransformFunction parsed = parseNextTransformFunction(value, i); |
| 314 |
if (parsed == null) { |
| 315 |
warnIllegalTransform(value, value.substring(i)); |
| 316 |
return null; |
| 317 |
} |
| 318 |
TransformPart part = parseSingleTransformPart(parsed); |
| 319 |
if (part == null) { |
| 320 |
warnIllegalTransform(value, parsed.args); |
| 321 |
return null; |
| 322 |
} |
| 323 |
parts.add(part); |
| 324 |
i = parsed.endIndex; |
| 325 |
} |
| 326 |
return parts; |
| 327 |
} |
| 328 |
|
| 329 |
private @Nullable TransformPart parseSingleTransformPart(@NotNull RawTransformFunction transformFunction) { |
| 330 |
String command = transformFunction.name.toLowerCase(Locale.ENGLISH); |
| 331 |
TransformPart.TransformType type = parseEnum(command, TransformPart.TransformType.class); |
| 332 |
if (type == null) return null; |
| 333 |
|
| 334 |
return parseTransformPart(type, transformFunction.args); |
| 335 |
} |
| 336 |
|
| 337 |
public @Nullable TransformPart parseTransformPart(TransformPart.TransformType type, @NotNull String value) { |
| 338 |
String[] values = parseStringList(value, NumberListSplitter.INSTANCE); |
| 339 |
Length[] lengths = parseTransformLengths(type, values); |
| 340 |
if (lengths == null) return null; |
| 341 |
return new TransformPart(type, lengths); |
| 342 |
} |
| 343 |
|
| 344 |
private Length @Nullable [] parseTransformLengths(TransformPart.@NotNull TransformType type, |
| 345 |
@NotNull String @NotNull [] values) { |
| 346 |
Length[] lengths; |
| 347 |
switch (type) { |
| 348 |
case MATRIX: |
| 349 |
if (values.length == 4) { |
| 350 |
lengths = toNonnullArray( |
| 351 |
parseNumber(values[0], null), |
| 352 |
parseNumber(values[1], null), |
| 353 |
parseNumber(values[2], null), |
| 354 |
parseNumber(values[3], null), |
| 355 |
Length.ZERO, |
| 356 |
Length.ZERO); |
| 357 |
} else if (values.length == 6) { |
| 358 |
lengths = toNonnullArray( |
| 359 |
parseNumber(values[0], null), |
| 360 |
parseNumber(values[1], null), |
| 361 |
parseNumber(values[2], null), |
| 362 |
parseNumber(values[3], null), |
| 363 |
parseNumber(values[4], null), |
| 364 |
parseNumber(values[5], null)); |
| 365 |
} else { |
| 366 |
lengths = null; |
| 367 |
} |
| 368 |
break; |
| 369 |
case TRANSLATE: |
| 370 |
if (values.length == 1) { |
| 371 |
lengths = toNonnullArray( |
| 372 |
parseLength(values[0], null, PercentageDimension.WIDTH), |
| 373 |
Length.ZERO); |
| 374 |
} else { |
| 375 |
lengths = toNonnullArray( |
| 376 |
parseLength(values[0], null, PercentageDimension.WIDTH), |
| 377 |
parseLength(values[1], null, PercentageDimension.HEIGHT)); |
| 378 |
} |
| 379 |
break; |
| 380 |
case TRANSLATE_X: |
| 381 |
lengths = toNonnullArray( |
| 382 |
parseLength(values[0], null, PercentageDimension.WIDTH)); |
| 383 |
break; |
| 384 |
case TRANSLATE_Y: |
| 385 |
lengths = toNonnullArray( |
| 386 |
parseLength(values[0], null, PercentageDimension.HEIGHT)); |
| 387 |
break; |
| 388 |
case ROTATE: |
| 389 |
if (values.length > 2) { |
| 390 |
lengths = toNonnullArray( |
| 391 |
parseNumber(values[0], null), |
| 392 |
parseLength(values[1], null, PercentageDimension.WIDTH), |
| 393 |
parseLength(values[2], null, PercentageDimension.HEIGHT)); |
| 394 |
} else { |
| 395 |
lengths = toNonnullArray( |
| 396 |
parseNumber(values[0], null)); |
| 397 |
} |
| 398 |
break; |
| 399 |
case SCALE: |
| 400 |
case SKEW: |
| 401 |
if (values.length == 1) { |
| 402 |
lengths = toNonnullArray( |
| 403 |
parseNumber(values[0], null)); |
| 404 |
} else { |
| 405 |
lengths = toNonnullArray( |
| 406 |
parseNumber(values[0], null), |
| 407 |
parseNumber(values[1], null)); |
| 408 |
} |
| 409 |
break; |
| 410 |
case SCALE_X: |
| 411 |
case SCALE_Y: |
| 412 |
case SKEW_X: |
| 413 |
case SKEW_Y: |
| 414 |
lengths = toNonnullArray( |
| 415 |
parseLength(values[0], null, PercentageDimension.NONE)); |
| 416 |
break; |
| 417 |
default: |
| 418 |
lengths = null; |
| 419 |
} |
| 420 |
return lengths; |
| 421 |
} |
| 422 |
|
| 423 |
public @NotNull PaintParser paintParser() { |
| 424 |
return paintParser; |
| 425 |
} |
| 426 |
|
| 427 |
} |