Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

Shape Bengali and the other Indic and South-East Asian scripts

VRonGitHubeb4016664d96
diff --git a/apps/rustcord/src/style.rs b/apps/rustcord/src/style.rs
index 555803b..fde3017 100644
--- a/apps/rustcord/src/style.rs
+++ b/apps/rustcord/src/style.rs
@@ -1417,6 +1417,44 @@ mod tests {
assert!((galley.rows[0].rect.width() - 30.188).abs() < 0.6, "{}", galley.rows[0].rect.width());
}
+ /// Bengali and the other scripts of South and South-East Asia are shaped
+ /// (PR #11): a conjunct, or a consonant with its vowel sign, is one
+ /// picture while each character keeps a glyph of its own (for the cursor
+ /// and copying), and words are as wide as Chrome draws them in the same
+ /// font (measured on Discord web, 2026-12-04).
+ #[cfg(windows)]
+ #[test]
+ fn brahmic_letters_are_shaped() {
+ let ctx = eframe::egui::Context::default();
+ super::install_fonts(&ctx);
+ let _ = ctx.run(Default::default(), |_| {});
+ let format = TextFormat { font_id: FontId::proportional(16.0), ..Default::default() };
+ let layout = |text: &str| ctx.fonts(|f| f.layout_job(LayoutJob::single_section(text.into(), format.clone())));
+
+ // "ki" (the vowel sign goes before the consonant) and "ksha" (a conjunct).
+ for (text, chars) in [("\u{995}\u{9bf}", 2), ("\u{995}\u{9cd}\u{9b7}", 3)] {
+ let galley = layout(text);
+ let glyphs = &galley.rows[0].glyphs;
+ assert_eq!(glyphs.len(), chars, "{text}: a glyph per character");
+ assert_eq!(glyphs.iter().filter(|g| !g.uv_rect.is_nothing()).count(), 1, "{text}: one picture");
+ }
+ for (text, chrome) in [
+ // Bengali and Tamil: Nirmala UI, in Chrome with Discord's font list too.
+ ("\u{9ac}\u{9be}\u{982}\u{9b2}\u{9be}", 36.766),
+ ("\u{9af}\u{9be}\u{99a}\u{9cd}\u{99b}\u{9c7}", 37.922),
+ ("\u{9ae}\u{9c2}\u{9b2}\u{9cd}\u{9af}\u{9be}\u{9af}\u{9bc}\u{9a8}", 53.75),
+ ("\u{ba4}\u{bae}\u{bbf}\u{bb4}\u{bcd}", 47.5),
+ // Hindi in Nirmala UI. (Discord web draws Devanagari in its own
+ // Noto Sans instead, 26.766 px wide.)
+ ("\u{939}\u{93f}\u{928}\u{94d}\u{926}\u{940}", 32.594),
+ ] {
+ let width = layout(text).rows[0].rect.width();
+ // (Within a pixel: a row ends at the whole pixel its last letter
+ // is drawn from, plus that letter's width.)
+ assert!((width - chrome).abs() < 1.0, "{text}: {width} px (Chrome {chrome})");
+ }
+ }
+
/// Arabic, Latin, Hebrew and digits in one line land where Chrome puts
/// them (Discord web's message font, measured 2026-12-03: the left edge
/// of each character below).
diff --git a/crates/vendor/epaint/src/text/text_layout.rs b/crates/vendor/epaint/src/text/text_layout.rs
index 1e73c90..bd88392 100644
--- a/crates/vendor/epaint/src/text/text_layout.rs
+++ b/crates/vendor/epaint/src/text/text_layout.rs
@@ -184,7 +184,7 @@ fn layout_section(
continue; // laid out with its run
}
if emoji_rest == 0 && starts_shaped_run(chr) {
- if let Some(end) = layout_shaped_run(font, section_text, byte, paragraph, line_height, section_index) {
+ if let Some(end) = layout_shaped_run(font, section_text, byte, paragraph, line_height, section_index, pixels_per_point) {
shaped_end = end;
last_glyph_id = None;
continue;
@@ -276,25 +276,37 @@ fn place_on_pixel_grid(x_pixels: f32, uv_rect: &mut UvRect) -> f32 {
}
/// RustCord: a letter of a script whose letters join (bidi class AL: Arabic,
-/// Syriac, Thaana, N'Ko), which starts a run laid out by
-/// [`layout_shaped_run`].
+/// Syriac, Thaana, N'Ko) or of a Brahmic script (see [`is_brahmic`]), which
+/// starts a run laid out by [`layout_shaped_run`].
fn starts_shaped_run(c: char) -> bool {
- c >= '\u{600}' && unicode_bidi::bidi_class(c) == unicode_bidi::BidiClass::AL
+ is_brahmic(c) || c >= '\u{600}' && unicode_bidi::bidi_class(c) == unicode_bidi::BidiClass::AL
}
/// RustCord: what a shaped run goes on with: more such letters, the marks on
/// them and the joiners between them.
fn continues_shaped_run(c: char) -> bool {
use unicode_bidi::BidiClass::{AL, NSM};
- matches!(c, '\u{200C}' | '\u{200D}') || matches!(unicode_bidi::bidi_class(c), AL | NSM)
+ matches!(c, '\u{200C}' | '\u{200D}') || is_brahmic(c) || matches!(unicode_bidi::bidi_class(c), AL | NSM)
+}
+
+/// RustCord: the scripts of South and South-East Asia, written left to right,
+/// whose letters change with their neighbours: consonants join into
+/// conjuncts, and vowel signs go before, above or below the consonant they
+/// follow in the text. Devanagari to Sinhala (Bengali, Tamil, Telugu…), Thai,
+/// Lao, Tibetan, Myanmar and Khmer.
+fn is_brahmic(c: char) -> bool {
+ matches!(c, '\u{900}'..='\u{DFF}' | '\u{E00}'..='\u{FFF}' | '\u{1000}'..='\u{109F}' | '\u{1780}'..='\u{17FF}')
}
/// RustCord: lays out a run of joining letters from `start` as a whole
/// (laid out a character at a time they would all take their standalone
/// forms): shaped (see `FontImpl::shape_run`), one glyph per character in the
/// text's order, the first of each cluster carrying its picture and width and
-/// the rest none. They are placed on the pixel grid by [`reorder_bidi_row`].
-/// Returns where the run ends, or `None` when no font draws its first letter.
+/// the rest none, at the cluster's end (so a row's last glyph ends where its
+/// ink does). Left-to-right clusters go on the pixel grid here, like other
+/// glyphs; right-to-left ones once [`reorder_bidi_row`] has put them in
+/// visual order. Returns where the run ends, or `None` when no font draws its
+/// first letter.
fn layout_shaped_run(
font: &Font,
text: &str,
@@ -302,8 +314,11 @@ fn layout_shaped_run(
paragraph: &mut Paragraph,
line_height: f32,
section_index: u32,
+ pixels_per_point: f32,
) -> Option<usize> {
- let font_impl = font.font_impl_for(text[start..].chars().next()?)?;
+ let first = text[start..].chars().next()?;
+ let font_impl = font.font_impl_for(first)?;
+ let ltr = is_brahmic(first);
// As far as this font draws it.
let end = text[start..]
.char_indices()
@@ -314,21 +329,27 @@ fn layout_shaped_run(
let mut byte = start;
for cluster in clusters {
let cluster_end = (byte + cluster.byte_len).min(end);
+ let mut uv_rect = cluster.uv_rect;
+ let x = if ltr {
+ place_on_pixel_grid(paragraph.cursor_x * pixels_per_point, &mut uv_rect) / pixels_per_point
+ } else {
+ paragraph.cursor_x
+ };
for (i, chr) in text[byte..cluster_end].chars().enumerate() {
let first = i == 0;
paragraph.glyphs.push(Glyph {
chr,
- pos: pos2(paragraph.cursor_x, f32::NAN),
+ pos: pos2(if first { x } else { paragraph.cursor_x + cluster.advance_width }, f32::NAN),
advance_width: if first { cluster.advance_width } else { 0.0 },
line_height,
font_impl_height: font_impl.row_height(),
font_impl_ascent: font_impl.ascent(),
font_height: font.row_height(),
font_ascent: font.ascent(),
- uv_rect: if first { cluster.uv_rect } else { UvRect::default() },
+ uv_rect: if first { uv_rect } else { UvRect::default() },
section_index,
bidi_level: 0,
- unsnapped: true,
+ unsnapped: !ltr,
});
}
paragraph.cursor_x += cluster.advance_width;
diff --git a/crates/vendor/epaint/src/text/text_layout_types.rs b/crates/vendor/epaint/src/text/text_layout_types.rs
index 9f2f0d4..b056222 100644
--- a/crates/vendor/epaint/src/text/text_layout_types.rs
+++ b/crates/vendor/epaint/src/text/text_layout_types.rs
@@ -639,9 +639,9 @@ pub struct Glyph {
/// left: the character then starts at its glyph's right edge.
pub bidi_level: u8,
- /// RustCord: part of a shaped cluster that isn't placed on the pixel grid
- /// yet (that happens once its row is in visual order): `pos.x` is still the
- /// exact pen position and `uv_rect` isn't shifted to it.
+ /// RustCord: part of a right-to-left shaped cluster that isn't placed on
+ /// the pixel grid yet (that happens once its row is in visual order):
+ /// `pos.x` is still the exact pen position and `uv_rect` isn't shifted to it.
pub(crate) unsnapped: bool,
}