diff --git a/native/android/Cargo.lock b/native/android/Cargo.lock index 69b61d2e..a8edd9d7 100644 --- a/native/android/Cargo.lock +++ b/native/android/Cargo.lock @@ -198,7 +198,9 @@ dependencies = [ "log", "minimp3", "naga", + "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1304,6 +1306,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1551,12 +1569,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2" diff --git a/native/ios/Cargo.lock b/native/ios/Cargo.lock index bcfedf57..636e6d4c 100644 --- a/native/ios/Cargo.lock +++ b/native/ios/Cargo.lock @@ -171,6 +171,7 @@ dependencies = [ "naga", "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1132,6 +1133,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1349,12 +1366,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2" diff --git a/native/linux/Cargo.lock b/native/linux/Cargo.lock index ba3356fc..9f1ced69 100644 --- a/native/linux/Cargo.lock +++ b/native/linux/Cargo.lock @@ -414,7 +414,9 @@ dependencies = [ "log", "minimp3", "naga", + "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1956,6 +1958,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scoped-tls" version = "1.0.1" @@ -2332,12 +2350,36 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2" diff --git a/native/macos/Cargo.lock b/native/macos/Cargo.lock index 34ccbdd8..0914b646 100644 --- a/native/macos/Cargo.lock +++ b/native/macos/Cargo.lock @@ -396,6 +396,7 @@ dependencies = [ "naga", "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -676,7 +677,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1983,7 +1984,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1992,6 +1993,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scoped-tls" version = "1.0.1" @@ -2193,7 +2210,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -2365,15 +2382,39 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.1.14" @@ -2815,7 +2856,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/native/shared/Cargo.lock b/native/shared/Cargo.lock index 6d295ba2..95a17bae 100644 --- a/native/shared/Cargo.lock +++ b/native/shared/Cargo.lock @@ -167,6 +167,7 @@ dependencies = [ "objc2", "pollster", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1236,6 +1237,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags 2.11.0", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "same-file" version = "1.0.6" @@ -1462,12 +1479,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.1.14" diff --git a/native/shared/Cargo.toml b/native/shared/Cargo.toml index 6b258630..aa5563f1 100644 --- a/native/shared/Cargo.toml +++ b/native/shared/Cargo.toml @@ -57,6 +57,8 @@ naga = { version = "29", features = ["glsl-in", "wgsl-out"] } log = "0.4" raw-window-handle = "0.6" fontdue = "0.9" +# complex scripts (Arabic, Hebrew, Indic): text_shaping.rs. 0.14 shares fontdue's ttf-parser 0.21 +rustybuzz = "0.14" libc = "0.2" bytemuck = { version = "1", features = ["derive"] } half = "2" diff --git a/native/shared/src/lib.rs b/native/shared/src/lib.rs index fa25692f..fbf57ad8 100644 --- a/native/shared/src/lib.rs +++ b/native/shared/src/lib.rs @@ -29,6 +29,7 @@ pub mod models_cooked_scene; pub mod renderer; pub mod string_header; pub mod text_renderer; +pub mod text_shaping; pub mod textures; #[cfg(feature = "models3d")] pub mod virtual_geometry; diff --git a/native/shared/src/text_renderer.rs b/native/shared/src/text_renderer.rs index 55e4fbd9..f8e22055 100644 --- a/native/shared/src/text_renderer.rs +++ b/native/shared/src/text_renderer.rs @@ -1,5 +1,7 @@ use crate::renderer::Renderer; +use crate::text_shaping::{needs_shaping, ShapeCache}; use fontdue::{Font, FontSettings}; +use std::borrow::Cow; use std::collections::HashMap; static DEFAULT_FONT_DATA: &[u8] = include_bytes!("../assets/default_font.ttf"); @@ -72,7 +74,12 @@ const MAX_ATLAS_SIZE: u32 = 2048; pub struct TextRenderer { fonts: Vec, + /// Each font's file, for shaping (text_shaping.rs); the same indices as `fonts`. + font_data: Vec>, glyph_cache: HashMap<(usize, char, u32), GlyphInfo>, + /// Glyphs of shaped runs, by glyph id (a shaped glyph needn't be any one character's). + shaped_glyph_cache: HashMap<(usize, u16, u32), GlyphInfo>, + shapes: ShapeCache, atlas_data: Vec, atlas_width: u32, atlas_height: u32, @@ -107,7 +114,10 @@ impl TextRenderer { pub fn empty() -> Self { Self { fonts: Vec::new(), + font_data: Vec::new(), glyph_cache: HashMap::new(), + shaped_glyph_cache: HashMap::new(), + shapes: ShapeCache::default(), atlas_data: Vec::new(), atlas_width: 0, atlas_height: 0, @@ -140,7 +150,10 @@ impl TextRenderer { Self { fonts: vec![default_font], + font_data: vec![Cow::Borrowed(DEFAULT_FONT_DATA)], glyph_cache: HashMap::new(), + shaped_glyph_cache: HashMap::new(), + shapes: ShapeCache::default(), atlas_data: vec![0u8; (atlas_width * atlas_height * 4) as usize], atlas_width, atlas_height, @@ -165,10 +178,11 @@ impl TextRenderer { } pub fn load_font(&mut self, data: &[u8]) -> usize { - let data = unicode_cmaps_only(data); + let data = default_outlines_only(unicode_cmaps_only(data).into_owned()); let font = Font::from_bytes(&data[..], FontSettings::default()).expect("Failed to load font"); self.fonts.push(font); + self.font_data.push(Cow::Owned(data)); self.fonts.len() - 1 } @@ -181,9 +195,27 @@ impl TextRenderer { return; } - let font = &self.fonts[font_idx]; - let (metrics, bitmap) = font.rasterize(ch, size as f32); + let (metrics, bitmap) = self.fonts[font_idx].rasterize(ch, size as f32); + if let Some(info) = self.place_in_atlas(&metrics, &bitmap) { + self.glyph_cache.insert(key, info); + } + } + + /// Rasterize a glyph by its id (a shaped run's), like `rasterize_glyph`. + fn rasterize_glyph_id(&mut self, font_idx: usize, id: u16, size: u32) { + let key = (font_idx, id, size); + if self.shaped_glyph_cache.contains_key(&key) || self.atlas_full { + return; + } + let (metrics, bitmap) = self.fonts[font_idx].rasterize_indexed(id, size as f32); + if let Some(info) = self.place_in_atlas(&metrics, &bitmap) { + self.shaped_glyph_cache.insert(key, info); + } + } + /// Copy a rasterized glyph into the atlas: where it went, or None when the atlas is full + /// (`atlas_full` is then set and the atlas grows at the end of the frame). + fn place_in_atlas(&mut self, metrics: &fontdue::Metrics, bitmap: &[u8]) -> Option { let gw = metrics.width as u32; let gh = metrics.height as u32; @@ -197,7 +229,7 @@ impl TextRenderer { } if cy + gh > self.atlas_height || gw > self.atlas_width { self.atlas_full = true; - return; + return None; } self.atlas_cursor_x = cx; self.atlas_cursor_y = cy; @@ -228,18 +260,15 @@ impl TextRenderer { } self.atlas_dirty = true; - self.glyph_cache.insert( - key, - GlyphInfo { - atlas_x: ax, - atlas_y: ay, - width: gw, - height: gh, - advance: metrics.advance_width, - x_offset: metrics.xmin as f32, - y_offset: metrics.ymin as f32, - }, - ); + Some(GlyphInfo { + atlas_x: ax, + atlas_y: ay, + width: gw, + height: gh, + advance: metrics.advance_width, + x_offset: metrics.xmin as f32, + y_offset: metrics.ymin as f32, + }) } /// A glyph's advance at a pixel size, from the font's metrics (cached; nothing rasterized). @@ -292,6 +321,7 @@ impl TextRenderer { ); } self.glyph_cache.clear(); + self.shaped_glyph_cache.clear(); self.atlas_data.iter_mut().for_each(|b| *b = 0); self.atlas_cursor_x = 0; self.atlas_cursor_y = 0; @@ -357,6 +387,13 @@ impl TextRenderer { } else { 0 }; + if needs_shaping(text) { + let shaped = self.shapes.get(idx, &self.font_data[idx], text); + let scale = size as f32 / shaped.units_per_em; + let n = shaped.glyphs.len(); + let w: f32 = shaped.glyphs.iter().map(|g| g.x_advance * scale).sum(); + return (w + spacing * n.saturating_sub(1) as f32) as f64; + } let mut width = 0.0f32; let mut first = true; for ch in text.chars() { @@ -421,6 +458,19 @@ impl TextRenderer { // rounded, so derive from it rather than from dpi). let inv_dpi = size as f32 / phys_size as f32; + if needs_shaping(text) { + let color = [ + crate::renderer::srgb_u8_to_linear(r), + crate::renderer::srgb_u8_to_linear(g), + crate::renderer::srgb_u8_to_linear(b), + (a / 255.0) as f32, + ]; + self.draw_shaped( + renderer, idx, text, x as f32, y as f32, size, phys_size, spacing, color, + ); + return; + } + // Ensure all glyphs are rasterized first for ch in text.chars() { self.rasterize_glyph(idx, ch, phys_size); @@ -482,9 +532,61 @@ impl TextRenderer { } } + /// A run of complex-script text (text_shaping.rs): its glyphs, placed by the shaper, in visual + /// order from `x`. Same atlas and metrics conventions as `draw_text_ex`. + #[allow(clippy::too_many_arguments)] + fn draw_shaped( + &mut self, + renderer: &mut Renderer, + idx: usize, + text: &str, + x: f32, + y: f32, + size: u32, + phys_size: u32, + spacing: f32, + color: [f32; 4], + ) { + let shaped = self.shapes.get(idx, &self.font_data[idx], text); + for gl in &shaped.glyphs { + self.rasterize_glyph_id(idx, gl.id, phys_size); + } + self.ensure_atlas_uploaded(renderer); + let Some(atlas_bg) = self.atlas_bind_group_idx else { + return; + }; + let inv_dpi = size as f32 / phys_size as f32; + let scale = size as f32 / shaped.units_per_em; + let (aw, ah) = (self.atlas_width as f32, self.atlas_height as f32); + let mut pen = x; + for (i, gl) in shaped.glyphs.iter().enumerate() { + if i > 0 { + pen += spacing; + } + if let Some(glyph) = self.shaped_glyph_cache.get(&(idx, gl.id, phys_size)) { + let ox = gl.x_offset * scale; + let oy = gl.y_offset * scale; + let gx = pen + ox + glyph.x_offset * inv_dpi; + let gy = y - oy - (glyph.y_offset + glyph.height as f32) * inv_dpi + size as f32; + let gw = glyph.width as f32 * inv_dpi; + let gh = glyph.height as f32 * inv_dpi; + if gw > 0.0 && gh > 0.0 { + let u0 = glyph.atlas_x as f32 / aw; + let v0 = glyph.atlas_y as f32 / ah; + let u1 = (glyph.atlas_x + glyph.width) as f32 / aw; + let v1 = (glyph.atlas_y + glyph.height) as f32 / ah; + renderer.draw_textured_quad(gx, gy, gw, gh, u0, v0, u1, v1, color, atlas_bg); + } + } + pen += gl.x_advance * scale; + } + } + pub fn unload_font(&mut self, font_idx: usize) { if font_idx > 0 && font_idx < self.fonts.len() { self.glyph_cache.retain(|k, _| k.0 != font_idx); + self.shaped_glyph_cache.retain(|k, _| k.0 != font_idx); + self.shapes.forget_font(font_idx); self.advances.retain(|k, _| k.0 != font_idx); self.sdf_glyph_cache.retain(|k, _| k.0 != font_idx); } @@ -697,6 +799,34 @@ fn unicode_cmaps_only(data: &[u8]) -> std::borrow::Cow<'_, [u8]> { } } +/// The font without its `gvar` table (its tag renamed to `gvaz`, which keeps the table directory +/// sorted), so glyphs are drawn from their default outlines in `glyf`. ttf-parser's +/// `variable-fonts` feature (rustybuzz turns it on) sends every outline of a font with `gvar` +/// through it, and for some fonts it gives nothing: macOS's SFNS.ttf, the system-ui font, drew +/// every letter empty. Bloom always draws a font's default instance, so nothing is lost. +fn default_outlines_only(mut data: Vec) -> Vec { + let at = |d: &[u8], i: usize, n: usize| -> Option { + Some( + d.get(i..i + n)? + .iter() + .fold(0usize, |v, &b| (v << 8) | b as usize), + ) + }; + let face = if data.get(0..4) == Some(b"ttcf") { + at(&data, 12, 4).unwrap_or(0) + } else { + 0 + }; + let tables = at(&data, face + 4, 2).unwrap_or(0); + for t in 0..tables { + let rec = face + 12 + t * 16; + if data.get(rec..rec + 4) == Some(b"gvar") { + data[rec + 3] = b'z'; + } + } + data +} + #[cfg(test)] mod cmap_tests { use super::*; @@ -706,6 +836,18 @@ mod cmap_tests { font.rasterize(ch, 30.0).0.height } + #[test] + fn variable_font_draws_its_default_outlines() { + // macOS's system-ui font: with ttf-parser's variable-fonts on, "E" came out 0x0 + let Ok(data) = std::fs::read("/System/Library/Fonts/SFNS.ttf") else { + return; + }; + let font = + Font::from_bytes(&default_outlines_only(data)[..], FontSettings::default()).unwrap(); + assert!(rows(&font, 'E') > 10); + assert!(rows(&font, ',') > 3); + } + #[test] fn mac_roman_cmap_does_not_override_unicode() { // macOS's Menlo has the same cmaps as the iOS/tvOS one (Unicode, then Mac Roman) diff --git a/native/shared/src/text_shaping.rs b/native/shared/src/text_shaping.rs new file mode 100644 index 00000000..a2332e1d --- /dev/null +++ b/native/shared/src/text_shaping.rs @@ -0,0 +1,115 @@ +//! Shaping for the scripts that can't be drawn one character at a time: Arabic (letters join and +//! change form), Hebrew (right to left, marks) and the Indic scripts (conjuncts, vowel signs drawn +//! before their consonant). A run of text with any of their characters goes through rustybuzz, +//! which picks glyphs by the font's own GSUB/GPOS rules; the result is glyph ids with positions, +//! in visual order (left to right on screen, also for right-to-left text). Everything else keeps +//! the per-character path in `text_renderer.rs`, so Latin, CJK and Thai measure and draw exactly +//! as before. +//! +//! A run is shaped in one direction, guessed from its text (rustybuzz's +//! `guess_segment_properties`: the first strong character). Mixed-direction text has to be split +//! into runs of one direction and put in visual order by the caller (the bidi algorithm, +//! @bloomengine/web does it per line). + +use std::collections::HashMap; +use std::sync::Arc; + +/// One shaped glyph, in font units (scale by size / units_per_em). +#[derive(Clone, Copy, Debug)] +pub struct ShapedGlyph { + pub id: u16, + pub x_advance: f32, + pub x_offset: f32, + /// Up is positive (font coordinates), as in the font. + pub y_offset: f32, +} + +/// A shaped run: glyphs in visual order and the font's units per em. +pub struct Shaped { + pub glyphs: Vec, + pub units_per_em: f32, +} + +/// Hebrew, Arabic (with Syriac, Thaana, NKo and the Arabic supplements), the Indic scripts +/// (Devanagari to Sinhala), and the Hebrew and Arabic presentation forms. +pub fn needs_shaping(text: &str) -> bool { + text.chars().any(|c| { + matches!(c as u32, + 0x0590..=0x08FF | 0x0900..=0x0DFF | 0xFB1D..=0xFDFF | 0xFE70..=0xFEFF) + }) +} + +/// Shaped runs by font and text (shaping doesn't depend on the size). Bounded: a page's labels +/// are a few hundred runs, redrawn every frame. +#[derive(Default)] +pub struct ShapeCache { + runs: HashMap<(usize, String), Arc>, +} + +impl ShapeCache { + pub fn get(&mut self, font_idx: usize, data: &[u8], text: &str) -> Arc { + if let Some(s) = self.runs.get(&(font_idx, text.to_string())) { + return s.clone(); + } + if self.runs.len() > 4096 { + self.runs.clear(); + } + let s = Arc::new(shape(data, text)); + self.runs.insert((font_idx, text.to_string()), s.clone()); + s + } + + pub fn forget_font(&mut self, font_idx: usize) { + self.runs.retain(|k, _| k.0 != font_idx); + } +} + +/// Shape `text` with the font in `data` (first face of a collection). A font rustybuzz can't +/// parse gives no glyphs. +pub fn shape(data: &[u8], text: &str) -> Shaped { + let Some(face) = rustybuzz::Face::from_slice(data, 0) else { + return Shaped { + glyphs: Vec::new(), + units_per_em: 1000.0, + }; + }; + let mut buf = rustybuzz::UnicodeBuffer::new(); + buf.push_str(text); + buf.guess_segment_properties(); + let out = rustybuzz::shape(&face, &[], buf); + let glyphs = out + .glyph_infos() + .iter() + .zip(out.glyph_positions()) + .map(|(info, pos)| ShapedGlyph { + id: info.glyph_id as u16, + x_advance: pos.x_advance as f32, + x_offset: pos.x_offset as f32, + y_offset: pos.y_offset as f32, + }) + .collect(); + Shaped { + glyphs, + units_per_em: face.units_per_em() as f32, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_complex_scripts_are_shaped() { + assert!(!needs_shaping("Hello, Wörld! Tiếng Việt ไทย 日本語")); + assert!(needs_shaping("שלום")); + assert!(needs_shaping("مرحبا")); + assert!(needs_shaping("नमस्ते")); + assert!(needs_shaping("abc ﻻ")); + } + + #[test] + fn unparseable_font_shapes_to_nothing() { + let s = shape(&[0u8; 16], "שלום"); + assert!(s.glyphs.is_empty()); + } +} diff --git a/native/tvos/Cargo.lock b/native/tvos/Cargo.lock index 865744c2..6dae96e6 100644 --- a/native/tvos/Cargo.lock +++ b/native/tvos/Cargo.lock @@ -159,6 +159,7 @@ dependencies = [ "naga", "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1129,6 +1130,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1344,12 +1361,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2" diff --git a/native/visionos/Cargo.lock b/native/visionos/Cargo.lock index b2449846..9367ff9c 100644 --- a/native/visionos/Cargo.lock +++ b/native/visionos/Cargo.lock @@ -157,7 +157,9 @@ dependencies = [ "log", "minimp3", "naga", + "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1130,6 +1132,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1347,12 +1365,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2" diff --git a/native/web/Cargo.lock b/native/web/Cargo.lock index e0d3b1b0..9c562c35 100644 --- a/native/web/Cargo.lock +++ b/native/web/Cargo.lock @@ -156,7 +156,9 @@ dependencies = [ "libc", "log", "naga", + "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1111,6 +1113,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1317,12 +1335,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2" diff --git a/native/windows/Cargo.lock b/native/windows/Cargo.lock index cb20d09b..7d1573a0 100644 --- a/native/windows/Cargo.lock +++ b/native/windows/Cargo.lock @@ -157,7 +157,9 @@ dependencies = [ "log", "minimp3", "naga", + "objc2", "raw-window-handle", + "rustybuzz", "serde_json", "sha2", "web-sys", @@ -1128,6 +1130,22 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rustybuzz" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb9cf8877777222e4a3bc7eb247e398b56baba500c38c1c46842431adc8b55c" +dependencies = [ + "bitflags", + "bytemuck", + "smallvec", + "ttf-parser", + "unicode-bidi-mirroring", + "unicode-ccc", + "unicode-properties", + "unicode-script", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1345,12 +1363,36 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "unicode-bidi-mirroring" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cb788ffebc92c5948d0e997106233eeb1d8b9512f93f41651f52b6c5f5af86" + +[[package]] +name = "unicode-ccc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df77b101bcc4ea3d78dafc5ad7e4f58ceffe0b2b16bf446aeb50b6cb4157656" + [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993ad287eb20e8ba755312801cfe1413e0f25d604075863acaddca10b04c1833" + +[[package]] +name = "unicode-script" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa04c955db05429d464c74d495c1ee677e1866e783a4c3747f9c0cef7756cabf" + [[package]] name = "unicode-width" version = "0.2.2"