| •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.mesh; |
| 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 |
import org.jetbrains.annotations.Nullable; |
| 30 |
|
| 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.nodes.SVGNode; |
| 35 |
import com.github.weisj.jsvg.nodes.animation.Animate; |
| 36 |
import com.github.weisj.jsvg.nodes.animation.AnimateTransform; |
| 37 |
import com.github.weisj.jsvg.nodes.animation.Set; |
| 38 |
import com.github.weisj.jsvg.nodes.container.ContainerNode; |
| 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.NotImplemented; |
| 42 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 43 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 44 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 45 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 46 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 47 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 48 |
|
| 49 |
@ElementCategories(Category.Gradient) |
| 50 |
@PermittedContent( |
| 51 |
categories = Category.Descriptive, |
| 52 |
anyOf = {MeshRow.class, Animate.class, AnimateTransform.class, Set.class } |
| 53 |
) |
| 54 |
public final class MeshGradient extends ContainerNode implements SVGPaint { |
| 55 |
public static final String TAG = "meshgradient"; |
| 56 |
|
| 57 |
private Length x; |
| 58 |
private Length y; |
| 59 |
|
| 60 |
@SuppressWarnings("UnusedVariable") |
| 61 |
private @NotImplemented UnitType gradientUnits; |
| 62 |
|
| 63 |
@Override |
| 64 |
public @NotNull String tagName() { |
| 65 |
return TAG; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void build(@NotNull AttributeNode attributeNode) { |
| 70 |
super.build(attributeNode); |
| 71 |
x = attributeNode.getLength("x", PercentageDimension.WIDTH, 0); |
| 72 |
y = attributeNode.getLength("y", PercentageDimension.HEIGHT, 0); |
| 73 |
gradientUnits = attributeNode.getEnum("gradientUnits", UnitType.ObjectBoundingBox); |
| 74 |
MeshBuilder.buildMesh(this, new Point2D.Float(x.raw(), y.raw())); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
public void renderMesh(@NotNull MeasureContext measure, @NotNull Output output) { |
| 81 |
Output meshOutput = output.createChild(); |
| 82 |
|
| 83 |
|
| 84 |
meshOutput.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); |
| 85 |
for (SVGNode child : children()) { |
| 86 |
MeshRow row = (MeshRow) child; |
| 87 |
for (SVGNode node : row.children()) { |
| 88 |
MeshPatch patch = (MeshPatch) node; |
| 89 |
patch.renderPath(meshOutput); |
| 90 |
} |
| 91 |
} |
| 92 |
meshOutput.dispose(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void fillShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape, |
| 97 |
@Nullable Rectangle2D bounds) { |
| 98 |
Output.SafeState safeState = output.safeState(); |
| 99 |
Rectangle2D b = bounds != null ? bounds : shape.getBounds2D(); |
| 100 |
output.applyClip(shape); |
| 101 |
output.translate(b.getX(), b.getY()); |
| 102 |
renderMesh(context.measureContext(), output); |
| 103 |
safeState.restore(); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void drawShape(@NotNull Output output, @NotNull RenderContext context, @NotNull Shape shape, |
| 108 |
@Nullable Rectangle2D bounds) { |
| 109 |
Output.SafeState safeState = output.safeState(); |
| 110 |
Rectangle2D b = bounds != null ? bounds : shape.getBounds2D(); |
| 111 |
output.applyClip(output.stroke().createStrokedShape(shape)); |
| 112 |
output.translate(b.getX(), b.getY()); |
| 113 |
renderMesh(context.measureContext(), output); |
| 114 |
safeState.restore(); |
| 115 |
} |
| 116 |
} |