-
Notifications
You must be signed in to change notification settings - Fork 756
Gallery Example: Add i18n and dynamic font loading for WASM #9762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
4b9375c
to
cba9573
Compare
- Bundle translations and detect browser language automatically - Load Noto Sans CJK fonts dynamically from GitHub at runtime - Add register_font_from_memory() API for runtime font registration - Export main() with #[wasm_bindgen] for explicit app startup control Fonts are loaded before app initialization to ensure proper CJK text rendering across all major browsers without bundling font files.
cba9573
to
77991b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for doing this.
This adds API and we should be careful to either make it properly internal, or have it public.
Ideally our examples shouldn't use the internal API as they should show the capability for everyone, so I would recommend we try to polish this API and make this public.
slint_build::compile_with_config( | ||
"gallery.slint", | ||
slint_build::CompilerConfiguration::new() | ||
.with_bundled_translations(concat!(env!("CARGO_MANIFEST_DIR"), "/lang/")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the printerdemo only does it for wasm and android.
slint/demos/printerdemo/rust/build.rs
Line 7 in 55a45a6
if target.contains("android") || target.contains("wasm32") { |
I think we should do the same.
software-renderer-systemfonts = ["renderer-software", "i-slint-core/software-renderer-systemfonts"] | ||
|
||
## Internal feature: Enable shared fontique font handling | ||
shared-fontique = ["i-slint-core/shared-fontique"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this feature is meant to be internal , it should not be documented and have a name that shows it.
Example: https://github.com/slint-ui/slint/blob/55a45a6c339223a19bcd79fc191e79193f495a79/api/rs/build/Cargo.toml#L28C1-L28C14
Maybe something like experimental-register-font
But actually, i'd rather not have experimental API like this enabled from the slint crate, but directly on the i-slint-core crate, and have app that want to use internal API to use the i-slint-core directly.
} | ||
|
||
/// Register a font from byte data dynamically. | ||
pub fn register_font_from_memory(font_data: Vec<u8>) -> usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just wondering what's the most efficient and future proof API.
Also in the light of #4250
I'm thinking we could have something like
fn register_font_from_memory(font_data : impl AsRef[u8] + Sync + Send /* + 'static ? */) -> Result<???>
That way we could also pass a &'static str
embedded in the binary or in mapped memory.
|
||
/// Register a font from memory. | ||
#[cfg(feature = "shared-fontique")] | ||
pub fn register_font_from_memory(font_data: alloc::vec::Vec<u8>) -> usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a re-export, it can be done with pub use
.
/// | ||
/// Returns the number of font families that were registered from the provided data. | ||
#[cfg(feature = "shared-fontique")] | ||
pub fn register_font_from_memory(font_data: Vec<u8>) -> usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just wondering what's the most efficient and future proof API.
Also in the light of #4250
I'm thinking we could have something like
fn register_font_from_memory(font_data : impl AsRef[u8] + Sync + Send /* + 'static ? */) -> Result<???, RegisterFontError>
That way we could also pass a &'static str
embedded in the binary or in mapped memory.
I see that you are returning the count, any reason to do that?
Maybe we could return some kind of FontHandle? Not sure what the use-case is to get access to that.
However, I think this could error for several reason (invalid format, unsupported by the backend or renderer, and stuff like that.)
We can also bike shed the name of this function.
|
||
let app = App::new().unwrap(); | ||
|
||
// For WASM builds, select translation after App::new() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably something we should do in i_slint_core instead.
slint/internal/core/translations.rs
Line 375 in 783f740
fn index_for_locale(languages: &[&'static str]) -> Option<usize> { |
Actually, we're already using the sys_locale
crate and it should support wasm. But we probably need to enable extra feature in it for it to work?
Fonts are loaded before app initialization to ensure proper CJK text rendering across all major browsers without bundling font files.