Debian Codegraph @

src/com/github/weisj/jsvg/renderer/impl/context/StrokeContext.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // Dash length is negative. Bail
66 return new Length[0];
67 }
68 if (!length.isZero()) return pattern;
69 }
70 // All values are zero. Bail.
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 }