-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
IconResolver.java
134 lines (113 loc) · 5.25 KB
/
IconResolver.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
package com.beardedhen.androidbootstrap;
import android.content.Context;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.beardedhen.androidbootstrap.font.IconSet;
import com.beardedhen.androidbootstrap.font.MaterialIcons;
import com.beardedhen.androidbootstrap.font.Typicon;
import static com.beardedhen.androidbootstrap.TypefaceProvider.getRegisteredIconSets;
import static com.beardedhen.androidbootstrap.TypefaceProvider.retrieveRegisteredIconSet;
/**
* Resolves markdown strings using FA codes and produces BootstrapText instances.
*/
class IconResolver {
private static final String REGEX_FONT_AWESOME = "(fa_|fa-)[a-z_0-9]+";
private static final String REGEX_TYPICONS = "(ty_|ty-)[a-z_0-9]+";
private static final String REGEX_MATERIAL_ICONS = "(md_)[a-z_0-9]+";
/**
* Resolves markdown to produce a BootstrapText instance. e.g. "{fa_android}" would be replaced
* with the appropriate FontAwesome character and a SpannableString producec.
*
* @param context the current context
* @param markdown the markdown string
* @param editMode - whether the view requesting the icon set is displayed in the preview editor
* @return a constructed BootstrapText
*/
static BootstrapText resolveMarkdown(Context context, String markdown, boolean editMode) {
if (markdown == null) {
return null;
}
else { // detect {fa_*} and split into spannable, ignore escaped chars
BootstrapText.Builder builder = new BootstrapText.Builder(context, editMode);
int lastAddedIndex = 0;
int startIndex = -1;
int endIndex = -1;
for (int i = 0; i < markdown.length(); i++) {
char c = markdown.charAt(i);
if (c == '\\') { // escape sequence, ignore next char
i += 2;
continue;
}
if (c == '{') {
startIndex = i;
}
else if (c == '}') {
endIndex = i;
}
if (startIndex != -1 && endIndex != -1) { // recognised markdown string
if (startIndex >= 0 && endIndex < markdown.length()) {
String iconCode = markdown.substring(startIndex + 1, endIndex).replaceAll("\\-", "_");
builder.addText(markdown.substring(lastAddedIndex, startIndex));
if (iconCode.matches(REGEX_FONT_AWESOME)) { // text is FontAwesome code
if (editMode) {
builder.addText("?");
}
else {
builder.addIcon(iconCode, retrieveRegisteredIconSet(FontAwesome.FONT_PATH, false));
}
}
else if (iconCode.matches(REGEX_TYPICONS)) {
if (editMode) {
builder.addText("?");
}
else {
builder.addIcon(iconCode, retrieveRegisteredIconSet(Typicon.FONT_PATH, false));
}
}
else if(iconCode.matches(REGEX_MATERIAL_ICONS)){
if (editMode) {
builder.addText("?");
}
else {
builder.addIcon(iconCode, retrieveRegisteredIconSet(MaterialIcons.FONT_PATH, false));
}
}
else {
if (editMode) {
builder.addText("?");
}
else {
builder.addIcon(iconCode, resolveIconSet(iconCode));
}
}
lastAddedIndex = endIndex + 1;
}
startIndex = -1;
endIndex = -1;
}
}
return builder.addText(markdown.substring(lastAddedIndex, markdown.length())).build();
}
}
/**
* Searches for the unicode character value for the Font Icon Code. This method searches all
* active FontIcons in the application.
*
* @param iconCode the font icon code
* @return the unicode character matching the icon, or null if none matches
*/
private static IconSet resolveIconSet(String iconCode) {
CharSequence unicode;
for (IconSet set : getRegisteredIconSets()) {
if (set.fontPath().equals(FontAwesome.FONT_PATH) || set.fontPath().equals(Typicon.FONT_PATH) || set.fontPath().equals(MaterialIcons.FONT_PATH)) {
continue; // already checked previously, ignore
}
unicode = set.unicodeForKey(iconCode);
if (unicode != null) {
return set;
}
}
String message = String.format("Could not find FontIcon value for '%s', " +
"please ensure that it is mapped to a valid font", iconCode);
throw new IllegalArgumentException(message);
}
}