No description
Find a file
leyoda 63072bbe29 yoda: snap monospace cell width via Unicode East Asian Width
Replaces the previous heuristic (font_monospace_em_width.is_none() ⇒ 2
cells) which was reviewed as unsound: Arabic, dingbats, math symbols and
other narrow scripts pulled from non-monospace fallback fonts would all
have been forced to 2 cells. It also didn't handle ZWJ emoji clusters
or ambiguous-width chars correctly.

Proper fix, computed at shape time when `line: &str` is in scope:
- new ShapeGlyph.terminal_cells: u8 (0, 1 or 2)
- populated via unicode-width crate applied to the cluster text
  line[start..end] (harfrust path, uses UnicodeWidthStr) or to the
  single codepoint (no-font fallback path, UnicodeWidthChar)
- layout_to_buffer consumes it when match_mono_width is Some:
      x_advance = cells * mono_width
  instead of the previous round(x_advance / mono_width) * mono_width
  which produced variable cell counts for fallback glyphs.

Covers:
- ASCII + Latin        → width 1  (unchanged visual)
- CJK + fullwidth      → width 2  ✓
- Emoji (incl. ZWJ)    → width 2  ✓  (cluster text handles the ZWJ case)
- Arabic / Hebrew      → width 1  ✓  (was wrongly snapped to 2 before)
- Combining marks      → width 0  ✓  (zero-advance, matches terminals)
- Variation selectors  → width 0  ✓

Limitations: ambiguous-width chars (EAW=A) resolve to 1 via unicode-width
default; a 'cjk' ambiguous mode (unicode-width::UnicodeWidthChar::width_cjk)
could be exposed later as a Buffer flag if needed — not needed for typical
terminal use, matching most wcwidth implementations.

Based on review feedback from lionel@wopr.io on the initial heuristic patch.
2026-04-23 23:20:40 +02:00
.github Add pull request template 2026-03-10 13:10:57 -06:00
benches feat: buffer setter methods are now lazy 2026-03-26 20:48:52 -06:00
examples fix: empty lines use span metrics 2026-02-24 14:09:08 -07:00
fonts Variable font support (#400) 2025-07-07 08:50:40 -06:00
sample Fix extra newline in crlf sample 2024-04-30 12:26:28 -06:00
screenshots Add screenshots 2022-11-09 07:22:20 -07:00
src yoda: snap monospace cell width via Unicode East Asian Width 2026-04-23 23:20:40 +02:00
tests feat: buffer setter methods are now lazy 2026-03-26 20:48:52 -06:00
.gitattributes Add image render tests 2023-09-27 11:57:18 +03:00
.gitignore Add image render tests 2023-09-27 11:57:18 +03:00
Cargo.toml yoda: snap monospace cell width via Unicode East Asian Width 2026-04-23 23:20:40 +02:00
CHANGELOG.md 0.19.0 2026-04-22 13:17:01 -06:00
ci.sh Better handling of newlines in editor insert and delete 2025-10-09 12:21:38 -06:00
deny.toml ci: ignore bincode being unmaintained 2026-02-24 15:45:42 -07:00
editor-test.sh Do not use mono feature for editor test 2022-10-25 10:18:11 -06:00
editor.sh Add line ending abstraction (#250) 2024-04-30 12:12:25 -06:00
LICENSE-APACHE Relicense MIT OR Apache 2.0 2022-10-24 12:58:15 -06:00
LICENSE-MIT Relicense MIT OR Apache 2.0 2022-10-24 12:58:15 -06:00
multiview.sh multiview example 2024-01-17 13:31:03 -07:00
README.md Update README.md 2025-10-04 13:23:50 -06:00
redoxer.sh Fix redoxer script 2023-11-15 12:43:14 -07:00
rich-text.sh Use Color in more places 2022-10-27 09:07:47 -06:00
terminal.sh Improvements to syntax editor, integrate with libcosmic editor 2022-11-09 10:44:51 -07:00
test.sh Vi-style editor and other editor improvements (#40) 2022-11-15 12:26:59 -07:00

COSMIC Text

crates.io docs.rs license Rust workflow

Pure Rust multi-line text handling.

COSMIC Text provides advanced text shaping, layout, and rendering wrapped up into a simple abstraction. Shaping is provided by HarfRust, and supports a wide variety of advanced shaping operations. Rendering is provided by swash, which supports ligatures and color emoji. Layout is implemented custom, in safe Rust, and supports bidirectional text. Font fallback is also a custom implementation, reusing some of the static fallback lists in browsers such as Chromium and Firefox. Linux, macOS, and Windows are supported with the full feature set. Other platforms may need to implement font fallback capabilities.

Screenshots

Arabic translation of Universal Declaration of Human Rights Arabic screenshot

Hindi translation of Universal Declaration of Human Rights Hindi screenshot

Simplified Chinese translation of Universal Declaration of Human Rights Simplified Chinses screenshot

View Universal Declaration of Human Rights on OHCHR

Roadmap

The following features must be supported before this is "ready":

  • Font loading (using fontdb)
    • Preset fonts
    • System fonts
  • Text styles (bold, italic, etc.)
    • Per-buffer
    • Per-span
  • Font shaping (using HarfRust)
    • Cache results
    • RTL
    • Bidirectional rendering
  • Font fallback
    • Choose font based on locale to work around "unification"
    • Per-line granularity
    • Per-character granularity
  • Font layout
    • Click detection
    • Simple wrapping
    • Wrapping with indentation
    • No wrapping
    • Ellipsize
  • Font rendering (using swash)
    • Cache results
    • Font hinting
    • Ligatures
    • Color emoji
  • Text editing
    • Performance improvements
    • Text selection
    • Can automatically recreate https://unicode.org/udhr/ without errors (see below)
    • Bidirectional selection
    • Copy/paste
  • no_std support (with default-features = false)
    • no_std font loading
    • no_std shaping
    • no_std layout
    • no_std rendering

The UDHR (Universal Declaration of Human Rights) test involves taking the entire set of UDHR translations (almost 500 languages), concatenating them as one file (which ends up being 8 megabytes!), then via the editor-test example, automatically simulating the entry of that file into cosmic-text per-character, with the use of backspace and delete tested per character and per line. Then, the final contents of the buffer is compared to the original file. All of the 106746 lines are correct.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.