Skip to content

fix: normalize font-family in XML attributes instead of escaping#38

Open
OmChillure wants to merge 1 commit into1jehuang:masterfrom
OmChillure:fix-normalize-font-family-in-XML-attributes-instead-of-escaping
Open

fix: normalize font-family in XML attributes instead of escaping#38
OmChillure wants to merge 1 commit into1jehuang:masterfrom
OmChillure:fix-normalize-font-family-in-XML-attributes-instead-of-escaping

Conversation

@OmChillure
Copy link

@OmChillure OmChillure commented Feb 26, 2026

Issue :

When passing a configuration with quoted font names (e.g., "Inter", "ui-sans-serif", "system-ui", "-apple-system", "Segoe UI", sans-serif), SVG renderers like usvg fail to render any text in the diagram.

The previous fix attempted to
escape_xml the font-family, resulting in output like:

font-family=""Inter", "ui-sans-serif", ..." .
When an SVG parser (like usvg) reads this, it decodes " back to " and treats the entire comma-separated string as a single font name. It then fails to find a font named literally "Inter", "ui-sans-serif"... and skips rendering the text.

The Fix

Renamed normalize_font_family_css to normalize_font_family. This helper was already correctly removing quotes but was only being used in CSS <style> blocks.

Replaced
escape_xml(&theme.font_family) with normalize_font_family(&theme.font_family) in all XML attribute contexts.
Found and fixed several other locations where theme.font_family was being interpolated in raw form without any escaping/normalization.

Resulting Output

font-family="Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif"

Without the surrounding quotes on each font name, the XML structural integrity is maintained, and usvg can successfully parse the list and resolve the fallback fonts individually.

Testing

Verified all 136 cargo tests pass.
Rendered SVG via CLI with a heavily quoted font-family and confirmed clean output font-family="Inter, ui-sans-serif...".

CLI Testing

Rendered a simple diagram using a JSON config that passes the exact fontFamily string causing issues.

Command run:

echo 'graph TD
    A[Start] --> B[End]' \
| cargo run -- -i - -c <(echo '{"themeVariables":{"fontFamily":"\"Inter\", \"ui-sans-serif\", \"system-ui\", \"-apple-system\", \"Segoe UI\", sans-serif"}}') \
| grep -oP 'font-family="[^"]*"' | head -1

Result : font-family="Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif"

The output confirmed that all double quotes surrounding individual font names were successfully stripped, leaving a clean, comma-separated list of fonts that usvg can parse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant