| •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 java.awt.*; |
| 25 |
import java.net.URI; |
| 26 |
import java.util.*; |
| 27 |
import java.util.List; |
| 28 |
import java.util.function.BiFunction; |
| 29 |
import java.util.stream.Collectors; |
| 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.value.*; |
| 37 |
import com.github.weisj.jsvg.attributes.Animatable; |
| 38 |
import com.github.weisj.jsvg.attributes.Inherited; |
| 39 |
import com.github.weisj.jsvg.attributes.filter.FilterChannelKey; |
| 40 |
import com.github.weisj.jsvg.attributes.transform.TransformPart; |
| 41 |
import com.github.weisj.jsvg.attributes.value.*; |
| 42 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 43 |
import com.github.weisj.jsvg.geometry.size.Percentage; |
| 44 |
import com.github.weisj.jsvg.geometry.size.Unit; |
| 45 |
import com.github.weisj.jsvg.nodes.ClipPath; |
| 46 |
import com.github.weisj.jsvg.nodes.Mask; |
| 47 |
import com.github.weisj.jsvg.nodes.animation.Animate; |
| 48 |
import com.github.weisj.jsvg.nodes.animation.AnimateTransform; |
| 49 |
import com.github.weisj.jsvg.nodes.animation.BaseAnimationNode; |
| 50 |
import com.github.weisj.jsvg.nodes.filter.Filter; |
| 51 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 52 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 53 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 54 |
import com.github.weisj.jsvg.paint.impl.PredefinedPaints; |
| 55 |
import com.github.weisj.jsvg.parser.PaintParser; |
| 56 |
import com.github.weisj.jsvg.parser.css.CssParser; |
| 57 |
import com.github.weisj.jsvg.parser.css.StyleSheet; |
| 58 |
import com.github.weisj.jsvg.parser.css.impl.SimpleCssParser; |
| 59 |
import com.github.weisj.jsvg.parser.resources.ResourceLoader; |
| 60 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 61 |
import com.github.weisj.jsvg.renderer.animation.AnimationState; |
| 62 |
import com.github.weisj.jsvg.util.AttributeUtil; |
| 63 |
import com.github.weisj.jsvg.view.ViewBox; |
| 64 |
|
| 65 |
public final class AttributeNode { |
| 66 |
|
| 67 |
public enum ElementRelation { |
| 68 |
GEOMETRY_DATA, |
| 69 |
PAINTED_CHILD, |
| 70 |
TEMPLATE, |
| 71 |
PAINT_SERVER |
| 72 |
} |
| 73 |
|
| 74 |
private static final Length Top = new Length(Unit.PERCENTAGE_HEIGHT, 0f); |
| 75 |
private static final Length CenterHeight = new Length(Unit.PERCENTAGE_HEIGHT, 50f); |
| 76 |
private static final Length Bottom = new Length(Unit.PERCENTAGE_HEIGHT, 100f); |
| 77 |
private static final Length Left = new Length(Unit.PERCENTAGE_WIDTH, 0f); |
| 78 |
private static final Length CenterWidth = new Length(Unit.PERCENTAGE_WIDTH, 50f); |
| 79 |
private static final Length Right = new Length(Unit.PERCENTAGE_WIDTH, 100f); |
| 80 |
private static final Length FALLBACK_LENGTH = new Length(Unit.RAW, 0f); |
| 81 |
private static final Percentage FALLBACK_PERCENTAGE = new Percentage(1f); |
| 82 |
private static final MeasureContext DUMMY_MEASURE_CONTEXT = |
| 83 |
new MeasureContext(0, 0, 0, 0, 0, new AnimationState(0, 0)); |
| 84 |
|
| 85 |
private final @NotNull String tagName; |
| 86 |
private final @NotNull Map<String, String> attributes; |
| 87 |
private final @NotNull List<@NotNull StyleSheet> styleSheets; |
| 88 |
|
| 89 |
private ParsedElement element = null; |
| 90 |
|
| 91 |
public AttributeNode(@NotNull String tagName, @NotNull Map<String, String> attributes, |
| 92 |
@NotNull List<@NotNull StyleSheet> styleSheets) { |
| 93 |
this.tagName = tagName; |
| 94 |
this.attributes = attributes; |
| 95 |
this.styleSheets = styleSheets; |
| 96 |
} |
| 97 |
|
| 98 |
void setElement(ParsedElement element) { |
| 99 |
this.element = element; |
| 100 |
} |
| 101 |
|
| 102 |
private @NotNull LoadHelper loadHelper() { |
| 103 |
return document().loadHelper(); |
| 104 |
} |
| 105 |
|
| 106 |
public @NotNull AttributeNode copy() { |
| 107 |
AttributeNode node = new AttributeNode(tagName, new HashMap<>(attributes), styleSheets); |
| 108 |
node.setElement(element); |
| 109 |
return node; |
| 110 |
} |
| 111 |
|
| 112 |
void prepareForNodeBuilding() { |
| 113 |
Map<String, String> styleSheetAttributes = new HashMap<>(); |
| 114 |
|
| 115 |
|
| 116 |
String styleStr = attributes.get("style"); |
| 117 |
if (styleStr != null && !AttributeUtil.isBlank(styleStr)) { |
| 118 |
CssParser cssParser = element.document().loaderContext().cssParser(); |
| 119 |
if (cssParser instanceof SimpleCssParser) { |
| 120 |
((SimpleCssParser) cssParser).parseRules(Collections.singletonList(styleStr.toCharArray())) |
| 121 |
.forEach(p -> { |
| 122 |
styleSheetAttributes.put(p.name(), p.value()); |
| 123 |
}); |
| 124 |
} else { |
| 125 |
|
| 126 |
StyleSheet sheet = cssParser.parse( |
| 127 |
Arrays.asList( |
| 128 |
element.tagName().toCharArray(), |
| 129 |
new char[] {'{'}, |
| 130 |
styleStr.toCharArray(), |
| 131 |
new char[] {'}'})); |
| 132 |
sheet.forEachMatchingRule(element, p -> { |
| 133 |
styleSheetAttributes.put(p.name(), p.value()); |
| 134 |
}); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
List<StyleSheet> sheets = styleSheets(); |
| 139 |
|
| 140 |
|
| 141 |
for (int i = sheets.size() - 1; i >= 0; i--) { |
| 142 |
StyleSheet sheet = sheets.get(i); |
| 143 |
sheet.forEachMatchingRule(element, p -> { |
| 144 |
if (!styleSheetAttributes.containsKey(p.name())) { |
| 145 |
styleSheetAttributes.put(p.name(), p.value()); |
| 146 |
} |
| 147 |
}); |
| 148 |
} |
| 149 |
attributes.putAll(styleSheetAttributes); |
| 150 |
} |
| 151 |
|
| 152 |
public @NotNull ParsedDocument document() { |
| 153 |
return element().document(); |
| 154 |
} |
| 155 |
|
| 156 |
public @NotNull ParsedElement element() { |
| 157 |
return element; |
| 158 |
} |
| 159 |
|
| 160 |
@NotNull |
| 161 |
List<@NotNull StyleSheet> styleSheets() { |
| 162 |
return styleSheets; |
| 163 |
} |
| 164 |
|
| 165 |
private <T> @Nullable T getElementByUrl(@NotNull Class<T> type, @Nullable String value) { |
| 166 |
if (value == null) return null; |
| 167 |
return loadHelper().elementLoader().loadElement(type, value, document()); |
| 168 |
} |
| 169 |
|
| 170 |
private <T> T recordIndirectChild(T child, String value, ElementRelation relation) { |
| 171 |
if (child != null && relation == ElementRelation.PAINTED_CHILD) { |
| 172 |
ParsedElement containingElement = getElementByUrl(ParsedElement.class, value); |
| 173 |
if (containingElement != null) { |
| 174 |
element().addIndirectChild(containingElement); |
| 175 |
} |
| 176 |
} |
| 177 |
return child; |
| 178 |
} |
| 179 |
|
| 180 |
public <T> @Nullable T getElementByHref(@NotNull Class<T> type, @Nullable String value, ElementRelation relation) { |
| 181 |
return recordIndirectChild(getElementByUrl(type, value), value, relation); |
| 182 |
} |
| 183 |
|
| 184 |
public <T> @Nullable T getElementByHref(@NotNull Class<T> type, @NotNull Category category, |
| 185 |
@Nullable String value, ElementRelation relation) { |
| 186 |
T e = getElementByUrl(type, value); |
| 187 |
if (e == null) return null; |
| 188 |
for (Category cat : e.getClass().getAnnotation(ElementCategories.class).value()) { |
| 189 |
if (cat == category) return recordIndirectChild(e, value, relation); |
| 190 |
} |
| 191 |
return null; |
| 192 |
} |
| 193 |
|
| 194 |
public @NotNull Map<String, String> attributes() { |
| 195 |
return attributes; |
| 196 |
} |
| 197 |
|
| 198 |
public @NotNull String tagName() { |
| 199 |
return tagName; |
| 200 |
} |
| 201 |
|
| 202 |
public boolean tagIsOneOf(@NotNull String... tags) { |
| 203 |
for (String tag : tags) { |
| 204 |
if (tagName.equals(tag)) return true; |
| 205 |
} |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
public @NotNull List<@NotNull String> classNames() { |
| 210 |
@NotNull String @NotNull [] classes = getStringList("class", SeparatorMode.WHITESPACE_ONLY); |
| 211 |
if (classes.length == 0) return Collections.emptyList(); |
| 212 |
return Arrays.asList(classes); |
| 213 |
} |
| 214 |
|
| 215 |
public @Nullable String getValue(@NotNull String key) { |
| 216 |
return attributes.get(key); |
| 217 |
} |
| 218 |
|
| 219 |
public @NotNull Color getColor(@NotNull String key) { |
| 220 |
return getColor(key, PaintParser.DEFAULT_COLOR); |
| 221 |
} |
| 222 |
|
| 223 |
@Contract("_,!null -> !null") |
| 224 |
public @Nullable Color getColor(@NotNull String key, @Nullable Color fallback) { |
| 225 |
String value = getValue(key); |
| 226 |
if (value == null) return fallback; |
| 227 |
Color c = loadHelper().attributeParser().paintParser().parseColor(value.toLowerCase(Locale.ENGLISH)); |
| 228 |
return c != null ? c : fallback; |
| 229 |
} |
| 230 |
|
| 231 |
public @Nullable SVGPaint getPaint(@NotNull String key, Inherited inherited, Animatable animatable) { |
| 232 |
return getPaint(key, null, inherited, animatable); |
| 233 |
} |
| 234 |
|
| 235 |
@Contract("_,!null,_,_ -> !null") |
| 236 |
public @Nullable SVGPaint getPaint(@NotNull String key, @Nullable SVGPaint fallback, |
| 237 |
Inherited inherited, Animatable animatable) { |
| 238 |
SVGPaint value = getPaintInternal(key, fallback); |
| 239 |
if (animatable == Animatable.YES) { |
| 240 |
SVGPaint initial = value; |
| 241 |
if (initial == null) { |
| 242 |
initial = inherited == Inherited.YES |
| 243 |
? PredefinedPaints.INHERITED |
| 244 |
: PredefinedPaints.DEFAULT_PAINT; |
| 245 |
} |
| 246 |
AnimatedPaint animatedPaint = getAnimatedPaint(key, initial); |
| 247 |
if (animatedPaint != null) return animatedPaint; |
| 248 |
} |
| 249 |
return value; |
| 250 |
} |
| 251 |
|
| 252 |
@Contract("_,!null -> !null") |
| 253 |
private @Nullable SVGPaint getPaintInternal(@NotNull String key, @Nullable SVGPaint fallback) { |
| 254 |
SVGPaint paint = parsePaint(getValue(key)); |
| 255 |
if (paint == null) return fallback; |
| 256 |
return paint; |
| 257 |
} |
| 258 |
|
| 259 |
public @Nullable SVGPaint parsePaint(@Nullable String value) { |
| 260 |
if (value == null) return null; |
| 261 |
|
| 262 |
SVGPaint paint = getElementByHref(SVGPaint.class, value, ElementRelation.PAINT_SERVER); |
| 263 |
if (paint != null) return paint; |
| 264 |
return loadHelper().attributeParser().parsePaint(value, this); |
| 265 |
} |
| 266 |
|
| 267 |
public @Nullable Length getLength(@NotNull String key, @NotNull PercentageDimension dimension) { |
| 268 |
return getLength(key, dimension, (Length) null); |
| 269 |
} |
| 270 |
|
| 271 |
public @NotNull Length getLength(@NotNull String key, @NotNull PercentageDimension dimension, float fallback) { |
| 272 |
return getLength(key, dimension, Unit.RAW.valueOf(fallback)); |
| 273 |
} |
| 274 |
|
| 275 |
@Contract("_,_,!null -> !null") |
| 276 |
public @Nullable Length getLength(@NotNull String key, @NotNull PercentageDimension dimension, |
| 277 |
@Nullable Length fallback) { |
| 278 |
return (Length) getLength(key, dimension, fallback, Inherited.NO, Animatable.NO); |
| 279 |
} |
| 280 |
|
| 281 |
public @Nullable LengthValue getLength(@NotNull String key, @NotNull PercentageDimension dimension, |
| 282 |
Inherited inherited, Animatable animatable) { |
| 283 |
return getLength(key, dimension, null, inherited, animatable); |
| 284 |
} |
| 285 |
|
| 286 |
@Contract("_,_,!null,_,_ -> !null") |
| 287 |
public @Nullable LengthValue getLength(@NotNull String key, @NotNull PercentageDimension dimension, |
| 288 |
@Nullable LengthValue fallback, Inherited inherited, Animatable animatable) { |
| 289 |
LengthValue value = getLengthInternal(key, dimension); |
| 290 |
if (value == FALLBACK_LENGTH) { |
| 291 |
value = fallback; |
| 292 |
} |
| 293 |
|
| 294 |
if (animatable == Animatable.YES) { |
| 295 |
LengthValue initial = value; |
| 296 |
if (initial == null) { |
| 297 |
initial = inherited == Inherited.YES |
| 298 |
? Length.INHERITED |
| 299 |
: NeutralElements.NEUTRAL_LENGTH; |
| 300 |
} |
| 301 |
if (initial instanceof AnimatedLength) { |
| 302 |
initial = ((AnimatedLength) initial).initial(); |
| 303 |
} |
| 304 |
AnimatedLength animatedLength = getAnimatedLength(key, initial, dimension); |
| 305 |
if (animatedLength != null) return animatedLength; |
| 306 |
} |
| 307 |
|
| 308 |
return value; |
| 309 |
} |
| 310 |
|
| 311 |
public @NotNull Duration getDuration(@NotNull String key, @NotNull Duration fallback) { |
| 312 |
return loadHelper().attributeParser().parseTimeOffsetValue(getValue(key), fallback); |
| 313 |
} |
| 314 |
|
| 315 |
private @NotNull Length getLengthInternal(@NotNull String key, @NotNull PercentageDimension dimension) { |
| 316 |
return loadHelper().attributeParser().parseLength(getValue(key), FALLBACK_LENGTH, dimension); |
| 317 |
} |
| 318 |
|
| 319 |
public @NotNull Length getHorizontalReferenceLengthFromKey(@NotNull String key) { |
| 320 |
return getHorizontalReferenceLength(getValue(key)); |
| 321 |
} |
| 322 |
|
| 323 |
public @NotNull Length getVerticalReferenceLengthFromKey(@NotNull String key) { |
| 324 |
return getVerticalReferenceLength(getValue(key)); |
| 325 |
} |
| 326 |
|
| 327 |
public @NotNull Length getHorizontalReferenceLength(@Nullable String value) { |
| 328 |
if ("left".equals(value)) { |
| 329 |
return Left; |
| 330 |
} else if ("center".equals(value)) { |
| 331 |
return CenterWidth; |
| 332 |
} else if ("right".equals(value)) { |
| 333 |
return Right; |
| 334 |
} else { |
| 335 |
return loadHelper().attributeParser().parseLength(value, Length.ZERO, PercentageDimension.WIDTH); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
public @NotNull Length getVerticalReferenceLength(@Nullable String value) { |
| 340 |
if ("top".equals(value)) { |
| 341 |
return Top; |
| 342 |
} else if ("center".equals(value)) { |
| 343 |
return CenterHeight; |
| 344 |
} else if ("bottom".equals(value)) { |
| 345 |
return Bottom; |
| 346 |
} else { |
| 347 |
return loadHelper().attributeParser().parseLength(value, Length.ZERO, PercentageDimension.HEIGHT); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
public boolean isHorizontalKeyword(@NotNull String value) { |
| 352 |
return "left".equals(value) || "right".equals(value); |
| 353 |
} |
| 354 |
|
| 355 |
public boolean isVerticalKeyword(@NotNull String value) { |
| 356 |
return "top".equals(value) || "bottom".equals(value); |
| 357 |
} |
| 358 |
|
| 359 |
@Contract("_,!null -> !null") |
| 360 |
public @Nullable Percentage getPercentage(@NotNull String key, @Nullable Percentage fallback) { |
| 361 |
return loadHelper().attributeParser().parsePercentage(getValue(key), fallback); |
| 362 |
} |
| 363 |
|
| 364 |
public @Nullable PercentageValue getPercentage(@NotNull String key, Inherited inherited, Animatable animatable) { |
| 365 |
return getPercentage(key, null, inherited, animatable); |
| 366 |
} |
| 367 |
|
| 368 |
@Contract("_,!null,_,_ -> !null") |
| 369 |
public @Nullable PercentageValue getPercentage(@NotNull String key, @Nullable PercentageValue fallback, |
| 370 |
Inherited inherited, Animatable animatable) { |
| 371 |
PercentageValue value = |
| 372 |
loadHelper().attributeParser().parsePercentage(getValue(key), FALLBACK_PERCENTAGE); |
| 373 |
if (value == FALLBACK_PERCENTAGE) { |
| 374 |
value = fallback; |
| 375 |
} |
| 376 |
|
| 377 |
if (animatable == Animatable.YES) { |
| 378 |
PercentageValue initial = value; |
| 379 |
if (initial == null) { |
| 380 |
initial = inherited == Inherited.YES |
| 381 |
? Percentage.INHERITED |
| 382 |
: NeutralElements.NEUTRAL_PERCENTAGE; |
| 383 |
} |
| 384 |
if (initial instanceof AnimatedPercentage) { |
| 385 |
initial = ((AnimatedPercentage) initial).initial(); |
| 386 |
} |
| 387 |
AnimatedPercentage animatedPercentage = getAnimatedPercentage(key, initial); |
| 388 |
if (animatedPercentage != null) return animatedPercentage; |
| 389 |
} |
| 390 |
return value; |
| 391 |
} |
| 392 |
|
| 393 |
public @NotNull Length @NotNull [] getLengthList(@NotNull String key, @NotNull PercentageDimension dimension) { |
| 394 |
return getLengthList(key, new Length[0], dimension); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
@Contract("_,!null,_ -> !null") |
| 399 |
public @NotNull Length @Nullable [] getLengthList(@NotNull String key, @NotNull Length @Nullable [] fallback, |
| 400 |
@NotNull PercentageDimension dimension) { |
| 401 |
return loadHelper().attributeParser().parseLengthList(getValue(key), fallback, dimension); |
| 402 |
} |
| 403 |
|
| 404 |
public float @NotNull [] getFloatList(@NotNull String key) { |
| 405 |
return loadHelper().attributeParser().parseFloatList(getValue(key)); |
| 406 |
} |
| 407 |
|
| 408 |
public @NotNull FloatListValue getFloatList(@NotNull String key, Inherited inherited, Animatable animatable) { |
| 409 |
String value = getValue(key); |
| 410 |
float[] initialRaw = loadHelper().attributeParser().parseFloatList(getValue(key)); |
| 411 |
|
| 412 |
FloatListValue initial = value != null |
| 413 |
? new ConstantFloatList(initialRaw) |
| 414 |
: null; |
| 415 |
|
| 416 |
if (animatable == Animatable.YES) { |
| 417 |
if (initial == null) { |
| 418 |
if (inherited == Inherited.YES) { |
| 419 |
throw new IllegalStateException("Inherited values for float lists aren't implemented yet"); |
| 420 |
} |
| 421 |
initial = NeutralElements.NEUTRAL_FLOAT_LIST; |
| 422 |
} |
| 423 |
AnimatedFloatList animatedLength = getAnimatedFloatList(key, initial); |
| 424 |
if (animatedLength != null) return animatedLength; |
| 425 |
} |
| 426 |
return initial != null ? initial : ConstantFloatList.EMPTY; |
| 427 |
} |
| 428 |
|
| 429 |
public double @NotNull [] getDoubleList(@NotNull String key) { |
| 430 |
return loadHelper().attributeParser().parseDoubleList(getValue(key)); |
| 431 |
} |
| 432 |
|
| 433 |
public <E extends Enum<E>> @NotNull E getEnum(@NotNull String key, @NotNull E fallback) { |
| 434 |
return loadHelper().attributeParser().parseEnum(getValue(key), fallback); |
| 435 |
} |
| 436 |
|
| 437 |
public <E extends Enum<E>> @Nullable E getEnumNullable(@NotNull String key, @NotNull Class<E> enumType) { |
| 438 |
return loadHelper().attributeParser().parseEnum(getValue(key), enumType); |
| 439 |
} |
| 440 |
|
| 441 |
public @Nullable ClipPath getClipPath() { |
| 442 |
return getElementByUrl(ClipPath.class, getValue("clip-path")); |
| 443 |
} |
| 444 |
|
| 445 |
public @Nullable Mask getMask() { |
| 446 |
return getElementByUrl(Mask.class, getValue("mask")); |
| 447 |
} |
| 448 |
|
| 449 |
public @Nullable Filter getFilter() { |
| 450 |
return getElementByUrl(Filter.class, getValue("filter")); |
| 451 |
} |
| 452 |
|
| 453 |
public @NotNull FilterChannelKey getFilterChannelKey(@NotNull String key, @NotNull FilterChannelKey fallback) { |
| 454 |
String in = getValue(key); |
| 455 |
if (in == null) return fallback; |
| 456 |
return new FilterChannelKey.StringKey(in); |
| 457 |
} |
| 458 |
|
| 459 |
public @Nullable TransformValue parseTransform(@NotNull String key) { |
| 460 |
return parseTransform(key, Inherited.NO, Animatable.NO); |
| 461 |
} |
| 462 |
|
| 463 |
private @NotNull TransformValue createTransformValueFromParts(@NotNull List<TransformPart> parts) { |
| 464 |
for (TransformPart part : parts) { |
| 465 |
if (!part.canBeFlattened()) return new ConstantLengthTransform(parts); |
| 466 |
} |
| 467 |
|
| 468 |
return new ConstantTransform(new ConstantLengthTransform(parts).get(DUMMY_MEASURE_CONTEXT)); |
| 469 |
} |
| 470 |
|
| 471 |
public @Nullable TransformValue parseTransform(@NotNull String key, Inherited inherited, Animatable animatable) { |
| 472 |
List<TransformPart> parts = loadHelper().attributeParser().parseTransform(getValue(key)); |
| 473 |
TransformValue value = parts != null |
| 474 |
? createTransformValueFromParts(parts) |
| 475 |
: null; |
| 476 |
|
| 477 |
if (animatable == Animatable.YES) { |
| 478 |
TransformValue initial = value; |
| 479 |
if (initial == null) { |
| 480 |
initial = inherited == Inherited.YES |
| 481 |
? ConstantLengthTransform.INHERITED |
| 482 |
: NeutralElements.NEUTRAL_TRANSFORM; |
| 483 |
} |
| 484 |
if (initial instanceof AnimatedTransform) { |
| 485 |
initial = ((AnimatedTransform) initial).initial(); |
| 486 |
} |
| 487 |
AnimatedTransform animatedTransform = getAnimatedTransform(key, initial); |
| 488 |
if (animatedTransform != null) return animatedTransform; |
| 489 |
} |
| 490 |
|
| 491 |
return value; |
| 492 |
} |
| 493 |
|
| 494 |
public boolean hasAttribute(@NotNull String name) { |
| 495 |
return attributes.containsKey(name); |
| 496 |
} |
| 497 |
|
| 498 |
public @NotNull String @NotNull [] getStringList(@NotNull String name) { |
| 499 |
return getStringList(name, SeparatorMode.COMMA_AND_WHITESPACE); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
public @NotNull String @NotNull [] getStringList(@NotNull String name, SeparatorMode separatorMode) { |
| 504 |
return loadHelper().attributeParser().parseStringList(getValue(name), separatorMode); |
| 505 |
} |
| 506 |
|
| 507 |
public float getFloat(@NotNull String name, float fallback) { |
| 508 |
return loadHelper().attributeParser().parseFloat(getValue(name), fallback); |
| 509 |
} |
| 510 |
|
| 511 |
public float getNonNegativeFloat(@NotNull String name, float fallback) { |
| 512 |
float value = getFloat(name, fallback); |
| 513 |
if (Float.isFinite(value) && value < 0) return fallback; |
| 514 |
return value; |
| 515 |
} |
| 516 |
|
| 517 |
public int getInt(@NotNull String key, int fallback) { |
| 518 |
return loadHelper().attributeParser().parseInt(getValue(key), fallback); |
| 519 |
} |
| 520 |
|
| 521 |
public @Nullable String getHref() { |
| 522 |
String href = getValue("href"); |
| 523 |
if (href == null) return getValue("xlink:href"); |
| 524 |
return href; |
| 525 |
} |
| 526 |
|
| 527 |
public @Nullable ViewBox getViewBox() { |
| 528 |
float[] viewBoxCords = getFloatList("viewBox"); |
| 529 |
return viewBoxCords.length == 4 ? new ViewBox(viewBoxCords) : null; |
| 530 |
} |
| 531 |
|
| 532 |
public @NotNull AttributeParser parser() { |
| 533 |
return loadHelper().attributeParser(); |
| 534 |
} |
| 535 |
|
| 536 |
public @NotNull ResourceLoader resourceLoader() { |
| 537 |
return loadHelper().resourceLoader(); |
| 538 |
} |
| 539 |
|
| 540 |
public @Nullable URI resolveResourceURI(@NotNull String url) { |
| 541 |
return loadHelper().externalResourcePolicy().resolveResourceURI(document().rootURI(), url); |
| 542 |
} |
| 543 |
|
| 544 |
private <T extends BaseAnimationNode> List<@NotNull T> animateNodes(@NotNull String property, Class<T> type) { |
| 545 |
List<ParsedElement> parsedElements = element.animationElements().get(property); |
| 546 |
if (parsedElements == null || parsedElements.isEmpty()) return Collections.emptyList(); |
| 547 |
|
| 548 |
List<T> animateNodes = parsedElements |
| 549 |
.stream() |
| 550 |
.filter(n -> type.isInstance(n.node())) |
| 551 |
.map(n -> type.cast(n.nodeEnsuringBuildStatus(document().currentNestingDepth()))) |
| 552 |
.collect(Collectors.toList()); |
| 553 |
for (T animateNode : animateNodes) { |
| 554 |
document().registerAnimatedElement(animateNode); |
| 555 |
} |
| 556 |
return animateNodes; |
| 557 |
} |
| 558 |
|
| 559 |
private static <T, A extends T, N extends BaseAnimationNode> @Nullable A makeAnimated( |
| 560 |
@NotNull List<N> animationNodes, |
| 561 |
@NotNull T initial, |
| 562 |
@NotNull BiFunction<N, T, A> factory) { |
| 563 |
if (animationNodes.isEmpty()) return null; |
| 564 |
|
| 565 |
@NotNull T currentInitial = initial; |
| 566 |
@Nullable A lastAnimationValue = null; |
| 567 |
for (N animate : animationNodes) { |
| 568 |
A animated = factory.apply(animate, currentInitial); |
| 569 |
if (animated != null) { |
| 570 |
currentInitial = animated; |
| 571 |
lastAnimationValue = animated; |
| 572 |
} |
| 573 |
} |
| 574 |
return lastAnimationValue; |
| 575 |
} |
| 576 |
|
| 577 |
public @Nullable AnimatedLength getAnimatedLength(@NotNull String property, @NotNull LengthValue initial, |
| 578 |
@NotNull PercentageDimension dimension) { |
| 579 |
return makeAnimated( |
| 580 |
animateNodes(property, Animate.class), initial, |
| 581 |
(animate, currentInitial) -> animate.animatedLength(currentInitial, dimension, this)); |
| 582 |
} |
| 583 |
|
| 584 |
private @Nullable AnimatedFloatList getAnimatedFloatList(@NotNull String property, |
| 585 |
@NotNull FloatListValue initial) { |
| 586 |
return makeAnimated( |
| 587 |
animateNodes(property, Animate.class), initial, |
| 588 |
|
| 589 |
(Animate animate, FloatListValue currentInitial) -> animate.animatedFloatList(currentInitial, this)); |
| 590 |
} |
| 591 |
|
| 592 |
private @Nullable AnimatedPercentage getAnimatedPercentage(@NotNull String property, |
| 593 |
@NotNull PercentageValue initial) { |
| 594 |
return makeAnimated( |
| 595 |
animateNodes(property, Animate.class), initial, |
| 596 |
(animate, currentInitial) -> animate.animatedPercentage(currentInitial, this)); |
| 597 |
} |
| 598 |
|
| 599 |
private @Nullable AnimatedPaint getAnimatedPaint(@NotNull String property, @NotNull SVGPaint initial) { |
| 600 |
return makeAnimated( |
| 601 |
animateNodes(property, Animate.class), initial, |
| 602 |
(animate, currentInitial) -> animate.animatedPaint(currentInitial, this)); |
| 603 |
} |
| 604 |
|
| 605 |
public @Nullable AnimatedColor getAnimatedColor(@NotNull String property, @NotNull ColorValue initial) { |
| 606 |
return makeAnimated( |
| 607 |
animateNodes(property, Animate.class), initial, |
| 608 |
(animate, currentInitial) -> animate.animatedColor(currentInitial, this)); |
| 609 |
} |
| 610 |
|
| 611 |
public @Nullable AnimatedTransform getAnimatedTransform(@NotNull String property, |
| 612 |
@NotNull TransformValue initial) { |
| 613 |
return makeAnimated( |
| 614 |
animateNodes(property, AnimateTransform.class), initial, |
| 615 |
(animate, currentInitial) -> animate.animatedTransform(currentInitial, this)); |
| 616 |
} |
| 617 |
} |