Debian Codegraph @

src/com/github/weisj/jsvg/nodes/SVG.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-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.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 * <altGlyphDef>, <color-profile>, <cursor>, <font>, <font-face>, <foreignObject>, <script>,
52 * <switch>
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 // Use a viewport of size 100x100 to interpret percentage values as raw pixels.
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 }