| •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.parser.impl.AttributeNode; |
| 43 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 44 |
|
| 45 |
@ElementCategories(Category.Gradient) |
| 46 |
@PermittedContent( |
| 47 |
categories = Category.Descriptive, |
| 48 |
anyOf = {Stop.class, Animate.class, AnimateTransform.class, Set.class} |
| 49 |
) |
| 50 |
public final class LinearGradient extends AbstractGradient<LinearGradient> { |
| 51 |
public static final String TAG = "lineargradient"; |
| 52 |
|
| 53 |
private Length x1; |
| 54 |
private Length x2; |
| 55 |
private Length y1; |
| 56 |
private Length y2; |
| 57 |
|
| 58 |
@Override |
| 59 |
public @NotNull String tagName() { |
| 60 |
return TAG; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected void buildGradient(@NotNull AttributeNode attributeNode, @Nullable LinearGradient template) { |
| 65 |
x1 = attributeNode.getLength("x1", PercentageDimension.WIDTH, |
| 66 |
template != null ? template.x1 : Unit.PERCENTAGE_WIDTH.valueOf(0)); |
| 67 |
x2 = attributeNode.getLength("x2", PercentageDimension.WIDTH, |
| 68 |
template != null ? template.x2 : Unit.PERCENTAGE_WIDTH.valueOf(100)); |
| 69 |
y1 = attributeNode.getLength("y1", PercentageDimension.HEIGHT, |
| 70 |
template != null ? template.y1 : Unit.PERCENTAGE_HEIGHT.valueOf(0)); |
| 71 |
y2 = attributeNode.getLength("y2", PercentageDimension.HEIGHT, |
| 72 |
template != null ? template.y2 : Unit.PERCENTAGE_HEIGHT.valueOf(0)); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected @NotNull Paint gradientForBounds(@NotNull MeasureContext measure, @NotNull Rectangle2D bounds, |
| 77 |
Percentage[] gradOffsets, @NotNull Color[] gradColors) { |
| 78 |
Point2D.Float pt1 = new Point2D.Float(x1.resolve(measure), y1.resolve(measure)); |
| 79 |
Point2D.Float pt2 = new Point2D.Float(x2.resolve(measure), y2.resolve(measure)); |
| 80 |
if (pt1.equals(pt2)) return gradColors[0]; |
| 81 |
|
| 82 |
return new LinearGradientPaint(pt1, pt2, offsetsToFractions(gradOffsets), gradColors, |
| 83 |
spreadMethod.cycleMethod(), |
| 84 |
MultipleGradientPaint.ColorSpaceType.SRGB, computeViewTransform(measure, bounds)); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public String toString() { |
| 89 |
return "LinearGradient{" + |
| 90 |
"spreadMethod=" + spreadMethod + |
| 91 |
", gradientTransform=" + gradientTransform + |
| 92 |
", x1=" + x1 + |
| 93 |
", x2=" + x2 + |
| 94 |
", y1=" + y1 + |
| 95 |
", y2=" + y2 + |
| 96 |
", colors=" + Arrays.toString(colors()) + |
| 97 |
", offsets=" + Arrays.toString(offsets()) + |
| 98 |
'}'; |
| 99 |
} |
| 100 |
} |