SVG text
Why Inkoxide lays out SVG text itself and draws it at probes.
Status: accepted, 2026-10-13.
Context
usvg converts SVG into the tree the PDF writer draws. Inkoxide builds it without its text
feature, so a <text> element converts to nothing, and SVG text used to fail the render.
Turning the feature on was not an option:
- it depends on harfrust 0.12, which waits, with the HarfRust migration (#83), for a release that includes harfrust#484;
- shaping inside
usvgwould run outside the render's glyph limit (MAX_EXTRA_GLYPHS), its fonts and its font fallback; usvgconverts text to outlines, which cannot be selected or searched; keeping it as text would still need harfrust.
Without the feature, usvg keeps nothing of a text element: its parsed tree (svgtree) is
private, and the converted tree has no text nodes. Whatever Inkoxide does with text has to
happen on the XML, before usvg, and has to survive usvg's conversion (use copies,
markers, patterns, masks, clip paths, switch, nested SVGs) to be drawn where the text was.
Decision
Inkoxide lays SVG text out with its own shaper and writes it as PDF text. Each <text> is
replaced, before usvg converts the file, by a placeholder: a "probe", a rectangle path over
the text's ink, or a group of probes, one per paint style of its spans.
- Reading (
images/svg_text/extract.rs, once per SVG, cached with it): the cascade of the text properties, ported fromusvg(presentation attributes, style sheets throughsimplecss,!important,style,inherit, thefontshorthand), white space afterxml:space, character positions and rotations, spans and their declared paint. - Layout (
text/svg.rs, per render): chunks shaped throughTextShaper::prepare, so font fallback, bidi, the shape cache, the glyph limit and ToUnicode apply as for any text; fonts chosen byFontSet::select, CSS font matching over the document's declared fonts. - Probes (
images/svg_text/rewrite.rs): each probe'sstyleholds the text's effects (transform, opacity, clip, mask, filter, display) and the paint its spans declare, sousvgresolves the paint the text would get, throughusecopies too. Where a style sheet declares a paint property or an effect, the probe declares it too (inherit, or the initial value), so that a rule about the probe's element cannot reach it.usvgapplies an!importantrule of a sheet overstyle(the first!importantdeclaration it meets wins), so a probe is apath, or apolygonwhen an!importantrule names paths; a group of probes is ana, or ag. The probe's geometry is the text's ink box, soobjectBoundingBoxpaint, clips, masks and group bounds resolve against it. - Recognition: the PDF writer finds a probe by its geometry, not its id, because
usvgdrops ids inusecopies and markers. Each probe's corners carry its number in their last bits (a few units in the last place), so they are unique within an SVG; a deliberate collision with a rectangle can only make an SVG draw its own text in place of one of its own rectangles. - Drawing (
pdf/svg.rs): a probe draws its text in its place, with its fill and stroke (gradients and patterns included) in paint order, as PDF text in the document's embedded fonts. Colour glyphs and emoji images (emojiImages) are images, in spans whoseActualTextis their text, as page text draws them (ColorGlyphs::push). An emoji image that is an SVG with text of its own is left out there, so that no form draws itself. - Per render: the SVG is converted again with the probes, under the same checks; the result is cached by the rewritten source, so renders whose fonts lay the text out the same way reuse it. It keeps the size of the SVG without its text: an SVG without a size takes it from what it draws, which would otherwise depend on the fonts.
- Limits: what a text's placeholder is (a probe, a group of them, bare probes in a clip path) depends on the text alone, not on its layout. So the decode builds the placeholders that cost most (every paint style, the widest geometry), counts their bytes before writing them, and runs the SVG checks on that document: a render's conversion costs the checks no more, and fails a limit only where validation does. The text nodes of a text count as elements.
Consequences
- SVG text is selectable, searchable and in the document's fonts, and does not wait for #83.
- The design depends on
usvginternals: how it parsesa, keeps invisible and empty groups, resolvesinheritand copies paths.usvgis pinned, and the probe survival suite (images/svg_text/survival.rs) checks every context on an upgrade. - Two cascades exist: ours for text properties,
usvg's for the rest. They can drift on edge cases; ours is a port ofusvg's, with the samesimplecssandsvgtypesversions. So do two readers of geometry: the paths atextPathfollows are read by ours (path.rs, with the samesvgtypes), the rest byusvg. - An SVG with text is converted again on each render that lays its text out differently.
- Not in this version: text that an XML entity expands to (refused, as all text was: the
placeholder replaces the element where it is in the source, and an entity's text is
somewhere else) and
tref. An author!importantrule that matches any element (*) applies to the text's paint, as it does in a browser; one that names bothpathandpolygonreaches the probes. - Text on a path and vertical text (along a path too), added since, fit the design unchanged.
The path a
textPathfollows is read from the XML with the text (usvgnever sees it, and a path in<defs>is not in its tree), once per element it refers to, and the probe covers the ink wherever the glyphs go. Each glyph on a curve, and each in a column, has its own text matrix, and the text of each paint style is written once asActualText, in logical order. font-stretchand font properties passed throughuse, also added since, stay in our cascade:font-stretchselects a face's width, and ausethat gives the text it names other font properties gets a copy of that element, laid out with theuse's cascade and written into the SVG under an id of its own (UseCopy, at most 64 an SVG). Ausethat an XML entity writes has itshrefin the DTD and gets no copy: the text keeps its own properties there.
Alternatives
usvg's text feature, converting text to outlines: not selectable, needs harfrust, shapes outside the glyph limit.usvg's text feature for layout only, with Inkoxide writing PDF text from its layout: still needs harfrust, and two shapers would disagree on glyphs.- Finding placeholders by id:
usvgdrops ids where it copies, so copies would be lost.
Synced from docs/adr/0001-svg-text-probes.md in the inkoxide repository.