Debian Codegraph @

src/com/github/weisj/jsvg/nodes/ClipPath.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.*;
26
27 import org.jetbrains.annotations.NotNull;
28
29 import com.github.weisj.jsvg.attributes.MaskType;
30 import com.github.weisj.jsvg.attributes.UnitType;
31 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
32 import com.github.weisj.jsvg.nodes.container.ContainerNode;
33 import com.github.weisj.jsvg.nodes.prototype.ShapedContainer;
34 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
35 import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories;
36 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
37 import com.github.weisj.jsvg.nodes.text.Text;
38 import com.github.weisj.jsvg.paint.impl.MaskedPaint;
39 import com.github.weisj.jsvg.parser.PaintParser;
40 import com.github.weisj.jsvg.parser.impl.AttributeNode;
41 import com.github.weisj.jsvg.renderer.RenderContext;
42 import com.github.weisj.jsvg.renderer.impl.ElementBounds;
43 import com.github.weisj.jsvg.renderer.output.Output;
44 import com.github.weisj.jsvg.util.BlittableImage;
45 import com.github.weisj.jsvg.util.CachedSurfaceSupplier;
46 import com.github.weisj.jsvg.util.ImageUtil;
47 import com.github.weisj.jsvg.util.ShapeUtil;
48
49 @ElementCategories({/* None */})
50 @PermittedContent(
51 categories = {Category.Animation, Category.Descriptive, Category.Shape},
52 anyOf = {Use.class, Text.class}
53 )
54 public final class ClipPath extends ContainerNode implements ShapedContainer<SVGNode> {
55 private static final boolean DEBUG = false;
56 public static final String TAG = "clippath";
57
58 private final CachedSurfaceSupplier surfaceSupplier =
59 new CachedSurfaceSupplier(ImageUtil::createLuminosityBuffer);
60 private boolean isValid;
61
62 private UnitType clipPathUnits;
63
64 @Override
65 public @NotNull String tagName() {
66 return TAG;
67 }
68
69 public boolean isValid() {
70 return isValid;
71 }
72
73 @Override
74 public void build(@NotNull AttributeNode attributeNode) {
75 super.build(attributeNode);
76 isValid = checkIsValid();
77 clipPathUnits = attributeNode.getEnum("clipPathUnits", UnitType.UserSpaceOnUse);
78 }
79
80 private boolean checkIsValid() {
81 for (SVGNode child : children()) {
82 if (!(child instanceof Use)) continue;
83 SVGNode referenced = ((Use) child).referencedNode();
84 if (referenced != null && !isAcceptableType(referenced)) {
85 return false;
86 }
87 }
88 return true;
89 }
90
91 public @NotNull Shape clipShape(@NotNull RenderContext context, @NotNull ElementBounds elementBounds,
92 boolean useSoftClip) {
93 // Todo: Handle bounding-box stuff as well (i.e. combined stroke etc.)
94 Shape shape = ShapedContainer.super.elementShape(context, Box.BoundingBox);
95 if (!useSoftClip && clipPathUnits == UnitType.ObjectBoundingBox) {
96 shape = ShapeUtil.transformShape(shape, clipPathUnits.viewTransform(elementBounds.boundingBox()));
97 }
98 Area areaShape = new Area(shape);
99 if (areaShape.isRectangular()) {
100 return areaShape.getBounds2D();
101 }
102 return areaShape;
103 }
104
105 public @NotNull Paint createPaintForSoftClipping(@NotNull Output output, @NotNull RenderContext context,
106 @NotNull ElementBounds elementBounds, @NotNull Shape clipShape) {
107 Rectangle2D transformedClipBounds = GeometryUtil.containingBoundsAfterTransform(
108 clipPathUnits.viewTransform(elementBounds.boundingBox()), clipShape.getBounds2D());
109
110 boolean useCache = surfaceSupplier.useCache(output, context);
111 BlittableImage blitImage = BlittableImage.create(
112 surfaceSupplier.surfaceSupplier(useCache), context, output.clipBounds(),
113 transformedClipBounds.createIntersection(elementBounds.geometryBox()),
114 elementBounds.boundingBox(), clipPathUnits);
115
116 if (blitImage == null) return PaintParser.DEFAULT_COLOR;
117
118 blitImage.clearBackground(Color.BLACK);
119 blitImage.render(output, g -> {
120 g.setColor(Color.WHITE);
121 g.fill(clipShape);
122 });
123
124 if (DEBUG) {
125 blitImage.debug(output);
126 }
127
128 Point2D offset = GeometryUtil.getLocation(blitImage.imageBoundsInDeviceSpace());
129 return new MaskedPaint(PaintParser.DEFAULT_COLOR, blitImage.image().getRaster(), offset,
130 surfaceSupplier.resourceCleaner(output, useCache), MaskType.Luminance);
131 }
132
133 public void applyClip(@NotNull Output output, @NotNull RenderContext context,
134 @NotNull ElementBounds elementBounds) {
135 if (output.isSoftClippingEnabled()) {
136 Rectangle2D bounds = elementBounds.geometryBox();
137 if (!bounds.isEmpty()) {
138 output.setPaint(() -> {
139 Shape childClipShape = clipShape(context, elementBounds, true);
140 return createPaintForSoftClipping(output, context, elementBounds, childClipShape);
141 });
142 }
143 } else {
144 Shape childClipShape = clipShape(context, elementBounds, false);
145 if (DEBUG) {
146 output.debugPaint(g -> {
147 g.setClip(null);
148 g.setPaint(Color.MAGENTA);
149 g.draw(childClipShape);
150 });
151 }
152 output.applyClip(childClipShape);
153 }
154 }
155 }