| •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.renderer.impl.context; |
| 23 |
|
| 24 |
|
| 25 |
import java.awt.*; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.animation.value.AnimatedColor; |
| 31 |
import com.github.weisj.jsvg.attributes.Animatable; |
| 32 |
import com.github.weisj.jsvg.attributes.FillRule; |
| 33 |
import com.github.weisj.jsvg.attributes.Inherited; |
| 34 |
import com.github.weisj.jsvg.attributes.PaintOrder; |
| 35 |
import com.github.weisj.jsvg.attributes.value.PercentageValue; |
| 36 |
import com.github.weisj.jsvg.geometry.size.Percentage; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.Mutator; |
| 38 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 39 |
import com.github.weisj.jsvg.paint.impl.AwtSVGPaint; |
| 40 |
import com.github.weisj.jsvg.paint.impl.PredefinedPaints; |
| 41 |
import com.github.weisj.jsvg.paint.impl.RGBColor; |
| 42 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 43 |
|
| 44 |
public final class PaintContext implements Mutator<PaintContext> { |
| 45 |
|
| 46 |
public final @Nullable SVGPaint color; |
| 47 |
public final @Nullable SVGPaint fillPaint; |
| 48 |
public final @Nullable SVGPaint strokePaint; |
| 49 |
|
| 50 |
public final @NotNull PercentageValue opacity; |
| 51 |
public final @Nullable PercentageValue fillOpacity; |
| 52 |
public final @Nullable PercentageValue strokeOpacity; |
| 53 |
|
| 54 |
public final @Nullable PaintOrder paintOrder; |
| 55 |
|
| 56 |
public final @Nullable StrokeContext strokeContext; |
| 57 |
public final @Nullable FillRule fillRule; |
| 58 |
|
| 59 |
public PaintContext(@Nullable SVGPaint color, @Nullable SVGPaint fillPaint, @Nullable PercentageValue fillOpacity, |
| 60 |
@Nullable SVGPaint strokePaint, @Nullable PercentageValue strokeOpacity, @NotNull PercentageValue opacity, |
| 61 |
@Nullable PaintOrder paintOrder, |
| 62 |
@Nullable StrokeContext strokeContext, @Nullable FillRule fillRule) { |
| 63 |
this.color = color; |
| 64 |
this.fillPaint = fillPaint; |
| 65 |
this.strokePaint = strokePaint; |
| 66 |
this.fillOpacity = fillOpacity; |
| 67 |
this.strokeOpacity = strokeOpacity; |
| 68 |
this.opacity = opacity; |
| 69 |
this.paintOrder = paintOrder; |
| 70 |
|
| 71 |
this.strokeContext = strokeContext == null || strokeContext.isTrivial() ? null : strokeContext; |
| 72 |
this.fillRule = fillRule; |
| 73 |
} |
| 74 |
|
| 75 |
public static @NotNull PaintContext createDefault(@NotNull SVGPaint currentColor) { |
| 76 |
return new PaintContext( |
| 77 |
currentColor, |
| 78 |
PredefinedPaints.DEFAULT_PAINT, Percentage.ONE, |
| 79 |
PredefinedPaints.NONE, Percentage.ONE, Percentage.ONE, |
| 80 |
PaintOrder.NORMAL, |
| 81 |
StrokeContext.createDefault(), |
| 82 |
FillRule.Nonzero); |
| 83 |
} |
| 84 |
|
| 85 |
public static @NotNull PaintContext parse(@NotNull AttributeNode attributeNode) { |
| 86 |
return new PaintContext( |
| 87 |
parseColorAttribute(attributeNode), |
| 88 |
attributeNode.getPaint("fill", Inherited.YES, Animatable.YES), |
| 89 |
attributeNode.getPercentage("fill-opacity", Inherited.YES, Animatable.YES), |
| 90 |
attributeNode.getPaint("stroke", Inherited.YES, Animatable.YES), |
| 91 |
attributeNode.getPercentage("stroke-opacity", Inherited.YES, Animatable.YES), |
| 92 |
attributeNode.getPercentage("opacity", Percentage.ONE, Inherited.NO, Animatable.YES), |
| 93 |
PaintOrder.parse(attributeNode), |
| 94 |
StrokeContext.parse(attributeNode), |
| 95 |
FillRule.parse(attributeNode)); |
| 96 |
} |
| 97 |
|
| 98 |
private static @Nullable SVGPaint parseColorAttribute(@NotNull AttributeNode attributeNode) { |
| 99 |
Color c = attributeNode.getColor("color", null); |
| 100 |
if (c == null) return null; |
| 101 |
AnimatedColor animatedColor = attributeNode.getAnimatedColor("color", new RGBColor(c)); |
| 102 |
if (animatedColor != null) return animatedColor; |
| 103 |
return new AwtSVGPaint(c); |
| 104 |
} |
| 105 |
|
| 106 |
public @NotNull PaintContext derive(@NotNull PaintContext context) { |
| 107 |
return new PaintContext( |
| 108 |
SVGPaint.derive(color, context.color), |
| 109 |
SVGPaint.derive(fillPaint, context.fillPaint), |
| 110 |
context.fillOpacity != null ? context.fillOpacity : fillOpacity, |
| 111 |
SVGPaint.derive(strokePaint, context.strokePaint), |
| 112 |
context.strokeOpacity != null ? context.strokeOpacity : strokeOpacity, |
| 113 |
opacity.multiply(context.opacity), |
| 114 |
context.paintOrder != null ? context.paintOrder : paintOrder, |
| 115 |
strokeContext != null |
| 116 |
? strokeContext.derive(context.strokeContext) |
| 117 |
: context.strokeContext, |
| 118 |
context.fillRule != null && context.fillRule != FillRule.Inherit |
| 119 |
? context.fillRule |
| 120 |
: this.fillRule); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public @NotNull PaintContext mutate(@NotNull PaintContext element) { |
| 125 |
return element.derive(this); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public String toString() { |
| 130 |
return "PaintContext{" + |
| 131 |
"color=" + color + |
| 132 |
", fillPaint=" + fillPaint + |
| 133 |
", strokePaint=" + strokePaint + |
| 134 |
", opacity=" + opacity + |
| 135 |
", fillOpacity=" + fillOpacity + |
| 136 |
", strokeOpacity=" + strokeOpacity + |
| 137 |
", strokeContext=" + strokeContext + |
| 138 |
", paintOrder=" + paintOrder + |
| 139 |
", fillRule=" + fillRule + |
| 140 |
'}'; |
| 141 |
} |
| 142 |
} |