forked from KyoriPowered/adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentCompaction.java
249 lines (203 loc) · 9.16 KB
/
ComponentCompaction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
final class ComponentCompaction {
private static final TextDecoration[] DECORATIONS = TextDecoration.values();
private ComponentCompaction() {
}
static Component compact(final @NotNull Component self, final @Nullable Style parentStyle) {
final List<Component> children = self.children();
Component optimized = self.children(Collections.emptyList());
if (parentStyle != null) {
optimized = optimized.style(simplifyStyle(self.style(), parentStyle));
}
final int childrenSize = children.size();
if (childrenSize == 0) {
// no children, style can be further simplified if self is blank
if (isBlank(optimized)) {
optimized = optimized.style(simplifyStyleForBlank(optimized.style()));
}
// leaf nodes do not need to be further optimized - there is no point
return optimized;
}
// if there is only one child, check if self a useless empty component
if (childrenSize == 1 && optimized instanceof TextComponent) {
final TextComponent textComponent = (TextComponent) optimized;
if (textComponent.content().isEmpty()) {
final Component child = children.get(0);
// merge the updated/parent style into the child before we return
return child.style(child.style().merge(optimized.style(), Style.Merge.Strategy.IF_ABSENT_ON_TARGET)).compact();
}
}
// propagate the parent style context to children
// by merging the parent's style into this component
Style childParentStyle = optimized.style();
if (parentStyle != null) {
childParentStyle = childParentStyle.merge(parentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
}
// optimize all children
final List<Component> childrenToAppend = new ArrayList<>(children.size());
for (int i = 0; i < children.size(); ++i) {
Component child = children.get(i);
// compact child recursively
child = compact(child, childParentStyle);
// ignore useless empty children (regardless of its style)
if (child.children().isEmpty() && child instanceof TextComponent) {
final TextComponent textComponent = (TextComponent) child;
if (textComponent.content().isEmpty()) {
continue;
}
}
childrenToAppend.add(child);
}
// try to merge children into this parent component
if (optimized instanceof TextComponent) {
while (!childrenToAppend.isEmpty()) {
final Component child = childrenToAppend.get(0);
final Style childStyle = child.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
if (child instanceof TextComponent && Objects.equals(childStyle, childParentStyle)) {
// merge child components into the parent if they are a text component with the same effective style
// in context of their parent style
optimized = joinText((TextComponent) optimized, (TextComponent) child);
childrenToAppend.remove(0);
// if the merged child had any children, retain them
childrenToAppend.addAll(0, child.children());
} else {
// this child can't be merged into the parent, so all children from now on must remain children
break;
}
}
}
// try to concatenate any further children with their neighbor
// until no further joining is possible
for (int i = 0; i + 1 < childrenToAppend.size();) {
final Component child = childrenToAppend.get(i);
final Component neighbor = childrenToAppend.get(i + 1);
if (child.children().isEmpty() && child instanceof TextComponent && neighbor instanceof TextComponent) {
// calculate the children's styles in context of their parent style
final Style childStyle = child.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
final Style neighborStyle = neighbor.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
// check if styles are equivalent
if (childStyle.equals(neighborStyle)) {
final Component combined = joinText((TextComponent) child, (TextComponent) neighbor);
// replace the child and its neighbor with the single, combined component
childrenToAppend.set(i, combined);
childrenToAppend.remove(i + 1);
// don't increment the index -
// we want to try and optimize this combined component even further
continue;
}
}
i++;
}
// no children, style can be further simplified if self is blank
if (childrenToAppend.isEmpty() && isBlank(optimized)) {
optimized = optimized.style(simplifyStyleForBlank(optimized.style()));
}
return optimized.children(childrenToAppend);
}
/**
* Simplify the provided style to remove any information that is redundant.
*
* @param style style to simplify
* @param parentStyle parent to compare against
* @return a new, simplified style
*/
private static @NotNull Style simplifyStyle(final @NotNull Style style, final @NotNull Style parentStyle) {
if (style.isEmpty()) {
// the target style is empty, so there is nothing to simplify
return style;
}
final Style.Builder builder = style.toBuilder();
if (Objects.equals(style.font(), parentStyle.font())) {
builder.font(null);
}
if (Objects.equals(style.color(), parentStyle.color())) {
builder.color(null);
}
for (final TextDecoration decoration : DECORATIONS) {
if (style.decoration(decoration) == parentStyle.decoration(decoration)) {
builder.decoration(decoration, TextDecoration.State.NOT_SET);
}
}
if (Objects.equals(style.clickEvent(), parentStyle.clickEvent())) {
builder.clickEvent(null);
}
if (Objects.equals(style.hoverEvent(), parentStyle.hoverEvent())) {
builder.hoverEvent(null);
}
if (Objects.equals(style.insertion(), parentStyle.insertion())) {
builder.insertion(null);
}
return builder.build();
}
/**
* Checks whether the Component is blank (a TextComponent containing only space characters).
*
* @param component the component to check
* @return true if the provided component is blank, false otherwise
*/
private static boolean isBlank(final Component component) {
if (component instanceof TextComponent) {
final TextComponent textComponent = (TextComponent) component;
final String content = textComponent.content();
for (int i = 0; i < content.length(); i++) {
final char c = content.charAt(i);
if (c != ' ') return false;
}
return true;
}
return false;
}
/**
* Simplify the provided style to remove any information that is redundant,
* given that the content is blank.
*
* @param style style to simplify
* @return a new, simplified style
*/
private static @NotNull Style simplifyStyleForBlank(final @NotNull Style style) {
final Style.Builder builder = style.toBuilder();
// TextColor doesn't affect spaces
builder.color(null);
// ITALIC/OBFUSCATED don't affect spaces (in modern versions), as these styles only affect glyph rendering
builder.decoration(TextDecoration.ITALIC, TextDecoration.State.NOT_SET);
builder.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.NOT_SET);
// UNDERLINE/STRIKETHROUGH affects spaces because the line renders on top
// BOLD affects spaces because it increments the character advance by 1
// font affects spaces in 1.19+ (since 22w11a), due to the font glyph provider for spaces
return builder.build();
}
private static TextComponent joinText(final TextComponent one, final TextComponent two) {
return TextComponentImpl.create(two.children(), one.style(), one.content() + two.content());
}
}