Make the TU passes

This commit is contained in:
Benoit Marty 2020-01-28 22:35:40 +01:00
parent 6d7d4993a6
commit b2338dfcd3
3 changed files with 52 additions and 18 deletions

View file

@ -113,3 +113,39 @@ fun containsOnlyEmojis(str: String?): Boolean {
return res
}
/**
* Same as split, but considering emojis
*/
fun CharSequence.splitEmoji(): List<CharSequence> {
val result = mutableListOf<CharSequence>()
var index = 0
while (index < length) {
val firstChar = get(index)
if (firstChar.toInt() == 0x200e) {
// Left to right mark. What should I do with it?
} else if (firstChar.toInt() in 0xD800..0xDBFF && index + 1 < length) {
// We have the start of a surrogate pair
val secondChar = get(index + 1)
if (secondChar.toInt() in 0xDC00..0xDFFF) {
// We have an emoji
result.add("$firstChar$secondChar")
index++
} else {
// Not sure what we have here...
result.add("$firstChar")
}
} else {
// Regular char
result.add("$firstChar")
}
index++
}
return result
}

View file

@ -16,6 +16,7 @@
package im.vector.riotx.features.home.room.detail.composer.rainbow
import im.vector.riotx.core.utils.splitEmoji
import javax.inject.Inject
import kotlin.math.abs
import kotlin.math.roundToInt
@ -30,9 +31,10 @@ class RainbowGenerator @Inject constructor() {
val frequency = 360f / text.length
return text
.splitEmoji()
.mapIndexed { idx, letter ->
// Do better than React-Sdk: Avoid adding font color for spaces
if (letter == ' ') {
if (letter == " ") {
"$letter"
} else {
val dashColor = hueToRGB(idx * frequency, 1.0f, 0.5f).toDashColor()

View file

@ -64,7 +64,7 @@ class RainbowGeneratorTest {
@Test
fun testEmoji1() {
assertEquals("""<font color="#ff0000">\uD83E\uDD1E</font>""", rainbowGenerator.generate("\uD83E\uDD1E")) // 🤞
assertEquals("""<font color="#ff0000">🤞</font>""", rainbowGenerator.generate("\uD83E\uDD1E")) // 🤞
}
@Test
@ -75,24 +75,20 @@ class RainbowGeneratorTest {
@Test
fun testEmojiMix() {
val expected = """
<font color="#ff0000">T</font>
<font color="#ff5500">h</font>
<font color="#ffaa00">i</font>
<font color="#ffff00">s</font>
<font color="#ff0000">H</font>
<font color="#ff6600">e</font>
<font color="#ffcc00">l</font>
<font color="#ccff00">l</font>
<font color="#66ff00">o</font>
<font color="#55ff00">i</font>
<font color="#00ff00">s</font>
<font color="#00ff66">🤞</font>
<font color="#00ffaa">a</font>
<font color="#00aaff">r</font>
<font color="#0055ff">a</font>
<font color="#0000ff">i</font>
<font color="#5500ff">n</font>
<font color="#aa00ff">b</font>
<font color="#ff00ff">o</font>
<font color="#ff00aa">w</font>
<font color="#ff0055">!</font>
<font color="#00ccff">w</font>
<font color="#0066ff">o</font>
<font color="#0000ff">r</font>
<font color="#6600ff">l</font>
<font color="#cc00ff">d</font>
<font color="#ff00cc">!</font>
"""
.trimIndent()
.replace("\n", "")