Created
March 29, 2023 02:41
-
-
Save yamin8000/08def5bd36d64debf214d873744b49ad to your computer and use it in GitHub Desktop.
Jetpack Compose Letter Spaced Persian Text
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
@Composable | |
fun LetterSpacedPersianText( | |
text: String, | |
modifier: Modifier = Modifier, | |
color: Color = Color.Unspecified, | |
fontSize: TextUnit = 14.sp, | |
fontStyle: FontStyle? = null, | |
fontWeight: FontWeight? = null, | |
fontFamily: FontFamily? = null, | |
letterSpacing: TextUnit = TextUnit.Unspecified, | |
textDecoration: TextDecoration? = null, | |
textAlign: TextAlign? = null, | |
lineHeight: TextUnit = TextUnit.Unspecified, | |
overflow: TextOverflow = TextOverflow.Clip, | |
softWrap: Boolean = true, | |
maxLines: Int = Int.MAX_VALUE, | |
onTextLayout: (TextLayoutResult) -> Unit = {}, | |
style: TextStyle = LocalTextStyle.current | |
) { | |
val isPersian = remember { Regex("[ء-ی]") } | |
val keshides = letterSpacing.value.toInt() | |
val keshidesText = buildAnnotatedString { repeat(keshides) { append('ـ') } } | |
val spacesText = buildAnnotatedString { | |
repeat(keshides) { | |
withStyle(style = SpanStyle(fontSize = fontSize.div(50 * letterSpacing.value))) { | |
append(' ') | |
} | |
} | |
} | |
if (isPersian.containsMatchIn(text)) { | |
if (text.length != 1) { | |
val totalCursive = "ئبپتثجچحخسشصضطظعغفقکگلمنهی" | |
val finalCursive = "أؤدذرزژو" | |
val newText = buildAnnotatedString { | |
var i = 0 | |
while (i < text.length) { | |
val current = text[i] | |
append(current) | |
val next = text.getOrNull(i + 1) | |
if (next != null) { | |
if (totalCursive.contains(current) && totalCursive.contains(next) || | |
totalCursive.contains(current) && finalCursive.contains(next) | |
) { | |
append(keshidesText) | |
} | |
if (current !in totalCursive && current !in finalCursive && next !in totalCursive && next !in finalCursive) { | |
append(spacesText) | |
} | |
} | |
i++ | |
} | |
} | |
Text( | |
newText, | |
modifier, | |
color, | |
fontSize, | |
fontStyle, | |
fontWeight, | |
fontFamily, | |
letterSpacing, | |
textDecoration, | |
textAlign, | |
lineHeight, | |
overflow, | |
softWrap, | |
maxLines, | |
onTextLayout, | |
style | |
) | |
} else { | |
Text( | |
text = text, | |
modifier = modifier, | |
color = color, | |
fontSize = fontSize, | |
fontStyle = fontStyle, | |
fontWeight = fontWeight, | |
fontFamily = fontFamily, | |
letterSpacing = letterSpacing, | |
textDecoration = textDecoration, | |
textAlign = textAlign, | |
lineHeight = lineHeight, | |
overflow = overflow, | |
softWrap = softWrap, | |
maxLines = maxLines, | |
onTextLayout = onTextLayout, | |
style = style | |
) | |
} | |
} else { | |
Text( | |
text = text, | |
modifier = modifier, | |
color = color, | |
fontSize = fontSize, | |
fontStyle = fontStyle, | |
fontWeight = fontWeight, | |
fontFamily = fontFamily, | |
letterSpacing = letterSpacing, | |
textDecoration = textDecoration, | |
textAlign = textAlign, | |
lineHeight = lineHeight, | |
overflow = overflow, | |
softWrap = softWrap, | |
maxLines = maxLines, | |
onTextLayout = onTextLayout, | |
style = style | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment