Skip to content

Fix invisible mermaid labels on Linux (font fallback) - #108

Closed
lazy-dinosaur wants to merge 1 commit into
1jehuang:masterfrom
lazy-dinosaur:fix/mermaid-terminal-theme-font-fallback
Closed

lazy-dinosaur wants to merge 1 commit into
1jehuang:masterfrom
lazy-dinosaur:fix/mermaid-terminal-theme-font-fallback

Conversation

@lazy-dinosaur

@lazy-dinosaur lazy-dinosaur commented May 3, 2026 •

Copy link
Copy Markdown

PR Body Draft

Summary

On Linux, mermaid diagrams rendered by jcode show only the box outlines and arrows — node labels are invisible. This PR adds Linux-native font fallbacks (DejaVu Sans, Liberation Sans) to terminal_theme() so the bundled resvg/usvg renderer can resolve a real font face for SVG <text> elements.

Reproduction (before fix)

graph LR
    A[Start] --> B[Middle] --> C[End]
Loading

On Arch Linux + kitty + tmux + jcode v0.11.9, the rendered diagram pinned in the side panel shows three box outlines connected by arrows but no text inside the boxes.

Verified on:

  • jcode v0.11.9 (release build) and v0.11.6-dev (built from f071bc2a)
  • resvg 0.46, usvg 0.46
  • Linux fonts present: DejaVu Sans, Liberation Sans, Noto Sans CJK KR, Noto Color Emoji, etc.

Root cause

crates/jcode-tui-mermaid/src/mermaid_content.rs::terminal_theme() declares the SVG font-family as:

Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif

mermaid-rs-renderer emits real SVG <text> elements with this font-family verbatim. resvg/usvg's embedded fontdb then tries to resolve those names:

Family requested Linux match
Inter not installed by default
ui-sans-serif not a real face name
system-ui not a real face name
-apple-system macOS only
Segoe UI Windows only
sans-serif resvg's fontdb does not resolve generic CSS families

→ resvg falls through every name and emits Warning (in usvg::text:129): No match for '...' font-family. Then it skips drawing the text glyphs entirely while still drawing the rect/path/marker elements. Result: empty boxes.

I verified this by:

  1. Generating an SVG with the original font list and feeding it through /usr/bin/resvg directly → identical empty output, identical OCR (zero text).
  2. Replacing the font-family with "DejaVu Sans" → resvg renders text correctly, OCR picks up "Start", "Middle", "End".
  3. Confirming mermaid-rs-renderer itself emits proper SVG <text> with the labels — the SVG is fine, the renderer's fontdb is the bottleneck.

This is not the same as the well-known mermaid <foreignObject>/htmlLabels issue, since mermaid-rs-renderer (a pure Rust mermaid implementation, not mmdc) emits real SVG <text> elements rather than embedded HTML.

Fix

Add DejaVu Sans and Liberation Sans between Segoe UI and sans-serif. These are present on essentially every Linux desktop install (Debian/Ubuntu/Fedora/Arch/openSUSE all ship one or both via base font packages), and are absent on macOS/Windows so the existing platform-preferred families still win there.

-font_family: "Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif"
+font_family: "Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, \"DejaVu Sans\", \"Liberation Sans\", sans-serif"

After fix

Same diagram, same machine, with the patched binary (v0.11.6-dev (dirty)):

  • All three box labels visible
  • No usvg::text warnings in stderr
  • Rendered PNG OCR ("Start", "Middle", "End")

Notes / alternatives considered

  • fontconfig alias (trebuchet ms → DejaVu Sans etc.) does not work because usvg::fontdb::Database::load_system_fonts() only scans font directories, it does not interpret fontconfig alias rules.
  • Bundling Inter in jcode would be more invasive (binary size, license review).
  • Patching mermaid-rs-renderer has analogous font-fallback issues in its Theme::mermaid_default() and Theme::modern() — happy to send a follow-up PR there too if helpful.
  • Not changing the order before Inter keeps macOS/Windows behavior unchanged: Inter/Segoe UI/-apple-system still win first when present.

Test plan

  • cargo build --release (nightly toolchain) succeeds
  • OCR of rendered cache PNG goes from empty → readable
  • No regression in other diagram types (verified against tests/fixtures/* corpus via mmdr CLI)
  • macOS/Windows behavior unchanged in principle (DejaVu/Liberation only chosen when prior fonts unavailable)

View in Codesmith
Need help on this PR? Tag @codesmith with what you need.

  • Let Codesmith autofix CI failures and bot reviews

terminal_theme() requested only generic CSS font families (Inter,
ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif). On
Linux, none of those resolve to a real face in usvg/resvg's bundled
fontdb (it does not honour the 'sans-serif' generic alias and only
indexes face names from font directories). resvg therefore drops
every <text> glyph silently, leaving boxes and arrows but no labels.

Insert DejaVu Sans and Liberation Sans before the generic
sans-serif fallback. Both ship by default on virtually every Linux
desktop install and are absent on macOS/Windows, so platform-native
families (Inter/Segoe UI/-apple-system) still win first there.
zombi3butt pushed a commit to zombi3butt/jcode that referenced this pull request May 22, 2026
The terminal mermaid theme set `font_family` to
"Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif".
On most desktop Linux distributions none of those names resolve to an
installed font (Inter is rare, ui-sans-serif/system-ui are CSS-only,
Segoe UI is Windows-only), so resvg's text rasterizer fell back to a
font that lacked the glyphs jcode actually emits in node labels —
labels rendered invisible inside the rendered PNG.

Append "DejaVu Sans" and "Liberation Sans" to the family list. Both
are part of `fonts-dejavu-core` / `fonts-liberation` which are pulled
in by virtually every desktop Linux install. macOS and Windows still
hit `-apple-system` / `Segoe UI` first, so this is purely additive.

Ports upstream PR 1jehuang#108.
Closes 1jehuang#92
@1jehuang 1jehuang added the triage: converted PR triage: worth tracking as an issue label Jun 12, 2026
@1jehuang

Copy link
Copy Markdown
Owner

Thanks! This repo no longer accepts external PRs, but the underlying problem is real - it's now tracked in #371 (with credit to this PR). Closing.

@1jehuang 1jehuang closed this Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage: converted PR triage: worth tracking as an issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants