From 8b873f794f089ca07e99c50fdd4fc3107483a5f0 Mon Sep 17 00:00:00 2001 From: Sin-Kang Date: Thu, 19 Nov 2026 23:10:20 +0900 Subject: [PATCH] android: keepWords joins Korean words only (0.12.1) keepWords put a word joiner between every two characters of every word. In Hindi and Thai that separated vowel signs from their consonants (drawn on dotted circles); in Japanese and Chinese it left a line no break point. Only a word that contains Hangul is joined now, between whole characters. --- android/CHANGELOG.ko.md | 10 ++++ android/CHANGELOG.md | 11 ++++ android/README.ko.md | 4 +- android/README.md | 4 +- .../dds/compose/theme/DdsTypography.kt | 56 ++++++++++++++----- .../kr/devslab/dds/compose/theme/BrandTest.kt | 28 ++++++++++ android/gradle.properties | 2 +- 7 files changed, 95 insertions(+), 20 deletions(-) diff --git a/android/CHANGELOG.ko.md b/android/CHANGELOG.ko.md index eddaf76d..5d3c09f2 100644 --- a/android/CHANGELOG.ko.md +++ b/android/CHANGELOG.ko.md @@ -5,6 +5,16 @@ `kr.devslab:dds-android-tokens`, `kr.devslab:dds-compose`, 그리고 0.7.0부터 `kr.devslab:dds-compose-testing`은 한 버전을 쓰고, 웹 패키지와는 버전이 따로다(D-048). 릴리스는 `android-v<버전>` 태그로 낸다. +## 0.12.1 — 2026-11-19 + +**고침** + +- **`keepWords()`가 한국어 단어만 묶는다.** 지금까지는 글자 종류와 상관없이 모든 단어의 모든 글자 사이에 결합 문자를 + 넣었다. 힌디어(와 태국어)에서는 모음 기호가 자음과 떨어져 점선 동그라미 위에 그려졌고, 띄어쓰기가 없는 일본어· + 중국어에서는 줄을 끊을 자리가 없어져 긴 문구가 넘치는 자리에서 아무 데서나 잘렸다(送/る). 이제 한글이 든 단어만 + 묶고, 결합 문자는 온전한 글자 사이에만 넣는다(글자와 그 결합 기호는 떨어지지 않는다). 한국어 줄바꿈은 그대로다. + 줄바꿈되는 글을 그리는 DDS 부품이 모두 이 함수를 쓰므로 버튼·탭·입력칸·배너가 함께 고쳐진다. + ## 0.12.0 — 2026-11-19 **새 기능** diff --git a/android/CHANGELOG.md b/android/CHANGELOG.md index f0160e49..f23037a9 100644 --- a/android/CHANGELOG.md +++ b/android/CHANGELOG.md @@ -5,6 +5,17 @@ `kr.devslab:dds-android-tokens`, `kr.devslab:dds-compose` and (from 0.7.0) `kr.devslab:dds-compose-testing` share one version, separate from the web packages (D-048). Releases are cut by an `android-v` tag. +## 0.12.1 — 2026-11-19 + +**Fixed** + +- **`keepWords()` joins Korean words only.** It put a word joiner between every two characters of every word, whatever + the script. In Hindi (and Thai) that drew each vowel sign apart from its consonant, on a dotted circle; in Japanese + and Chinese, which have no spaces, it left a line no break point, so a long label broke wherever it ran out of room + (送/る). Now only a word that contains Hangul is joined, and the joiner goes between whole characters (a letter keeps + its combining marks). Korean text wraps as before. Every DDS component that draws wrapping text uses this function, + so buttons, tabs, fields and banners are fixed with it. + ## 0.12.0 — 2026-11-19 **New** diff --git a/android/README.ko.md b/android/README.ko.md index 5e453f60..a4382d47 100644 --- a/android/README.ko.md +++ b/android/README.ko.md @@ -26,8 +26,8 @@ dependencies { ```kotlin dependencies { - implementation("kr.devslab:dds-compose:0.12.0") - androidTestImplementation("kr.devslab:dds-compose-testing:0.12.0") + implementation("kr.devslab:dds-compose:0.12.1") + androidTestImplementation("kr.devslab:dds-compose-testing:0.12.1") } ``` diff --git a/android/README.md b/android/README.md index b255a7e4..d22c9a2c 100644 --- a/android/README.md +++ b/android/README.md @@ -26,8 +26,8 @@ dependencies { ```kotlin dependencies { - implementation("kr.devslab:dds-compose:0.12.0") - androidTestImplementation("kr.devslab:dds-compose-testing:0.12.0") + implementation("kr.devslab:dds-compose:0.12.1") + androidTestImplementation("kr.devslab:dds-compose-testing:0.12.1") } ``` diff --git a/android/dds-compose/src/main/kotlin/kr/devslab/dds/compose/theme/DdsTypography.kt b/android/dds-compose/src/main/kotlin/kr/devslab/dds/compose/theme/DdsTypography.kt index 532e59ee..11c95a1d 100644 --- a/android/dds-compose/src/main/kotlin/kr/devslab/dds/compose/theme/DdsTypography.kt +++ b/android/dds-compose/src/main/kotlin/kr/devslab/dds/compose/theme/DdsTypography.kt @@ -1,5 +1,6 @@ package kr.devslab.dds.compose.theme +import java.text.BreakIterator import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.semantics.text import androidx.compose.ui.semantics.clearAndSetSemantics @@ -134,30 +135,55 @@ internal val LocalDdsTypography = staticCompositionLocalOf { /** * Android breaks Korean between any two syllables. This joins the characters - * of each word so a line breaks only at spaces — the same result as the web's - * `word-break: keep-all`. + * of each Korean word so a line breaks only at spaces — the same result as the + * web's `word-break: keep-all`. * - * Use it for short Korean UI text (titles, buttons, notices). Do not use it - * on a string with no spaces that may be longer than the line: it could then + * Only a word that contains Hangul is joined, and the joiner goes between + * whole characters as a reader sees them (a letter keeps its combining marks): + * - Devanagari, Thai and Arabic shape a letter together with its neighbours, + * and a joiner between them draws the marks apart on dotted circles; + * - Japanese and Chinese have no spaces, so joining every character would + * leave the line no break point at all; + * - a Latin or Cyrillic word already breaks only at its spaces. + * + * Use it for short UI text (titles, buttons, notices). Do not use it on a + * Korean string with no spaces that may be longer than the line: it could then * not wrap at all. */ public fun String.keepWords(): String { - if (isEmpty()) return this + if (isEmpty() || none(::isHangul)) return this val out = StringBuilder(length * 2) - var i = 0 - while (i < length) { - // Step by code point, so an emoji or a rare ideograph (a surrogate pair) is never split. - val codePoint = codePointAt(i) - val next = i + Character.charCount(codePoint) - out.appendCodePoint(codePoint) - if (next < length && !Character.isWhitespace(codePoint) && !Character.isWhitespace(codePointAt(next))) { - out.append(WORD_JOINER) - } - i = next + var wordStart = 0 + while (wordStart < length) { + var wordEnd = wordStart + while (wordEnd < length && !Character.isWhitespace(this[wordEnd])) wordEnd++ + if (wordEnd > wordStart) out.appendWord(substring(wordStart, wordEnd)) + while (wordEnd < length && Character.isWhitespace(this[wordEnd])) out.append(this[wordEnd++]) + wordStart = wordEnd } return out.toString() } +private fun isHangul(char: Char): Boolean = Character.UnicodeScript.of(char.code) == Character.UnicodeScript.HANGUL + +/** [word] (no whitespace in it), with a joiner between its characters when it is a Korean word. */ +private fun StringBuilder.appendWord(word: String) { + if (word.none(::isHangul)) { + append(word) + return + } + // By grapheme cluster, so a surrogate pair, a letter with its combining marks and decomposed Hangul stay whole. + val clusters = BreakIterator.getCharacterInstance().apply { setText(word) } + var start = clusters.first() + var end = clusters.next() + while (end != BreakIterator.DONE) { + if (start > 0) append(WORD_JOINER) + append(word, start, end) + start = end + end = clusters.next() + } +} + /** U+2060 WORD JOINER: invisible, zero width, forbids a line break on either side. */ private const val WORD_JOINER: Char = 0x2060.toChar() diff --git a/android/dds-compose/src/test/kotlin/kr/devslab/dds/compose/theme/BrandTest.kt b/android/dds-compose/src/test/kotlin/kr/devslab/dds/compose/theme/BrandTest.kt index 765ae021..2bdc4b2b 100644 --- a/android/dds-compose/src/test/kotlin/kr/devslab/dds/compose/theme/BrandTest.kt +++ b/android/dds-compose/src/test/kotlin/kr/devslab/dds/compose/theme/BrandTest.kt @@ -453,4 +453,32 @@ class BrandTest { val emoji = String(Character.toChars(0x1F600)) assertEquals("확$j$emoji", "확$emoji".keepWords()) } + + @Test + fun keepWordsJoinsLatinDigitsAndPunctuationToTheKoreanWordTheyBelongTo() { + val j = 0x2060.toChar() + assertEquals("Q${j}R${j}로 보${j}내${j}기", "QR로 보내기".keepWords()) + assertEquals("5${j}명${j}까${j}지", "5명까지".keepWords()) + assertEquals("괜${j}찮${j}아${j}.", "괜찮아.".keepWords()) + } + + @Test + fun keepWordsLeavesTextWithoutHangulAsItIs() { + // Devanagari, Thai and Arabic shape a letter with the marks and letters next to it: a joiner between them + // draws the marks apart on dotted circles. Japanese and Chinese have no spaces, so joining every character + // would leave no break point at all. Latin words already break only at spaces. + for (text in listOf("मेरे कार्ड", "นามบัตรของฉัน", "بطاقاتي", "連絡先ファイルを送る (Quick Share)", "收到的名片", "My cards", "Мои визитки")) { + assertEquals(text, text.keepWords()) + } + } + + @Test + fun keepWordsNeverSeparatesALetterFromItsCombiningMark() { + val j = 0x2060.toChar() + // Decomposed Hangul (conjoining jamo) and a combining accent stay with their base. + val decomposed = java.text.Normalizer.normalize("한글", java.text.Normalizer.Form.NFD) + val parts = java.text.Normalizer.normalize("한", java.text.Normalizer.Form.NFD) to java.text.Normalizer.normalize("글", java.text.Normalizer.Form.NFD) + assertEquals(parts.first + j + parts.second, decomposed.keepWords()) + assertEquals("카${j}페${j}é", "카페é".keepWords()) + } } diff --git a/android/gradle.properties b/android/gradle.properties index 7d8757f6..26d2dd23 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -8,4 +8,4 @@ kotlin.code.style=official # Android releases are versioned apart from the web packages (docs/decisions.md D-048). # The release workflow overrides VERSION_NAME from the android-v* tag. GROUP=kr.devslab -VERSION_NAME=0.12.0 +VERSION_NAME=0.12.1