| •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.*; |
| 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({}) |
| 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 |
|
| 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 |
} |