forked from gluonhq/rich-text-area
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitBuffer.java
More file actions
272 lines (249 loc) · 9.67 KB
/
UnitBuffer.java
File metadata and controls
272 lines (249 loc) · 9.67 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2023, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.richtextarea.model;
import com.gluonhq.emoji.Emoji;
import com.gluonhq.emoji.util.TextUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A UnitBuffer is a collection of units and related operations, like getting their internal
* or exportable text.
* A PieceTable uses two buffers of units, original and addition, but the UnitBuffer can be
* used elsewhere as a rich string.
* In its simplest implementation, this would be a String class.
* An example of more complex units is: "Emoji: \ud83d\ude00!", where the unit buffer will
* have three units: [TU{'Emoji: '}, EU{1F600}, TU{'!'}], and the internal text will be:
* "Emoji: \u2063!".
* Note that the length of the original text is 10, while the internal length, after
* replacing the emoji characters with the anchor is 9.
*/
public class UnitBuffer {
/**
* The block pattern is defined by
* \ufeff, a @ or a # symbol, any text (any combination of letters, numbers, punctuation and spaces), and a final \ufeff
* For example:
* {@code \ufeff@name foo33!\ufeff} has group 0: {@code @}, and group 1: {@code name foo33!}
*/
private static final Pattern BLOCK_PATTERN = Pattern.compile(
TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT + "([@#])([\\p{L}\\p{N}\\p{P}\\s]*)" + TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT,
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
final List<Unit> unitList;
volatile boolean dirty = true;
private String internalText = "";
public UnitBuffer() {
this(List.of());
}
public UnitBuffer(Unit unit) {
this(List.of(unit));
}
public UnitBuffer(Collection<Unit> units) {
unitList = new ArrayList<>(units);
}
/**
* Gets the exportable text of the unit buffer. It is useful for clipboard or
* serializing the document, but shouldn't be used internally to do Piece
* operations
* @return a string with the exportable text content
*/
public String getText() {
final StringBuilder sb = new StringBuilder();
unitList.forEach(unit -> sb.append(unit.getText()));
return sb.toString();
}
/**
* Gets the internal text of the unit buffer. For text units, matches the exportable text.
* For non-text units, this is used as an anchor point
* @return a string with the internal text representation
*/
public String getInternalText() {
if (!dirty) return internalText;
final StringBuilder sb = new StringBuilder();
unitList.forEach(unit -> sb.append(unit.getInternalText()));
this.internalText = sb.toString();
this.dirty = false;
return this.internalText;
}
/**
* Gets the internal length of the unit buffer. Useful for Piece operations
* @return an integer value of the internal number of positions that the unit spans
*/
public int length() {
return getUnitList().stream().mapToInt(Unit::length).sum();
}
/**
* Adds a new unit to the buffer
* @param unit the unit that is added
*/
public void append(Unit unit) {
unitList.add(unit);
this.dirty = true;
}
/**
* Adds a list of units to the buffer
* @param units a list of units to be added
*/
public void append(List<Unit> units) {
unitList.addAll(units);
this.dirty = true;
}
/**
* Inserts a Unit into the buffer after a given position within its
* internal length, splitting in two the unit found at that position,
* if needed
* @param unit the unit to insert into this buffer
* @param position the position within the buffer range
*/
public void insert(Unit unit, int position) {
if (unit == null) {
return;
}
if (position < 0 || position > length()) {
return;
}
int accum = 0;
List<Unit> buffer = new ArrayList<>();
for (Unit u : unitList) {
buffer.add(u);
if (u.isEmpty()) continue;
if (accum <= position && position <= accum + u.length()) {
if (u instanceof TextUnit) {
buffer.remove(u);
String substring0 = u.getText().substring(0, position - accum);
if (substring0.length() > 0) {
buffer.add(new TextUnit(substring0));
}
buffer.add(unit);
String substring1 = u.getText().substring(position - accum);
if (substring1.length() > 0) {
buffer.add(new TextUnit(substring1));
}
} else {
buffer.add(unit);
}
accum += unit.length();
}
accum += u.length();
}
unitList.clear();
unitList.addAll(buffer);
this.dirty = true;
}
/**
* Removes content from the buffer of a given range within its
* internal length, splitting in two the units found at the limits,
* if needed
* @param start the initial position of the range
* @param end the end position of the range
*/
public void remove(int start, int end) {
if (Math.min(start, end) < 0 || Math.max(start, end) > length()) {
return;
}
TextUnit unit = new TextUnit("\u2065");
insert(unit, Math.max(start, end));
insert(unit, Math.min(start, end));
unitList.subList(unitList.indexOf(unit), unitList.lastIndexOf(unit) + 1).clear();
this.dirty = true;
}
/**
* Gives the list of units of this buffer
* @return the list of units
*/
public List<Unit> getUnitList() {
return unitList;
}
/**
* Check if the buffer is empty or not
* @return true if the list of units is null or empty
*/
public boolean isEmpty() {
return unitList == null || unitList.isEmpty();
}
/**
* Finds the unit that contains a range of document coordinates.
* Convenience method to find the unit that a piece belongs to.
* @param start the initial value of the range
* @param end the end value of the range
* @return the unit that has this range or an empty TextUnit
*/
public Unit getUnitWithRange(int start, int end) {
int accum = 0;
for (Unit unit : unitList) {
if (unit.isEmpty()) continue;
if (accum <= start && end <= accum + unit.length()) {
return unit;
}
accum += unit.length();
}
return new TextUnit("");
}
@Override
public String toString() {
return "UnitBuffer{" + unitList + "}";
}
/**
* Utility method that parses an external text that might contain emoji unicode characters
* and returns a UnitBuffer
* @param text a string that might contain emoji unicode characters
* @return a UnitBuffer with a list of units.
*/
public static UnitBuffer convertTextToUnits(String text) {
List<Unit> units = new ArrayList<>();
TextUtils.convertToStringAndEmojiObjects(text).stream()
.map(o -> {
if (o instanceof Emoji) {
return List.of(new EmojiUnit((Emoji) o));
} else {
return createTextAndBlockUnits((String) o);
}
})
.forEach(units::addAll);
return new UnitBuffer(units);
}
private static List<Unit> createTextAndBlockUnits(String text) {
ArrayList<Unit> units = new ArrayList<>();
Matcher matcher = BLOCK_PATTERN.matcher(text);
int previousEnd = 0;
while (matcher.find()) {
int matchStart = matcher.start();
int matchEnd = matcher.end();
if (matchStart > previousEnd) {
units.add(new TextUnit(text.substring(previousEnd, matchStart)));
}
units.add(new BlockUnit(new Block(matcher.group(1) + matcher.group(2).trim())));
previousEnd = matchEnd;
}
if (previousEnd <= text.length() - 1) {
units.add(new TextUnit(text.substring(previousEnd)));
}
return units;
}
}