Debian Codegraph @

src/com/github/weisj/jsvg/nodes/container/BaseInnerViewContainer.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.container;
23
24 import java.awt.geom.AffineTransform;
25 import java.awt.geom.Point2D;
26
27 import org.jetbrains.annotations.MustBeInvokedByOverriders;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30
31 import com.github.weisj.jsvg.attributes.Overflow;
32 import com.github.weisj.jsvg.attributes.PreserveAspectRatio;
33 import com.github.weisj.jsvg.geometry.size.Length;
34 import com.github.weisj.jsvg.parser.impl.AttributeNode;
35 import com.github.weisj.jsvg.renderer.MeasureContext;
36 import com.github.weisj.jsvg.renderer.RenderContext;
37 import com.github.weisj.jsvg.renderer.impl.NodeRenderer;
38 import com.github.weisj.jsvg.renderer.output.Output;
39 import com.github.weisj.jsvg.view.FloatSize;
40 import com.github.weisj.jsvg.view.ViewBox;
41
42 public abstract class BaseInnerViewContainer extends CommonRenderableContainerNode {
43
44 protected ViewBox viewBox;
45 protected PreserveAspectRatio preserveAspectRatio;
46 private Overflow overflow;
47
48 protected abstract @NotNull Point2D outerLocation(@NotNull MeasureContext context);
49
50 protected abstract @Nullable Point2D anchorLocation(@NotNull MeasureContext context);
51
52 public abstract @NotNull FloatSize size(@NotNull RenderContext context);
53
54 protected abstract @NotNull Overflow defaultOverflow();
55
56 public @Nullable ViewBox viewBox(@NotNull RenderContext context) {
57 return viewBox != null ? viewBox : new ViewBox(size(context));
58 }
59
60 public @NotNull ViewBox staticViewBox(@NotNull FloatSize fallbackSize) {
61 return viewBox != null ? viewBox : new ViewBox(fallbackSize);
62 }
63
64 @Override
65 @MustBeInvokedByOverriders
66 public void build(@NotNull AttributeNode attributeNode) {
67 super.build(attributeNode);
68 viewBox = attributeNode.getViewBox();
69 preserveAspectRatio = PreserveAspectRatio.parse(
70 attributeNode.getValue("preserveAspectRatio"), attributeNode.parser());
71 overflow = attributeNode.getEnum("overflow", defaultOverflow());
72 }
73
74 public void renderWithEstablishedViewBox(@NotNull RenderContext context, @NotNull Output output) {
75 super.render(context, output);
76 }
77
78 @Override
79 public void render(@NotNull RenderContext context, @NotNull Output output) {
80 renderWithSize(size(context), viewBox(context), context, output);
81 }
82
83 public final void renderWithSize(@NotNull FloatSize useSiteSize, @Nullable ViewBox view,
84 @NotNull RenderContext context, @NotNull Output output) {
85 RenderContext innerContext = createInnerContextForViewBox(useSiteSize, view, context, output);
86 renderWithEstablishedViewBox(innerContext, output);
87 }
88
89 protected boolean inheritAttributes() {
90 return true;
91 }
92
93 private @NotNull RenderContext createInnerContext(@NotNull RenderContext context,
94 @NotNull ViewBox viewBox) {
95 return NodeRenderer.setupInnerViewRenderContext(viewBox, context, inheritAttributes());
96 }
97
98 private @NotNull ViewBox computeOuterViewBox(@NotNull RenderContext context, @NotNull FloatSize useSiteSize) {
99 MeasureContext measureContext = context.measureContext();
100 Point2D outerPos = outerLocation(measureContext);
101 ViewBox vb = new ViewBox(outerPos, useSiteSize);
102
103 if (Length.isUnspecified(vb.width) || Length.isUnspecified(vb.height)) {
104 FloatSize size = size(context);
105 if (Length.isUnspecified(vb.width)) vb.width = size.width;
106 if (Length.isUnspecified(vb.height)) vb.height = size.height;
107 }
108
109 return vb;
110 }
111
112 public final @NotNull RenderContext createInnerContextForViewBox(@NotNull FloatSize useSiteSize,
113 @Nullable ViewBox view, @NotNull RenderContext context, @NotNull Output output) {
114 ViewBox outerViewBox = computeOuterViewBox(context, useSiteSize);
115 ViewBox innerViewBox = view;
116
117 // innerViewBox == null should behave as if it were (0,0,width,height).
118 // If no viewBox is specified we can avoid the computation of the transform.
119 AffineTransform viewTransform = innerViewBox != null
120 ? preserveAspectRatio.computeViewportTransform(outerViewBox.size(), innerViewBox)
121 : null;
122
123 if (innerViewBox == null) {
124 innerViewBox = new ViewBox(outerViewBox.size());
125 }
126
127 RenderContext innerContext = createInnerContext(context, innerViewBox);
128 MeasureContext innerMeasure = innerContext.measureContext();
129 Point2D anchorPos = anchorLocation(innerMeasure);
130
131 // Clip the viewbox established at the use-site e.g. where an <svg> node is instantiated with <use>
132 if (overflow.establishesClip()) {
133 ViewBox clipViewBox = new ViewBox(outerViewBox);
134 if (anchorPos != null) {
135 Point2D clipAnchor = anchorPos;
136 if (viewTransform != null) {
137 clipAnchor = new Point2D.Double();
138 viewTransform.transform(anchorPos, clipAnchor);
139 }
140 clipViewBox.x += (float) clipAnchor.getX();
141 clipViewBox.y += (float) clipAnchor.getY();
142 }
143 output.applyClip(clipViewBox);
144 }
145
146 innerContext.translate(output, outerViewBox.location());
147 if (viewTransform != null) {
148 // This also applies the translation to the inner viewbox location.
149 innerContext.transform(output, viewTransform);
150 }
151
152 if (anchorPos != null) {
153 innerContext.translate(output, anchorPos);
154 }
155
156 return innerContext;
157 }
158 }