wasm-interpreter: register a monospace face at startup - #12953
Open
thelipe7 wants to merge 1 commit into
Open
Conversation
Styled text's Code style asks for GenericFamily::Monospace directly. On a platform with no system fonts that generic resolves to no family at all, so a code span renders in the proportional face -- the only face there is. Mapping it in the shared collection would put a programming font in every application compiled for wasm, and most of them never draw code. Add register_monospace_font to i-slint-common instead, which create_collection deliberately doesn't call, and call it from the interpreter's init: the interpreter renders snippets it didn't write, whose authors have no way to supply a font themselves. Measured on the printerdemo wasm build: an application grows by 41 bytes, because the linker drops the uncalled function along with the font it embeds. The interpreter grows by the 133 KB it now carries. The unit test asserts the monospace generics resolve to nothing until register_monospace_font runs, so moving the font back into the shared collection fails the test rather than passing silently.
thelipe7
force-pushed
the
embed-monospace-in-wasm-interpreter
branch
from
August 20, 2026 14:10
77cc5d0 to
fd11bbd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Styled text's
Codestyle asks forGenericFamily::Monospacedirectly(
internal/core/textlayout/sharedparley/shaping.rs:201). On a platform with nosystem fonts to query — wasm, nto — the embedded set covers
SansSerif,SystemUiandUiSansSerifand nothing else, so that request resolves to nofamily at all and the code span renders in the proportional face.
A snippet author hits this in any browser preview, and has no way to fix it: the
font has to come from somewhere in the binary.
Change
The obvious fix — registering a monospace face in
create_collection— puts aprogramming font in every application compiled for wasm, and most never draw
code. So
create_collectiondeliberately doesn't do it.Instead,
i-slint-commongainsregister_monospace_font(), which nothing in thecore calls, and the wasm interpreter calls it from
init(). The interpreter iswhere snippets it didn't write get rendered, so it is the binary that should
carry the font.
Measured on the printerdemo wasm release build:
create_collectioninsteadThe 41 bytes are symbol noise: the linker drops the uncalled function along with
the font it embeds. The interpreter carries the 133 KB, 0.9% of its 14.3 MB.
Source Code Pro is already vendored for the LSP, so this adds no new third-party
dependency.
Validation
cargo test -p i-slint-common --all-features— two tests. The monospace oneasserts the generics resolve to nothing before
register_monospace_fontruns, so moving the font back into the shared collection fails the test rather
than passing silently.
cargo check -p slint-wasm-interpreter --target wasm32-unknown-unknown,cargo test -p i-slint-core.wasm-pack --releasebuild: aStyledTextcode spanrenders monospaced, and an unknown family still falls back to the sans.
Notes for review
rather than a real bold. A variable mono would fix that at roughly 3–5× the
bytes. Happy to swap if you'd prefer the fidelity over the size.
Left out here so this PR proposes one shape in one place; happy to follow up.
proportional. That is the deliberate trade above — it seemed wrong to charge
every application for a font most don't use. Say the word if you'd rather
cover them too.