Skip to content

Conversation

@Wangnov
Copy link

@Wangnov Wangnov commented Dec 24, 2025

Summary

This PR adds support for loading system fonts when converting SVG to PNG, fixing the issue where CJK (Chinese, Japanese, Korean) and other non-ASCII characters are not rendered correctly in PNG output.

Changes

  • Add platform-specific font directory definitions using build tags:
    • dirs_darwin.go: macOS system font directories
    • dirs_linux.go: Linux system font directories
    • dirs_windows.go: Windows system font directories
    • dirs_other.go: fallback for unsupported platforms
  • Modify png.go to load fonts from system directories
  • Add FREEZE_NO_SYSTEM_FONTS=1 env var to skip system font loading for performance
  • Warn when font directories fail to load (visible at default log level)
  • Handle os.UserHomeDir() errors properly to avoid relative path issues

Performance Note

Font directories are scanned on each conversion. For better performance:

  • Set FREEZE_NO_SYSTEM_FONTS=1 to disable system font loading
  • Use SVG output instead of PNG
  • Install rsvg-convert (which handles fonts natively)

Testing

  • Tested cross-platform compilation (darwin, linux, windows, freebsd)
  • Tested PNG output with CJK characters on macOS

Related Issues

Fixes #93
Related to #83 (this PR follows the maintainer suggestion to use build tags instead of runtime checks)

This commit adds support for loading system fonts when converting SVG to PNG,
which fixes the issue where CJK and other non-ASCII characters are not rendered
correctly in PNG output.

Changes:
- Add platform-specific font directory definitions using build tags:
  - dirs_darwin.go: macOS system font directories
  - dirs_linux.go: Linux system font directories
  - dirs_windows.go: Windows system font directories
  - dirs_other.go: fallback for unsupported platforms
- Modify png.go to load fonts from system directories
- Add FREEZE_NO_SYSTEM_FONTS=1 env var to skip system font loading
- Warn when font directories fail to load (visible at default log level)
- Handle os.UserHomeDir() errors properly

Performance note: Font directories are scanned on each conversion. For better
performance, set FREEZE_NO_SYSTEM_FONTS=1, use SVG output, or install rsvg-convert.

Fixes charmbracelet#93
Related to charmbracelet#83
@Wangnov Wangnov requested a review from a team as a code owner December 24, 2025 04:39
@Wangnov Wangnov requested review from meowgorithm and tauraamui and removed request for a team December 24, 2025 04:39
@Delta456
Copy link

I tried your patch but this didn't work for me.

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.

The Chinese in the picture is garbled

2 participants