When an emoji is used as the 'initial' for an avatar, use the whole emoji (#4277)
* When an emoji is used as the 'initial' for an avatar, use the whole emoji Use `BreakIterator.getCharacterInstance()` for a simpler solution.
This commit is contained in:
parent
beffba11b6
commit
5e1bdbd5d9
3 changed files with 68 additions and 10 deletions
|
|
@ -8,6 +8,8 @@
|
|||
package io.element.android.libraries.designsystem.components.avatar
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import io.element.android.libraries.core.data.tryOrNull
|
||||
import java.text.BreakIterator
|
||||
|
||||
@Immutable
|
||||
data class AvatarData(
|
||||
|
|
@ -27,24 +29,36 @@ data class AvatarData(
|
|||
startIndex++
|
||||
}
|
||||
|
||||
var length = 1
|
||||
var first = dn[startIndex]
|
||||
var next = dn[startIndex]
|
||||
|
||||
// LEFT-TO-RIGHT MARK
|
||||
if (dn.length >= 2 && 0x200e == first.code) {
|
||||
if (dn.length >= 2 && 0x200e == next.code) {
|
||||
startIndex++
|
||||
first = dn[startIndex]
|
||||
next = dn[startIndex]
|
||||
}
|
||||
|
||||
// check if it’s the start of a surrogate pair
|
||||
if (first.code in 0xD800..0xDBFF && dn.length > startIndex + 1) {
|
||||
val second = dn[startIndex + 1]
|
||||
if (second.code in 0xDC00..0xDFFF) {
|
||||
length++
|
||||
while (next.isWhitespace()) {
|
||||
if (dn.length > startIndex + 1) {
|
||||
startIndex++
|
||||
next = dn[startIndex]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
dn.substring(startIndex, startIndex + length)
|
||||
val fullCharacterIterator = BreakIterator.getCharacterInstance()
|
||||
fullCharacterIterator.setText(dn)
|
||||
val glyphBoundary = tryOrNull { fullCharacterIterator.following(startIndex) }
|
||||
?.takeIf { it in startIndex..dn.length }
|
||||
|
||||
when {
|
||||
// Use the found boundary
|
||||
glyphBoundary != null -> dn.substring(startIndex, glyphBoundary)
|
||||
// If no boundary was found, default to the next char if possible
|
||||
startIndex + 1 < dn.length -> dn.substring(startIndex, startIndex + 1)
|
||||
// Return a fallback character otherwise
|
||||
else -> "#"
|
||||
}
|
||||
}
|
||||
.uppercase()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue