rich icon indicating copy to clipboard operation
rich copied to clipboard

[REQUEST] Add support for more characters for SVG formatting

Open onlyacat opened this issue 3 years ago • 2 comments

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.

onlyacat avatar Sep 19 '22 06:09 onlyacat

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')

image

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)
...

image

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

image

onlyacat avatar Sep 19 '22 07:09 onlyacat

Yes, we would like to support CJK at all times. Will look in to that.

willmcgugan avatar Sep 19 '22 13:09 willmcgugan

Did I solve your problem?

Why not buy the devs a coffee to say thanks?

github-actions[bot] avatar Sep 23 '22 11:09 github-actions[bot]

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.

darrenburns avatar Sep 23 '22 11:09 darrenburns

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.

help me a lot, thanks!

onlyacat avatar Sep 23 '22 13:09 onlyacat