Skip to content

Commit

Permalink
Merge pull request #1 from cbruegg/support-annotated-strings
Browse files Browse the repository at this point in the history
Support annotated strings
  • Loading branch information
SalomonBrys authored Mar 9, 2024
2 parents 9aa6d06 + 49a6ec9 commit 194bba0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public fun String.withEmoji(): String {

@Composable
private fun WithNotoEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit,
createInlineTextContent: suspend (FoundEmoji) -> InlineTextContent?
) {
Expand All @@ -55,7 +55,10 @@ private fun WithNotoEmoji(
val annotatedString = buildAnnotatedString {
var start = 0
all.forEach { (found, inlineTextContent) ->
append(text.substring(start, found.start))
if (text is AnnotatedString)
append(text.subSequence(start, found.start))
else
append(text.substring(start, found.start))
val itc = inlineTextContent.value
if (itc != null) {
val inlineContentID = "emoji:${found.emoji}"
Expand All @@ -66,7 +69,10 @@ private fun WithNotoEmoji(
}
start = found.end
}
append(text.substring(start, text.length))
if (text is AnnotatedString)
append(text.subSequence(start, text.length))
else
append(text.substring(start, text.length))
}

content(annotatedString, inlineContent)
Expand All @@ -90,7 +96,7 @@ private suspend fun createNotoSvgInlineContent(emoji: Emoji, download: suspend (

@Composable
public fun WithNotoImageEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit
) {
val download = LocalEmojiDownloader.current
Expand Down Expand Up @@ -120,7 +126,7 @@ private suspend fun createNotoLottieInlineContent(emoji: Emoji, download: suspen

@Composable
public fun WithNotoAnimatedEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit
) {
val download = LocalEmojiDownloader.current
Expand All @@ -133,6 +139,6 @@ public fun WithNotoAnimatedEmoji(

@Composable
public expect fun WithPlatformEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.kodein.emoji.compose

import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.AnnotatedString
import kotlinx.cinterop.ExperimentalForeignApi
Expand Down Expand Up @@ -34,10 +35,11 @@ internal actual suspend fun platformDownloadBytes(url: String): ByteArray {

@Composable
public actual fun WithPlatformEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit
) {
content(AnnotatedString(text), emptyMap())
val annotatedString = remember(text) { AnnotatedString.Builder().append(text).toAnnotatedString() }
content(annotatedString, emptyMap())
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.kodein.emoji.compose

import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.AnnotatedString
import kotlinx.coroutines.Dispatchers
Expand All @@ -17,10 +18,11 @@ internal actual suspend fun platformDownloadBytes(url: String): ByteArray =

@Composable
public actual fun WithPlatformEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit
) {
content(AnnotatedString(text), emptyMap())
val annotatedString = remember(text) { AnnotatedString.Builder().append(text).toAnnotatedString() }
content(annotatedString, emptyMap())
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal actual suspend fun platformDownloadBytes(url: String): ByteArray {

@Composable
public actual fun WithPlatformEmoji(
text: String,
text: CharSequence,
content: @Composable (AnnotatedString, Map<String, InlineTextContent>) -> Unit
) {
WithNotoImageEmoji(text, content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal fun codePointCharLength(code: Int) =
if (isCodePointInOneChar(code)) 1 else 2


internal fun codePointAt(string: String, index: Int): Int {
internal fun codePointAt(string: CharSequence, index: Int): Int {
if (isCodePointInOneChar(string[index].code)) {
return string[index].code
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public data class FoundEmoji(
public val length: Int get() = end - start
}

private tailrec fun follow(string: String, index: Int, node: EmojiFinder.Node, start: Int): FoundEmoji? {
private tailrec fun follow(string: CharSequence, index: Int, node: EmojiFinder.Node, start: Int): FoundEmoji? {
if (index >= string.length) return node.emoji?.let { FoundEmoji(start, index, it) }
val branches = node.branches ?: return node.emoji?.let { FoundEmoji(start, index, it) }
val code = codePointAt(string, index)
Expand All @@ -87,7 +87,7 @@ private tailrec fun follow(string: String, index: Int, node: EmojiFinder.Node, s
/**
* Finds all emojis inside a String and returns their position and details.
*/
public fun EmojiFinder.findEmoji(str: String): Sequence<FoundEmoji> =
public fun EmojiFinder.findEmoji(str: CharSequence): Sequence<FoundEmoji> =
sequence {
var index = 0
while (index < str.length) {
Expand Down

0 comments on commit 194bba0

Please sign in to comment.