| •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 |
import java.util.Arrays; |
| 25 |
|
| 26 |
import org.jetbrains.annotations.Contract; |
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.attributes.Animatable; |
| 31 |
import com.github.weisj.jsvg.attributes.Inherited; |
| 32 |
import com.github.weisj.jsvg.attributes.stroke.LineCap; |
| 33 |
import com.github.weisj.jsvg.attributes.stroke.LineJoin; |
| 34 |
import com.github.weisj.jsvg.attributes.value.LengthValue; |
| 35 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 36 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 37 |
import com.github.weisj.jsvg.geometry.size.Unit; |
| 38 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 39 |
|
| 40 |
public final class StrokeContext { |
| 41 |
public final @Nullable LengthValue strokeWidth; |
| 42 |
public final @Nullable LineCap lineCap; |
| 43 |
public final @Nullable LineJoin lineJoin; |
| 44 |
public final float miterLimit; |
| 45 |
|
| 46 |
public final Length @Nullable [] dashPattern; |
| 47 |
public final @Nullable LengthValue dashOffset; |
| 48 |
|
| 49 |
public StrokeContext(@Nullable LengthValue strokeWidth, @Nullable LineCap lineCap, @Nullable LineJoin lineJoin, |
| 50 |
float miterLimit, Length @Nullable [] dashPattern, @Nullable LengthValue dashOffset) { |
| 51 |
this.strokeWidth = strokeWidth; |
| 52 |
this.lineCap = lineCap; |
| 53 |
this.lineJoin = lineJoin; |
| 54 |
this.miterLimit = miterLimit; |
| 55 |
this.dashPattern = validateDashPattern(dashPattern); |
| 56 |
this.dashOffset = dashOffset; |
| 57 |
} |
| 58 |
|
| 59 |
@Contract("null -> null") |
| 60 |
private static Length @Nullable [] validateDashPattern(Length @Nullable [] pattern) { |
| 61 |
if (pattern == null) return null; |
| 62 |
if (pattern.length == 0) return pattern; |
| 63 |
for (Length length : pattern) { |
| 64 |
if (length.raw() < 0) { |
| 65 |
|
| 66 |
return new Length[0]; |
| 67 |
} |
| 68 |
if (!length.isZero()) return pattern; |
| 69 |
} |
| 70 |
|
| 71 |
return new Length[0]; |
| 72 |
} |
| 73 |
|
| 74 |
public @NotNull StrokeContext derive(@Nullable StrokeContext context) { |
| 75 |
if (context == null) return this; |
| 76 |
if (context.isTrivial()) return this; |
| 77 |
return new StrokeContext( |
| 78 |
LengthValue.derive(strokeWidth, context.strokeWidth), |
| 79 |
context.lineCap != null ? context.lineCap : lineCap, |
| 80 |
context.lineJoin != null ? context.lineJoin : lineJoin, |
| 81 |
Length.isSpecified(context.miterLimit) ? context.miterLimit : miterLimit, |
| 82 |
context.dashPattern != null ? context.dashPattern : dashPattern, |
| 83 |
context.dashOffset != null ? context.dashOffset : dashOffset); |
| 84 |
} |
| 85 |
|
| 86 |
public boolean isTrivial() { |
| 87 |
return strokeWidth == null |
| 88 |
&& lineCap == null |
| 89 |
&& lineJoin == null |
| 90 |
&& Length.isUnspecified(miterLimit) |
| 91 |
&& dashPattern == null |
| 92 |
&& dashOffset == null; |
| 93 |
} |
| 94 |
|
| 95 |
public static @NotNull StrokeContext createDefault() { |
| 96 |
return new StrokeContext(Unit.RAW.valueOf(1), LineCap.Butt, LineJoin.Miter, 4f, new Length[0], Length.ZERO); |
| 97 |
} |
| 98 |
|
| 99 |
public static @NotNull StrokeContext parse(@NotNull AttributeNode attributeNode) { |
| 100 |
return new StrokeContext( |
| 101 |
attributeNode.getLength("stroke-width", PercentageDimension.LENGTH, Inherited.YES, Animatable.YES), |
| 102 |
attributeNode.getEnumNullable("stroke-linecap", LineCap.class), |
| 103 |
attributeNode.getEnumNullable("stroke-linejoin", LineJoin.class), |
| 104 |
attributeNode.getNonNegativeFloat("stroke-miterlimit", Length.UNSPECIFIED_RAW), |
| 105 |
attributeNode.getLengthList("stroke-dasharray", null, PercentageDimension.LENGTH), |
| 106 |
attributeNode.getLength("stroke-dashoffset", PercentageDimension.CUSTOM, |
| 107 |
Inherited.YES, Animatable.YES)); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public String toString() { |
| 112 |
return "StrokeContext{" + |
| 113 |
"strokeWidth=" + strokeWidth + |
| 114 |
", lineCap=" + lineCap + |
| 115 |
", lineJoin=" + lineJoin + |
| 116 |
", miterLimit=" + miterLimit + |
| 117 |
", dashPattern=" + Arrays.toString(dashPattern) + |
| 118 |
", dashOffset=" + dashOffset + |
| 119 |
'}'; |
| 120 |
} |
| 121 |
} |