Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object UnicodeStyleEngine {
private class StyleSet(val styles: List<StyleDefinition>) {
val byId: Map<String, StyleDefinition> = styles.associateBy { it.id }
val reverse: Map<Int, Int> = buildReverseMap(styles)
val combiningMarks: Set<Int> = styles.mapNotNull { style -> style.combiningMark.takeIf { it != 0 } }.toSet()
}

private val builtinStyles: List<StyleDefinition> by lazy { initializeStyles() }
Expand Down Expand Up @@ -57,7 +58,7 @@ object UnicodeStyleEngine {
}

/**
* Checks whether a codepoint is a styled Unicode character or combining mark.
* Checks whether a codepoint is a styled Unicode character or any combining mark, so it belongs to a word.
*/
fun isStyledCharacter(codePoint: Int): Boolean {
return currentSet().reverse.containsKey(codePoint) || isCombiningMark(codePoint)
Expand Down Expand Up @@ -118,7 +119,7 @@ object UnicodeStyleEngine {
val len = styledText.length
while (i < len) {
val cp = styledText.codePointAt(i)
if (isCombiningMark(cp)) {
if (isStyleMark(cp)) {
i += Character.charCount(cp)
continue
}
Expand All @@ -129,6 +130,9 @@ object UnicodeStyleEngine {
return sb.toString()
}

// only the marks a style adds: others are accents of the text itself, for example decomposed Vietnamese or Yoruba
private fun isStyleMark(cp: Int): Boolean = cp in currentSet().combiningMarks

private fun isCombiningMark(cp: Int): Boolean {
return cp in 0x0300..0x036F || cp in 0x1AB0..0x1AFF || cp in 0x1DC0..0x1DFF || cp in 0x20D0..0x20FF
}
Expand Down Expand Up @@ -752,7 +756,8 @@ object UnicodeStyleEngine {
testChars.append("+-=() .,?!'\"<>_")

for (style in styles) {
if (style.combiningMark != 0) continue
// upside-down can't be typed live and uses real letters (ɔ, ǝ, Ɛ ...), which must stay as they are
if (style.combiningMark != 0 || style.isReversed) continue
var i = 0
val len = testChars.length
while (i < len) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ class UnicodeStyleEngineTest {
assertFalse(UnicodeStyleEngine.isStyledCharacter('\''.code))
}

@Test
fun testToPlainTextKeepsRealLetters() {
// letters of real alphabets that the upside-down style also uses
val word = "ɐɔǝɟɥɯɹʇʌʍʎƆƎƐƖɾʞ"
assertEquals(word, UnicodeStyleEngine.toPlainText(word))
assertFalse(UnicodeStyleEngine.isStyledCharacter('ɔ'.code))
}

@Test
fun testToPlainTextKeepsAccentsButRemovesStyleMarks() {
// acute, dot below, hook above and tilde as separate combining characters
val accented = "e\u0301 o\u0323 a\u0309 n\u0303"
assertEquals(accented, UnicodeStyleEngine.toPlainText(accented))
assertTrue(UnicodeStyleEngine.isStyledCharacter(0x0301)) // still part of a word, for typing and backspace
for (id in UnicodeStyleEngine.getAllStyles().filter { it.combiningMark != 0 }.map { it.id }) {
val styled = UnicodeStyleEngine.transformText(accented, id)
assertNotEquals(accented, styled)
assertEquals(accented, UnicodeStyleEngine.toPlainText(styled))
}
}

@Test
fun testInvertedText() {
val input = "hello"
Expand Down
Loading