rich
rich copied to clipboard
[REQUEST] Add support for more characters for SVG formatting
Current status For now, Rich uses the the Fora Code as the font for exporting and saving the SVG.
Currently, this font does not support some kind of characters like Chinese or Japanese, which causes tofu blocks after exporting. The issue can be referred to here: https://github.com/tonsky/FiraCode/issues/620
A workaround is to specify the font that supports the Chinese in the CONSOLE_SVG_FORMAT.
However, another problem comes. Every character's width in FiraCode is 0.6, thus the tables or other formatting in the SVGs are in a mess. Because most of the fonts have width of 0.5.
Therefore, a solution is changing the hardcode width from 0.61 to 0.5.
https://github.com/Textualize/rich/blob/eaba86262174e2b12bb065f7269a425a2971585e/rich/console.py#L2309-L2311
How would you improve Rich?
we could change the font in the parameter but it would be hard to change the variable inside the lib. Will it be possible to help export the variable, or find other methods to solve it? Thanks in advance.
1. Export directly
from rich.console import Console
from rich.table import Table
table = Table(title="测试")
table.add_column("Title 测试")
table.add_column("Title 2 测试")
table.add_row("测试","test")
table.add_row("test","测试")
console = Console(record=True)
console.print(table)
console.save_svg('output.svg')

2. Export with specifying font in CONSOLE_SVG_FORMAT
Here I use Sarasa Mono SC which supports the Chinese characters
...
console.save_svg('output.svg',code_format=CONSOLE_SVG_FORMAT)
...

3. Export with specifying font in CONSOLE_SVG_FORMAT and change the char_width from 0.61 to 0.5

Yes, we would like to support CJK at all times. Will look in to that.
Hey @onlyacat, I've added a font_aspect_ratio to export_svg and save_svg. If you use a custom font, you can use this new parameter to modify the default 0.61 value. That'll be available in the next release.
Hey @onlyacat, I've added a
font_aspect_ratiotoexport_svgandsave_svg. If you use a custom font, you can use this new parameter to modify the default0.61value. That'll be available in the next release.
help me a lot, thanks!