Detect RTL text in events and adjust the text rendering (#6994)
* Add `TextDirection.detect` to check the overall LTR or RTL direction of texts * Calculate if the contents of events have a different direction than the layout to reverse the layout direction if needed. LTR content, RTL text -> mismatched direction, reverse. RTL content, LTR text -> also mismatched, reversed too. * Measure text in `measureLastTextLine` based on its direction, not the layout's, to avoid some issues with incorrectly calculated free space. * Tweak the paddings in `TimelineEventTimestampView` a bit if the layout and text directions don't match * Update screenshots --------- Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
committed by
GitHub
parent
46460e1c27
commit
6068aeaf01
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.ui.utils.text
|
||||
|
||||
import androidx.compose.ui.text.style.TextDirection
|
||||
|
||||
/**
|
||||
* Detect if a text is RTL or not, based on its content.
|
||||
*/
|
||||
fun TextDirection.Companion.detect(text: String): TextDirection {
|
||||
return if (text.any(::isRtlChar)) TextDirection.Rtl else TextDirection.Ltr
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if a character is an RTL character or not, based on its Unicode directionality.
|
||||
*/
|
||||
fun isRtlChar(char: Char): Boolean = when (Character.getDirectionality(char)) {
|
||||
Character.DIRECTIONALITY_RIGHT_TO_LEFT,
|
||||
Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC,
|
||||
Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING,
|
||||
Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE -> true
|
||||
else -> false
|
||||
}
|
||||
Reference in New Issue
Block a user