forked from QuantumBadger/RedReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetterSSB.java
223 lines (185 loc) · 5.51 KB
/
BetterSSB.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
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader 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.
*
* RedReader 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 RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package org.quantumbadger.redreader.common;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import androidx.annotation.NonNull;
import java.util.HashSet;
import java.util.Observable;
public class BetterSSB extends Observable {
private final SpannableStringBuilder sb;
public static final int BOLD = 1;
public static final int ITALIC = 1 << 1;
public static final int UNDERLINE = 1 << 2;
public static final int STRIKETHROUGH = 1 << 3;
public static final int FOREGROUND_COLOR = 1 << 4;
public static final int BACKGROUND_COLOR = 1 << 5;
public static final int SIZE = 1 << 6;
public static final int SUPERSCRIPT = 1 << 7;
public static final char NBSP = '\u00A0';
public BetterSSB() {
this.sb = new SpannableStringBuilder();
}
public void append(final String str, final int flags) {
append(str, flags, 0, 0, 1f);
}
public void append(final String str, final int flags, final String url) {
append(str, flags, 0, 0, 1f, url);
}
public void append(
@NonNull final String str,
final int flags,
final int foregroundCol,
final int backgroundCol,
final float scale) {
append(str, flags, foregroundCol, backgroundCol, scale, null);
}
public void append(
@NonNull final String str,
final int flags,
final int foregroundCol,
final int backgroundCol,
final float scale,
final String url) {
final int strStart = sb.length();
sb.append(str);
final int strEnd = sb.length();
if((flags & BOLD) != 0) {
sb.setSpan(
new StyleSpan(Typeface.BOLD),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & ITALIC) != 0) {
sb.setSpan(
new StyleSpan(Typeface.ITALIC),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & UNDERLINE) != 0) {
sb.setSpan(
new UnderlineSpan(),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & STRIKETHROUGH) != 0) {
sb.setSpan(
new StrikethroughSpan(),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & FOREGROUND_COLOR) != 0) {
sb.setSpan(
new ForegroundColorSpan(foregroundCol),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & BACKGROUND_COLOR) != 0) {
sb.setSpan(
new BackgroundColorSpan(backgroundCol),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & SIZE) != 0) {
sb.setSpan(
new RelativeSizeSpan(scale),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if((flags & SUPERSCRIPT) != 0) {
sb.setSpan(
new SuperscriptSpan(),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
if(url != null) {
sb.setSpan(
new URLSpan(url),
strStart,
strEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
public void linkify() {
final String asText = sb.toString();
final HashSet<UriString> links = LinkHandler.computeAllLinks(asText);
for(final UriString uri : links) {
final String link = uri.value;
int index = -1;
while(index < asText.length()
&& (index = asText.indexOf(link, index + 1)) >= 0) {
if(sb.getSpans(index, index + link.length(), URLSpan.class).length < 1) {
sb.setSpan(
new URLSpan(link),
index,
index + link.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
}
}
public void append(final CharSequence text) {
this.sb.append(text);
this.setChanged();
this.notifyObservers(this.sb);
}
public void replace(final int start, final int end, final CharSequence text) {
this.sb.replace(start, end, text);
this.setChanged();
this.notifyObservers(this.sb);
}
public void replace(
@NonNull final CharSequence textToBeReplaced,
@NonNull final Object replacement) {
final int textStartIndex = TextUtils.indexOf(this.sb, textToBeReplaced);
this.sb.setSpan(replacement,
textStartIndex,
textStartIndex + textToBeReplaced.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
this.setChanged();
this.notifyObservers(this.sb);
}
public boolean isEmpty() {
return this.sb.length() == 0;
}
public void setSpan(final Object what, final int start, final int end, final int flag) {
this.sb.setSpan(what, start, end, flag);
this.setChanged();
this.notifyObservers(this.sb);
}
public SpannableStringBuilder get() {
return sb;
}
}