Skip to content

Repository files navigation

Pangu.ArkTS

中文文档

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.

Why?

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

Installation

ohpm install pangu_cjk

Usage

Standalone function

import { spacingText } from 'pangu_cjk';

const result: string = spacingText('中文English混排123');
// '中文 English 混排 123'

Class static method

import { Pangu } from 'pangu_cjk';

const result: string = Pangu.spacingText('当您使用OpenHarmony时');
// '当您使用 OpenHarmony 时'

API

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.

Processing rules

The library applies the following transformations in order:

  1. Half-width → full-width punctuation between CJK characters (~!;,? → ~!;,。?)
  2. Ellipsis / dots adjacent to CJK — add space
  3. CJK colon + alphanumeric — convert colon to full-width
  4. CJK + quotation marks — add space
  5. Single quotes near CJK — add space, but preserve English possessive 's
  6. Hash / number signs near CJK (hashtags)
  7. Operators (+ - * / = & | < >) between CJK and alphanumeric — add space
  8. Slash path normalization — undo spacing inside paths like /home/user
  9. Brackets (round, square, curly, angle) near CJK — add space
  10. General CJK ↔ alphanumeric spacing (the primary rule)
  11. Percent sign spacing
  12. 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.

Project structure

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

Demo app

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.

Compatibility

Item Value
Language ArkTS
Min SDK 6.0.0 (API 20)
Target SDK 6.1.1 (API 24)
Runtime OS HarmonyOS
Runtime dependencies None

License

Mulan PSL v2

Acknowledgements

Ported from pangu.kt by darkokoa.

About

Inserts spacing between CJK characters and Latin letters, numbers, or symbols in ArkTS

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages