| •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 |
import java.util.Arrays; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
import org.jetbrains.annotations.Nullable; |
| 31 |
|
| 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.Percentage; |
| 35 |
import com.github.weisj.jsvg.geometry.size.Unit; |
| 36 |
import com.github.weisj.jsvg.nodes.animation.Animate; |
| 37 |
import com.github.weisj.jsvg.nodes.animation.AnimateTransform; |
| 38 |
import com.github.weisj.jsvg.nodes.animation.Set; |
| 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.paint.impl.jdk.SVGRadialGradientPaint; |
| 43 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 44 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 45 |
|
| 46 |
@ElementCategories(Category.Gradient) |
| 47 |
@PermittedContent( |
| 48 |
categories = Category.Descriptive, |
| 49 |
anyOf = {Stop.class, Animate.class, AnimateTransform.class, Set.class,} |
| 50 |
) |
| 51 |
public final class RadialGradient extends AbstractGradient<RadialGradient> { |
| 52 |
public static final String TAG = "radialgradient"; |
| 53 |
|
| 54 |
private Length cx; |
| 55 |
private Length cy; |
| 56 |
private Length r; |
| 57 |
private Length fr; |
| 58 |
private Length fx; |
| 59 |
private Length fy; |
| 60 |
|
| 61 |
@Override |
| 62 |
public @NotNull String tagName() { |
| 63 |
return TAG; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
protected void buildGradient(@NotNull AttributeNode attributeNode, @Nullable RadialGradient template) { |
| 68 |
cx = attributeNode.getLength("cx", PercentageDimension.WIDTH, |
| 69 |
template != null ? template.cx : Unit.PERCENTAGE_WIDTH.valueOf(50)); |
| 70 |
cy = attributeNode.getLength("cy", PercentageDimension.HEIGHT, |
| 71 |
template != null ? template.cy : Unit.PERCENTAGE_HEIGHT.valueOf(50)); |
| 72 |
r = attributeNode.getLength("r", PercentageDimension.LENGTH, |
| 73 |
template != null ? template.r : Unit.PERCENTAGE_LENGTH.valueOf(50)); |
| 74 |
fr = attributeNode.getLength("fr", PercentageDimension.LENGTH, |
| 75 |
template != null ? template.fr : Unit.PERCENTAGE_LENGTH.valueOf(0)); |
| 76 |
fx = attributeNode.getLength("fx", PercentageDimension.WIDTH, |
| 77 |
template != null ? template.fx : cx); |
| 78 |
fy = attributeNode.getLength("fy", PercentageDimension.HEIGHT, |
| 79 |
template != null ? template.fy : cy); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected @NotNull Paint gradientForBounds(@NotNull MeasureContext measure, @NotNull Rectangle2D bounds, |
| 84 |
Percentage[] gradOffsets, @NotNull Color[] gradColors) { |
| 85 |
assert gradColors.length > 0; |
| 86 |
Point2D.Float center = new Point2D.Float(cx.resolve(measure), cy.resolve(measure)); |
| 87 |
Point2D.Float focusCenter = new Point2D.Float(fx.resolve(measure), fy.resolve(measure)); |
| 88 |
|
| 89 |
float radius = r.resolve(measure); |
| 90 |
float focusRadius = fr.resolve(measure); |
| 91 |
|
| 92 |
if (radius <= 0) { |
| 93 |
return gradColors[gradColors.length - 1]; |
| 94 |
} |
| 95 |
|
| 96 |
if (focusRadius == 0) { |
| 97 |
|
| 98 |
return new RadialGradientPaint(center, radius, focusCenter, |
| 99 |
offsetsToFractions(gradOffsets), gradColors, spreadMethod.cycleMethod(), |
| 100 |
MultipleGradientPaint.ColorSpaceType.SRGB, computeViewTransform(measure, bounds)); |
| 101 |
} |
| 102 |
|
| 103 |
return new SVGRadialGradientPaint(center, radius, focusCenter, focusRadius, |
| 104 |
offsetsToFractions(gradOffsets), gradColors, spreadMethod.cycleMethod(), |
| 105 |
MultipleGradientPaint.ColorSpaceType.SRGB, computeViewTransform(measure, bounds)); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public String toString() { |
| 110 |
return "RadialGradient{" + |
| 111 |
"spreadMethod=" + spreadMethod + |
| 112 |
", gradientTransform=" + gradientTransform + |
| 113 |
", cx=" + cx + |
| 114 |
", cy=" + cy + |
| 115 |
", r=" + r + |
| 116 |
", fr=" + fr + |
| 117 |
", fx=" + fx + |
| 118 |
", fy=" + fy + |
| 119 |
", colors=" + Arrays.toString(colors()) + |
| 120 |
", offsets=" + Arrays.toString(offsets()) + |
| 121 |
'}'; |
| 122 |
} |
| 123 |
} |