Automatically insert spacing between CJK (Chinese, Japanese, Korean) characters and half-width Latin letters, numbers, and symbols — for ArkTS.
Note
"Pangu" (盤古) in this context refers to a typographic convention for CJK–Latin mixed-script spacing, not to be confused with the Pangu large language model (盘古大模型) or any other homonymous product. The name originates from pangu.js by Vinta Chen.
When CJK characters and Latin characters sit side by side without spacing, readability suffers. Pangu.ArkTS fixes this automatically:
| Before | After |
|---|---|
| 新八的构造成分有95%是眼镜和3%是泥巴以及2%的垃圾 | 新八的构造成分有 95% 是眼镜和 3% 是泥巴以及 2% 的垃圾 |
| 所以,请问Jack,你说的"这件事情"是怎么回事? | 所以,请问 Jack,你说的 "这件事情" 是怎么回事? |
| 当您使用这些服务时,您将同意OpenHarmony的条款与条件。 | 当您使用这些服务时,您将同意 OpenHarmony 的条款与条件。 |
| 中文English混排测试123 | 中文 English 混排测试 123 |
ohpm install pangu_cjkimport { spacingText } from 'pangu_cjk';
const result: string = spacingText('中文English混排123');
// '中文 English 混排 123'
import { Pangu } from 'pangu_cjk';
const result: string = Pangu.spacingText('当您使用OpenHarmony时');
// '当您使用 OpenHarmony 时'
| Export | Type | Description |
|---|---|---|
spacingText |
(text: string): string |
Returns a new string with proper spacing inserted between CJK characters and adjacent Latin/number/symbol characters. |
Pangu |
Class | Wrapper class exposing Pangu.spacingText(text: string): string, which delegates to the standalone spacingText function. |
The library applies the following transformations in order:
- Half-width → full-width punctuation between CJK characters (
~!;,?→~!;,。?) - Ellipsis / dots adjacent to CJK — add space
- CJK colon + alphanumeric — convert colon to full-width
- CJK + quotation marks — add space
- Single quotes near CJK — add space, but preserve English possessive
's - Hash / number signs near CJK (hashtags)
- Operators (
+ - * / = & | < >) between CJK and alphanumeric — add space - Slash path normalization — undo spacing inside paths like
/home/user - Brackets (round, square, curly, angle) near CJK — add space
- General CJK ↔ alphanumeric spacing (the primary rule)
- Percent sign spacing
- Middle dot normalization —
·•‧→・(katakana middle dot U+30FB)
Supported CJK Unicode ranges: CJK Radicals Supplement, Kangxi Radicals, Hiragana, Katakana, Bopomofo, Enclosed CJK Letters and Months, CJK Unified Ideographs Extension A, CJK Unified Ideographs, CJK Compatibility Ideographs.
pangu_cjk/ # HAR library module (the core, distributable library)
Index.ets # Public API entry point
src/main/ets/pangu/Pangu.ets # Core implementation
entry/ # HAP demo application
src/main/ets/pages/Index.ets # Sample UI with built-in examples & live input
The entry module provides a working demo with:
- 6 built-in before/after examples
- Interactive text input for real-time processing
Build and run the entry module on a device or emulator to try it out.
| Item | Value |
|---|---|
| Language | ArkTS |
| Min SDK | 6.0.0 (API 20) |
| Target SDK | 6.1.1 (API 24) |
| Runtime OS | HarmonyOS |
| Runtime dependencies | None |
Ported from pangu.kt by darkokoa.