forked from TweetWallFX/TweetwallFX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TweetWallFX#1288 Emojify the tweet stream
- Loading branch information
1 parent
f51b458
commit 97696c9
Showing
9 changed files
with
405 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2022 TweetWallFX | ||
* | ||
* 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. | ||
*/ | ||
|
||
dependencies { | ||
api project(':tweetwallfx-cache') | ||
implementation 'org.apache.logging.log4j:log4j-api:2.19.0' | ||
implementation 'com.vdurmont:emoji-java:5.1.1' | ||
} |
46 changes: 46 additions & 0 deletions
46
emoji/src/main/java/org/tweetwallfx/emoji/EmojiImageCache.java
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 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2022 TweetWallFX | ||
* | ||
* 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 org.tweetwallfx.emoji; | ||
|
||
import org.tweetwallfx.cache.URLContent; | ||
import org.tweetwallfx.cache.URLContentCacheBase; | ||
|
||
/** | ||
* Cache used to provide the avatar image of a User. | ||
*/ | ||
public final class EmojiImageCache extends URLContentCacheBase { | ||
|
||
/** | ||
* Cache instance. | ||
*/ | ||
public static final EmojiImageCache INSTANCE = new EmojiImageCache(); | ||
|
||
private EmojiImageCache() { | ||
super("emojiImage"); | ||
} | ||
|
||
public URLContent get(String hex) { | ||
return EmojiImageCache.INSTANCE.getCachedOrLoad("https://twemoji.maxcdn.com/v/latest/72x72/"+ hex + ".png"); | ||
} | ||
} |
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,81 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2022 TweetWallFX | ||
* | ||
* 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 org.tweetwallfx.emoji; | ||
|
||
import com.vdurmont.emoji.EmojiParser; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Emojify { | ||
|
||
@SuppressWarnings({"NarrowCalculation","StringSplitter"}) | ||
private static String convert(String unicodeStr) { | ||
if (unicodeStr.isEmpty()) return unicodeStr; | ||
List<String> parts = new ArrayList<String>(); | ||
int c = 0; | ||
int p = 0; | ||
for (int i=0; i<unicodeStr.length(); i++) { | ||
c = unicodeStr.charAt(i); | ||
if (p > 0) { | ||
parts.add(Integer.toString(0x10000 + ((p - 0xD800) << 10) + (c - 0xDC00), 16)); | ||
p = 0; | ||
} else if (0xD800 <= c && c <= 0xDBFF) { | ||
p = c; | ||
} else { | ||
parts.add(Integer.toString(c,16)); | ||
p=0; | ||
} | ||
} | ||
String dashedLogic = parts.stream().collect(Collectors.joining("-")); | ||
return dashedLogic; | ||
} | ||
|
||
|
||
public static List<Object> tokenizeStringToTextAndEmoji(final String message) { | ||
var emojiCandidates = new ArrayList<EmojiParser.UnicodeCandidate>(); | ||
EmojiParser.parseFromUnicode(message, new EmojiParser.EmojiTransformer() { | ||
@Override | ||
public String transform(EmojiParser.UnicodeCandidate unicodeCandidate) { | ||
emojiCandidates.add(unicodeCandidate); | ||
return ""; | ||
} | ||
}); | ||
var objects = new ArrayList<Object>(); | ||
int startIndex = 0; | ||
for (com.vdurmont.emoji.EmojiParser.UnicodeCandidate unicodeCandidate : emojiCandidates) { | ||
String text = message.substring(startIndex, unicodeCandidate.getEmojiStartIndex()); | ||
if (!text.isEmpty()) { | ||
objects.add(text); | ||
} | ||
startIndex = unicodeCandidate.getFitzpatrickEndIndex(); | ||
String hex = convert(unicodeCandidate.getEmoji().getUnicode()); | ||
objects.add(new Twemoji(hex)); | ||
} | ||
objects.add(message.substring(startIndex, message.length())); | ||
return objects; | ||
} | ||
} | ||
|
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,29 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2022 TweetWallFX | ||
* | ||
* 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 org.tweetwallfx.emoji; | ||
|
||
public record Twemoji(String hex) { | ||
|
||
} |
Oops, something went wrong.