| •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 |
import java.awt.geom.Rectangle2D; |
| 27 |
|
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.attributes.MaskType; |
| 31 |
import com.github.weisj.jsvg.attributes.UnitType; |
| 32 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 33 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 34 |
import com.github.weisj.jsvg.geometry.size.Unit; |
| 35 |
import com.github.weisj.jsvg.geometry.util.GeometryUtil; |
| 36 |
import com.github.weisj.jsvg.nodes.container.CommonRenderableContainerNode; |
| 37 |
import com.github.weisj.jsvg.nodes.filter.Filter; |
| 38 |
import com.github.weisj.jsvg.nodes.prototype.Instantiator; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 40 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 41 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 42 |
import com.github.weisj.jsvg.nodes.text.Text; |
| 43 |
import com.github.weisj.jsvg.paint.impl.MaskedPaint; |
| 44 |
import com.github.weisj.jsvg.parser.PaintParser; |
| 45 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 46 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 47 |
import com.github.weisj.jsvg.renderer.impl.ElementBounds; |
| 48 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 49 |
import com.github.weisj.jsvg.util.BlittableImage; |
| 50 |
import com.github.weisj.jsvg.util.CachedSurfaceSupplier; |
| 51 |
|
| 52 |
@ElementCategories(Category.Container) |
| 53 |
@PermittedContent( |
| 54 |
categories = {Category.Animation, Category.Descriptive, Category.Shape, Category.Structural, Category.Gradient}, |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
anyOf = {Anchor.class, ClipPath.class, Filter.class, Image.class, Marker.class, Mask.class, Pattern.class, |
| 60 |
Style.class, Text.class, View.class} |
| 61 |
) |
| 62 |
public final class Mask extends CommonRenderableContainerNode implements Instantiator { |
| 63 |
private static final boolean DEBUG = false; |
| 64 |
public static final String TAG = "mask"; |
| 65 |
|
| 66 |
private CachedSurfaceSupplier surfaceSupplier; |
| 67 |
private Length x; |
| 68 |
private Length y; |
| 69 |
private Length width; |
| 70 |
private Length height; |
| 71 |
|
| 72 |
private UnitType maskContentUnits; |
| 73 |
private UnitType maskUnits; |
| 74 |
private MaskType maskType; |
| 75 |
|
| 76 |
@Override |
| 77 |
public @NotNull String tagName() { |
| 78 |
return TAG; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void build(@NotNull AttributeNode attributeNode) { |
| 83 |
super.build(attributeNode); |
| 84 |
maskContentUnits = attributeNode.getEnum("maskContentUnits", UnitType.UserSpaceOnUse); |
| 85 |
maskUnits = attributeNode.getEnum("maskUnits", UnitType.ObjectBoundingBox); |
| 86 |
maskType = attributeNode.getEnum("mask-type", MaskType.Luminance); |
| 87 |
surfaceSupplier = new CachedSurfaceSupplier(maskType.bufferSurface()); |
| 88 |
|
| 89 |
x = attributeNode.getLength("x", PercentageDimension.WIDTH, Unit.PERCENTAGE_WIDTH.valueOf(-10)) |
| 90 |
.coercePercentageToCorrectUnit(maskUnits, PercentageDimension.WIDTH); |
| 91 |
y = attributeNode.getLength("y", PercentageDimension.HEIGHT, Unit.PERCENTAGE_HEIGHT.valueOf(-10)) |
| 92 |
.coercePercentageToCorrectUnit(maskUnits, PercentageDimension.HEIGHT); |
| 93 |
width = attributeNode.getLength("width", PercentageDimension.WIDTH, Unit.PERCENTAGE_WIDTH.valueOf(120)) |
| 94 |
.coercePercentageToCorrectUnit(maskUnits, PercentageDimension.WIDTH); |
| 95 |
height = attributeNode.getLength("height", PercentageDimension.HEIGHT, Unit.PERCENTAGE_HEIGHT.valueOf(120)) |
| 96 |
.coercePercentageToCorrectUnit(maskUnits, PercentageDimension.HEIGHT); |
| 97 |
} |
| 98 |
|
| 99 |
public @NotNull Paint createMaskPaint(@NotNull Output output, @NotNull RenderContext context, |
| 100 |
@NotNull ElementBounds elementBounds) { |
| 101 |
Rectangle2D.Double maskBounds = maskUnits.computeViewBounds( |
| 102 |
context.measureContext(), elementBounds.boundingBox(), x, y, width, height); |
| 103 |
|
| 104 |
boolean useCache = surfaceSupplier.useCache(output, context); |
| 105 |
BlittableImage blitImage = BlittableImage.create( |
| 106 |
surfaceSupplier.surfaceSupplier(useCache), context, output.clipBounds(), |
| 107 |
maskBounds.createIntersection(elementBounds.geometryBox()), elementBounds.boundingBox(), |
| 108 |
maskContentUnits); |
| 109 |
|
| 110 |
if (blitImage == null) return PaintParser.DEFAULT_COLOR; |
| 111 |
|
| 112 |
if (maskType == MaskType.Luminance) { |
| 113 |
blitImage.clearBackground(Color.BLACK); |
| 114 |
} else { |
| 115 |
Graphics2D g = blitImage.image().createGraphics(); |
| 116 |
g.setComposite(AlphaComposite.Clear); |
| 117 |
g.fillRect(0, 0, blitImage.image().getWidth(), blitImage.image().getHeight()); |
| 118 |
g.dispose(); |
| 119 |
} |
| 120 |
blitImage.renderNode(output, this, this); |
| 121 |
|
| 122 |
if (DEBUG) { |
| 123 |
blitImage.debug(output); |
| 124 |
} |
| 125 |
|
| 126 |
Point2D offset = GeometryUtil.getLocation(blitImage.imageBoundsInDeviceSpace()); |
| 127 |
return new MaskedPaint(PaintParser.DEFAULT_COLOR, blitImage.image().getRaster(), offset, |
| 128 |
surfaceSupplier.resourceCleaner(output, useCache), maskType); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public boolean requiresInstantiation() { |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean canInstantiate(@NotNull SVGNode node) { |
| 138 |
return node == this; |
| 139 |
} |
| 140 |
} |