| •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.nodes; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.Point2D; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.animation.AnimationPeriod; |
| 31 |
import com.github.weisj.jsvg.attributes.Coordinate; |
| 32 |
import com.github.weisj.jsvg.attributes.Overflow; |
| 33 |
import com.github.weisj.jsvg.attributes.value.LengthValue; |
| 34 |
import com.github.weisj.jsvg.geometry.size.Unit; |
| 35 |
import com.github.weisj.jsvg.nodes.container.CommonInnerViewContainer; |
| 36 |
import com.github.weisj.jsvg.nodes.filter.Filter; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 38 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 40 |
import com.github.weisj.jsvg.nodes.text.Text; |
| 41 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 42 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 43 |
import com.github.weisj.jsvg.renderer.animation.AnimationState; |
| 44 |
import com.github.weisj.jsvg.view.FloatSize; |
| 45 |
import com.github.weisj.jsvg.view.ViewBox; |
| 46 |
|
| 47 |
@ElementCategories({Category.Container, Category.Structural}) |
| 48 |
@PermittedContent( |
| 49 |
categories = {Category.Animation, Category.Descriptive, Category.Shape, Category.Structural, Category.Gradient}, |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
anyOf = {Anchor.class, ClipPath.class, Filter.class, Image.class, Mask.class, Marker.class, Pattern.class, |
| 55 |
Style.class, Text.class, View.class} |
| 56 |
) |
| 57 |
public final class SVG extends CommonInnerViewContainer { |
| 58 |
public static final String TAG = "svg"; |
| 59 |
|
| 60 |
private static final @NotNull Coordinate<LengthValue> TOP_LEVEL_TRANSFORM_ORIGIN = new Coordinate<>( |
| 61 |
Unit.PERCENTAGE_WIDTH.valueOf(50), |
| 62 |
Unit.PERCENTAGE_WIDTH.valueOf(50)); |
| 63 |
private static final float FALLBACK_WIDTH = 300; |
| 64 |
private static final float FALLBACK_HEIGHT = 150; |
| 65 |
|
| 66 |
private boolean isTopLevel; |
| 67 |
private AnimationPeriod animationPeriod; |
| 68 |
|
| 69 |
@Override |
| 70 |
public @NotNull String tagName() { |
| 71 |
return TAG; |
| 72 |
} |
| 73 |
|
| 74 |
public boolean isTopLevel() { |
| 75 |
return isTopLevel; |
| 76 |
} |
| 77 |
|
| 78 |
public @NotNull AnimationPeriod animationPeriod() { |
| 79 |
return animationPeriod; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void build(@NotNull AttributeNode attributeNode) { |
| 84 |
isTopLevel = attributeNode.element().parent() == null; |
| 85 |
super.build(attributeNode); |
| 86 |
animationPeriod = attributeNode.document().animationPeriod(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected @NotNull Point2D outerLocation(@NotNull MeasureContext context) { |
| 91 |
if (isTopLevel) return new Point(0, 0); |
| 92 |
return super.outerLocation(context); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public @NotNull Coordinate<LengthValue> transformOrigin() { |
| 97 |
if (isTopLevel) return TOP_LEVEL_TRANSFORM_ORIGIN; |
| 98 |
return super.transformOrigin(); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
protected @NotNull Overflow defaultOverflow() { |
| 103 |
return Overflow.Hidden; |
| 104 |
} |
| 105 |
|
| 106 |
public @NotNull FloatSize sizeForTopLevel(@Nullable ViewBox outerViewBox, float em, float ex) { |
| 107 |
|
| 108 |
FloatSize size = outerViewBox != null ? outerViewBox.size() : new FloatSize(100, 100); |
| 109 |
MeasureContext topLevelContext = MeasureContext.createInitial(size, em, ex, AnimationState.NO_ANIMATION); |
| 110 |
return new FloatSize( |
| 111 |
width.orElseIfUnspecified(viewBox != null ? viewBox.width : FALLBACK_WIDTH) |
| 112 |
.resolve(topLevelContext), |
| 113 |
height.orElseIfUnspecified(viewBox != null ? viewBox.height : FALLBACK_HEIGHT) |
| 114 |
.resolve(topLevelContext)); |
| 115 |
} |
| 116 |
} |