Skip to content

Add support for unselectable text - #8902

Open
andylizi wants to merge 2 commits into
typst:mainfrom
andylizi:selectable-text-2
Open

andylizi wants to merge 2 commits into
typst:mainfrom
andylizi:selectable-text-2

Conversation

@andylizi

@andylizi andylizi commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Add a new parameter set text(selectable: false) that partially addresses #2249.

With the parameter set, the glyphs of a text run are drawn as outlined paths, making them unselectable in viewers. For accessibility, the text of the run is attached as /ActualText to a Span structure element that wraps the run's marked content in the tag tree.

This only covers page-level text. Text in SVGs is rendered by krilla-svg, which draws glyphs individually and cannot nest its own marked content for the text runs within Typst's marked content for the image. So there is currently nothing to attach the replacement text to. Supporting this would require changes in krilla, probably an API for krilla-svg to report its text runs back to the caller (or accept a callback that lets the caller tag each run), and I'd like to leave that as follow-up work.

The user-facing design was proposed in #5938 (comment). The implementation approach was discussed in #7295 (comment) with one major difference: it is done fully in Typst.

In the original proposal:

Then, krilla would automatically take care of writing the actual text (though I'm not familiar enough with the code to judge whether krilla could cleanly do this automatically if the /ActualText shall go into the tag tree instead of into the content stream.

If we do this fully in Typst (adding just a tag tree setter in krilla), I don't think there'd be a way to give text in SVGs the same treatment.

Unfortunately, doing this automatically in krilla turns out to be impractical. The tag tree is built entirely by the caller, and krilla only serializes the finished tree. For krilla to attach the replacement text itself, it would have to modify that tree after the fact, and it would have to infer where to insert the text from the marked content that was active when the glyphs were drawn, which krilla does not even track today. And there are more complications, such as what to do when several outlined runs share one marked content sequence, or when the caller already wraps its runs in Span elements for language and script attributes. So while it is technically possible, I don't think it is worth the complexity. It also would not help with the SVG case described above. CC @LaurenzV in case I'm missing something here!

We went down the more difficult path of placing /ActualText on structure elements due to broader viewer support, but there is mozilla/pdf.js#12237 for pdf.js to also support /ActualText in marked content properties. If and when that support becomes widespread, we can revisit this and move the /ActualText into the content stream, where krilla can write it during text rendering. That will be a lot cleaner to implement. The pdf.js issue also mentions that PDFium has added support for the marked content properties form as well, but I have not tested that myself.

Closes #2249 (we could open a follow-up issue for SVG images if needed).

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Oct 9, 2026
@andylizi
andylizi marked this pull request as ready for review November 1, 2026 18:40
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a way to make text non-selectable

2 participants