#emoji #grapheme #cjk #unicode #localization #syllable #cluster #display-width

runefix-core

Unicode character display width engine supporting CJK, emoji, and grapheme clusters

8 releases

Uses new Rust 2024

new 0.1.9 May 27, 2025
0.1.8 May 26, 2025

#453 in Text processing

Download history 160/week @ 2025-05-20

160 downloads per month

MIT/Apache

280KB
480 lines

runefix-core

๐ŸŽฏ A Rust core library that fixes terminal and Markdown misalignment caused by CJK and emoji widths.

Crates.io Docs.rs Rust Version License: MIT OR Apache-2.0 CI

English | ็ฎ€ไฝ“ไธญๆ–‡ | ๆ—ฅๆœฌ่ชž


โœจ Overview

runefix-core is a Unicode-based display width engine that provides precise character width calculation, designed for:

  • Terminal CLI alignment
  • Markdown table rendering
  • TUI layout engines
  • Rich text editor rendering

Its goal is to solve a decades-long problem: misaligned characters in terminal and UI environments due to inconsistent Unicode width handling. This includes:

  • CJK characters (Chinese, Japanese, Korean), Kana, and Hangul syllables
  • Emoji (base, composite, and flag sequences)
  • Fullwidth punctuation and multilingual symbols
  • Grapheme cluster boundary cases

โœ… Key Features

  • โœ… Accurate width detection for emoji, CJK, Hangul, Kana, and fullwidth symbols
  • โœ… Full support for grapheme clusters (emoji composition, regional indicators)
  • โœ… Zero dependencies, minimal API, compatible with any terminal or UI renderer
  • โœ… Fully reproducible datasets, powered by char-table
  • โœ… Language bindings planned for JS / Python / Go and more

๐Ÿงฉ Optional: Runtime Width Policies

๐Ÿงช --features policy required

By default, runefix-core uses the terminal layout policy, where both emoji and CJK characters occupy 2 columns.
You can optionally enable runtime policies to adapt to other rendering environments:

# Cargo.toml
runefix-core = { version = "0.1", features = ["policy"] }

Then:

use runefix_core::{WidthPolicy, WithPolicy};

let policy = WidthPolicy::markdown();
let width = WithPolicy::new(&policy).apply("๐Ÿ˜‚").display_width();

assert_eq!(width, 1); // markdown prefers emoji width = 1

โ„น๏ธ Note: Some advanced methods (like .truncate_by_width(...)) may require intermediate bindings due to borrowing rules of &str. See with_policy.rs for idiomatic usage.

๐Ÿง  Built-in Policies

Policy Emoji CJK Variant Use case
terminal() 2 2 2 Default. Best for terminal apps
markdown() 1 2 2 GitHub / Typora / table renderers
compact() 1 1 1 Logs, status bars, tight layouts

You can also override policies dynamically at runtime for your renderer.

๐Ÿš€ Quick Example

use runefix_core::RuneDisplayWidth;

fn main() {
    println!("{}", "ไฝ ".rune_width());         // Output: 2
    println!("{}", "๐Ÿ˜‚".rune_width());         // Output: 2
    println!("{}", "ไฝ a1๐Ÿ‘‡".display_width());  // Output: 6
}

๐Ÿ“ท Real-World Examples

For a full demo of grapheme-aware alignment and terminal behavior, see examples/text_align.rs.

It showcases:

  • Naive centering using .chars().count() โŒ
  • Fixed centering using runefix_core::RuneDisplayWidth โœ…
  • Screenshot comparisons across terminals

๐Ÿ‘‰ Read more in examples/README.md

๐Ÿ“ฆ Installation

Add the following to your Cargo.toml:

# Stable release (recommended)
runefix-core = "0.1"

See crates.io for the latest version.

๐Ÿ“š Use Cases

  • ๐Ÿ–ฅ๏ธ Terminal alignment (CLI box drawing, tables)
  • ๐Ÿงพ Markdown renderers (accurate width handling in table cells)
  • ๐Ÿ“Š TUI layout engines (React-style terminal UIs)
  • ๐Ÿ“„ Editors and terminal emulators (accurate text wrapping)
  • ๐Ÿงฉ Width testing tools, playgrounds, and visualization platforms

๐Ÿ“ Dataset Sources

This crate relies on structured, versioned datasets from char-table, including:

  • emoji_base.json, emoji_zwj.json
  • cjk_unified.json, japanese_kana.json, korean_syllables.json
  • fullwidth_variants.json, fullwidth_punctuations.json

All datasets are reproducible, regularly updated, and aligned with the latest Unicode releases.

๐Ÿ› ๏ธ Project Status

Module Status
CJK width detection โœ… Completed
Emoji width support โœ… Completed
Grapheme cluster โœ… Completed

๐Ÿ“Œ CHANGELOG

See CHANGELOG.md for version history.

๐Ÿ”– License

MIT OR Apache-2.0
ยฉ 2025 Pokeya Z. Chen / Runefix Labs

๐Ÿ“ฃ Project Affiliation

This project is maintained by the runefix-labs organization. It serves as the core engine for the runefix suite โ€” a multi-language, cross-platform solution for Unicode width handling.

๐ŸŒ Contact

GitHub: github.com/runefix-labs
Author: pokeyaro
Email: pokeya.mystic@gmail.com

Dependencies

~0.8โ€“1.4MB
~24K SLoC