Debian Codegraph @

src/com/github/weisj/jsvg/attributes/PaintOrder.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022-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.attributes;
23
24 import java.util.Arrays;
25
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import com.github.weisj.jsvg.parser.impl.AttributeNode;
30 import com.github.weisj.jsvg.parser.impl.AttributeParser;
31 import com.github.weisj.jsvg.parser.impl.SeparatorMode;
32 import com.github.weisj.jsvg.util.AttributeUtil;
33
34 public final class PaintOrder {
35
36 public static final PaintOrder NORMAL = new PaintOrder(Phase.FILL, Phase.STROKE, Phase.MARKERS);
37
38 public enum Phase {
39 FILL,
40 STROKE,
41 MARKERS
42 }
43
44 private final @NotNull Phase[] phases;
45
46 public PaintOrder(@NotNull Phase... phases) {
47 this.phases = phases;
48 }
49
50 public @NotNull Phase[] phases() {
51 return phases;
52 }
53
54 public static @Nullable PaintOrder parse(@NotNull AttributeNode attributeNode) {
55 @Nullable String value = attributeNode.getValue("paint-order");
56 @NotNull AttributeParser parser = attributeNode.parser();
57
58 if (value == null) return null;
59 if ("inherit".equals(value)) return null;
60 if ("none".equals(value)) return NORMAL;
61 if ("normal".equals(value)) return NORMAL;
62
63 String[] rawPhases = parser.parseStringList(value, SeparatorMode.COMMA_AND_WHITESPACE);
64 Phase[] phases = new Phase[3];
65 int length = Math.min(phases.length, rawPhases.length);
66 int phasesIndex = 0;
67 int rawPhasesIndex = 0;
68 while (phasesIndex < length && rawPhasesIndex < length) {
69 Phase phase = parser.parseEnum(rawPhases[rawPhasesIndex], Phase.class);
70 if (phase != null && !AttributeUtil.arrayContains(phases, phase)) {
71 phases[phasesIndex] = phase;
72 phasesIndex++;
73 }
74 rawPhasesIndex++;
75 }
76 while (phasesIndex < 3) {
77 // Fill up with normal order
78 phases[phasesIndex] = findNextInNormalOrder(phases, phasesIndex);
79 phasesIndex++;
80 }
81 return new PaintOrder(phases);
82 }
83
84 private static @NotNull Phase findNextInNormalOrder(@NotNull Phase[] phases, int maxIndex) {
85 for (Phase phase : NORMAL.phases()) {
86 boolean found = false;
87 for (int i = 0; i < maxIndex; i++) {
88 if (phases[i] == phase) {
89 found = true;
90 break;
91 }
92 }
93 if (!found) return phase;
94 }
95 throw new IllegalStateException();
96 }
97
98 @Override
99 public boolean equals(Object o) {
100 if (this == o) return true;
101 if (o == null || getClass() != o.getClass()) return false;
102 PaintOrder that = (PaintOrder) o;
103 return Arrays.equals(phases, that.phases);
104 }
105
106 @Override
107 public int hashCode() {
108 return Arrays.hashCode(phases);
109 }
110
111 @Override
112 public String toString() {
113 return "PaintOrder" + Arrays.toString(phases);
114 }
115 }