-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,13 @@ | ||
package to.itsme.itsmyconfig.font; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.TextReplacementConfig; | ||
import net.kyori.adventure.text.minimessage.tag.TagPattern; | ||
import to.itsme.itsmyconfig.util.Strings; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
public interface Font { | ||
|
||
public enum Font { | ||
SMALL_CAPS( | ||
"smallcaps", | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÑÓÚñóú", | ||
"ᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢɴᴏᴜɴᴏᴜ" | ||
), | ||
UPSIDE_DOWN( | ||
"upsidedown", | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | ||
"∀qƆpƎℲפHIſʞ˥WNOԀQɹS┴∩ΛMX⅄Zɐqɔpǝɟƃɥᴉɾʞlɯuodbɹsʇnʌʍxʎz" | ||
); | ||
|
||
private final Map<Character, Character> characterMap; | ||
private final TextReplacementConfig config; | ||
private final @TagPattern String name; | ||
|
||
Font( | ||
@TagPattern final String name, | ||
final String original, | ||
final String replacements | ||
) { | ||
this.name = name; | ||
this.characterMap = createMapping(original, replacements); | ||
this.config = createConfig(); | ||
} | ||
|
||
public @TagPattern String getName() { | ||
return name; | ||
} | ||
|
||
private TextReplacementConfig createConfig() { | ||
final TextReplacementConfig.Builder config = TextReplacementConfig.builder(); | ||
config.match(Strings.LETTERS_PATTERN).replacement((matchResult, builder) -> { | ||
final String text = this.apply(matchResult.group()); | ||
return Component.text().content(text); | ||
}); | ||
return config.build(); | ||
} | ||
|
||
public String apply(final String text) { | ||
final byte[] bytes = this.englishify(text).getBytes(StandardCharsets.UTF_8); | ||
final StringBuilder builder = new StringBuilder(); | ||
for (byte messageByte : bytes) { | ||
final char originalChar = (char) messageByte; | ||
final char styledChar = characterMap.getOrDefault(originalChar, originalChar); | ||
builder.append(styledChar); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
public String englishify(final String text) { | ||
return text | ||
.replace("À", "A") | ||
.replace("Â", "A") | ||
.replace("à", "a") | ||
.replace("â", "a") | ||
.replace("É", "E") | ||
.replace("È", "E") | ||
.replace("Ê", "E") | ||
.replace("é", "e") | ||
.replace("è", "e") | ||
.replace("ê", "e") | ||
.replace("Î", "I") | ||
.replace("î", "i") | ||
.replace("Ô", "O") | ||
.replace("ô", "o") | ||
.replace("Û", "U") | ||
.replace("û", "u") | ||
.replace("Ç", "C") | ||
.replace("ç", "c"); | ||
} | ||
|
||
public Component apply(final Component component) { | ||
return component.replaceText(config); | ||
} | ||
|
||
private static Map<Character, Character> createMapping(final String original, final String replacements) { | ||
if (original.length() != replacements.length()) { | ||
throw new IllegalArgumentException("Original and replacement texts must be of the same length."); | ||
} | ||
|
||
final Map<Character, Character> mapping = new HashMap<>(); | ||
for (int i = 0; i < original.length(); i++) { | ||
mapping.put(original.charAt(i), replacements.charAt(i)); | ||
} | ||
return mapping; | ||
} | ||
@TagPattern String getName(); | ||
@NotNull String apply(final @NotNull String text); | ||
@NotNull Component apply(final @NotNull Component component); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package to.itsme.itsmyconfig.font; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.TextReplacementConfig; | ||
import net.kyori.adventure.text.minimessage.tag.TagPattern; | ||
import org.jetbrains.annotations.NotNull; | ||
import to.itsme.itsmyconfig.util.Strings; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public abstract class FontImpl implements Font { | ||
|
||
private static final Map<String, Font> FONTS = new HashMap<>(); | ||
|
||
private final @TagPattern String name; | ||
private final TextReplacementConfig config = this.createConfig(); | ||
|
||
public FontImpl(final @TagPattern String name) { | ||
this.name = name; | ||
} | ||
|
||
public @TagPattern String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public @NotNull Component apply(final @NotNull Component component) { | ||
return component.replaceText(config); | ||
} | ||
|
||
private TextReplacementConfig createConfig() { | ||
final TextReplacementConfig.Builder config = TextReplacementConfig.builder(); | ||
config.match(Strings.LETTERS_PATTERN).replacement((matchResult, builder) -> { | ||
final String text = this.apply(matchResult.group()); | ||
return Component.text().content(text); | ||
}); | ||
return config.build(); | ||
} | ||
|
||
public static Collection<Font> values() { | ||
return FONTS.values(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package to.itsme.itsmyconfig.font; | ||
|
||
import net.kyori.adventure.text.minimessage.tag.TagPattern; | ||
import org.jetbrains.annotations.NotNull; | ||
import to.itsme.itsmyconfig.util.Strings; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MappedFont extends FontImpl { | ||
|
||
public static final MappedFont SMALL_CAPS = new MappedFont( | ||
"smallcaps", | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | ||
"ᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘꞯʀsᴛᴜᴠᴡxʏᴢᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘꞯʀsᴛᴜᴠᴡxʏᴢ" | ||
); | ||
|
||
public static final MappedFont UPSIDE_DOWN = new MappedFont( | ||
"upsidedown", | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | ||
"∀qƆpƎℲפHIſʞ˥WNOԀQɹS┴∩ΛMX⅄Zɐqɔpǝɟƃɥᴉɾʞlɯuodbɹsʇnʌʍxʎz" | ||
); | ||
|
||
private final Map<Character, Character> characterMap; | ||
|
||
MappedFont( | ||
@TagPattern final String name, | ||
final String original, | ||
final String replacements | ||
) { | ||
super(name); | ||
this.characterMap = createMapping(original, replacements); | ||
} | ||
|
||
@Override | ||
public @NotNull String apply(final @NotNull String text) { | ||
final byte[] bytes = Strings.englishify(text).getBytes(StandardCharsets.UTF_8); | ||
final StringBuilder builder = new StringBuilder(); | ||
for (final byte messageByte : bytes) { | ||
final char originalChar = (char) messageByte; | ||
final char styledChar = characterMap.getOrDefault(originalChar, originalChar); | ||
builder.append(styledChar); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
private static Map<Character, Character> createMapping(final String original, final String replacements) { | ||
if (original.length() != replacements.length()) { | ||
throw new IllegalArgumentException("Original and replacement texts must be of the same length."); | ||
} | ||
|
||
final Map<Character, Character> mapping = new HashMap<>(); | ||
for (int i = 0; i < original.length(); i++) { | ||
mapping.put(original.charAt(i), replacements.charAt(i)); | ||
} | ||
return mapping; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters